Here is a diff to refactor and update dvrmpd to use the new ibuf API. Instead of doing the checksum calculations in various places do it in send_packet() as a central place.
Could people using dvrmpd test this diff, thanks -- :wq Claudio Index: ask_nbrs2.c =================================================================== RCS file: /cvs/src/usr.sbin/dvmrpd/ask_nbrs2.c,v retrieving revision 1.5 diff -u -p -r1.5 ask_nbrs2.c --- ask_nbrs2.c 7 Dec 2015 19:17:18 -0000 1.5 +++ ask_nbrs2.c 15 Jun 2023 07:43:01 -0000 @@ -36,7 +36,6 @@ send_ask_nbrs2(struct iface *iface, stru { struct sockaddr_in dst; struct ibuf *buf; - struct dvmrp_hdr *dvmrp_hdr; int ret = 0; log_debug("send_ask_nbrs2: interface %s addr %s", @@ -56,11 +55,7 @@ send_ask_nbrs2(struct iface *iface, stru dst.sin_len = sizeof(struct sockaddr_in); dst.sin_addr.s_addr = addr.s_addr; - /* update chksum */ - dvmrp_hdr = ibuf_seek(buf, 0, sizeof(*dvmrp_hdr)); - dvmrp_hdr->chksum = in_cksum(buf->buf, buf->wpos); - - ret = send_packet(iface, buf->buf, buf->wpos, &dst); + ret = send_packet(iface, buf, &dst); ibuf_free(buf); return (ret); fail: Index: dvmrpe.h =================================================================== RCS file: /cvs/src/usr.sbin/dvmrpd/dvmrpe.h,v retrieving revision 1.7 diff -u -p -r1.7 dvmrpe.h --- dvmrpe.h 19 Jan 2021 12:23:30 -0000 1.7 +++ dvmrpe.h 15 Jun 2023 07:43:01 -0000 @@ -183,7 +183,8 @@ struct ctl_nbr *nbr_to_ctl(struct nbr *) /* packet.c */ int gen_dvmrp_hdr(struct ibuf *, struct iface *, u_int8_t); -int send_packet(struct iface *, void *, size_t, struct sockaddr_in *); +int send_packet(struct iface *, struct ibuf *, + struct sockaddr_in *); void recv_packet(int, short, void *); /* probe.c */ Index: graft.c =================================================================== RCS file: /cvs/src/usr.sbin/dvmrpd/graft.c,v retrieving revision 1.5 diff -u -p -r1.5 graft.c --- graft.c 7 Dec 2015 19:17:18 -0000 1.5 +++ graft.c 15 Jun 2023 07:43:01 -0000 @@ -36,7 +36,6 @@ send_graft(struct iface *iface, struct i { struct sockaddr_in dst; struct ibuf *buf; - struct dvmrp_hdr *dvmrp_hdr; int ret = 0; log_debug("send_graft: interface %s addr %s", @@ -56,11 +55,7 @@ send_graft(struct iface *iface, struct i dst.sin_len = sizeof(struct sockaddr_in); dst.sin_addr.s_addr = addr.s_addr; - /* update chksum */ - dvmrp_hdr = ibuf_seek(buf, 0, sizeof(*dvmrp_hdr)); - dvmrp_hdr->chksum = in_cksum(buf->buf, buf->wpos); - - ret = send_packet(iface, buf->buf, buf->wpos, &dst); + ret = send_packet(iface, buf, &dst); ibuf_free(buf); return (ret); fail: Index: graft_ack.c =================================================================== RCS file: /cvs/src/usr.sbin/dvmrpd/graft_ack.c,v retrieving revision 1.5 diff -u -p -r1.5 graft_ack.c --- graft_ack.c 7 Dec 2015 19:17:18 -0000 1.5 +++ graft_ack.c 15 Jun 2023 07:43:01 -0000 @@ -36,7 +36,6 @@ send_graft_ack(struct iface *iface, stru { struct sockaddr_in dst; struct ibuf *buf; - struct dvmrp_hdr *dvmrp_hdr; int ret = 0; log_debug("send_graft_ack: interface %s addr %s", @@ -56,11 +55,7 @@ send_graft_ack(struct iface *iface, stru dst.sin_len = sizeof(struct sockaddr_in); dst.sin_addr.s_addr = addr.s_addr; - /* update chksum */ - dvmrp_hdr = ibuf_seek(buf, 0, sizeof(*dvmrp_hdr)); - dvmrp_hdr->chksum = in_cksum(buf->buf, buf->wpos); - - ret = send_packet(iface, buf->buf, buf->wpos, &dst); + ret = send_packet(iface, buf, &dst); ibuf_free(buf); return (ret); fail: Index: igmp.c =================================================================== RCS file: /cvs/src/usr.sbin/dvmrpd/igmp.c,v retrieving revision 1.4 diff -u -p -r1.4 igmp.c --- igmp.c 7 Dec 2015 19:14:49 -0000 1.4 +++ igmp.c 15 Jun 2023 07:43:01 -0000 @@ -73,9 +73,6 @@ send_igmp_query(struct iface *iface, str igmp_hdr.max_resp_time = iface->last_member_query_interval; } - /* update chksum */ - igmp_hdr.chksum = in_cksum(&igmp_hdr, sizeof(igmp_hdr)); - ibuf_add(buf, &igmp_hdr, sizeof(igmp_hdr)); /* set destination address */ @@ -83,7 +80,7 @@ send_igmp_query(struct iface *iface, str dst.sin_len = sizeof(struct sockaddr_in); inet_aton(AllSystems, &dst.sin_addr); - ret = send_packet(iface, buf->buf, buf->wpos, &dst); + ret = send_packet(iface, buf, &dst); ibuf_free(buf); return (ret); } Index: nbrs2.c =================================================================== RCS file: /cvs/src/usr.sbin/dvmrpd/nbrs2.c,v retrieving revision 1.4 diff -u -p -r1.4 nbrs2.c --- nbrs2.c 5 May 2015 01:26:37 -0000 1.4 +++ nbrs2.c 15 Jun 2023 07:43:01 -0000 @@ -37,7 +37,6 @@ send_nbrs2(struct iface *iface, struct i { struct sockaddr_in dst; struct ibuf *buf; - struct dvmrp_hdr *dvmrp_hdr; int ret = 0; log_debug("send_nbrs2: interface %s addr %s", @@ -57,11 +56,7 @@ send_nbrs2(struct iface *iface, struct i dst.sin_len = sizeof(struct sockaddr_in); dst.sin_addr.s_addr = addr.s_addr; - /* update chksum */ - dvmrp_hdr = ibuf_seek(buf, 0, sizeof(*dvmrp_hdr)); - dvmrp_hdr->chksum = in_cksum(buf->buf, buf->wpos); - - ret = send_packet(iface, buf->buf, buf->wpos, &dst); + ret = send_packet(iface, buf, &dst); ibuf_free(buf); return (ret); fail: Index: packet.c =================================================================== RCS file: /cvs/src/usr.sbin/dvmrpd/packet.c,v retrieving revision 1.7 diff -u -p -r1.7 packet.c --- packet.c 19 Jan 2021 16:02:56 -0000 1.7 +++ packet.c 16 Jun 2023 10:23:47 -0000 @@ -27,6 +27,7 @@ #include <errno.h> #include <event.h> +#include <stddef.h> #include <stdlib.h> #include <string.h> @@ -61,8 +62,10 @@ gen_dvmrp_hdr(struct ibuf *buf, struct i /* send and receive packets */ int -send_packet(struct iface *iface, void *pkt, size_t len, struct sockaddr_in *dst) +send_packet(struct iface *iface, struct ibuf *pkt, struct sockaddr_in *dst) { + u_int16_t chksum; + if (iface->passive) { log_warnx("send_packet: cannot send packet on passive " "interface %s", iface->name); @@ -77,7 +80,15 @@ send_packet(struct iface *iface, void *p return (-1); } - if (sendto(iface->fd, pkt, len, 0, + /* update chksum */ + chksum = in_cksum(ibuf_data(pkt), ibuf_size(pkt)); + if (ibuf_set(pkt, offsetof(struct dvmrp_hdr, chksum), + &chksum, sizeof(chksum)) == -1) { + log_warn("send_packet: failed to update checksum"); + return (-1); + } + + if (sendto(iface->fd, ibuf_data(pkt), ibuf_size(pkt), 0, (struct sockaddr *)dst, sizeof(*dst)) == -1 ) { log_warn("send_packet: error sending packet on interface %s", iface->name); Index: probe.c =================================================================== RCS file: /cvs/src/usr.sbin/dvmrpd/probe.c,v retrieving revision 1.4 diff -u -p -r1.4 probe.c --- probe.c 5 May 2015 01:26:37 -0000 1.4 +++ probe.c 15 Jun 2023 07:46:04 -0000 @@ -39,7 +39,6 @@ send_probe(struct iface *iface) { struct sockaddr_in dst; struct ibuf *buf; - struct dvmrp_hdr *dvmrp_hdr; struct nbr *nbr; int ret = 0; @@ -67,11 +66,7 @@ send_probe(struct iface *iface) dst.sin_len = sizeof(struct sockaddr_in); inet_aton(AllDVMRPRouters, &dst.sin_addr); - /* update chksum */ - dvmrp_hdr = ibuf_seek(buf, 0, sizeof(*dvmrp_hdr)); - dvmrp_hdr->chksum = in_cksum(buf->buf, buf->wpos); - - ret = send_packet(iface, buf->buf, buf->wpos, &dst); + ret = send_packet(iface, buf, &dst); ibuf_free(buf); return (ret); fail: Index: prune.c =================================================================== RCS file: /cvs/src/usr.sbin/dvmrpd/prune.c,v retrieving revision 1.6 diff -u -p -r1.6 prune.c --- prune.c 7 Dec 2015 19:14:49 -0000 1.6 +++ prune.c 15 Jun 2023 07:43:01 -0000 @@ -37,7 +37,6 @@ send_prune(struct nbr *nbr, struct prune { struct sockaddr_in dst; struct ibuf *buf; - struct dvmrp_hdr *dvmrp_hdr; struct prune_hdr prune; int ret = 0; @@ -69,11 +68,7 @@ send_prune(struct nbr *nbr, struct prune ibuf_add(buf, &prune, sizeof(prune)); - /* update chksum */ - dvmrp_hdr = ibuf_seek(buf, 0, sizeof(*dvmrp_hdr)); - dvmrp_hdr->chksum = in_cksum(buf->buf, buf->wpos); - - ret = send_packet(nbr->iface, buf->buf, buf->wpos, &dst); + ret = send_packet(nbr->iface, buf, &dst); ibuf_free(buf); return (ret); Index: report.c =================================================================== RCS file: /cvs/src/usr.sbin/dvmrpd/report.c,v retrieving revision 1.11 diff -u -p -r1.11 report.c --- report.c 7 Dec 2015 18:59:31 -0000 1.11 +++ report.c 15 Jun 2023 07:43:01 -0000 @@ -41,7 +41,6 @@ send_report(struct iface *iface, struct { struct sockaddr_in dst; struct ibuf *buf; - struct dvmrp_hdr *dvmrp_hdr; int ret = 0; log_debug("send_report: interface %s addr %s", @@ -63,11 +62,7 @@ send_report(struct iface *iface, struct dst.sin_len = sizeof(struct sockaddr_in); dst.sin_addr.s_addr = addr.s_addr; - /* update chksum */ - dvmrp_hdr = ibuf_seek(buf, 0, sizeof(*dvmrp_hdr)); - dvmrp_hdr->chksum = in_cksum(buf->buf, buf->wpos); - - ret = send_packet(iface, buf->buf, buf->wpos, &dst); + ret = send_packet(iface, buf, &dst); ibuf_free(buf); return (ret); fail: @@ -247,7 +242,7 @@ rr_list_send(struct rr_head *rr_list, st prefixlen = 0; while (((le = TAILQ_FIRST(rr_list)) != NULL) && - (buf->wpos < 1000)) { + (ibuf_size(buf) < 1000)) { /* netmask */ netmask = le->re->mask.s_addr; if (prefixlen != mask2prefixlen(netmask)) { @@ -288,7 +283,7 @@ rr_list_send(struct rr_head *rr_list, st rr_list_remove(le->re); free(le); } - send_report(iface, addr, buf->buf, buf->wpos); + send_report(iface, addr, ibuf_data(buf), ibuf_size(buf)); ibuf_free(buf); } }