2 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 #define NETDISSECT_REWORKED
27 #include <tcpdump-stdinc.h>
39 #include "interface.h"
42 * Print out a null-terminated filename (or other ascii string).
43 * If ep is NULL, assume no truncation check is needed.
44 * Return true if truncated.
47 fn_print(netdissect_options
*ndo
,
48 register const u_char
*s
, register const u_char
*ep
)
53 ret
= 1; /* assume truncated */
54 while (ep
== NULL
|| s
< ep
) {
62 ND_PRINT((ndo
, "M-"));
65 c
^= 0x40; /* DEL to ?, others to alpha */
68 ND_PRINT((ndo
, "%c", c
));
74 * Print out a counted filename (or other ascii string).
75 * If ep is NULL, assume no truncation check is needed.
76 * Return true if truncated.
79 fn_printn(netdissect_options
*ndo
,
80 register const u_char
*s
, register u_int n
, register const u_char
*ep
)
84 while (n
> 0 && (ep
== NULL
|| s
< ep
)) {
89 ND_PRINT((ndo
, "M-"));
92 c
^= 0x40; /* DEL to ?, others to alpha */
95 ND_PRINT((ndo
, "%c", c
));
97 return (n
== 0) ? 0 : 1;
101 * Print out a null-padded filename (or other ascii string).
102 * If ep is NULL, assume no truncation check is needed.
103 * Return true if truncated.
106 fn_printzp(netdissect_options
*ndo
,
107 register const u_char
*s
, register u_int n
,
108 register const u_char
*ep
)
113 ret
= 1; /* assume truncated */
114 while (n
> 0 && (ep
== NULL
|| s
< ep
)) {
121 if (!ND_ISASCII(c
)) {
123 ND_PRINT((ndo
, "M-"));
125 if (!ND_ISPRINT(c
)) {
126 c
^= 0x40; /* DEL to ?, others to alpha */
127 ND_PRINT((ndo
, "^"));
129 ND_PRINT((ndo
, "%c", c
));
131 return (n
== 0) ? 0 : ret
;
135 * Format the timestamp
138 ts_format(netdissect_options
*ndo
, int sec
, int usec
)
140 static char buf
[sizeof("00:00:00.000000000")];
141 const char *format
= ndo
->ndo_tstamp_precision
== PCAP_TSTAMP_PRECISION_NANO
?
142 "%02d:%02d:%02d.%09u" : "%02d:%02d:%02d.%06u";
144 snprintf(buf
, sizeof(buf
), format
,
145 sec
/ 3600, (sec
% 3600) / 60, sec
% 60, usec
);
151 * Print the timestamp
154 ts_print(netdissect_options
*ndo
,
155 register const struct timeval
*tvp
)
160 static unsigned b_sec
;
161 static unsigned b_usec
;
165 switch (ndo
->ndo_tflag
) {
167 case 0: /* Default */
168 s
= (tvp
->tv_sec
+ thiszone
) % 86400;
169 ND_PRINT((ndo
, "%s ", ts_format(ndo
, s
, tvp
->tv_usec
)));
172 case 1: /* No time stamp */
175 case 2: /* Unix timeval style */
176 ND_PRINT((ndo
, "%u.%06u ",
177 (unsigned)tvp
->tv_sec
,
178 (unsigned)tvp
->tv_usec
));
181 case 3: /* Microseconds since previous packet */
182 case 5: /* Microseconds since first packet */
184 /* init timestamp for first packet */
185 b_usec
= tvp
->tv_usec
;
189 d_usec
= tvp
->tv_usec
- b_usec
;
190 d_sec
= tvp
->tv_sec
- b_sec
;
197 ND_PRINT((ndo
, "%s ", ts_format(ndo
, d_sec
, d_usec
)));
199 if (ndo
->ndo_tflag
== 3) { /* set timestamp for last packet */
201 b_usec
= tvp
->tv_usec
;
205 case 4: /* Default + Date*/
206 s
= (tvp
->tv_sec
+ thiszone
) % 86400;
207 Time
= (tvp
->tv_sec
+ thiszone
) - s
;
210 ND_PRINT((ndo
, "Date fail "));
212 ND_PRINT((ndo
, "%04d-%02d-%02d %s ",
213 tm
->tm_year
+1900, tm
->tm_mon
+1, tm
->tm_mday
,
214 ts_format(ndo
, s
, tvp
->tv_usec
)));
220 * Print a relative number of seconds (e.g. hold time, prune timer)
221 * in the form 5m1s. This does no truncation, so 32230861 seconds
222 * is represented as 1y1w1d1h1m1s.
225 relts_print(netdissect_options
*ndo
,
228 static const char *lengths
[] = {"y", "w", "d", "h", "m", "s"};
229 static const int seconds
[] = {31536000, 604800, 86400, 3600, 60, 1};
230 const char **l
= lengths
;
231 const int *s
= seconds
;
234 ND_PRINT((ndo
, "0s"));
238 ND_PRINT((ndo
, "-"));
243 ND_PRINT((ndo
, "%d%s", secs
/ *s
, *l
));
244 secs
-= (secs
/ *s
) * *s
;
252 * this is a generic routine for printing unknown data;
253 * we pass on the linefeed plus indentation string to
254 * get a proper output - returns 0 on error
258 print_unknown_data(netdissect_options
*ndo
, const u_char
*cp
,const char *ident
,int len
)
261 ND_PRINT((ndo
,"%sDissector error: print_unknown_data called with negative length",
265 if (ndo
->ndo_snapend
- cp
< len
)
266 len
= ndo
->ndo_snapend
- cp
;
268 ND_PRINT((ndo
,"%sDissector error: print_unknown_data called with pointer past end of packet",
272 hex_print(ndo
, ident
,cp
,len
);
273 return(1); /* everything is ok */
277 * Convert a token value to a string; use "fmt" if not found.
280 tok2strbuf(register const struct tok
*lp
, register const char *fmt
,
281 register u_int v
, char *buf
, size_t bufsize
)
284 while (lp
->s
!= NULL
) {
293 (void)snprintf(buf
, bufsize
, fmt
, v
);
294 return (const char *)buf
;
298 * Convert a token value to a string; use "fmt" if not found.
301 tok2str(register const struct tok
*lp
, register const char *fmt
,
304 static char buf
[4][128];
310 return tok2strbuf(lp
, fmt
, v
, ret
, sizeof(buf
[0]));
314 * Convert a bit token value to a string; use "fmt" if not found.
315 * this is useful for parsing bitfields, the output strings are seperated
316 * if the s field is positive.
319 bittok2str_internal(register const struct tok
*lp
, register const char *fmt
,
320 register int v
, register int sep
)
322 static char buf
[256]; /* our stringbuffer */
324 register int rotbit
; /* this is the bit we rotate through all bitpositions */
327 while (lp
!= NULL
&& lp
->s
!= NULL
) {
328 tokval
=lp
->v
; /* load our first value */
330 while (rotbit
!= 0) {
332 * lets AND the rotating bit with our token value
333 * and see if we have got a match
335 if (tokval
== (v
&rotbit
)) {
336 /* ok we have found something */
337 buflen
+=snprintf(buf
+buflen
, sizeof(buf
)-buflen
, "%s%s",
338 lp
->s
, sep
? ", " : "");
341 rotbit
=rotbit
<<1; /* no match - lets shift and try again */
346 /* user didn't want string seperation - no need to cut off trailing seperators */
351 if (buflen
!= 0) { /* did we find anything */
352 /* yep, set the trailing zero 2 bytes before to eliminate the last comma & whitespace */
353 buf
[buflen
-2] = '\0';
357 /* bummer - lets print the "unknown" message as advised in the fmt string if we got one */
360 (void)snprintf(buf
, sizeof(buf
), fmt
, v
);
366 * Convert a bit token value to a string; use "fmt" if not found.
367 * this is useful for parsing bitfields, the output strings are not seperated.
370 bittok2str_nosep(register const struct tok
*lp
, register const char *fmt
,
373 return (bittok2str_internal(lp
, fmt
, v
, 0));
377 * Convert a bit token value to a string; use "fmt" if not found.
378 * this is useful for parsing bitfields, the output strings are comma seperated.
381 bittok2str(register const struct tok
*lp
, register const char *fmt
,
384 return (bittok2str_internal(lp
, fmt
, v
, 1));
388 * Convert a value to a string using an array; the macro
389 * tok2strary() in <interface.h> is the public interface to
390 * this function and ensures that the second argument is
391 * correct for bounds-checking.
394 tok2strary_internal(register const char **lp
, int n
, register const char *fmt
,
397 static char buf
[128];
399 if (v
>= 0 && v
< n
&& lp
[v
] != NULL
)
403 (void)snprintf(buf
, sizeof(buf
), fmt
, v
);
408 * Convert a 32-bit netmask to prefixlen if possible
409 * the function returns the prefix-len; if plen == -1
410 * then conversion was not possible;
414 mask2plen(uint32_t mask
)
416 uint32_t bitmasks
[33] = {
418 0x80000000, 0xc0000000, 0xe0000000, 0xf0000000,
419 0xf8000000, 0xfc000000, 0xfe000000, 0xff000000,
420 0xff800000, 0xffc00000, 0xffe00000, 0xfff00000,
421 0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000,
422 0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000,
423 0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00,
424 0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0,
425 0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff
429 /* let's see if we can transform the mask into a prefixlen */
430 while (prefix_len
>= 0) {
431 if (bitmasks
[prefix_len
] == mask
)
440 mask62plen(const u_char
*mask
)
442 u_char bitmasks
[9] = {
444 0x80, 0xc0, 0xe0, 0xf0,
445 0xf8, 0xfc, 0xfe, 0xff
450 for (byte
= 0; byte
< 16; byte
++) {
453 for (bits
= 0; bits
< (sizeof (bitmasks
) / sizeof (bitmasks
[0])); bits
++) {
454 if (mask
[byte
] == bitmasks
[bits
]) {
460 if (mask
[byte
] != 0xff)
469 error(const char *fmt
, ...)
473 (void)fprintf(stderr
, "%s: ", program_name
);
475 (void)vfprintf(stderr
, fmt
, ap
);
480 (void)fputc('\n', stderr
);
488 warning(const char *fmt
, ...)
492 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
494 (void)vfprintf(stderr
, fmt
, ap
);
499 (void)fputc('\n', stderr
);
504 * Copy arg vector into a new buffer, concatenating arguments with spaces.
507 copy_argv(register char **argv
)
510 register u_int len
= 0;
519 len
+= strlen(*p
++) + 1;
521 buf
= (char *)malloc(len
);
523 error("copy_argv: malloc");
527 while ((src
= *p
++) != NULL
) {
528 while ((*dst
++ = *src
++) != '\0')
538 * On Windows, we need to open the file in binary mode, so that
539 * we get all the bytes specified by the size we get from "fstat()".
540 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
541 * we define it as 0 if it's not defined, so it does nothing.
548 read_infile(char *fname
)
550 register int i
, fd
, cc
;
554 fd
= open(fname
, O_RDONLY
|O_BINARY
);
556 error("can't open %s: %s", fname
, pcap_strerror(errno
));
558 if (fstat(fd
, &buf
) < 0)
559 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
561 cp
= malloc((u_int
)buf
.st_size
+ 1);
563 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
564 fname
, pcap_strerror(errno
));
565 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
567 error("read %s: %s", fname
, pcap_strerror(errno
));
568 if (cc
!= buf
.st_size
)
569 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
572 /* replace "# comment" with spaces */
573 for (i
= 0; i
< cc
; i
++) {
575 while (i
< cc
&& cp
[i
] != '\n')
583 safeputs(netdissect_options
*ndo
,
584 const u_char
*s
, const u_int maxlen
)
588 while (*s
&& idx
< maxlen
) {
589 safeputchar(ndo
, *s
);
596 safeputchar(netdissect_options
*ndo
,
599 ND_PRINT((ndo
, (c
< 0x80 && ND_ISPRINT(c
)) ? "%c" : "\\0x%02x", c
));
604 * Some compilers try to optimize memcpy(), using the alignment constraint
605 * on the argument pointer type. by using this function, we try to avoid the
609 unaligned_memcpy(void *p
, const void *q
, size_t l
)
614 /* As with memcpy(), so with memcmp(). */
616 unaligned_memcmp(const void *p
, const void *q
, size_t l
)
618 return (memcmp(p
, q
, l
));