In packet-enttec.c and packet-fw1.c, replace string pointer tracking with a
[obnox/wireshark/wip.git] / epan / dissectors / packet-gift.c
1 /* packet-gift.c
2  * Routines for giFT Internet File Transfer dissection
3  * Copyright 2000, Jon Oberheide <jon@oberheide.org>
4  *
5  * See http://www.giftproject.org/
6  *
7  * $Id$
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
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 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <glib.h>
36 #include <epan/packet.h>
37 #include <epan/strutil.h>
38
39 #define TCP_PORT_GIFT 1213
40
41 static int proto_gift = -1;
42 static int hf_gift_response = -1;
43 static int hf_gift_request = -1;
44
45 static gint ett_gift = -1;
46 static gint ett_gift_cmd = -1;
47
48 static void
49 dissect_gift(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
50 {
51         proto_item      *ti, *hidden_item;
52         proto_tree      *gift_tree, *cmd_tree;
53         gboolean        is_request;
54         gint            offset = 0;
55         const guchar    *line;
56         gint            next_offset;
57         int             linelen;
58         int             tokenlen;
59         const guchar    *next_token;
60
61         /* set "Protocol" column text */
62         if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
63                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "giFT");
64
65         /* determine whether it is a request to or response from the server */
66         if (pinfo->match_port == pinfo->destport)
67                 is_request = TRUE;
68         else
69                 is_request = FALSE;
70
71         linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
72         line = tvb_get_ptr(tvb, offset, linelen);
73
74         /* clear the "Info" column */
75         if (check_col(pinfo->cinfo, COL_INFO)) 
76                 col_clear(pinfo->cinfo, COL_INFO);
77
78         /* set "Info" column text */
79         if (check_col(pinfo->cinfo, COL_INFO)) 
80                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s",
81                              is_request ? "Request" : "Response",
82                              format_text(line, linelen));
83
84         /* if tree != NULL, build protocol tree */
85         if (tree) {
86                 ti = proto_tree_add_item(tree, proto_gift, tvb, 0, -1, FALSE);
87                 gift_tree = proto_item_add_subtree(ti, ett_gift);
88
89                 if (is_request) {
90                         hidden_item = proto_tree_add_boolean(gift_tree, hf_gift_request, tvb, 0, 0, TRUE);
91                 } else {
92                         hidden_item = proto_tree_add_boolean(gift_tree, hf_gift_response, tvb, 0, 0, TRUE);
93                 }
94                 PROTO_ITEM_SET_HIDDEN(hidden_item);
95
96                 ti = proto_tree_add_text(gift_tree, tvb, offset, next_offset - offset, "%s", 
97                                          tvb_format_text(tvb, offset, next_offset - offset));
98                 cmd_tree = proto_item_add_subtree(ti, ett_gift_cmd);
99
100                 tokenlen = get_token_len(line, line + linelen, &next_token);
101                 if (tokenlen != 0) {
102                         if (is_request) {
103                                 proto_tree_add_text(cmd_tree, tvb, offset,
104                                                     tokenlen, "Request Command: %s",
105                                                     format_text(line, tokenlen));
106                         } else {
107                                 proto_tree_add_text(cmd_tree, tvb, offset,
108                                                     tokenlen, "Response Command: %s",
109                                                     format_text(line, tokenlen));
110                         }
111                         offset += (gint) (next_token - line);
112                         linelen -= (int) (next_token - line);
113                         line = next_token;
114                 }
115
116                 if (linelen != 0) {
117                         if (is_request) {
118                                 proto_tree_add_text(cmd_tree, tvb, offset,
119                                                     linelen, "Request Arg: %s",
120                                                     format_text(line, linelen));
121                         } else {
122                                 proto_tree_add_text(cmd_tree, tvb, offset,
123                                                     linelen, "Response Arg: %s",
124                                                     format_text(line, linelen));
125                         }
126                 }
127         }
128 }
129
130 void
131 proto_register_gift(void)
132 {
133         static hf_register_info hf[] = {
134                 { &hf_gift_response,
135                         { "Response", "gift.response", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "TRUE if giFT response", HFILL }
136                 },
137                 { &hf_gift_request,
138                         { "Request", "gift.request", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "TRUE if giFT request", HFILL }
139                 }
140         };
141
142         static gint *ett[] = {
143                 &ett_gift,
144                 &ett_gift_cmd,
145         };
146
147         proto_gift = proto_register_protocol("giFT Internet File Transfer",
148                                              "giFT", "gift");
149
150         proto_register_field_array(proto_gift, hf, array_length(hf));
151         proto_register_subtree_array(ett, array_length(ett));
152 }
153
154 void
155 proto_reg_handoff_gift(void)
156 {
157         dissector_handle_t gift_handle;
158
159         gift_handle = create_dissector_handle(dissect_gift, proto_gift);
160         dissector_add("tcp.port", TCP_PORT_GIFT, gift_handle);
161 }