diff options
Diffstat (limited to 'libntp/numtoa.c')
-rw-r--r-- | libntp/numtoa.c | 64 |
1 files changed, 39 insertions, 25 deletions
diff --git a/libntp/numtoa.c b/libntp/numtoa.c index 51645de01c30..ac8978ee5e58 100644 --- a/libntp/numtoa.c +++ b/libntp/numtoa.c @@ -12,7 +12,6 @@ #include <ctype.h> #include "ntp_fp.h" -#include "lib_strbuf.h" #include "ntp_stdlib.h" char * @@ -34,7 +33,11 @@ numtoa( } -/* Convert a refid & stratum to a string */ +/* + * Convert a refid & stratum to a string. If stratum is negative and the + * refid consists entirely of graphic chars, up to an optional + * terminating zero, display as text similar to stratum 0 & 1. + */ const char * refid_str( u_int32 refid, @@ -43,33 +46,44 @@ refid_str( { char * text; size_t tlen; - char * cp; - - if (stratum > 1) - return numtoa(refid); - - LIB_GETBUF(text); - text[0] = '.'; - /* What if any non-NUL char is not printable? */ - memcpy(&text[1], &refid, sizeof(refid)); - text[1 + sizeof(refid)] = '\0'; - tlen = strlen(text); - text[tlen] = '.'; - text[tlen + 1] = '\0'; + char * cp; + int printable; /* - * Now make sure the contents are 'graphic'. - * - * This refid is expected to be up to 4 ascii graphics. - * If any character is not a graphic, replace it with a space. - * This will at least alert the viewer of a problem. + * ntpd can have stratum = 0 and refid 127.0.0.1 in orphan mode. + * https://bugs.ntp.org/3854. Mirror the refid logic in timer(). */ - for (cp = text + 1; *cp; ++cp) { - if (!isgraph((int)*cp)) { - *cp = ' '; + if (0 == stratum && LOOPBACKADR_N == refid) { + return ".ORPH."; + } + printable = FALSE; + if (stratum < 2) { + text = lib_getbuf(); + text[0] = '.'; + memcpy(&text[1], &refid, sizeof(refid)); + text[1 + sizeof(refid)] = '\0'; + tlen = strlen(text); + text[tlen] = '.'; + text[tlen + 1] = '\0'; + /* + * Now make sure the contents are 'graphic'. + * + * This refid is expected to be up to 4 printable ASCII. + * isgraph() is similar to isprint() but excludes space. + * If any character is not graphic, replace it with a '?'. + * This will at least alert the viewer of a problem. + */ + for (cp = text + 1; '\0' != *cp; ++cp) { + if (!isgraph((int)*cp)) { + printable = FALSE; + *cp = '?'; + } + } + if ( (stratum < 0 && printable) + || stratum < 2) { + return text; } } - - return text; + return numtoa(refid); } |