]> The Tcpdump Group git mirrors - libpcap/blob - pcap-dbus.c
40ace7f9f31f7d2de989849c390dae9cc76e6a19
[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 static int
46 dbus_read(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
47 {
48 DBusConnection *conn = handle->md.priv;
49
50 struct pcap_pkthdr pkth;
51 DBusMessage *message;
52
53 char *raw_msg;
54 int raw_msg_len;
55
56 int count = 0;
57
58 message = dbus_connection_pop_message(conn);
59
60 while (!message) {
61 // XXX p->md.timeout = timeout_ms;
62 if (!dbus_connection_read_write(conn, 100)) {
63 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Connection closed");
64 return -1;
65 }
66
67 if (handle->break_loop) {
68 handle->break_loop = 0;
69 return -2;
70 }
71
72 message = dbus_connection_pop_message(conn);
73 }
74
75 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
76 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Disconnected");
77 return -1;
78 }
79
80 if (dbus_message_marshal(message, &raw_msg, &raw_msg_len)) {
81 pkth.caplen = pkth.len = raw_msg_len;
82 /* pkth.caplen = min (payload_len, handle->snapshot); */
83
84 gettimeofday(&pkth.ts, NULL);
85 if (handle->fcode.bf_insns == NULL ||
86 bpf_filter(handle->fcode.bf_insns, (u_char *)raw_msg, pkth.len, pkth.caplen)) {
87 handle->md.packets_read++;
88 callback(user, &pkth, (u_char *)raw_msg);
89 count++;
90 }
91
92 dbus_free(raw_msg);
93 }
94 return count;
95 }
96
97 static int
98 dbus_write(pcap_t *handle, const void *buf, size_t size)
99 {
100 /* XXX, not tested */
101 DBusConnection *conn = handle->md.priv;
102
103 DBusError error = DBUS_ERROR_INIT;
104 DBusMessage *msg;
105
106 if (!(msg = dbus_message_demarshal(buf, size, &error))) {
107 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dbus_message_demarshal() failed: %s", error.message);
108 dbus_error_free(&error);
109 return -1;
110 }
111
112 dbus_connection_send(conn, msg, NULL);
113 dbus_connection_flush(conn);
114
115 dbus_message_unref(msg);
116 return 0;
117 }
118
119 static int
120 dbus_stats(pcap_t *handle, struct pcap_stat *stats)
121 {
122 stats->ps_recv = handle->md.packets_read;
123 stats->ps_drop = 0;
124 stats->ps_ifdrop = 0;
125 return 0;
126 }
127
128 static void
129 dbus_cleanup(pcap_t *handle)
130 {
131 DBusConnection *conn = handle->md.priv;
132
133 handle->md.priv = NULL;
134 dbus_connection_unref(conn);
135
136 pcap_cleanup_live_common(handle);
137 }
138
139 static int
140 dbus_activate(pcap_t *handle)
141 {
142 #define EAVESDROPPING_RULE "eavesdrop=true,"
143
144 static const char *rules[] = {
145 EAVESDROPPING_RULE "type='signal'",
146 EAVESDROPPING_RULE "type='method_call'",
147 EAVESDROPPING_RULE "type='method_return'",
148 EAVESDROPPING_RULE "type='error'",
149 };
150
151 #define N_RULES sizeof(rules)/sizeof(rules[0])
152
153 const char *dev = handle->opt.source;
154
155 DBusError error = DBUS_ERROR_INIT;
156 DBusConnection *conn;
157 int i;
158
159 if (strcmp(dev, "dbus-system") == 0) {
160 if (!(conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) {
161 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to get system bus: %s", error.message);
162 dbus_error_free(&error);
163 return PCAP_ERROR;
164 }
165
166 } else if (strcmp(dev, "dbus-session") == 0) {
167 if (!(conn = dbus_bus_get(DBUS_BUS_SESSION, &error))) {
168 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to get session bus: %s", error.message);
169 dbus_error_free(&error);
170 return PCAP_ERROR;
171 }
172
173 } else if (strncmp(dev, "dbus://", 7) == 0) {
174 const char *addr = dev + 7;
175
176 if (!(conn = dbus_connection_open(addr, &error))) {
177 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to open connection to: %s: %s", addr, error.message);
178 dbus_error_free(&error);
179 return PCAP_ERROR;
180 }
181
182 if (!dbus_bus_register(conn, &error)) {
183 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to register bus %s: %s\n", addr, error.message);
184 dbus_error_free(&error);
185 return PCAP_ERROR;
186 }
187
188 } else {
189 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't get bus address from %s", handle->opt.source);
190 return PCAP_ERROR;
191 }
192
193 /* Initialize some components of the pcap structure. */
194 handle->bufsize = 0;
195 handle->offset = 0;
196 handle->linktype = DLT_DBUS;
197 handle->read_op = dbus_read;
198 handle->inject_op = dbus_write;
199 handle->setfilter_op = install_bpf_program; /* XXX, later add support for dbus_bus_add_match() */
200 handle->setdirection_op = NULL;
201 handle->set_datalink_op = NULL; /* can't change data link type */
202 handle->getnonblock_op = pcap_getnonblock_fd;
203 handle->setnonblock_op = pcap_setnonblock_fd;
204 handle->stats_op = dbus_stats;
205
206 handle->selectable_fd = handle->fd = -1;
207 handle->md.priv = conn;
208
209 if (handle->opt.rfmon) {
210 /*
211 * Monitor mode doesn't apply to dbus connections.
212 */
213 dbus_cleanup(handle);
214 return PCAP_ERROR_RFMON_NOTSUP;
215 }
216
217 /* dbus_connection_set_max_message_size(conn, handle->snapshot); */
218 if (handle->opt.buffer_size != 0)
219 dbus_connection_set_max_received_size(conn, handle->opt.buffer_size);
220
221 for (i = 0; i < N_RULES; i++) {
222 dbus_bus_add_match(conn, rules[i], &error);
223 if (dbus_error_is_set(&error)) {
224 dbus_error_free(&error);
225
226 /* try without eavesdrop */
227 dbus_bus_add_match(conn, rules[i] + strlen(EAVESDROPPING_RULE), &error);
228 if (dbus_error_is_set(&error)) {
229 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to add bus match: %s\n", error.message);
230 dbus_error_free(&error);
231 dbus_cleanup(handle);
232 return PCAP_ERROR;
233 }
234 }
235 }
236
237 return 0;
238 }
239
240 pcap_t *
241 dbus_create(const char *device, char *ebuf, int *is_ours)
242 {
243 pcap_t *p;
244
245 if (strcmp(device, "dbus-system") &&
246 strcmp(device, "dbus-session") &&
247 strncmp(device, "dbus://", 7))
248 {
249 *is_ours = 0;
250 return NULL;
251 }
252
253 *is_ours = 1;
254 p = pcap_create_common(device, ebuf);
255 if (p == NULL)
256 return (NULL);
257
258 p->activate_op = dbus_activate;
259 return (p);
260 }
261
262 int
263 dbus_findalldevs(pcap_if_t **alldevsp, char *err_str)
264 {
265 pcap_if_t *found_dev = *alldevsp;
266
267 if (pcap_add_if(&found_dev, "dbus-system", 0, "D-BUS system bus", err_str) < 0)
268 return -1;
269 if (pcap_add_if(&found_dev, "dbus-session", 0, "D-BUS session bus", err_str) < 0)
270 return -1;
271 return 0;
272 }
273