X-Git-Url: https://git.tcpdump.org/libpcap/blobdiff_plain/d3c9726731f635a3c636c06fe03cc825860303fc..4e145de1143f597ef8277b51c5524af1e994f277:/pcap-dbus.c diff --git a/pcap-dbus.c b/pcap-dbus.c index dd858437..b1fb548f 100644 --- a/pcap-dbus.c +++ b/pcap-dbus.c @@ -11,8 +11,8 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior written + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written * permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS @@ -29,11 +29,9 @@ */ #ifdef HAVE_CONFIG_H -#include "config.h" +#include #endif -#include "pcap-int.h" - #include #include @@ -41,10 +39,21 @@ #include +#include "pcap-int.h" +#include "pcap-dbus.h" + +/* + * Private data for capturing on D-Bus. + */ +struct pcap_dbus { + DBusConnection *conn; + u_int packets_read; /* count of packets read */ +}; + static int dbus_read(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user) { - DBusConnection *conn = handle->md.priv; + struct pcap_dbus *handlep = handle->priv; struct pcap_pkthdr pkth; DBusMessage *message; @@ -54,12 +63,12 @@ dbus_read(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user) int count = 0; - message = dbus_connection_pop_message(conn); + message = dbus_connection_pop_message(handlep->conn); while (!message) { - // XXX p->md.timeout = timeout_ms; - if (!dbus_connection_read_write(conn, 100)) { - snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Connection closed"); + /* XXX handle->opt.timeout = timeout_ms; */ + if (!dbus_connection_read_write(handlep->conn, 100)) { + pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Connection closed"); return -1; } @@ -68,11 +77,11 @@ dbus_read(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user) return -2; } - message = dbus_connection_pop_message(conn); + message = dbus_connection_pop_message(handlep->conn); } if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) { - snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Disconnected"); + pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Disconnected"); return -1; } @@ -81,9 +90,10 @@ dbus_read(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user) /* pkth.caplen = min (payload_len, handle->snapshot); */ gettimeofday(&pkth.ts, NULL); - if (handle->fcode.bf_insns == NULL || bpf_filter(handle->fcode.bf_insns, raw_msg, pkth.len, pkth.caplen)) { - handle->md.packets_read++; - callback(user, &pkth, raw_msg); + if (handle->fcode.bf_insns == NULL || + bpf_filter(handle->fcode.bf_insns, (u_char *)raw_msg, pkth.len, pkth.caplen)) { + handlep->packets_read++; + callback(user, &pkth, (u_char *)raw_msg); count++; } @@ -96,28 +106,30 @@ static int dbus_write(pcap_t *handle, const void *buf, size_t size) { /* XXX, not tested */ - DBusConnection *conn = handle->md.priv; + struct pcap_dbus *handlep = handle->priv; DBusError error = DBUS_ERROR_INIT; DBusMessage *msg; if (!(msg = dbus_message_demarshal(buf, size, &error))) { - snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dbus_message_demarshal() failed: %s", error.message); + pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dbus_message_demarshal() failed: %s", error.message); dbus_error_free(&error); return -1; } - dbus_connection_send(conn, msg, NULL); - dbus_connection_flush(conn); + dbus_connection_send(handlep->conn, msg, NULL); + dbus_connection_flush(handlep->conn); dbus_message_unref(msg); return 0; -} +} static int dbus_stats(pcap_t *handle, struct pcap_stat *stats) { - stats->ps_recv = handle->md.packets_read; + struct pcap_dbus *handlep = handle->priv; + + stats->ps_recv = handlep->packets_read; stats->ps_drop = 0; stats->ps_ifdrop = 0; return 0; @@ -126,12 +138,11 @@ dbus_stats(pcap_t *handle, struct pcap_stat *stats) static void dbus_cleanup(pcap_t *handle) { - DBusConnection *conn = handle->md.priv; + struct pcap_dbus *handlep = handle->priv; - handle->md.priv = NULL; - dbus_connection_unref(conn); + dbus_connection_unref(handlep->conn); - pcap_live_cleanup_common(handle); + pcap_cleanup_live_common(handle); } static int @@ -148,22 +159,22 @@ dbus_activate(pcap_t *handle) #define N_RULES sizeof(rules)/sizeof(rules[0]) - const char *dev = handle->opt.source; + struct pcap_dbus *handlep = handle->priv; + const char *dev = handle->opt.device; DBusError error = DBUS_ERROR_INIT; - DBusConnection *conn; - int i; + u_int i; if (strcmp(dev, "dbus-system") == 0) { - if (!(conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) { - snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to get system bus: %s", error.message); + if (!(handlep->conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) { + pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to get system bus: %s", error.message); dbus_error_free(&error); return PCAP_ERROR; } } else if (strcmp(dev, "dbus-session") == 0) { - if (!(conn = dbus_bus_get(DBUS_BUS_SESSION, &error))) { - snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to get session bus: %s", error.message); + if (!(handlep->conn = dbus_bus_get(DBUS_BUS_SESSION, &error))) { + pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to get session bus: %s", error.message); dbus_error_free(&error); return PCAP_ERROR; } @@ -171,20 +182,20 @@ dbus_activate(pcap_t *handle) } else if (strncmp(dev, "dbus://", 7) == 0) { const char *addr = dev + 7; - if (!(conn = dbus_connection_open(addr, &error))) { - snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to open connection to: %s: %s", addr, error.message); + if (!(handlep->conn = dbus_connection_open(addr, &error))) { + pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to open connection to: %s: %s", addr, error.message); dbus_error_free(&error); return PCAP_ERROR; } - if (!dbus_bus_register(conn, &error)) { - snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to register bus %s: %s\n", addr, error.message); + if (!dbus_bus_register(handlep->conn, &error)) { + pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to register bus %s: %s\n", addr, error.message); dbus_error_free(&error); return PCAP_ERROR; } } else { - snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't get bus address from %s", handle->opt.source); + pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't get bus address from %s", handle->opt.device); return PCAP_ERROR; } @@ -200,9 +211,9 @@ dbus_activate(pcap_t *handle) handle->getnonblock_op = pcap_getnonblock_fd; handle->setnonblock_op = pcap_setnonblock_fd; handle->stats_op = dbus_stats; + handle->cleanup_op = dbus_cleanup; handle->selectable_fd = handle->fd = -1; - handle->md.priv = conn; if (handle->opt.rfmon) { /* @@ -212,19 +223,27 @@ dbus_activate(pcap_t *handle) return PCAP_ERROR_RFMON_NOTSUP; } - /* dbus_connection_set_max_message_size(conn, handle->snapshot); */ + /* + * Turn a negative snapshot value (invalid), a snapshot value of + * 0 (unspecified), or a value bigger than the normal maximum + * value, into the maximum message length for D-Bus (128MB). + */ + if (handle->snapshot <= 0 || handle->snapshot > 134217728) + handle->snapshot = 134217728; + + /* dbus_connection_set_max_message_size(handlep->conn, handle->snapshot); */ if (handle->opt.buffer_size != 0) - dbus_connection_set_max_received_size(conn, handle->opt.buffer_size); + dbus_connection_set_max_received_size(handlep->conn, handle->opt.buffer_size); for (i = 0; i < N_RULES; i++) { - dbus_bus_add_match(conn, rules[i], &error); + dbus_bus_add_match(handlep->conn, rules[i], &error); if (dbus_error_is_set(&error)) { dbus_error_free(&error); /* try without eavesdrop */ - dbus_bus_add_match(conn, rules[i] + strlen(EAVESDROPPING_RULE), &error); + dbus_bus_add_match(handlep->conn, rules[i] + strlen(EAVESDROPPING_RULE), &error); if (dbus_error_is_set(&error)) { - snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to add bus match: %s\n", error.message); + pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to add bus match: %s\n", error.message); dbus_error_free(&error); dbus_cleanup(handle); return PCAP_ERROR; @@ -240,8 +259,8 @@ dbus_create(const char *device, char *ebuf, int *is_ours) { pcap_t *p; - if (strcmp(device, "dbus-system") && - strcmp(device, "dbus-session") && + if (strcmp(device, "dbus-system") && + strcmp(device, "dbus-session") && strncmp(device, "dbus://", 7)) { *is_ours = 0; @@ -249,7 +268,7 @@ dbus_create(const char *device, char *ebuf, int *is_ours) } *is_ours = 1; - p = pcap_create_common(device, ebuf); + p = pcap_create_common(ebuf, sizeof (struct pcap_dbus)); if (p == NULL) return (NULL); @@ -257,14 +276,12 @@ dbus_create(const char *device, char *ebuf, int *is_ours) return (p); } -int -dbus_findalldevs(pcap_if_t **alldevsp, char *err_str) +int +dbus_findalldevs(pcap_if_list_t *devlistp, char *err_str) { - pcap_if_t *found_dev = *alldevsp; - - if (pcap_add_if(&found_dev, "dbus-system", 0, "D-BUS system bus", err_str) < 0) + if (add_dev(devlistp, "dbus-system", 0, "D-Bus system bus", err_str) == NULL) return -1; - if (pcap_add_if(&found_dev, "dbus-session", 0, "D-BUS session bus", err_str) < 0) + if (add_dev(devlistp, "dbus-session", 0, "D-Bus session bus", err_str) == NULL) return -1; return 0; }