]>
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.
42 #include <dbus/dbus.h>
45 dbus_read(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
47 DBusConnection
*conn
= handle
->md
.priv
;
49 struct pcap_pkthdr pkth
;
57 message
= dbus_connection_pop_message(conn
);
60 // XXX p->md.timeout = timeout_ms;
61 if (!dbus_connection_read_write(conn
, 100)) {
62 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Connection closed");
66 if (handle
->break_loop
) {
67 handle
->break_loop
= 0;
71 message
= dbus_connection_pop_message(conn
);
74 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected")) {
75 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Disconnected");
79 if (dbus_message_marshal(message
, &raw_msg
, &raw_msg_len
)) {
80 pkth
.caplen
= pkth
.len
= raw_msg_len
;
81 /* pkth.caplen = min (payload_len, handle->snapshot); */
83 gettimeofday(&pkth
.ts
, NULL
);
84 if (handle
->fcode
.bf_insns
== NULL
|| bpf_filter(handle
->fcode
.bf_insns
, raw_msg
, pkth
.len
, pkth
.caplen
)) {
85 handle
->md
.packets_read
++;
86 callback(user
, &pkth
, raw_msg
);
96 dbus_write(pcap_t
*handle
, const void *buf
, size_t size
)
99 DBusConnection
*conn
= handle
->md
.priv
;
101 DBusError error
= DBUS_ERROR_INIT
;
104 if (!(msg
= dbus_message_demarshal(buf
, size
, &error
))) {
105 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "dbus_message_demarshal() failed: %s", error
.message
);
106 dbus_error_free(&error
);
110 dbus_connection_send(conn
, msg
, NULL
);
111 dbus_connection_flush(conn
);
113 dbus_message_unref(msg
);
118 dbus_stats(pcap_t
*handle
, struct pcap_stat
*stats
)
120 stats
->ps_recv
= handle
->md
.packets_read
;
122 stats
->ps_ifdrop
= 0;
127 dbus_cleanup(pcap_t
*handle
)
129 DBusConnection
*conn
= handle
->md
.priv
;
131 handle
->md
.priv
= NULL
;
132 dbus_connection_unref(conn
);
134 pcap_cleanup_live_common(handle
);
138 dbus_activate(pcap_t
*handle
)
140 #define EAVESDROPPING_RULE "eavesdrop=true,"
142 static const char *rules
[] = {
143 EAVESDROPPING_RULE
"type='signal'",
144 EAVESDROPPING_RULE
"type='method_call'",
145 EAVESDROPPING_RULE
"type='method_return'",
146 EAVESDROPPING_RULE
"type='error'",
149 #define N_RULES sizeof(rules)/sizeof(rules[0])
151 const char *dev
= handle
->opt
.source
;
153 DBusError error
= DBUS_ERROR_INIT
;
154 DBusConnection
*conn
;
157 if (strcmp(dev
, "dbus-system") == 0) {
158 if (!(conn
= dbus_bus_get(DBUS_BUS_SYSTEM
, &error
))) {
159 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to get system bus: %s", error
.message
);
160 dbus_error_free(&error
);
164 } else if (strcmp(dev
, "dbus-session") == 0) {
165 if (!(conn
= dbus_bus_get(DBUS_BUS_SESSION
, &error
))) {
166 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to get session bus: %s", error
.message
);
167 dbus_error_free(&error
);
171 } else if (strncmp(dev
, "dbus://", 7) == 0) {
172 const char *addr
= dev
+ 7;
174 if (!(conn
= dbus_connection_open(addr
, &error
))) {
175 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to open connection to: %s: %s", addr
, error
.message
);
176 dbus_error_free(&error
);
180 if (!dbus_bus_register(conn
, &error
)) {
181 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to register bus %s: %s\n", addr
, error
.message
);
182 dbus_error_free(&error
);
187 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't get bus address from %s", handle
->opt
.source
);
191 /* Initialize some components of the pcap structure. */
194 handle
->linktype
= DLT_DBUS
;
195 handle
->read_op
= dbus_read
;
196 handle
->inject_op
= dbus_write
;
197 handle
->setfilter_op
= install_bpf_program
; /* XXX, later add support for dbus_bus_add_match() */
198 handle
->setdirection_op
= NULL
;
199 handle
->set_datalink_op
= NULL
; /* can't change data link type */
200 handle
->getnonblock_op
= pcap_getnonblock_fd
;
201 handle
->setnonblock_op
= pcap_setnonblock_fd
;
202 handle
->stats_op
= dbus_stats
;
204 handle
->selectable_fd
= handle
->fd
= -1;
205 handle
->md
.priv
= conn
;
207 if (handle
->opt
.rfmon
) {
209 * Monitor mode doesn't apply to dbus connections.
211 dbus_cleanup(handle
);
212 return PCAP_ERROR_RFMON_NOTSUP
;
215 /* dbus_connection_set_max_message_size(conn, handle->snapshot); */
216 if (handle
->opt
.buffer_size
!= 0)
217 dbus_connection_set_max_received_size(conn
, handle
->opt
.buffer_size
);
219 for (i
= 0; i
< N_RULES
; i
++) {
220 dbus_bus_add_match(conn
, rules
[i
], &error
);
221 if (dbus_error_is_set(&error
)) {
222 dbus_error_free(&error
);
224 /* try without eavesdrop */
225 dbus_bus_add_match(conn
, rules
[i
] + strlen(EAVESDROPPING_RULE
), &error
);
226 if (dbus_error_is_set(&error
)) {
227 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Failed to add bus match: %s\n", error
.message
);
228 dbus_error_free(&error
);
229 dbus_cleanup(handle
);
239 dbus_create(const char *device
, char *ebuf
, int *is_ours
)
243 if (strcmp(device
, "dbus-system") &&
244 strcmp(device
, "dbus-session") &&
245 strncmp(device
, "dbus://", 7))
252 p
= pcap_create_common(device
, ebuf
);
256 p
->activate_op
= dbus_activate
;
261 dbus_findalldevs(pcap_if_t
**alldevsp
, char *err_str
)
263 pcap_if_t
*found_dev
= *alldevsp
;
265 if (pcap_add_if(&found_dev
, "dbus-system", 0, "D-BUS system bus", err_str
) < 0)
267 if (pcap_add_if(&found_dev
, "dbus-session", 0, "D-BUS session bus", err_str
) < 0)