diff options
author | Anton Luka Šijanec <anton@sijanec.eu> | 2024-08-21 15:18:29 +0200 |
---|---|---|
committer | Anton Luka Šijanec <anton@sijanec.eu> | 2024-08-21 15:18:29 +0200 |
commit | e54c16b1c0c8b1180c5874166de45f61c62385d6 (patch) | |
tree | 62f63865a97a8ce3998c9de7f307bda29fcfeaba /iv/orodja/ldmitm/tcp_times_example.c | |
parent | ldmitm (diff) | |
download | r-e54c16b1c0c8b1180c5874166de45f61c62385d6.tar r-e54c16b1c0c8b1180c5874166de45f61c62385d6.tar.gz r-e54c16b1c0c8b1180c5874166de45f61c62385d6.tar.bz2 r-e54c16b1c0c8b1180c5874166de45f61c62385d6.tar.lz r-e54c16b1c0c8b1180c5874166de45f61c62385d6.tar.xz r-e54c16b1c0c8b1180c5874166de45f61c62385d6.tar.zst r-e54c16b1c0c8b1180c5874166de45f61c62385d6.zip |
Diffstat (limited to 'iv/orodja/ldmitm/tcp_times_example.c')
-rw-r--r-- | iv/orodja/ldmitm/tcp_times_example.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/iv/orodja/ldmitm/tcp_times_example.c b/iv/orodja/ldmitm/tcp_times_example.c new file mode 100644 index 0000000..c98b29a --- /dev/null +++ b/iv/orodja/ldmitm/tcp_times_example.c @@ -0,0 +1,56 @@ +/* +Posluša na TCP vratih 6969, prejme eno povezavo, vsako sekundo nanjo izpiše LF in piše statistiko, dobljeno iz jedrnega modula tcp_times. +*/ +#include <stdint.h> +#include "tcp_times.h" +#include <stdio.h> +#include <stdlib.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <netinet/tcp.h> +#include <unistd.h> +#include <stdbool.h> +#include <sys/ioctl.h> +#include <fcntl.h> +int main (void) { + int tcp_socket = socket(AF_INET6, SOCK_STREAM, 0); + if (tcp_socket == -1) { + perror("socket"); + return 1; + } + struct sockaddr_in6 sa6 = { + .sin6_family = AF_INET6, + .sin6_port = htons(6969), + .sin6_addr = IN6ADDR_ANY_INIT, + }; + if (bind(tcp_socket, (struct sockaddr *) &sa6, sizeof sa6) == -1) { + perror("bind"); + return 1; + } + if (listen(tcp_socket, 1 /* only one client is handled*/) == -1) { + perror("listen"); + return 1; + } + int flow = accept(tcp_socket, NULL, NULL); + if (flow == -1) { + perror("accept"); + return 1; + } + int tcp_times = open("/proc/tcp_times", O_RDWR); + struct tcp_times tt = { + .fd = flow, + }; + char buf = '\n'; + while (true) { + if (ioctl(tcp_times, 0, &tt) == -1) { + perror("ioctl"); + return 1; + } + printf(TCP_TIMES_PRINTF_FORMAT "\n", TCP_TIMES_PRINTF_VARIABLES(tt.)); + if (send(flow, &buf, 1, MSG_NOSIGNAL) == -1) { + perror("write"); + return 1; + } + sleep(1); + } +} |