1c0b746e5SOllivier Robert /* 2c0b746e5SOllivier Robert * numtoa - return asciized network numbers store in local array space 3c0b746e5SOllivier Robert */ 42b15cb3dSCy Schubert #include <config.h> 52b15cb3dSCy Schubert 62b15cb3dSCy Schubert #include <sys/types.h> 72b15cb3dSCy Schubert #ifdef HAVE_NETINET_IN_H 82b15cb3dSCy Schubert #include <netinet/in.h> /* ntohl */ 92b15cb3dSCy Schubert #endif 102b15cb3dSCy Schubert 11c0b746e5SOllivier Robert #include <stdio.h> 12a466cc55SCy Schubert #include <ctype.h> 13c0b746e5SOllivier Robert 14c0b746e5SOllivier Robert #include "ntp_fp.h" 15c0b746e5SOllivier Robert #include "ntp_stdlib.h" 16c0b746e5SOllivier Robert 17c0b746e5SOllivier Robert char * 18c0b746e5SOllivier Robert numtoa( 19c0b746e5SOllivier Robert u_int32 num 20c0b746e5SOllivier Robert ) 21c0b746e5SOllivier Robert { 22c0b746e5SOllivier Robert register u_int32 netnum; 23c0b746e5SOllivier Robert register char *buf; 24c0b746e5SOllivier Robert 25c0b746e5SOllivier Robert netnum = ntohl(num); 26c0b746e5SOllivier Robert LIB_GETBUF(buf); 272b15cb3dSCy Schubert snprintf(buf, LIB_BUFLENGTH, "%lu.%lu.%lu.%lu", 282b15cb3dSCy Schubert ((u_long)netnum >> 24) & 0xff, 292b15cb3dSCy Schubert ((u_long)netnum >> 16) & 0xff, 302b15cb3dSCy Schubert ((u_long)netnum >> 8) & 0xff, 31c0b746e5SOllivier Robert (u_long)netnum & 0xff); 32c0b746e5SOllivier Robert return buf; 33c0b746e5SOllivier Robert } 342b15cb3dSCy Schubert 352b15cb3dSCy Schubert 36*f5f40dd6SCy Schubert /* 37*f5f40dd6SCy Schubert * Convert a refid & stratum to a string. If stratum is negative and the 38*f5f40dd6SCy Schubert * refid consists entirely of graphic chars, up to an optional 39*f5f40dd6SCy Schubert * terminating zero, display as text similar to stratum 0 & 1. 40*f5f40dd6SCy Schubert */ 412b15cb3dSCy Schubert const char * 422b15cb3dSCy Schubert refid_str( 432b15cb3dSCy Schubert u_int32 refid, 442b15cb3dSCy Schubert int stratum 452b15cb3dSCy Schubert ) 462b15cb3dSCy Schubert { 472b15cb3dSCy Schubert char * text; 482b15cb3dSCy Schubert size_t tlen; 49a466cc55SCy Schubert char * cp; 50*f5f40dd6SCy Schubert int printable; 512b15cb3dSCy Schubert 52*f5f40dd6SCy Schubert /* 53*f5f40dd6SCy Schubert * ntpd can have stratum = 0 and refid 127.0.0.1 in orphan mode. 54*f5f40dd6SCy Schubert * https://bugs.ntp.org/3854. Mirror the refid logic in timer(). 55*f5f40dd6SCy Schubert */ 56*f5f40dd6SCy Schubert if (0 == stratum && LOOPBACKADR_N == refid) { 57*f5f40dd6SCy Schubert return ".ORPH."; 58*f5f40dd6SCy Schubert } 59*f5f40dd6SCy Schubert printable = FALSE; 60*f5f40dd6SCy Schubert if (stratum < 2) { 61*f5f40dd6SCy Schubert text = lib_getbuf(); 622b15cb3dSCy Schubert text[0] = '.'; 632b15cb3dSCy Schubert memcpy(&text[1], &refid, sizeof(refid)); 642b15cb3dSCy Schubert text[1 + sizeof(refid)] = '\0'; 652b15cb3dSCy Schubert tlen = strlen(text); 662b15cb3dSCy Schubert text[tlen] = '.'; 672b15cb3dSCy Schubert text[tlen + 1] = '\0'; 68a466cc55SCy Schubert /* 69a466cc55SCy Schubert * Now make sure the contents are 'graphic'. 70a466cc55SCy Schubert * 71*f5f40dd6SCy Schubert * This refid is expected to be up to 4 printable ASCII. 72*f5f40dd6SCy Schubert * isgraph() is similar to isprint() but excludes space. 73*f5f40dd6SCy Schubert * If any character is not graphic, replace it with a '?'. 74a466cc55SCy Schubert * This will at least alert the viewer of a problem. 75a466cc55SCy Schubert */ 76*f5f40dd6SCy Schubert for (cp = text + 1; '\0' != *cp; ++cp) { 77a466cc55SCy Schubert if (!isgraph((int)*cp)) { 78*f5f40dd6SCy Schubert printable = FALSE; 79*f5f40dd6SCy Schubert *cp = '?'; 80a466cc55SCy Schubert } 81a466cc55SCy Schubert } 82*f5f40dd6SCy Schubert if ( (stratum < 0 && printable) 83*f5f40dd6SCy Schubert || stratum < 2) { 842b15cb3dSCy Schubert return text; 852b15cb3dSCy Schubert } 86*f5f40dd6SCy Schubert } 87*f5f40dd6SCy Schubert return numtoa(refid); 88*f5f40dd6SCy Schubert } 892b15cb3dSCy Schubert 90