From 4450f06082ea6e337762f9acdfefd012f815411a Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sun, 12 Aug 2018 12:41:35 +0200 Subject: Rename {client,server}d to {client,server} --- Makefile | 2 +- client.c | 58 ++++++++++++++++++++++++++++++++++++++++++++ clientd.c | 58 -------------------------------------------- server.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ serverd.c | 83 --------------------------------------------------------------- 5 files changed, 142 insertions(+), 142 deletions(-) create mode 100644 client.c delete mode 100644 clientd.c create mode 100644 server.c delete mode 100644 serverd.c diff --git a/Makefile b/Makefile index cbdd2f3..1e2834c 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC = gcc CFLAGS = -Wall -Wextra -O2 -g -std=c11 -fwrapv -pthread LDFLAGS = -pthread -TARGETS = serverd clientd +TARGETS = server client SOURCES = $(filter-out $(patsubst %,%.c,$(TARGETS)),$(wildcard *.c)) OBJECTS = $(patsubst %.c,%.o,$(SOURCES)) diff --git a/client.c b/client.c new file mode 100644 index 0000000..0f8bde1 --- /dev/null +++ b/client.c @@ -0,0 +1,58 @@ +#define _GNU_SOURCE // getline +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "icmpd.h" +#include "util.h" + +int main(void) { + const char *remote_addr = "127.0.0.1"; + // const char *remote_addr = "198.211.118.67"; + struct icmpd *d = icmpd_create_client(inet_addr(remote_addr)); + int d_fd = icmpd_get_select_fd(d); + + while (true) { + fd_set inset; + FD_ZERO(&inset); + FD_SET(0, &inset); + FD_SET(d_fd, &inset); + + int ret = select(d_fd + 1, &inset, NULL, NULL, NULL); + 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; + errno = 0; + ssize_t nr = getline(&line, &linelen, stdin); + if (nr < 0) { + if (errno == 0) break; // EOF + perror("getline"); + exit(1); + } + if (nr > 0) nr--; + icmpd_send(d, line, nr); + free(line); + } + + if (FD_ISSET(d_fd, &inset) && icmpd_peek(d)) { + struct icmpd_received msg = icmpd_recv(d); + printf("Recv: %zu\n", msg.length); + xxd(msg.data, msg.length); + } + } +} diff --git a/clientd.c b/clientd.c deleted file mode 100644 index 0f8bde1..0000000 --- a/clientd.c +++ /dev/null @@ -1,58 +0,0 @@ -#define _GNU_SOURCE // getline -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "icmpd.h" -#include "util.h" - -int main(void) { - const char *remote_addr = "127.0.0.1"; - // const char *remote_addr = "198.211.118.67"; - struct icmpd *d = icmpd_create_client(inet_addr(remote_addr)); - int d_fd = icmpd_get_select_fd(d); - - while (true) { - fd_set inset; - FD_ZERO(&inset); - FD_SET(0, &inset); - FD_SET(d_fd, &inset); - - int ret = select(d_fd + 1, &inset, NULL, NULL, NULL); - 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; - errno = 0; - ssize_t nr = getline(&line, &linelen, stdin); - if (nr < 0) { - if (errno == 0) break; // EOF - perror("getline"); - exit(1); - } - if (nr > 0) nr--; - icmpd_send(d, line, nr); - free(line); - } - - if (FD_ISSET(d_fd, &inset) && icmpd_peek(d)) { - struct icmpd_received msg = icmpd_recv(d); - printf("Recv: %zu\n", msg.length); - xxd(msg.data, msg.length); - } - } -} diff --git a/server.c b/server.c new file mode 100644 index 0000000..b99e4ea --- /dev/null +++ b/server.c @@ -0,0 +1,83 @@ +#define _GNU_SOURCE // usleep, getline +#include +#include +#include +#include +#include +#include +#include +#include +#include "icmpd.h" +#include "util.h" + + +int main(void) { + struct icmpd *server = icmpd_create_server(-1, 0); + struct icmpd *conn = NULL; + + int server_fd = icmpd_get_select_fd(server); + int conn_fd = -1; + + while (true) { + fd_set inset; + FD_ZERO(&inset); + FD_SET(0, &inset); + FD_SET(server_fd, &inset); + if (conn) FD_SET(conn_fd, &inset); + int nfds = maxi(0, maxi(server_fd, conn_fd)) + 1; + + int ret = select(nfds, &inset, NULL, NULL, NULL); + 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; + errno = 0; + ssize_t nr = getline(&line, &linelen, stdin); + if (nr < 0) { + if (errno == 0) break; // EOF + perror("getline"); + exit(1); + } + if (nr > 0) nr--; + icmpd_send(conn, line, nr); + free(line); + } + + if (FD_ISSET(server_fd, &inset) && 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); + conn_fd = icmpd_get_select_fd(conn); + + // icmpd_send(conn, "dankje", 6); + // icmpd_destroy(conn); + } + + free(msg.data); + } + + if (conn && FD_ISSET(conn_fd, &inset) && 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); + } + } +} diff --git a/serverd.c b/serverd.c deleted file mode 100644 index b99e4ea..0000000 --- a/serverd.c +++ /dev/null @@ -1,83 +0,0 @@ -#define _GNU_SOURCE // usleep, getline -#include -#include -#include -#include -#include -#include -#include -#include -#include "icmpd.h" -#include "util.h" - - -int main(void) { - struct icmpd *server = icmpd_create_server(-1, 0); - struct icmpd *conn = NULL; - - int server_fd = icmpd_get_select_fd(server); - int conn_fd = -1; - - while (true) { - fd_set inset; - FD_ZERO(&inset); - FD_SET(0, &inset); - FD_SET(server_fd, &inset); - if (conn) FD_SET(conn_fd, &inset); - int nfds = maxi(0, maxi(server_fd, conn_fd)) + 1; - - int ret = select(nfds, &inset, NULL, NULL, NULL); - 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; - errno = 0; - ssize_t nr = getline(&line, &linelen, stdin); - if (nr < 0) { - if (errno == 0) break; // EOF - perror("getline"); - exit(1); - } - if (nr > 0) nr--; - icmpd_send(conn, line, nr); - free(line); - } - - if (FD_ISSET(server_fd, &inset) && 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); - conn_fd = icmpd_get_select_fd(conn); - - // icmpd_send(conn, "dankje", 6); - // icmpd_destroy(conn); - } - - free(msg.data); - } - - if (conn && FD_ISSET(conn_fd, &inset) && 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); - } - } -} -- cgit v1.2.3-70-g09d2