Make "epan_init()" take, as additional arguments, pointers to routines
[obnox/wireshark/wip.git] / packet-etherip.c
1 /*
2  * Copyright (c) 2003 Markus Friedl.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * $Id: packet-etherip.c,v 1.2 2003/10/02 22:44:19 guy Exp $
25  */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <string.h>
31 #include <glib.h>
32 #include <epan/packet.h>
33 #include "ipproto.h"
34
35 static int proto_etherip = -1;
36 static int hf_etherip_ver = -1;
37
38 static gint ett_etherip = -1;
39
40 static dissector_handle_t eth_handle;
41
42 #ifndef offsetof
43 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
44 #endif
45
46
47 /*
48  * RFC 3378: EtherIP: Tunneling Ethernet Frames in IP Datagrams
49  *
50  *      Bits 0-3:  Protocol version
51  *      Bits 4-15: Reserved for future use
52  */
53
54 struct etheriphdr {
55         guint8 ver;                /* version/reserved */
56         guint8 pad;                /* required padding byte */
57 };
58
59 #define ETHERIP_VERS_MASK 0x0f
60
61
62 static void
63 dissect_etherip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
64 {
65   struct etheriphdr etheriph;
66   tvbuff_t *next_tvb;
67   proto_tree *etherip_tree;
68   proto_item *ti;
69
70   if (check_col(pinfo->cinfo, COL_PROTOCOL))
71     col_set_str(pinfo->cinfo, COL_PROTOCOL, "ETHERIP");
72
73   /* Copy out the etherip header to insure alignment */
74   tvb_memcpy(tvb, (guint8 *)&etheriph, 0, sizeof(etheriph));
75
76   /* mask out reserved bits */
77   etheriph.ver &= ETHERIP_VERS_MASK;
78
79   if (tree) {
80     ti = proto_tree_add_protocol_format(tree, proto_etherip, tvb, 0,
81              sizeof(etheriph),
82              "EtherIP, Version %d",
83              etheriph.ver
84              );
85     etherip_tree = proto_item_add_subtree(ti, ett_etherip);
86
87     proto_tree_add_uint(etherip_tree, hf_etherip_ver, tvb,
88              offsetof(struct etheriphdr, ver), sizeof(etheriph.ver),
89              etheriph.ver);
90   }
91
92   /* Set the tvbuff for the payload after the header */
93   next_tvb = tvb_new_subset(tvb, sizeof(etheriph), -1, -1);
94
95   call_dissector(eth_handle, next_tvb, pinfo, tree);
96 }
97
98 void
99 proto_register_etherip(void)
100 {
101   static hf_register_info hf_etherip[] = {
102     { &hf_etherip_ver,
103       { "Version",      "etherip.ver",  FT_UINT8,       BASE_HEX, NULL, 0x0,
104         "", HFILL }},
105   };
106   static gint *ett[] = {
107     &ett_etherip,
108   };
109
110   proto_etherip = proto_register_protocol("Ethernet over IP",
111                                           "ETHERIP", "etherip");
112   proto_register_field_array(proto_etherip, hf_etherip, array_length(hf_etherip));
113   proto_register_subtree_array(ett, array_length(ett));
114
115   register_dissector("etherip", dissect_etherip, proto_etherip);
116 }
117
118 void
119 proto_reg_handoff_etherip(void)
120 {
121   dissector_handle_t etherip_handle;
122
123   eth_handle = find_dissector("eth");
124   etherip_handle = find_dissector("etherip");
125   dissector_add("ip.proto", IP_PROTO_ETHERIP, etherip_handle);
126 }