]> The Tcpdump Group git mirrors - tcpdump/blob - print-rip.c
remove non-STDC code
[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.42 2000-07-01 03:39:09 assar Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <sys/socket.h>
34
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
38 #include <netinet/ip_var.h>
39 #include <netinet/udp.h>
40 #include <netinet/udp_var.h>
41
42 #include <stdio.h>
43 #include <ctype.h>
44
45 #include "interface.h"
46 #include "addrtoname.h"
47 #include "extract.h" /* must come after interface.h */
48
49 struct rip {
50 u_char rip_cmd; /* request/response */
51 u_char rip_vers; /* protocol version # */
52 u_short rip_zero2; /* unused */
53 };
54 #define RIPCMD_REQUEST 1 /* want info */
55 #define RIPCMD_RESPONSE 2 /* responding to request */
56 #define RIPCMD_TRACEON 3 /* turn tracing on */
57 #define RIPCMD_TRACEOFF 4 /* turn it off */
58 #define RIPCMD_POLL 5 /* want info from everybody */
59 #define RIPCMD_POLLENTRY 6 /* poll for entry */
60
61 #define RIP_AUTHLEN 16
62
63 struct rip_netinfo {
64 u_short rip_family;
65 u_short rip_tag;
66 u_int32_t rip_dest;
67 u_int32_t rip_dest_mask;
68 u_int32_t rip_router;
69 u_int32_t rip_metric; /* cost of route */
70 };
71
72 static void
73 rip_printblk(const u_char *cp, const u_char *ep)
74 {
75 for (; cp < ep; cp += 2)
76 printf(" %04x", EXTRACT_16BITS(cp));
77 return;
78 }
79
80 static void
81 rip_entry_print_v1(register int vers, register const struct rip_netinfo *ni)
82 {
83 register u_short family;
84
85 /* RFC 1058 */
86 family = EXTRACT_16BITS(&ni->rip_family);
87 if (family != AF_INET) {
88 printf(" [family %d:", family);
89 rip_printblk((u_char *)&ni->rip_tag,
90 (u_char *)&ni->rip_metric +
91 sizeof(ni->rip_metric));
92 printf("]");
93 return;
94 }
95 if (ni->rip_tag || ni->rip_dest_mask || ni->rip_router) {
96 /* MBZ fields not zero */
97 printf(" [");
98 rip_printblk((u_char *)&ni->rip_family,
99 (u_char *)&ni->rip_metric +
100 sizeof(ni->rip_metric));
101 printf("]");
102 return;
103 }
104 printf(" {%s}(%d)", ipaddr_string(&ni->rip_dest),
105 EXTRACT_32BITS(&ni->rip_metric));
106 }
107
108 static void
109 rip_entry_print_v2(register int vers, register const struct rip_netinfo *ni)
110 {
111 register u_char *p;
112 register u_short family;
113 char buf[RIP_AUTHLEN];
114
115 /* RFC 1723 */
116 family = EXTRACT_16BITS(&ni->rip_family);
117 if (family == 0xFFFF) {
118 if (EXTRACT_16BITS(&ni->rip_tag) == 2) {
119 memcpy(buf, &ni->rip_dest, sizeof(buf));
120 buf[sizeof(buf)-1] = '\0';
121 for (p = buf; *p; p++) {
122 if (!isprint(*p))
123 break;
124 }
125 if (!*p) {
126 printf(" [password %s]", buf);
127 } else {
128 printf(" [password: ");
129 rip_printblk((u_char *)&ni->rip_dest,
130 (u_char *)&ni->rip_metric +
131 sizeof(ni->rip_metric));
132 printf("]");
133 }
134 } else {
135 printf(" [auth %d:",
136 EXTRACT_16BITS(&ni->rip_tag));
137 rip_printblk((u_char *)&ni->rip_dest,
138 (u_char *)&ni->rip_metric +
139 sizeof(ni->rip_metric));
140 printf("]");
141 }
142 } else if (family != AF_INET) {
143 printf(" [family %d:", family);
144 rip_printblk((u_char *)&ni->rip_tag,
145 (u_char *)&ni->rip_metric +
146 sizeof(ni->rip_metric));
147 printf("]");
148 return;
149 } else { /* AF_INET */
150 printf(" {%s", ipaddr_string(&ni->rip_dest));
151 if (ni->rip_dest_mask)
152 printf("/%s", ipaddr_string(&ni->rip_dest_mask));
153 if (ni->rip_router)
154 printf("->%s", ipaddr_string(&ni->rip_router));
155 if (ni->rip_tag)
156 printf(" tag %04x", EXTRACT_16BITS(&ni->rip_tag));
157 printf("}(%d)", EXTRACT_32BITS(&ni->rip_metric));
158 }
159 }
160
161 void
162 rip_print(const u_char *dat, u_int length)
163 {
164 register const struct rip *rp;
165 register const struct rip_netinfo *ni;
166 register int i, j, trunc;
167
168 i = min(length, snapend - dat) - sizeof(*rp);
169 if (i < 0) {
170 printf(" [|rip]");
171 return;
172 }
173
174 rp = (struct rip *)dat;
175 switch (rp->rip_vers) {
176 case 0:
177 /* RFC 1058 */
178 printf(" RIPv0: ");
179 rip_printblk((u_char *)&ni->rip_family,
180 (u_char *)&ni->rip_metric +
181 sizeof(ni->rip_metric));
182 break;
183 default:
184 switch (rp->rip_cmd) {
185 case RIPCMD_REQUEST:
186 printf(" RIPv%d-req %d", rp->rip_vers, length);
187 break;
188 case RIPCMD_RESPONSE:
189 j = length / sizeof(*ni);
190 if (j * sizeof(*ni) != length - 4)
191 printf(" RIPv%d-resp [items %d] [%d]:",
192 rp->rip_vers, j, length);
193 else
194 printf(" RIPv%d-resp [items %d]:",
195 rp->rip_vers, j);
196 trunc = (i / sizeof(*ni)) != j;
197 ni = (struct rip_netinfo *)(rp + 1);
198 for (; (i -= sizeof(*ni)) >= 0; ++ni) {
199 if (rp->rip_vers == 1)
200 rip_entry_print_v1(rp->rip_vers, ni);
201 else
202 rip_entry_print_v2(rp->rip_vers, ni);
203 }
204 if (trunc)
205 printf("[|rip]");
206 break;
207 case RIPCMD_TRACEON:
208 printf(" RIPv%d-traceon %d: \"", rp->rip_vers, length);
209 (void)fn_print((const u_char *)(rp + 1), snapend);
210 fputs("\"\n", stdout);
211 break;
212 case RIPCMD_TRACEOFF:
213 printf(" RIPv%d-traceoff %d", rp->rip_vers, length);
214 break;
215 case RIPCMD_POLL:
216 printf(" RIPv%d-poll %d", rp->rip_vers, length);
217 break;
218 case RIPCMD_POLLENTRY:
219 printf(" RIPv%d-pollentry %d", rp->rip_vers, length);
220 break;
221 default:
222 printf(" RIPv%d-#%d %d", rp->rip_vers, rp->rip_cmd,
223 length);
224 break;
225 }
226 }
227 }