summaryrefslogtreecommitdiff
path: root/serverd.c
diff options
context:
space:
mode:
Diffstat (limited to 'serverd.c')
-rw-r--r--serverd.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/serverd.c b/serverd.c
new file mode 100644
index 0000000..6f21530
--- /dev/null
+++ b/serverd.c
@@ -0,0 +1,73 @@
+#define _GNU_SOURCE // usleep, getline
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <sys/select.h>
+#include <unistd.h>
+#include <errno.h>
+#include <assert.h>
+#include "icmpd.h"
+#include "util.h"
+
+
+int main(void) {
+ struct icmpd *server = icmpd_create_server(-1, 0);
+ struct icmpd *conn = NULL;
+
+ while (true) {
+ // printf("Iter\n");
+
+ if (icmpd_peek(server)) {
+ struct icmpd_received msg = icmpd_recv(server);
+ printf("Server recv: %zu\n", msg.length);
+ xxd(msg.data, msg.length);
+
+ if (conn) {
+ printf("Message received while already connected\n");
+ struct icmpd *d = icmpd_create_server(msg.id, msg.source_addr);
+ icmpd_server_set_outstanding(d, msg.seqnum);
+ icmpd_send(d, msg.data, msg.length);
+ icmpd_destroy(d);
+ } else {
+ conn = icmpd_create_server(msg.id, msg.source_addr);
+ icmpd_server_set_outstanding(conn, msg.seqnum);
+
+ // icmpd_send(conn, "dankje", 6);
+ // icmpd_destroy(conn);
+ }
+
+ free(msg.data);
+ }
+
+ if (icmpd_peek(conn)) {
+ struct icmpd_received msg = icmpd_recv(conn);
+ printf("Connection recv: %zu\n", msg.length);
+ xxd(msg.data, msg.length);
+ free(msg.data);
+ }
+
+ 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(conn, line, nr);
+ free(line);
+ }
+ }
+}