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