Add tvbuff class.
[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.14 2000/05/11 08:15:33 gram Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
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
46 static int proto_pop = -1;
47 static int hf_pop_response = -1;
48 static int hf_pop_request = -1;
49
50 static gint ett_pop = -1;
51
52 #define TCP_PORT_POP                    110
53
54 static gboolean is_continuation(const u_char *data);
55         
56 static void
57 dissect_pop(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
58 {
59         proto_tree      *pop_tree, *ti;
60         gchar          rr[50], rd[1500];
61         int i1 = (u_char *)strchr(pd + offset, ' ') - (pd + offset); /* Where is that space */
62         int i2;
63         int max_data = pi.captured_len - offset;
64
65         memset(rr, '\0', sizeof(rr));
66         memset(rd, '\0', sizeof(rd));
67
68         if ((i1 > max_data) || (i1 <= 0)) {
69           
70           i1 = max_data;
71           strncpy(rr, pd + offset, MIN(max_data - 2, sizeof(rr) - 1));
72
73         }
74         else {
75
76           strncpy(rr, pd + offset, MIN(i1, sizeof(rr) - 1));
77           i2 = ((u_char *)strchr(pd + offset + i1 + 1, '\r') - (pd + offset)) - i1 - 1;
78           if (i2 > max_data - i1 - 1 || i2 <= 0) {
79             i2 = ((u_char *)strchr(pd + offset + i1 + 1, '\n') - (pd + offset)) - i1 - 1;
80             if (i2 > max_data - i1 - 1 || i2 <= 0)
81               i2 = max_data - i1 - 1;
82           }
83           strncpy(rd, pd + offset + i1 + 1, MIN(i2, sizeof(rd) - 1));
84         }
85
86         if (check_col(fd, COL_PROTOCOL))
87           col_add_str(fd, COL_PROTOCOL, "POP");
88
89         if (check_col(fd, COL_INFO)) {
90           /*
91            * Do it like HTTP does.
92            * Put the first line from the buffer into the summary,
93            * if it's an POP request or reply.
94            * Otherwise, just call it a continuation.
95            */
96           if (pi.match_port == pi.srcport && is_continuation(pd+offset))
97             col_add_str(fd, COL_INFO, "Continuation");
98           else
99             col_add_fstr(fd, COL_INFO, "%s: %s %s", (pi.match_port == pi.destport)? "Request" : "Response", rr, rd);      
100         }
101
102         if (tree) {
103
104           ti = proto_tree_add_item(tree, proto_pop, NullTVB, offset, END_OF_FRAME, NULL);
105           pop_tree = proto_item_add_subtree(ti, ett_pop);
106
107           if (pi.match_port == pi.destport) { /* Request */
108             proto_tree_add_item_hidden(pop_tree, hf_pop_request, NullTVB, offset, i1, TRUE);
109             proto_tree_add_text(pop_tree, NullTVB, offset, i1, "Request: %s", rr);
110
111             if (strlen(rd) != 0)
112               proto_tree_add_text(pop_tree, NullTVB, offset + i1 + 1, END_OF_FRAME, "Request Arg: %s", rd);
113
114           }
115           else {
116             proto_tree_add_item_hidden(pop_tree, hf_pop_response, NullTVB, offset, i1, TRUE);
117
118             if (is_continuation(pd+offset))
119               dissect_data(pd, offset, fd, pop_tree);
120             else {
121               proto_tree_add_text(pop_tree, NullTVB, offset, i1, "Response: %s", rr);
122
123               if (strlen(rd) != 0)
124               proto_tree_add_text(pop_tree, NullTVB, offset + i1 + 1, END_OF_FRAME, "Response Arg: %s", rd);
125             }
126           }
127
128         }
129 }
130
131 static gboolean is_continuation(const u_char *data)
132 {
133   if (strncmp(data, "+OK", strlen("+OK")) == 0)
134     return FALSE;
135
136   if (strncmp(data, "-ERR", strlen("-ERR")) == 0)
137     return FALSE;
138
139   return TRUE;
140 }
141
142 void
143 proto_register_pop(void)
144 {
145
146   static hf_register_info hf[] = {
147     { &hf_pop_response,
148       { "Response",           "pop.response",
149         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
150         "TRUE if POP response" }},
151
152     { &hf_pop_request,
153       { "Request",            "pop.request",
154         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
155         "TRUE if POP request" }}
156   };
157   static gint *ett[] = {
158     &ett_pop,
159   };
160
161   proto_pop = proto_register_protocol("Post Office Protocol", "pop");
162   proto_register_field_array(proto_pop, hf, array_length(hf));
163   proto_register_subtree_array(ett, array_length(ett));
164 }
165
166 void
167 proto_reg_handoff_pop(void)
168 {
169   dissector_add("tcp.port", TCP_PORT_POP, dissect_pop);
170 }