As with "file_write_error_message()", so with
[obnox/wireshark/wip.git] / packet-sebek.c
1 /* packet-sebek.c
2  * Routines for Sebek - Kernel based data capture - packet dissection
3  * Copyright 1999, Nathan Neulinger <nneul@umr.edu>
4  *
5  * See: http://project.honeynet.org/tools/sebek/ for more details
6  *
7  * $Id: packet-sebek.c,v 1.1 2003/11/19 22:13:29 nneul Exp $
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
34 #include <string.h>
35 #include <time.h>
36 #include <math.h>
37 #include <glib.h>
38
39 #ifdef NEED_SNPRINTF_H
40 # include "snprintf.h"
41 #endif
42
43 #include <epan/packet.h>
44 #include <epan/resolv.h>
45
46 /*
47         IP address:     32bit unsigned
48         MAGIC Val:      32bit unsigned
49         Sebek Ver:      16bit unsigned
50         Type            16bit unsigned
51         Counter:        32bit unsigned
52         Time_sec:       32bit unsigned
53         Time_usec:      32bit unsigned
54         Proc ID:        32bit unsigned
55         User ID:        32bit unsigned
56         File Desc:      32bit unsigned
57         Command:        12char array
58         Length:         Data Length
59
60         Data:           Variable Length data
61
62  *
63  */
64
65 /* By default, but can be completely different */
66 #define UDP_PORT_SEBEK  1101
67
68 static int proto_sebek = -1;
69
70 static int hf_sebek_magic = -1;
71 static int hf_sebek_version = -1;
72 static int hf_sebek_type = -1;
73 static int hf_sebek_counter = -1;
74 static int hf_sebek_time = -1;
75 static int hf_sebek_pid = -1;
76 static int hf_sebek_uid = -1;
77 static int hf_sebek_fd = -1;
78 static int hf_sebek_cmd = -1;
79 static int hf_sebek_len = -1;
80 static int hf_sebek_data = -1;
81
82 static gint ett_sebek = -1;
83
84 /* dissect_sebek - dissects sebek packet data
85  * tvb - tvbuff for packet data (IN)
86  * pinfo - packet info
87  * proto_tree - resolved protocol tree
88  */
89 static void
90 dissect_sebek(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
91 {
92         proto_tree      *sebek_tree;
93         proto_item      *ti;
94         int offset = 0;
95         int datalen = 0;
96         nstime_t ts;
97         
98         if (check_col(pinfo->cinfo, COL_PROTOCOL))
99                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "SEBEK");
100
101         if (check_col(pinfo->cinfo, COL_INFO))
102         {
103                 col_clear(pinfo->cinfo, COL_INFO);
104                 col_set_str(pinfo->cinfo, COL_INFO, "SEBEK - ");
105                 col_append_fstr(pinfo->cinfo, COL_INFO, " pid(%d)", tvb_get_ntohl(tvb, 20));
106                 col_append_fstr(pinfo->cinfo, COL_INFO, " uid(%d)", tvb_get_ntohl(tvb, 24));
107                 col_append_fstr(pinfo->cinfo, COL_INFO, " fd(%d)", tvb_get_ntohl(tvb, 28));
108                 col_append_fstr(pinfo->cinfo, COL_INFO, " cmd: %s", tvb_get_string(tvb, 32, 12));
109         }
110
111
112         if (tree) {
113                 /* Adding NTP item and subtree */
114                 ti = proto_tree_add_item(tree, proto_sebek, tvb, 0, -1, FALSE);
115                 sebek_tree = proto_item_add_subtree(ti, ett_sebek);
116
117                 proto_tree_add_item(sebek_tree, hf_sebek_magic, tvb, offset, 4, FALSE);
118                 offset += 4;
119
120                 proto_tree_add_item(sebek_tree, hf_sebek_version, tvb, offset, 2, FALSE);
121                 offset += 2;
122
123                 proto_tree_add_item(sebek_tree, hf_sebek_type, tvb, offset, 2, FALSE);
124                 offset += 2;
125
126                 proto_tree_add_item(sebek_tree, hf_sebek_counter, tvb, offset, 4, FALSE);
127                 offset += 4;
128
129         ts.secs = tvb_get_ntohl(tvb, offset);
130         ts.nsecs = tvb_get_ntohl(tvb, offset+4);
131         proto_tree_add_time(sebek_tree, hf_sebek_time, tvb, offset, 8, &ts);
132         offset += 8; 
133
134                 proto_tree_add_item(sebek_tree, hf_sebek_pid, tvb, offset, 4, FALSE);
135                 offset += 4;
136
137                 proto_tree_add_item(sebek_tree, hf_sebek_uid, tvb, offset, 4, FALSE);
138                 offset += 4;
139
140                 proto_tree_add_item(sebek_tree, hf_sebek_fd, tvb, offset, 4, FALSE);
141                 offset += 4;
142
143                 proto_tree_add_item(sebek_tree, hf_sebek_cmd, tvb, offset, 12, FALSE);
144                 offset += 12;
145
146                 datalen = tvb_get_letohl(tvb, offset);
147                 proto_tree_add_item(sebek_tree, hf_sebek_len, tvb, offset, 4, FALSE);
148                 offset += 4;
149
150                 proto_tree_add_item(sebek_tree, hf_sebek_data, tvb, offset, -1, FALSE);
151                 
152         }
153 }
154
155 void
156 proto_register_sebek(void)
157 {
158         static hf_register_info hf[] = {
159                 { &hf_sebek_magic, {
160                         "Magic", "sebek.magic", FT_UINT32, BASE_HEX,
161                         NULL, 0, "Magic Number", HFILL }},
162                 { &hf_sebek_version, {
163                         "Version", "sebek.version", FT_UINT16, BASE_DEC,
164                         NULL, 0, "Version Number", HFILL }},
165                 { &hf_sebek_type, {
166                         "Type", "sebek.type", FT_UINT16, BASE_DEC,
167                         NULL, 0, "Type", HFILL }},
168                 { &hf_sebek_counter, {
169                         "Counter", "sebek.counter", FT_UINT32, BASE_DEC,
170                         NULL, 0, "Counter", HFILL }},
171                 { &hf_sebek_time, {
172                         "Time", "sebek.time.sec", FT_ABSOLUTE_TIME, BASE_NONE,
173                         NULL, 0, "Time", HFILL }},
174                 { &hf_sebek_pid, {
175                         "Process ID", "sebek.pid", FT_UINT32, BASE_DEC,
176                         NULL, 0, "Process ID", HFILL }},
177                 { &hf_sebek_uid, {
178                         "User ID", "sebek.uid", FT_UINT32, BASE_DEC,
179                         NULL, 0, "User ID", HFILL }},
180                 { &hf_sebek_fd, {
181                         "File Descriptor", "sebek.fd", FT_UINT32, BASE_DEC,
182                         NULL, 0, "File Descriptor Number", HFILL }},
183                 { &hf_sebek_cmd, {
184                         "Command Name", "sebek.cmd", FT_STRING, 0,
185                         NULL, 0, "Command Name", HFILL }},
186                 { &hf_sebek_len, {
187                         "Data Length", "sebek.len", FT_UINT32, BASE_DEC,
188                         NULL, 0, "Data Length", HFILL }},
189                 { &hf_sebek_data, {
190                         "Data", "sebek.data", FT_STRING, 0,
191                         NULL, 0, "Data", HFILL }},
192         };
193         static gint *ett[] = {
194                 &ett_sebek,
195         };
196
197         proto_sebek = proto_register_protocol("SEBEK - Kernel Data Capture", "SEBEK",
198             "sebek");
199         proto_register_field_array(proto_sebek, hf, array_length(hf));
200         proto_register_subtree_array(ett, array_length(ett));
201 }
202
203 void
204 proto_reg_handoff_sebek(void)
205 {
206         dissector_handle_t sebek_handle;
207
208         sebek_handle = create_dissector_handle(dissect_sebek, proto_sebek);
209         dissector_add("udp.port", UDP_PORT_SEBEK, sebek_handle);
210 }