|
| 1 | +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 4 | + |
| 5 | +#include <tinyhal.h> |
| 6 | +#include <lwip\netif.h> |
| 7 | +#include <lwip\tcpip.h> |
| 8 | +#include <lwip\dhcp.h> |
| 9 | +#include <netif\etharp.h> |
| 10 | + |
| 11 | +#include "WinPcap_Eth_lwIP_Adapter.h" |
| 12 | + |
| 13 | +extern "C" |
| 14 | +{ |
| 15 | +#include "pcapif.h" |
| 16 | +} |
| 17 | + |
| 18 | +extern WINPCAP_ETH_LWIP_DEVICE_CONFIG g_WINPCAP_ETH_LWIP_Config; |
| 19 | +extern NETWORK_CONFIG g_NetworkConfig; |
| 20 | + |
| 21 | +static BOOL LwipNetworkStatus = FALSE; |
| 22 | +static struct netif g_WinPcap_ETH_NetIF; |
| 23 | + |
| 24 | +static WINPCAP_ETH_LWIP_Driver g_WINPCAP_ETH_LWIP_Driver; |
| 25 | + |
| 26 | +BOOL Network_Interface_Bind(int index) |
| 27 | +{ |
| 28 | + NATIVE_PROFILE_HAL_DRIVERS_ETHERNET(); |
| 29 | + |
| 30 | + return g_WINPCAP_ETH_LWIP_Driver.Bind(); |
| 31 | +} |
| 32 | + |
| 33 | +int Network_Interface_Open(int index) |
| 34 | +{ |
| 35 | + NATIVE_PROFILE_HAL_DRIVERS_ETHERNET(); |
| 36 | + if(index >= ARRAYSIZE(g_WINPCAP_ETH_LWIP_Config.DeviceConfigs)) |
| 37 | + return -1; |
| 38 | + |
| 39 | + HAL_CONFIG_BLOCK::ApplyConfig( WINPCAP_ETH_LWIP_DEVICE_CONFIG::GetDriverName(), &g_WINPCAP_ETH_LWIP_Config, sizeof(g_WINPCAP_ETH_LWIP_Config) ); |
| 40 | + |
| 41 | + return g_WINPCAP_ETH_LWIP_Driver.Open(&g_WINPCAP_ETH_LWIP_Config.DeviceConfigs[index], index); |
| 42 | +} |
| 43 | + |
| 44 | +BOOL Network_Interface_Close(int index) |
| 45 | +{ |
| 46 | + NATIVE_PROFILE_HAL_DRIVERS_ETHERNET(); |
| 47 | + |
| 48 | + return g_WINPCAP_ETH_LWIP_Driver.Close(index); |
| 49 | +} |
| 50 | + |
| 51 | +int WINPCAP_ETH_LWIP_Driver::Open(WINPCAP_ETH_LWIP_DRIVER_CONFIG* config, int index) |
| 52 | +{ |
| 53 | + /* Network interface variables */ |
| 54 | + struct ip_addr ipaddr, subnetmask, gateway; |
| 55 | + BOOL isDHCPenabled; |
| 56 | + const SOCK_NetworkConfiguration *iface; |
| 57 | + |
| 58 | + if(config == NULL) |
| 59 | + { |
| 60 | + return -1; |
| 61 | + } |
| 62 | + |
| 63 | + /* Apply network configuration */ |
| 64 | + iface = &g_NetworkConfig.NetworkInterfaces[index]; |
| 65 | + isDHCPenabled = (iface->flags & SOCK_NETWORKCONFIGURATION_FLAGS_DHCP)? TRUE: FALSE; |
| 66 | + |
| 67 | + if(!isDHCPenabled) |
| 68 | + { |
| 69 | + /* Set network address variables for static ip configuration */ |
| 70 | + ipaddr.addr = iface->ipaddr; |
| 71 | + gateway.addr = iface->gateway; |
| 72 | + subnetmask.addr = iface->subnetmask; |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + /* Set network address variables - this will be set by either DHCP or when the configuration is applied */ |
| 77 | + IP4_ADDR(&gateway, 0, 0, 0, 0); |
| 78 | + IP4_ADDR(&ipaddr , 0, 0, 0, 0); |
| 79 | + IP4_ADDR(&subnetmask, 255, 255, 255, 0); |
| 80 | + } |
| 81 | + |
| 82 | + /* Configure the MAC address */ |
| 83 | + if(iface->macAddressLen != ETHARP_HWADDR_LEN) |
| 84 | + { |
| 85 | + return -1; |
| 86 | + } |
| 87 | + |
| 88 | + memcpy(g_WinPcap_ETH_NetIF.hwaddr, iface->macAddressBuffer, iface->macAddressLen); |
| 89 | + |
| 90 | + auto pNetIf = netif_add( &g_WinPcap_ETH_NetIF, &ipaddr, &subnetmask, &gateway, (void*) config->adapterGuid, pcapif_init, tcpip_input ); |
| 91 | + if( pNetIf == nullptr ) |
| 92 | + return -1; |
| 93 | + |
| 94 | + netif_set_default( &g_WinPcap_ETH_NetIF ); |
| 95 | + |
| 96 | + LwipNetworkStatus = TRUE; |
| 97 | + |
| 98 | + return g_WinPcap_ETH_NetIF.num; |
| 99 | +} |
| 100 | + |
| 101 | +BOOL WINPCAP_ETH_LWIP_Driver::Close(int index) |
| 102 | +{ |
| 103 | + const SOCK_NetworkConfiguration *iface; |
| 104 | + |
| 105 | + LwipNetworkStatus = FALSE; |
| 106 | + |
| 107 | + iface = &g_NetworkConfig.NetworkInterfaces[index]; |
| 108 | + |
| 109 | + netif_remove( &g_WinPcap_ETH_NetIF ); |
| 110 | + |
| 111 | + pcapif_shutdown(&g_WinPcap_ETH_NetIF); |
| 112 | + |
| 113 | + memset( &g_WinPcap_ETH_NetIF, 0, sizeof g_WinPcap_ETH_NetIF ); |
| 114 | + |
| 115 | + return TRUE; |
| 116 | +} |
| 117 | + |
| 118 | +/* Bind function doesn't actually do anything for this implementation */ |
| 119 | +BOOL WINPCAP_ETH_LWIP_Driver::Bind(void) |
| 120 | +{ |
| 121 | + return TRUE; |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
0 commit comments