4df7af29f39d1f0819a107b320b756d8412871e3
[obnox/wireshark/wip.git] / epan / dissectors / 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$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
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 #include <glib.h>
30 #include <epan/packet.h>
31 #include "packet-ripng.h"
32
33 #ifndef offsetof
34 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
35 #endif
36
37 static int proto_ripng = -1;
38 static int hf_ripng_cmd = -1;
39 static int hf_ripng_version = -1;
40
41 static gint ett_ripng = -1;
42 static gint ett_ripng_addr = -1;
43
44 #define UDP_PORT_RIPNG  521
45
46 static const value_string cmdvals[] = {
47     { RIP6_REQUEST, "Request" },
48     { RIP6_RESPONSE, "Response" },
49     { 0, NULL },
50 };
51
52 static void
53 dissect_ripng(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
54     int offset = 0;
55     struct rip6 rip6;
56     struct netinfo6 ni6;
57     proto_tree *ripng_tree = NULL;
58     proto_tree *subtree = NULL;
59     proto_item *ti;
60
61     col_set_str(pinfo->cinfo, COL_PROTOCOL, "RIPng");
62     col_clear(pinfo->cinfo, COL_INFO);
63
64     /* avoid alignment problem */
65     tvb_memcpy(tvb, (guint8 *)&rip6, offset, sizeof(rip6));
66
67     if (check_col(pinfo->cinfo, COL_PROTOCOL))
68         col_add_fstr(pinfo->cinfo, COL_PROTOCOL, "RIPng version %u", rip6.rip6_vers);
69     if (check_col(pinfo->cinfo, COL_INFO))
70         col_add_str(pinfo->cinfo, COL_INFO,
71             val_to_str(rip6.rip6_cmd, cmdvals, "Unknown command (%u)"));
72
73     if (tree) {
74         ti = proto_tree_add_item(tree, proto_ripng, tvb, offset, -1, FALSE);
75         ripng_tree = proto_item_add_subtree(ti, ett_ripng);
76
77         proto_tree_add_uint(ripng_tree, hf_ripng_cmd, tvb, offset, 1,
78             rip6.rip6_cmd);
79         proto_tree_add_uint(ripng_tree, hf_ripng_version, tvb, offset + 1, 1,
80             rip6.rip6_vers);
81
82         offset += 4;
83         while (tvb_reported_length_remaining(tvb, offset) > 0) {
84                     tvb_memcpy(tvb, (guint8 *)&ni6, offset, sizeof(ni6));
85                     if (ni6.rip6_tag) {
86                         ti = proto_tree_add_text(ripng_tree, tvb, offset,
87                                         sizeof(ni6), "IP Address: %s/%u, Metric: %u, tag: 0x%04x",
88                                         ip6_to_str(&ni6.rip6_dest),
89                                         ni6.rip6_plen,
90                                         ni6.rip6_metric,
91                                         g_ntohs(ni6.rip6_tag));
92                     } else {
93                         ti = proto_tree_add_text(ripng_tree, tvb, offset,
94                                         sizeof(ni6), "IP Address: %s/%u, Metric: %u",
95                                         ip6_to_str(&ni6.rip6_dest),
96                                         ni6.rip6_plen,
97                                         ni6.rip6_metric);
98                     }
99                     subtree = proto_item_add_subtree(ti, ett_ripng_addr);
100                     proto_tree_add_text(subtree, tvb,
101                                 offset + offsetof(struct netinfo6, rip6_dest),
102                                 sizeof(ni6.rip6_dest), "IP Address: %s",
103                                 ip6_to_str(&ni6.rip6_dest));
104                     proto_tree_add_text(subtree, tvb,
105                                 offset + offsetof(struct netinfo6, rip6_tag),
106                                 sizeof(ni6.rip6_tag), "Tag: 0x%04x",
107                                 g_ntohs(ni6.rip6_tag));
108                     proto_tree_add_text(subtree, tvb,
109                                 offset + offsetof(struct netinfo6, rip6_plen),
110                                 sizeof(ni6.rip6_plen), "Prefix length: %u",
111                                 ni6.rip6_plen);
112                     proto_tree_add_text(subtree, tvb,
113                                 offset + offsetof(struct netinfo6, rip6_metric),
114                                 sizeof(ni6.rip6_metric), "Metric: %u",
115                                 ni6.rip6_metric);
116
117                     offset += sizeof(ni6);
118         }
119     }
120 }
121
122 void
123 proto_register_ripng(void)
124 {
125     static hf_register_info hf[] = {
126       { &hf_ripng_cmd,
127         { "Command",            "ripng.cmd",
128                                 FT_UINT8, BASE_DEC, VALS(cmdvals),
129                                 0x0, NULL, HFILL }},
130       { &hf_ripng_version,
131         { "Version",            "ripng.version",
132                                 FT_UINT8, BASE_DEC, NULL,
133                                 0x0, NULL, HFILL }},
134     };
135     static gint *ett[] = {
136       &ett_ripng,
137       &ett_ripng_addr,
138     };
139
140     proto_ripng = proto_register_protocol("RIPng", "RIPng", "ripng");
141     proto_register_field_array(proto_ripng, hf, array_length(hf));
142     proto_register_subtree_array(ett, array_length(ett));
143 }
144
145 void
146 proto_reg_handoff_ripng(void)
147 {
148     dissector_handle_t ripng_handle;
149
150     ripng_handle = create_dissector_handle(dissect_ripng, proto_ripng);
151     dissector_add_uint("udp.port", UDP_PORT_RIPNG, ripng_handle);
152 }