blob: 6f8f61d1433b8ec7dc21105561ffcf0d0c9f0b5f [file] [log] [blame]
The Android Open Source Project2949f582009-03-03 19:30:46 -08001/*
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
Elliott Hughes892a68b2015-10-19 14:43:53 -070022/*
23 * txtproto_print() derived from original code by Hannes Gredler
Elliott Hughescec480a2017-12-19 16:54:57 -080024 * (hannes@gredler.at):
Elliott Hughes892a68b2015-10-19 14:43:53 -070025 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that: (1) source code
28 * distributions retain the above copyright notice and this paragraph
29 * in its entirety, and (2) distributions including binary code include
30 * the above copyright notice and this paragraph in its entirety in
31 * the documentation or other materials provided with the distribution.
32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
33 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
34 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE.
36 */
The Android Open Source Project2949f582009-03-03 19:30:46 -080037
38#ifdef HAVE_CONFIG_H
Elliott Hughes820eced2021-08-20 18:00:50 -070039#include <config.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080040#endif
41
Elliott Hughes820eced2021-08-20 18:00:50 -070042#include "netdissect-stdinc.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080043
44#include <sys/stat.h>
45
The Android Open Source Project2949f582009-03-03 19:30:46 -080046#ifdef HAVE_FCNTL_H
47#include <fcntl.h>
48#endif
The Android Open Source Project2949f582009-03-03 19:30:46 -080049#include <stdio.h>
50#include <stdarg.h>
51#include <stdlib.h>
52#include <string.h>
53
Elliott Hughes820eced2021-08-20 18:00:50 -070054#include "netdissect-ctype.h"
55
Elliott Hughese2e3bd12017-05-15 10:59:29 -070056#include "netdissect.h"
Elliott Hughes820eced2021-08-20 18:00:50 -070057#include "extract.h"
Elliott Hughese2e3bd12017-05-15 10:59:29 -070058#include "ascii_strcasecmp.h"
59#include "timeval-operations.h"
60
Elliott Hughese2e3bd12017-05-15 10:59:29 -070061#define TOKBUFSIZE 128
62
Elliott Hughes820eced2021-08-20 18:00:50 -070063enum date_flag { WITHOUT_DATE = 0, WITH_DATE = 1 };
64enum time_flag { UTC_TIME = 0, LOCAL_TIME = 1 };
65
Elliott Hughese2e3bd12017-05-15 10:59:29 -070066/*
67 * Print out a character, filtering out the non-printable ones
68 */
69void
70fn_print_char(netdissect_options *ndo, u_char c)
71{
72 if (!ND_ISASCII(c)) {
73 c = ND_TOASCII(c);
Elliott Hughes820eced2021-08-20 18:00:50 -070074 ND_PRINT("M-");
Elliott Hughese2e3bd12017-05-15 10:59:29 -070075 }
Elliott Hughes820eced2021-08-20 18:00:50 -070076 if (!ND_ASCII_ISPRINT(c)) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -070077 c ^= 0x40; /* DEL to ?, others to alpha */
Elliott Hughes820eced2021-08-20 18:00:50 -070078 ND_PRINT("^");
Elliott Hughese2e3bd12017-05-15 10:59:29 -070079 }
Elliott Hughes820eced2021-08-20 18:00:50 -070080 ND_PRINT("%c", c);
Elliott Hughese2e3bd12017-05-15 10:59:29 -070081}
The Android Open Source Project2949f582009-03-03 19:30:46 -080082
83/*
Elliott Hughes820eced2021-08-20 18:00:50 -070084 * Print a null-terminated string, filtering out non-printable characters.
85 * DON'T USE IT with a pointer on the packet buffer because there is no
86 * truncation check. For this use, see the nd_printX() functions below.
The Android Open Source Project2949f582009-03-03 19:30:46 -080087 */
Elliott Hughes820eced2021-08-20 18:00:50 -070088void
89fn_print_str(netdissect_options *ndo, const u_char *s)
The Android Open Source Project2949f582009-03-03 19:30:46 -080090{
Elliott Hughes820eced2021-08-20 18:00:50 -070091 while (*s != '\0') {
92 fn_print_char(ndo, *s);
93 s++;
94 }
The Android Open Source Project2949f582009-03-03 19:30:46 -080095}
96
97/*
Elliott Hughes820eced2021-08-20 18:00:50 -070098 * Print out a null-terminated filename (or other ASCII string) from
99 * a fixed-length field in the packet buffer, or from what remains of
100 * the packet.
101 *
102 * n is the length of the fixed-length field, or the number of bytes
103 * remaining in the packet based on its on-the-network length.
104 *
105 * If ep is non-null, it should point just past the last captured byte
106 * of the packet, e.g. ndo->ndo_snapend. If ep is NULL, we assume no
107 * truncation check, other than the checks of the field length/remaining
108 * packet data length, is needed.
109 *
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700110 * Return the number of bytes of string processed, including the
Elliott Hughes820eced2021-08-20 18:00:50 -0700111 * terminating null, if not truncated; as the terminating null is
112 * included in the count, and as there must be a terminating null,
113 * this will always be non-zero. Return 0 if truncated.
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700114 */
115u_int
Elliott Hughes820eced2021-08-20 18:00:50 -0700116nd_printztn(netdissect_options *ndo,
117 const u_char *s, u_int n, const u_char *ep)
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700118{
Elliott Hughes820eced2021-08-20 18:00:50 -0700119 u_int bytes;
120 u_char c;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700121
122 bytes = 0;
123 for (;;) {
124 if (n == 0 || (ep != NULL && s >= ep)) {
125 /*
126 * Truncated. This includes "no null before we
Elliott Hughes820eced2021-08-20 18:00:50 -0700127 * got to the end of the fixed-length buffer or
128 * the end of the packet".
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700129 *
130 * XXX - BOOTP says "null-terminated", which
131 * means the maximum length of the string, in
132 * bytes, is 1 less than the size of the buffer,
133 * as there must always be a terminating null.
134 */
135 bytes = 0;
136 break;
137 }
138
Elliott Hughes820eced2021-08-20 18:00:50 -0700139 c = GET_U_1(s);
140 s++;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700141 bytes++;
142 n--;
143 if (c == '\0') {
144 /* End of string */
145 break;
146 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700147 fn_print_char(ndo, c);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700148 }
149 return(bytes);
150}
151
152/*
Elliott Hughes820eced2021-08-20 18:00:50 -0700153 * Print out a counted filename (or other ASCII string), part of
154 * the packet buffer.
The Android Open Source Project2949f582009-03-03 19:30:46 -0800155 * If ep is NULL, assume no truncation check is needed.
156 * Return true if truncated.
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700157 * Stop at ep (if given) or after n bytes, whichever is first.
The Android Open Source Project2949f582009-03-03 19:30:46 -0800158 */
159int
Elliott Hughes820eced2021-08-20 18:00:50 -0700160nd_printn(netdissect_options *ndo,
161 const u_char *s, u_int n, const u_char *ep)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800162{
Elliott Hughes820eced2021-08-20 18:00:50 -0700163 u_char c;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800164
165 while (n > 0 && (ep == NULL || s < ep)) {
166 n--;
Elliott Hughes820eced2021-08-20 18:00:50 -0700167 c = GET_U_1(s);
168 s++;
169 fn_print_char(ndo, c);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800170 }
171 return (n == 0) ? 0 : 1;
172}
173
174/*
Elliott Hughes820eced2021-08-20 18:00:50 -0700175 * Print a null-padded filename (or other ASCII string), part of
176 * the packet buffer, filtering out non-printable characters.
177 * Stop if truncated (via GET_U_1/longjmp) or after n bytes or before
178 * the null char, whichever occurs first.
179 * The suffix comes from: j:longJmp, n:after N bytes, p:null-Padded.
The Android Open Source Project2949f582009-03-03 19:30:46 -0800180 */
Elliott Hughes820eced2021-08-20 18:00:50 -0700181void
182nd_printjnp(netdissect_options *ndo, const u_char *s, u_int n)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800183{
Elliott Hughes820eced2021-08-20 18:00:50 -0700184 u_char c;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800185
Elliott Hughes820eced2021-08-20 18:00:50 -0700186 while (n > 0) {
187 c = GET_U_1(s);
188 if (c == '\0')
The Android Open Source Project2949f582009-03-03 19:30:46 -0800189 break;
Elliott Hughes820eced2021-08-20 18:00:50 -0700190 fn_print_char(ndo, c);
191 n--;
192 s++;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800193 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800194}
195
196/*
Elliott Hughes820eced2021-08-20 18:00:50 -0700197 * Print the timestamp .FRAC part (Microseconds/nanoseconds)
JP Abgrall53f17a92014-02-12 14:02:41 -0800198 */
Elliott Hughes820eced2021-08-20 18:00:50 -0700199static void
200ts_frac_print(netdissect_options *ndo, long usec)
JP Abgrall53f17a92014-02-12 14:02:41 -0800201{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700202#ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
203 switch (ndo->ndo_tstamp_precision) {
204
205 case PCAP_TSTAMP_PRECISION_MICRO:
Elliott Hughes820eced2021-08-20 18:00:50 -0700206 ND_PRINT(".%06u", (unsigned)usec);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700207 break;
208
209 case PCAP_TSTAMP_PRECISION_NANO:
Elliott Hughes820eced2021-08-20 18:00:50 -0700210 ND_PRINT(".%09u", (unsigned)usec);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700211 break;
212
213 default:
Elliott Hughes820eced2021-08-20 18:00:50 -0700214 ND_PRINT(".{unknown}");
Elliott Hughes892a68b2015-10-19 14:43:53 -0700215 break;
216 }
217#else
Elliott Hughes820eced2021-08-20 18:00:50 -0700218 ND_PRINT(".%06u", (unsigned)usec);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700219#endif
JP Abgrall53f17a92014-02-12 14:02:41 -0800220}
221
222/*
Elliott Hughes820eced2021-08-20 18:00:50 -0700223 * Print the timestamp as [YY:MM:DD] HH:MM:SS.FRAC.
224 * if time_flag == LOCAL_TIME print local time else UTC/GMT time
225 * if date_flag == WITH_DATE print YY:MM:DD before HH:MM:SS.FRAC
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700226 */
Elliott Hughes820eced2021-08-20 18:00:50 -0700227static void
228ts_date_hmsfrac_print(netdissect_options *ndo, long sec, long usec,
229 enum date_flag date_flag, enum time_flag time_flag)
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700230{
Elliott Hughes820eced2021-08-20 18:00:50 -0700231 time_t Time = sec;
232 struct tm *tm;
233 char timestr[32];
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700234
Elliott Hughes820eced2021-08-20 18:00:50 -0700235 if ((unsigned)sec & 0x80000000) {
236 ND_PRINT("[Error converting time]");
237 return;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700238 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700239
Elliott Hughes820eced2021-08-20 18:00:50 -0700240 if (time_flag == LOCAL_TIME)
241 tm = localtime(&Time);
242 else
243 tm = gmtime(&Time);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700244
Elliott Hughes820eced2021-08-20 18:00:50 -0700245 if (!tm) {
246 ND_PRINT("[Error converting time]");
247 return;
248 }
249 if (date_flag == WITH_DATE)
250 strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", tm);
251 else
252 strftime(timestr, sizeof(timestr), "%H:%M:%S", tm);
253 ND_PRINT("%s", timestr);
254
255 ts_frac_print(ndo, usec);
256}
257
258/*
259 * Print the timestamp - Unix timeval style, as SECS.FRAC.
260 */
261static void
262ts_unix_print(netdissect_options *ndo, long sec, long usec)
263{
264 if ((unsigned)sec & 0x80000000) {
265 ND_PRINT("[Error converting time]");
266 return;
267 }
268
269 ND_PRINT("%u", (unsigned)sec);
270 ts_frac_print(ndo, usec);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700271}
272
273/*
The Android Open Source Project2949f582009-03-03 19:30:46 -0800274 * Print the timestamp
275 */
276void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700277ts_print(netdissect_options *ndo,
Elliott Hughes820eced2021-08-20 18:00:50 -0700278 const struct timeval *tvp)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800279{
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700280 static struct timeval tv_ref;
281 struct timeval tv_result;
282 int negative_offset;
283 int nano_prec;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800284
Elliott Hughes892a68b2015-10-19 14:43:53 -0700285 switch (ndo->ndo_tflag) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800286
287 case 0: /* Default */
Elliott Hughes820eced2021-08-20 18:00:50 -0700288 ts_date_hmsfrac_print(ndo, tvp->tv_sec, tvp->tv_usec,
289 WITHOUT_DATE, LOCAL_TIME);
290 ND_PRINT(" ");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800291 break;
292
293 case 1: /* No time stamp */
294 break;
295
296 case 2: /* Unix timeval style */
Elliott Hughes820eced2021-08-20 18:00:50 -0700297 ts_unix_print(ndo, tvp->tv_sec, tvp->tv_usec);
298 ND_PRINT(" ");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800299 break;
300
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700301 case 3: /* Microseconds/nanoseconds since previous packet */
302 case 5: /* Microseconds/nanoseconds since first packet */
303#ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
304 switch (ndo->ndo_tstamp_precision) {
305 case PCAP_TSTAMP_PRECISION_MICRO:
306 nano_prec = 0;
307 break;
308 case PCAP_TSTAMP_PRECISION_NANO:
309 nano_prec = 1;
310 break;
311 default:
312 nano_prec = 0;
313 break;
314 }
315#else
316 nano_prec = 0;
317#endif
318 if (!(netdissect_timevalisset(&tv_ref)))
319 tv_ref = *tvp; /* set timestamp for first packet */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800320
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700321 negative_offset = netdissect_timevalcmp(tvp, &tv_ref, <);
322 if (negative_offset)
323 netdissect_timevalsub(&tv_ref, tvp, &tv_result, nano_prec);
324 else
325 netdissect_timevalsub(tvp, &tv_ref, &tv_result, nano_prec);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700326
Elliott Hughes820eced2021-08-20 18:00:50 -0700327 ND_PRINT((negative_offset ? "-" : " "));
328 ts_date_hmsfrac_print(ndo, tv_result.tv_sec, tv_result.tv_usec,
329 WITHOUT_DATE, UTC_TIME);
330 ND_PRINT(" ");
JP Abgrall53f17a92014-02-12 14:02:41 -0800331
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700332 if (ndo->ndo_tflag == 3)
333 tv_ref = *tvp; /* set timestamp for previous packet */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800334 break;
335
Elliott Hughes820eced2021-08-20 18:00:50 -0700336 case 4: /* Date + Default */
337 ts_date_hmsfrac_print(ndo, tvp->tv_sec, tvp->tv_usec,
338 WITH_DATE, LOCAL_TIME);
339 ND_PRINT(" ");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800340 break;
341 }
342}
343
344/*
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700345 * Print an unsigned relative number of seconds (e.g. hold time, prune timer)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800346 * in the form 5m1s. This does no truncation, so 32230861 seconds
347 * is represented as 1y1w1d1h1m1s.
348 */
349void
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700350unsigned_relts_print(netdissect_options *ndo,
351 uint32_t secs)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800352{
353 static const char *lengths[] = {"y", "w", "d", "h", "m", "s"};
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700354 static const u_int seconds[] = {31536000, 604800, 86400, 3600, 60, 1};
The Android Open Source Project2949f582009-03-03 19:30:46 -0800355 const char **l = lengths;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700356 const u_int *s = seconds;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800357
358 if (secs == 0) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700359 ND_PRINT("0s");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800360 return;
361 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800362 while (secs > 0) {
363 if (secs >= *s) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700364 ND_PRINT("%u%s", secs / *s, *l);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800365 secs -= (secs / *s) * *s;
366 }
367 s++;
368 l++;
369 }
370}
371
372/*
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700373 * Print a signed relative number of seconds (e.g. hold time, prune timer)
374 * in the form 5m1s. This does no truncation, so 32230861 seconds
375 * is represented as 1y1w1d1h1m1s.
376 */
377void
378signed_relts_print(netdissect_options *ndo,
379 int32_t secs)
380{
381 if (secs < 0) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700382 ND_PRINT("-");
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700383 if (secs == INT32_MIN) {
384 /*
385 * -2^31; you can't fit its absolute value into
386 * a 32-bit signed integer.
387 *
388 * Just directly pass said absolute value to
389 * unsigned_relts_print() directly.
390 *
391 * (XXX - does ISO C guarantee that -(-2^n),
392 * when calculated and cast to an n-bit unsigned
393 * integer type, will have the value 2^n?)
394 */
395 unsigned_relts_print(ndo, 2147483648U);
396 } else {
397 /*
398 * We now know -secs will fit into an int32_t;
399 * negate it and pass that to unsigned_relts_print().
400 */
401 unsigned_relts_print(ndo, -secs);
402 }
403 return;
404 }
405 unsigned_relts_print(ndo, secs);
406}
407
Elliott Hughes820eced2021-08-20 18:00:50 -0700408/* Print the truncated string */
409void nd_print_trunc(netdissect_options *ndo)
410{
411 ND_PRINT(" [|%s]", ndo->ndo_protocol);
412}
413
414/* Print the protocol name */
415void nd_print_protocol(netdissect_options *ndo)
416{
417 ND_PRINT("%s", ndo->ndo_protocol);
418}
419
420/* Print the protocol name in caps (uppercases) */
421void nd_print_protocol_caps(netdissect_options *ndo)
422{
423 const char *p;
424 for (p = ndo->ndo_protocol; *p != '\0'; p++)
425 ND_PRINT("%c", ND_ASCII_TOUPPER(*p));
426}
427
428/* Print the invalid string */
429void nd_print_invalid(netdissect_options *ndo)
430{
431 ND_PRINT(" (invalid)");
432}
433
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700434/*
The Android Open Source Project2949f582009-03-03 19:30:46 -0800435 * this is a generic routine for printing unknown data;
436 * we pass on the linefeed plus indentation string to
437 * get a proper output - returns 0 on error
438 */
439
440int
Elliott Hughes820eced2021-08-20 18:00:50 -0700441print_unknown_data(netdissect_options *ndo, const u_char *cp,
442 const char *ident, u_int len)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800443{
Elliott Hughes820eced2021-08-20 18:00:50 -0700444 u_int len_to_print;
445
446 len_to_print = len;
447 if (!ND_TTEST_LEN(cp, 0)) {
448 ND_PRINT("%sDissector error: print_unknown_data called with pointer past end of packet",
449 ident);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800450 return(0);
451 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700452 if (ND_BYTES_AVAILABLE_AFTER(cp) < len_to_print)
453 len_to_print = ND_BYTES_AVAILABLE_AFTER(cp);
454 hex_print(ndo, ident, cp, len_to_print);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800455 return(1); /* everything is ok */
456}
457
458/*
459 * Convert a token value to a string; use "fmt" if not found.
460 */
461const char *
Elliott Hughes820eced2021-08-20 18:00:50 -0700462tok2strbuf(const struct tok *lp, const char *fmt,
463 u_int v, char *buf, size_t bufsize)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800464{
465 if (lp != NULL) {
466 while (lp->s != NULL) {
467 if (lp->v == v)
468 return (lp->s);
469 ++lp;
470 }
471 }
472 if (fmt == NULL)
473 fmt = "#%d";
474
475 (void)snprintf(buf, bufsize, fmt, v);
476 return (const char *)buf;
477}
478
479/*
480 * Convert a token value to a string; use "fmt" if not found.
Elliott Hughes820eced2021-08-20 18:00:50 -0700481 * Uses tok2strbuf() on one of four local static buffers of size TOKBUFSIZE
482 * in round-robin fashion.
The Android Open Source Project2949f582009-03-03 19:30:46 -0800483 */
484const char *
Elliott Hughes820eced2021-08-20 18:00:50 -0700485tok2str(const struct tok *lp, const char *fmt,
486 u_int v)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800487{
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700488 static char buf[4][TOKBUFSIZE];
The Android Open Source Project2949f582009-03-03 19:30:46 -0800489 static int idx = 0;
490 char *ret;
491
492 ret = buf[idx];
493 idx = (idx+1) & 3;
494 return tok2strbuf(lp, fmt, v, ret, sizeof(buf[0]));
495}
496
497/*
498 * Convert a bit token value to a string; use "fmt" if not found.
Elliott Hughes820eced2021-08-20 18:00:50 -0700499 * this is useful for parsing bitfields, the output strings are separated
JP Abgrall53f17a92014-02-12 14:02:41 -0800500 * if the s field is positive.
Elliott Hughes820eced2021-08-20 18:00:50 -0700501 *
502 * A token matches iff it has one or more bits set and every bit that is set
503 * in the token is set in v. Consequently, a 0 token never matches.
The Android Open Source Project2949f582009-03-03 19:30:46 -0800504 */
JP Abgrall53f17a92014-02-12 14:02:41 -0800505static char *
Elliott Hughes820eced2021-08-20 18:00:50 -0700506bittok2str_internal(const struct tok *lp, const char *fmt,
507 u_int v, const char *sep)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800508{
Elliott Hughescec480a2017-12-19 16:54:57 -0800509 static char buf[1024+1]; /* our string buffer */
510 char *bufp = buf;
511 size_t space_left = sizeof(buf), string_size;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700512 const char * sepstr = "";
The Android Open Source Project2949f582009-03-03 19:30:46 -0800513
Elliott Hughes820eced2021-08-20 18:00:50 -0700514 while (lp != NULL && lp->s != NULL) {
515 if (lp->v && (v & lp->v) == lp->v) {
516 /* ok we have found something */
517 if (space_left <= 1)
518 return (buf); /* only enough room left for NUL, if that */
519 string_size = strlcpy(bufp, sepstr, space_left);
520 if (string_size >= space_left)
521 return (buf); /* we ran out of room */
522 bufp += string_size;
523 space_left -= string_size;
524 if (space_left <= 1)
525 return (buf); /* only enough room left for NUL, if that */
526 string_size = strlcpy(bufp, lp->s, space_left);
527 if (string_size >= space_left)
528 return (buf); /* we ran out of room */
529 bufp += string_size;
530 space_left -= string_size;
531 sepstr = sep;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800532 }
533 lp++;
Elliott Hughes820eced2021-08-20 18:00:50 -0700534 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800535
Elliott Hughescec480a2017-12-19 16:54:57 -0800536 if (bufp == buf)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800537 /* bummer - lets print the "unknown" message as advised in the fmt string if we got one */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700538 (void)snprintf(buf, sizeof(buf), fmt == NULL ? "#%08x" : fmt, v);
539 return (buf);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800540}
541
542/*
JP Abgrall53f17a92014-02-12 14:02:41 -0800543 * Convert a bit token value to a string; use "fmt" if not found.
Elliott Hughes820eced2021-08-20 18:00:50 -0700544 * this is useful for parsing bitfields, the output strings are not separated.
JP Abgrall53f17a92014-02-12 14:02:41 -0800545 */
546char *
Elliott Hughes820eced2021-08-20 18:00:50 -0700547bittok2str_nosep(const struct tok *lp, const char *fmt,
548 u_int v)
JP Abgrall53f17a92014-02-12 14:02:41 -0800549{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700550 return (bittok2str_internal(lp, fmt, v, ""));
JP Abgrall53f17a92014-02-12 14:02:41 -0800551}
552
553/*
554 * Convert a bit token value to a string; use "fmt" if not found.
Elliott Hughes820eced2021-08-20 18:00:50 -0700555 * this is useful for parsing bitfields, the output strings are comma separated.
JP Abgrall53f17a92014-02-12 14:02:41 -0800556 */
557char *
Elliott Hughes820eced2021-08-20 18:00:50 -0700558bittok2str(const struct tok *lp, const char *fmt,
559 u_int v)
JP Abgrall53f17a92014-02-12 14:02:41 -0800560{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700561 return (bittok2str_internal(lp, fmt, v, ", "));
JP Abgrall53f17a92014-02-12 14:02:41 -0800562}
563
564/*
The Android Open Source Project2949f582009-03-03 19:30:46 -0800565 * Convert a value to a string using an array; the macro
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700566 * tok2strary() in <netdissect.h> is the public interface to
The Android Open Source Project2949f582009-03-03 19:30:46 -0800567 * this function and ensures that the second argument is
568 * correct for bounds-checking.
569 */
570const char *
Elliott Hughes820eced2021-08-20 18:00:50 -0700571tok2strary_internal(const char **lp, int n, const char *fmt,
572 int v)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800573{
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700574 static char buf[TOKBUFSIZE];
The Android Open Source Project2949f582009-03-03 19:30:46 -0800575
576 if (v >= 0 && v < n && lp[v] != NULL)
577 return lp[v];
578 if (fmt == NULL)
579 fmt = "#%d";
580 (void)snprintf(buf, sizeof(buf), fmt, v);
581 return (buf);
582}
583
Elliott Hughes820eced2021-08-20 18:00:50 -0700584const struct tok *
585uint2tokary_internal(const struct uint_tokary dict[], const size_t size,
586 const u_int val)
587{
588 size_t i;
589 /* Try a direct lookup before the full scan. */
590 if (val < size && dict[val].uintval == val)
591 return dict[val].tokary; /* OK if NULL */
592 for (i = 0; i < size; i++)
593 if (dict[i].uintval == val)
594 return dict[i].tokary; /* OK if NULL */
595 return NULL;
596}
597
The Android Open Source Project2949f582009-03-03 19:30:46 -0800598/*
599 * Convert a 32-bit netmask to prefixlen if possible
600 * the function returns the prefix-len; if plen == -1
601 * then conversion was not possible;
602 */
603
604int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700605mask2plen(uint32_t mask)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800606{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700607 uint32_t bitmasks[33] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800608 0x00000000,
609 0x80000000, 0xc0000000, 0xe0000000, 0xf0000000,
610 0xf8000000, 0xfc000000, 0xfe000000, 0xff000000,
611 0xff800000, 0xffc00000, 0xffe00000, 0xfff00000,
612 0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000,
613 0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000,
614 0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00,
615 0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0,
616 0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff
617 };
618 int prefix_len = 32;
619
620 /* let's see if we can transform the mask into a prefixlen */
621 while (prefix_len >= 0) {
622 if (bitmasks[prefix_len] == mask)
623 break;
624 prefix_len--;
625 }
626 return (prefix_len);
627}
628
JP Abgrall53f17a92014-02-12 14:02:41 -0800629int
630mask62plen(const u_char *mask)
631{
632 u_char bitmasks[9] = {
633 0x00,
634 0x80, 0xc0, 0xe0, 0xf0,
635 0xf8, 0xfc, 0xfe, 0xff
636 };
637 int byte;
638 int cidr_len = 0;
639
640 for (byte = 0; byte < 16; byte++) {
641 u_int bits;
642
643 for (bits = 0; bits < (sizeof (bitmasks) / sizeof (bitmasks[0])); bits++) {
644 if (mask[byte] == bitmasks[bits]) {
645 cidr_len += bits;
646 break;
647 }
648 }
649
650 if (mask[byte] != 0xff)
651 break;
652 }
653 return (cidr_len);
654}
JP Abgrall53f17a92014-02-12 14:02:41 -0800655
Elliott Hughes892a68b2015-10-19 14:43:53 -0700656/*
657 * Routine to print out information for text-based protocols such as FTP,
658 * HTTP, SMTP, RTSP, SIP, ....
659 */
660#define MAX_TOKEN 128
661
662/*
663 * Fetch a token from a packet, starting at the specified index,
664 * and return the length of the token.
665 *
666 * Returns 0 on error; yes, this is indistinguishable from an empty
667 * token, but an "empty token" isn't a valid token - it just means
668 * either a space character at the beginning of the line (this
669 * includes a blank line) or no more tokens remaining on the line.
670 */
671static int
672fetch_token(netdissect_options *ndo, const u_char *pptr, u_int idx, u_int len,
673 u_char *tbuf, size_t tbuflen)
674{
675 size_t toklen = 0;
Elliott Hughes820eced2021-08-20 18:00:50 -0700676 u_char c;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700677
678 for (; idx < len; idx++) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700679 if (!ND_TTEST_1(pptr + idx)) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700680 /* ran past end of captured data */
681 return (0);
682 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700683 c = GET_U_1(pptr + idx);
684 if (!ND_ISASCII(c)) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700685 /* not an ASCII character */
686 return (0);
687 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700688 if (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700689 /* end of token */
690 break;
691 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700692 if (!ND_ASCII_ISPRINT(c)) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700693 /* not part of a command token or response code */
694 return (0);
695 }
696 if (toklen + 2 > tbuflen) {
697 /* no room for this character and terminating '\0' */
698 return (0);
699 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700700 tbuf[toklen] = c;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700701 toklen++;
702 }
703 if (toklen == 0) {
704 /* no token */
705 return (0);
706 }
707 tbuf[toklen] = '\0';
708
709 /*
710 * Skip past any white space after the token, until we see
711 * an end-of-line (CR or LF).
712 */
713 for (; idx < len; idx++) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700714 if (!ND_TTEST_1(pptr + idx)) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700715 /* ran past end of captured data */
716 break;
717 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700718 c = GET_U_1(pptr + idx);
719 if (c == '\r' || c == '\n') {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700720 /* end of line */
721 break;
722 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700723 if (!ND_ASCII_ISPRINT(c)) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700724 /* not a printable ASCII character */
725 break;
726 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700727 if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700728 /* beginning of next token */
729 break;
730 }
731 }
732 return (idx);
733}
734
735/*
736 * Scan a buffer looking for a line ending - LF or CR-LF.
737 * Return the index of the character after the line ending or 0 if
738 * we encounter a non-ASCII or non-printable character or don't find
739 * the line ending.
740 */
741static u_int
Elliott Hughes820eced2021-08-20 18:00:50 -0700742print_txt_line(netdissect_options *ndo, const char *prefix,
743 const u_char *pptr, u_int idx, u_int len)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700744{
745 u_int startidx;
746 u_int linelen;
Elliott Hughes820eced2021-08-20 18:00:50 -0700747 u_char c;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700748
749 startidx = idx;
750 while (idx < len) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700751 c = GET_U_1(pptr + idx);
752 if (c == '\n') {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700753 /*
754 * LF without CR; end of line.
755 * Skip the LF and print the line, with the
756 * exception of the LF.
757 */
758 linelen = idx - startidx;
759 idx++;
760 goto print;
Elliott Hughes820eced2021-08-20 18:00:50 -0700761 } else if (c == '\r') {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700762 /* CR - any LF? */
763 if ((idx+1) >= len) {
764 /* not in this packet */
765 return (0);
766 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700767 if (GET_U_1(pptr + idx + 1) == '\n') {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700768 /*
769 * CR-LF; end of line.
770 * Skip the CR-LF and print the line, with
771 * the exception of the CR-LF.
772 */
773 linelen = idx - startidx;
774 idx += 2;
775 goto print;
776 }
777
778 /*
779 * CR followed by something else; treat this
780 * as if it were binary data, and don't print
781 * it.
782 */
783 return (0);
Elliott Hughes820eced2021-08-20 18:00:50 -0700784 } else if (!ND_ASCII_ISPRINT(c) && c != '\t') {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700785 /*
786 * Not a printable ASCII character and not a tab;
787 * treat this as if it were binary data, and
788 * don't print it.
789 */
790 return (0);
791 }
792 idx++;
793 }
794
795 /*
796 * All printable ASCII, but no line ending after that point
797 * in the buffer; treat this as if it were truncated.
798 */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700799 linelen = idx - startidx;
Elliott Hughes820eced2021-08-20 18:00:50 -0700800 ND_PRINT("%s%.*s", prefix, (int)linelen, pptr + startidx);
801 nd_print_trunc(ndo);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700802 return (0);
803
804print:
Elliott Hughes820eced2021-08-20 18:00:50 -0700805 ND_PRINT("%s%.*s", prefix, (int)linelen, pptr + startidx);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700806 return (idx);
807}
808
Elliott Hughes820eced2021-08-20 18:00:50 -0700809/* Assign needed before calling txtproto_print(): ndo->ndo_protocol = "proto" */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700810void
811txtproto_print(netdissect_options *ndo, const u_char *pptr, u_int len,
Elliott Hughes820eced2021-08-20 18:00:50 -0700812 const char **cmds, u_int flags)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700813{
814 u_int idx, eol;
815 u_char token[MAX_TOKEN+1];
816 const char *cmd;
Elliott Hughes820eced2021-08-20 18:00:50 -0700817 int print_this = 0;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700818
819 if (cmds != NULL) {
820 /*
821 * This protocol has more than just request and
822 * response lines; see whether this looks like a
Elliott Hughes820eced2021-08-20 18:00:50 -0700823 * request or response and, if so, print it and,
824 * in verbose mode, print everything after it.
825 *
826 * This is for HTTP-like protocols, where we
827 * want to print requests and responses, but
828 * don't want to print continuations of request
829 * or response bodies in packets that don't
830 * contain the request or response line.
Elliott Hughes892a68b2015-10-19 14:43:53 -0700831 */
832 idx = fetch_token(ndo, pptr, 0, len, token, sizeof(token));
833 if (idx != 0) {
834 /* Is this a valid request name? */
835 while ((cmd = *cmds++) != NULL) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700836 if (ascii_strcasecmp((const char *)token, cmd) == 0) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700837 /* Yes. */
Elliott Hughes820eced2021-08-20 18:00:50 -0700838 print_this = 1;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700839 break;
840 }
841 }
842
843 /*
844 * No - is this a valid response code (3 digits)?
845 *
846 * Is this token the response code, or is the next
847 * token the response code?
848 */
849 if (flags & RESP_CODE_SECOND_TOKEN) {
850 /*
851 * Next token - get it.
852 */
853 idx = fetch_token(ndo, pptr, idx, len, token,
854 sizeof(token));
855 }
856 if (idx != 0) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700857 if (ND_ASCII_ISDIGIT(token[0]) && ND_ASCII_ISDIGIT(token[1]) &&
858 ND_ASCII_ISDIGIT(token[2]) && token[3] == '\0') {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700859 /* Yes. */
Elliott Hughes820eced2021-08-20 18:00:50 -0700860 print_this = 1;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700861 }
862 }
863 }
864 } else {
865 /*
Elliott Hughes820eced2021-08-20 18:00:50 -0700866 * Either:
867 *
868 * 1) This protocol has only request and response lines
869 * (e.g., FTP, where all the data goes over a different
870 * connection); assume the payload is a request or
871 * response.
872 *
873 * or
874 *
875 * 2) This protocol is just text, so that we should
876 * always, at minimum, print the first line and,
877 * in verbose mode, print all lines.
Elliott Hughes892a68b2015-10-19 14:43:53 -0700878 */
Elliott Hughes820eced2021-08-20 18:00:50 -0700879 print_this = 1;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700880 }
881
Elliott Hughes820eced2021-08-20 18:00:50 -0700882 nd_print_protocol_caps(ndo);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700883
Elliott Hughes820eced2021-08-20 18:00:50 -0700884 if (print_this) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700885 /*
886 * In non-verbose mode, just print the protocol, followed
Elliott Hughes820eced2021-08-20 18:00:50 -0700887 * by the first line.
Elliott Hughes892a68b2015-10-19 14:43:53 -0700888 *
889 * In verbose mode, print lines as text until we run out
890 * of characters or see something that's not a
891 * printable-ASCII line.
892 */
893 if (ndo->ndo_vflag) {
894 /*
895 * We're going to print all the text lines in the
896 * request or response; just print the length
897 * on the first line of the output.
898 */
Elliott Hughes820eced2021-08-20 18:00:50 -0700899 ND_PRINT(", length: %u", len);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700900 for (idx = 0;
Elliott Hughes820eced2021-08-20 18:00:50 -0700901 idx < len && (eol = print_txt_line(ndo, "\n\t", pptr, idx, len)) != 0;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700902 idx = eol)
903 ;
904 } else {
905 /*
906 * Just print the first text line.
907 */
Elliott Hughes820eced2021-08-20 18:00:50 -0700908 print_txt_line(ndo, ": ", pptr, 0, len);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700909 }
910 }
911}
912
Elliott Hughes820eced2021-08-20 18:00:50 -0700913#if (defined(__i386__) || defined(_M_IX86) || defined(__X86__) || defined(__x86_64__) || defined(_M_X64)) || \
914 (defined(__arm__) || defined(_M_ARM) || defined(__aarch64__)) || \
915 (defined(__m68k__) && (!defined(__mc68000__) && !defined(__mc68010__))) || \
916 (defined(__ppc__) || defined(__ppc64__) || defined(_M_PPC) || defined(_ARCH_PPC) || defined(_ARCH_PPC64)) || \
917 (defined(__s390__) || defined(__s390x__) || defined(__zarch__)) || \
918 defined(__vax__)
JP Abgrall53f17a92014-02-12 14:02:41 -0800919/*
Elliott Hughes820eced2021-08-20 18:00:50 -0700920 * The procesor natively handles unaligned loads, so just use memcpy()
921 * and memcmp(), to enable those optimizations.
922 *
923 * XXX - are those all the x86 tests we need?
924 * XXX - do we need to worry about ARMv1 through ARMv5, which didn't
925 * support unaligned loads, and, if so, do we need to worry about all
926 * of them, or just some of them, e.g. ARMv5?
927 * XXX - are those the only 68k tests we need not to generated
928 * unaligned accesses if the target is the 68000 or 68010?
929 * XXX - are there any tests we don't need, because some definitions are for
930 * compilers that also predefine the GCC symbols?
931 * XXX - do we need to test for both 32-bit and 64-bit versions of those
932 * architectures in all cases?
933 */
934#else
935/*
936 * The processor doesn't natively handle unaligned loads,
937 * and the compiler might "helpfully" optimize memcpy()
938 * and memcmp(), when handed pointers that would normally
939 * be properly aligned, into sequences that assume proper
940 * alignment.
941 *
942 * Do copies and compares of possibly-unaligned data by
943 * calling routines that wrap memcpy() and memcmp(), to
944 * prevent that optimization.
JP Abgrall53f17a92014-02-12 14:02:41 -0800945 */
946void
947unaligned_memcpy(void *p, const void *q, size_t l)
948{
949 memcpy(p, q, l);
950}
951
952/* As with memcpy(), so with memcmp(). */
953int
954unaligned_memcmp(const void *p, const void *q, size_t l)
955{
956 return (memcmp(p, q, l));
957}
958#endif
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700959