stat: display 'D' mode for doors
[unleashed.git] / contrib / libpcap / pcap-dbus.c
blob8e92093da11070a88c078353061fc8cebad4e15d
1 /*
2 * Copyright (c) 2012 Jakub Zawadzki
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
16 * permission.
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.
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
35 #include <string.h>
37 #include <time.h>
38 #include <sys/time.h>
40 #include <dbus/dbus.h>
42 #include "pcap-int.h"
43 #include "pcap-dbus.h"
46 * Private data for capturing on D-Bus.
48 struct pcap_dbus {
49 DBusConnection *conn;
50 u_int packets_read; /* count of packets read */
53 static int
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;
59 DBusMessage *message;
61 char *raw_msg;
62 int raw_msg_len;
64 int count = 0;
66 message = dbus_connection_pop_message(handlep->conn);
68 while (!message) {
69 /* XXX handle->opt.timeout = timeout_ms; */
70 if (!dbus_connection_read_write(handlep->conn, 100)) {
71 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Connection closed");
72 return -1;
75 if (handle->break_loop) {
76 handle->break_loop = 0;
77 return -2;
80 message = dbus_connection_pop_message(handlep->conn);
83 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
84 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Disconnected");
85 return -1;
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);
97 count++;
100 dbus_free(raw_msg);
102 return count;
105 static int
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;
112 DBusMessage *msg;
114 if (!(msg = dbus_message_demarshal(buf, size, &error))) {
115 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dbus_message_demarshal() failed: %s", error.message);
116 dbus_error_free(&error);
117 return -1;
120 dbus_connection_send(handlep->conn, msg, NULL);
121 dbus_connection_flush(handlep->conn);
123 dbus_message_unref(msg);
124 return 0;
127 static int
128 dbus_stats(pcap_t *handle, struct pcap_stat *stats)
130 struct pcap_dbus *handlep = handle->priv;
132 stats->ps_recv = handlep->packets_read;
133 stats->ps_drop = 0;
134 stats->ps_ifdrop = 0;
135 return 0;
138 static void
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);
148 static int
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.device;
165 DBusError error = DBUS_ERROR_INIT;
166 u_int i;
168 if (strcmp(dev, "dbus-system") == 0) {
169 if (!(handlep->conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) {
170 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to get system bus: %s", error.message);
171 dbus_error_free(&error);
172 return PCAP_ERROR;
175 } else if (strcmp(dev, "dbus-session") == 0) {
176 if (!(handlep->conn = dbus_bus_get(DBUS_BUS_SESSION, &error))) {
177 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to get session bus: %s", error.message);
178 dbus_error_free(&error);
179 return PCAP_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 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to open connection to: %s: %s", addr, error.message);
187 dbus_error_free(&error);
188 return PCAP_ERROR;
191 if (!dbus_bus_register(handlep->conn, &error)) {
192 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to register bus %s: %s\n", addr, error.message);
193 dbus_error_free(&error);
194 return PCAP_ERROR;
197 } else {
198 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't get bus address from %s", handle->opt.device);
199 return PCAP_ERROR;
202 /* Initialize some components of the pcap structure. */
203 handle->bufsize = 0;
204 handle->offset = 0;
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 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to add bus match: %s\n", error.message);
238 dbus_error_free(&error);
239 dbus_cleanup(handle);
240 return PCAP_ERROR;
245 return 0;
248 pcap_t *
249 dbus_create(const char *device, char *ebuf, int *is_ours)
251 pcap_t *p;
253 if (strcmp(device, "dbus-system") &&
254 strcmp(device, "dbus-session") &&
255 strncmp(device, "dbus://", 7))
257 *is_ours = 0;
258 return NULL;
261 *is_ours = 1;
262 p = pcap_create_common(ebuf, sizeof (struct pcap_dbus));
263 if (p == NULL)
264 return (NULL);
266 p->activate_op = dbus_activate;
267 return (p);
271 dbus_findalldevs(pcap_if_t **alldevsp, char *err_str)
273 if (pcap_add_if(alldevsp, "dbus-system", 0, "D-Bus system bus", err_str) < 0)
274 return -1;
275 if (pcap_add_if(alldevsp, "dbus-session", 0, "D-Bus session bus", err_str) < 0)
276 return -1;
277 return 0;