From 4ba5b9c3352c7d27de9783c8b49177defd283294 Mon Sep 17 00:00:00 2001 From: dmiller Date: Tue, 12 May 2026 14:35:31 +0000 Subject: [PATCH] Replace duplicate implementations of libnetutil functions --- nping/utils_net.cc | 198 +++++++-------------------------------------- nping/utils_net.h | 4 - 2 files changed, 30 insertions(+), 172 deletions(-) diff --git a/nping/utils_net.cc b/nping/utils_net.cc index 924aa23bb..fbef91db1 100644 --- a/nping/utils_net.cc +++ b/nping/utils_net.cc @@ -374,6 +374,11 @@ bool isICMPCode(u8 code, u8 type){ } /* End of isICMPType() */ +const char *arppackethdrinfo(const u8 *packet, u32 len, int detail ); +int arppackethdrinfo(const u8 *packet, u32 len, u8 *dstbuff, u32 dstlen); +int tcppackethdrinfo(const u8 *packet, size_t len, u8 *dstbuff, size_t dstlen, int detail, const char *src, const char *dst); +int udppackethdrinfo(const u8 *packet, size_t len, u8 *dstbuff, size_t dstlen, int detail, const char *src, const char *dst); + /* This function fills buffer "dstbuff" with a printable string that * represents the supplied packet. When sending IPv6 packet at raw TCP * level, the caller may specify source and/or destination address so they @@ -401,10 +406,28 @@ int getPacketStrInfo(const char *proto, const u8 *packet, u32 len, u8 *dstbuff, }else if( !strcasecmp(proto, "ARP") || !strcasecmp(proto, "RARP") ){ return arppackethdrinfo(packet, len, dstbuff, dstlen); }else if( !strcasecmp(proto, "IPv6_NO_HEADER") || o.ipv6UsingSocket() ){ + char srcipstring[128]; + const char *src = NULL; + char dstipstring[128]; + const char *dst = NULL; + if (ss_src) { + src = inet_ntop_ez(ss_src, sizeof(*ss_src)); + if (src) { + Strncpy(srcipstring, src, sizeof(srcipstring)); + src = srcipstring; + } + } + if (ss_dst) { + dst = inet_ntop_ez(ss_dst, sizeof(*ss_dst)); + if (dst) { + Strncpy(dstipstring, dst, sizeof(dstipstring)); + dst = dstipstring; + } + } if( o.getMode()==TCP ) - return tcppackethdrinfo(packet, len, dstbuff, dstlen, detail, ss_src, ss_dst); + return tcppackethdrinfo(packet, len, dstbuff, dstlen, detail, src, dst); else if ( o.getMode()==UDP ) - return udppackethdrinfo(packet, len, dstbuff, dstlen, detail, ss_src, ss_dst); + return udppackethdrinfo(packet, len, dstbuff, dstlen, detail, src, dst); else nping_fatal(QT_3, "getPacketStrInfo(): Unable to determinate transport layer protocol"); }else{ @@ -883,117 +906,12 @@ int arppackethdrinfo(const u8 *packet, u32 len, u8 *dstbuff, u32 dstlen){ int tcppackethdrinfo(const u8 *packet, size_t len, u8 *dstbuff, size_t dstlen, - int detail, struct sockaddr_storage *src, struct sockaddr_storage *dst){ - - struct tcp_hdr *tcp=NULL; ; /* TCP header structure. */ - char *p = NULL; /* Aux pointer. */ - static char protoinfo[1024] = ""; /* Stores final info string. */ - char tflags[10]; - char tcpinfo[64] = ""; - char buf[32]; - char tcpoptinfo[256] = ""; - struct sockaddr_in *s4=(struct sockaddr_in *)src; - struct sockaddr_in6 *s6=(struct sockaddr_in6 *)src; - struct sockaddr_in *d4=(struct sockaddr_in *)dst; - struct sockaddr_in6 *d6=(struct sockaddr_in6 *)dst; - char srcipstring[128]; - char dstipstring[128]; - + int detail, const char *src, const char *dst){ assert(packet); assert(dstbuff); - assert(len>=20); - tcp=(struct tcp_hdr *)packet; - - /* Ensure we end up with a valid detail number */ - if( detail!=LOW_DETAIL && detail!=MEDIUM_DETAIL && detail!=HIGH_DETAIL) - detail=LOW_DETAIL; - - - /* Determine target IP address */ - if(src!=NULL){ - if( s4->sin_family==AF_INET ){ - inet_ntop(AF_INET, &s4->sin_addr, srcipstring, sizeof(srcipstring)); - } - else if( s6->sin6_family==AF_INET6){ - inet_ntop(AF_INET6, &s6->sin6_addr, srcipstring, sizeof(srcipstring)); - }else{ - sprintf(dstipstring, "unknown_addr_family"); - } - }else{ - sprintf(srcipstring, "this_host"); - } - - /* Determine source IP address */ - if(dst!=NULL){ - if( d4->sin_family==AF_INET ){ - inet_ntop(AF_INET, &d4->sin_addr, dstipstring, sizeof(dstipstring)); - } - else if( d6->sin6_family==AF_INET6){ - inet_ntop(AF_INET6, &d6->sin6_addr, dstipstring, sizeof(dstipstring)); - }else{ - sprintf(dstipstring, "unknown_addr_family"); - } - }else{ - sprintf(dstipstring, "unknown_host"); - } - - /* TCP Flags */ - p = tflags; - /* These are basically in tcpdump order */ - if (tcp->th_flags & TH_SYN) *p++ = 'S'; - if (tcp->th_flags & TH_FIN) *p++ = 'F'; - if (tcp->th_flags & TH_RST) *p++ = 'R'; - if (tcp->th_flags & TH_PUSH) *p++ = 'P'; - if (tcp->th_flags & TH_ACK){ *p++ = 'A'; - Snprintf(buf, sizeof(buf), " ack=%lu", - (unsigned long) ntohl(tcp->th_ack)); - strncat(tcpinfo, buf, sizeof(tcpinfo) - strlen(tcpinfo) - 1); - } - if (tcp->th_flags & TH_URG) *p++ = 'U'; - if (tcp->th_flags & TH_ECE) *p++ = 'E'; /* rfc 2481/3168 */ - if (tcp->th_flags & TH_CWR) *p++ = 'C'; /* rfc 2481/3168 */ - *p++ = '\0'; - - - /* TCP Options */ - if((u32) tcp->th_off * 4 > sizeof(struct tcp_hdr)) { - if(len < (u32) tcp->th_off * 4) { - Snprintf(tcpoptinfo, sizeof(tcpoptinfo), "option incomplete"); - - } else { - tcppacketoptinfo((u8*) tcp + sizeof(struct tcp_hdr), - tcp->th_off*4 - sizeof(struct tcp_hdr), - tcpoptinfo, sizeof(tcpoptinfo)); - } - } - - /* Rest of header fields */ - if( detail == LOW_DETAIL ){ - Snprintf(protoinfo, sizeof(protoinfo), "TCP %s:%d > %s:%d %s seq=%lu win=%hu %s", - srcipstring, ntohs(tcp->th_sport), dstipstring, ntohs(tcp->th_dport), - tflags, (unsigned long) ntohl(tcp->th_seq), - ntohs(tcp->th_win), tcpoptinfo); - }else if( detail == MEDIUM_DETAIL ){ - Snprintf(protoinfo, sizeof(protoinfo), "TCP [%s:%d > %s:%d %s seq=%lu win=%hu csum=0x%04X%s%s]", - srcipstring, ntohs(tcp->th_sport), dstipstring, ntohs(tcp->th_dport), - tflags, (unsigned long) ntohl(tcp->th_seq), - ntohs(tcp->th_win), ntohs(tcp->th_sum), - (tcpoptinfo[0]!='\0') ? " " : "", - tcpoptinfo); - }else if( detail==HIGH_DETAIL ){ - Snprintf(protoinfo, sizeof(protoinfo), "TCP [%s:%d > %s:%d %s seq=%lu ack=%lu off=%d res=%d win=%hu csum=0x%04X urp=%d%s%s] ", - srcipstring, ntohs(tcp->th_sport), - dstipstring, ntohs(tcp->th_dport), - tflags, (unsigned long) ntohl(tcp->th_seq), - (unsigned long) ntohl(tcp->th_ack), - (u8)tcp->th_off, (u8)tcp->th_x2, ntohs(tcp->th_win), - ntohs(tcp->th_sum), ntohs(tcp->th_urp), - (tcpoptinfo[0]!='\0') ? " " : "", - tcpoptinfo); - } - - strncpy((char*)dstbuff, protoinfo, dstlen); + const char *protoinfo = tcphdrinfo(packet, len, detail, 0, src, dst); + Strncpy((char*)dstbuff, protoinfo, dstlen); return OP_SUCCESS; @@ -1003,68 +921,12 @@ int tcppackethdrinfo(const u8 *packet, size_t len, u8 *dstbuff, size_t dstlen, int udppackethdrinfo(const u8 *packet, size_t len, u8 *dstbuff, size_t dstlen, - int detail, struct sockaddr_storage *src, struct sockaddr_storage *dst){ - - struct udp_hdr *udp = NULL; /* UDP header structure. */ - static char protoinfo[1024] = ""; /* Stores final info string. */ - struct sockaddr_in *s4=(struct sockaddr_in *)src; - struct sockaddr_in6 *s6=(struct sockaddr_in6 *)src; - struct sockaddr_in *d4=(struct sockaddr_in *)dst; - struct sockaddr_in6 *d6=(struct sockaddr_in6 *)dst; - char srcipstring[128]; - char dstipstring[128]; + int detail, const char *src, const char *dst){ assert(packet); assert(dstbuff); - assert(len>=8); - - udp=(struct udp_hdr *)packet; - - /* Ensure we end up with a valid detail number */ - if( detail!=LOW_DETAIL && detail!=MEDIUM_DETAIL && detail!=HIGH_DETAIL) - detail=LOW_DETAIL; - - - /* Determine target IP address */ - if(src!=NULL){ - if( s4->sin_family==AF_INET ){ - inet_ntop(AF_INET, &s4->sin_addr, srcipstring, sizeof(srcipstring)); - } - else if( s6->sin6_family==AF_INET6){ - inet_ntop(AF_INET6, &s6->sin6_addr, srcipstring, sizeof(srcipstring)); - }else{ - sprintf(dstipstring, "unknown_addr_family"); - } - }else{ - sprintf(srcipstring, "this_host"); - } - - /* Determine source IP address */ - if(dst!=NULL){ - if( d4->sin_family==AF_INET ){ - inet_ntop(AF_INET, &d4->sin_addr, dstipstring, sizeof(dstipstring)); - } - else if( d6->sin6_family==AF_INET6){ - inet_ntop(AF_INET6, &d6->sin6_addr, dstipstring, sizeof(dstipstring)); - }else{ - sprintf(dstipstring, "unknown_addr_family"); - } - }else{ - sprintf(dstipstring, "unknown_host"); - } - - if( detail == LOW_DETAIL ){ - Snprintf(protoinfo, sizeof(protoinfo), "UDP %s:%d > %s:%d", - srcipstring, ntohs(udp->uh_sport), dstipstring, ntohs(udp->uh_dport)); - }else if( detail == MEDIUM_DETAIL ){ - Snprintf(protoinfo, sizeof(protoinfo), "UDP [%s:%d > %s:%d csum=0x%04X]", - srcipstring, ntohs(udp->uh_sport), dstipstring, ntohs(udp->uh_dport), ntohs(udp->uh_sum)); - }else if( detail==HIGH_DETAIL ){ - Snprintf(protoinfo, sizeof(protoinfo), "UDP [%s:%d > %s:%d len=%d csum=0x%04X]", - srcipstring, ntohs(udp->uh_sport), dstipstring, ntohs(udp->uh_dport), - ntohs(udp->uh_ulen), ntohs(udp->uh_sum)); - } + const char *protoinfo = udphdrinfo(packet, len, detail, 0, src, dst); strncpy((char*)dstbuff, protoinfo, dstlen); return OP_SUCCESS; diff --git a/nping/utils_net.h b/nping/utils_net.h index 9f8092f47..3ed526e77 100644 --- a/nping/utils_net.h +++ b/nping/utils_net.h @@ -102,10 +102,6 @@ struct hostent *hostentcpy(struct hostent *src); int hostentfree(struct hostent *src); int parseMAC(const char *txt, u8 *targetbuff); char *MACtoa(u8 *mac); -const char *arppackethdrinfo(const u8 *packet, u32 len, int detail ); -int arppackethdrinfo(const u8 *packet, u32 len, u8 *dstbuff, u32 dstlen); -int tcppackethdrinfo(const u8 *packet, size_t len, u8 *dstbuff, size_t dstlen, int detail, struct sockaddr_storage *src, struct sockaddr_storage *dst); -int udppackethdrinfo(const u8 *packet, size_t len, u8 *dstbuff, size_t dstlen, int detail, struct sockaddr_storage *src, struct sockaddr_storage *dst); const char *getRandomTextPayload(); int send_packet(NpingTarget *target, int rawfd, u8 *pkt, size_t pktLen); int print_dnet_interface(const struct intf_entry *entry, void *arg) ;