]> The Tcpdump Group git mirrors - tcpdump/blob - print-rip.c
We no longer use "packetp" for anything, so eliminate it. (If any
[tcpdump] / print-rip.c
1 /*
2 * Copyright (c) 1989, 1990, 1991, 1993, 1994, 1996
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22 #ifndef lint
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-rip.c,v 1.54 2002-12-11 07:14:07 guy Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <tcpdump-stdinc.h>
32
33 #include <stdio.h>
34 #include <string.h>
35
36 #include "interface.h"
37 #include "addrtoname.h"
38 #include "extract.h" /* must come after interface.h */
39
40 struct rip {
41 u_int8_t rip_cmd; /* request/response */
42 u_int8_t rip_vers; /* protocol version # */
43 u_int16_t rip_zero2; /* unused */
44 };
45 #define RIPCMD_REQUEST 1 /* want info */
46 #define RIPCMD_RESPONSE 2 /* responding to request */
47 #define RIPCMD_TRACEON 3 /* turn tracing on */
48 #define RIPCMD_TRACEOFF 4 /* turn it off */
49 #define RIPCMD_POLL 5 /* want info from everybody */
50 #define RIPCMD_POLLENTRY 6 /* poll for entry */
51
52 #define RIP_AUTHLEN 16
53
54 struct rip_netinfo {
55 u_int16_t rip_family;
56 u_int16_t rip_tag;
57 u_int32_t rip_dest;
58 u_int32_t rip_dest_mask;
59 u_int32_t rip_router;
60 u_int32_t rip_metric; /* cost of route */
61 };
62
63 static void
64 rip_printblk(const u_char *cp, const u_char *ep)
65 {
66 for (; cp < ep; cp += 2)
67 printf(" %04x", EXTRACT_16BITS(cp));
68 return;
69 }
70
71 static void
72 rip_entry_print_v1(register const struct rip_netinfo *ni)
73 {
74 register u_short family;
75
76 /* RFC 1058 */
77 family = EXTRACT_16BITS(&ni->rip_family);
78 if (family != AF_INET) {
79 printf(" [family %d:", family);
80 rip_printblk((u_char *)&ni->rip_tag,
81 (u_char *)&ni->rip_metric +
82 sizeof(ni->rip_metric));
83 printf("]");
84 return;
85 }
86 if (EXTRACT_16BITS(&ni->rip_tag) ||
87 EXTRACT_32BITS(&ni->rip_dest_mask) ||
88 EXTRACT_32BITS(&ni->rip_router)) {
89 /* MBZ fields not zero */
90 printf(" [");
91 rip_printblk((u_char *)&ni->rip_family,
92 (u_char *)&ni->rip_metric +
93 sizeof(ni->rip_metric));
94 printf("]");
95 return;
96 }
97 printf(" {%s}(%d)", ipaddr_string(&ni->rip_dest),
98 EXTRACT_32BITS(&ni->rip_metric));
99 }
100
101 static void
102 rip_entry_print_v2(register const struct rip_netinfo *ni)
103 {
104 register u_char *p;
105 register u_short family;
106 u_char buf[RIP_AUTHLEN];
107
108 /* RFC 1723 */
109 family = EXTRACT_16BITS(&ni->rip_family);
110 if (family == 0xFFFF) {
111 if (EXTRACT_16BITS(&ni->rip_tag) == 2) {
112 memcpy(buf, &ni->rip_dest, sizeof(buf));
113 buf[sizeof(buf)-1] = '\0';
114 for (p = buf; *p; p++) {
115 if (!isprint(*p))
116 break;
117 }
118 if (!*p) {
119 printf(" [password %s]", buf);
120 } else {
121 printf(" [password: ");
122 rip_printblk((u_char *)&ni->rip_dest,
123 (u_char *)&ni->rip_metric +
124 sizeof(ni->rip_metric));
125 printf("]");
126 }
127 } else {
128 printf(" [auth %d:",
129 EXTRACT_16BITS(&ni->rip_tag));
130 rip_printblk((u_char *)&ni->rip_dest,
131 (u_char *)&ni->rip_metric +
132 sizeof(ni->rip_metric));
133 printf("]");
134 }
135 } else if (family != AF_INET) {
136 printf(" [family %d:", family);
137 rip_printblk((u_char *)&ni->rip_tag,
138 (u_char *)&ni->rip_metric +
139 sizeof(ni->rip_metric));
140 printf("]");
141 return;
142 } else { /* AF_INET */
143 printf(" {%s", ipaddr_string(&ni->rip_dest));
144 if (EXTRACT_32BITS(&ni->rip_dest_mask))
145 printf("/%s", ipaddr_string(&ni->rip_dest_mask));
146 if (EXTRACT_32BITS(&ni->rip_router))
147 printf("->%s", ipaddr_string(&ni->rip_router));
148 if (EXTRACT_16BITS(&ni->rip_tag))
149 printf(" tag %04x", EXTRACT_16BITS(&ni->rip_tag));
150 printf("}(%d)", EXTRACT_32BITS(&ni->rip_metric));
151 }
152 }
153
154 void
155 rip_print(const u_char *dat, u_int length)
156 {
157 register const struct rip *rp;
158 register const struct rip_netinfo *ni;
159 register u_int i, j;
160 register int trunc;
161
162 if (snapend < dat) {
163 printf(" [|rip]");
164 return;
165 }
166 i = snapend - dat;
167 if (i > length)
168 i = length;
169 if (i < sizeof(*rp)) {
170 printf(" [|rip]");
171 return;
172 }
173 i -= sizeof(*rp);
174
175 rp = (struct rip *)dat;
176 switch (rp->rip_vers) {
177 case 0:
178 /*
179 * RFC 1058.
180 *
181 * XXX - RFC 1058 says
182 *
183 * 0 Datagrams whose version number is zero are to be ignored.
184 * These are from a previous version of the protocol, whose
185 * packet format was machine-specific.
186 *
187 * so perhaps we should just dump the first few words of
188 * the packet, in hex.
189 */
190 printf(" RIPv0: ");
191 ni = (struct rip_netinfo *)(rp + 1);
192 rip_printblk((u_char *)&ni->rip_family,
193 (u_char *)&ni->rip_metric +
194 sizeof(ni->rip_metric));
195 break;
196 default:
197 switch (rp->rip_cmd) {
198 case RIPCMD_REQUEST:
199 printf(" RIPv%d-req %d", rp->rip_vers, length);
200 break;
201 case RIPCMD_RESPONSE:
202 j = length / sizeof(*ni);
203 if (j * sizeof(*ni) + 4 != length)
204 printf(" RIPv%d-resp [items %d] [%d]:",
205 rp->rip_vers, j, length);
206 else
207 printf(" RIPv%d-resp [items %d]:",
208 rp->rip_vers, j);
209 trunc = (i / sizeof(*ni)) != j;
210 ni = (struct rip_netinfo *)(rp + 1);
211 for (; i >= sizeof(*ni); ++ni) {
212 if (rp->rip_vers == 1)
213 rip_entry_print_v1(ni);
214 else
215 rip_entry_print_v2(ni);
216 i -= sizeof(*ni);
217 }
218 if (trunc)
219 printf("[|rip]");
220 break;
221 case RIPCMD_TRACEON:
222 printf(" RIPv%d-traceon %d: \"", rp->rip_vers, length);
223 (void)fn_print((const u_char *)(rp + 1), snapend);
224 fputs("\"", stdout);
225 break;
226 case RIPCMD_TRACEOFF:
227 printf(" RIPv%d-traceoff %d", rp->rip_vers, length);
228 break;
229 case RIPCMD_POLL:
230 printf(" RIPv%d-poll %d", rp->rip_vers, length);
231 break;
232 case RIPCMD_POLLENTRY:
233 printf(" RIPv%d-pollentry %d", rp->rip_vers, length);
234 break;
235 default:
236 printf(" RIPv%d-#%d %d", rp->rip_vers, rp->rip_cmd,
237 length);
238 break;
239 }
240 }
241 }