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