1 /* $NetBSD: pcap-dbus.c,v 1.3 2015/03/31 21:39:42 christos Exp $ */
4 * Copyright (c) 2012 Jakub Zawadzki
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior written
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: pcap-dbus.c,v 1.3 2015/03/31 21:39:42 christos Exp $");
45 #include <dbus/dbus.h>
48 #include "pcap-dbus.h"
51 * Private data for capturing on D-Bus.
55 u_int packets_read
; /* count of packets read */
59 dbus_read(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
61 struct pcap_dbus
*handlep
= handle
->priv
;
63 struct pcap_pkthdr pkth
;
71 message
= dbus_connection_pop_message(handlep
->conn
);
74 // XXX handle->opt.timeout = timeout_ms;
75 if (!dbus_connection_read_write(handlep
->conn
, 100)) {
76 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Connection closed");
80 if (handle
->break_loop
) {
81 handle
->break_loop
= 0;
85 message
= dbus_connection_pop_message(handlep
->conn
);
88 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected")) {
89 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Disconnected");
93 if (dbus_message_marshal(message
, &raw_msg
, &raw_msg_len
)) {
94 pkth
.caplen
= pkth
.len
= raw_msg_len
;
95 /* pkth.caplen = min (payload_len, handle->snapshot); */
97 gettimeofday(&pkth
.ts
, NULL
);
98 if (handle
->fcode
.bf_insns
== NULL
||
99 bpf_filter(handle
->fcode
.bf_insns
, (u_char
*)raw_msg
, pkth
.len
, pkth
.caplen
)) {
100 handlep
->packets_read
++;
101 callback(user
, &pkth
, (u_char
*)raw_msg
);
111 dbus_write(pcap_t
*handle
, const void *buf
, size_t size
)
113 /* XXX, not tested */
114 struct pcap_dbus
*handlep
= handle
->priv
;
116 DBusError error
= DBUS_ERROR_INIT
;
119 if (!(msg
= dbus_message_demarshal(buf
, size
, &error
))) {
120 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dbus_message_demarshal() failed: %s", error
.message
);
121 dbus_error_free(&error
);
125 dbus_connection_send(handlep
->conn
, msg
, NULL
);
126 dbus_connection_flush(handlep
->conn
);
128 dbus_message_unref(msg
);
133 dbus_stats(pcap_t
*handle
, struct pcap_stat
*stats
)
135 struct pcap_dbus
*handlep
= handle
->priv
;
137 stats
->ps_recv
= handlep
->packets_read
;
139 stats
->ps_ifdrop
= 0;
144 dbus_cleanup(pcap_t
*handle
)
146 struct pcap_dbus
*handlep
= handle
->priv
;
148 dbus_connection_unref(handlep
->conn
);
150 pcap_cleanup_live_common(handle
);
154 dbus_activate(pcap_t
*handle
)
156 #define EAVESDROPPING_RULE "eavesdrop=true,"
158 static const char *rules
[] = {
159 EAVESDROPPING_RULE
"type='signal'",
160 EAVESDROPPING_RULE
"type='method_call'",
161 EAVESDROPPING_RULE
"type='method_return'",
162 EAVESDROPPING_RULE
"type='error'",
165 #define N_RULES sizeof(rules)/sizeof(rules[0])
167 struct pcap_dbus
*handlep
= handle
->priv
;
168 const char *dev
= handle
->opt
.source
;
170 DBusError error
= DBUS_ERROR_INIT
;
173 if (strcmp(dev
, "dbus-system") == 0) {
174 if (!(handlep
->conn
= dbus_bus_get(DBUS_BUS_SYSTEM
, &error
))) {
175 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to get system bus: %s", error
.message
);
176 dbus_error_free(&error
);
180 } else if (strcmp(dev
, "dbus-session") == 0) {
181 if (!(handlep
->conn
= dbus_bus_get(DBUS_BUS_SESSION
, &error
))) {
182 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to get session bus: %s", error
.message
);
183 dbus_error_free(&error
);
187 } else if (strncmp(dev
, "dbus://", 7) == 0) {
188 const char *addr
= dev
+ 7;
190 if (!(handlep
->conn
= dbus_connection_open(addr
, &error
))) {
191 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to open connection to: %s: %s", addr
, error
.message
);
192 dbus_error_free(&error
);
196 if (!dbus_bus_register(handlep
->conn
, &error
)) {
197 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to register bus %s: %s\n", addr
, error
.message
);
198 dbus_error_free(&error
);
203 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't get bus address from %s", handle
->opt
.source
);
207 /* Initialize some components of the pcap structure. */
210 handle
->linktype
= DLT_DBUS
;
211 handle
->read_op
= dbus_read
;
212 handle
->inject_op
= dbus_write
;
213 handle
->setfilter_op
= install_bpf_program
; /* XXX, later add support for dbus_bus_add_match() */
214 handle
->setdirection_op
= NULL
;
215 handle
->set_datalink_op
= NULL
; /* can't change data link type */
216 handle
->getnonblock_op
= pcap_getnonblock_fd
;
217 handle
->setnonblock_op
= pcap_setnonblock_fd
;
218 handle
->stats_op
= dbus_stats
;
220 handle
->selectable_fd
= handle
->fd
= -1;
222 if (handle
->opt
.rfmon
) {
224 * Monitor mode doesn't apply to dbus connections.
226 dbus_cleanup(handle
);
227 return PCAP_ERROR_RFMON_NOTSUP
;
230 /* dbus_connection_set_max_message_size(handlep->conn, handle->snapshot); */
231 if (handle
->opt
.buffer_size
!= 0)
232 dbus_connection_set_max_received_size(handlep
->conn
, handle
->opt
.buffer_size
);
234 for (i
= 0; i
< N_RULES
; i
++) {
235 dbus_bus_add_match(handlep
->conn
, rules
[i
], &error
);
236 if (dbus_error_is_set(&error
)) {
237 dbus_error_free(&error
);
239 /* try without eavesdrop */
240 dbus_bus_add_match(handlep
->conn
, rules
[i
] + strlen(EAVESDROPPING_RULE
), &error
);
241 if (dbus_error_is_set(&error
)) {
242 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to add bus match: %s\n", error
.message
);
243 dbus_error_free(&error
);
244 dbus_cleanup(handle
);
254 dbus_create(const char *device
, char *ebuf
, int *is_ours
)
258 if (strcmp(device
, "dbus-system") &&
259 strcmp(device
, "dbus-session") &&
260 strncmp(device
, "dbus://", 7))
267 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_dbus
));
271 p
->activate_op
= dbus_activate
;
276 dbus_findalldevs(pcap_if_t
**alldevsp
, char *err_str
)
278 if (pcap_add_if(alldevsp
, "dbus-system", 0, "D-Bus system bus", err_str
) < 0)
280 if (pcap_add_if(alldevsp
, "dbus-session", 0, "D-Bus session bus", err_str
) < 0)