From f4c66a4c77e5818ca9703f59a80ea92e27ed45c6 Mon Sep 17 00:00:00 2001 From: kartofen Date: Sat, 22 Oct 2022 21:08:08 +0300 Subject: abstract messages for easier use --- src/socket.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/socket.c') diff --git a/src/socket.c b/src/socket.c index 2ce4900..2efe53a 100644 --- a/src/socket.c +++ b/src/socket.c @@ -21,7 +21,7 @@ void *get_in_addr(struct sockaddr *sa) return &(((struct sockaddr_in6*)sa)->sin6_addr); } -int listener(char *port, void (*on_recv)(char *, int)) +int listener(char *port, void (*on_recv)(message *)) { int sockfd; int ret; @@ -73,17 +73,18 @@ int listener(char *port, void (*on_recv)(char *, int)) i = 0; } - char buf[BUF_CAP] = {0}; + message m = {0}; struct sockaddr_storage their_addr; socklen_t addr_len = sizeof(their_addr); int numbytes; - if ((numbytes = recvfrom(sockfd, buf, BUF_CAP-1 , 0, + if((numbytes = recvfrom(sockfd, &m, BUF_CAP, 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) { perror("recvfrom"); return 1; } + #ifdef LOG_INFO_STDOUT char s[INET6_ADDRSTRLEN]; info("listener: got packet from %s, %d bytes long\n", @@ -92,7 +93,7 @@ int listener(char *port, void (*on_recv)(char *, int)) s, sizeof(s)), numbytes); #endif - on_recv(buf, numbytes); + on_recv(&m); i++; } @@ -100,7 +101,7 @@ int listener(char *port, void (*on_recv)(char *, int)) return 0; } -int talker(char *port, char *address, void (*on_send)(char *, int *)) +int talker(char *port, char *address, void (*on_send)(message *)) { int sockfd; @@ -142,12 +143,11 @@ int talker(char *port, char *address, void (*on_send)(char *, int *)) i = 0; } - char buf[BUF_CAP] = {0}; - int bytes; - on_send(buf, &bytes); + message m = {0}; + on_send(&m); int numbytes; - if((numbytes = sendto(sockfd, buf, bytes, 0, + if((numbytes = sendto(sockfd, &m, MESSAGE_SZ, 0, p->ai_addr, p->ai_addrlen)) == -1) { perror("talker: sendto"); return 1; -- cgit v1.2.3