]> The Tcpdump Group git mirrors - tcpdump/blob - print-loopback.c
Have a #define to squelch -Wunused-result warnings.
[tcpdump] / print-loopback.c
1 /*
2 * Copyright (c) 2014 The TCPDUMP project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
18 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /* \summary: Loopback Protocol printer */
29
30 /*
31 * originally defined as the Ethernet Configuration Testing Protocol.
32 * specification:
33 * https://web.archive.org/web/20060919181108/http://www.mit.edu/people/jhawk/ctp.pdf
34 */
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include "netdissect-stdinc.h"
41
42 #define ND_LONGJMP_FROM_TCHECK
43 #include "netdissect.h"
44 #include "extract.h"
45 #include "addrtoname.h"
46
47
48 #define LOOPBACK_REPLY 1
49 #define LOOPBACK_FWDDATA 2
50
51 static const struct tok fcode_str[] = {
52 { LOOPBACK_REPLY, "Reply" },
53 { LOOPBACK_FWDDATA, "Forward Data" },
54 { 0, NULL }
55 };
56
57 static void
58 loopback_message_print(netdissect_options *ndo,
59 const u_char *cp, u_int len)
60 {
61 uint16_t function;
62
63 if (len < 2)
64 goto invalid;
65 /* function */
66 function = GET_LE_U_2(cp);
67 cp += 2;
68 len -= 2;
69 ND_PRINT(", %s", tok2str(fcode_str, " invalid (%u)", function));
70
71 switch (function) {
72 case LOOPBACK_REPLY:
73 if (len < 2)
74 goto invalid;
75 /* receipt number */
76 ND_PRINT(", receipt number %u", GET_LE_U_2(cp));
77 cp += 2;
78 len -= 2;
79 /* data */
80 ND_PRINT(", data (%u octets)", len);
81 ND_TCHECK_LEN(cp, len);
82 break;
83 case LOOPBACK_FWDDATA:
84 if (len < MAC48_LEN)
85 goto invalid;
86 /* forwarding address */
87 ND_PRINT(", forwarding address %s", GET_MAC48_STRING(cp));
88 cp += MAC48_LEN;
89 len -= MAC48_LEN;
90 /* data */
91 ND_PRINT(", data (%u octets)", len);
92 ND_TCHECK_LEN(cp, len);
93 break;
94 default:
95 ND_TCHECK_LEN(cp, len);
96 break;
97 }
98 return;
99
100 invalid:
101 nd_print_invalid(ndo);
102 ND_TCHECK_LEN(cp, len);
103 }
104
105 void
106 loopback_print(netdissect_options *ndo,
107 const u_char *cp, u_int len)
108 {
109 uint16_t skipCount;
110
111 ndo->ndo_protocol = "loopback";
112 ND_PRINT("Loopback");
113 if (len < 2)
114 goto invalid;
115 /* skipCount */
116 skipCount = GET_LE_U_2(cp);
117 cp += 2;
118 len -= 2;
119 ND_PRINT(", skipCount %u", skipCount);
120 if (skipCount % 8)
121 ND_PRINT(" (bogus)");
122 if (skipCount > len)
123 goto invalid;
124 /* the octets to skip */
125 ND_TCHECK_LEN(cp, skipCount);
126 cp += skipCount;
127 len -= skipCount;
128 /* the first message to decode */
129 loopback_message_print(ndo, cp, len);
130 return;
131
132 invalid:
133 nd_print_invalid(ndo);
134 ND_TCHECK_LEN(cp, len);
135 }
136