]>
The Tcpdump Group git mirrors - tcpdump/blob - util.c
cb4f2aaa768ce16a5fd07261f14c6d0ee4b1e9cd
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.
23 static const char rcsid
[] _U_
=
24 "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.106 2006-02-08 01:38:16 hannes Exp $ (LBL)";
31 #include <tcpdump-stdinc.h>
45 #include "interface.h"
47 char * ts_format(register int, register int);
50 * Print out a null-terminated filename (or other ascii string).
51 * If ep is NULL, assume no truncation check is needed.
52 * Return true if truncated.
55 fn_print(register const u_char
*s
, register const u_char
*ep
)
60 ret
= 1; /* assume truncated */
61 while (ep
== NULL
|| s
< ep
) {
73 c
^= 0x40; /* DEL to ?, others to alpha */
82 * Print out a counted filename (or other ascii string).
83 * If ep is NULL, assume no truncation check is needed.
84 * Return true if truncated.
87 fn_printn(register const u_char
*s
, register u_int n
,
88 register const u_char
*ep
)
92 while (n
> 0 && (ep
== NULL
|| s
< ep
)) {
101 c
^= 0x40; /* DEL to ?, others to alpha */
106 return (n
== 0) ? 0 : 1;
110 * Print out a null-padded filename (or other ascii string).
111 * If ep is NULL, assume no truncation check is needed.
112 * Return true if truncated.
115 fn_printzp(register const u_char
*s
, register u_int n
,
116 register const u_char
*ep
)
121 ret
= 1; /* assume truncated */
122 while (n
> 0 && (ep
== NULL
|| s
< ep
)) {
135 c
^= 0x40; /* DEL to ?, others to alpha */
140 return (n
== 0) ? 0 : ret
;
144 * Format the timestamp
147 ts_format(register int sec
, register int usec
)
149 static char buf
[sizeof("00:00:00.000000")];
150 (void)snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d.%06u",
151 sec
/ 3600, (sec
% 3600) / 60, sec
% 60, usec
);
157 * Print the timestamp
160 ts_print(register const struct timeval
*tvp
)
165 static unsigned b_sec
;
166 static unsigned b_usec
;
172 case 0: /* Default */
173 s
= (tvp
->tv_sec
+ thiszone
) % 86400;
174 (void)printf("%s ", ts_format(s
, tvp
->tv_usec
));
177 case 1: /* No time stamp */
180 case 2: /* Unix timeval style */
181 (void)printf("%u.%06u ",
182 (unsigned)tvp
->tv_sec
,
183 (unsigned)tvp
->tv_usec
);
186 case 3: /* Microseconds since previous packet */
187 case 5: /* Microseconds since first packet */
189 /* init timestamp for first packet */
190 b_usec
= tvp
->tv_usec
;
194 d_usec
= tvp
->tv_usec
- b_usec
;
195 d_sec
= tvp
->tv_sec
- b_sec
;
202 (void)printf("%s ", ts_format(d_sec
, d_usec
));
204 if (tflag
== 3) { /* set timestamp for last packet */
206 b_usec
= tvp
->tv_usec
;
210 case 4: /* Default + Date*/
211 s
= (tvp
->tv_sec
+ thiszone
) % 86400;
212 Time
= (tvp
->tv_sec
+ thiszone
) - s
;
215 printf("Date fail ");
217 printf("%04d-%02d-%02d %s ",
218 tm
->tm_year
+1900, tm
->tm_mon
+1, tm
->tm_mday
,
219 ts_format(s
, tvp
->tv_usec
));
225 * Print a relative number of seconds (e.g. hold time, prune timer)
226 * in the form 5m1s. This does no truncation, so 32230861 seconds
227 * is represented as 1y1w1d1h1m1s.
230 relts_print(int secs
)
232 static const char *lengths
[] = {"y", "w", "d", "h", "m", "s"};
233 static const int seconds
[] = {31536000, 604800, 86400, 3600, 60, 1};
234 const char **l
= lengths
;
235 const int *s
= seconds
;
247 (void)printf("%d%s", secs
/ *s
, *l
);
248 secs
-= (secs
/ *s
) * *s
;
256 * this is a generic routine for printing unknown data;
257 * we pass on the linefeed plus indentation string to
258 * get a proper output - returns 0 on error
262 print_unknown_data(const u_char
*cp
,const char *ident
,int len
)
265 printf("%sDissector error: print_unknown_data called with negative length",
269 if (snapend
- cp
< len
)
272 printf("%sDissector error: print_unknown_data called with pointer past end of packet",
276 hex_print(ident
,cp
,len
);
277 return(1); /* everything is ok */
281 * Convert a token value to a string; use "fmt" if not found.
284 tok2strbuf(register const struct tok
*lp
, register const char *fmt
,
285 register int v
, char *buf
, size_t bufsize
)
288 while (lp
->s
!= NULL
) {
297 (void)snprintf(buf
, bufsize
, fmt
, v
);
298 return (const char *)buf
;
302 * Convert a token value to a string; use "fmt" if not found.
305 tok2str(register const struct tok
*lp
, register const char *fmt
,
308 static char buf
[4][128];
314 return tok2strbuf(lp
, fmt
, v
, ret
, sizeof(buf
[0]));
318 * Convert a bit token value to a string; use "fmt" if not found.
319 * this is useful for parsing bitfields, the output strings are comma seperated
322 bittok2str(register const struct tok
*lp
, register const char *fmt
,
325 static char buf
[256]; /* our stringbuffer */
327 register int rotbit
; /* this is the bit we rotate through all bitpositions */
330 while (lp
->s
!= NULL
&& lp
!= NULL
) {
331 tokval
=lp
->v
; /* load our first value */
333 while (rotbit
!= 0) {
335 * lets AND the rotating bit with our token value
336 * and see if we have got a match
338 if (tokval
== (v
&rotbit
)) {
339 /* ok we have found something */
340 buflen
+=snprintf(buf
+buflen
, sizeof(buf
)-buflen
, "%s, ",lp
->s
);
343 rotbit
=rotbit
<<1; /* no match - lets shift and try again */
348 if (buflen
!= 0) { /* did we find anything */
349 /* yep, set the the trailing zero 2 bytes before to eliminate the last comma & whitespace */
350 buf
[buflen
-2] = '\0';
354 /* bummer - lets print the "unknown" message as advised in the fmt string if we got one */
357 (void)snprintf(buf
, sizeof(buf
), fmt
, v
);
363 * Convert a value to a string using an array; the macro
364 * tok2strary() in <interface.h> is the public interface to
365 * this function and ensures that the second argument is
366 * correct for bounds-checking.
369 tok2strary_internal(register const char **lp
, int n
, register const char *fmt
,
372 static char buf
[128];
374 if (v
>= 0 && v
< n
&& lp
[v
] != NULL
)
378 (void)snprintf(buf
, sizeof(buf
), fmt
, v
);
383 * Convert a 32-bit netmask to prefixlen if possible
384 * the function returns the prefix-len; if plen == -1
385 * then conversion was not possible;
389 mask2plen (u_int32_t mask
)
391 u_int32_t bitmasks
[33] = {
393 0x80000000, 0xc0000000, 0xe0000000, 0xf0000000,
394 0xf8000000, 0xfc000000, 0xfe000000, 0xff000000,
395 0xff800000, 0xffc00000, 0xffe00000, 0xfff00000,
396 0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000,
397 0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000,
398 0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00,
399 0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0,
400 0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff
404 /* let's see if we can transform the mask into a prefixlen */
405 while (prefix_len
>= 0) {
406 if (bitmasks
[prefix_len
] == mask
)
415 error(const char *fmt
, ...)
419 (void)fprintf(stderr
, "%s: ", program_name
);
421 (void)vfprintf(stderr
, fmt
, ap
);
426 (void)fputc('\n', stderr
);
434 warning(const char *fmt
, ...)
438 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
440 (void)vfprintf(stderr
, fmt
, ap
);
445 (void)fputc('\n', stderr
);
450 * Copy arg vector into a new buffer, concatenating arguments with spaces.
453 copy_argv(register char **argv
)
456 register u_int len
= 0;
465 len
+= strlen(*p
++) + 1;
467 buf
= (char *)malloc(len
);
469 error("copy_argv: malloc");
473 while ((src
= *p
++) != NULL
) {
474 while ((*dst
++ = *src
++) != '\0')
484 * On Windows, we need to open the file in binary mode, so that
485 * we get all the bytes specified by the size we get from "fstat()".
486 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
487 * we define it as 0 if it's not defined, so it does nothing.
494 read_infile(char *fname
)
496 register int i
, fd
, cc
;
500 fd
= open(fname
, O_RDONLY
|O_BINARY
);
502 error("can't open %s: %s", fname
, pcap_strerror(errno
));
504 if (fstat(fd
, &buf
) < 0)
505 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
507 cp
= malloc((u_int
)buf
.st_size
+ 1);
509 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
510 fname
, pcap_strerror(errno
));
511 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
513 error("read %s: %s", fname
, pcap_strerror(errno
));
514 if (cc
!= buf
.st_size
)
515 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
518 /* replace "# comment" with spaces */
519 for (i
= 0; i
< cc
; i
++) {
521 while (i
< cc
&& cp
[i
] != '\n')
529 safeputs(const char *s
, int maxlen
)
532 while (*s
&& idx
< maxlen
) {
544 ch
= (unsigned char)(c
& 0xff);
545 if (ch
< 0x80 && isprint(ch
))
548 printf("\\0x%02x ", ch
);