]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-dbus.c
40ace7f9f31f7d2de989849c390dae9cc76e6a19
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 dbus_read(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
48 DBusConnection
*conn
= handle
->md
.priv
;
50 struct pcap_pkthdr pkth
;
58 message
= dbus_connection_pop_message(conn
);
61 // XXX p->md.timeout = timeout_ms;
62 if (!dbus_connection_read_write(conn
, 100)) {
63 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Connection closed");
67 if (handle
->break_loop
) {
68 handle
->break_loop
= 0;
72 message
= dbus_connection_pop_message(conn
);
75 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected")) {
76 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Disconnected");
80 if (dbus_message_marshal(message
, &raw_msg
, &raw_msg_len
)) {
81 pkth
.caplen
= pkth
.len
= raw_msg_len
;
82 /* pkth.caplen = min (payload_len, handle->snapshot); */
84 gettimeofday(&pkth
.ts
, NULL
);
85 if (handle
->fcode
.bf_insns
== NULL
||
86 bpf_filter(handle
->fcode
.bf_insns
, (u_char
*)raw_msg
, pkth
.len
, pkth
.caplen
)) {
87 handle
->md
.packets_read
++;
88 callback(user
, &pkth
, (u_char
*)raw_msg
);
98 dbus_write(pcap_t
*handle
, const void *buf
, size_t size
)
100 /* XXX, not tested */
101 DBusConnection
*conn
= handle
->md
.priv
;
103 DBusError error
= DBUS_ERROR_INIT
;
106 if (!(msg
= dbus_message_demarshal(buf
, size
, &error
))) {
107 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dbus_message_demarshal() failed: %s", error
.message
);
108 dbus_error_free(&error
);
112 dbus_connection_send(conn
, msg
, NULL
);
113 dbus_connection_flush(conn
);
115 dbus_message_unref(msg
);
120 dbus_stats(pcap_t
*handle
, struct pcap_stat
*stats
)
122 stats
->ps_recv
= handle
->md
.packets_read
;
124 stats
->ps_ifdrop
= 0;
129 dbus_cleanup(pcap_t
*handle
)
131 DBusConnection
*conn
= handle
->md
.priv
;
133 handle
->md
.priv
= NULL
;
134 dbus_connection_unref(conn
);
136 pcap_cleanup_live_common(handle
);
140 dbus_activate(pcap_t
*handle
)
142 #define EAVESDROPPING_RULE "eavesdrop=true,"
144 static const char *rules
[] = {
145 EAVESDROPPING_RULE
"type='signal'",
146 EAVESDROPPING_RULE
"type='method_call'",
147 EAVESDROPPING_RULE
"type='method_return'",
148 EAVESDROPPING_RULE
"type='error'",
151 #define N_RULES sizeof(rules)/sizeof(rules[0])
153 const char *dev
= handle
->opt
.source
;
155 DBusError error
= DBUS_ERROR_INIT
;
156 DBusConnection
*conn
;
159 if (strcmp(dev
, "dbus-system") == 0) {
160 if (!(conn
= dbus_bus_get(DBUS_BUS_SYSTEM
, &error
))) {
161 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to get system bus: %s", error
.message
);
162 dbus_error_free(&error
);
166 } else if (strcmp(dev
, "dbus-session") == 0) {
167 if (!(conn
= dbus_bus_get(DBUS_BUS_SESSION
, &error
))) {
168 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to get session bus: %s", error
.message
);
169 dbus_error_free(&error
);
173 } else if (strncmp(dev
, "dbus://", 7) == 0) {
174 const char *addr
= dev
+ 7;
176 if (!(conn
= dbus_connection_open(addr
, &error
))) {
177 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to open connection to: %s: %s", addr
, error
.message
);
178 dbus_error_free(&error
);
182 if (!dbus_bus_register(conn
, &error
)) {
183 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to register bus %s: %s\n", addr
, error
.message
);
184 dbus_error_free(&error
);
189 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't get bus address from %s", handle
->opt
.source
);
193 /* Initialize some components of the pcap structure. */
196 handle
->linktype
= DLT_DBUS
;
197 handle
->read_op
= dbus_read
;
198 handle
->inject_op
= dbus_write
;
199 handle
->setfilter_op
= install_bpf_program
; /* XXX, later add support for dbus_bus_add_match() */
200 handle
->setdirection_op
= NULL
;
201 handle
->set_datalink_op
= NULL
; /* can't change data link type */
202 handle
->getnonblock_op
= pcap_getnonblock_fd
;
203 handle
->setnonblock_op
= pcap_setnonblock_fd
;
204 handle
->stats_op
= dbus_stats
;
206 handle
->selectable_fd
= handle
->fd
= -1;
207 handle
->md
.priv
= conn
;
209 if (handle
->opt
.rfmon
) {
211 * Monitor mode doesn't apply to dbus connections.
213 dbus_cleanup(handle
);
214 return PCAP_ERROR_RFMON_NOTSUP
;
217 /* dbus_connection_set_max_message_size(conn, handle->snapshot); */
218 if (handle
->opt
.buffer_size
!= 0)
219 dbus_connection_set_max_received_size(conn
, handle
->opt
.buffer_size
);
221 for (i
= 0; i
< N_RULES
; i
++) {
222 dbus_bus_add_match(conn
, rules
[i
], &error
);
223 if (dbus_error_is_set(&error
)) {
224 dbus_error_free(&error
);
226 /* try without eavesdrop */
227 dbus_bus_add_match(conn
, rules
[i
] + strlen(EAVESDROPPING_RULE
), &error
);
228 if (dbus_error_is_set(&error
)) {
229 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to add bus match: %s\n", error
.message
);
230 dbus_error_free(&error
);
231 dbus_cleanup(handle
);
241 dbus_create(const char *device
, char *ebuf
, int *is_ours
)
245 if (strcmp(device
, "dbus-system") &&
246 strcmp(device
, "dbus-session") &&
247 strncmp(device
, "dbus://", 7))
254 p
= pcap_create_common(device
, ebuf
);
258 p
->activate_op
= dbus_activate
;
263 dbus_findalldevs(pcap_if_t
**alldevsp
, char *err_str
)
265 pcap_if_t
*found_dev
= *alldevsp
;
267 if (pcap_add_if(&found_dev
, "dbus-system", 0, "D-BUS system bus", err_str
) < 0)
269 if (pcap_add_if(&found_dev
, "dbus-session", 0, "D-BUS session bus", err_str
) < 0)