diff --git a/libdnet-stripped/include/dnet/intf.h b/libdnet-stripped/include/dnet/intf.h index 3d1939f12..ae54861dc 100644 --- a/libdnet-stripped/include/dnet/intf.h +++ b/libdnet-stripped/include/dnet/intf.h @@ -58,6 +58,7 @@ typedef int (*intf_handler)(const struct intf_entry *entry, void *arg); __BEGIN_DECLS intf_t *intf_open(void); int intf_get(intf_t *i, struct intf_entry *entry); +int intf_get_index(intf_t *intf, struct intf_entry *entry, int af, unsigned int index); int intf_get_src(intf_t *i, struct intf_entry *entry, struct addr *src); int intf_get_dst(intf_t *i, struct intf_entry *entry, struct addr *dst); int intf_get_pcap_devname(const char *intf_name, char *pcapdev, int pcapdevlen); diff --git a/libdnet-stripped/src/intf-win32.c b/libdnet-stripped/src/intf-win32.c index 2c5e2f497..fc2644732 100644 --- a/libdnet-stripped/src/intf-win32.c +++ b/libdnet-stripped/src/intf-win32.c @@ -257,6 +257,21 @@ _find_adapter_address(intf_t *intf, const char *device) return NULL; } +static IP_ADAPTER_ADDRESSES * +_find_adapter_address_by_index(intf_t *intf, int af, unsigned int index) +{ + IP_ADAPTER_ADDRESSES *a; + + for (a = intf->iftable; a != NULL; a = a->Next) { + if (af == AF_INET && index == a->IfIndex) + return a; + if (af == AF_INET6 && index == a->Ipv6IfIndex) + return a; + } + + return NULL; +} + intf_t * intf_open(void) { @@ -280,6 +295,24 @@ intf_get(intf_t *intf, struct intf_entry *entry) return (0); } +/* Look up an interface from an index, such as a sockaddr_in6.sin6_scope_id. */ +int +intf_get_index(intf_t *intf, struct intf_entry *entry, int af, unsigned int index) +{ + IP_ADAPTER_ADDRESSES *a; + + if (_refresh_tables(intf) < 0) + return (-1); + + a = _find_adapter_address_by_index(intf, af, index); + if (a == NULL) + return (-1); + + _adapter_address_to_entry(intf, a, entry); + + return (0); +} + int intf_get_src(intf_t *intf, struct intf_entry *entry, struct addr *src) { diff --git a/libdnet-stripped/src/intf.c b/libdnet-stripped/src/intf.c index 57b14eed3..93e7b00cf 100644 --- a/libdnet-stripped/src/intf.c +++ b/libdnet-stripped/src/intf.c @@ -590,6 +590,21 @@ intf_get(intf_t *intf, struct intf_entry *entry) return (_intf_get_aliases(intf, entry)); } +/* Look up an interface from an index, such as a sockaddr_in6.sin6_scope_id. */ +int +intf_get_index(intf_t *intf, struct intf_entry *entry, int af, unsigned int index) +{ + char namebuf[IFNAMSIZ]; + char *devname; + + /* af is ignored; only used in intf-win32.c. */ + devname = if_indextoname(index, namebuf); + if (devname == NULL) + return (-1); + strlcpy(entry->intf_name, devname, sizeof(entry->intf_name)); + return intf_get(intf, entry); +} + static int _match_intf_src(const struct intf_entry *entry, void *arg) {