From: Guus Sliepen Date: Sat, 9 Aug 2014 14:01:43 +0000 (+0200) Subject: Pass buffers as const. X-Git-Url: http://git.meshlink.io/?p=utcp;a=commitdiff_plain;h=93d2e72d3e72e4d91575fc00545dbba96dcdea69 Pass buffers as const. --- diff --git a/selftest.c b/selftest.c index 6edbc0d..3fb5c49 100644 --- a/selftest.c +++ b/selftest.c @@ -10,7 +10,7 @@ struct utcp *a; struct utcp *b; struct utcp_connection *c; -int do_recv(struct utcp_connection *x, void *data, size_t len) { +int do_recv(struct utcp_connection *x, const void *data, size_t len) { if(!len) { if(errno) fprintf(stderr, "%p Error: %s\n", x->utcp, strerror(errno)); @@ -43,7 +43,7 @@ void do_accept(struct utcp_connection *c, uint16_t port) { utcp_accept(c, do_recv, NULL); } -int do_send(struct utcp *utcp, void *data, size_t len) { +int do_send(struct utcp *utcp, const void *data, size_t len) { static int count = 0; if(++count > 1000) { fprintf(stderr, "Too many packets!\n"); diff --git a/test.c b/test.c index 956d1ef..4742308 100644 --- a/test.c +++ b/test.c @@ -17,7 +17,7 @@ struct utcp_connection *c; int dir = 3; bool running = true; -int do_recv(struct utcp_connection *c, void *data, size_t len) { +int do_recv(struct utcp_connection *c, const void *data, size_t len) { if(!data || !len) { if(errno) fprintf(stderr, "Error: %s\n", strerror(errno)); @@ -35,7 +35,7 @@ void do_accept(struct utcp_connection *nc, uint16_t port) { c = nc; } -int do_send(struct utcp *utcp, void *data, size_t len) { +int do_send(struct utcp *utcp, const void *data, size_t len) { int s = *(int *)utcp->priv; return send(s, data, len, MSG_DONTWAIT); } diff --git a/utcp.c b/utcp.c index 91aee3c..d9ba952 100644 --- a/utcp.c +++ b/utcp.c @@ -134,7 +134,7 @@ static void set_state(struct utcp_connection *c, enum state state) { fprintf(stderr, "%p new state: %s\n", c->utcp, strstate[state]); } -static void print_packet(void *pkt, size_t len) { +static void print_packet(const void *pkt, size_t len) { struct hdr hdr; if(len < sizeof hdr) { fprintf(stderr, "short packet (%zu bytes)\n", len); @@ -155,7 +155,7 @@ static void print_packet(void *pkt, size_t len) { if(len > sizeof hdr) { fprintf(stderr, " data="); for(int i = sizeof hdr; i < len; i++) { - char *data = pkt; + const char *data = pkt; fprintf(stderr, "%c", data[i] >= 32 ? data[i] : '.'); } } @@ -306,7 +306,7 @@ void utcp_accept(struct utcp_connection *c, utcp_recv_t recv, void *priv) { set_state(c, ESTABLISHED); } -ssize_t utcp_send(struct utcp_connection *c, void *data, size_t len) { +ssize_t utcp_send(struct utcp_connection *c, const void *data, size_t len) { if(c->reapable) { fprintf(stderr, "Error: send() called on closed connection %p\n", c); errno = EBADF; @@ -393,7 +393,7 @@ static int16_t seqdiff(uint16_t a, uint16_t b) { return a -b; } -int utcp_recv(struct utcp *utcp, void *data, size_t len) { +int utcp_recv(struct utcp *utcp, const void *data, size_t len) { if(!utcp) { errno = EFAULT; return -1; diff --git a/utcp.h b/utcp.h index 5729ef2..9c80c45 100644 --- a/utcp.h +++ b/utcp.h @@ -41,15 +41,15 @@ struct utcp_connection; typedef bool (*utcp_pre_accept_t)(struct utcp *utcp, uint16_t port); typedef void (*utcp_accept_t)(struct utcp_connection *utcp_connection, uint16_t port); -typedef int (*utcp_send_t)(struct utcp *utcp, void *data, size_t len); -typedef int (*utcp_recv_t)(struct utcp_connection *connection, void *data, size_t len); +typedef int (*utcp_send_t)(struct utcp *utcp, const void *data, size_t len); +typedef int (*utcp_recv_t)(struct utcp_connection *connection, const void *data, size_t len); extern struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_send_t send, void *priv); extern void utcp_exit(struct utcp *utcp); extern struct utcp_connection *utcp_connect(struct utcp *utcp, uint16_t port, utcp_recv_t recv, void *priv); extern void utcp_accept(struct utcp_connection *utcp, utcp_recv_t recv, void *priv); -extern ssize_t utcp_send(struct utcp_connection *connection, void *data, size_t len); -extern int utcp_recv(struct utcp *utcp, void *data, size_t len); +extern ssize_t utcp_send(struct utcp_connection *connection, const void *data, size_t len); +extern int utcp_recv(struct utcp *utcp, const void *data, size_t len); extern int utcp_close(struct utcp_connection *connection); extern int utcp_abort(struct utcp_connection *connection); extern int utcp_shutdown(struct utcp_connection *connection, int how);