Make "dissector_add()", "dissector_delete()", and "dissector_change()"
[obnox/wireshark/wip.git] / packet-pop.c
1 /* packet-pop.c
2  * Routines for pop packet dissection
3  * Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
4  *
5  * $Id: packet-pop.c,v 1.27 2001/12/03 03:59:38 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * Copied from packet-tftp.c
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
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
40 #endif
41
42 #include <string.h>
43 #include <glib.h>
44 #include "packet.h"
45 #include "strutil.h"
46
47 static int proto_pop = -1;
48 static int hf_pop_response = -1;
49 static int hf_pop_request = -1;
50
51 static gint ett_pop = -1;
52
53 static dissector_handle_t data_handle;
54
55 #define TCP_PORT_POP                    110
56
57 static gboolean response_is_continuation(const u_char *data);
58         
59 static void
60 dissect_pop(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
61 {
62         gboolean        is_request;
63         gboolean        is_continuation;
64         proto_tree      *pop_tree;
65         proto_item      *ti;
66         gint            offset = 0;
67         const u_char    *line;
68         gint            next_offset;
69         int             linelen;
70         int             tokenlen;
71         const u_char    *next_token;
72
73         if (check_col(pinfo->fd, COL_PROTOCOL))
74                 col_set_str(pinfo->fd, COL_PROTOCOL, "POP");
75
76         /*
77          * Find the end of the first line.
78          *
79          * Note that "tvb_find_line_end()" will return a value that is
80          * not longer than what's in the buffer, so the "tvb_get_ptr()"
81          * call won't throw an exception.
82          */
83         linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
84         line = tvb_get_ptr(tvb, offset, linelen);
85
86         if (pinfo->match_port == pinfo->destport) {
87                 is_request = TRUE;
88                 is_continuation = FALSE;
89         } else {
90                 is_request = FALSE;
91                 is_continuation = response_is_continuation(line);
92         }
93
94         if (check_col(pinfo->fd, COL_INFO)) {
95                 /*
96                  * Put the first line from the buffer into the summary
97                  * if it's a POP request or reply (but leave out the
98                  * line terminator).
99                  * Otherwise, just call it a continuation.
100                  */
101                 if (is_continuation)
102                         col_set_str(pinfo->fd, COL_INFO, "Continuation");
103                 else
104                         col_add_fstr(pinfo->fd, COL_INFO, "%s: %s",
105                             is_request ? "Request" : "Response",
106                             format_text(line, linelen));
107         }
108
109         if (tree) {
110                 ti = proto_tree_add_item(tree, proto_pop, tvb, offset,
111                     tvb_length_remaining(tvb, offset), FALSE);
112                 pop_tree = proto_item_add_subtree(ti, ett_pop);
113
114                 if (is_continuation) {
115                         /*
116                          * Put the whole packet into the tree as data.
117                          */
118                         call_dissector(data_handle,tvb, pinfo, pop_tree);
119                         return;
120                 }
121
122                 if (is_request) {
123                         proto_tree_add_boolean_hidden(pop_tree,
124                             hf_pop_request, tvb, 0, 0, TRUE);
125                 } else {
126                         proto_tree_add_boolean_hidden(pop_tree,
127                             hf_pop_response, tvb, 0, 0, TRUE);
128                 }
129
130                 /*
131                  * Extract the first token, and, if there is a first
132                  * token, add it as the request or reply code.
133                  */
134                 tokenlen = get_token_len(line, line + linelen, &next_token);
135                 if (tokenlen != 0) {
136                         if (is_request) {
137                                 proto_tree_add_text(pop_tree, tvb, offset,
138                                     tokenlen, "Request: %s",
139                                     format_text(line, tokenlen));
140                         } else {
141                                 proto_tree_add_text(pop_tree, tvb, offset,
142                                     tokenlen, "Response: %s",
143                                     format_text(line, tokenlen));
144                         }
145                         offset += next_token - line;
146                         linelen -= next_token - line;
147                         line = next_token;
148                 }
149
150                 /*
151                  * Add the rest of the first line as request or
152                  * reply data.
153                  */
154                 if (linelen != 0) {
155                         if (is_request) {
156                                 proto_tree_add_text(pop_tree, tvb, offset,
157                                     linelen, "Request Arg: %s",
158                                     format_text(line, linelen));
159                         } else {
160                                 proto_tree_add_text(pop_tree, tvb, offset,
161                                     linelen, "Response Arg: %s",
162                                     format_text(line, linelen));
163                         }
164                 }
165                 offset = next_offset;
166
167                 /*
168                  * Show the rest of the request or response as text,
169                  * a line at a time.
170                  */
171                 while (tvb_offset_exists(tvb, offset)) {
172                         /*
173                          * Find the end of the line.
174                          */
175                         linelen = tvb_find_line_end(tvb, offset, -1,
176                             &next_offset);
177
178                         /*
179                          * Put this line.
180                          */
181                         proto_tree_add_text(pop_tree, tvb, offset,
182                             next_offset - offset, "%s",
183                             tvb_format_text(tvb, offset, next_offset - offset));
184                         offset = next_offset;
185                 }
186         }
187 }
188
189 static gboolean response_is_continuation(const u_char *data)
190 {
191   if (strncmp(data, "+OK", strlen("+OK")) == 0)
192     return FALSE;
193
194   if (strncmp(data, "-ERR", strlen("-ERR")) == 0)
195     return FALSE;
196
197   return TRUE;
198 }
199
200 void
201 proto_register_pop(void)
202 {
203
204   static hf_register_info hf[] = {
205     { &hf_pop_response,
206       { "Response",           "pop.response",
207         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
208         "TRUE if POP response", HFILL }},
209
210     { &hf_pop_request,
211       { "Request",            "pop.request",
212         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
213         "TRUE if POP request", HFILL }}
214   };
215   static gint *ett[] = {
216     &ett_pop,
217   };
218
219   proto_pop = proto_register_protocol("Post Office Protocol", "POP", "pop");
220   proto_register_field_array(proto_pop, hf, array_length(hf));
221   proto_register_subtree_array(ett, array_length(ett));
222 }
223
224 void
225 proto_reg_handoff_pop(void)
226 {
227   dissector_handle_t pop_handle;
228
229   pop_handle = create_dissector_handle(dissect_pop, proto_pop);
230   dissector_add("tcp.port", TCP_PORT_POP, pop_handle);
231   data_handle = find_dissector("data");
232 }