Have "proto_register_protocol()" build a list of data structures for
[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.21 2001/01/03 06:55:31 guy 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     CHECK_DISPLAY_AS_DATA(proto_rip, tvb, pinfo, tree);
86
87     pinfo->current_proto = "RIP";
88
89     command = tvb_get_guint8(tvb, 0);
90     version = tvb_get_guint8(tvb, 1);
91   
92     if (check_col(pinfo->fd, COL_PROTOCOL))
93         col_add_str(pinfo->fd, COL_PROTOCOL,
94                     val_to_str(version, version_vals, "RIP"));
95     if (check_col(pinfo->fd, COL_INFO))
96         col_add_str(pinfo->fd, COL_INFO,
97                     val_to_str(command, command_vals, "Unknown command (%u)"));
98
99     if (tree) {
100         ti = proto_tree_add_item(tree, proto_rip, tvb, 0, tvb_length(tvb), FALSE);
101         rip_tree = proto_item_add_subtree(ti, ett_rip);
102
103         proto_tree_add_text(rip_tree, tvb, 0, 1, "Command: %u (%s)", command,
104                 val_to_str(command, command_vals, "Unknown"));
105         proto_tree_add_text(rip_tree, tvb, 1, 1, "Version: %u", version);
106         if (version == RIPv2)
107             proto_tree_add_text(rip_tree, tvb, 2, 2, "Routing Domain: %u",
108                                 tvb_get_ntohs(tvb, 2));
109
110         /* skip header */
111         offset = RIP_HEADER_LENGTH;
112
113         /* zero or more entries */
114         reported_length = tvb_reported_length(tvb);
115         while (offset < reported_length) {
116             family = tvb_get_ntohs(tvb, offset);
117             switch (family) {
118             case 2: /* IP */
119                 dissect_ip_rip_vektor(tvb, offset, version, rip_tree);
120                 break;
121             case 0xFFFF:
122                 dissect_rip_authentication(tvb, offset, rip_tree);
123                 break;
124             default:
125                 proto_tree_add_text(rip_tree, tvb, offset,
126                                 RIP_ENTRY_LENGTH, "Unknown address family %u",
127                                 family);
128                 break;
129             }
130
131             offset += RIP_ENTRY_LENGTH;
132         }
133     }
134 }
135
136 static void
137 dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
138                       proto_tree *tree)
139 {
140     proto_item *ti;
141     proto_tree *rip_vektor_tree;
142     guint8 *ip;
143     guint32 metric;
144
145     ip = tvb_get_ptr(tvb, offset+4, 4);
146     metric = tvb_get_ntohl(tvb, offset+16);
147     ti = proto_tree_add_text(tree, tvb, offset,
148                              RIP_ENTRY_LENGTH, "IP Address: %s, Metric: %u",
149                              ip_to_str(ip), metric);
150     rip_vektor_tree = proto_item_add_subtree(ti, ett_rip_vec);
151            
152     proto_tree_add_text(rip_vektor_tree, tvb, offset, 2,
153                         "Address Family ID: IP"); 
154     if (version == RIPv2)
155         proto_tree_add_text(rip_vektor_tree, tvb, offset + 2, 2,
156                             "Route Tag: %u", tvb_get_ntohs(tvb, offset+2));
157     proto_tree_add_text(rip_vektor_tree, tvb, offset + 4, 4, "IP Address: %s",
158                                 ip_to_str(ip));
159     if (version == RIPv2) {
160         proto_tree_add_text(rip_vektor_tree, tvb, offset + 8, 4,
161                             "Netmask: %s", 
162                             ip_to_str(tvb_get_ptr(tvb, offset + 8, 4))); 
163         proto_tree_add_text(rip_vektor_tree, tvb, offset + 12, 4,
164                             "Next Hop: %s", 
165                              ip_to_str(tvb_get_ptr(tvb, offset+12, 4))); 
166     }
167     proto_tree_add_text(rip_vektor_tree, tvb, offset + 16, 4, "Metric: %u",
168                         metric);
169 }
170
171 static void
172 dissect_rip_authentication(tvbuff_t *tvb, int offset, proto_tree *tree)
173 {
174     proto_item *ti;
175     proto_tree *rip_authentication_tree;
176     guint16 authtype;
177     char authentication[16];
178
179     ti = proto_tree_add_text(tree, tvb, offset, RIP_ENTRY_LENGTH,
180                              "Authentication");
181     rip_authentication_tree = proto_item_add_subtree(ti, ett_rip_vec);
182
183     authtype = tvb_get_ntohs(tvb, offset + 2);
184     proto_tree_add_text(rip_authentication_tree, tvb, offset + 2, 2,
185                                 "Authentication type: %u", authtype);
186     if (authtype == 2) {
187         tvb_get_nstringz0(tvb, offset + 4, 16, authentication);
188         proto_tree_add_text(rip_authentication_tree, tvb, offset + 4, 16,
189                                 "Password: %s", authentication);
190     }
191 }
192
193 void
194 proto_register_rip(void)
195 {
196 /*  static hf_register_info hf[] = {
197         { &variable,
198         { "Name",           "rip.abbreviation", TYPE, VALS_POINTER }},
199     };*/
200     static gint *ett[] = {
201         &ett_rip,
202         &ett_rip_vec,
203     };
204
205     proto_rip = proto_register_protocol("Routing Information Protocol",
206                                         "RIP", "rip");
207 /*  proto_register_field_array(proto_rip, hf, array_length(hf));*/
208     proto_register_subtree_array(ett, array_length(ett));
209 }
210
211 void
212 proto_reg_handoff_rip(void)
213 {
214     dissector_add("udp.port", UDP_PORT_RIP, dissect_rip);
215 }