Home | History | Annotate | Download | only in libpcap
      1 /*
      2  * Copyright (c) 2012 Jakub Zawadzki
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *
      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.
     17  *
     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.
     29  */
     30 
     31 #ifdef HAVE_CONFIG_H
     32 #include "config.h"
     33 #endif
     34 
     35 #include <string.h>
     36 
     37 #include <time.h>
     38 #include <sys/time.h>
     39 
     40 #include <dbus/dbus.h>
     41 
     42 #include "pcap-int.h"
     43 #include "pcap-dbus.h"
     44 
     45 /*
     46  * Private data for capturing on D-Bus.
     47  */
     48 struct pcap_dbus {
     49 	DBusConnection *conn;
     50 	u_int	packets_read;	/* count of packets read */
     51 };
     52 
     53 static int
     54 dbus_read(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
     55 {
     56 	struct pcap_dbus *handlep = handle->priv;
     57 
     58 	struct pcap_pkthdr pkth;
     59 	DBusMessage *message;
     60 
     61 	char *raw_msg;
     62 	int raw_msg_len;
     63 
     64 	int count = 0;
     65 
     66 	message = dbus_connection_pop_message(handlep->conn);
     67 
     68 	while (!message) {
     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");
     72 			return -1;
     73 		}
     74 
     75 		if (handle->break_loop) {
     76 			handle->break_loop = 0;
     77 			return -2;
     78 		}
     79 
     80 		message = dbus_connection_pop_message(handlep->conn);
     81 	}
     82 
     83 	if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
     84 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Disconnected");
     85 		return -1;
     86 	}
     87 
     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); */
     91 
     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++;
     98 		}
     99 
    100 		dbus_free(raw_msg);
    101 	}
    102 	return count;
    103 }
    104 
    105 static int
    106 dbus_write(pcap_t *handle, const void *buf, size_t size)
    107 {
    108 	/* XXX, not tested */
    109 	struct pcap_dbus *handlep = handle->priv;
    110 
    111 	DBusError error = DBUS_ERROR_INIT;
    112 	DBusMessage *msg;
    113 
    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);
    117 		return -1;
    118 	}
    119 
    120 	dbus_connection_send(handlep->conn, msg, NULL);
    121 	dbus_connection_flush(handlep->conn);
    122 
    123 	dbus_message_unref(msg);
    124 	return 0;
    125 }
    126 
    127 static int
    128 dbus_stats(pcap_t *handle, struct pcap_stat *stats)
    129 {
    130 	struct pcap_dbus *handlep = handle->priv;
    131 
    132 	stats->ps_recv = handlep->packets_read;
    133 	stats->ps_drop = 0;
    134 	stats->ps_ifdrop = 0;
    135 	return 0;
    136 }
    137 
    138 static void
    139 dbus_cleanup(pcap_t *handle)
    140 {
    141 	struct pcap_dbus *handlep = handle->priv;
    142 
    143 	dbus_connection_unref(handlep->conn);
    144 
    145 	pcap_cleanup_live_common(handle);
    146 }
    147 
    148 static int
    149 dbus_activate(pcap_t *handle)
    150 {
    151 #define EAVESDROPPING_RULE "eavesdrop=true,"
    152 
    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'",
    158 	};
    159 
    160 	#define N_RULES sizeof(rules)/sizeof(rules[0])
    161 
    162 	struct pcap_dbus *handlep = handle->priv;
    163 	const char *dev = handle->opt.source;
    164 
    165 	DBusError error = DBUS_ERROR_INIT;
    166 	int i;
    167 
    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);
    172 			return PCAP_ERROR;
    173 		}
    174 
    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);
    179 			return PCAP_ERROR;
    180 		}
    181 
    182 	} else if (strncmp(dev, "dbus://", 7) == 0) {
    183 		const char *addr = dev + 7;
    184 
    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);
    188 			return PCAP_ERROR;
    189 		}
    190 
    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);
    194 			return PCAP_ERROR;
    195 		}
    196 
    197 	} else {
    198 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't get bus address from %s", handle->opt.source);
    199 		return PCAP_ERROR;
    200 	}
    201 
    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;
    214 
    215 	handle->selectable_fd = handle->fd = -1;
    216 
    217 	if (handle->opt.rfmon) {
    218 		/*
    219 		 * Monitor mode doesn't apply to dbus connections.
    220 		 */
    221 		dbus_cleanup(handle);
    222 		return PCAP_ERROR_RFMON_NOTSUP;
    223 	}
    224 
    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);
    228 
    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);
    233 
    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);
    240 				return PCAP_ERROR;
    241 			}
    242 		}
    243 	}
    244 
    245 	return 0;
    246 }
    247 
    248 pcap_t *
    249 dbus_create(const char *device, char *ebuf, int *is_ours)
    250 {
    251 	pcap_t *p;
    252 
    253 	if (strcmp(device, "dbus-system") &&
    254 		strcmp(device, "dbus-session") &&
    255 		strncmp(device, "dbus://", 7))
    256 	{
    257 		*is_ours = 0;
    258 		return NULL;
    259 	}
    260 
    261 	*is_ours = 1;
    262 	p = pcap_create_common(device, ebuf, sizeof (struct pcap_dbus));
    263 	if (p == NULL)
    264 		return (NULL);
    265 
    266 	p->activate_op = dbus_activate;
    267 	return (p);
    268 }
    269 
    270 int
    271 dbus_findalldevs(pcap_if_t **alldevsp, char *err_str)
    272 {
    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;
    278 }
    279 
    280