xref: /netbsd/external/bsd/libpcap/dist/pcap-dbus.c (revision c15d5eff)
1 /*	$NetBSD: pcap-dbus.c,v 1.1.1.1 2013/12/31 16:57:18 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2012 Jakub Zawadzki
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote
17  * products derived from this software without specific prior written
18  * permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 
37 #include <string.h>
38 
39 #include <time.h>
40 #include <sys/time.h>
41 
42 #include <dbus/dbus.h>
43 
44 #include "pcap-int.h"
45 #include "pcap-dbus.h"
46 
47 /*
48  * Private data for capturing on D-Bus.
49  */
50 struct pcap_dbus {
51 	DBusConnection *conn;
52 	u_int	packets_read;	/* count of packets read */
53 };
54 
55 static int
56 dbus_read(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
57 {
58 	struct pcap_dbus *handlep = handle->priv;
59 
60 	struct pcap_pkthdr pkth;
61 	DBusMessage *message;
62 
63 	char *raw_msg;
64 	int raw_msg_len;
65 
66 	int count = 0;
67 
68 	message = dbus_connection_pop_message(handlep->conn);
69 
70 	while (!message) {
71 		// XXX handle->opt.timeout = timeout_ms;
72 		if (!dbus_connection_read_write(handlep->conn, 100)) {
73 			snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Connection closed");
74 			return -1;
75 		}
76 
77 		if (handle->break_loop) {
78 			handle->break_loop = 0;
79 			return -2;
80 		}
81 
82 		message = dbus_connection_pop_message(handlep->conn);
83 	}
84 
85 	if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
86 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Disconnected");
87 		return -1;
88 	}
89 
90 	if (dbus_message_marshal(message, &raw_msg, &raw_msg_len)) {
91 		pkth.caplen = pkth.len = raw_msg_len;
92 		/* pkth.caplen = min (payload_len, handle->snapshot); */
93 
94 		gettimeofday(&pkth.ts, NULL);
95 		if (handle->fcode.bf_insns == NULL ||
96 		    bpf_filter(handle->fcode.bf_insns, (u_char *)raw_msg, pkth.len, pkth.caplen)) {
97 			handlep->packets_read++;
98 			callback(user, &pkth, (u_char *)raw_msg);
99 			count++;
100 		}
101 
102 		dbus_free(raw_msg);
103 	}
104 	return count;
105 }
106 
107 static int
108 dbus_write(pcap_t *handle, const void *buf, size_t size)
109 {
110 	/* XXX, not tested */
111 	struct pcap_dbus *handlep = handle->priv;
112 
113 	DBusError error = DBUS_ERROR_INIT;
114 	DBusMessage *msg;
115 
116 	if (!(msg = dbus_message_demarshal(buf, size, &error))) {
117 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dbus_message_demarshal() failed: %s", error.message);
118 		dbus_error_free(&error);
119 		return -1;
120 	}
121 
122 	dbus_connection_send(handlep->conn, msg, NULL);
123 	dbus_connection_flush(handlep->conn);
124 
125 	dbus_message_unref(msg);
126 	return 0;
127 }
128 
129 static int
130 dbus_stats(pcap_t *handle, struct pcap_stat *stats)
131 {
132 	struct pcap_dbus *handlep = handle->priv;
133 
134 	stats->ps_recv = handlep->packets_read;
135 	stats->ps_drop = 0;
136 	stats->ps_ifdrop = 0;
137 	return 0;
138 }
139 
140 static void
141 dbus_cleanup(pcap_t *handle)
142 {
143 	struct pcap_dbus *handlep = handle->priv;
144 
145 	dbus_connection_unref(handlep->conn);
146 
147 	pcap_cleanup_live_common(handle);
148 }
149 
150 static int
151 dbus_activate(pcap_t *handle)
152 {
153 #define EAVESDROPPING_RULE "eavesdrop=true,"
154 
155 	static const char *rules[] = {
156 		EAVESDROPPING_RULE "type='signal'",
157 		EAVESDROPPING_RULE "type='method_call'",
158 		EAVESDROPPING_RULE "type='method_return'",
159 		EAVESDROPPING_RULE "type='error'",
160 	};
161 
162 	#define N_RULES sizeof(rules)/sizeof(rules[0])
163 
164 	struct pcap_dbus *handlep = handle->priv;
165 	const char *dev = handle->opt.source;
166 
167 	DBusError error = DBUS_ERROR_INIT;
168 	int i;
169 
170 	if (strcmp(dev, "dbus-system") == 0) {
171 		if (!(handlep->conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) {
172 			snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to get system bus: %s", error.message);
173 			dbus_error_free(&error);
174 			return PCAP_ERROR;
175 		}
176 
177 	} else if (strcmp(dev, "dbus-session") == 0) {
178 		if (!(handlep->conn = dbus_bus_get(DBUS_BUS_SESSION, &error))) {
179 			snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to get session bus: %s", error.message);
180 			dbus_error_free(&error);
181 			return PCAP_ERROR;
182 		}
183 
184 	} else if (strncmp(dev, "dbus://", 7) == 0) {
185 		const char *addr = dev + 7;
186 
187 		if (!(handlep->conn = dbus_connection_open(addr, &error))) {
188 			snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to open connection to: %s: %s", addr, error.message);
189 			dbus_error_free(&error);
190 			return PCAP_ERROR;
191 		}
192 
193 		if (!dbus_bus_register(handlep->conn, &error)) {
194 			snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to register bus %s: %s\n", addr, error.message);
195 			dbus_error_free(&error);
196 			return PCAP_ERROR;
197 		}
198 
199 	} else {
200 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't get bus address from %s", handle->opt.source);
201 		return PCAP_ERROR;
202 	}
203 
204 	/* Initialize some components of the pcap structure. */
205 	handle->bufsize = 0;
206 	handle->offset = 0;
207 	handle->linktype = DLT_DBUS;
208 	handle->read_op = dbus_read;
209 	handle->inject_op = dbus_write;
210 	handle->setfilter_op = install_bpf_program; /* XXX, later add support for dbus_bus_add_match() */
211 	handle->setdirection_op = NULL;
212 	handle->set_datalink_op = NULL;      /* can't change data link type */
213 	handle->getnonblock_op = pcap_getnonblock_fd;
214 	handle->setnonblock_op = pcap_setnonblock_fd;
215 	handle->stats_op = dbus_stats;
216 
217 	handle->selectable_fd = handle->fd = -1;
218 
219 	if (handle->opt.rfmon) {
220 		/*
221 		 * Monitor mode doesn't apply to dbus connections.
222 		 */
223 		dbus_cleanup(handle);
224 		return PCAP_ERROR_RFMON_NOTSUP;
225 	}
226 
227 	/* dbus_connection_set_max_message_size(handlep->conn, handle->snapshot); */
228 	if (handle->opt.buffer_size != 0)
229 		dbus_connection_set_max_received_size(handlep->conn, handle->opt.buffer_size);
230 
231 	for (i = 0; i < N_RULES; i++) {
232 		dbus_bus_add_match(handlep->conn, rules[i], &error);
233 		if (dbus_error_is_set(&error)) {
234 			dbus_error_free(&error);
235 
236 			/* try without eavesdrop */
237 			dbus_bus_add_match(handlep->conn, rules[i] + strlen(EAVESDROPPING_RULE), &error);
238 			if (dbus_error_is_set(&error)) {
239 				snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to add bus match: %s\n", error.message);
240 				dbus_error_free(&error);
241 				dbus_cleanup(handle);
242 				return PCAP_ERROR;
243 			}
244 		}
245 	}
246 
247 	return 0;
248 }
249 
250 pcap_t *
251 dbus_create(const char *device, char *ebuf, int *is_ours)
252 {
253 	pcap_t *p;
254 
255 	if (strcmp(device, "dbus-system") &&
256 		strcmp(device, "dbus-session") &&
257 		strncmp(device, "dbus://", 7))
258 	{
259 		*is_ours = 0;
260 		return NULL;
261 	}
262 
263 	*is_ours = 1;
264 	p = pcap_create_common(device, ebuf, sizeof (struct pcap_dbus));
265 	if (p == NULL)
266 		return (NULL);
267 
268 	p->activate_op = dbus_activate;
269 	return (p);
270 }
271 
272 int
273 dbus_findalldevs(pcap_if_t **alldevsp, char *err_str)
274 {
275 	if (pcap_add_if(alldevsp, "dbus-system", 0, "D-Bus system bus", err_str) < 0)
276 		return -1;
277 	if (pcap_add_if(alldevsp, "dbus-session", 0, "D-Bus session bus", err_str) < 0)
278 		return -1;
279 	return 0;
280 }
281 
282