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 */
326 const char * sepstr
= "";
328 while (lp
!= NULL
&& lp
->s
!= NULL
) {
329 tokval
=lp
->v
; /* load our first value */
331 while (rotbit
!= 0) {
333 * lets AND the rotating bit with our token value
334 * and see if we have got a match
336 if (tokval
== (v
&rotbit
)) {
337 /* ok we have found something */
338 buflen
+=snprintf(buf
+buflen
, sizeof(buf
)-buflen
, "%s%s",
340 sepstr
= sep
? ", " : "";
343 rotbit
=rotbit
<<1; /* no match - lets shift and try again */
349 /* bummer - lets print the "unknown" message as advised in the fmt string if we got one */
350 (void)snprintf(buf
, sizeof(buf
), fmt
== NULL
? "#%d" : fmt
, v
);
355 * Convert a bit token value to a string; use "fmt" if not found.
356 * this is useful for parsing bitfields, the output strings are not seperated.
359 bittok2str_nosep(register const struct tok
*lp
, register const char *fmt
,
362 return (bittok2str_internal(lp
, fmt
, v
, 0));
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 comma seperated.
370 bittok2str(register const struct tok
*lp
, register const char *fmt
,
373 return (bittok2str_internal(lp
, fmt
, v
, 1));
377 * Convert a value to a string using an array; the macro
378 * tok2strary() in <interface.h> is the public interface to
379 * this function and ensures that the second argument is
380 * correct for bounds-checking.
383 tok2strary_internal(register const char **lp
, int n
, register const char *fmt
,
386 static char buf
[128];
388 if (v
>= 0 && v
< n
&& lp
[v
] != NULL
)
392 (void)snprintf(buf
, sizeof(buf
), fmt
, v
);
397 * Convert a 32-bit netmask to prefixlen if possible
398 * the function returns the prefix-len; if plen == -1
399 * then conversion was not possible;
403 mask2plen(uint32_t mask
)
405 uint32_t bitmasks
[33] = {
407 0x80000000, 0xc0000000, 0xe0000000, 0xf0000000,
408 0xf8000000, 0xfc000000, 0xfe000000, 0xff000000,
409 0xff800000, 0xffc00000, 0xffe00000, 0xfff00000,
410 0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000,
411 0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000,
412 0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00,
413 0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0,
414 0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff
418 /* let's see if we can transform the mask into a prefixlen */
419 while (prefix_len
>= 0) {
420 if (bitmasks
[prefix_len
] == mask
)
429 mask62plen(const u_char
*mask
)
431 u_char bitmasks
[9] = {
433 0x80, 0xc0, 0xe0, 0xf0,
434 0xf8, 0xfc, 0xfe, 0xff
439 for (byte
= 0; byte
< 16; byte
++) {
442 for (bits
= 0; bits
< (sizeof (bitmasks
) / sizeof (bitmasks
[0])); bits
++) {
443 if (mask
[byte
] == bitmasks
[bits
]) {
449 if (mask
[byte
] != 0xff)
458 error(const char *fmt
, ...)
462 (void)fprintf(stderr
, "%s: ", program_name
);
464 (void)vfprintf(stderr
, fmt
, ap
);
469 (void)fputc('\n', stderr
);
477 warning(const char *fmt
, ...)
481 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
483 (void)vfprintf(stderr
, fmt
, ap
);
488 (void)fputc('\n', stderr
);
493 * Copy arg vector into a new buffer, concatenating arguments with spaces.
496 copy_argv(register char **argv
)
499 register u_int len
= 0;
508 len
+= strlen(*p
++) + 1;
510 buf
= (char *)malloc(len
);
512 error("copy_argv: malloc");
516 while ((src
= *p
++) != NULL
) {
517 while ((*dst
++ = *src
++) != '\0')
527 * On Windows, we need to open the file in binary mode, so that
528 * we get all the bytes specified by the size we get from "fstat()".
529 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
530 * we define it as 0 if it's not defined, so it does nothing.
537 read_infile(char *fname
)
539 register int i
, fd
, cc
;
543 fd
= open(fname
, O_RDONLY
|O_BINARY
);
545 error("can't open %s: %s", fname
, pcap_strerror(errno
));
547 if (fstat(fd
, &buf
) < 0)
548 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
550 cp
= malloc((u_int
)buf
.st_size
+ 1);
552 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
553 fname
, pcap_strerror(errno
));
554 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
556 error("read %s: %s", fname
, pcap_strerror(errno
));
557 if (cc
!= buf
.st_size
)
558 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
561 /* replace "# comment" with spaces */
562 for (i
= 0; i
< cc
; i
++) {
564 while (i
< cc
&& cp
[i
] != '\n')
572 safeputs(netdissect_options
*ndo
,
573 const u_char
*s
, const u_int maxlen
)
577 while (*s
&& idx
< maxlen
) {
578 safeputchar(ndo
, *s
);
585 safeputchar(netdissect_options
*ndo
,
588 ND_PRINT((ndo
, (c
< 0x80 && ND_ISPRINT(c
)) ? "%c" : "\\0x%02x", c
));
593 * Some compilers try to optimize memcpy(), using the alignment constraint
594 * on the argument pointer type. by using this function, we try to avoid the
598 unaligned_memcpy(void *p
, const void *q
, size_t l
)
603 /* As with memcpy(), so with memcmp(). */
605 unaligned_memcmp(const void *p
, const void *q
, size_t l
)
607 return (memcmp(p
, q
, l
));