From: Guy Harris Date: Thu, 22 Jul 2021 08:19:07 +0000 (-0700) Subject: rpcapd: clean up the code to fill in the struct rpcap_findalldevs_if. X-Git-Tag: libpcap-1.10.2~365 X-Git-Url: https://git.tcpdump.org/libpcap/commitdiff_plain/a352cd0b7e7be56e8091d06fce5e51244bf413e5 rpcapd: clean up the code to fill in the struct rpcap_findalldevs_if. Make findalldevs_if the on-the-stack struct rpcap_findalldevs_if, and fill it in directly. Once it's completely filled in, check that it fits in the output buffer and copy it to the output buffer using sock_bufferize(). This makes the code a little easier to figure out. (cherry picked from commit 95ab2d72bbb979fd65c6536bf6796c17c5f7404d) --- diff --git a/rpcapd/daemon.c b/rpcapd/daemon.c index 548cbde3..d856196d 100644 --- a/rpcapd/daemon.c +++ b/rpcapd/daemon.c @@ -1602,7 +1602,6 @@ daemon_msg_findallif_req(uint8 ver, struct daemon_slpars *pars, uint32 plen) pcap_if_t *alldevs = NULL; // pointer to the header of the interface chain pcap_if_t *d; // temp pointer needed to scan the interface chain struct pcap_addr *address; // pcap structure that keeps a network address of an interface - struct rpcap_findalldevs_if *findalldevs_if;// rpcap structure that packet all the data of an interface together uint32 replylen; // length of reply payload uint16 nif = 0; // counts the number of interface listed @@ -1713,17 +1712,8 @@ daemon_msg_findallif_req(uint8 ver, struct daemon_slpars *pars, uint32 plen) // systems support unaligned access (some, such as // SPARC, crash; others, such as Arm, may just ignore // the lower-order bits). - struct rpcap_findalldevs_if dev_if_tmp; - char *dev_if_p = &sendbuf[sendbufidx]; + struct rpcap_findalldevs_if findalldevs_if; - findalldevs_if = (struct rpcap_findalldevs_if *) &dev_if_tmp; - - if (sock_bufferize(NULL, sizeof(struct rpcap_findalldevs_if), NULL, - &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1) - goto error; - - memset(findalldevs_if, 0, sizeof(struct rpcap_findalldevs_if)); - /* * We've already established that the string lengths * fit in 16 bits. @@ -1737,10 +1727,11 @@ daemon_msg_findallif_req(uint8 ver, struct daemon_slpars *pars, uint32 plen) else lname = 0; - findalldevs_if->desclen = htons(ldescr); - findalldevs_if->namelen = htons(lname); - findalldevs_if->flags = htonl(d->flags); + findalldevs_if.desclen = htons(ldescr); + findalldevs_if.namelen = htons(lname); + findalldevs_if.flags = htonl(d->flags); + uint16_t naddrs = 0; for (address = d->addresses; address != NULL; address = address->next) { /* @@ -1752,16 +1743,20 @@ daemon_msg_findallif_req(uint8 ver, struct daemon_slpars *pars, uint32 plen) #ifdef AF_INET6 case AF_INET6: #endif - findalldevs_if->naddr++; + naddrs++; break; default: break; } } - findalldevs_if->naddr = htons(findalldevs_if->naddr); + findalldevs_if.naddr = htons(naddrs); + findalldevs_if.dummy = 0; - memcpy(dev_if_p, &dev_if_tmp, sizeof(struct rpcap_findalldevs_if)); + if (sock_bufferize(&findalldevs_if, sizeof(struct rpcap_findalldevs_if), sendbuf, + &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_BUFFERIZE, errmsgbuf, + PCAP_ERRBUF_SIZE) == -1) + goto error; if (sock_bufferize(d->name, lname, sendbuf, &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_BUFFERIZE, errmsgbuf,