Include <time.h> to declare "gmtime()".
[obnox/wireshark/wip.git] / packet-ldap.c
1 /* packet-ldap.c
2  * Routines for ldap packet dissection
3  *
4  * $Id: packet-ldap.c,v 1.1 1999/12/09 04:06:53 nneul Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@unicom.net>
8  * Copyright 1998 Gerald Combs
9  *
10  * Copied from packet-tftp.c
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
35 #endif
36
37 #ifdef HAVE_NETINET_IN_H
38 # include <netinet/in.h>
39 #endif
40
41 #include <string.h>
42 #include <glib.h>
43 #include "packet.h"
44
45 static int proto_ldap = -1;
46 static int hf_ldap_request = -1;
47 static int hf_ldap_response = -1;
48 static int hf_ldap_command = -1;
49
50 static gint ett_ldap = -1;
51
52 void dissect_ldap_request(proto_tree *tree, char *line, int offset, int len)
53 {
54         proto_tree_add_item_hidden(tree, hf_ldap_request,
55                 offset, len, TRUE);
56         proto_tree_add_text(tree, offset, 
57                 len, "Request Line: %s", line);
58 }
59
60 void dissect_ldap_response(proto_tree *tree, char *line, int offset, int len)
61 {
62         proto_tree_add_item_hidden(tree, hf_ldap_response,
63                 offset, len, TRUE);
64         proto_tree_add_text(tree, offset, 
65                 len, "Response Line: %s", line);
66 }
67
68 void
69 dissect_ldap(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
70 {
71         proto_tree      *ldap_tree, *ti;
72         char *tmpline;
73         int start, cur, len;
74         const u_char *i;
75
76         if (check_col(fd, COL_PROTOCOL))
77         col_add_str(fd, COL_PROTOCOL, "LDAP");
78
79         if (check_col(fd, COL_INFO))
80         {
81                 col_add_fstr(fd, COL_INFO, "%s", 
82                         (pi.match_port == pi.destport) ? "Request" : "Response");         
83         }
84
85         if (tree) 
86         {
87                 ti = proto_tree_add_item(tree, proto_ldap, offset, END_OF_FRAME, NULL);
88                 ldap_tree = proto_item_add_subtree(ti, ett_ldap);
89
90                 tmpline = (char *)g_malloc( pi.captured_len );
91                 i = pd+offset;
92                 while ( i < pd + pi.captured_len )
93                 {
94                         start = i - pd;
95                         cur = 0;
96                         len = 0;
97                         tmpline[cur] = 0;
98
99                         /* copy up to end or cr/nl */
100                         while ( i < pd + pi.captured_len && *i != '\r' && *i != '\n' )
101                         {
102                                 tmpline[cur++] = *(i++);
103                                 len++;
104                         }
105                         tmpline[cur] = 0;
106
107                         /* skip any CR/NL */
108                         while ( i < pd + pi.captured_len && 
109                                 (*i == '\r' || *i == '\n') )
110                         {
111                                 i++;
112                                 len++;
113                         }
114
115                         if ( strlen(tmpline) > 0 )
116                         {
117                                 if (pi.match_port == pi.destport)
118                                 {
119                                         dissect_ldap_request(ldap_tree, tmpline, start, len);
120                                 }
121                                 else
122                                 {
123                                         dissect_ldap_response(ldap_tree, tmpline, start, len);
124                                 }
125                         }
126                 }
127                 g_free(tmpline);
128                 tmpline = 0;
129         }
130 }
131
132 void
133 proto_register_ldap(void)
134 {
135         static hf_register_info hf[] = {
136           { &hf_ldap_response,
137             { "Response",           "ldap.response",
138               FT_BOOLEAN, BASE_NONE, NULL, 0x0,
139               "TRUE if LDAP response" }},
140           
141           { &hf_ldap_request,
142             { "Request",            "ldap.request",
143               FT_BOOLEAN, BASE_NONE, NULL, 0x0,
144               "TRUE if LDAP request" }},
145
146           { &hf_ldap_command,
147             { "Command",            "ldap.command",
148               FT_STRING, BASE_NONE, NULL, 0x0,
149               "Command associated with request" }}
150         };
151
152         static gint *ett[] = {
153                 &ett_ldap,
154         };
155         proto_ldap = proto_register_protocol("Lightweight Directory Access Protocol", "ldap");
156         proto_register_field_array(proto_ldap, hf, array_length(hf));
157         proto_register_subtree_array(ett, array_length(ett));
158 }