]> The Tcpdump Group git mirrors - tcpdump/blob - util.c
From Motonori Shindo <[email protected]>: squelch warnings from some
[tcpdump] / util.c
1 /*
2 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
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
16 * written permission.
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.
20 */
21
22 #ifndef lint
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.78 2002-08-02 04:22:11 guy Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <tcpdump-stdinc.h>
32
33 #include <sys/stat.h>
34
35 #include <errno.h>
36 #ifdef HAVE_FCNTL_H
37 #include <fcntl.h>
38 #endif
39 #include <pcap.h>
40 #include <stdio.h>
41 #include <stdarg.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #ifdef TIME_WITH_SYS_TIME
45 #include <time.h>
46 #endif
47
48 #include "interface.h"
49
50 /*
51 * Print out a filename (or other ascii string).
52 * If ep is NULL, assume no truncation check is needed.
53 * Return true if truncated.
54 */
55 int
56 fn_print(register const u_char *s, register const u_char *ep)
57 {
58 register int ret;
59 register u_char c;
60
61 ret = 1; /* assume truncated */
62 while (ep == NULL || s < ep) {
63 c = *s++;
64 if (c == '\0') {
65 ret = 0;
66 break;
67 }
68 if (!isascii(c)) {
69 c = toascii(c);
70 putchar('M');
71 putchar('-');
72 }
73 if (!isprint(c)) {
74 c ^= 0x40; /* DEL to ?, others to alpha */
75 putchar('^');
76 }
77 putchar(c);
78 }
79 return(ret);
80 }
81
82 /*
83 * Print out a counted filename (or other ascii string).
84 * If ep is NULL, assume no truncation check is needed.
85 * Return true if truncated.
86 */
87 int
88 fn_printn(register const u_char *s, register u_int n,
89 register const u_char *ep)
90 {
91 register int ret;
92 register u_char c;
93
94 ret = 1; /* assume truncated */
95 while (ep == NULL || s < ep) {
96 if (n-- <= 0) {
97 ret = 0;
98 break;
99 }
100 c = *s++;
101 if (!isascii(c)) {
102 c = toascii(c);
103 putchar('M');
104 putchar('-');
105 }
106 if (!isprint(c)) {
107 c ^= 0x40; /* DEL to ?, others to alpha */
108 putchar('^');
109 }
110 putchar(c);
111 }
112 return(ret);
113 }
114
115 /*
116 * Print the timestamp
117 */
118 void
119 ts_print(register const struct timeval *tvp)
120 {
121 register int s;
122 struct tm *tm;
123 time_t Time;
124 static unsigned b_sec;
125 static unsigned b_usec;
126
127 switch(tflag) {
128 case 1: /* Default */
129 s = (tvp->tv_sec + thiszone) % 86400;
130 (void)printf("%02d:%02d:%02d.%06u ",
131 s / 3600, (s % 3600) / 60, s % 60,
132 (unsigned)tvp->tv_usec);
133 break;
134 case -1: /* Unix timeval style */
135 (void)printf("%u.%06u ",
136 (unsigned)tvp->tv_sec,
137 (unsigned)tvp->tv_usec);
138 break;
139 case -2:
140 if (b_sec == 0) {
141 printf("000000 ");
142 } else {
143 int d_usec = tvp->tv_usec - b_usec;
144 int d_sec = tvp->tv_sec - b_sec;
145
146 while (d_usec < 0) {
147 d_usec += 1000000;
148 d_sec--;
149 }
150 if (d_sec)
151 printf("%d. ", d_sec);
152 printf("%06d ", d_usec);
153 }
154 b_sec = tvp->tv_sec;
155 b_usec = tvp->tv_usec;
156 break;
157 case -3: /* Default + Date*/
158 s = (tvp->tv_sec + thiszone) % 86400;
159 Time = (tvp->tv_sec + thiszone) - s;
160 tm = gmtime (&Time);
161 (void)printf("%02d/%02d/%04d %02d:%02d:%02d.%06u ",
162 tm->tm_mon+1, tm->tm_mday,
163 tm->tm_year+1900,
164 s / 3600, (s % 3600) / 60,
165 s % 60, (unsigned)tvp->tv_usec);
166 break;
167 }
168 }
169
170 /*
171 * Print a relative number of seconds (e.g. hold time, prune timer)
172 * in the form 5m1s. This does no truncation, so 32230861 seconds
173 * is represented as 1y1w1d1h1m1s.
174 */
175 void
176 relts_print(int secs)
177 {
178 static const char *lengths[] = {"y", "w", "d", "h", "m", "s"};
179 static const int seconds[] = {31536000, 604800, 86400, 3600, 60, 1};
180 const char **l = lengths;
181 const int *s = seconds;
182
183 if (secs == 0) {
184 (void)printf("0s");
185 return;
186 }
187 if (secs < 0) {
188 (void)printf("-");
189 secs = -secs;
190 }
191 while (secs > 0) {
192 if (secs >= *s) {
193 (void)printf("%d%s", secs / *s, *l);
194 secs -= (secs / *s) * *s;
195 }
196 s++;
197 l++;
198 }
199 }
200
201 /*
202 * this is a generic routine for printing unknown data;
203 * we pass on the linefeed plus indentation string to
204 * get a proper output - returns 0 on error
205 */
206
207 int
208 print_unknown_data(const u_char *cp,const char *lf,int len)
209 {
210 int i;
211
212 printf("%s0x0000: ",lf);
213 for(i=0;i<len;i++) {
214 if (!TTEST2(*(cp+i), 1)) {
215 printf("%spacket exceeded snapshot",lf);
216 return(0);
217 }
218 printf("%02x",*(cp+i));
219 if (i%2)
220 printf(" ");
221 if (i/16!=(i+1)/16) {
222 if (i<(len-1))
223 printf("%s0x%04x: ",lf,i);
224 }
225 }
226 return(1); /* everything is ok */
227 }
228
229 /*
230 * Convert a token value to a string; use "fmt" if not found.
231 */
232 const char *
233 tok2str(register const struct tok *lp, register const char *fmt,
234 register int v)
235 {
236 static char buf[128];
237
238 while (lp->s != NULL) {
239 if (lp->v == v)
240 return (lp->s);
241 ++lp;
242 }
243 if (fmt == NULL)
244 fmt = "#%d";
245 (void)snprintf(buf, sizeof(buf), fmt, v);
246 return (buf);
247 }
248
249 /*
250 * Convert a value to a string using an array; the macro
251 * tok2strary() in <interface.h> is the public interface to
252 * this function and ensures that the second argument is
253 * correct for bounds-checking.
254 */
255 const char *
256 tok2strary_internal(register const char **lp, int n, register const char *fmt,
257 register int v)
258 {
259 static char buf[128];
260
261 if (v >= 0 && v < n && lp[v] != NULL)
262 return lp[v];
263 if (fmt == NULL)
264 fmt = "#%d";
265 (void)snprintf(buf, sizeof(buf), fmt, v);
266 return (buf);
267 }
268
269 /* VARARGS */
270 void
271 error(const char *fmt, ...)
272 {
273 va_list ap;
274
275 (void)fprintf(stderr, "%s: ", program_name);
276 va_start(ap, fmt);
277 (void)vfprintf(stderr, fmt, ap);
278 va_end(ap);
279 if (*fmt) {
280 fmt += strlen(fmt);
281 if (fmt[-1] != '\n')
282 (void)fputc('\n', stderr);
283 }
284 exit(1);
285 /* NOTREACHED */
286 }
287
288 /* VARARGS */
289 void
290 warning(const char *fmt, ...)
291 {
292 va_list ap;
293
294 (void)fprintf(stderr, "%s: WARNING: ", program_name);
295 va_start(ap, fmt);
296 (void)vfprintf(stderr, fmt, ap);
297 va_end(ap);
298 if (*fmt) {
299 fmt += strlen(fmt);
300 if (fmt[-1] != '\n')
301 (void)fputc('\n', stderr);
302 }
303 }
304
305 /*
306 * Copy arg vector into a new buffer, concatenating arguments with spaces.
307 */
308 char *
309 copy_argv(register char **argv)
310 {
311 register char **p;
312 register u_int len = 0;
313 char *buf;
314 char *src, *dst;
315
316 p = argv;
317 if (*p == 0)
318 return 0;
319
320 while (*p)
321 len += strlen(*p++) + 1;
322
323 buf = (char *)malloc(len);
324 if (buf == NULL)
325 error("copy_argv: malloc");
326
327 p = argv;
328 dst = buf;
329 while ((src = *p++) != NULL) {
330 while ((*dst++ = *src++) != '\0')
331 ;
332 dst[-1] = ' ';
333 }
334 dst[-1] = '\0';
335
336 return buf;
337 }
338
339 char *
340 read_infile(char *fname)
341 {
342 register int fd, cc;
343 register char *cp;
344 struct stat buf;
345
346 fd = open(fname, O_RDONLY);
347 if (fd < 0)
348 error("can't open %s: %s", fname, pcap_strerror(errno));
349
350 if (fstat(fd, &buf) < 0)
351 error("can't stat %s: %s", fname, pcap_strerror(errno));
352
353 cp = malloc((u_int)buf.st_size + 1);
354 if (cp == NULL)
355 error("malloc(%d) for %s: %s", (u_int)buf.st_size + 1,
356 fname, pcap_strerror(errno));
357 cc = read(fd, cp, (u_int)buf.st_size);
358 if (cc < 0)
359 error("read %s: %s", fname, pcap_strerror(errno));
360 if (cc != buf.st_size)
361 #ifndef WIN32
362 error("short read %s (%d != %d)", fname, cc, (int)buf.st_size);
363 #else
364 /* Windows seems not to like the final \xa character */
365 {
366 char *pdest;
367 pdest=strchr( cp, '\xa');
368 *pdest=0;
369 }
370 #endif /* WIN32 */
371 cp[(int)buf.st_size] = '\0';
372
373 return (cp);
374 }
375
376 void
377 safeputs(const char *s)
378 {
379 while (*s) {
380 safeputchar(*s);
381 s++;
382 }
383 }
384
385 void
386 safeputchar(int c)
387 {
388 unsigned char ch;
389
390 ch = (unsigned char)(c & 0xff);
391 if (ch < 0x80 && isprint(ch))
392 printf("%c", ch);
393 else
394 printf("\\%03o", ch);
395 }