b4d3a992e72d5bb6e2ad8268a8977fb54a00f61d
[obnox/wireshark/wip.git] / packet-rip.c
1 /* packet-ospf.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.8 1999/03/23 03:14:43 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 <glib.h>
38 #include "packet.h"
39 #include "packet-rip.h"
40
41 static void dissect_ip_rip_vektor(guint8 version,
42     const e_rip_vektor *rip_vektor, int offset, proto_tree *tree);
43 static void dissect_rip_authentication(const e_rip_authentication *rip_authentication,
44   int offset, proto_tree *tree);
45
46 void 
47 dissect_rip(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
48     e_riphdr rip_header;
49     e_rip_entry rip_entry;
50     guint16 family;
51     proto_tree *rip_tree = NULL;
52         proto_item *ti; 
53
54     /* we do the range checking of the index when checking wether or not this is a RIP packet */
55     static char *packet_type[8] = { "never used", "Request", "Response", 
56     "Traceon", "Traceoff", "Vendor specific (Sun)" };
57     static char *version[3] = { "RIP", "RIPv1", "RIPv2" };
58
59     /* avoid alignment problem */
60     memcpy(&rip_header, &pd[offset], sizeof(rip_header));
61   
62     /* Check if we 've realy got a RIP packet */
63
64     switch(rip_header.version) {
65         case RIPv1:
66             /* the domain field has to be set to zero for RIPv1 */
67             if(!(rip_header.domain == 0)){ 
68                 dissect_data(pd, offset, fd, tree);
69                 return;
70             }
71             /* the RIPv2 checks are also made for v1 packets */
72         case RIPv2:
73             /* check wether or not command nr. is between 1-7 
74              * (range checking for index of char* packet_type is done at the same time) 
75              */
76             if( !( (rip_header.command > 0) && (rip_header.command <= 7) )){ 
77                 dissect_data(pd, offset, fd, tree);
78                 return;
79             }
80             break;
81         default:
82             /* we only know RIPv1 and RIPv2 */
83             dissect_data(pd, offset, fd, tree);
84             return;
85     }
86
87     if (check_col(fd, COL_PROTOCOL))
88         col_add_str(fd, COL_PROTOCOL, version[rip_header.version] );
89     if (check_col(fd, COL_INFO))
90         col_add_str(fd, COL_INFO, packet_type[rip_header.command]); 
91
92     if (tree) {
93         ti = proto_tree_add_item(tree, offset, (fd->cap_len - offset), "Routing Information Protocol"); 
94         rip_tree = proto_tree_new(); 
95         proto_item_add_subtree(ti, rip_tree, ETT_RIP);
96
97         proto_tree_add_item(rip_tree, offset, 1, "Command: %d (%s)", rip_header.command, packet_type[rip_header.command]); 
98         proto_tree_add_item(rip_tree, offset + 1, 1, "Version: %d", rip_header.version);
99         if(rip_header.version == RIPv2)
100             proto_tree_add_item(rip_tree, offset + 2 , 2, "Routing Domain: %d", ntohs(rip_header.domain)); 
101
102         /* skip header */
103         offset += RIP_HEADER_LENGTH;
104
105         /* zero or more entries */
106
107         while((fd->cap_len - offset) >= RIP_ENTRY_LENGTH){
108             memcpy(&rip_entry, &pd[offset], sizeof(rip_entry)); /* avoid alignment problem */
109             family = ntohs(rip_entry.vektor.family);
110             switch (family) {
111             case 2: /* IP */
112                 ti = proto_tree_add_item(rip_tree, offset,
113                                 RIP_ENTRY_LENGTH, "IP Address: %s, Metric: %ld",
114                                 ip_to_str((guint8 *) &(rip_entry.vektor.ip)),
115                                 (long)ntohl(rip_entry.vektor.metric));
116                 dissect_ip_rip_vektor(rip_header.version, &rip_entry.vektor,
117                                 offset, ti);
118                 break;
119             case 0xFFFF:
120                 proto_tree_add_item(rip_tree, offset,
121                                 RIP_ENTRY_LENGTH, "Authentication");
122                 dissect_rip_authentication(&rip_entry.authentication,
123                                 offset, ti);
124                 break;
125             default:
126                 proto_tree_add_item(rip_tree, 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(guint8 version, const e_rip_vektor *rip_vektor,
139   int offset, proto_tree *tree)
140 {
141     proto_tree *rip_vektor_tree;
142
143     rip_vektor_tree = proto_tree_new(); 
144     proto_item_add_subtree(tree, rip_vektor_tree, ETT_RIP_VEC);
145            
146     proto_tree_add_item(rip_vektor_tree, offset, 2, "Address Family ID: IP"); 
147     if(version == RIPv2)
148         proto_tree_add_item(rip_vektor_tree, offset + 2 , 2, "Route Tag: %d",
149                                 ntohs(rip_vektor->tag)); 
150     proto_tree_add_item(rip_vektor_tree, offset + 4, 4, "IP Address: %s",
151                                 ip_to_str((guint8 *) &(rip_vektor->ip))); 
152     if(version == RIPv2) {
153         proto_tree_add_item(rip_vektor_tree, offset + 8 , 4, "Netmask: %s", 
154                                 ip_to_str((guint8 *) &(rip_vektor->mask))); 
155         proto_tree_add_item(rip_vektor_tree, offset + 12, 4, "Next Hop: %s", 
156                                 ip_to_str((guint8 *) &(rip_vektor->next_hop))); 
157     }
158     proto_tree_add_item(rip_vektor_tree, offset + 16, 4, "Metric: %ld",
159                                 (long)ntohl(rip_vektor->metric)); 
160 }
161
162 static void
163 dissect_rip_authentication(const e_rip_authentication *rip_authentication,
164   int offset, proto_tree *tree)
165 {
166     proto_tree *rip_authentication_tree;
167     guint16 authtype;
168
169     rip_authentication_tree = proto_tree_new(); 
170     proto_item_add_subtree(tree, rip_authentication_tree, ETT_RIP_VEC);
171
172     authtype = ntohs(rip_authentication->authtype);
173     proto_tree_add_item(rip_authentication_tree, offset + 2, 2,
174                                 "Authentication type: %u", authtype); 
175     if (authtype == 2)
176         proto_tree_add_item(rip_authentication_tree, offset + 4 , 16,
177                                 "Password: %.16s",
178                                 rip_authentication->authentication);
179 }
180