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