The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 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 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 22 | /* |
| 23 | * txtproto_print() derived from original code by Hannes Gredler |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 24 | * (hannes@gredler.at): |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 25 | * |
| 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 Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 37 | |
| 38 | #ifdef HAVE_CONFIG_H |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 39 | #include <config.h> |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 40 | #endif |
| 41 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 42 | #include "netdissect-stdinc.h" |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 43 | |
| 44 | #include <sys/stat.h> |
| 45 | |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 46 | #ifdef HAVE_FCNTL_H |
| 47 | #include <fcntl.h> |
| 48 | #endif |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 49 | #include <stdio.h> |
| 50 | #include <stdarg.h> |
| 51 | #include <stdlib.h> |
| 52 | #include <string.h> |
| 53 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 54 | #include "netdissect-ctype.h" |
| 55 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 56 | #include "netdissect.h" |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 57 | #include "extract.h" |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 58 | #include "ascii_strcasecmp.h" |
| 59 | #include "timeval-operations.h" |
| 60 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 61 | #define TOKBUFSIZE 128 |
| 62 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 63 | enum date_flag { WITHOUT_DATE = 0, WITH_DATE = 1 }; |
| 64 | enum time_flag { UTC_TIME = 0, LOCAL_TIME = 1 }; |
| 65 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 66 | /* |
| 67 | * Print out a character, filtering out the non-printable ones |
| 68 | */ |
| 69 | void |
| 70 | fn_print_char(netdissect_options *ndo, u_char c) |
| 71 | { |
| 72 | if (!ND_ISASCII(c)) { |
| 73 | c = ND_TOASCII(c); |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 74 | ND_PRINT("M-"); |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 75 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 76 | if (!ND_ASCII_ISPRINT(c)) { |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 77 | c ^= 0x40; /* DEL to ?, others to alpha */ |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 78 | ND_PRINT("^"); |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 79 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 80 | ND_PRINT("%c", c); |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 81 | } |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 82 | |
| 83 | /* |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 84 | * 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 Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 87 | */ |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 88 | void |
| 89 | fn_print_str(netdissect_options *ndo, const u_char *s) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 90 | { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 91 | while (*s != '\0') { |
| 92 | fn_print_char(ndo, *s); |
| 93 | s++; |
| 94 | } |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 98 | * 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 Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 110 | * Return the number of bytes of string processed, including the |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 111 | * 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 Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 114 | */ |
| 115 | u_int |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 116 | nd_printztn(netdissect_options *ndo, |
| 117 | const u_char *s, u_int n, const u_char *ep) |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 118 | { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 119 | u_int bytes; |
| 120 | u_char c; |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 121 | |
| 122 | bytes = 0; |
| 123 | for (;;) { |
| 124 | if (n == 0 || (ep != NULL && s >= ep)) { |
| 125 | /* |
| 126 | * Truncated. This includes "no null before we |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 127 | * got to the end of the fixed-length buffer or |
| 128 | * the end of the packet". |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 129 | * |
| 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 Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 139 | c = GET_U_1(s); |
| 140 | s++; |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 141 | bytes++; |
| 142 | n--; |
| 143 | if (c == '\0') { |
| 144 | /* End of string */ |
| 145 | break; |
| 146 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 147 | fn_print_char(ndo, c); |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 148 | } |
| 149 | return(bytes); |
| 150 | } |
| 151 | |
| 152 | /* |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 153 | * Print out a counted filename (or other ASCII string), part of |
| 154 | * the packet buffer. |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 155 | * If ep is NULL, assume no truncation check is needed. |
| 156 | * Return true if truncated. |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 157 | * Stop at ep (if given) or after n bytes, whichever is first. |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 158 | */ |
| 159 | int |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 160 | nd_printn(netdissect_options *ndo, |
| 161 | const u_char *s, u_int n, const u_char *ep) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 162 | { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 163 | u_char c; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 164 | |
| 165 | while (n > 0 && (ep == NULL || s < ep)) { |
| 166 | n--; |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 167 | c = GET_U_1(s); |
| 168 | s++; |
| 169 | fn_print_char(ndo, c); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 170 | } |
| 171 | return (n == 0) ? 0 : 1; |
| 172 | } |
| 173 | |
| 174 | /* |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 175 | * 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 Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 180 | */ |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 181 | void |
| 182 | nd_printjnp(netdissect_options *ndo, const u_char *s, u_int n) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 183 | { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 184 | u_char c; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 185 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 186 | while (n > 0) { |
| 187 | c = GET_U_1(s); |
| 188 | if (c == '\0') |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 189 | break; |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 190 | fn_print_char(ndo, c); |
| 191 | n--; |
| 192 | s++; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 193 | } |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 197 | * Print the timestamp .FRAC part (Microseconds/nanoseconds) |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 198 | */ |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 199 | static void |
| 200 | ts_frac_print(netdissect_options *ndo, long usec) |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 201 | { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 202 | #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION |
| 203 | switch (ndo->ndo_tstamp_precision) { |
| 204 | |
| 205 | case PCAP_TSTAMP_PRECISION_MICRO: |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 206 | ND_PRINT(".%06u", (unsigned)usec); |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 207 | break; |
| 208 | |
| 209 | case PCAP_TSTAMP_PRECISION_NANO: |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 210 | ND_PRINT(".%09u", (unsigned)usec); |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 211 | break; |
| 212 | |
| 213 | default: |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 214 | ND_PRINT(".{unknown}"); |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 215 | break; |
| 216 | } |
| 217 | #else |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 218 | ND_PRINT(".%06u", (unsigned)usec); |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 219 | #endif |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /* |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 223 | * 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 Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 226 | */ |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 227 | static void |
| 228 | ts_date_hmsfrac_print(netdissect_options *ndo, long sec, long usec, |
| 229 | enum date_flag date_flag, enum time_flag time_flag) |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 230 | { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 231 | time_t Time = sec; |
| 232 | struct tm *tm; |
| 233 | char timestr[32]; |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 234 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 235 | if ((unsigned)sec & 0x80000000) { |
| 236 | ND_PRINT("[Error converting time]"); |
| 237 | return; |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 238 | } |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 239 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 240 | if (time_flag == LOCAL_TIME) |
| 241 | tm = localtime(&Time); |
| 242 | else |
| 243 | tm = gmtime(&Time); |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 244 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 245 | 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 | */ |
| 261 | static void |
| 262 | ts_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 Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 274 | * Print the timestamp |
| 275 | */ |
| 276 | void |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 277 | ts_print(netdissect_options *ndo, |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 278 | const struct timeval *tvp) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 279 | { |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 280 | static struct timeval tv_ref; |
| 281 | struct timeval tv_result; |
| 282 | int negative_offset; |
| 283 | int nano_prec; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 284 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 285 | switch (ndo->ndo_tflag) { |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 286 | |
| 287 | case 0: /* Default */ |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 288 | ts_date_hmsfrac_print(ndo, tvp->tv_sec, tvp->tv_usec, |
| 289 | WITHOUT_DATE, LOCAL_TIME); |
| 290 | ND_PRINT(" "); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 291 | break; |
| 292 | |
| 293 | case 1: /* No time stamp */ |
| 294 | break; |
| 295 | |
| 296 | case 2: /* Unix timeval style */ |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 297 | ts_unix_print(ndo, tvp->tv_sec, tvp->tv_usec); |
| 298 | ND_PRINT(" "); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 299 | break; |
| 300 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 301 | 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 Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 320 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 321 | 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 Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 326 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 327 | 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 Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 331 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 332 | if (ndo->ndo_tflag == 3) |
| 333 | tv_ref = *tvp; /* set timestamp for previous packet */ |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 334 | break; |
| 335 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 336 | 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 Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 340 | break; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /* |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 345 | * Print an unsigned relative number of seconds (e.g. hold time, prune timer) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 346 | * in the form 5m1s. This does no truncation, so 32230861 seconds |
| 347 | * is represented as 1y1w1d1h1m1s. |
| 348 | */ |
| 349 | void |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 350 | unsigned_relts_print(netdissect_options *ndo, |
| 351 | uint32_t secs) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 352 | { |
| 353 | static const char *lengths[] = {"y", "w", "d", "h", "m", "s"}; |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 354 | static const u_int seconds[] = {31536000, 604800, 86400, 3600, 60, 1}; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 355 | const char **l = lengths; |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 356 | const u_int *s = seconds; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 357 | |
| 358 | if (secs == 0) { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 359 | ND_PRINT("0s"); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 360 | return; |
| 361 | } |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 362 | while (secs > 0) { |
| 363 | if (secs >= *s) { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 364 | ND_PRINT("%u%s", secs / *s, *l); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 365 | secs -= (secs / *s) * *s; |
| 366 | } |
| 367 | s++; |
| 368 | l++; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /* |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 373 | * 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 | */ |
| 377 | void |
| 378 | signed_relts_print(netdissect_options *ndo, |
| 379 | int32_t secs) |
| 380 | { |
| 381 | if (secs < 0) { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 382 | ND_PRINT("-"); |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 383 | 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 Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 408 | /* Print the truncated string */ |
| 409 | void nd_print_trunc(netdissect_options *ndo) |
| 410 | { |
| 411 | ND_PRINT(" [|%s]", ndo->ndo_protocol); |
| 412 | } |
| 413 | |
| 414 | /* Print the protocol name */ |
| 415 | void nd_print_protocol(netdissect_options *ndo) |
| 416 | { |
| 417 | ND_PRINT("%s", ndo->ndo_protocol); |
| 418 | } |
| 419 | |
| 420 | /* Print the protocol name in caps (uppercases) */ |
| 421 | void 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 */ |
| 429 | void nd_print_invalid(netdissect_options *ndo) |
| 430 | { |
| 431 | ND_PRINT(" (invalid)"); |
| 432 | } |
| 433 | |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 434 | /* |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 435 | * 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 | |
| 440 | int |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 441 | print_unknown_data(netdissect_options *ndo, const u_char *cp, |
| 442 | const char *ident, u_int len) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 443 | { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 444 | 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 Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 450 | return(0); |
| 451 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 452 | 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 Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 455 | return(1); /* everything is ok */ |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Convert a token value to a string; use "fmt" if not found. |
| 460 | */ |
| 461 | const char * |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 462 | tok2strbuf(const struct tok *lp, const char *fmt, |
| 463 | u_int v, char *buf, size_t bufsize) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 464 | { |
| 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 Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 481 | * Uses tok2strbuf() on one of four local static buffers of size TOKBUFSIZE |
| 482 | * in round-robin fashion. |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 483 | */ |
| 484 | const char * |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 485 | tok2str(const struct tok *lp, const char *fmt, |
| 486 | u_int v) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 487 | { |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 488 | static char buf[4][TOKBUFSIZE]; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 489 | 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 Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 499 | * this is useful for parsing bitfields, the output strings are separated |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 500 | * if the s field is positive. |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 501 | * |
| 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 Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 504 | */ |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 505 | static char * |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 506 | bittok2str_internal(const struct tok *lp, const char *fmt, |
| 507 | u_int v, const char *sep) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 508 | { |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 509 | static char buf[1024+1]; /* our string buffer */ |
| 510 | char *bufp = buf; |
| 511 | size_t space_left = sizeof(buf), string_size; |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 512 | const char * sepstr = ""; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 513 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 514 | 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 Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 532 | } |
| 533 | lp++; |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 534 | } |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 535 | |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 536 | if (bufp == buf) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 537 | /* bummer - lets print the "unknown" message as advised in the fmt string if we got one */ |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 538 | (void)snprintf(buf, sizeof(buf), fmt == NULL ? "#%08x" : fmt, v); |
| 539 | return (buf); |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /* |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 543 | * Convert a bit token value to a string; use "fmt" if not found. |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 544 | * this is useful for parsing bitfields, the output strings are not separated. |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 545 | */ |
| 546 | char * |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 547 | bittok2str_nosep(const struct tok *lp, const char *fmt, |
| 548 | u_int v) |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 549 | { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 550 | return (bittok2str_internal(lp, fmt, v, "")); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | /* |
| 554 | * Convert a bit token value to a string; use "fmt" if not found. |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 555 | * this is useful for parsing bitfields, the output strings are comma separated. |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 556 | */ |
| 557 | char * |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 558 | bittok2str(const struct tok *lp, const char *fmt, |
| 559 | u_int v) |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 560 | { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 561 | return (bittok2str_internal(lp, fmt, v, ", ")); |
JP Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /* |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 565 | * Convert a value to a string using an array; the macro |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 566 | * tok2strary() in <netdissect.h> is the public interface to |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 567 | * this function and ensures that the second argument is |
| 568 | * correct for bounds-checking. |
| 569 | */ |
| 570 | const char * |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 571 | tok2strary_internal(const char **lp, int n, const char *fmt, |
| 572 | int v) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 573 | { |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 574 | static char buf[TOKBUFSIZE]; |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 575 | |
| 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 Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 584 | const struct tok * |
| 585 | uint2tokary_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 Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 598 | /* |
| 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 | |
| 604 | int |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 605 | mask2plen(uint32_t mask) |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 606 | { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 607 | uint32_t bitmasks[33] = { |
The Android Open Source Project | 2949f58 | 2009-03-03 19:30:46 -0800 | [diff] [blame] | 608 | 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 Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 629 | int |
| 630 | mask62plen(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 Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 655 | |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 656 | /* |
| 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 | */ |
| 671 | static int |
| 672 | fetch_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 Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 676 | u_char c; |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 677 | |
| 678 | for (; idx < len; idx++) { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 679 | if (!ND_TTEST_1(pptr + idx)) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 680 | /* ran past end of captured data */ |
| 681 | return (0); |
| 682 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 683 | c = GET_U_1(pptr + idx); |
| 684 | if (!ND_ISASCII(c)) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 685 | /* not an ASCII character */ |
| 686 | return (0); |
| 687 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 688 | if (c == ' ' || c == '\t' || c == '\r' || c == '\n') { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 689 | /* end of token */ |
| 690 | break; |
| 691 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 692 | if (!ND_ASCII_ISPRINT(c)) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 693 | /* 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 Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 700 | tbuf[toklen] = c; |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 701 | 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 Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 714 | if (!ND_TTEST_1(pptr + idx)) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 715 | /* ran past end of captured data */ |
| 716 | break; |
| 717 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 718 | c = GET_U_1(pptr + idx); |
| 719 | if (c == '\r' || c == '\n') { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 720 | /* end of line */ |
| 721 | break; |
| 722 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 723 | if (!ND_ASCII_ISPRINT(c)) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 724 | /* not a printable ASCII character */ |
| 725 | break; |
| 726 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 727 | if (c != ' ' && c != '\t' && c != '\r' && c != '\n') { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 728 | /* 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 | */ |
| 741 | static u_int |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 742 | print_txt_line(netdissect_options *ndo, const char *prefix, |
| 743 | const u_char *pptr, u_int idx, u_int len) |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 744 | { |
| 745 | u_int startidx; |
| 746 | u_int linelen; |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 747 | u_char c; |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 748 | |
| 749 | startidx = idx; |
| 750 | while (idx < len) { |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 751 | c = GET_U_1(pptr + idx); |
| 752 | if (c == '\n') { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 753 | /* |
| 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 Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 761 | } else if (c == '\r') { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 762 | /* CR - any LF? */ |
| 763 | if ((idx+1) >= len) { |
| 764 | /* not in this packet */ |
| 765 | return (0); |
| 766 | } |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 767 | if (GET_U_1(pptr + idx + 1) == '\n') { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 768 | /* |
| 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 Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 784 | } else if (!ND_ASCII_ISPRINT(c) && c != '\t') { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 785 | /* |
| 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 Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 799 | linelen = idx - startidx; |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 800 | ND_PRINT("%s%.*s", prefix, (int)linelen, pptr + startidx); |
| 801 | nd_print_trunc(ndo); |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 802 | return (0); |
| 803 | |
| 804 | print: |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 805 | ND_PRINT("%s%.*s", prefix, (int)linelen, pptr + startidx); |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 806 | return (idx); |
| 807 | } |
| 808 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 809 | /* Assign needed before calling txtproto_print(): ndo->ndo_protocol = "proto" */ |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 810 | void |
| 811 | txtproto_print(netdissect_options *ndo, const u_char *pptr, u_int len, |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 812 | const char **cmds, u_int flags) |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 813 | { |
| 814 | u_int idx, eol; |
| 815 | u_char token[MAX_TOKEN+1]; |
| 816 | const char *cmd; |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 817 | int print_this = 0; |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 818 | |
| 819 | if (cmds != NULL) { |
| 820 | /* |
| 821 | * This protocol has more than just request and |
| 822 | * response lines; see whether this looks like a |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 823 | * 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 Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 831 | */ |
| 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 Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 836 | if (ascii_strcasecmp((const char *)token, cmd) == 0) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 837 | /* Yes. */ |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 838 | print_this = 1; |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 839 | 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 Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 857 | if (ND_ASCII_ISDIGIT(token[0]) && ND_ASCII_ISDIGIT(token[1]) && |
| 858 | ND_ASCII_ISDIGIT(token[2]) && token[3] == '\0') { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 859 | /* Yes. */ |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 860 | print_this = 1; |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | } |
| 864 | } else { |
| 865 | /* |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 866 | * 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 Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 878 | */ |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 879 | print_this = 1; |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 882 | nd_print_protocol_caps(ndo); |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 883 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 884 | if (print_this) { |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 885 | /* |
| 886 | * In non-verbose mode, just print the protocol, followed |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 887 | * by the first line. |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 888 | * |
| 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 Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 899 | ND_PRINT(", length: %u", len); |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 900 | for (idx = 0; |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 901 | idx < len && (eol = print_txt_line(ndo, "\n\t", pptr, idx, len)) != 0; |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 902 | idx = eol) |
| 903 | ; |
| 904 | } else { |
| 905 | /* |
| 906 | * Just print the first text line. |
| 907 | */ |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 908 | print_txt_line(ndo, ": ", pptr, 0, len); |
Elliott Hughes | 892a68b | 2015-10-19 14:43:53 -0700 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | } |
| 912 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 913 | #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 Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 919 | /* |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 920 | * 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 Abgrall | 53f17a9 | 2014-02-12 14:02:41 -0800 | [diff] [blame] | 945 | */ |
| 946 | void |
| 947 | unaligned_memcpy(void *p, const void *q, size_t l) |
| 948 | { |
| 949 | memcpy(p, q, l); |
| 950 | } |
| 951 | |
| 952 | /* As with memcpy(), so with memcmp(). */ |
| 953 | int |
| 954 | unaligned_memcmp(const void *p, const void *q, size_t l) |
| 955 | { |
| 956 | return (memcmp(p, q, l)); |
| 957 | } |
| 958 | #endif |
Elliott Hughes | e2e3bd1 | 2017-05-15 10:59:29 -0700 | [diff] [blame] | 959 | |