diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2022-08-30 13:38:05 +0200 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2022-08-30 13:50:17 +0200 |
commit | 40ce7bbfae7332b05eac7aa110241fc7b589fd70 (patch) | |
tree | 896a3c45f9da8e0115703434cfe4dffee476bdeb /src | |
parent | net: bind to IPv6 (diff) | |
download | cimple-40ce7bbfae7332b05eac7aa110241fc7b589fd70.tar.gz cimple-40ce7bbfae7332b05eac7aa110241fc7b589fd70.zip |
net: more portable
Use a predefined byte order, integers with fixed width, etc.
Diffstat (limited to 'src')
-rw-r--r-- | src/msg.c | 9 | ||||
-rw-r--r-- | src/net.c | 13 | ||||
-rw-r--r-- | src/net.h | 5 |
3 files changed, 17 insertions, 10 deletions
@@ -2,6 +2,7 @@ #include "log.h" #include "net.h" +#include <stdint.h> #include <stdlib.h> #include <string.h> @@ -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); @@ -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; } @@ -1,6 +1,7 @@ #ifndef __NET_H__ #define __NET_H__ +#include <stdint.h> #include <stdlib.h> #include <sys/types.h> @@ -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 |