More updates from Hannes Gredler.
[obnox/wireshark/wip.git] / packet-rip.c
1 /* packet-rip.c
2  * Routines for RIPv1 and RIPv2 packet disassembly
3  * (c) Copyright Hannes R. Boehm <hannes@boehm.org>
4  *
5  * $Id: packet-rip.c,v 1.24 2001/03/13 21:34:23 gram Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
9  * Copyright 1998 Gerald Combs
10  * 
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26  
27 #include "config.h"
28
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32
33 #ifdef HAVE_NETINET_IN_H
34 #include <netinet/in.h>
35 #endif
36
37 #include <string.h>
38 #include <glib.h>
39 #include "packet.h"
40
41 #define UDP_PORT_RIP    520
42
43 #define RIPv1   1
44 #define RIPv2   2
45
46 static const value_string version_vals[] = {
47         { RIPv1, "RIPv1" },
48         { RIPv2, "RIPv2" },
49         { 0,     NULL }
50 };
51
52 static const value_string command_vals[] = {
53         { 1, "Request" },
54         { 2, "Response" },
55         { 3, "Traceon" },
56         { 4, "Traceoff" },
57         { 5, "Vendor specific (Sun)" },
58         { 0, NULL }
59 };
60
61 #define RIP_HEADER_LENGTH 4
62 #define RIP_ENTRY_LENGTH 20
63
64 static int proto_rip = -1;
65
66 static gint ett_rip = -1;
67 static gint ett_rip_vec = -1;
68
69 static void dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
70     proto_tree *tree);
71 static void dissect_rip_authentication(tvbuff_t *tvb, int offset,
72     proto_tree *tree);
73
74 static void 
75 dissect_rip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
76 {
77     int offset = 0;
78     proto_tree *rip_tree = NULL;
79     proto_item *ti;
80     guint8 command;
81     guint8 version;
82     guint16 family;
83     guint reported_length;
84
85     if (check_col(pinfo->fd, COL_PROTOCOL))
86         col_set_str(pinfo->fd, COL_PROTOCOL, "RIP");
87     if (check_col(pinfo->fd, COL_INFO))
88         col_clear(pinfo->fd, COL_INFO);
89
90     command = tvb_get_guint8(tvb, 0);
91     version = tvb_get_guint8(tvb, 1);
92   
93     if (check_col(pinfo->fd, COL_PROTOCOL))
94         col_add_str(pinfo->fd, COL_PROTOCOL,
95                     val_to_str(version, version_vals, "RIP"));
96     if (check_col(pinfo->fd, COL_INFO))
97         col_add_str(pinfo->fd, COL_INFO,
98                     val_to_str(command, command_vals, "Unknown command (%u)"));
99
100     if (tree) {
101         ti = proto_tree_add_item(tree, proto_rip, tvb, 0, tvb_length(tvb), FALSE);
102         rip_tree = proto_item_add_subtree(ti, ett_rip);
103
104         proto_tree_add_text(rip_tree, tvb, 0, 1, "Command: %u (%s)", command,
105                 val_to_str(command, command_vals, "Unknown"));
106         proto_tree_add_text(rip_tree, tvb, 1, 1, "Version: %u", version);
107         if (version == RIPv2)
108             proto_tree_add_text(rip_tree, tvb, 2, 2, "Routing Domain: %u",
109                                 tvb_get_ntohs(tvb, 2));
110
111         /* skip header */
112         offset = RIP_HEADER_LENGTH;
113
114         /* zero or more entries */
115         reported_length = tvb_reported_length(tvb);
116         while (offset < reported_length) {
117             family = tvb_get_ntohs(tvb, offset);
118             switch (family) {
119             case 2: /* IP */
120                 dissect_ip_rip_vektor(tvb, offset, version, rip_tree);
121                 break;
122             case 0xFFFF:
123                 dissect_rip_authentication(tvb, offset, rip_tree);
124                 break;
125             default:
126                 proto_tree_add_text(rip_tree, tvb, offset,
127                                 RIP_ENTRY_LENGTH, "Unknown address family %u",
128                                 family);
129                 break;
130             }
131
132             offset += RIP_ENTRY_LENGTH;
133         }
134     }
135 }
136
137 static void
138 dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
139                       proto_tree *tree)
140 {
141     proto_item *ti;
142     proto_tree *rip_vektor_tree;
143     const guint8 *ip;
144     guint32 metric;
145
146     ip = tvb_get_ptr(tvb, offset+4, 4);
147     metric = tvb_get_ntohl(tvb, offset+16);
148     ti = proto_tree_add_text(tree, tvb, offset,
149                              RIP_ENTRY_LENGTH, "IP Address: %s, Metric: %u",
150                              ip_to_str(ip), metric);
151     rip_vektor_tree = proto_item_add_subtree(ti, ett_rip_vec);
152            
153     proto_tree_add_text(rip_vektor_tree, tvb, offset, 2,
154                         "Address Family ID: IP"); 
155     if (version == RIPv2)
156         proto_tree_add_text(rip_vektor_tree, tvb, offset + 2, 2,
157                             "Route Tag: %u", tvb_get_ntohs(tvb, offset+2));
158     proto_tree_add_text(rip_vektor_tree, tvb, offset + 4, 4, "IP Address: %s",
159                                 ip_to_str(ip));
160     if (version == RIPv2) {
161         proto_tree_add_text(rip_vektor_tree, tvb, offset + 8, 4,
162                             "Netmask: %s", 
163                             ip_to_str(tvb_get_ptr(tvb, offset + 8, 4))); 
164         proto_tree_add_text(rip_vektor_tree, tvb, offset + 12, 4,
165                             "Next Hop: %s", 
166                              ip_to_str(tvb_get_ptr(tvb, offset+12, 4))); 
167     }
168     proto_tree_add_text(rip_vektor_tree, tvb, offset + 16, 4, "Metric: %u",
169                         metric);
170 }
171
172 static void
173 dissect_rip_authentication(tvbuff_t *tvb, int offset, proto_tree *tree)
174 {
175     proto_item *ti;
176     proto_tree *rip_authentication_tree;
177     guint16 authtype;
178     char authentication[16];
179
180     ti = proto_tree_add_text(tree, tvb, offset, RIP_ENTRY_LENGTH,
181                              "Authentication");
182     rip_authentication_tree = proto_item_add_subtree(ti, ett_rip_vec);
183
184     authtype = tvb_get_ntohs(tvb, offset + 2);
185     proto_tree_add_text(rip_authentication_tree, tvb, offset + 2, 2,
186                                 "Authentication type: %u", authtype);
187     if (authtype == 2) {
188         tvb_get_nstringz0(tvb, offset + 4, 16, authentication);
189         proto_tree_add_text(rip_authentication_tree, tvb, offset + 4, 16,
190                                 "Password: %s", authentication);
191     }
192 }
193
194 void
195 proto_register_rip(void)
196 {
197 /*  static hf_register_info hf[] = {
198         { &variable,
199         { "Name",           "rip.abbreviation", TYPE, VALS_POINTER }},
200     };*/
201     static gint *ett[] = {
202         &ett_rip,
203         &ett_rip_vec,
204     };
205
206     proto_rip = proto_register_protocol("Routing Information Protocol",
207                                         "RIP", "rip");
208 /*  proto_register_field_array(proto_rip, hf, array_length(hf));*/
209     proto_register_subtree_array(ett, array_length(ett));
210 }
211
212 void
213 proto_reg_handoff_rip(void)
214 {
215     dissector_add("udp.port", UDP_PORT_RIP, dissect_rip, proto_rip);
216 }