From a7453e30ca4c9f42d27d42f96bc223533c330900 Mon Sep 17 00:00:00 2001 From: John Ogness Date: Fri, 1 Feb 2019 17:02:02 +0106 Subject: ipc_tcp: add new ipc example using loopback sockets This demo should (hopefully serve) as a basis for overlaying pcap and ftrace data within trace compass. Signed-off-by: John Ogness --- schulung_tools/ipc_tcp/recv.c | 68 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 schulung_tools/ipc_tcp/recv.c (limited to 'schulung_tools/ipc_tcp/recv.c') diff --git a/schulung_tools/ipc_tcp/recv.c b/schulung_tools/ipc_tcp/recv.c new file mode 100644 index 0000000..bd76f20 --- /dev/null +++ b/schulung_tools/ipc_tcp/recv.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +static char buffer[1024 * 1024]; + +int main(void) +{ + struct sockaddr_in sa; + size_t count = 0; + int listen_s; + size_t size; + int rv; + int s; + + listen_s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (listen_s < 0) + return 1; + memset(&sa, 0, sizeof(sa)); + sa.sin_family = AF_INET; + sa.sin_addr.s_addr = inet_addr("127.0.0.1"); + sa.sin_port = htons(8806); + + rv = 1; + if (setsockopt(listen_s, SOL_SOCKET, SO_REUSEADDR, + (void *)&rv, sizeof(rv)) < 0) { + return 1; + } + + if (bind(listen_s, (struct sockaddr *)&sa, sizeof(sa)) < 0) { + fprintf(stderr, "bind() failed: %s\n", strerror(errno)); + return 1; + } + + if (listen(listen_s, 1) != 0) + return 1; + + s = accept(listen_s, NULL, NULL); + if (s < 0) + return 1; + + if (read(s, &size, sizeof(size)) != sizeof(size)) + return 1; + + if (size >= sizeof(buffer)) + size = sizeof(buffer) - 1; + + while (count < size) { + rv = read(s, buffer + count, size - count); + if (rv <= 0) + return 1; + count += rv; + } + + /* set "received" uprobe here */ + + printf("recv (%d): received message: %s\n", getpid(), buffer); + + close(s); + close(listen_s); + + return 0; +} -- cgit v1.2.3