Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / tcpdump / print-rip.c
blob070b8dc9331e75afdccb1f21c60029974dc2cc0f
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 1989, 1990, 1991, 1993, 1994, 1996
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 #include <sys/cdefs.h>
25 #ifndef lint
26 #if 0
27 static const char rcsid[] _U_ =
28 "@(#) Header: /tcpdump/master/tcpdump/print-rip.c,v 1.57 2003/11/16 09:36:34 guy Exp (LBL)";
29 #else
30 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
31 #endif
32 #endif
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
38 #include <tcpdump-stdinc.h>
40 #include <stdio.h>
41 #include <string.h>
43 #include "interface.h"
44 #include "addrtoname.h"
45 #include "extract.h" /* must come after interface.h */
47 struct rip {
48 u_int8_t rip_cmd; /* request/response */
49 u_int8_t rip_vers; /* protocol version # */
50 u_int8_t unused[2]; /* unused */
53 #define RIPCMD_REQUEST 1 /* want info */
54 #define RIPCMD_RESPONSE 2 /* responding to request */
55 #define RIPCMD_TRACEON 3 /* turn tracing on */
56 #define RIPCMD_TRACEOFF 4 /* turn it off */
57 #define RIPCMD_POLL 5 /* want info from everybody */
58 #define RIPCMD_POLLENTRY 6 /* poll for entry */
60 static const struct tok rip_cmd_values[] = {
61 { RIPCMD_REQUEST, "Request" },
62 { RIPCMD_RESPONSE, "Response" },
63 { RIPCMD_TRACEON, "Trace on" },
64 { RIPCMD_TRACEOFF, "Trace off" },
65 { RIPCMD_POLL, "Poll" },
66 { RIPCMD_POLLENTRY, "Poll Entry" },
67 { 0, NULL}
70 #define RIP_AUTHLEN 16
71 #define RIP_ROUTELEN 20
74 * rfc 1723
76 * 0 1 2 3 3
77 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
78 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
79 * | Command (1) | Version (1) | unused |
80 * +---------------+---------------+-------------------------------+
81 * | Address Family Identifier (2) | Route Tag (2) |
82 * +-------------------------------+-------------------------------+
83 * | IP Address (4) |
84 * +---------------------------------------------------------------+
85 * | Subnet Mask (4) |
86 * +---------------------------------------------------------------+
87 * | Next Hop (4) |
88 * +---------------------------------------------------------------+
89 * | Metric (4) |
90 * +---------------------------------------------------------------+
94 struct rip_netinfo {
95 u_int16_t rip_family;
96 u_int16_t rip_tag;
97 u_int32_t rip_dest;
98 u_int32_t rip_dest_mask;
99 u_int32_t rip_router;
100 u_int32_t rip_metric; /* cost of route */
103 static void
104 rip_entry_print_v1(register const struct rip_netinfo *ni)
106 register u_short family;
108 /* RFC 1058 */
109 family = EXTRACT_16BITS(&ni->rip_family);
110 if (family != AF_INET) {
111 printf("\n\t AFI: %u:", family);
112 print_unknown_data((u_int8_t *)&ni->rip_family,"\n\t ",RIP_ROUTELEN);
113 return;
115 if (EXTRACT_16BITS(&ni->rip_tag) ||
116 EXTRACT_32BITS(&ni->rip_dest_mask) ||
117 EXTRACT_32BITS(&ni->rip_router)) {
118 /* MBZ fields not zero */
119 print_unknown_data((u_int8_t *)&ni->rip_family,"\n\t ",RIP_ROUTELEN);
120 return;
121 } /* AF_INET */
122 printf("\n\t %s, metric: %u",
123 ipaddr_string(&ni->rip_dest),
124 EXTRACT_32BITS(&ni->rip_metric));
127 static void
128 rip_entry_print_v2(register const struct rip_netinfo *ni)
130 register u_char *p;
131 register u_short family;
132 u_char buf[RIP_AUTHLEN];
134 family = EXTRACT_16BITS(&ni->rip_family);
135 if (family == 0xFFFF) { /* 16 bytes authentication ? */
136 if (EXTRACT_16BITS(&ni->rip_tag) == 2) { /* simple text authentication ? */
137 memcpy(buf, &ni->rip_dest, sizeof(buf));
138 buf[sizeof(buf)-1] = '\0';
139 for (p = buf; *p; p++) {
140 if (!isprint(*p))
141 break;
143 printf("\n\t Simple Text Authentication data: %s", buf);
144 } else {
145 printf("\n\t Unknown (%u) Authentication data:",
146 EXTRACT_16BITS(&ni->rip_tag));
147 print_unknown_data((u_int8_t *)&ni->rip_dest,"\n\t ",RIP_AUTHLEN);
149 } else if (family != AF_INET) {
150 printf("\n\t AFI: %u", family);
151 print_unknown_data((u_int8_t *)&ni->rip_tag,"\n\t ",RIP_ROUTELEN-2);
152 return;
153 } else { /* AF_INET */
154 printf("\n\t AFI: IPv4: %15s/%-2d, tag 0x%04x, metric: %u, next-hop: ",
155 ipaddr_string(&ni->rip_dest),
156 mask2plen(EXTRACT_32BITS(&ni->rip_dest_mask)),
157 EXTRACT_16BITS(&ni->rip_tag),
158 EXTRACT_32BITS(&ni->rip_metric));
159 if (EXTRACT_32BITS(&ni->rip_router))
160 printf("%s", ipaddr_string(&ni->rip_router));
161 else
162 printf("self");
166 void
167 rip_print(const u_char *dat, u_int length)
169 register const struct rip *rp;
170 register const struct rip_netinfo *ni;
171 register u_int i, j;
172 register int trunc;
174 if (snapend < dat) {
175 printf(" [|rip]");
176 return;
178 i = snapend - dat;
179 if (i > length)
180 i = length;
181 if (i < sizeof(*rp)) {
182 printf(" [|rip]");
183 return;
185 i -= sizeof(*rp);
187 rp = (struct rip *)dat;
189 printf("%sRIPv%u",
190 (vflag >= 1) ? "\n\t" : "",
191 rp->rip_vers);
193 switch (rp->rip_vers) {
194 case 0:
196 * RFC 1058.
198 * XXX - RFC 1058 says
200 * 0 Datagrams whose version number is zero are to be ignored.
201 * These are from a previous version of the protocol, whose
202 * packet format was machine-specific.
204 * so perhaps we should just dump the packet, in hex.
206 print_unknown_data((u_int8_t *)&rp->rip_cmd,"\n\t",length);
207 break;
208 default:
209 /* dump version and lets see if we know the commands name*/
210 printf(", %s, length: %u",
211 tok2str(rip_cmd_values,
212 "unknown command (%u)",
213 rp->rip_cmd),
214 length);
216 if (vflag < 1)
217 return;
219 switch (rp->rip_cmd) {
220 case RIPCMD_RESPONSE:
221 j = length / sizeof(*ni);
222 printf(", routes: %u",j);
223 trunc = (i / sizeof(*ni)) != j;
224 ni = (struct rip_netinfo *)(rp + 1);
225 for (; i >= sizeof(*ni); ++ni) {
226 if (rp->rip_vers == 1)
227 rip_entry_print_v1(ni);
228 else if (rp->rip_vers == 2)
229 rip_entry_print_v2(ni);
230 else
231 break;
232 i -= sizeof(*ni);
234 if (trunc)
235 printf("[|rip]");
236 break;
238 case RIPCMD_REQUEST:
239 case RIPCMD_TRACEOFF:
240 case RIPCMD_POLL:
241 case RIPCMD_POLLENTRY:
242 break;
244 case RIPCMD_TRACEON:
245 /* fall through */
246 default:
247 if (vflag <= 1) {
248 if(!print_unknown_data((u_int8_t *)rp,"\n\t",length))
249 return;
251 break;
253 /* do we want to see an additionally hexdump ? */
254 if (vflag> 1) {
255 if(!print_unknown_data((u_int8_t *)rp,"\n\t",length))
256 return;