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> 12*a466cc55SCy Schubert #include <ctype.h> 13c0b746e5SOllivier Robert 14c0b746e5SOllivier Robert #include "ntp_fp.h" 15c0b746e5SOllivier Robert #include "lib_strbuf.h" 16c0b746e5SOllivier Robert #include "ntp_stdlib.h" 17c0b746e5SOllivier Robert 18c0b746e5SOllivier Robert char * 19c0b746e5SOllivier Robert numtoa( 20c0b746e5SOllivier Robert u_int32 num 21c0b746e5SOllivier Robert ) 22c0b746e5SOllivier Robert { 23c0b746e5SOllivier Robert register u_int32 netnum; 24c0b746e5SOllivier Robert register char *buf; 25c0b746e5SOllivier Robert 26c0b746e5SOllivier Robert netnum = ntohl(num); 27c0b746e5SOllivier Robert LIB_GETBUF(buf); 282b15cb3dSCy Schubert snprintf(buf, LIB_BUFLENGTH, "%lu.%lu.%lu.%lu", 292b15cb3dSCy Schubert ((u_long)netnum >> 24) & 0xff, 302b15cb3dSCy Schubert ((u_long)netnum >> 16) & 0xff, 312b15cb3dSCy Schubert ((u_long)netnum >> 8) & 0xff, 32c0b746e5SOllivier Robert (u_long)netnum & 0xff); 33c0b746e5SOllivier Robert return buf; 34c0b746e5SOllivier Robert } 352b15cb3dSCy Schubert 362b15cb3dSCy Schubert 372b15cb3dSCy Schubert /* Convert a refid & stratum to a string */ 382b15cb3dSCy Schubert const char * 392b15cb3dSCy Schubert refid_str( 402b15cb3dSCy Schubert u_int32 refid, 412b15cb3dSCy Schubert int stratum 422b15cb3dSCy Schubert ) 432b15cb3dSCy Schubert { 442b15cb3dSCy Schubert char * text; 452b15cb3dSCy Schubert size_t tlen; 46*a466cc55SCy Schubert char * cp; 472b15cb3dSCy Schubert 482b15cb3dSCy Schubert if (stratum > 1) 492b15cb3dSCy Schubert return numtoa(refid); 502b15cb3dSCy Schubert 512b15cb3dSCy Schubert LIB_GETBUF(text); 522b15cb3dSCy Schubert text[0] = '.'; 53*a466cc55SCy Schubert /* What if any non-NUL char is not printable? */ 542b15cb3dSCy Schubert memcpy(&text[1], &refid, sizeof(refid)); 552b15cb3dSCy Schubert text[1 + sizeof(refid)] = '\0'; 562b15cb3dSCy Schubert tlen = strlen(text); 572b15cb3dSCy Schubert text[tlen] = '.'; 582b15cb3dSCy Schubert text[tlen + 1] = '\0'; 592b15cb3dSCy Schubert 60*a466cc55SCy Schubert /* 61*a466cc55SCy Schubert * Now make sure the contents are 'graphic'. 62*a466cc55SCy Schubert * 63*a466cc55SCy Schubert * This refid is expected to be up to 4 ascii graphics. 64*a466cc55SCy Schubert * If any character is not a graphic, replace it with a space. 65*a466cc55SCy Schubert * This will at least alert the viewer of a problem. 66*a466cc55SCy Schubert */ 67*a466cc55SCy Schubert for (cp = text + 1; *cp; ++cp) { 68*a466cc55SCy Schubert if (!isgraph((int)*cp)) { 69*a466cc55SCy Schubert *cp = ' '; 70*a466cc55SCy Schubert } 71*a466cc55SCy Schubert } 72*a466cc55SCy Schubert 732b15cb3dSCy Schubert return text; 742b15cb3dSCy Schubert } 752b15cb3dSCy Schubert 76