]> The Tcpdump Group git mirrors - libpcap/blob - pcap-dbus.c
Pick up some fixes from a comment for the SourceForge bug for D-Bus support.
[libpcap] / pcap-dbus.c
1 /*
2 * Copyright (c) 2012 Jakub Zawadzki
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include "pcap-int.h"
36
37 #include <string.h>
38
39 #include <time.h>
40 #include <sys/time.h>
41
42 #include <dbus/dbus.h>
43
44 static int
45 dbus_read(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
46 {
47 DBusConnection *conn = handle->md.priv;
48
49 struct pcap_pkthdr pkth;
50 DBusMessage *message;
51
52 char *raw_msg;
53 int raw_msg_len;
54
55 int count = 0;
56
57 message = dbus_connection_pop_message(conn);
58
59 while (!message) {
60 // XXX p->md.timeout = timeout_ms;
61 if (!dbus_connection_read_write(conn, 100)) {
62 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Connection closed");
63 return -1;
64 }
65
66 if (handle->break_loop) {
67 handle->break_loop = 0;
68 return -2;
69 }
70
71 message = dbus_connection_pop_message(conn);
72 }
73
74 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
75 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Disconnected");
76 return -1;
77 }
78
79 if (dbus_message_marshal(message, &raw_msg, &raw_msg_len)) {
80 pkth.caplen = pkth.len = raw_msg_len;
81 /* pkth.caplen = min (payload_len, handle->snapshot); */
82
83 gettimeofday(&pkth.ts, NULL);
84 if (handle->fcode.bf_insns == NULL || bpf_filter(handle->fcode.bf_insns, raw_msg, pkth.len, pkth.caplen)) {
85 handle->md.packets_read++;
86 callback(user, &pkth, raw_msg);
87 count++;
88 }
89
90 dbus_free(raw_msg);
91 }
92 return count;
93 }
94
95 static int
96 dbus_write(pcap_t *handle, const void *buf, size_t size)
97 {
98 /* XXX, not tested */
99 DBusConnection *conn = handle->md.priv;
100
101 DBusError error = DBUS_ERROR_INIT;
102 DBusMessage *msg;
103
104 if (!(msg = dbus_message_demarshal(buf, size, &error))) {
105 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dbus_message_demarshal() failed: %s", error.message);
106 dbus_error_free(&error);
107 return -1;
108 }
109
110 dbus_connection_send(conn, msg, NULL);
111 dbus_connection_flush(conn);
112
113 dbus_message_unref(msg);
114 return 0;
115 }
116
117 static int
118 dbus_stats(pcap_t *handle, struct pcap_stat *stats)
119 {
120 stats->ps_recv = handle->md.packets_read;
121 stats->ps_drop = 0;
122 stats->ps_ifdrop = 0;
123 return 0;
124 }
125
126 static void
127 dbus_cleanup(pcap_t *handle)
128 {
129 DBusConnection *conn = handle->md.priv;
130
131 handle->md.priv = NULL;
132 dbus_connection_unref(conn);
133
134 pcap_cleanup_live_common(handle);
135 }
136
137 static int
138 dbus_activate(pcap_t *handle)
139 {
140 #define EAVESDROPPING_RULE "eavesdrop=true,"
141
142 static const char *rules[] = {
143 EAVESDROPPING_RULE "type='signal'",
144 EAVESDROPPING_RULE "type='method_call'",
145 EAVESDROPPING_RULE "type='method_return'",
146 EAVESDROPPING_RULE "type='error'",
147 };
148
149 #define N_RULES sizeof(rules)/sizeof(rules[0])
150
151 const char *dev = handle->opt.source;
152
153 DBusError error = DBUS_ERROR_INIT;
154 DBusConnection *conn;
155 int i;
156
157 if (strcmp(dev, "dbus-system") == 0) {
158 if (!(conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) {
159 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to get system bus: %s", error.message);
160 dbus_error_free(&error);
161 return PCAP_ERROR;
162 }
163
164 } else if (strcmp(dev, "dbus-session") == 0) {
165 if (!(conn = dbus_bus_get(DBUS_BUS_SESSION, &error))) {
166 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to get session bus: %s", error.message);
167 dbus_error_free(&error);
168 return PCAP_ERROR;
169 }
170
171 } else if (strncmp(dev, "dbus://", 7) == 0) {
172 const char *addr = dev + 7;
173
174 if (!(conn = dbus_connection_open(addr, &error))) {
175 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to open connection to: %s: %s", addr, error.message);
176 dbus_error_free(&error);
177 return PCAP_ERROR;
178 }
179
180 if (!dbus_bus_register(conn, &error)) {
181 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to register bus %s: %s\n", addr, error.message);
182 dbus_error_free(&error);
183 return PCAP_ERROR;
184 }
185
186 } else {
187 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't get bus address from %s", handle->opt.source);
188 return PCAP_ERROR;
189 }
190
191 /* Initialize some components of the pcap structure. */
192 handle->bufsize = 0;
193 handle->offset = 0;
194 handle->linktype = DLT_DBUS;
195 handle->read_op = dbus_read;
196 handle->inject_op = dbus_write;
197 handle->setfilter_op = install_bpf_program; /* XXX, later add support for dbus_bus_add_match() */
198 handle->setdirection_op = NULL;
199 handle->set_datalink_op = NULL; /* can't change data link type */
200 handle->getnonblock_op = pcap_getnonblock_fd;
201 handle->setnonblock_op = pcap_setnonblock_fd;
202 handle->stats_op = dbus_stats;
203
204 handle->selectable_fd = handle->fd = -1;
205 handle->md.priv = conn;
206
207 if (handle->opt.rfmon) {
208 /*
209 * Monitor mode doesn't apply to dbus connections.
210 */
211 dbus_cleanup(handle);
212 return PCAP_ERROR_RFMON_NOTSUP;
213 }
214
215 /* dbus_connection_set_max_message_size(conn, handle->snapshot); */
216 if (handle->opt.buffer_size != 0)
217 dbus_connection_set_max_received_size(conn, handle->opt.buffer_size);
218
219 for (i = 0; i < N_RULES; i++) {
220 dbus_bus_add_match(conn, rules[i], &error);
221 if (dbus_error_is_set(&error)) {
222 dbus_error_free(&error);
223
224 /* try without eavesdrop */
225 dbus_bus_add_match(conn, rules[i] + strlen(EAVESDROPPING_RULE), &error);
226 if (dbus_error_is_set(&error)) {
227 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to add bus match: %s\n", error.message);
228 dbus_error_free(&error);
229 dbus_cleanup(handle);
230 return PCAP_ERROR;
231 }
232 }
233 }
234
235 return 0;
236 }
237
238 pcap_t *
239 dbus_create(const char *device, char *ebuf, int *is_ours)
240 {
241 pcap_t *p;
242
243 if (strcmp(device, "dbus-system") &&
244 strcmp(device, "dbus-session") &&
245 strncmp(device, "dbus://", 7))
246 {
247 *is_ours = 0;
248 return NULL;
249 }
250
251 *is_ours = 1;
252 p = pcap_create_common(device, ebuf);
253 if (p == NULL)
254 return (NULL);
255
256 p->activate_op = dbus_activate;
257 return (p);
258 }
259
260 int
261 dbus_findalldevs(pcap_if_t **alldevsp, char *err_str)
262 {
263 pcap_if_t *found_dev = *alldevsp;
264
265 if (pcap_add_if(&found_dev, "dbus-system", 0, "D-BUS system bus", err_str) < 0)
266 return -1;
267 if (pcap_add_if(&found_dev, "dbus-session", 0, "D-BUS session bus", err_str) < 0)
268 return -1;
269 return 0;
270 }
271