aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/net.c
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2022-08-30 13:38:05 +0200
committerEgor Tensin <Egor.Tensin@gmail.com>2022-08-30 13:50:17 +0200
commit40ce7bbfae7332b05eac7aa110241fc7b589fd70 (patch)
tree896a3c45f9da8e0115703434cfe4dffee476bdeb /src/net.c
parentnet: bind to IPv6 (diff)
downloadcimple-40ce7bbfae7332b05eac7aa110241fc7b589fd70.tar.gz
cimple-40ce7bbfae7332b05eac7aa110241fc7b589fd70.zip
net: more portable
Use a predefined byte order, integers with fixed width, etc.
Diffstat (limited to '')
-rw-r--r--src/net.c13
1 files changed, 9 insertions, 4 deletions
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 <netdb.h>
+#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
@@ -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;
}