New proto_tree header_field_info stuff. Header_field_infos now contain
[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.8 1999/10/03 13:44:32 deniel Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@unicom.net>
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
48 void
49 dissect_pop(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
50 {
51         proto_tree      *pop_tree, *ti;
52         gchar          rr[50], rd[1500];
53         int i1 = (u_char *)strchr(pd + offset, ' ') - (pd + offset); /* Where is that space */
54         int i2;
55         int max_data = pi.captured_len - offset;
56
57         memset(rr, '\0', sizeof(rr));
58         memset(rd, '\0', sizeof(rd));
59
60         if ((i1 > max_data) || (i1 <= 0)) {
61           
62           i1 = max_data;
63           strncpy(rr, pd + offset, MIN(max_data - 2, sizeof(rr) - 1));
64
65         }
66         else {
67
68           strncpy(rr, pd + offset, MIN(i1, sizeof(rr) - 1));
69           i2 = ((u_char *)strchr(pd + offset + i1 + 1, '\r') - (pd + offset)) - i1 - 1;
70           if (i2 > max_data - i1 - 1 || i2 <= 0) {
71             i2 = ((u_char *)strchr(pd + offset + i1 + 1, '\n') - (pd + offset)) - i1 - 1;
72             if (i2 > max_data - i1 - 1 || i2 <= 0)
73               i2 = max_data - i1 - 1;
74           }
75           strncpy(rd, pd + offset + i1 + 1, MIN(i2, sizeof(rd) - 1));
76         }
77
78         if (check_col(fd, COL_PROTOCOL))
79                 col_add_str(fd, COL_PROTOCOL, "POP");
80
81         if (check_col(fd, COL_INFO)) {
82
83           col_add_fstr(fd, COL_INFO, "%s: %s %s", (pi.match_port == pi.destport)? "Request" : "Response", rr, rd);        
84         }
85
86         if (tree) {
87
88           ti = proto_tree_add_item(tree, proto_pop, offset, END_OF_FRAME, NULL);
89           pop_tree = proto_item_add_subtree(ti, ETT_POP);
90
91           if (pi.match_port == pi.destport) { /* Request */
92
93             proto_tree_add_text(pop_tree, offset, i1, "Request: %s", rr);
94
95             proto_tree_add_text(pop_tree, offset + i1 + 1, END_OF_FRAME, "Request Arg: %s", rd);
96
97           }
98           else {
99
100             proto_tree_add_text(pop_tree, offset, i1, "Response: %s", rr);
101
102             proto_tree_add_text(pop_tree, offset + i1 + 1, END_OF_FRAME, "Response Arg: %s", rd);
103           }
104
105         }
106 }
107
108 void
109 proto_register_pop(void)
110 {
111 /*        static hf_register_info hf[] = {
112                 { &variable,
113                 { "Name",           "pop.abbreviation", TYPE, VALS_POINTER }},
114         };*/
115
116         proto_pop = proto_register_protocol("Post Office Protocol", "pop");
117  /*       proto_register_field_array(proto_pop, hf, array_length(hf));*/
118 }