Debian Codegraph @

listen/ripdump.c

raw
1 /* RIP packet tracing
2 * Copyright 1991 Phil Karn, KA9Q
3 *
4 * Changes Copyright (c) 1993 Jeff White - N0POY, All Rights Reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Rehack for RIP-2 (RFC1388) by N0POY 4/1993
30 *
31 * Beta release 11/10/93 V0.91
32 *
33 * 2/19/94 release V1.0
34 *
35 */
36
37 #include <stdio.h>
38
39 #include "listen.h"
40
41 #define RIP_VERSION_2 2
42 #define RIP_VERSION_98 98
43
44 #define RIP_HEADER 4
45 #define RIP_ENTRY 20
46
47 #define RIP_AF_INET 2 /* IP Family */
48
49 #define RIPCMD_REQUEST 1 /* want info */
50 #define RIPCMD_RESPONSE 2 /* responding to request */
51
52 #define RIP98_ENTRY 6
53
54 static struct mask_struct {
55 unsigned int mask;
56 unsigned int width;
57 } mask_table[] = {
58 {
59 0xFFFFFFFF, 32}, {
60 0xFFFFFFFE, 31}, {
61 0xFFFFFFFC, 30}, {
62 0xFFFFFFF8, 29}, {
63 0xFFFFFFF0, 28}, {
64 0xFFFFFFE0, 27}, {
65 0xFFFFFFC0, 26}, {
66 0xFFFFFF80, 25}, {
67 0xFFFFFF00, 24}, {
68 0xFFFFFE00, 23}, {
69 0xFFFFFC00, 22}, {
70 0xFFFFF800, 21}, {
71 0xFFFFF000, 20}, {
72 0xFFFFE000, 19}, {
73 0xFFFFC000, 18}, {
74 0xFFFF8000, 17}, {
75 0xFFFF0000, 16}, {
76 0xFFFE0000, 15}, {
77 0xFFFC0000, 14}, {
78 0xFFF80000, 13}, {
79 0xFFF00000, 12}, {
80 0xFFE00000, 11}, {
81 0xFFC00000, 10}, {
82 0xFF800000, 9}, {
83 0xFF000000, 8}, {
84 0xFE000000, 7}, {
85 0xFC000000, 6}, {
86 0xF8000000, 5}, {
87 0xF0000000, 4}, {
88 0xE0000000, 3}, {
89 0xC0000000, 2}, {
90 0x80000000, 1}, {
91 0x00000000, 0},};
92
93 static unsigned int mask2width(unsigned int mask)
94 {
95 struct mask_struct *t;
96
97 for (t = mask_table; t->mask != 0; t++)
98 if (mask == t->mask)
99 return t->width;
100
101 return 0;
102 }
103
104 void rip_dump(unsigned char *data, int length)
105 {
106 int i;
107 int cmd;
108 int version;
109 int domain;
110 char ipaddmask[25];
111
112 lprintf(T_PROTOCOL, "RIP: ");
113
114 cmd = data[0];
115 version = data[1];
116 domain = get16(data + 2);
117
118 length -= RIP_HEADER;
119 data += RIP_HEADER;
120
121 switch (cmd) {
122 case RIPCMD_REQUEST:
123 lprintf(T_IPHDR, "REQUEST");
124 break;
125
126 case RIPCMD_RESPONSE:
127 lprintf(T_IPHDR, "RESPONSE");
128 break;
129
130 default:
131 lprintf(T_IPHDR, " cmd %u", cmd);
132 break;
133 }
134
135 switch (version) {
136 case RIP_VERSION_98: /* IPGATE * */
137 lprintf(T_IPHDR, " vers %u entries %u\n", version,
138 length / RIP98_ENTRY);
139
140 i = 0;
141 while (length >= RIP98_ENTRY) {
142 sprintf(ipaddmask, "%u.%u.%u.%u/%-2u", data[0],
143 data[1], data[2], data[3], data[4]);
144 lprintf(T_ADDR, "%-16s %-3u ", ipaddmask, data[5]);
145
146 if ((++i % 3) == 0)
147 lprintf(T_IPHDR, "\n");
148
149 length -= RIP98_ENTRY;
150 data += RIP98_ENTRY;
151 }
152
153 if ((i % 3) != 0)
154 lprintf(T_IPHDR, "\n");
155 break;
156
157 default:
158 lprintf(T_IPHDR, " vers %u entries %u domain %u:\n",
159 version, length / RIP_ENTRY, domain);
160
161 i = 0;
162 while (length >= RIP_ENTRY) {
163 if (get16(data + 0) != RIP_AF_INET) {
164 /* Skip non-IP addresses */
165 length -= RIP_ENTRY;
166 data += RIP_ENTRY;
167 continue;
168 }
169
170 if (version >= RIP_VERSION_2) {
171 sprintf(ipaddmask, "%u.%u.%u.%u/%-4d",
172 data[4], data[5], data[6], data[7],
173 mask2width(get32(data + 8)));
174 } else {
175 sprintf(ipaddmask, "%u.%u.%u.%u/??",
176 data[4], data[5], data[6],
177 data[7]);
178 }
179
180 lprintf(T_ADDR, "%-20s%-3u", ipaddmask,
181 get32(data + 16));
182
183 if ((++i % 3) == 0)
184 lprintf(T_IPHDR, "\n");
185
186 length -= RIP_ENTRY;
187 data += RIP_ENTRY;
188 }
189
190 if ((i % 3) != 0)
191 lprintf(T_IPHDR, "\n");
192 break;
193 }
194 }