Permalink
Browse files

Have non-interface modules take responsibility for identifying their …

…devices.

Have a table of routines to do pcap_create() for devices that aren't
regular network interfaces.  Try each of those in succession until one
says "it's mine" (whether it succeeds or fails); if none do, do a
pcap_create() for a regular interface.

Have those routines do more stringent tests of the name - don't just
accept any name that has a particular substring anywhere in it.  That
reduces the likelihood of a false match (as happened with the CANbus
module when somebody renamed their Ethernet interface "canopy").

Have the table also include routines for pcap_findalldevs().
  • Loading branch information...
1 parent 4e14566 commit 2426611584e9099af5f98d18ef37337df9bef025 @guyharris guyharris committed Jun 11, 2012
Showing with 510 additions and 300 deletions.
  1. +1 −12 fad-getad.c
  2. +3 −11 fad-gifc.c
  3. +1 −10 fad-glifc.c
  4. +1 −1 fad-sita.c
  5. +1 −27 pcap-bpf.c
  6. +31 −2 pcap-bt-linux.c
  7. +2 −2 pcap-bt-linux.h
  8. +47 −1 pcap-can-linux.c
  9. +2 −1 pcap-can-linux.h
  10. +53 −29 pcap-canusb-linux.c
  11. +2 −2 pcap-canusb-linux.h
  12. +39 −13 pcap-dag.c
  13. +2 −2 pcap-dag.h
  14. +1 −1 pcap-dlpi.c
  15. +19 −3 pcap-int.h
  16. +1 −1 pcap-libdlpi.c
  17. +1 −139 pcap-linux.c
  18. +26 −2 pcap-netfilter-linux.c
  19. +2 −2 pcap-netfilter-linux.h
  20. +1 −1 pcap-nit.c
  21. +1 −1 pcap-null.c
  22. +1 −1 pcap-pf.c
  23. +16 −11 pcap-septel.c
  24. +2 −2 pcap-septel.h
  25. +2 −2 pcap-sita.c
  26. +17 −10 pcap-snf.c
  27. +2 −2 pcap-snf.h
  28. +1 −1 pcap-snit.c
  29. +1 −1 pcap-snoop.c
  30. +31 −2 pcap-usb-linux.c
  31. +2 −2 pcap-usb-linux.h
  32. +1 −1 pcap-win32.c
  33. +197 −2 pcap.c
View
@@ -141,11 +141,9 @@ get_sa_len(struct sockaddr *addr)
* Returns -1 on error, 0 otherwise.
* The list, as returned through "alldevsp", may be null if no interfaces
* were up and could be opened.
- *
- * This is the implementation used on platforms that have "getifaddrs()".
*/
int
-pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
+pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf)
{
pcap_if_t *devlist = NULL;
struct ifaddrs *ifap, *ifa;
@@ -273,15 +271,6 @@ pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
freeifaddrs(ifap);
- if (ret != -1) {
- /*
- * We haven't had any errors yet; do any platform-specific
- * operations to add devices.
- */
- if (pcap_platform_finddevs(&devlist, errbuf) < 0)
- ret = -1;
- }
-
if (ret == -1) {
/*
* We had an error; free the list we've been constructing.
View
@@ -133,10 +133,11 @@ struct rtentry; /* declarations in <net/if.h> */
*
* XXX - or platforms that have other, better mechanisms but for which
* we don't yet have code to use that mechanism; I think there's a better
- * way on Linux, for example.
+ * way on Linux, for example, but if that better way is "getifaddrs()",
+ * we already have that.
*/
int
-pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
+pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf)
{
pcap_if_t *devlist = NULL;
register int fd;
@@ -409,15 +410,6 @@ pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
free(buf);
(void)close(fd);
- if (ret != -1) {
- /*
- * We haven't had any errors yet; do any platform-specific
- * operations to add devices.
- */
- if (pcap_platform_finddevs(&devlist, errbuf) < 0)
- ret = -1;
- }
-
if (ret == -1) {
/*
* We had an error; free the list we've been constructing.
View
@@ -80,7 +80,7 @@ struct rtentry; /* declarations in <net/if.h> */
* SIOCGLIFCONF rather than SIOCGIFCONF in order to get IPv6 addresses.)
*/
int
-pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
+pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf)
{
pcap_if_t *devlist = NULL;
register int fd4, fd6, fd;
@@ -362,15 +362,6 @@ pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
(void)close(fd6);
(void)close(fd4);
- if (ret != -1) {
- /*
- * We haven't had any errors yet; do any platform-specific
- * operations to add devices.
- */
- if (pcap_platform_finddevs(&devlist, errbuf) < 0)
- ret = -1;
- }
-
if (ret == -1) {
/*
* We had an error; free the list we've been constructing.
View
@@ -37,7 +37,7 @@
extern pcap_if_t *acn_if_list; /* pcap's list of available interfaces */
-int pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf) {
+int pcap_findalldevs_interfaces(pcap_if_t **alldevsp, char *errbuf) {
//printf("pcap_findalldevs()\n"); // fulko
View
@@ -122,14 +122,6 @@ static int bpf_load(char *errbuf);
#include "pcap-int.h"
-#ifdef HAVE_DAG_API
-#include "pcap-dag.h"
-#endif /* HAVE_DAG_API */
-
-#ifdef HAVE_SNF_API
-#include "pcap-snf.h"
-#endif /* HAVE_SNF_API */
-
#ifdef HAVE_OS_PROTO_H
#include "os-proto.h"
#endif
@@ -407,19 +399,10 @@ pcap_ack_zbuf(pcap_t *p)
#endif /* HAVE_ZEROCOPY_BPF */
pcap_t *
-pcap_create(const char *device, char *ebuf)
+pcap_create_interface(const char *device, char *ebuf)
{
pcap_t *p;
-#ifdef HAVE_DAG_API
- if (strstr(device, "dag"))
- return (dag_create(device, ebuf));
-#endif /* HAVE_DAG_API */
-#ifdef HAVE_SNF_API
- if (strstr(device, "snf"))
- return (snf_create(device, ebuf));
-#endif /* HAVE_SNF_API */
-
p = pcap_create_common(device, ebuf);
if (p == NULL)
return (NULL);
@@ -2283,15 +2266,6 @@ pcap_activate_bpf(pcap_t *p)
int
pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
{
-#ifdef HAVE_DAG_API
- if (dag_platform_finddevs(alldevsp, errbuf) < 0)
- return (-1);
-#endif /* HAVE_DAG_API */
-#ifdef HAVE_SNF_API
- if (snf_platform_finddevs(alldevsp, errbuf) < 0)
- return (-1);
-#endif /* HAVE_SNF_API */
-
return (0);
}
View
@@ -71,7 +71,7 @@ static int bt_setdirection_linux(pcap_t *, pcap_direction_t);
static int bt_stats_linux(pcap_t *, struct pcap_stat *);
int
-bt_platform_finddevs(pcap_if_t **alldevsp, char *err_str)
+bt_findalldevs(pcap_if_t **alldevsp, char *err_str)
{
pcap_if_t *found_dev = *alldevsp;
struct hci_dev_list_req *dev_list;
@@ -135,10 +135,39 @@ bt_platform_finddevs(pcap_if_t **alldevsp, char *err_str)
}
pcap_t *
-bt_create(const char *device, char *ebuf)
+bt_create(const char *device, char *ebuf, int *is_ours)
{
+ char *cp, *cpend;
+ long devnum;
pcap_t *p;
+ /* Does this look like a Bluetooth device? */
+ cp = strrchr(device, '/');
+ if (cp == NULL)
+ cp = device;
+ /* Does it begin with BT_IFACE? */
+ if (strncmp(cp, BT_IFACE, sizeof BT_IFACE - 1) != 0) {
+ /* Nope, doesn't begin with BT_IFACE */
+ *is_ours = 0;
+ return NULL;
+ }
+ /* Yes - is BT_IFACE followed by a number? */
+ cp += sizeof BT_IFACE - 1;
+ devnum = strtol(cp, &cpend, 10);
+ if (cpend == cp || *cpend != '\0') {
+ /* Not followed by a number. */
+ *is_ours = 0;
+ return NULL;
+ }
+ if (devnum < 0) {
+ /* Followed by a non-valid number. */
+ *is_ours = 0;
+ return NULL;
+ }
+
+ /* OK, it's probably ours. */
+ *is_ours = 1;
+
p = pcap_create_common(device, ebuf);
if (p == NULL)
return (NULL);
View
@@ -36,5 +36,5 @@
/*
* Prototypes for Bluetooth-related functions
*/
-int bt_platform_finddevs(pcap_if_t **alldevsp, char *err_str);
-pcap_t *bt_create(const char *device, char *ebuf);
+int bt_findalldevs(pcap_if_t **alldevsp, char *err_str);
+pcap_t *bt_create(const char *device, char *ebuf, int *is_ours);
View
@@ -72,11 +72,57 @@ static int can_setfilter_linux(pcap_t *, struct bpf_program *);
static int can_setdirection_linux(pcap_t *, pcap_direction_t);
static int can_stats_linux(pcap_t *, struct pcap_stat *);
+int
+can_findalldevs(pcap_if_t **devlistp, char *errbuf)
+{
+ /*
+ * There are no platform-specific devices since each device
+ * exists as a regular network interface.
+ *
+ * XXX - true?
+ */
+ return 0;
+}
+
pcap_t *
-can_create(const char *device, char *ebuf)
+can_create(const char *device, char *ebuf, int *is_ours)
{
+ char *cp, *cpend;
+ long devnum;
pcap_t* p;
+ /* Does this look like a CANbus device? */
+ cp = strrchr(device, '/');
+ if (cp == NULL)
+ cp = device;
+ /* Does it begin with "can" or "vcan"? */
+ if (strncmp(cp, "can", 3) == 0) {
+ /* Begins with "can" */
+ cp += 3; /* skip past "can" */
+ } else if (strncmp(cp, "vcan", 4) == 0) {
+ /* Begins with "vcan" */
+ cp += 4;
+ } else {
+ /* Nope, doesn't begin with "can" or "vcan" */
+ *is_ours = 0;
+ return NULL;
+ }
+ /* Yes - is "can" or "vcan" followed by a number from 0? */
+ devnum = strtol(cp, &cpend, 10);
+ if (cpend == cp || *cpend != '\0') {
+ /* Not followed by a number. */
+ *is_ours = 0;
+ return NULL;
+ }
+ if (devnum < 0) {
+ /* Followed by a non-valid number. */
+ *is_ours = 0;
+ return NULL;
+ }
+
+ /* OK, it's probably ours. */
+ *is_ours = 1;
+
p = pcap_create_common(device, ebuf);
if (p == NULL)
return (NULL);
View
@@ -32,4 +32,5 @@
/*
* Prototypes for SocketCAN related functions
*/
-pcap_t* can_create(const char *device, char *ebuf);
+pcap_t* can_create(const char *device, char *ebuf, int *is_ours);
+int can_findalldevs(pcap_if_t **devlistp, char *errbuf);
View
@@ -75,20 +75,17 @@ struct CAN_Msg
struct canusb_t
{
- libusb_context *ctx;
- libusb_device_handle *dev;
- char* src;
- pthread_t worker;
- int rdpipe, wrpipe;
- volatile int* loop;
+ libusb_context *ctx;
+ libusb_device_handle *dev;
+ pthread_t worker;
+ int rdpipe, wrpipe;
+ volatile int* loop;
};
static struct canusb_t canusb;
static volatile int loop;
-
-
-int canusb_platform_finddevs(pcap_if_t **alldevsp, char *err_str)
+int canusb_findalldevs(pcap_if_t **alldevsp, char *err_str)
{
libusb_context *fdctx;
libusb_device** devs;
@@ -108,27 +105,27 @@ int canusb_platform_finddevs(pcap_if_t **alldevsp, char *err_str)
libusb_get_device_descriptor(devs[i],&desc);
if ((desc.idVendor != CANUSB_VID) || (desc.idProduct != CANUSB_PID))
- continue; //It is not, check next device
+ continue; //It is not, check next device
//It is!
libusb_device_handle *dh = NULL;
if (ret = libusb_open(devs[i],&dh) == 0)
{
- char dev_name[30];
- char dev_descr[50];
+ char dev_name[30];
+ char dev_descr[50];
int n = libusb_get_string_descriptor_ascii(dh,desc.iSerialNumber,sernum,64);
sernum[n] = 0;
- snprintf(dev_name, 30, CANUSB_IFACE"%s", sernum);
- snprintf(dev_descr, 50, "CanUSB [%s]", sernum);
+ snprintf(dev_name, 30, CANUSB_IFACE"%s", sernum);
+ snprintf(dev_descr, 50, "CanUSB [%s]", sernum);
libusb_close(dh);
if (pcap_add_if(alldevsp, dev_name, 0, dev_descr, err_str) < 0)
{
- libusb_free_device_list(devs,1);
- return -1;
+ libusb_free_device_list(devs,1);
+ return -1;
}
}
}
@@ -199,23 +196,50 @@ static libusb_device_handle* canusb_opendevice(struct libusb_context *ctx, char*
pcap_t *
-canusb_create(const char *device, char *ebuf)
+canusb_create(const char *device, char *ebuf, int *is_ours)
{
- pcap_t* p;
+ char *cp, *cpend;
+ long devnum;
+ pcap_t* p;
- libusb_init(&canusb.ctx);
-
- p = pcap_create_common(device, ebuf);
- if (p == NULL)
- return (NULL);
-
- memset(&canusb, 0x00, sizeof(canusb));
+ libusb_init(&canusb.ctx);
+
+ /* Does this look like a DAG device? */
+ cp = strrchr(device, '/');
+ if (cp == NULL)
+ cp = device;
+ /* Does it begin with "canusb"? */
+ if (strncmp(cp, "canusb", 6) != 0) {
+ /* Nope, doesn't begin with "canusb" */
+ *is_ours = 0;
+ return NULL;
+ }
+ /* Yes - is "canusb" followed by a number? */
+ cp += 6;
+ devnum = strtol(cp, &cpend, 10);
+ if (cpend == cp || *cpend != '\0') {
+ /* Not followed by a number. */
+ *is_ours = 0;
+ return NULL;
+ }
+ if (devnum < 0) {
+ /* Followed by a non-valid number. */
+ *is_ours = 0;
+ return NULL;
+ }
+
+ /* OK, it's probably ours. */
+ *is_ours = 1;
+
+ p = pcap_create_common(device, ebuf);
+ if (p == NULL)
+ return (NULL);
+
+ memset(&canusb, 0x00, sizeof(canusb));
+ p->activate_op = canusb_activate;
- p->activate_op = canusb_activate;
-
- canusb.src = strdup(p->opt.source);
- return (p);
+ return (p);
}
Oops, something went wrong.

0 comments on commit 2426611

Please sign in to comment.