From 0ba4f18ea6981b4d2b4eded11b2da4b2a2192d5b Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Sun, 21 Jun 2020 21:39:15 +0200 Subject: Finish up the assert conversion --- igmp.c | 77 ++++++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 44 insertions(+), 33 deletions(-) (limited to 'igmp.c') diff --git a/igmp.c b/igmp.c index b809e4b..01a4dc4 100644 --- a/igmp.c +++ b/igmp.c @@ -105,63 +105,68 @@ from32to16(unsigned int x) } static unsigned int -do_csum(const unsigned char *buff, int len) +do_csum(const unsigned char *buf, int len) { int odd; unsigned int result = 0; - if (len <= 0) - goto out; - odd = 1 & (unsigned long) buff; + assert_return(buf && len > 0, 0); + + odd = 1 & (unsigned long)buf; if (odd) { #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - result += (*buff << 8); + result += (*buf << 8); #else - result = *buff; + result = *buf; #endif len--; - buff++; + buf++; } + if (len >= 2) { - if (2 & (unsigned long) buff) { - result += *(unsigned short *) buff; + if (2 & (unsigned long)buf) { + result += *(unsigned short *)buf; len -= 2; - buff += 2; + buf += 2; } if (len >= 4) { - const unsigned char *end = buff + ((unsigned)len & ~3); + const unsigned char *end = buf + ((unsigned)len & ~3); unsigned int carry = 0; do { - unsigned int w = *(unsigned int *) buff; - buff += 4; + unsigned int w = *(unsigned int *)buf; + buf += 4; result += carry; result += w; carry = (w > result); - } while (buff < end); + } while (buf < end); result += carry; result = (result & 0xffff) + (result >> 16); } if (len & 2) { - result += *(unsigned short *) buff; - buff += 2; + result += *(unsigned short *)buf; + buf += 2; } } + if (len & 1) #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - result += *buff; + result += *buf; #else - result += (*buff << 8); + result += (*buf << 8); #endif + result = from32to16(result); if (odd) result = ((result >> 8) & 0xff) | ((result & 0xff) << 8); -out: + return result; } static inline bool csum_valid(const char *buf, size_t len) { + assert_return(buf && len > 0, false); + return do_csum((unsigned const char *)buf, len) == 0xffff; } @@ -180,12 +185,18 @@ igmp_match() static void igmp_parse(struct cfg *cfg, struct igmp *igmp) { - char *buf = igmp->task.tbuf->buf; - size_t len = igmp->task.tbuf->len; - struct ipv4_hdr *hdr = (struct ipv4_hdr *)buf; + char *buf; + size_t len; + struct ipv4_hdr *hdr; size_t body_len; union igmp_msg *igmp_msg; + assert_return(cfg && igmp); + + buf = igmp->task.tbuf->buf; + len = igmp->task.tbuf->len; + hdr = (struct ipv4_hdr *)buf; + if (len <= IPV4_MIN_HDR_LEN) return; @@ -389,10 +400,10 @@ igmp_read_cb(struct cfg *cfg, struct uring_task *task, int res) { struct igmp *igmp = container_of(task, struct igmp, task); - debug(DBG_IGMP, "task %p, igmp %p, res %i", task, igmp, res); - + assert_return(cfg && task); assert_task_alive(DBG_IGMP, task); + debug(DBG_IGMP, "task %p, igmp %p, res %i", task, igmp, res); if (res < 0) { error("res: %i", res); return; @@ -414,6 +425,7 @@ igmp_free(struct uring_task *task) { struct igmp *igmp = container_of(task, struct igmp, task); + assert_return(task); debug(DBG_IGMP, "task %p, igmp %p", task, igmp); xfree(igmp); } @@ -421,8 +433,7 @@ igmp_free(struct uring_task *task) void igmp_refdump(struct igmp *igmp) { - if (!igmp) - return; + assert_return_silent(igmp); uring_task_refdump(&igmp->task); } @@ -430,13 +441,11 @@ igmp_refdump(struct igmp *igmp) void igmp_delete(struct cfg *cfg) { - struct igmp *igmp = cfg->igmp; + assert_return(cfg); + assert_return_silent(cfg->igmp); - if (!igmp) - return; - - debug(DBG_IGMP, "closing fd %i", igmp->task.fd); - uring_task_destroy(cfg, &igmp->task); + debug(DBG_IGMP, "closing fd %i", cfg->igmp->task.fd); + uring_task_destroy(cfg, &cfg->igmp->task); cfg->igmp = NULL; } @@ -497,6 +506,8 @@ igmp_init(struct cfg *cfg) int sfd; int opt; + assert_return(cfg); + if (!cfg->do_igmp) { debug(DBG_IGMP, "igmp snooping disabled"); return; @@ -567,7 +578,7 @@ igmp_init(struct cfg *cfg) } debug(DBG_IGMP, "init successful, using fd %i", sfd); - uring_task_init(&igmp->task, "igmp", uring_parent(cfg), igmp_free); + uring_task_init(cfg, &igmp->task, "igmp", uring_parent(cfg), igmp_free); uring_task_set_fd(&igmp->task, sfd); uring_task_set_buf(&igmp->task, &igmp->tbuf); igmp->task.saddr.addrlen = sizeof(igmp->task.saddr.ll); -- cgit v1.2.3