569acd5b80496393201661596433650917d6b98d
[obnox/wireshark/wip.git] / epan / dissectors / packet-loop.c
1 /* packet-loop.c
2  * Routines for Ethernet loopback/Configuration Test Protocol dissection
3  *
4  * See
5  *
6  *      http://stuff.mit.edu/people/jhawk/ctp.html
7  *
8  * $Id$
9  *
10  * Wireshark - Network traffic analyzer
11  * By Gerald Combs <gerald@wireshark.org>
12  * Copyright 1998 Gerald Combs
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <glib.h>
34 #include <epan/packet.h>
35 #include <epan/etypes.h>
36
37 static int proto_loop = -1;
38 static int hf_loop_skipcount = -1;
39 static int hf_loop_function = -1;
40 static int hf_loop_receipt_number = -1;
41 static int hf_loop_forwarding_address = -1;
42
43 static gint ett_loop = -1;
44
45 static dissector_handle_t data_handle;
46
47 #define FUNC_REPLY              1
48 #define FUNC_FORWARD_DATA       2
49
50 static const value_string function_vals[] = {
51   { FUNC_REPLY, "Reply" },
52   { FUNC_FORWARD_DATA, "Forward Data" },
53   { 0, NULL }
54 };
55
56 static void
57 dissect_loop(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
58 {
59   proto_tree  *loop_tree = NULL;
60   proto_item  *ti;
61   guint16     function;
62   int         offset = 0;
63   int         skip_offset;
64   gboolean    set_info = TRUE;
65   gboolean    more_function;
66   tvbuff_t    *next_tvb;
67
68   col_set_str(pinfo->cinfo, COL_PROTOCOL, "LOOP");
69   col_clear(pinfo->cinfo, COL_INFO);
70
71   if (tree) {
72     ti = proto_tree_add_item(tree, proto_loop, tvb, offset, -1, FALSE);
73     loop_tree = proto_item_add_subtree(ti, ett_loop);
74
75     proto_tree_add_item(loop_tree, hf_loop_skipcount, tvb, offset, 2, TRUE);
76   }
77   skip_offset = 2 + tvb_get_letohs(tvb, offset);
78   offset += 2;
79
80   do {
81     function = tvb_get_letohs(tvb, offset);
82     if (offset == skip_offset) {
83       if (check_col(pinfo->cinfo, COL_INFO)) {
84         col_add_str(pinfo->cinfo, COL_INFO,
85                     val_to_str(function, function_vals, "Unknown function (%u)"));
86       }
87       if (tree)
88         proto_tree_add_text(loop_tree, tvb, offset, 2, "Relevant function:"); 
89       set_info = FALSE;
90     }
91     if (tree)
92       proto_tree_add_uint(loop_tree, hf_loop_function, tvb, offset, 2, function);
93     offset += 2;
94     switch (function) {
95
96     case FUNC_REPLY:
97       if (tree)
98         proto_tree_add_item(loop_tree, hf_loop_receipt_number, tvb, offset, 2,
99                             TRUE);
100       offset += 2;
101       more_function = FALSE;
102       break;
103
104     case FUNC_FORWARD_DATA:
105       if (tree)
106         proto_tree_add_item(loop_tree, hf_loop_forwarding_address, tvb, offset,
107                             6, FALSE);
108       offset += 6;
109       more_function = TRUE;
110       break;
111
112     default:
113       more_function = FALSE;
114       break;
115     }
116   } while (more_function);
117
118   if (set_info && check_col(pinfo->cinfo, COL_INFO)) {
119     col_set_str(pinfo->cinfo, COL_INFO, "No valid function found");
120   }
121
122   if (tvb_length_remaining(tvb, offset) > 0)
123   {
124     next_tvb = tvb_new_subset(tvb, offset, -1, -1);
125     call_dissector(data_handle, next_tvb, pinfo, tree);
126   }
127 }
128
129 void
130 proto_register_loop(void)
131 {
132   static hf_register_info hf[] = {
133     { &hf_loop_skipcount,
134       { "skipCount",            "loop.skipcount",
135         FT_UINT16,      BASE_DEC,       NULL,   0x0,
136         NULL, HFILL }},
137
138     { &hf_loop_function,
139       { "Function",             "loop.function",
140         FT_UINT16,      BASE_DEC,       VALS(function_vals),    0x0,
141         NULL, HFILL }},
142
143     { &hf_loop_receipt_number,
144       { "Receipt number",       "loop.receipt_number",
145         FT_UINT16,      BASE_DEC,       NULL,   0x0,
146         NULL, HFILL }},
147
148     { &hf_loop_forwarding_address,
149       { "Forwarding address",   "loop.forwarding_address",
150         FT_ETHER,       BASE_NONE,      NULL,   0x0,
151         NULL, HFILL }},
152   };
153   static gint *ett[] = {
154     &ett_loop,
155   };
156
157   proto_loop = proto_register_protocol("Configuration Test Protocol (loopback)",
158                                       "LOOP", "loop");
159   proto_register_field_array(proto_loop, hf, array_length(hf));
160   proto_register_subtree_array(ett, array_length(ett));
161 }
162
163 void
164 proto_reg_handoff_loop(void)
165 {
166   dissector_handle_t loop_handle;
167
168   loop_handle = create_dissector_handle(dissect_loop, proto_loop);
169
170   dissector_add("ethertype", ETHERTYPE_LOOP, loop_handle);
171
172   data_handle = find_dissector("data");
173 }