]> The Tcpdump Group git mirrors - libpcap/blob - pcap-dbus.c
ea3a390ff55c0ab8f65d7dbc0e72e6e198cefa7f
[libpcap] / pcap-dbus.c
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 pcap_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 pcap_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 pcap_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.device;
164
165 DBusError error = DBUS_ERROR_INIT;
166 u_int i;
167
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;
173 }
174
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;
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 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;
189 }
190
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;
195 }
196
197 } else {
198 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't get bus address from %s", handle->opt.device);
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 handle->cleanup_op = dbus_cleanup;
215
216 handle->selectable_fd = handle->fd = -1;
217
218 if (handle->opt.rfmon) {
219 /*
220 * Monitor mode doesn't apply to dbus connections.
221 */
222 dbus_cleanup(handle);
223 return PCAP_ERROR_RFMON_NOTSUP;
224 }
225
226 /* dbus_connection_set_max_message_size(handlep->conn, handle->snapshot); */
227 if (handle->opt.buffer_size != 0)
228 dbus_connection_set_max_received_size(handlep->conn, handle->opt.buffer_size);
229
230 for (i = 0; i < N_RULES; i++) {
231 dbus_bus_add_match(handlep->conn, rules[i], &error);
232 if (dbus_error_is_set(&error)) {
233 dbus_error_free(&error);
234
235 /* try without eavesdrop */
236 dbus_bus_add_match(handlep->conn, rules[i] + strlen(EAVESDROPPING_RULE), &error);
237 if (dbus_error_is_set(&error)) {
238 pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to add bus match: %s\n", error.message);
239 dbus_error_free(&error);
240 dbus_cleanup(handle);
241 return PCAP_ERROR;
242 }
243 }
244 }
245
246 return 0;
247 }
248
249 pcap_t *
250 dbus_create(const char *device, char *ebuf, int *is_ours)
251 {
252 pcap_t *p;
253
254 if (strcmp(device, "dbus-system") &&
255 strcmp(device, "dbus-session") &&
256 strncmp(device, "dbus://", 7))
257 {
258 *is_ours = 0;
259 return NULL;
260 }
261
262 *is_ours = 1;
263 p = pcap_create_common(ebuf, sizeof (struct pcap_dbus));
264 if (p == NULL)
265 return (NULL);
266
267 p->activate_op = dbus_activate;
268 return (p);
269 }
270
271 int
272 dbus_findalldevs(pcap_if_list_t *devlistp, char *err_str)
273 {
274 if (add_dev(devlistp, "dbus-system", 0, "D-Bus system bus", err_str) == NULL)
275 return -1;
276 if (add_dev(devlistp, "dbus-session", 0, "D-Bus session bus", err_str) == NULL)
277 return -1;
278 return 0;
279 }
280