Set the svn:eol-style property on all text files to "native", so that
[obnox/wireshark/wip.git] / 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  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
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;
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                         proto_tree_add_boolean_hidden(gift_tree, hf_gift_request, tvb, 0, 0, TRUE);
91                 } else {
92                         proto_tree_add_boolean_hidden(gift_tree, hf_gift_response, tvb, 0, 0, TRUE);
93                 }
94
95                 ti = proto_tree_add_text(gift_tree, tvb, offset, next_offset - offset, "%s", 
96                                          tvb_format_text(tvb, offset, next_offset - offset));
97                 cmd_tree = proto_item_add_subtree(ti, ett_gift_cmd);
98
99                 tokenlen = get_token_len(line, line + linelen, &next_token);
100                 if (tokenlen != 0) {
101                         if (is_request) {
102                                 proto_tree_add_text(cmd_tree, tvb, offset,
103                                                     tokenlen, "Request Command: %s",
104                                                     format_text(line, tokenlen));
105                         } else {
106                                 proto_tree_add_text(cmd_tree, tvb, offset,
107                                                     tokenlen, "Response Command: %s",
108                                                     format_text(line, tokenlen));
109                         }
110                         offset += next_token - line;
111                         linelen -= next_token - line;
112                         line = next_token;
113                 }
114
115                 if (linelen != 0) {
116                         if (is_request) {
117                                 proto_tree_add_text(cmd_tree, tvb, offset,
118                                                     linelen, "Request Arg: %s",
119                                                     format_text(line, linelen));
120                         } else {
121                                 proto_tree_add_text(cmd_tree, tvb, offset,
122                                                     linelen, "Response Arg: %s",
123                                                     format_text(line, linelen));
124                         }
125                 }
126         }
127 }
128
129 void
130 proto_register_gift(void)
131 {
132         static hf_register_info hf[] = {
133                 { &hf_gift_response,
134                         { "Response", "gift.response", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "TRUE if giFT response", HFILL }
135                 },
136                 { &hf_gift_request,
137                         { "Request", "gift.request", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "TRUE if giFT request", HFILL }
138                 }
139         };
140
141         static gint *ett[] = {
142                 &ett_gift,
143                 &ett_gift_cmd,
144         };
145
146         proto_gift = proto_register_protocol("giFT Internet File Transfer",
147                                              "giFT", "gift");
148
149         proto_register_field_array(proto_gift, hf, array_length(hf));
150         proto_register_subtree_array(ett, array_length(ett));
151 }
152
153 void
154 proto_reg_handoff_gift(void)
155 {
156         dissector_handle_t gift_handle;
157
158         gift_handle = create_dissector_handle(dissect_gift, proto_gift);
159         dissector_add("tcp.port", TCP_PORT_GIFT, gift_handle);
160 }