In "dissect_eth()", update "pi.len" and "pi.captured_len" regardless of
[obnox/wireshark/wip.git] / packet-ripng.c
1 /* packet-ripng.c
2  * Routines for RIPng disassembly
3  * (c) Copyright Jun-ichiro itojun Hagino <itojun@itojun.org>
4  * derived from packet-rip.c
5  *
6  * $Id: packet-ripng.c,v 1.6 1999/11/16 11:42:51 guy Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@zing.org>
10  * Copyright 1998 Gerald Combs
11  * 
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27  
28 #include "config.h"
29
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
32 #endif
33
34 #ifdef HAVE_NETINET_IN_H
35 #include <netinet/in.h>
36 #endif
37
38 #include <glib.h>
39 #include "packet.h"
40 #include "packet-ipv6.h"
41 #include "packet-ripng.h"
42
43 #ifndef offsetof
44 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
45 #endif
46
47 static int proto_ripng = -1;
48 static int hf_ripng_cmd = -1;
49 static int hf_ripng_version = -1;
50
51 static gint ett_ripng = -1;
52 static gint ett_ripng_addr = -1;
53
54 void 
55 dissect_ripng(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
56     struct rip6 rip6;
57     struct netinfo6 ni6;
58     proto_tree *ripng_tree = NULL;
59     proto_tree *subtree = NULL;
60     proto_item *ti; 
61     static const value_string cmdvals[] = {
62         { RIP6_REQUEST, "Request" },
63         { RIP6_RESPONSE, "Response" },
64         { 0, NULL },
65     };
66     const char *cmd;
67
68     /* avoid alignment problem */
69     memcpy(&rip6, &pd[offset], sizeof(rip6));
70   
71     cmd = val_to_str(rip6.rip6_cmd, cmdvals, "Unknown");
72
73     if (check_col(fd, COL_PROTOCOL))
74         col_add_fstr(fd, COL_PROTOCOL, "RIPng version %d", rip6.rip6_vers);
75     if (check_col(fd, COL_INFO))
76         col_add_fstr(fd, COL_INFO, "%s", cmd); 
77
78     if (tree) {
79         ti = proto_tree_add_item(tree, proto_ripng, offset, END_OF_FRAME, NULL);
80         ripng_tree = proto_item_add_subtree(ti, ett_ripng);
81
82         proto_tree_add_item_format(ripng_tree, hf_ripng_cmd, offset, 1,
83             rip6.rip6_cmd,
84             "Command: %s (%u)", cmd, rip6.rip6_cmd); 
85         proto_tree_add_item(ripng_tree, hf_ripng_version, offset + 1, 1,
86             rip6.rip6_vers);
87
88         offset += 4;
89         while ((pi.captured_len - offset) >= sizeof(struct netinfo6)){
90             memcpy(&ni6, &pd[offset], sizeof(ni6));
91             if (ni6.rip6_tag) {
92                 ti = proto_tree_add_text(ripng_tree, offset,
93                                 sizeof(ni6), "IP Address: %s/%d, Metric: %ld, tag: 0x%04x",
94                                 ip6_to_str(&ni6.rip6_dest),
95                                 ni6.rip6_plen,
96                                 ni6.rip6_metric,
97                                 ntohs(ni6.rip6_tag));
98             } else {
99                 ti = proto_tree_add_text(ripng_tree, offset,
100                                 sizeof(ni6), "IP Address: %s/%d, Metric: %ld",
101                                 ip6_to_str(&ni6.rip6_dest),
102                                 ni6.rip6_plen,
103                                 ni6.rip6_metric);
104             }
105             subtree = proto_item_add_subtree(ti, ett_ripng_addr);
106             proto_tree_add_text(subtree,
107                         offset + offsetof(struct netinfo6, rip6_dest),
108                         sizeof(ni6.rip6_dest), "IP Address: %s",
109                         ip6_to_str(&ni6.rip6_dest));
110             proto_tree_add_text(subtree,
111                         offset + offsetof(struct netinfo6, rip6_tag),
112                         sizeof(ni6.rip6_tag), "Tag: 0x%04x",
113                         ntohs(ni6.rip6_tag));
114             proto_tree_add_text(subtree,
115                         offset + offsetof(struct netinfo6, rip6_plen),
116                         sizeof(ni6.rip6_plen), "Prefix length: %d",
117                         ni6.rip6_plen);
118             proto_tree_add_text(subtree,
119                         offset + offsetof(struct netinfo6, rip6_metric),
120                         sizeof(ni6.rip6_metric), "Metric: %d",
121                         ni6.rip6_metric);
122
123             offset += sizeof(ni6);
124         }
125     }
126 }
127
128 void
129 proto_register_ripng(void)
130 {
131     static hf_register_info hf[] = {
132       { &hf_ripng_cmd,
133         { "Command",            "ripng.cmd",
134                                 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
135       { &hf_ripng_version,
136         { "Version",            "ripng.version",
137                                 FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
138     };
139     static gint *ett[] = {
140       &ett_ripng,
141       &ett_ripng_addr,
142     };
143
144     proto_ripng = proto_register_protocol("RIPng", "ripng");
145     proto_register_field_array(proto_ripng, hf, array_length(hf));
146     proto_register_subtree_array(ett, array_length(ett));
147 }