wintap.c (n2n-2.8) | : | wintap.c (n2n-3.0) | ||
---|---|---|---|---|
skipping to change at line 23 | skipping to change at line 23 | |||
err = WSAStartup(MAKEWORD(2, 2), &wsaData ); | err = WSAStartup(MAKEWORD(2, 2), &wsaData ); | |||
if( err != 0 ) { | if( err != 0 ) { | |||
/* Tell the user that we could not find a usable */ | /* Tell the user that we could not find a usable */ | |||
/* WinSock DLL. */ | /* WinSock DLL. */ | |||
printf("FATAL ERROR: unable to initialise Winsock 2.x."); | printf("FATAL ERROR: unable to initialise Winsock 2.x."); | |||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | |||
} | } | |||
} | } | |||
void destroyWin32() { | ||||
WSACleanup(); | ||||
} | ||||
struct win_adapter_info { | struct win_adapter_info { | |||
HANDLE handle; | HANDLE handle; | |||
char adapterid[1024]; | char adapterid[1024]; | |||
char adaptername[1024]; | char adaptername[1024]; | |||
}; | }; | |||
/* ***************************************************** */ | /* ***************************************************** */ | |||
static HANDLE open_tap_device(const char *adapterid) { | static HANDLE open_tap_device(const char *adapterid) { | |||
char tapname[1024]; | char tapname[1024]; | |||
skipping to change at line 100 | skipping to change at line 104 | |||
/* continue */ | /* continue */ | |||
} | } | |||
} | } | |||
RegCloseKey(key); | RegCloseKey(key); | |||
} | } | |||
/* ***************************************************** */ | /* ***************************************************** */ | |||
static int print_adapter_callback(struct win_adapter_info *adapter, struct tunta p_dev *device) { | static int print_adapter_callback(struct win_adapter_info *adapter, struct tunta p_dev *device) { | |||
printf(" %s - %s\n", adapter->adapterid, adapter->adaptername); | printf(" %s - %s\n", adapter->adapterid, adapter->adaptername); | |||
/* continue */ | /* continue */ | |||
return(1); | return(1); | |||
} | } | |||
void win_print_available_adapters() { | void win_print_available_adapters() { | |||
iterate_win_network_adapters(print_adapter_callback, NULL); | iterate_win_network_adapters(print_adapter_callback, NULL); | |||
} | } | |||
/* ***************************************************** */ | /* ***************************************************** */ | |||
skipping to change at line 225 | skipping to change at line 229 | |||
} | } | |||
/* ***************************************************** */ | /* ***************************************************** */ | |||
int open_wintap(struct tuntap_dev *device, | int open_wintap(struct tuntap_dev *device, | |||
const char * devname, | const char * devname, | |||
const char * address_mode, /* "static" or "dhcp" */ | const char * address_mode, /* "static" or "dhcp" */ | |||
char *device_ip, | char *device_ip, | |||
char *device_mask, | char *device_mask, | |||
const char *device_mac, | const char *device_mac, | |||
int mtu) { | int mtu, | |||
int metric) { | ||||
char cmd[256]; | char cmd[256]; | |||
DWORD len; | DWORD len; | |||
ULONG status = TRUE; | ULONG status = TRUE; | |||
memset(device, 0, sizeof(struct tuntap_dev)); | memset(device, 0, sizeof(struct tuntap_dev)); | |||
device->device_handle = INVALID_HANDLE_VALUE; | device->device_handle = INVALID_HANDLE_VALUE; | |||
device->device_name = devname[0] ? _strdup(devname) : NULL; | device->device_name = devname[0] ? _strdup(devname) : NULL; | |||
device->ifName = NULL; | device->ifName = NULL; | |||
device->ip_addr = inet_addr(device_ip); | device->ip_addr = inet_addr(device_ip); | |||
skipping to change at line 248 | skipping to change at line 254 | |||
if(device->device_handle == INVALID_HANDLE_VALUE) { | if(device->device_handle == INVALID_HANDLE_VALUE) { | |||
if(!devname[0]) | if(!devname[0]) | |||
printf("No Windows tap devices found, did you run tapinstall.exe?\n"); | printf("No Windows tap devices found, did you run tapinstall.exe?\n"); | |||
else | else | |||
printf("Cannot find tap device \"%s\"\n", devname); | printf("Cannot find tap device \"%s\"\n", devname); | |||
return -1; | return -1; | |||
} | } | |||
/* ************************************** */ | /* ************************************** */ | |||
/* interface index, required for routing */ | ||||
ULONG buffer_len = 0; | ||||
IP_ADAPTER_INFO *buffer; | ||||
// get required buffer size and allocate buffer | ||||
GetAdaptersInfo(NULL, &buffer_len); | ||||
buffer = malloc(buffer_len); | ||||
// find device by name and get its index | ||||
if(buffer && !GetAdaptersInfo(buffer, &buffer_len)) { | ||||
IP_ADAPTER_INFO *i; | ||||
for(i = buffer; i != NULL; i = i->Next) { | ||||
if(!strcmp(device->device_name, i->AdapterName)) { | ||||
device->if_idx = i->Index; | ||||
break; | ||||
} | ||||
} | ||||
} | ||||
free(buffer); | ||||
/* ************************************** */ | ||||
if(device_mac[0]) | if(device_mac[0]) | |||
set_interface_mac(device, device_mac); | set_interface_mac(device, device_mac); | |||
/* Get MAC address from tap device->device_name */ | /* Get MAC address from tap device->device_name */ | |||
if(!DeviceIoControl(device->device_handle, TAP_IOCTL_GET_MAC, | if(!DeviceIoControl(device->device_handle, TAP_IOCTL_GET_MAC, | |||
device->mac_addr, sizeof(device->mac_addr), | device->mac_addr, sizeof(device->mac_addr), | |||
device->mac_addr, sizeof(device->mac_addr), &len, 0)) { | device->mac_addr, sizeof(device->mac_addr), &len, 0)) { | |||
printf("Could not get MAC address from Windows tap %s (%s)\n", | printf("Could not get MAC address from Windows tap %s (%s)\n", | |||
device->device_name, device->ifName); | device->device_name, device->ifName); | |||
skipping to change at line 297 | skipping to change at line 327 | |||
if(system(cmd) == 0) { | if(system(cmd) == 0) { | |||
device->ip_addr = inet_addr(device_ip); | device->ip_addr = inet_addr(device_ip); | |||
device->device_mask = inet_addr(device_mask); | device->device_mask = inet_addr(device_mask); | |||
} else | } else | |||
printf("WARNING: Unable to set device %s IP address [%s]\n", | printf("WARNING: Unable to set device %s IP address [%s]\n", | |||
device->ifName, cmd); | device->ifName, cmd); | |||
/* ****************** */ | /* ****************** */ | |||
/* MTU */ | /* MTU */ | |||
_snprintf(cmd, sizeof(cmd), | _snprintf(cmd, sizeof(cmd), | |||
"netsh interface ipv4 set subinterface \"%s\" mtu=%d store=persistent > nul" , | "netsh interface ipv4 set subinterface \"%s\" mtu=%d store=persistent > nul" , | |||
device->ifName, mtu); | device->ifName, mtu); | |||
if(system(cmd) != 0) | if(system(cmd) != 0) | |||
printf("WARNING: Unable to set device %s MTU [%s]\n", | printf("WARNING: Unable to set device %s parameters MTU=%d store=persistent | |||
device->ifName, cmd); | [%s]\n", | |||
device->ifName, mtu, cmd); | ||||
/* ****************** */ | ||||
/* metric */ | ||||
PMIB_IPINTERFACE_ROW Row; | ||||
if(metric) { /* try to change only if a value has been given, otherwise leave | ||||
with default or as set before */ | ||||
// find & store original metric | ||||
Row = calloc(1, sizeof(MIB_IPINTERFACE_ROW)); | ||||
InitializeIpInterfaceEntry(Row); | ||||
Row->InterfaceIndex = device->if_idx; | ||||
Row->Family = AF_INET; | ||||
GetIpInterfaceEntry(Row); | ||||
device->metric_original = Row->Metric; | ||||
device->metric = metric; | ||||
// set new value | ||||
Row->Metric = metric; | ||||
// store | ||||
Row->SitePrefixLength = 0; /* if not set to zero, following function call fa | ||||
ils... */ | ||||
SetIpInterfaceEntry(Row); | ||||
free(Row); | ||||
} | ||||
/* ****************** */ | ||||
/* set driver media status to 'connected' (i.e. set the interface up) */ | /* set driver media status to 'connected' (i.e. set the interface up) */ | |||
if (!DeviceIoControl (device->device_handle, TAP_IOCTL_SET_MEDIA_STATUS, | if (!DeviceIoControl (device->device_handle, TAP_IOCTL_SET_MEDIA_STATUS, | |||
&status, sizeof (status), | &status, sizeof (status), | |||
&status, sizeof (status), &len, NULL)) | &status, sizeof (status), &len, NULL)) | |||
printf("WARNING: Unable to enable TAP adapter\n"); | printf("WARNING: Unable to enable TAP adapter\n"); | |||
/* | /* | |||
* Initialize overlapped structures | * Initialize overlapped structures | |||
*/ | */ | |||
skipping to change at line 386 | skipping to change at line 446 | |||
} | } | |||
/* ************************************************ */ | /* ************************************************ */ | |||
int tuntap_open(struct tuntap_dev *device, | int tuntap_open(struct tuntap_dev *device, | |||
char *dev, | char *dev, | |||
const char *address_mode, /* static or dhcp */ | const char *address_mode, /* static or dhcp */ | |||
char *device_ip, | char *device_ip, | |||
char *device_mask, | char *device_mask, | |||
const char * device_mac, | const char * device_mac, | |||
int mtu) { | int mtu, | |||
return(open_wintap(device, dev, address_mode, device_ip, device_mask, device | int metric) { | |||
_mac, mtu)); | return(open_wintap(device, dev, address_mode, device_ip, device_mask, device | |||
_mac, mtu, metric)); | ||||
} | } | |||
/* ************************************************ */ | /* ************************************************ */ | |||
void tuntap_close(struct tuntap_dev *tuntap) { | void tuntap_close(struct tuntap_dev *tuntap) { | |||
PMIB_IPINTERFACE_ROW Row; | ||||
if(tuntap->metric) { /* only required if a value has been given (and thus stor | ||||
ed) */ | ||||
// find device entry | ||||
Row = calloc(1, sizeof(MIB_IPINTERFACE_ROW)); | ||||
InitializeIpInterfaceEntry(Row); | ||||
Row->InterfaceIndex = tuntap->if_idx; | ||||
Row->Family = AF_INET; | ||||
GetIpInterfaceEntry(Row); | ||||
// restore original value | ||||
Row->Metric = tuntap->metric_original; | ||||
// store | ||||
Row->SitePrefixLength = 0; /* if not set to zero, following function call fa | ||||
ils... */ | ||||
SetIpInterfaceEntry(Row); | ||||
free(Row); | ||||
} | ||||
CloseHandle(tuntap->device_handle); | CloseHandle(tuntap->device_handle); | |||
} | } | |||
/* Fill out the ip_addr value from the interface. Called to pick up dynamic | /* Fill out the ip_addr value from the interface. Called to pick up dynamic | |||
* address changes. */ | * address changes. */ | |||
void tuntap_get_address(struct tuntap_dev *tuntap) | void tuntap_get_address(struct tuntap_dev *tuntap) | |||
{ | { | |||
} | } | |||
/* ************************************************ */ | /* ************************************************ */ | |||
#if 0 | #if 0 | |||
int main(int argc, char* argv[]) { | int main(int argc, char* argv[]) { | |||
struct tuntap_dev tuntap; | struct tuntap_dev tuntap; | |||
int i; | int i; | |||
int mtu = 1400; | int mtu = 1400; | |||
printf("Welcome to n2n\n"); | printf("Welcome to n2n\n"); | |||
initWin32(); | initWin32(); | |||
open_wintap(&tuntap, "static", "1.2.3.20", "255.255.255.0", mtu); | open_wintap(&tuntap, "static", "1.2.3.20", "255.255.255.0", mtu, 0); | |||
for(i=0; i<10; i++) { | for(i=0; i<10; i++) { | |||
u_char buf[MTU]; | u_char buf[MTU]; | |||
int rc; | int rc; | |||
rc = tun_read(&tuntap, buf, sizeof(buf)); | rc = tun_read(&tuntap, buf, sizeof(buf)); | |||
buf[0]=2; | buf[0]=2; | |||
buf[1]=3; | buf[1]=3; | |||
buf[2]=4; | buf[2]=4; | |||
End of changes. 9 change blocks. | ||||
8 lines changed or deleted | 95 lines changed or added |