@@ -92,7 +92,6 @@ static int settos = 0; /* Set TOS, Precendence or other QOS options */
9292
9393static int broadcast_pings = 0 ;
9494
95- static char * pr_addr (__u32 );
9695static void pr_options (unsigned char * cp , int hlen );
9796static void pr_iph (struct iphdr * ip );
9897static void usage (void ) __attribute__((noreturn ));
@@ -904,7 +903,7 @@ int ping4_receive_error_msg(socket_st *sock)
904903 write_stdout ("\bE" , 2 );
905904 } else {
906905 print_timestamp ();
907- printf ("From %s icmp_seq=%u " , pr_addr (sin -> sin_addr . s_addr ), ntohs (icmph .un .echo .sequence ));
906+ printf ("From %s icmp_seq=%u " , pr_addr (sin , sizeof * sin ), ntohs (icmph .un .echo .sequence ));
908907 pr_icmph (e -> ee_type , e -> ee_code , e -> ee_info , NULL );
909908 fflush (stdout );
910909 }
@@ -1000,7 +999,7 @@ ping4_parse_reply(struct socket_st *sock, struct msghdr *msg, int cc, void *addr
1000999 if (cc < hlen + 8 || ip -> ihl < 5 ) {
10011000 if (options & F_VERBOSE )
10021001 fprintf (stderr , "ping: packet too short (%d bytes) from %s\n" , cc ,
1003- pr_addr (from -> sin_addr . s_addr ));
1002+ pr_addr (from , sizeof * from ));
10041003 return 1 ;
10051004 }
10061005 ttl = ip -> ttl ;
@@ -1035,7 +1034,7 @@ ping4_parse_reply(struct socket_st *sock, struct msghdr *msg, int cc, void *addr
10351034 return 1 ; /* 'Twas not our ECHO */
10361035 if (gather_statistics ((__u8 * )icp , sizeof (* icp ), cc ,
10371036 ntohs (icp -> un .echo .sequence ),
1038- ttl , 0 , tv , pr_addr (from -> sin_addr . s_addr ),
1037+ ttl , 0 , tv , pr_addr (from , sizeof * from ),
10391038 pr_echo_reply )) {
10401039 fflush (stdout );
10411040 return 0 ;
@@ -1081,7 +1080,7 @@ ping4_parse_reply(struct socket_st *sock, struct msghdr *msg, int cc, void *addr
10811080 }
10821081 print_timestamp ();
10831082 printf ("From %s: icmp_seq=%u " ,
1084- pr_addr (from -> sin_addr . s_addr ),
1083+ pr_addr (from , sizeof * from ),
10851084 ntohs (icp1 -> un .echo .sequence ));
10861085 if (csfailed )
10871086 printf ("(BAD CHECKSUM)" );
@@ -1106,7 +1105,7 @@ ping4_parse_reply(struct socket_st *sock, struct msghdr *msg, int cc, void *addr
11061105 gettimeofday (& recv_time , NULL );
11071106 printf ("%lu.%06lu " , (unsigned long )recv_time .tv_sec , (unsigned long )recv_time .tv_usec );
11081107 }
1109- printf ("From %s: " , pr_addr (from -> sin_addr . s_addr ));
1108+ printf ("From %s: " , pr_addr (from , sizeof * from ));
11101109 if (csfailed ) {
11111110 printf ("(BAD CHECKSUM)\n" );
11121111 return 0 ;
@@ -1261,8 +1260,11 @@ void pr_icmph(__u8 type, __u8 code, __u32 info, struct icmphdr *icp)
12611260 printf ("Redirect, Bad Code: %d" , code );
12621261 break ;
12631262 }
1264- printf ("(New nexthop: %s)\n" ,
1265- pr_addr (icp ? icp -> un .gateway : info ));
1263+ {
1264+ struct sockaddr_in sin = { .sin_family = AF_INET , .sin_addr = { icp ? icp -> un .gateway : info } };
1265+
1266+ printf ("(New nexthop: %s)\n" , pr_addr (& sin , sizeof sin ));
1267+ }
12661268 if (icp && (options & F_VERBOSE ))
12671269 pr_iph ((struct iphdr * )(icp + 1 ));
12681270 break ;
@@ -1361,8 +1363,11 @@ void pr_options(unsigned char * cp, int hlen)
13611363 cp += 4 ;
13621364 if (address == 0 )
13631365 printf ("\t0.0.0.0" );
1364- else
1365- printf ("\t%s" , pr_addr (address ));
1366+ else {
1367+ struct sockaddr_in sin = { .sin_family = AF_INET , .sin_addr = { address } };
1368+
1369+ printf ("\t%s" , pr_addr (& sin , sizeof sin ));
1370+ }
13661371 j -= 4 ;
13671372 putchar ('\n' );
13681373 if (j <= IPOPT_MINOFF )
@@ -1396,8 +1401,11 @@ void pr_options(unsigned char * cp, int hlen)
13961401 cp += 4 ;
13971402 if (address == 0 )
13981403 printf ("\t0.0.0.0" );
1399- else
1400- printf ("\t%s" , pr_addr (address ));
1404+ else {
1405+ struct sockaddr_in sin = { .sin_family = AF_INET , .sin_addr = { address } };
1406+
1407+ printf ("\t%s" , pr_addr (& sin , sizeof sin ));
1408+ }
14011409 i -= 4 ;
14021410 putchar ('\n' );
14031411 if (i <= 0 )
@@ -1427,8 +1435,11 @@ void pr_options(unsigned char * cp, int hlen)
14271435 cp += 4 ;
14281436 if (address == 0 )
14291437 printf ("\t0.0.0.0" );
1430- else
1431- printf ("\t%s" , pr_addr (address ));
1438+ else {
1439+ struct sockaddr_in sin = { .sin_family = AF_INET , .sin_addr = { address } };
1440+
1441+ printf ("\t%s" , pr_addr (& sin , sizeof sin ));
1442+ }
14321443 i -= 4 ;
14331444 if (i <= 0 )
14341445 break ;
@@ -1496,44 +1507,37 @@ void pr_iph(struct iphdr *ip)
14961507
14971508/*
14981509 * pr_addr --
1499- * Return an ascii host address as a dotted quad and optionally with
1500- * a hostname.
1510+ *
1511+ * Return an ascii host address optionally with a hostname.
15011512 */
15021513char *
1503- pr_addr (__u32 addr )
1514+ pr_addr (void * sa , socklen_t salen )
15041515{
1505- struct hostent * hp ;
1506- static char buf [4096 ] = "" ;
1507- static __u32 last_addr = 0 ;
1516+ static char buffer [4096 ] = "" ;
1517+ static struct sockaddr_storage last_sa = { 0 };
1518+ static socklen_t last_salen = 0 ;
1519+ char name [NI_MAXHOST ] = "" ;
1520+ char address [NI_MAXHOST ] = "" ;
15081521
1509- if ( * buf && addr == last_addr )
1510- return ( buf ) ;
1522+ if ( salen == last_salen && ! memcmp ( sa , & last_sa , salen ) )
1523+ return buffer ;
15111524
1512- last_addr = addr ;
1525+ memcpy ( & last_sa , sa , ( last_salen = salen )) ;
15131526
15141527 in_pr_addr = !setjmp (pr_addr_jmp );
15151528
1516- if (exiting || (options & F_NUMERIC ) ||
1517- !(hp = gethostbyaddr ((char * )& addr , 4 , AF_INET )))
1518- sprintf (buf , "%s" , inet_ntoa (* (struct in_addr * )& addr ));
1519- else {
1520- char * s ;
1521- #if USE_IDN
1522- if (idna_to_unicode_lzlz (hp -> h_name , & s , 0 ) != IDNA_SUCCESS )
1523- s = NULL ;
1524- #else
1525- s = NULL ;
1526- #endif
1527- snprintf (buf , sizeof (buf ), "%s (%s)" , s ? s : hp -> h_name ,
1528- inet_ntoa (* (struct in_addr * )& addr ));
1529- #if USE_IDN
1530- free (s );
1531- #endif
1532- }
1529+ getnameinfo (sa , salen , address , sizeof address , NULL , 0 , getnameinfo_flags | NI_NUMERICHOST );
1530+ if (!exiting && !(options & F_NUMERIC ))
1531+ getnameinfo (sa , salen , name , sizeof name , NULL , 0 , getnameinfo_flags );
1532+
1533+ if (* name )
1534+ snprintf (buffer , sizeof buffer , "%s (%s)" , name , address );
1535+ else
1536+ snprintf (buffer , sizeof buffer , "%s" , address );
15331537
15341538 in_pr_addr = 0 ;
15351539
1536- return (buf );
1540+ return (buffer );
15371541}
15381542
15391543
0 commit comments