diff --git a/ports/win32/lwipcfg_msvc.h.example b/ports/win32/lwipcfg_msvc.h.example index 1fa16fc..545b819 100644 --- a/ports/win32/lwipcfg_msvc.h.example +++ b/ports/win32/lwipcfg_msvc.h.example @@ -12,6 +12,8 @@ /** Define this to the GUID of the windows network adapter to use * or NOT define this if you want PACKET_LIB_ADAPTER_NR to be used */ /*#define PACKET_LIB_ADAPTER_GUID "00000000-0000-0000-0000-000000000000"*/ +/*#define PACKET_LIB_GET_ADAPTER_NETADDRESS(addr) IP4_ADDR((addr), 192,168,1,0)*/ +/*#define PACKET_LIB_QUIET*/ #define LWIP_PORT_INIT_IPADDR(addr) IP4_ADDR((addr), 192,168,1,200) #define LWIP_PORT_INIT_GW(addr) IP4_ADDR((addr), 192,168,1,1) diff --git a/ports/win32/msvc8/lwIP_Test.vcproj b/ports/win32/msvc8/lwIP_Test.vcproj index 1fb9457..b90d7e5 100644 --- a/ports/win32/msvc8/lwIP_Test.vcproj +++ b/ports/win32/msvc8/lwIP_Test.vcproj @@ -71,7 +71,7 @@ /> + @@ -158,7 +161,7 @@ /> + diff --git a/ports/win32/msvc8/lwIP_pktif.vcproj b/ports/win32/msvc8/lwIP_pktif.vcproj index c954a41..e0b2b4c 100644 --- a/ports/win32/msvc8/lwIP_pktif.vcproj +++ b/ports/win32/msvc8/lwIP_pktif.vcproj @@ -167,6 +167,31 @@ Name="Source Files" Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" > + + + + + + + + @@ -221,6 +246,10 @@ + + diff --git a/ports/win32/pcap_helper.c b/ports/win32/pcap_helper.c new file mode 100644 index 0000000..f879044 --- /dev/null +++ b/ports/win32/pcap_helper.c @@ -0,0 +1,64 @@ +#include "pcap_helper.h" + +#define WIN32_LEAN_AND_MEAN +/* get the windows definitions of the following 4 functions out of the way */ +#include +#include +#include +#define HAVE_REMOTE +#include "pcap.h" + +/** Get the index of an adapter by its network address + * + * @param netaddr network address of the adapter (e.g. 192.168.1.0) + * @return index of the adapter or negative on error + */ +int +get_adapter_index_from_addr(U32 netaddr, char *guid, U32 guid_len) +{ + pcap_if_t *alldevs; + pcap_if_t *d; + char errbuf[PCAP_ERRBUF_SIZE+1]; + char source[PCAP_ERRBUF_SIZE+1]; + int index = 0; + + memset(guid, 0, guid_len); + + /* Retrieve the interfaces list */ + if (pcap_findalldevs_ex(source, NULL, &alldevs, errbuf) == -1) { + printf("Error in pcap_findalldevs: %s\n", errbuf); + return -1; + } + /* Scan the list printing every entry */ + for (d = alldevs; d != NULL; d = d->next, index++) { + pcap_addr_t *a; + for(a = d->addresses; a != NULL; a = a->next) { + if (a->addr->sa_family == AF_INET) { + U32 a_addr = ((struct sockaddr_in *)a->addr)->sin_addr.s_addr; + U32 a_netmask = ((struct sockaddr_in *)a->netmask)->sin_addr.s_addr; + U32 a_netaddr = a_addr & a_netmask; + if (a_netaddr == netaddr) { + int ret = -1; + char name[128]; + char *start, *end; + strcpy(name, d->name); + start = strstr(name, "{"); + if (start != NULL) { + end = strstr(start, "}"); + if (end != NULL) { + size_t len = end - start + 1; + memcpy(guid, start, len); + ret = index; + } + } + pcap_freealldevs(alldevs); + return ret; + } + } + } + } + printf("Network address not found.\n"); + + pcap_freealldevs(alldevs); + return index; +} diff --git a/ports/win32/pcap_helper.h b/ports/win32/pcap_helper.h new file mode 100644 index 0000000..3ddacfa --- /dev/null +++ b/ports/win32/pcap_helper.h @@ -0,0 +1,6 @@ +#ifndef __PCAP_HELPER_H__ +#define __PCAP_HELPER_H__ + +int get_adapter_index_from_addr(U32 netaddr, char *guid, U32 guid_len); + +#endif /* __PCAP_HELPER_H__ */ \ No newline at end of file diff --git a/ports/win32/pktdrv.c b/ports/win32/pktdrv.c index 2125392..54afca7 100644 --- a/ports/win32/pktdrv.c +++ b/ports/win32/pktdrv.c @@ -63,6 +63,7 @@ */ #include "pktdrv.h" +#include "lwipcfg_msvc.h" #define WIN32_LEAN_AND_MEAN /* get the windows definitions of the following 4 functions out of the way */ @@ -144,7 +145,7 @@ get_adapter_list(char** adapter_list, int list_len, void* buffer, size_t buf_len /** Get the index of an adapter by its GUID * - * @param GUID of the adapter + * @param adapter_guid GUID of the adapter * @return index of the adapter or negative on error */ int @@ -168,7 +169,6 @@ get_adapter_index(const char* adapter_guid) return -1; } - /** * Open a network adapter and set it up for packet input * @@ -183,7 +183,9 @@ void* init_adapter(int adapter_num, char *mac_addr, input_fn input, void *arg, enum link_adapter_event *linkstate) { char *AdapterList[MAX_NUM_ADAPTERS]; +#ifndef PACKET_LIB_QUIET int i; +#endif /* PACKET_LIB_QUIET */ char AdapterName[ADAPTER_NAME_LEN]; /* string that contains a list of the network adapters */ int AdapterNum; PPACKET_OID_DATA ppacket_oid_data; @@ -208,6 +210,7 @@ init_adapter(int adapter_num, char *mac_addr, input_fn input, void *arg, enum li free(pa); return NULL; /* no adapters found */ } +#ifndef PACKET_LIB_QUIET for (i = 0; i < AdapterNum; i++) { LPADAPTER lpAdapter; printf("%2i: %s\n", i, AdapterList[i]); @@ -227,6 +230,7 @@ init_adapter(int adapter_num, char *mac_addr, input_fn input, void *arg, enum li lpAdapter = NULL; } } +#endif /* PACKET_LIB_QUIET */ /* invalid adapter index -> check this after printing the adapters */ if (adapter_num < 0) { printf("Invalid adapter_num: %d\n", adapter_num); @@ -239,7 +243,9 @@ init_adapter(int adapter_num, char *mac_addr, input_fn input, void *arg, enum li free(pa); return NULL; } +#ifndef PACKET_LIB_QUIET printf("Using adapter_num: %d\n", adapter_num); +#endif /* PACKET_LIB_QUIET */ /* set up the selected adapter */ pa->lpAdapter = PacketOpenAdapter(AdapterList[adapter_num]); if (!pa->lpAdapter || (pa->lpAdapter->hFile == INVALID_HANDLE_VALUE)) { @@ -257,7 +263,7 @@ init_adapter(int adapter_num, char *mac_addr, input_fn input, void *arg, enum li ppacket_oid_data->Oid = OID_GEN_VENDOR_DESCRIPTION; ppacket_oid_data->Length = PACKET_OID_DATA_SIZE; if (PacketRequest(pa->lpAdapter, FALSE, ppacket_oid_data)) { - printf("USE: %s", ppacket_oid_data->Data); + printf("Using adapter: \"%s\"", ppacket_oid_data->Data); } /* get the MAC address of the selected adapter */ ppacket_oid_data->Oid = OID_802_3_PERMANENT_ADDRESS; diff --git a/ports/win32/pktif.c b/ports/win32/pktif.c index ac5dbec..2ddeff9 100644 --- a/ports/win32/pktif.c +++ b/ports/win32/pktif.c @@ -86,6 +86,7 @@ #include "netif/etharp.h" #include "pktdrv.h" +#include "pcap_helper.h" /* include the port-dependent configuration */ #include "lwipcfg_msvc.h" @@ -124,6 +125,25 @@ low_level_init(struct netif *netif) int adapter_num = PACKET_LIB_ADAPTER_NR; enum link_adapter_event linkstate; +#ifdef PACKET_LIB_GET_ADAPTER_NETADDRESS + ip_addr_t netaddr; +#define GUID_LEN 128 + char guid[GUID_LEN + 1]; + memset(&guid, 0, sizeof(guid)); + PACKET_LIB_GET_ADAPTER_NETADDRESS(&netaddr); + if (get_adapter_index_from_addr(ip4_addr_get_u32(&netaddr), guid, GUID_LEN) < 0) { + printf("ERROR initializing network adapter, failed to get GUID for network address %s\n", ip_ntoa(&netaddr)); + LWIP_ASSERT("ERROR initializing network adapter!\n", 0); + return; + } + adapter_num = get_adapter_index(guid); + if (adapter_num < 0) { + printf("ERROR finding network adapter with GUID \"%s\"!\n", guid); + LWIP_ASSERT("ERROR initializing network adapter!\n", 0); + return; + } + +#else /* PACKET_LIB_GET_ADAPTER_NETADDRESS */ #ifdef PACKET_LIB_ADAPTER_GUID /* get adapter index for guid string */ adapter_num = get_adapter_index(PACKET_LIB_ADAPTER_GUID); @@ -133,6 +153,7 @@ low_level_init(struct netif *netif) return; } #endif /* PACKET_LIB_ADAPTER_GUID */ +#endif /* PACKET_LIB_GET_ADAPTER_NETADDRESS */ /* Do whatever else is needed to initialize interface. */ if ((netif->state = init_adapter(adapter_num, adapter_mac_addr,