win32 port: added the option to select pcap adapter from its network address

This commit is contained in:
goldsimon 2010-03-09 09:35:15 +00:00
parent e97f6e1909
commit bb3aa84605
7 changed files with 139 additions and 5 deletions

View File

@ -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)

View File

@ -71,7 +71,7 @@
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="Packet.lib"
AdditionalDependencies="Packet.lib wpcap.lib"
OutputFile=".\Release/test.exe"
LinkIncremental="1"
SuppressStartupBanner="true"
@ -98,6 +98,9 @@
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCWebDeploymentTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
@ -158,7 +161,7 @@
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="Packet.lib"
AdditionalDependencies="Packet.lib wpcap.lib"
OutputFile=".\Debug/test.exe"
LinkIncremental="1"
SuppressStartupBanner="true"
@ -185,6 +188,9 @@
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCWebDeploymentTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>

View File

@ -167,6 +167,31 @@
Name="Source Files"
Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
>
<File
RelativePath="..\pcap_helper.c"
>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories=""
PreprocessorDefinitions=""
BasicRuntimeChecks="3"
/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32"
>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
AdditionalIncludeDirectories=""
PreprocessorDefinitions=""
/>
</FileConfiguration>
</File>
<File
RelativePath="..\pktdrv.c"
>
@ -221,6 +246,10 @@
<Filter
Name="Header Files"
>
<File
RelativePath="..\pcap_helper.h"
>
</File>
<File
RelativePath="..\pktdrv.h"
>

64
ports/win32/pcap_helper.c Normal file
View File

@ -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 <stdlib.h>
#include <stdio.h>
#include <windows.h>
#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;
}

View File

@ -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__ */

View File

@ -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;

View File

@ -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,