Add the relative time to the frame tree, at the request of Manfred Young.
[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.19 2000/11/17 21:00:36 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 #include "packet-rip.h"
41
42 #define UDP_PORT_RIP    520
43
44 static int proto_rip = -1;
45
46 static gint ett_rip = -1;
47 static gint ett_rip_vec = -1;
48
49 static void dissect_ip_rip_vektor(guint8 version,
50     const e_rip_vektor *rip_vektor, int offset, proto_tree *tree);
51 static void dissect_rip_authentication(const e_rip_authentication *rip_authentication,
52   int offset, proto_tree *tree);
53
54 static void 
55 dissect_rip(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
56     e_riphdr rip_header;
57     e_rip_entry rip_entry;
58     guint16 family;
59     proto_tree *rip_tree = NULL;
60         proto_item *ti; 
61
62     /* we do the range checking of the index when checking wether or not this is a RIP packet */
63     static char *packet_type[8] = { "never used", "Request", "Response", 
64     "Traceon", "Traceoff", "Vendor specific (Sun)" };
65     static char *version[3] = { "RIP", "RIPv1", "RIPv2" };
66
67     OLD_CHECK_DISPLAY_AS_DATA(proto_rip, pd, offset, fd, tree);
68
69     /* avoid alignment problem */
70     memcpy(&rip_header, &pd[offset], sizeof(rip_header));
71   
72     /* Check if we 've realy got a RIP packet */
73
74     switch(rip_header.version) {
75         case RIPv1:
76             /* the domain field has to be set to zero for RIPv1 */
77             if(!(rip_header.domain == 0)){ 
78                 old_dissect_data(pd, offset, fd, tree);
79                 return;
80             }
81             /* the RIPv2 checks are also made for v1 packets */
82         case RIPv2:
83             /* check wether or not command nr. is between 1-7 
84              * (range checking for index of char* packet_type is done at the same time) 
85              */
86             if( !( (rip_header.command > 0) && (rip_header.command <= 7) )){ 
87                 old_dissect_data(pd, offset, fd, tree);
88                 return;
89             }
90             break;
91         default:
92             /* we only know RIPv1 and RIPv2 */
93             old_dissect_data(pd, offset, fd, tree);
94             return;
95     }
96
97     if (check_col(fd, COL_PROTOCOL))
98         col_add_str(fd, COL_PROTOCOL, version[rip_header.version] );
99     if (check_col(fd, COL_INFO))
100         col_add_str(fd, COL_INFO, packet_type[rip_header.command]); 
101
102     if (tree) {
103         ti = proto_tree_add_item(tree, proto_rip, NullTVB, offset, END_OF_FRAME, FALSE);
104         rip_tree = proto_item_add_subtree(ti, ett_rip);
105
106         proto_tree_add_text(rip_tree, NullTVB, offset, 1, "Command: %d (%s)", rip_header.command, packet_type[rip_header.command]); 
107         proto_tree_add_text(rip_tree, NullTVB, offset + 1, 1, "Version: %d", rip_header.version);
108         if(rip_header.version == RIPv2)
109             proto_tree_add_text(rip_tree, NullTVB, offset + 2 , 2, "Routing Domain: %d", ntohs(rip_header.domain)); 
110
111         /* skip header */
112         offset += RIP_HEADER_LENGTH;
113
114         /* zero or more entries */
115
116         while((pi.captured_len - offset) >= RIP_ENTRY_LENGTH){
117             memcpy(&rip_entry, &pd[offset], sizeof(rip_entry)); /* avoid alignment problem */
118             family = ntohs(rip_entry.vektor.family);
119             switch (family) {
120             case 2: /* IP */
121                 ti = proto_tree_add_text(rip_tree, NullTVB, offset,
122                                 RIP_ENTRY_LENGTH, "IP Address: %s, Metric: %ld",
123                                 ip_to_str((guint8 *) &(rip_entry.vektor.ip)),
124                                 (long)ntohl(rip_entry.vektor.metric));
125                 dissect_ip_rip_vektor(rip_header.version, &rip_entry.vektor,
126                                 offset, ti);
127                 break;
128             case 0xFFFF:
129                 proto_tree_add_text(rip_tree, NullTVB, offset,
130                                 RIP_ENTRY_LENGTH, "Authentication");
131                 dissect_rip_authentication(&rip_entry.authentication,
132                                 offset, ti);
133                 break;
134             default:
135                 proto_tree_add_text(rip_tree, NullTVB, offset,
136                                 RIP_ENTRY_LENGTH, "Unknown address family %u",
137                                 family);
138                 break;
139             }
140
141             offset += RIP_ENTRY_LENGTH;
142         }
143     }
144 }
145
146 static void
147 dissect_ip_rip_vektor(guint8 version, const e_rip_vektor *rip_vektor,
148   int offset, proto_tree *tree)
149 {
150     proto_tree *rip_vektor_tree;
151
152     rip_vektor_tree = proto_item_add_subtree(tree, ett_rip_vec);
153            
154     proto_tree_add_text(rip_vektor_tree, NullTVB, offset, 2, "Address Family ID: IP"); 
155     if(version == RIPv2)
156         proto_tree_add_text(rip_vektor_tree, NullTVB, offset + 2 , 2, "Route Tag: %d",
157                                 ntohs(rip_vektor->tag)); 
158     proto_tree_add_text(rip_vektor_tree, NullTVB, offset + 4, 4, "IP Address: %s",
159                                 ip_to_str((guint8 *) &(rip_vektor->ip))); 
160     if(version == RIPv2) {
161         proto_tree_add_text(rip_vektor_tree, NullTVB, offset + 8 , 4, "Netmask: %s", 
162                                 ip_to_str((guint8 *) &(rip_vektor->mask))); 
163         proto_tree_add_text(rip_vektor_tree, NullTVB, offset + 12, 4, "Next Hop: %s", 
164                                 ip_to_str((guint8 *) &(rip_vektor->next_hop))); 
165     }
166     proto_tree_add_text(rip_vektor_tree, NullTVB, offset + 16, 4, "Metric: %ld",
167                                 (long)ntohl(rip_vektor->metric)); 
168 }
169
170 static void
171 dissect_rip_authentication(const e_rip_authentication *rip_authentication,
172   int offset, proto_tree *tree)
173 {
174     proto_tree *rip_authentication_tree;
175     guint16 authtype;
176
177     rip_authentication_tree = proto_item_add_subtree(tree, ett_rip_vec);
178
179     authtype = ntohs(rip_authentication->authtype);
180     proto_tree_add_text(rip_authentication_tree, NullTVB, offset + 2, 2,
181                                 "Authentication type: %u", authtype); 
182     if (authtype == 2)
183         proto_tree_add_text(rip_authentication_tree, NullTVB, offset + 4 , 16,
184                                 "Password: %.16s",
185                                 rip_authentication->authentication);
186 }
187
188 void
189 proto_register_rip(void)
190 {
191 /*        static hf_register_info hf[] = {
192                 { &variable,
193                 { "Name",           "rip.abbreviation", TYPE, VALS_POINTER }},
194         };*/
195         static gint *ett[] = {
196                 &ett_rip,
197                 &ett_rip_vec,
198         };
199
200         proto_rip = proto_register_protocol("Routing Information Protocol", "rip");
201  /*       proto_register_field_array(proto_rip, hf, array_length(hf));*/
202         proto_register_subtree_array(ett, array_length(ett));
203 }
204
205 void
206 proto_reg_handoff_rip(void)
207 {
208         old_dissector_add("udp.port", UDP_PORT_RIP, dissect_rip);
209 }