From c06031014042f10811d43b4daae7d483dbc48bc3 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Mon, 19 Jan 2009 22:06:52 +0000 Subject: first working version of windows TunDevice (currently only tap) --- src/win32/tunDevice.cpp | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) (limited to 'src/win32') diff --git a/src/win32/tunDevice.cpp b/src/win32/tunDevice.cpp index 7fc774e..3049c24 100644 --- a/src/win32/tunDevice.cpp +++ b/src/win32/tunDevice.cpp @@ -101,10 +101,12 @@ TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifc int status = true; if(!DeviceIoControl(handle_, TAP_IOCTL_SET_MEDIA_STATUS, &status, sizeof(status), &status, sizeof(status), &len, NULL)) { std::stringstream msg; - msg << "Unable set device media status: " << LogErrno(GetLastError()); + msg << "Unable to set device media status: " << LogErrno(GetLastError()); throw std::runtime_error(msg.str()); } + conf_.type_ = TYPE_TAP; + if(ifcfg_lp != "" && ifcfg_rnmp != "") do_ifconfig(); } @@ -122,18 +124,40 @@ int TunDevice::fix_return(int ret, size_t pi_length) int TunDevice::read(u_int8_t* buf, u_int32_t len) { - std::string input; - std::cin >> input; - Buffer b(input); - u_int32_t cpylen = b.getLength() < len ? b.getLength() : len; - std::memcpy(buf, b.getBuf(), b.getLength()); - return cpylen; + DWORD lenout; + OVERLAPPED overlapped; + overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); + overlapped.Offset = 0; + overlapped.OffsetHigh = 0; +// ResetEvent(overlapped.hEvent); + + if(!ReadFile(handle_, buf, len, &lenout, &overlapped)) { + DWORD err = GetLastError(); + if(err == ERROR_IO_PENDING) { + WaitForSingleObject(overlapped.hEvent, INFINITE); + if(!GetOverlappedResult(handle_, &overlapped, &lenout, FALSE)) + cLog.msg(Log::PRIO_ERR) << "Error while trying to get overlapped result: " << LogErrno(GetLastError()); + + return lenout; + } + else + cLog.msg(Log::PRIO_ERR) << "Error while reading from: " << LogErrno(GetLastError()); + + return -1; + } + return lenout; } int TunDevice::write(u_int8_t* buf, u_int32_t len) { - std::cout << Buffer(buf, len).getHexDumpOneLine() << std::endl; - return 0; + DWORD lenout; + OVERLAPPED overlapped = {0}; + + if(!WriteFile(handle_, buf, len, &lenout, &overlapped)) { + cLog.msg(Log::PRIO_ERR) << "Error while writing to device: " << LogErrno(GetLastError()); + return -1; + } + return lenout; } void TunDevice::init_post() -- cgit v1.2.3