]>
The Tcpdump Group git mirrors - tcpdump/blob - util.c
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.98 2005-05-06 07:56:54 guy Exp $ (LBL)";
31 #include <tcpdump-stdinc.h>
45 #include "interface.h"
48 * Print out a null-terminated filename (or other ascii string).
49 * If ep is NULL, assume no truncation check is needed.
50 * Return true if truncated.
53 fn_print(register const u_char
*s
, register const u_char
*ep
)
58 ret
= 1; /* assume truncated */
59 while (ep
== NULL
|| s
< ep
) {
71 c
^= 0x40; /* DEL to ?, others to alpha */
80 * Print out a counted filename (or other ascii string).
81 * If ep is NULL, assume no truncation check is needed.
82 * Return true if truncated.
85 fn_printn(register const u_char
*s
, register u_int n
,
86 register const u_char
*ep
)
90 while (n
> 0 && (ep
== NULL
|| s
< ep
)) {
99 c
^= 0x40; /* DEL to ?, others to alpha */
104 return (n
== 0) ? 0 : 1;
108 * Print out a null-padded filename (or other ascii string).
109 * If ep is NULL, assume no truncation check is needed.
110 * Return true if truncated.
113 fn_printzp(register const u_char
*s
, register u_int n
,
114 register const u_char
*ep
)
119 ret
= 1; /* assume truncated */
120 while (n
> 0 && (ep
== NULL
|| s
< ep
)) {
133 c
^= 0x40; /* DEL to ?, others to alpha */
138 return (n
== 0) ? 0 : 1;
142 * Print the timestamp
145 ts_print(register const struct timeval
*tvp
)
150 static unsigned b_sec
;
151 static unsigned b_usec
;
155 case 0: /* Default */
156 s
= (tvp
->tv_sec
+ thiszone
) % 86400;
157 (void)printf("%02d:%02d:%02d.%06u ",
158 s
/ 3600, (s
% 3600) / 60, s
% 60,
159 (unsigned)tvp
->tv_usec
);
162 case 1: /* No time stamp */
165 case 2: /* Unix timeval style */
166 (void)printf("%u.%06u ",
167 (unsigned)tvp
->tv_sec
,
168 (unsigned)tvp
->tv_usec
);
171 case 3: /* Microseconds since previous packet */
175 int d_usec
= tvp
->tv_usec
- b_usec
;
176 int d_sec
= tvp
->tv_sec
- b_sec
;
183 printf("%d. ", d_sec
);
184 printf("%06d ", d_usec
);
187 b_usec
= tvp
->tv_usec
;
190 case 4: /* Default + Date*/
191 s
= (tvp
->tv_sec
+ thiszone
) % 86400;
192 Time
= (tvp
->tv_sec
+ thiszone
) - s
;
195 printf("Date fail ");
197 printf("%04d-%02d-%02d ",
198 tm
->tm_year
+1900, tm
->tm_mon
+1, tm
->tm_mday
);
199 printf("%02d:%02d:%02d.%06u ",
200 s
/ 3600, (s
% 3600) / 60, s
% 60, (unsigned)tvp
->tv_usec
);
206 * Print a relative number of seconds (e.g. hold time, prune timer)
207 * in the form 5m1s. This does no truncation, so 32230861 seconds
208 * is represented as 1y1w1d1h1m1s.
211 relts_print(int secs
)
213 static const char *lengths
[] = {"y", "w", "d", "h", "m", "s"};
214 static const int seconds
[] = {31536000, 604800, 86400, 3600, 60, 1};
215 const char **l
= lengths
;
216 const int *s
= seconds
;
228 (void)printf("%d%s", secs
/ *s
, *l
);
229 secs
-= (secs
/ *s
) * *s
;
237 * this is a generic routine for printing unknown data;
238 * we pass on the linefeed plus indentation string to
239 * get a proper output - returns 0 on error
243 print_unknown_data(const u_char
*cp
,const char *ident
,int len
)
245 if (snapend
- cp
< len
)
247 hex_print(ident
,cp
,len
);
248 return(1); /* everything is ok */
252 * Convert a token value to a string; use "fmt" if not found.
255 tok2strbuf(register const struct tok
*lp
, register const char *fmt
,
256 register int v
, char *buf
, size_t bufsize
)
259 while (lp
->s
!= NULL
) {
268 (void)snprintf(buf
, bufsize
, fmt
, v
);
269 return (const char *)buf
;
273 * Convert a token value to a string; use "fmt" if not found.
276 tok2str(register const struct tok
*lp
, register const char *fmt
,
279 static char buf
[4][128];
285 return tok2strbuf(lp
, fmt
, v
, ret
, sizeof(buf
[0]));
289 * Convert a bit token value to a string; use "fmt" if not found.
290 * this is useful for parsing bitfields, the output strings are comma seperated
293 bittok2str(register const struct tok
*lp
, register const char *fmt
,
296 static char buf
[256]; /* our stringbuffer */
298 register int rotbit
; /* this is the bit we rotate through all bitpositions */
301 while (lp
->s
!= NULL
&& lp
!= NULL
) {
302 tokval
=lp
->v
; /* load our first value */
304 while (rotbit
!= 0) {
306 * lets AND the rotating bit with our token value
307 * and see if we have got a match
309 if (tokval
== (v
&rotbit
)) {
310 /* ok we have found something */
311 buflen
+=snprintf(buf
+buflen
, sizeof(buf
)-buflen
, "%s, ",lp
->s
);
314 rotbit
=rotbit
<<1; /* no match - lets shift and try again */
319 if (buflen
!= 0) { /* did we find anything */
320 /* yep, set the the trailing zero 2 bytes before to eliminate the last comma & whitespace */
321 buf
[buflen
-2] = '\0';
325 /* bummer - lets print the "unknown" message as advised in the fmt string if we got one */
328 (void)snprintf(buf
, sizeof(buf
), fmt
, v
);
334 * Convert a value to a string using an array; the macro
335 * tok2strary() in <interface.h> is the public interface to
336 * this function and ensures that the second argument is
337 * correct for bounds-checking.
340 tok2strary_internal(register const char **lp
, int n
, register const char *fmt
,
343 static char buf
[128];
345 if (v
>= 0 && v
< n
&& lp
[v
] != NULL
)
349 (void)snprintf(buf
, sizeof(buf
), fmt
, v
);
354 * Convert a 32-bit netmask to prefixlen if possible
355 * the function returns the prefix-len; if plen == -1
356 * then conversion was not possible;
360 mask2plen (u_int32_t mask
)
362 u_int32_t bitmasks
[33] = {
364 0x80000000, 0xc0000000, 0xe0000000, 0xf0000000,
365 0xf8000000, 0xfc000000, 0xfe000000, 0xff000000,
366 0xff800000, 0xffc00000, 0xffe00000, 0xfff00000,
367 0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000,
368 0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000,
369 0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00,
370 0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0,
371 0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff
375 /* let's see if we can transform the mask into a prefixlen */
376 while (prefix_len
>= 0) {
377 if (bitmasks
[prefix_len
] == mask
)
386 error(const char *fmt
, ...)
390 (void)fprintf(stderr
, "%s: ", program_name
);
392 (void)vfprintf(stderr
, fmt
, ap
);
397 (void)fputc('\n', stderr
);
405 warning(const char *fmt
, ...)
409 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
411 (void)vfprintf(stderr
, fmt
, ap
);
416 (void)fputc('\n', stderr
);
421 * Copy arg vector into a new buffer, concatenating arguments with spaces.
424 copy_argv(register char **argv
)
427 register u_int len
= 0;
436 len
+= strlen(*p
++) + 1;
438 buf
= (char *)malloc(len
);
440 error("copy_argv: malloc");
444 while ((src
= *p
++) != NULL
) {
445 while ((*dst
++ = *src
++) != '\0')
455 * On Windows, we need to open the file in binary mode, so that
456 * we get all the bytes specified by the size we get from "fstat()".
457 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
458 * we define it as 0 if it's not defined, so it does nothing.
465 read_infile(char *fname
)
467 register int i
, fd
, cc
;
471 fd
= open(fname
, O_RDONLY
|O_BINARY
);
473 error("can't open %s: %s", fname
, pcap_strerror(errno
));
475 if (fstat(fd
, &buf
) < 0)
476 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
478 cp
= malloc((u_int
)buf
.st_size
+ 1);
480 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
481 fname
, pcap_strerror(errno
));
482 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
484 error("read %s: %s", fname
, pcap_strerror(errno
));
485 if (cc
!= buf
.st_size
)
486 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
489 /* replace "# comment" with spaces */
490 for (i
= 0; i
< cc
; i
++) {
492 while (i
< cc
&& cp
[i
] != '\n')
500 safeputs(const char *s
)
513 ch
= (unsigned char)(c
& 0xff);
514 if (ch
< 0x80 && isprint(ch
))
517 printf("\\%03o", ch
);