]> The Tcpdump Group git mirrors - tcpdump/blob - util-print.c
Use double rather than float.
[tcpdump] / util-print.c
1 /*
2 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22 /*
23 * txtproto_print() derived from original code by Hannes Gredler
24 * (hannes@juniper.net):
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 */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <netdissect-stdinc.h>
43
44 #include <sys/stat.h>
45
46 #ifdef HAVE_FCNTL_H
47 #include <fcntl.h>
48 #endif
49 #include <ctype.h>
50 #include <stdio.h>
51 #include <stdarg.h>
52 #include <stdlib.h>
53 #include <string.h>
54
55 #include "netdissect.h"
56 #include "ascii_strcasecmp.h"
57 #include "timeval-operations.h"
58
59 int32_t thiszone; /* seconds offset from gmt to local time */
60
61 /*
62 * timestamp display buffer size, the biggest size of both formats is needed
63 * sizeof("0000000000.000000000") > sizeof("00:00:00.000000000")
64 */
65 #define TS_BUF_SIZE sizeof("0000000000.000000000")
66
67 #define TOKBUFSIZE 128
68
69 /*
70 * Print out a null-terminated filename (or other ascii string).
71 * If ep is NULL, assume no truncation check is needed.
72 * Return true if truncated.
73 * Stop at ep (if given) or before the null char, whichever is first.
74 */
75 int
76 fn_print(netdissect_options *ndo,
77 register const u_char *s, register const u_char *ep)
78 {
79 register int ret;
80 register u_char c;
81
82 ret = 1; /* assume truncated */
83 while (ep == NULL || s < ep) {
84 c = *s++;
85 if (c == '\0') {
86 ret = 0;
87 break;
88 }
89 if (!ND_ISASCII(c)) {
90 c = ND_TOASCII(c);
91 ND_PRINT((ndo, "M-"));
92 }
93 if (!ND_ISPRINT(c)) {
94 c ^= 0x40; /* DEL to ?, others to alpha */
95 ND_PRINT((ndo, "^"));
96 }
97 ND_PRINT((ndo, "%c", c));
98 }
99 return(ret);
100 }
101
102 /*
103 * Print out a counted filename (or other ascii string).
104 * If ep is NULL, assume no truncation check is needed.
105 * Return true if truncated.
106 * Stop at ep (if given) or after n bytes, whichever is first.
107 */
108 int
109 fn_printn(netdissect_options *ndo,
110 register const u_char *s, register u_int n, register const u_char *ep)
111 {
112 register u_char c;
113
114 while (n > 0 && (ep == NULL || s < ep)) {
115 n--;
116 c = *s++;
117 if (!ND_ISASCII(c)) {
118 c = ND_TOASCII(c);
119 ND_PRINT((ndo, "M-"));
120 }
121 if (!ND_ISPRINT(c)) {
122 c ^= 0x40; /* DEL to ?, others to alpha */
123 ND_PRINT((ndo, "^"));
124 }
125 ND_PRINT((ndo, "%c", c));
126 }
127 return (n == 0) ? 0 : 1;
128 }
129
130 /*
131 * Print out a null-padded filename (or other ascii string).
132 * If ep is NULL, assume no truncation check is needed.
133 * Return true if truncated.
134 * Stop at ep (if given) or after n bytes or before the null char,
135 * whichever is first.
136 */
137 int
138 fn_printzp(netdissect_options *ndo,
139 register const u_char *s, register u_int n,
140 register const u_char *ep)
141 {
142 register int ret;
143 register u_char c;
144
145 ret = 1; /* assume truncated */
146 while (n > 0 && (ep == NULL || s < ep)) {
147 n--;
148 c = *s++;
149 if (c == '\0') {
150 ret = 0;
151 break;
152 }
153 if (!ND_ISASCII(c)) {
154 c = ND_TOASCII(c);
155 ND_PRINT((ndo, "M-"));
156 }
157 if (!ND_ISPRINT(c)) {
158 c ^= 0x40; /* DEL to ?, others to alpha */
159 ND_PRINT((ndo, "^"));
160 }
161 ND_PRINT((ndo, "%c", c));
162 }
163 return (n == 0) ? 0 : ret;
164 }
165
166 /*
167 * Format the timestamp
168 */
169 static char *
170 ts_format(netdissect_options *ndo
171 #ifndef HAVE_PCAP_SET_TSTAMP_PRECISION
172 _U_
173 #endif
174 , int sec, int usec, char *buf)
175 {
176 const char *format;
177
178 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
179 switch (ndo->ndo_tstamp_precision) {
180
181 case PCAP_TSTAMP_PRECISION_MICRO:
182 format = "%02d:%02d:%02d.%06u";
183 break;
184
185 case PCAP_TSTAMP_PRECISION_NANO:
186 format = "%02d:%02d:%02d.%09u";
187 break;
188
189 default:
190 format = "%02d:%02d:%02d.{unknown}";
191 break;
192 }
193 #else
194 format = "%02d:%02d:%02d.%06u";
195 #endif
196
197 snprintf(buf, TS_BUF_SIZE, format,
198 sec / 3600, (sec % 3600) / 60, sec % 60, usec);
199
200 return buf;
201 }
202
203 /*
204 * Format the timestamp - Unix timeval style
205 */
206 static char *
207 ts_unix_format(netdissect_options *ndo
208 #ifndef HAVE_PCAP_SET_TSTAMP_PRECISION
209 _U_
210 #endif
211 , int sec, int usec, char *buf)
212 {
213 const char *format;
214
215 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
216 switch (ndo->ndo_tstamp_precision) {
217
218 case PCAP_TSTAMP_PRECISION_MICRO:
219 format = "%u.%06u";
220 break;
221
222 case PCAP_TSTAMP_PRECISION_NANO:
223 format = "%u.%09u";
224 break;
225
226 default:
227 format = "%u.{unknown}";
228 break;
229 }
230 #else
231 format = "%u.%06u";
232 #endif
233
234 snprintf(buf, TS_BUF_SIZE, format,
235 (unsigned)sec, (unsigned)usec);
236
237 return buf;
238 }
239
240 /*
241 * Print the timestamp
242 */
243 void
244 ts_print(netdissect_options *ndo,
245 register const struct timeval *tvp)
246 {
247 register int s;
248 struct tm *tm;
249 time_t Time;
250 char buf[TS_BUF_SIZE];
251 static struct timeval tv_ref;
252 struct timeval tv_result;
253 int negative_offset;
254 int nano_prec;
255
256 switch (ndo->ndo_tflag) {
257
258 case 0: /* Default */
259 s = (tvp->tv_sec + thiszone) % 86400;
260 ND_PRINT((ndo, "%s ", ts_format(ndo, s, tvp->tv_usec, buf)));
261 break;
262
263 case 1: /* No time stamp */
264 break;
265
266 case 2: /* Unix timeval style */
267 ND_PRINT((ndo, "%s ", ts_unix_format(ndo,
268 tvp->tv_sec, tvp->tv_usec, buf)));
269 break;
270
271 case 3: /* Microseconds/nanoseconds since previous packet */
272 case 5: /* Microseconds/nanoseconds since first packet */
273 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
274 switch (ndo->ndo_tstamp_precision) {
275 case PCAP_TSTAMP_PRECISION_MICRO:
276 nano_prec = 0;
277 break;
278 case PCAP_TSTAMP_PRECISION_NANO:
279 nano_prec = 1;
280 break;
281 default:
282 nano_prec = 0;
283 break;
284 }
285 #else
286 nano_prec = 0;
287 #endif
288 if (!(netdissect_timevalisset(&tv_ref)))
289 tv_ref = *tvp; /* set timestamp for first packet */
290
291 negative_offset = netdissect_timevalcmp(tvp, &tv_ref, <);
292 if (negative_offset)
293 netdissect_timevalsub(&tv_ref, tvp, &tv_result, nano_prec);
294 else
295 netdissect_timevalsub(tvp, &tv_ref, &tv_result, nano_prec);
296
297 ND_PRINT((ndo, (negative_offset ? "-" : " ")));
298
299 ND_PRINT((ndo, "%s ", ts_format(ndo,
300 tv_result.tv_sec, tv_result.tv_usec, buf)));
301
302 if (ndo->ndo_tflag == 3)
303 tv_ref = *tvp; /* set timestamp for previous packet */
304 break;
305
306 case 4: /* Default + Date */
307 s = (tvp->tv_sec + thiszone) % 86400;
308 Time = (tvp->tv_sec + thiszone) - s;
309 tm = gmtime (&Time);
310 if (!tm)
311 ND_PRINT((ndo, "Date fail "));
312 else
313 ND_PRINT((ndo, "%04d-%02d-%02d %s ",
314 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
315 ts_format(ndo, s, tvp->tv_usec, buf)));
316 break;
317 }
318 }
319
320 /*
321 * Print a relative number of seconds (e.g. hold time, prune timer)
322 * in the form 5m1s. This does no truncation, so 32230861 seconds
323 * is represented as 1y1w1d1h1m1s.
324 */
325 void
326 relts_print(netdissect_options *ndo,
327 int secs)
328 {
329 static const char *lengths[] = {"y", "w", "d", "h", "m", "s"};
330 static const int seconds[] = {31536000, 604800, 86400, 3600, 60, 1};
331 const char **l = lengths;
332 const int *s = seconds;
333
334 if (secs == 0) {
335 ND_PRINT((ndo, "0s"));
336 return;
337 }
338 if (secs < 0) {
339 ND_PRINT((ndo, "-"));
340 secs = -secs;
341 }
342 while (secs > 0) {
343 if (secs >= *s) {
344 ND_PRINT((ndo, "%d%s", secs / *s, *l));
345 secs -= (secs / *s) * *s;
346 }
347 s++;
348 l++;
349 }
350 }
351
352 /*
353 * this is a generic routine for printing unknown data;
354 * we pass on the linefeed plus indentation string to
355 * get a proper output - returns 0 on error
356 */
357
358 int
359 print_unknown_data(netdissect_options *ndo, const u_char *cp,const char *ident,int len)
360 {
361 if (len < 0) {
362 ND_PRINT((ndo,"%sDissector error: print_unknown_data called with negative length",
363 ident));
364 return(0);
365 }
366 if (ndo->ndo_snapend - cp < len)
367 len = ndo->ndo_snapend - cp;
368 if (len < 0) {
369 ND_PRINT((ndo,"%sDissector error: print_unknown_data called with pointer past end of packet",
370 ident));
371 return(0);
372 }
373 hex_print(ndo, ident,cp,len);
374 return(1); /* everything is ok */
375 }
376
377 /*
378 * Convert a token value to a string; use "fmt" if not found.
379 */
380 const char *
381 tok2strbuf(register const struct tok *lp, register const char *fmt,
382 register u_int v, char *buf, size_t bufsize)
383 {
384 if (lp != NULL) {
385 while (lp->s != NULL) {
386 if (lp->v == v)
387 return (lp->s);
388 ++lp;
389 }
390 }
391 if (fmt == NULL)
392 fmt = "#%d";
393
394 (void)snprintf(buf, bufsize, fmt, v);
395 return (const char *)buf;
396 }
397
398 /*
399 * Convert a token value to a string; use "fmt" if not found.
400 */
401 const char *
402 tok2str(register const struct tok *lp, register const char *fmt,
403 register u_int v)
404 {
405 static char buf[4][TOKBUFSIZE];
406 static int idx = 0;
407 char *ret;
408
409 ret = buf[idx];
410 idx = (idx+1) & 3;
411 return tok2strbuf(lp, fmt, v, ret, sizeof(buf[0]));
412 }
413
414 /*
415 * Convert a bit token value to a string; use "fmt" if not found.
416 * this is useful for parsing bitfields, the output strings are seperated
417 * if the s field is positive.
418 */
419 static char *
420 bittok2str_internal(register const struct tok *lp, register const char *fmt,
421 register u_int v, const char *sep)
422 {
423 static char buf[256]; /* our stringbuffer */
424 int buflen=0;
425 register u_int rotbit; /* this is the bit we rotate through all bitpositions */
426 register u_int tokval;
427 const char * sepstr = "";
428
429 while (lp != NULL && lp->s != NULL) {
430 tokval=lp->v; /* load our first value */
431 rotbit=1;
432 while (rotbit != 0) {
433 /*
434 * lets AND the rotating bit with our token value
435 * and see if we have got a match
436 */
437 if (tokval == (v&rotbit)) {
438 /* ok we have found something */
439 buflen+=snprintf(buf+buflen, sizeof(buf)-buflen, "%s%s",
440 sepstr, lp->s);
441 sepstr = sep;
442 break;
443 }
444 rotbit=rotbit<<1; /* no match - lets shift and try again */
445 }
446 lp++;
447 }
448
449 if (buflen == 0)
450 /* bummer - lets print the "unknown" message as advised in the fmt string if we got one */
451 (void)snprintf(buf, sizeof(buf), fmt == NULL ? "#%08x" : fmt, v);
452 return (buf);
453 }
454
455 /*
456 * Convert a bit token value to a string; use "fmt" if not found.
457 * this is useful for parsing bitfields, the output strings are not seperated.
458 */
459 char *
460 bittok2str_nosep(register const struct tok *lp, register const char *fmt,
461 register u_int v)
462 {
463 return (bittok2str_internal(lp, fmt, v, ""));
464 }
465
466 /*
467 * Convert a bit token value to a string; use "fmt" if not found.
468 * this is useful for parsing bitfields, the output strings are comma seperated.
469 */
470 char *
471 bittok2str(register const struct tok *lp, register const char *fmt,
472 register u_int v)
473 {
474 return (bittok2str_internal(lp, fmt, v, ", "));
475 }
476
477 /*
478 * Convert a value to a string using an array; the macro
479 * tok2strary() in <netdissect.h> is the public interface to
480 * this function and ensures that the second argument is
481 * correct for bounds-checking.
482 */
483 const char *
484 tok2strary_internal(register const char **lp, int n, register const char *fmt,
485 register int v)
486 {
487 static char buf[TOKBUFSIZE];
488
489 if (v >= 0 && v < n && lp[v] != NULL)
490 return lp[v];
491 if (fmt == NULL)
492 fmt = "#%d";
493 (void)snprintf(buf, sizeof(buf), fmt, v);
494 return (buf);
495 }
496
497 /*
498 * Convert a 32-bit netmask to prefixlen if possible
499 * the function returns the prefix-len; if plen == -1
500 * then conversion was not possible;
501 */
502
503 int
504 mask2plen(uint32_t mask)
505 {
506 uint32_t bitmasks[33] = {
507 0x00000000,
508 0x80000000, 0xc0000000, 0xe0000000, 0xf0000000,
509 0xf8000000, 0xfc000000, 0xfe000000, 0xff000000,
510 0xff800000, 0xffc00000, 0xffe00000, 0xfff00000,
511 0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000,
512 0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000,
513 0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00,
514 0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0,
515 0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff
516 };
517 int prefix_len = 32;
518
519 /* let's see if we can transform the mask into a prefixlen */
520 while (prefix_len >= 0) {
521 if (bitmasks[prefix_len] == mask)
522 break;
523 prefix_len--;
524 }
525 return (prefix_len);
526 }
527
528 #ifdef INET6
529 int
530 mask62plen(const u_char *mask)
531 {
532 u_char bitmasks[9] = {
533 0x00,
534 0x80, 0xc0, 0xe0, 0xf0,
535 0xf8, 0xfc, 0xfe, 0xff
536 };
537 int byte;
538 int cidr_len = 0;
539
540 for (byte = 0; byte < 16; byte++) {
541 u_int bits;
542
543 for (bits = 0; bits < (sizeof (bitmasks) / sizeof (bitmasks[0])); bits++) {
544 if (mask[byte] == bitmasks[bits]) {
545 cidr_len += bits;
546 break;
547 }
548 }
549
550 if (mask[byte] != 0xff)
551 break;
552 }
553 return (cidr_len);
554 }
555 #endif /* INET6 */
556
557 /*
558 * Routine to print out information for text-based protocols such as FTP,
559 * HTTP, SMTP, RTSP, SIP, ....
560 */
561 #define MAX_TOKEN 128
562
563 /*
564 * Fetch a token from a packet, starting at the specified index,
565 * and return the length of the token.
566 *
567 * Returns 0 on error; yes, this is indistinguishable from an empty
568 * token, but an "empty token" isn't a valid token - it just means
569 * either a space character at the beginning of the line (this
570 * includes a blank line) or no more tokens remaining on the line.
571 */
572 static int
573 fetch_token(netdissect_options *ndo, const u_char *pptr, u_int idx, u_int len,
574 u_char *tbuf, size_t tbuflen)
575 {
576 size_t toklen = 0;
577
578 for (; idx < len; idx++) {
579 if (!ND_TTEST(*(pptr + idx))) {
580 /* ran past end of captured data */
581 return (0);
582 }
583 if (!isascii(*(pptr + idx))) {
584 /* not an ASCII character */
585 return (0);
586 }
587 if (isspace(*(pptr + idx))) {
588 /* end of token */
589 break;
590 }
591 if (!isprint(*(pptr + idx))) {
592 /* not part of a command token or response code */
593 return (0);
594 }
595 if (toklen + 2 > tbuflen) {
596 /* no room for this character and terminating '\0' */
597 return (0);
598 }
599 tbuf[toklen] = *(pptr + idx);
600 toklen++;
601 }
602 if (toklen == 0) {
603 /* no token */
604 return (0);
605 }
606 tbuf[toklen] = '\0';
607
608 /*
609 * Skip past any white space after the token, until we see
610 * an end-of-line (CR or LF).
611 */
612 for (; idx < len; idx++) {
613 if (!ND_TTEST(*(pptr + idx))) {
614 /* ran past end of captured data */
615 break;
616 }
617 if (*(pptr + idx) == '\r' || *(pptr + idx) == '\n') {
618 /* end of line */
619 break;
620 }
621 if (!isascii(*(pptr + idx)) || !isprint(*(pptr + idx))) {
622 /* not a printable ASCII character */
623 break;
624 }
625 if (!isspace(*(pptr + idx))) {
626 /* beginning of next token */
627 break;
628 }
629 }
630 return (idx);
631 }
632
633 /*
634 * Scan a buffer looking for a line ending - LF or CR-LF.
635 * Return the index of the character after the line ending or 0 if
636 * we encounter a non-ASCII or non-printable character or don't find
637 * the line ending.
638 */
639 static u_int
640 print_txt_line(netdissect_options *ndo, const char *protoname,
641 const char *prefix, const u_char *pptr, u_int idx, u_int len)
642 {
643 u_int startidx;
644 u_int linelen;
645
646 startidx = idx;
647 while (idx < len) {
648 ND_TCHECK(*(pptr+idx));
649 if (*(pptr+idx) == '\n') {
650 /*
651 * LF without CR; end of line.
652 * Skip the LF and print the line, with the
653 * exception of the LF.
654 */
655 linelen = idx - startidx;
656 idx++;
657 goto print;
658 } else if (*(pptr+idx) == '\r') {
659 /* CR - any LF? */
660 if ((idx+1) >= len) {
661 /* not in this packet */
662 return (0);
663 }
664 ND_TCHECK(*(pptr+idx+1));
665 if (*(pptr+idx+1) == '\n') {
666 /*
667 * CR-LF; end of line.
668 * Skip the CR-LF and print the line, with
669 * the exception of the CR-LF.
670 */
671 linelen = idx - startidx;
672 idx += 2;
673 goto print;
674 }
675
676 /*
677 * CR followed by something else; treat this
678 * as if it were binary data, and don't print
679 * it.
680 */
681 return (0);
682 } else if (!isascii(*(pptr+idx)) ||
683 (!isprint(*(pptr+idx)) && *(pptr+idx) != '\t')) {
684 /*
685 * Not a printable ASCII character and not a tab;
686 * treat this as if it were binary data, and
687 * don't print it.
688 */
689 return (0);
690 }
691 idx++;
692 }
693
694 /*
695 * All printable ASCII, but no line ending after that point
696 * in the buffer; treat this as if it were truncated.
697 */
698 trunc:
699 linelen = idx - startidx;
700 ND_PRINT((ndo, "%s%.*s[!%s]", prefix, (int)linelen, pptr + startidx,
701 protoname));
702 return (0);
703
704 print:
705 ND_PRINT((ndo, "%s%.*s", prefix, (int)linelen, pptr + startidx));
706 return (idx);
707 }
708
709 void
710 txtproto_print(netdissect_options *ndo, const u_char *pptr, u_int len,
711 const char *protoname, const char **cmds, u_int flags)
712 {
713 u_int idx, eol;
714 u_char token[MAX_TOKEN+1];
715 const char *cmd;
716 int is_reqresp = 0;
717 const char *pnp;
718
719 if (cmds != NULL) {
720 /*
721 * This protocol has more than just request and
722 * response lines; see whether this looks like a
723 * request or response.
724 */
725 idx = fetch_token(ndo, pptr, 0, len, token, sizeof(token));
726 if (idx != 0) {
727 /* Is this a valid request name? */
728 while ((cmd = *cmds++) != NULL) {
729 if (ascii_strcasecmp((const char *)token, cmd) == 0) {
730 /* Yes. */
731 is_reqresp = 1;
732 break;
733 }
734 }
735
736 /*
737 * No - is this a valid response code (3 digits)?
738 *
739 * Is this token the response code, or is the next
740 * token the response code?
741 */
742 if (flags & RESP_CODE_SECOND_TOKEN) {
743 /*
744 * Next token - get it.
745 */
746 idx = fetch_token(ndo, pptr, idx, len, token,
747 sizeof(token));
748 }
749 if (idx != 0) {
750 if (isdigit(token[0]) && isdigit(token[1]) &&
751 isdigit(token[2]) && token[3] == '\0') {
752 /* Yes. */
753 is_reqresp = 1;
754 }
755 }
756 }
757 } else {
758 /*
759 * This protocol has only request and response lines
760 * (e.g., FTP, where all the data goes over a
761 * different connection); assume the payload is
762 * a request or response.
763 */
764 is_reqresp = 1;
765 }
766
767 /* Capitalize the protocol name */
768 for (pnp = protoname; *pnp != '\0'; pnp++)
769 ND_PRINT((ndo, "%c", toupper(*pnp)));
770
771 if (is_reqresp) {
772 /*
773 * In non-verbose mode, just print the protocol, followed
774 * by the first line as the request or response info.
775 *
776 * In verbose mode, print lines as text until we run out
777 * of characters or see something that's not a
778 * printable-ASCII line.
779 */
780 if (ndo->ndo_vflag) {
781 /*
782 * We're going to print all the text lines in the
783 * request or response; just print the length
784 * on the first line of the output.
785 */
786 ND_PRINT((ndo, ", length: %u", len));
787 for (idx = 0;
788 idx < len && (eol = print_txt_line(ndo, protoname, "\n\t", pptr, idx, len)) != 0;
789 idx = eol)
790 ;
791 } else {
792 /*
793 * Just print the first text line.
794 */
795 print_txt_line(ndo, protoname, ": ", pptr, 0, len);
796 }
797 }
798 }
799
800 void
801 safeputs(netdissect_options *ndo,
802 const u_char *s, const u_int maxlen)
803 {
804 u_int idx = 0;
805
806 while (*s && idx < maxlen) {
807 safeputchar(ndo, *s);
808 idx++;
809 s++;
810 }
811 }
812
813 void
814 safeputchar(netdissect_options *ndo,
815 const u_char c)
816 {
817 ND_PRINT((ndo, (c < 0x80 && ND_ISPRINT(c)) ? "%c" : "\\0x%02x", c));
818 }
819
820 #ifdef LBL_ALIGN
821 /*
822 * Some compilers try to optimize memcpy(), using the alignment constraint
823 * on the argument pointer type. by using this function, we try to avoid the
824 * optimization.
825 */
826 void
827 unaligned_memcpy(void *p, const void *q, size_t l)
828 {
829 memcpy(p, q, l);
830 }
831
832 /* As with memcpy(), so with memcmp(). */
833 int
834 unaligned_memcmp(const void *p, const void *q, size_t l)
835 {
836 return (memcmp(p, q, l));
837 }
838 #endif
839