Remove more "CHECK_DISPLAY_AS_DATA()" calls and "pinfo->current_proto ="
[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.18 2001/01/22 08:03:45 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 <string.h>
39 #include <glib.h>
40 #include "packet.h"
41 #include "packet-ipv6.h"
42 #include "packet-ripng.h"
43
44 #ifndef offsetof
45 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
46 #endif
47
48 static int proto_ripng = -1;
49 static int hf_ripng_cmd = -1;
50 static int hf_ripng_version = -1;
51
52 static gint ett_ripng = -1;
53 static gint ett_ripng_addr = -1;
54
55 #define UDP_PORT_RIPNG  521
56
57 static const value_string cmdvals[] = {
58     { RIP6_REQUEST, "Request" },
59     { RIP6_RESPONSE, "Response" },
60     { 0, NULL },
61 };
62
63 static void 
64 dissect_ripng(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
65     struct rip6 rip6;
66     struct netinfo6 ni6;
67     proto_tree *ripng_tree = NULL;
68     proto_tree *subtree = NULL;
69     proto_item *ti; 
70
71     if (check_col(fd, COL_PROTOCOL))
72         col_set_str(fd, COL_PROTOCOL, "RIPng");
73     if (check_col(fd, COL_INFO))
74         col_clear(fd, COL_INFO);
75
76     /* avoid alignment problem */
77     memcpy(&rip6, &pd[offset], sizeof(rip6));
78   
79     if (check_col(fd, COL_PROTOCOL))
80         col_add_fstr(fd, COL_PROTOCOL, "RIPng version %u", rip6.rip6_vers);
81     if (check_col(fd, COL_INFO))
82         col_add_str(fd, COL_INFO,
83             val_to_str(rip6.rip6_cmd, cmdvals, "Unknown command (%u)")); 
84
85     if (tree) {
86         ti = proto_tree_add_item(tree, proto_ripng, NullTVB, offset, END_OF_FRAME, FALSE);
87         ripng_tree = proto_item_add_subtree(ti, ett_ripng);
88
89         proto_tree_add_uint(ripng_tree, hf_ripng_cmd, NullTVB, offset, 1,
90             rip6.rip6_cmd);
91         proto_tree_add_uint(ripng_tree, hf_ripng_version, NullTVB, offset + 1, 1,
92             rip6.rip6_vers);
93
94         offset += 4;
95         while ((pi.captured_len - offset) >= sizeof(struct netinfo6)){
96                     if (! BYTES_ARE_IN_FRAME(offset, sizeof(ni6))) {
97                        proto_tree_add_text(ripng_tree, NullTVB, offset, sizeof(ni6), "No IP Address information");
98                        break;
99                     }
100
101                     memcpy(&ni6, &pd[offset], sizeof(ni6));
102                     if (ni6.rip6_tag) {
103                         ti = proto_tree_add_text(ripng_tree, NullTVB, offset,
104                                         sizeof(ni6), "IP Address: %s/%u, Metric: %u, tag: 0x%04x",
105                                         ip6_to_str(&ni6.rip6_dest),
106                                         ni6.rip6_plen,
107                                         ni6.rip6_metric,
108                                         ntohs(ni6.rip6_tag));
109                     } else {
110                         ti = proto_tree_add_text(ripng_tree, NullTVB, offset,
111                                         sizeof(ni6), "IP Address: %s/%u, Metric: %u",
112                                         ip6_to_str(&ni6.rip6_dest),
113                                         ni6.rip6_plen,
114                                         ni6.rip6_metric);
115                     }
116                     subtree = proto_item_add_subtree(ti, ett_ripng_addr);
117                     proto_tree_add_text(subtree, NullTVB,
118                                 offset + offsetof(struct netinfo6, rip6_dest),
119                                 sizeof(ni6.rip6_dest), "IP Address: %s",
120                                 ip6_to_str(&ni6.rip6_dest));
121                     proto_tree_add_text(subtree, NullTVB,
122                                 offset + offsetof(struct netinfo6, rip6_tag),
123                                 sizeof(ni6.rip6_tag), "Tag: 0x%04x",
124                                 ntohs(ni6.rip6_tag));
125                     proto_tree_add_text(subtree, NullTVB,
126                                 offset + offsetof(struct netinfo6, rip6_plen),
127                                 sizeof(ni6.rip6_plen), "Prefix length: %u",
128                                 ni6.rip6_plen);
129                     proto_tree_add_text(subtree, NullTVB,
130                                 offset + offsetof(struct netinfo6, rip6_metric),
131                                 sizeof(ni6.rip6_metric), "Metric: %u",
132                                 ni6.rip6_metric);
133
134                     offset += sizeof(ni6);
135         }
136     }
137 }
138
139 void
140 proto_register_ripng(void)
141 {
142     static hf_register_info hf[] = {
143       { &hf_ripng_cmd,
144         { "Command",            "ripng.cmd",
145                                 FT_UINT8, BASE_DEC, VALS(cmdvals),
146                                 0x0, "" }},
147       { &hf_ripng_version,
148         { "Version",            "ripng.version",
149                                 FT_UINT8, BASE_DEC, NULL,
150                                 0x0, "" }},
151     };
152     static gint *ett[] = {
153       &ett_ripng,
154       &ett_ripng_addr,
155     };
156
157     proto_ripng = proto_register_protocol("RIPng", "RIPng", "ripng");
158     proto_register_field_array(proto_ripng, hf, array_length(hf));
159     proto_register_subtree_array(ett, array_length(ett));
160 }
161
162 void
163 proto_reg_handoff_ripng(void)
164 {
165     old_dissector_add("udp.port", UDP_PORT_RIPNG, dissect_ripng, proto_ripng);
166 }