X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/dfa8cbe527ad9095726bff83494ac5fac347cb3b..ad06a893cfd3acca3fba38d8b39b6b15dd3053ea:/util.c diff --git a/util.c b/util.c index 1e451fea..a1c9f094 100644 --- a/util.c +++ b/util.c @@ -21,19 +21,17 @@ #ifndef lint static const char rcsid[] = - "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.74 2002-07-16 03:58:16 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.82 2002-12-22 01:26:49 hannes Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include -#include -#include +#include + #include -#include #include #ifdef HAVE_FCNTL_H #include @@ -43,10 +41,6 @@ static const char rcsid[] = #include #include #include -#ifdef TIME_WITH_SYS_TIME -#include -#endif -#include #include "interface.h" @@ -201,6 +195,37 @@ relts_print(int secs) } } +/* + * this is a generic routine for printing unknown data; + * we pass on the linefeed plus indentation string to + * get a proper output - returns 0 on error + */ + +int +print_unknown_data(const u_char *cp,const char *lf,int len) +{ + int i; + + if (len ==0) + return(0); + + printf("%s0x0000: ",lf); + for(i=0;is != NULL) { + tokval=lp->v; /* load our first value */ + rotbit=1; + while (rotbit != 0) { + /* + * lets AND the rotating bit with our token value + * and see if we have got a match + */ + if (tokval == (v&rotbit)) { + /* ok we have found something */ + buflen+=snprintf(buf+buflen, sizeof(buf)-buflen, "%s, ",lp->s); + break; + } + rotbit=rotbit<<1; /* no match - lets shift and try again */ + } + lp++; + } + + if (buflen != 0) { /* did we find anything */ + /* yep, set the the trailing zero 2 bytes before to eliminate the last comma & whitespace */ + buf[buflen-2] = '\0'; + return (buf); + } + else { + /* bummer - lets print the "unknown" message as advised in the fmt string if we got one */ + if (fmt == NULL) + fmt = "#%d"; + (void)snprintf(buf, sizeof(buf), fmt, v); + return (buf); + } +} + /* * Convert a value to a string using an array; the macro * tok2strary() in is the public interface to @@ -241,6 +311,37 @@ tok2strary_internal(register const char **lp, int n, register const char *fmt, return (buf); } +/* + * Convert a 32-bit netmask to prefixlen if possible + * the function returns the prefix-len; if plen == -1 + * then conversion was not possible; + */ + +int +mask2plen (u_int32_t mask) +{ + u_int32_t bitmasks[33] = { + 0x00000000, + 0x80000000, 0xc0000000, 0xe0000000, 0xf0000000, + 0xf8000000, 0xfc000000, 0xfe000000, 0xff000000, + 0xff800000, 0xffc00000, 0xffe00000, 0xfff00000, + 0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000, + 0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000, + 0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00, + 0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0, + 0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff + }; + int prefix_len = 33; + + /* lets see if we can transform the mask into a prefixlen */ + while (prefix_len >= 0) { + if (bitmasks[prefix_len] == mask) + break; + prefix_len--; + } + return (prefix_len); +} + /* VARARGS */ void error(const char *fmt, ...) @@ -333,7 +434,16 @@ read_infile(char *fname) if (cc < 0) error("read %s: %s", fname, pcap_strerror(errno)); if (cc != buf.st_size) +#ifndef WIN32 error("short read %s (%d != %d)", fname, cc, (int)buf.st_size); +#else +/* Windows seems not to like the final \xa character */ + { + char *pdest; + pdest=strchr( cp, '\xa'); + *pdest=0; + } +#endif /* WIN32 */ cp[(int)buf.st_size] = '\0'; return (cp); @@ -355,7 +465,7 @@ safeputchar(int c) ch = (unsigned char)(c & 0xff); if (ch < 0x80 && isprint(ch)) - printf("%c", ch & 0xff); + printf("%c", ch); else - printf("\\%03o", ch & 0xff); + printf("\\%03o", ch); }