struct TAPDevice* TAPDevice_find(const char* preferredName, struct Except* eh, struct Allocator* alloc) { #define BUFF_SZ 0x100 char guid[BUFF_SZ] = {0}; char buff[BUFF_SZ] = {0}; if (preferredName != NULL) { snprintf(buff, sizeof(buff), "%s", preferredName); } if (get_device_guid(guid, sizeof(guid), buff, sizeof(buff), eh)) { return NULL; } struct TAPDevice* out = Allocator_malloc(alloc, sizeof(struct TAPDevice)); out->name = Allocator_malloc(alloc, CString_strlen(buff)+1); Bits_memcpy(out->name, buff, CString_strlen(buff)+1); snprintf(buff, sizeof(buff), USERMODEDEVICEDIR "%s" TAPSUFFIX, guid); out->path = Allocator_malloc(alloc, CString_strlen(buff)+1); Bits_memcpy(out->path, buff, CString_strlen(buff)+1); return out; }
void c_tun_device_windows::init() { _fact("Creating TUN/TAP (windows version)"); m_guid = get_device_guid(); _fact("GUID " << to_string(m_guid)); m_handle = get_device_handle(); m_stream_handle_ptr = std::make_unique<boost::asio::windows::stream_handle>(m_ioservice, m_handle); m_mac_address = get_mac(m_handle); m_buffer.fill(0); if (!m_stream_handle_ptr->is_open()) throw std::runtime_error("TUN/TAP stream handle open error"); _fact("Start reading from TUN"); m_stream_handle_ptr->async_read_some(boost::asio::buffer(m_buffer), boost::bind(&c_tun_device_windows::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); }
co_rc_t open_tap_win32(HANDLE *phandle, char *prefered_name) { char device_path[256]; char device_guid[0x100]; co_rc_t rc; HANDLE handle; BOOL bret; char name_buffer[0x100] = {0, }; struct { unsigned long major; unsigned long minor; unsigned long debug; } version; LONG version_len; if (prefered_name != NULL) co_snprintf(name_buffer, sizeof(name_buffer), "%s", prefered_name); rc = get_device_guid(device_guid, sizeof(device_guid), name_buffer, sizeof(name_buffer)); if (!CO_OK(rc)) return CO_RC(NOT_FOUND); co_terminal_print("colinux-net-daemon: opening TAP: \"%s\"\n", name_buffer); /* * Open Windows TAP-Win32 adapter */ snprintf (device_path, sizeof(device_path), "%s%s%s", USERMODEDEVICEDIR, device_guid, TAPSUFFIX); handle = CreateFile ( device_path, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0 ); if (handle == INVALID_HANDLE_VALUE) { co_terminal_print_last_error("colinux-net-daemon: tap device open"); return CO_RC(ERROR); } bret = DeviceIoControl(handle, TAP_IOCTL_GET_VERSION, &version, sizeof (version), &version, sizeof (version), &version_len, NULL); if (bret == FALSE) { co_terminal_print_last_error("colinux-net-daemon: error getting driver version"); CloseHandle(handle); return CO_RC(ERROR); } co_terminal_print("colinux-net-daemon: TAP driver version %d.%d\n", version.major, version.minor); *phandle = handle; return CO_RC(OK); }
static int tap_win32_open(tap_win32_overlapped_t **phandle, const char *prefered_name) { char device_path[256]; char device_guid[0x100]; int rc; HANDLE handle; BOOL bret; char name_buffer[0x100] = {0, }; struct { unsigned long major; unsigned long minor; unsigned long debug; } version; DWORD version_len; DWORD idThread; if (prefered_name != NULL) snprintf(name_buffer, sizeof(name_buffer), "%s", prefered_name); rc = get_device_guid(device_guid, sizeof(device_guid), name_buffer, sizeof(name_buffer)); if (rc) return -1; snprintf (device_path, sizeof(device_path), "%s%s%s", USERMODEDEVICEDIR, device_guid, TAPSUFFIX); handle = CreateFile ( device_path, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0 ); if (handle == INVALID_HANDLE_VALUE) { return -1; } bret = DeviceIoControl(handle, TAP_IOCTL_GET_VERSION, &version, sizeof (version), &version, sizeof (version), &version_len, NULL); if (bret == FALSE) { CloseHandle(handle); return -1; } if (!tap_win32_set_status(handle, TRUE)) { return -1; } tap_win32_overlapped_init(&tap_overlapped, handle); *phandle = &tap_overlapped; CreateThread(NULL, 0, tap_win32_thread_entry, (LPVOID)&tap_overlapped, 0, &idThread); return 0; }
static int initDevice(struct net_device *net_dev) { char device_path[256]; char device_guid[0x100]; char device_name[1024]; ULONG tapVer[3]; ULONG len = 0; tap_win32 *dev = (net_dev ? (tap_win32*)(net_dev->priv) : NULL); if(dev == NULL) return -1; bzero(dev, sizeof(tap_win32)); if(get_device_guid(device_guid, sizeof(device_guid), device_name, sizeof(device_name)) != 0) { printm("ERROR: SkyEye requires a TAP-Win32 driver that is at least version %d.%d\n" "Please install from http://prdownloads.sourceforge.net/openvpn/openvpn-2.0_beta2-install.exe\n", TAP_WIN32_MIN_MAJOR, TAP_WIN32_MIN_MINOR); return -1; } if(GetVersion() < 0x80000000) { char hostip[16]; sprintf(hostip, "%d.%d.%d.%d", net_dev->hostip[0], net_dev->hostip[1], net_dev->hostip[2], net_dev->hostip[3]); if(_spawnlp(_P_DETACH, "netsh.exe", "netsh.exe", "interface", "ip", "set", "address", device_name, "static", hostip, "255.255.255.0", NULL) == -1) { printm("WARNING: Failed to Change IP Address/Mask.\n"); } } snprintf(device_path, sizeof(device_path), "%s%s%s", USERMODEDEVICEDIR, device_guid, TAPSUFFIX); dev->fHandle = CreateFile(device_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, NULL); if(dev->fHandle == INVALID_HANDLE_VALUE) { printm("Opening TAP connection failed\n"); return -1; } bzero(&tapVer[0], sizeof(tapVer)); if(!DeviceIoControl(dev->fHandle, TAP_IOCTL_GET_VERSION, &tapVer[0], sizeof(tapVer), &tapVer[0], sizeof(tapVer), &len, NULL)) { ShowErrorMsg("Could not get driver version info"); CloseHandle(dev->fHandle); return -1; } if(!(tapVer[0] > TAP_WIN32_MIN_MAJOR || (tapVer[0] == TAP_WIN32_MIN_MAJOR && tapVer[1] >= TAP_WIN32_MIN_MINOR))) { printm("ERROR: SkyEye requires a TAP-Win32 driver that is at least version %d.%d\n" "Please install an updated version from http://prdownloads.sourceforge.net/openvpn/openvpn-2.0_beta2-install.exe\n", TAP_WIN32_MIN_MAJOR, TAP_WIN32_MIN_MINOR); CloseHandle(dev->fHandle); return -1; } tapVer[0] = 1; if(!DeviceIoControl(dev->fHandle, TAP_IOCTL_SET_MEDIA_STATUS, &tapVer[0], sizeof(ULONG), &tapVer[0], sizeof(ULONG), &len, NULL)) { ShowErrorMsg("Setting Media Status to connected failed"); CloseHandle(dev->fHandle); return -1; } InitializeCriticalSection(&dev->fLocker); dev->fEvent = CreateEvent(NULL, TRUE, FALSE, NULL); return 0; }