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