]> The Tcpdump Group git mirrors - tcpdump/blob - print-rip.c
put __attribute__((packed)) to packet headers. s/u_short/u_int16_t/ and so
[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.53 2002-11-09 17:19:30 itojun 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 } __attribute__((packed));
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 } __attribute__((packed));
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 (ni->rip_tag || ni->rip_dest_mask || ni->rip_router) {
87 /* MBZ fields not zero */
88 printf(" [");
89 rip_printblk((u_char *)&ni->rip_family,
90 (u_char *)&ni->rip_metric +
91 sizeof(ni->rip_metric));
92 printf("]");
93 return;
94 }
95 printf(" {%s}(%d)", ipaddr_string(&ni->rip_dest),
96 EXTRACT_32BITS(&ni->rip_metric));
97 }
98
99 static void
100 rip_entry_print_v2(register const struct rip_netinfo *ni)
101 {
102 register u_char *p;
103 register u_short family;
104 u_char buf[RIP_AUTHLEN];
105
106 /* RFC 1723 */
107 family = EXTRACT_16BITS(&ni->rip_family);
108 if (family == 0xFFFF) {
109 if (EXTRACT_16BITS(&ni->rip_tag) == 2) {
110 memcpy(buf, &ni->rip_dest, sizeof(buf));
111 buf[sizeof(buf)-1] = '\0';
112 for (p = buf; *p; p++) {
113 if (!isprint(*p))
114 break;
115 }
116 if (!*p) {
117 printf(" [password %s]", buf);
118 } else {
119 printf(" [password: ");
120 rip_printblk((u_char *)&ni->rip_dest,
121 (u_char *)&ni->rip_metric +
122 sizeof(ni->rip_metric));
123 printf("]");
124 }
125 } else {
126 printf(" [auth %d:",
127 EXTRACT_16BITS(&ni->rip_tag));
128 rip_printblk((u_char *)&ni->rip_dest,
129 (u_char *)&ni->rip_metric +
130 sizeof(ni->rip_metric));
131 printf("]");
132 }
133 } else if (family != AF_INET) {
134 printf(" [family %d:", family);
135 rip_printblk((u_char *)&ni->rip_tag,
136 (u_char *)&ni->rip_metric +
137 sizeof(ni->rip_metric));
138 printf("]");
139 return;
140 } else { /* AF_INET */
141 printf(" {%s", ipaddr_string(&ni->rip_dest));
142 if (ni->rip_dest_mask)
143 printf("/%s", ipaddr_string(&ni->rip_dest_mask));
144 if (ni->rip_router)
145 printf("->%s", ipaddr_string(&ni->rip_router));
146 if (ni->rip_tag)
147 printf(" tag %04x", EXTRACT_16BITS(&ni->rip_tag));
148 printf("}(%d)", EXTRACT_32BITS(&ni->rip_metric));
149 }
150 }
151
152 void
153 rip_print(const u_char *dat, u_int length)
154 {
155 register const struct rip *rp;
156 register const struct rip_netinfo *ni;
157 register u_int i, j;
158 register int trunc;
159
160 if (snapend < dat) {
161 printf(" [|rip]");
162 return;
163 }
164 i = snapend - dat;
165 if (i > length)
166 i = length;
167 if (i < sizeof(*rp)) {
168 printf(" [|rip]");
169 return;
170 }
171 i -= sizeof(*rp);
172
173 rp = (struct rip *)dat;
174 switch (rp->rip_vers) {
175 case 0:
176 /*
177 * RFC 1058.
178 *
179 * XXX - RFC 1058 says
180 *
181 * 0 Datagrams whose version number is zero are to be ignored.
182 * These are from a previous version of the protocol, whose
183 * packet format was machine-specific.
184 *
185 * so perhaps we should just dump the first few words of
186 * the packet, in hex.
187 */
188 printf(" RIPv0: ");
189 ni = (struct rip_netinfo *)(rp + 1);
190 rip_printblk((u_char *)&ni->rip_family,
191 (u_char *)&ni->rip_metric +
192 sizeof(ni->rip_metric));
193 break;
194 default:
195 switch (rp->rip_cmd) {
196 case RIPCMD_REQUEST:
197 printf(" RIPv%d-req %d", rp->rip_vers, length);
198 break;
199 case RIPCMD_RESPONSE:
200 j = length / sizeof(*ni);
201 if (j * sizeof(*ni) + 4 != length)
202 printf(" RIPv%d-resp [items %d] [%d]:",
203 rp->rip_vers, j, length);
204 else
205 printf(" RIPv%d-resp [items %d]:",
206 rp->rip_vers, j);
207 trunc = (i / sizeof(*ni)) != j;
208 ni = (struct rip_netinfo *)(rp + 1);
209 for (; i >= sizeof(*ni); ++ni) {
210 if (rp->rip_vers == 1)
211 rip_entry_print_v1(ni);
212 else
213 rip_entry_print_v2(ni);
214 i -= sizeof(*ni);
215 }
216 if (trunc)
217 printf("[|rip]");
218 break;
219 case RIPCMD_TRACEON:
220 printf(" RIPv%d-traceon %d: \"", rp->rip_vers, length);
221 (void)fn_print((const u_char *)(rp + 1), snapend);
222 fputs("\"", stdout);
223 break;
224 case RIPCMD_TRACEOFF:
225 printf(" RIPv%d-traceoff %d", rp->rip_vers, length);
226 break;
227 case RIPCMD_POLL:
228 printf(" RIPv%d-poll %d", rp->rip_vers, length);
229 break;
230 case RIPCMD_POLLENTRY:
231 printf(" RIPv%d-pollentry %d", rp->rip_vers, length);
232 break;
233 default:
234 printf(" RIPv%d-#%d %d", rp->rip_vers, rp->rip_cmd,
235 length);
236 break;
237 }
238 }
239 }