2 * Copyright (c) 2012 Jakub Zawadzki
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
40 #include <dbus/dbus.h>
43 #include "pcap-dbus.h"
46 * Private data for capturing on D-Bus.
50 u_int packets_read; /* count of packets read */
54 dbus_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user)
56 struct pcap_dbus *handlep = handle->priv;
58 struct pcap_pkthdr pkth;
66 message = dbus_connection_pop_message(handlep->conn);
69 /* XXX handle->opt.timeout = timeout_ms; */
70 if (!dbus_connection_read_write(handlep->conn, 100)) {
71 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Connection closed");
75 if (handle->break_loop) {
76 handle->break_loop = 0;
80 message = dbus_connection_pop_message(handlep->conn);
83 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
84 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Disconnected");
88 if (dbus_message_marshal(message, &raw_msg, &raw_msg_len)) {
89 pkth.caplen = pkth.len = raw_msg_len;
90 /* pkth.caplen = min (payload_len, handle->snapshot); */
92 gettimeofday(&pkth.ts, NULL);
93 if (handle->fcode.bf_insns == NULL ||
94 bpf_filter(handle->fcode.bf_insns, (u_char *)raw_msg, pkth.len, pkth.caplen)) {
95 handlep->packets_read++;
96 callback(user, &pkth, (u_char *)raw_msg);
106 dbus_write(pcap_t *handle, const void *buf, size_t size)
108 /* XXX, not tested */
109 struct pcap_dbus *handlep = handle->priv;
111 DBusError error = DBUS_ERROR_INIT;
114 if (!(msg = dbus_message_demarshal(buf, size, &error))) {
115 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dbus_message_demarshal() failed: %s", error.message);
116 dbus_error_free(&error);
120 dbus_connection_send(handlep->conn, msg, NULL);
121 dbus_connection_flush(handlep->conn);
123 dbus_message_unref(msg);
128 dbus_stats(pcap_t *handle, struct pcap_stat *stats)
130 struct pcap_dbus *handlep = handle->priv;
132 stats->ps_recv = handlep->packets_read;
134 stats->ps_ifdrop = 0;
139 dbus_cleanup(pcap_t *handle)
141 struct pcap_dbus *handlep = handle->priv;
143 dbus_connection_unref(handlep->conn);
145 pcap_cleanup_live_common(handle);
149 * We don't support non-blocking mode. I'm not sure what we'd
150 * do to support it and, given that we don't support select()/
151 * poll()/epoll_wait()/kevent() etc., it probably doesn't
155 dbus_getnonblock(pcap_t *p)
157 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
158 "Non-blocking mode isn't supported for capturing on D-Bus");
163 dbus_setnonblock(pcap_t *p, int nonblock _U_)
165 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
166 "Non-blocking mode isn't supported for capturing on D-Bus");
171 dbus_activate(pcap_t *handle)
173 #define EAVESDROPPING_RULE "eavesdrop=true,"
175 static const char *rules[] = {
176 EAVESDROPPING_RULE "type='signal'",
177 EAVESDROPPING_RULE "type='method_call'",
178 EAVESDROPPING_RULE "type='method_return'",
179 EAVESDROPPING_RULE "type='error'",
182 #define N_RULES sizeof(rules)/sizeof(rules[0])
184 struct pcap_dbus *handlep = handle->priv;
185 const char *dev = handle->opt.device;
187 DBusError error = DBUS_ERROR_INIT;
190 if (strcmp(dev, "dbus-system") == 0) {
191 if (!(handlep->conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) {
192 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to get system bus: %s", error.message);
193 dbus_error_free(&error);
197 } else if (strcmp(dev, "dbus-session") == 0) {
198 if (!(handlep->conn = dbus_bus_get(DBUS_BUS_SESSION, &error))) {
199 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to get session bus: %s", error.message);
200 dbus_error_free(&error);
204 } else if (strncmp(dev, "dbus://", 7) == 0) {
205 const char *addr = dev + 7;
207 if (!(handlep->conn = dbus_connection_open(addr, &error))) {
208 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to open connection to: %s: %s", addr, error.message);
209 dbus_error_free(&error);
213 if (!dbus_bus_register(handlep->conn, &error)) {
214 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to register bus %s: %s\n", addr, error.message);
215 dbus_error_free(&error);
220 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't get bus address from %s", handle->opt.device);
224 /* Initialize some components of the pcap structure. */
227 handle->linktype = DLT_DBUS;
228 handle->read_op = dbus_read;
229 handle->inject_op = dbus_write;
230 handle->setfilter_op = install_bpf_program; /* XXX, later add support for dbus_bus_add_match() */
231 handle->setdirection_op = NULL;
232 handle->set_datalink_op = NULL; /* can't change data link type */
233 handle->getnonblock_op = dbus_getnonblock;
234 handle->setnonblock_op = dbus_setnonblock;
235 handle->stats_op = dbus_stats;
236 handle->cleanup_op = dbus_cleanup;
240 * Unfortunately, trying to do a select()/poll()/epoll_wait()/
241 * kevent()/etc. on a D-Bus connection isn't a simple
242 * case of "give me an FD on which to wait".
244 * Apparently, you have to register "add watch", "remove watch",
245 * and "toggle watch" functions with
246 * dbus_connection_set_watch_functions(),
247 * keep a *set* of FDs, add to that set in the "add watch"
248 * function, subtract from it in the "remove watch" function,
249 * and either add to or subtract from that set in the "toggle
250 * watch" function, and do the wait on *all* of the FDs in the
251 * set. (Yes, you need the "toggle watch" function, so that
252 * the main loop doesn't itself need to check for whether
253 * a given watch is enabled or disabled - most libpcap programs
254 * know nothing about D-Bus and shouldn't *have* to know anything
255 * about D-Bus other than how to decode D-Bus messages.)
257 * Implementing that would require considerable changes in
258 * the way libpcap exports "selectable FDs" to its client.
259 * Until that's done, we just say "you can't do that".
261 handle->selectable_fd = handle->fd = -1;
264 if (handle->opt.rfmon) {
266 * Monitor mode doesn't apply to dbus connections.
268 dbus_cleanup(handle);
269 return PCAP_ERROR_RFMON_NOTSUP;
273 * Turn a negative snapshot value (invalid), a snapshot value of
274 * 0 (unspecified), or a value bigger than the normal maximum
275 * value, into the maximum message length for D-Bus (128MB).
277 if (handle->snapshot <= 0 || handle->snapshot > 134217728)
278 handle->snapshot = 134217728;
280 /* dbus_connection_set_max_message_size(handlep->conn, handle->snapshot); */
281 if (handle->opt.buffer_size != 0)
282 dbus_connection_set_max_received_size(handlep->conn, handle->opt.buffer_size);
284 for (i = 0; i < N_RULES; i++) {
285 dbus_bus_add_match(handlep->conn, rules[i], &error);
286 if (dbus_error_is_set(&error)) {
287 dbus_error_free(&error);
289 /* try without eavesdrop */
290 dbus_bus_add_match(handlep->conn, rules[i] + strlen(EAVESDROPPING_RULE), &error);
291 if (dbus_error_is_set(&error)) {
292 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to add bus match: %s\n", error.message);
293 dbus_error_free(&error);
294 dbus_cleanup(handle);
304 dbus_create(const char *device, char *ebuf, int *is_ours)
308 if (strcmp(device, "dbus-system") &&
309 strcmp(device, "dbus-session") &&
310 strncmp(device, "dbus://", 7))
317 p = pcap_create_common(ebuf, sizeof (struct pcap_dbus));
321 p->activate_op = dbus_activate;
323 * Set these up front, so that, even if our client tries
324 * to set non-blocking mode before we're activated, or
325 * query the state of non-blocking mode, they get an error,
326 * rather than having the non-blocking mode option set
329 p->getnonblock_op = dbus_getnonblock;
330 p->setnonblock_op = dbus_setnonblock;
335 dbus_findalldevs(pcap_if_list_t *devlistp, char *err_str)
338 * The notion of "connected" vs. "disconnected" doesn't apply.
339 * XXX - what about the notions of "up" and "running"?
341 if (add_dev(devlistp, "dbus-system",
342 PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE, "D-Bus system bus",
345 if (add_dev(devlistp, "dbus-session",
346 PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE, "D-Bus session bus",