Replace duplicate implementations of libnetutil functions
Some checks are pending
nmap multiplatform autobuilds / build (arm64, gcc, ubuntu-latest-gcc-arm64, ubuntu-latest) (push) Waiting to run
nmap multiplatform autobuilds / build (clang, freebsd-15-clang, ubuntu-latest) (push) Waiting to run
nmap multiplatform autobuilds / build (clang, macos-15-clang, macos-15) (push) Waiting to run
nmap multiplatform autobuilds / build (clang, macos-26-clang, macos-26) (push) Waiting to run
nmap multiplatform autobuilds / build (clang, netbsd-10-clang, ubuntu-latest) (push) Waiting to run
nmap multiplatform autobuilds / build (clang, openbsd-7-clang, ubuntu-latest) (push) Waiting to run
nmap multiplatform autobuilds / build (clang, solaris-11-clang, ubuntu-latest) (push) Waiting to run
nmap multiplatform autobuilds / build (clang, ubuntu-latest-clang, ubuntu-latest) (push) Waiting to run
nmap multiplatform autobuilds / build (egcc, openbsd-7-gcc, ubuntu-latest) (push) Waiting to run
nmap multiplatform autobuilds / build (gcc, freebsd-15-gcc, ubuntu-latest) (push) Waiting to run
nmap multiplatform autobuilds / build (gcc, netbsd-10-gcc, ubuntu-latest) (push) Waiting to run
nmap multiplatform autobuilds / build (gcc, solaris-11-gcc, ubuntu-latest) (push) Waiting to run
nmap multiplatform autobuilds / build (gcc, ubuntu-latest-gcc, ubuntu-latest) (push) Waiting to run
nmap multiplatform autobuilds / build (msvc, windows-latest-msvc, windows-latest) (push) Waiting to run

This commit is contained in:
dmiller 2026-05-12 14:35:31 +00:00
parent 4ea1c703de
commit 4ba5b9c335
2 changed files with 30 additions and 172 deletions

View file

@ -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;

View file

@ -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) ;