diff options
author | kartofen <mladenovnasko0@gmail.com> | 2022-10-22 21:08:08 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2022-10-22 21:08:08 +0300 |
commit | f4c66a4c77e5818ca9703f59a80ea92e27ed45c6 (patch) | |
tree | e334b3f1c8bdb5c96bd756be4e451836a7762ab4 /src/socket.c | |
parent | 9d88d471ac059c93523c00b96ae1cbbc52e07b69 (diff) |
abstract messages for easier use
Diffstat (limited to 'src/socket.c')
-rw-r--r-- | src/socket.c | 18 |
1 files changed, 9 insertions, 9 deletions
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; |