summaryrefslogtreecommitdiff
path: root/clientd.c
diff options
context:
space:
mode:
Diffstat (limited to 'clientd.c')
-rw-r--r--clientd.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/clientd.c b/clientd.c
new file mode 100644
index 0000000..a5e4d75
--- /dev/null
+++ b/clientd.c
@@ -0,0 +1,49 @@
+#define _GNU_SOURCE // getline
+#include <stdio.h>
+#include <stddef.h>
+#include <string.h>
+#include <sys/select.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/ip_icmp.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <errno.h>
+#include <assert.h>
+#include "icmpd.h"
+#include "util.h"
+
+int main(void) {
+ struct icmpd *d = icmpd_create_client(inet_addr("127.0.0.1"));
+
+ while (true) {
+ if (icmpd_peek(d)) {
+ struct icmpd_received msg = icmpd_recv(d);
+ printf("Recv: %zu\n", msg.length);
+ xxd(msg.data, msg.length);
+ }
+
+ fd_set inset;
+ FD_ZERO(&inset);
+ FD_SET(0, &inset);
+ struct timeval tv;
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
+ int ret = select(1, &inset, NULL, NULL, &tv);
+ if (ret < 0) {
+ if (errno == EINTR) continue;
+ perror("select");
+ return 1;
+ }
+ if (ret == 0) continue; // timeout
+ if (FD_ISSET(0, &inset)) {
+ char *line = NULL;
+ size_t linelen = 0;
+ ssize_t nr = getline(&line, &linelen, stdin);
+ assert(nr >= 0);
+ if (nr > 0) nr--;
+ icmpd_send(d, line, nr);
+ free(line);
+ }
+ }
+}