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