]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-dbus.c
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
, 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 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 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 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 dbus_activate(pcap_t
*handle
)
151 #define EAVESDROPPING_RULE "eavesdrop=true,"
153 static const char *rules
[] = {
154 EAVESDROPPING_RULE
"type='signal'",
155 EAVESDROPPING_RULE
"type='method_call'",
156 EAVESDROPPING_RULE
"type='method_return'",
157 EAVESDROPPING_RULE
"type='error'",
160 #define N_RULES sizeof(rules)/sizeof(rules[0])
162 struct pcap_dbus
*handlep
= handle
->priv
;
163 const char *dev
= handle
->opt
.source
;
165 DBusError error
= DBUS_ERROR_INIT
;
168 if (strcmp(dev
, "dbus-system") == 0) {
169 if (!(handlep
->conn
= dbus_bus_get(DBUS_BUS_SYSTEM
, &error
))) {
170 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to get system bus: %s", error
.message
);
171 dbus_error_free(&error
);
175 } else if (strcmp(dev
, "dbus-session") == 0) {
176 if (!(handlep
->conn
= dbus_bus_get(DBUS_BUS_SESSION
, &error
))) {
177 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to get session bus: %s", error
.message
);
178 dbus_error_free(&error
);
182 } else if (strncmp(dev
, "dbus://", 7) == 0) {
183 const char *addr
= dev
+ 7;
185 if (!(handlep
->conn
= dbus_connection_open(addr
, &error
))) {
186 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to open connection to: %s: %s", addr
, error
.message
);
187 dbus_error_free(&error
);
191 if (!dbus_bus_register(handlep
->conn
, &error
)) {
192 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to register bus %s: %s\n", addr
, error
.message
);
193 dbus_error_free(&error
);
198 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't get bus address from %s", handle
->opt
.source
);
202 /* Initialize some components of the pcap structure. */
205 handle
->linktype
= DLT_DBUS
;
206 handle
->read_op
= dbus_read
;
207 handle
->inject_op
= dbus_write
;
208 handle
->setfilter_op
= install_bpf_program
; /* XXX, later add support for dbus_bus_add_match() */
209 handle
->setdirection_op
= NULL
;
210 handle
->set_datalink_op
= NULL
; /* can't change data link type */
211 handle
->getnonblock_op
= pcap_getnonblock_fd
;
212 handle
->setnonblock_op
= pcap_setnonblock_fd
;
213 handle
->stats_op
= dbus_stats
;
215 handle
->selectable_fd
= handle
->fd
= -1;
217 if (handle
->opt
.rfmon
) {
219 * Monitor mode doesn't apply to dbus connections.
221 dbus_cleanup(handle
);
222 return PCAP_ERROR_RFMON_NOTSUP
;
225 /* dbus_connection_set_max_message_size(handlep->conn, handle->snapshot); */
226 if (handle
->opt
.buffer_size
!= 0)
227 dbus_connection_set_max_received_size(handlep
->conn
, handle
->opt
.buffer_size
);
229 for (i
= 0; i
< N_RULES
; i
++) {
230 dbus_bus_add_match(handlep
->conn
, rules
[i
], &error
);
231 if (dbus_error_is_set(&error
)) {
232 dbus_error_free(&error
);
234 /* try without eavesdrop */
235 dbus_bus_add_match(handlep
->conn
, rules
[i
] + strlen(EAVESDROPPING_RULE
), &error
);
236 if (dbus_error_is_set(&error
)) {
237 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to add bus match: %s\n", error
.message
);
238 dbus_error_free(&error
);
239 dbus_cleanup(handle
);
249 dbus_create(const char *device
, char *ebuf
, int *is_ours
)
253 if (strcmp(device
, "dbus-system") &&
254 strcmp(device
, "dbus-session") &&
255 strncmp(device
, "dbus://", 7))
262 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_dbus
));
266 p
->activate_op
= dbus_activate
;
271 dbus_findalldevs(pcap_if_t
**alldevsp
, char *err_str
)
273 pcap_if_t
*found_dev
= *alldevsp
;
275 if (pcap_add_if(&found_dev
, "dbus-system", 0, "D-Bus system bus", err_str
) < 0)
277 if (pcap_add_if(&found_dev
, "dbus-session", 0, "D-Bus session bus", err_str
) < 0)