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.
38 #include <dbus/dbus.h>
41 #include "pcap-dbus.h"
44 * Private data for capturing on D-Bus.
48 u_int packets_read
; /* count of packets read */
52 dbus_read(pcap_t
*handle
, int max_packets _U_
, pcap_handler callback
, u_char
*user
)
54 struct pcap_dbus
*handlep
= handle
->priv
;
56 struct pcap_pkthdr pkth
;
64 message
= dbus_connection_pop_message(handlep
->conn
);
67 /* XXX handle->opt.timeout = timeout_ms; */
68 if (!dbus_connection_read_write(handlep
->conn
, 100)) {
69 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Connection closed");
73 if (handle
->break_loop
) {
74 handle
->break_loop
= 0;
78 message
= dbus_connection_pop_message(handlep
->conn
);
81 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected")) {
82 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Disconnected");
83 dbus_message_unref(message
);
87 if (dbus_message_marshal(message
, &raw_msg
, &raw_msg_len
)) {
88 pkth
.caplen
= pkth
.len
= raw_msg_len
;
89 /* pkth.caplen = min (payload_len, handle->snapshot); */
91 gettimeofday(&pkth
.ts
, NULL
);
92 if (handle
->fcode
.bf_insns
== NULL
||
93 pcapint_filter(handle
->fcode
.bf_insns
, (u_char
*)raw_msg
, pkth
.len
, pkth
.caplen
)) {
94 handlep
->packets_read
++;
95 callback(user
, &pkth
, (u_char
*)raw_msg
);
102 dbus_message_unref(message
);
108 dbus_write(pcap_t
*handle
, const void *buf
, int size
)
110 /* XXX, not tested */
111 struct pcap_dbus
*handlep
= handle
->priv
;
113 DBusError error
= DBUS_ERROR_INIT
;
116 if (!(msg
= dbus_message_demarshal(buf
, size
, &error
))) {
117 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dbus_message_demarshal() failed: %s", error
.message
);
118 dbus_error_free(&error
);
122 dbus_connection_send(handlep
->conn
, msg
, NULL
);
123 dbus_connection_flush(handlep
->conn
);
125 dbus_message_unref(msg
);
130 dbus_stats(pcap_t
*handle
, struct pcap_stat
*stats
)
132 struct pcap_dbus
*handlep
= handle
->priv
;
134 stats
->ps_recv
= handlep
->packets_read
;
136 stats
->ps_ifdrop
= 0;
141 dbus_cleanup(pcap_t
*handle
)
143 struct pcap_dbus
*handlep
= handle
->priv
;
145 dbus_connection_unref(handlep
->conn
);
147 pcapint_cleanup_live_common(handle
);
151 * We don't support non-blocking mode. I'm not sure what we'd
152 * do to support it and, given that we don't support select()/
153 * poll()/epoll_wait()/kevent() etc., it probably doesn't
157 dbus_getnonblock(pcap_t
*p
)
159 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
160 "Non-blocking mode isn't supported for capturing on D-Bus");
165 dbus_setnonblock(pcap_t
*p
, int nonblock _U_
)
167 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
168 "Non-blocking mode isn't supported for capturing on D-Bus");
173 dbus_activate(pcap_t
*handle
)
175 #define EAVESDROPPING_RULE "eavesdrop=true,"
177 static const char *rules
[] = {
178 EAVESDROPPING_RULE
"type='signal'",
179 EAVESDROPPING_RULE
"type='method_call'",
180 EAVESDROPPING_RULE
"type='method_return'",
181 EAVESDROPPING_RULE
"type='error'",
184 #define N_RULES sizeof(rules)/sizeof(rules[0])
186 struct pcap_dbus
*handlep
= handle
->priv
;
187 const char *dev
= handle
->opt
.device
;
189 DBusError error
= DBUS_ERROR_INIT
;
192 if (strcmp(dev
, "dbus-system") == 0) {
193 if (!(handlep
->conn
= dbus_bus_get(DBUS_BUS_SYSTEM
, &error
))) {
194 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to get system bus: %s", error
.message
);
195 dbus_error_free(&error
);
199 } else if (strcmp(dev
, "dbus-session") == 0) {
200 if (!(handlep
->conn
= dbus_bus_get(DBUS_BUS_SESSION
, &error
))) {
201 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to get session bus: %s", error
.message
);
202 dbus_error_free(&error
);
206 } else if (strncmp(dev
, "dbus://", 7) == 0) {
207 const char *addr
= dev
+ 7;
209 if (!(handlep
->conn
= dbus_connection_open(addr
, &error
))) {
210 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to open connection to: %s: %s", addr
, error
.message
);
211 dbus_error_free(&error
);
215 if (!dbus_bus_register(handlep
->conn
, &error
)) {
216 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to register bus %s: %s\n", addr
, error
.message
);
217 dbus_error_free(&error
);
222 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't get bus address from %s", handle
->opt
.device
);
226 /* Initialize some components of the pcap structure. */
229 handle
->linktype
= DLT_DBUS
;
230 handle
->read_op
= dbus_read
;
231 handle
->inject_op
= dbus_write
;
232 handle
->setfilter_op
= pcapint_install_bpf_program
; /* XXX, later add support for dbus_bus_add_match() */
233 handle
->setdirection_op
= NULL
;
234 handle
->set_datalink_op
= NULL
; /* can't change data link type */
235 handle
->getnonblock_op
= dbus_getnonblock
;
236 handle
->setnonblock_op
= dbus_setnonblock
;
237 handle
->stats_op
= dbus_stats
;
238 handle
->cleanup_op
= dbus_cleanup
;
242 * Unfortunately, trying to do a select()/poll()/epoll_wait()/
243 * kevent()/etc. on a D-Bus connection isn't a simple
244 * case of "give me an FD on which to wait".
246 * Apparently, you have to register "add watch", "remove watch",
247 * and "toggle watch" functions with
248 * dbus_connection_set_watch_functions(),
249 * keep a *set* of FDs, add to that set in the "add watch"
250 * function, subtract from it in the "remove watch" function,
251 * and either add to or subtract from that set in the "toggle
252 * watch" function, and do the wait on *all* of the FDs in the
253 * set. (Yes, you need the "toggle watch" function, so that
254 * the main loop doesn't itself need to check for whether
255 * a given watch is enabled or disabled - most libpcap programs
256 * know nothing about D-Bus and shouldn't *have* to know anything
257 * about D-Bus other than how to decode D-Bus messages.)
259 * Implementing that would require considerable changes in
260 * the way libpcap exports "selectable FDs" to its client.
261 * Until that's done, we just say "you can't do that".
263 handle
->selectable_fd
= handle
->fd
= -1;
266 if (handle
->opt
.rfmon
) {
268 * Monitor mode doesn't apply to dbus connections.
270 dbus_cleanup(handle
);
271 return PCAP_ERROR_RFMON_NOTSUP
;
275 * Turn a negative snapshot value (invalid), a snapshot value of
276 * 0 (unspecified), or a value bigger than the normal maximum
277 * value, into the maximum message length for D-Bus (128MB).
279 if (handle
->snapshot
<= 0 || handle
->snapshot
> 134217728)
280 handle
->snapshot
= 134217728;
282 /* dbus_connection_set_max_message_size(handlep->conn, handle->snapshot); */
283 if (handle
->opt
.buffer_size
!= 0)
284 dbus_connection_set_max_received_size(handlep
->conn
, handle
->opt
.buffer_size
);
286 for (i
= 0; i
< N_RULES
; i
++) {
287 dbus_bus_add_match(handlep
->conn
, rules
[i
], &error
);
288 if (dbus_error_is_set(&error
)) {
289 dbus_error_free(&error
);
291 /* try without eavesdrop */
292 dbus_bus_add_match(handlep
->conn
, rules
[i
] + strlen(EAVESDROPPING_RULE
), &error
);
293 if (dbus_error_is_set(&error
)) {
294 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to add bus match: %s\n", error
.message
);
295 dbus_error_free(&error
);
296 dbus_cleanup(handle
);
306 dbus_create(const char *device
, char *ebuf
, int *is_ours
)
310 if (strcmp(device
, "dbus-system") &&
311 strcmp(device
, "dbus-session") &&
312 strncmp(device
, "dbus://", 7))
319 p
= PCAP_CREATE_COMMON(ebuf
, struct pcap_dbus
);
323 p
->activate_op
= dbus_activate
;
325 * Set these up front, so that, even if our client tries
326 * to set non-blocking mode before we're activated, or
327 * query the state of non-blocking mode, they get an error,
328 * rather than having the non-blocking mode option set
331 p
->getnonblock_op
= dbus_getnonblock
;
332 p
->setnonblock_op
= dbus_setnonblock
;
337 dbus_findalldevs(pcap_if_list_t
*devlistp
, char *err_str
)
340 * The notion of "connected" vs. "disconnected" doesn't apply.
341 * XXX - what about the notions of "up" and "running"?
343 if (pcapint_add_dev(devlistp
, "dbus-system",
344 PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
, "D-Bus system bus",
347 if (pcapint_add_dev(devlistp
, "dbus-session",
348 PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
, "D-Bus session bus",