From 40ce7bbfae7332b05eac7aa110241fc7b589fd70 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 30 Aug 2022 13:38:05 +0200 Subject: net: more portable Use a predefined byte order, integers with fixed width, etc. --- src/msg.c | 9 +++++---- src/net.c | 13 +++++++++---- src/net.h | 5 +++-- 3 files changed, 17 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/msg.c b/src/msg.c index d1f3b5e..d9e6c3b 100644 --- a/src/msg.c +++ b/src/msg.c @@ -2,6 +2,7 @@ #include "log.h" #include "net.h" +#include #include #include @@ -94,9 +95,9 @@ int msg_from_argv(struct msg *msg, char **argv) return msg_copy_argv(msg, argv); } -static size_t calc_buf_len(const struct msg *msg) +static uint32_t calc_buf_len(const struct msg *msg) { - size_t len = 0; + uint32_t len = 0; for (int i = 0; i < msg->argc; ++i) len += strlen(msg->argv[i]) + 1; return len; @@ -151,7 +152,7 @@ int msg_send(int fd, const struct msg *msg) { int ret = 0; - size_t len = calc_buf_len(msg); + uint32_t len = calc_buf_len(msg); char *buf = malloc(len); if (!buf) { print_errno("malloc"); @@ -187,7 +188,7 @@ int msg_send_and_wait(int fd, const struct msg *request, struct msg *response) int msg_recv(int fd, struct msg *msg) { void *buf; - size_t len; + uint32_t len; int ret = 0; ret = net_recv_buf(fd, &buf, &len); diff --git a/src/net.c b/src/net.c index 534807c..f819af3 100644 --- a/src/net.c +++ b/src/net.c @@ -2,6 +2,7 @@ #include "log.h" #include +#include #include #include #include @@ -185,13 +186,15 @@ ssize_t net_recv_all(int fd, void *buf, size_t len) return read_total; } -int net_send_buf(int fd, const void *buf, size_t len) +int net_send_buf(int fd, const void *buf, uint32_t len) { int ret = 0; + len = htonl(len); ret = net_send_all(fd, &len, sizeof(len)); if (ret < 0) return ret; + len = ntohl(len); ret = net_send_all(fd, buf, len); if (ret < 0) @@ -200,7 +203,7 @@ int net_send_buf(int fd, const void *buf, size_t len) return ret; } -int net_recv_buf(int fd, void **buf, size_t *len) +int net_recv_buf(int fd, void **buf, uint32_t *len) { ssize_t nb = 0; @@ -213,6 +216,8 @@ int net_recv_buf(int fd, void **buf, size_t *len) goto fail; } + *len = ntohl(*len); + *buf = malloc(*len); if (!*buf) { print_errno("malloc"); @@ -240,7 +245,7 @@ fail: int net_recv_static(int fd, void *buf, size_t len) { void *actual_buf; - size_t actual_len; + uint32_t actual_len; int ret = 0; ret = net_recv_buf(fd, &actual_buf, &actual_len); @@ -248,7 +253,7 @@ int net_recv_static(int fd, void *buf, size_t len) return ret; if (actual_len != len) { - print_error("Expected message length: %lu, actual: %lu\n", len, actual_len); + print_error("Expected message length: %lu, actual: %u\n", len, actual_len); ret = -1; goto free_buf; } diff --git a/src/net.h b/src/net.h index 125d4f3..5afe4b7 100644 --- a/src/net.h +++ b/src/net.h @@ -1,6 +1,7 @@ #ifndef __NET_H__ #define __NET_H__ +#include #include #include @@ -9,10 +10,10 @@ int net_accept(int fd); int net_connect(const char *host, const char *port); int net_send_all(int fd, const void *, size_t); -int net_send_buf(int fd, const void *, size_t); +int net_send_buf(int fd, const void *, uint32_t); ssize_t net_recv_all(int fd, void *, size_t); -int net_recv_buf(int fd, void **, size_t *); +int net_recv_buf(int fd, void **, uint32_t *); int net_recv_static(int fd, void *, size_t); #endif -- cgit v1.2.3