summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anytun.org>2009-01-19 23:21:30 +0000
committerChristian Pointner <equinox@anytun.org>2009-01-19 23:21:30 +0000
commitcf44e028d076399177619d8bc523fa13521e0b5e (patch)
treefdf5577e32b1ad656f32101e530fe255b3089ee6 /src
parentfixed overlapped handling of read and write at windows tundevice (diff)
fixed silly bug at new tundevice
Diffstat (limited to 'src')
-rw-r--r--src/anytun.cpp16
-rw-r--r--src/anytun.suobin51200 -> 51712 bytes
-rw-r--r--src/encryptedPacket.cpp2
-rw-r--r--src/plainPacket.cpp2
-rw-r--r--src/tunDevice.h9
-rw-r--r--src/win32/tunDevice.cpp24
6 files changed, 32 insertions, 21 deletions
diff --git a/src/anytun.cpp b/src/anytun.cpp
index 1886a60..3a1ab73 100644
--- a/src/anytun.cpp
+++ b/src/anytun.cpp
@@ -168,7 +168,12 @@ void sender(void* p)
encrypted_packet.setLength(MAX_PACKET_LENGTH);
// read packet from device
- u_int32_t len = param->dev.read(plain_packet.getPayload(), plain_packet.getPayloadLength());
+ int len = param->dev.read(plain_packet.getPayload(), plain_packet.getPayloadLength());
+ if(len < 0)
+ continue; // silently ignore device read errors, this is probably no good idea...
+
+ if(static_cast<u_int32_t>(len) < PlainPacket::getHeaderLength())
+ continue; // ignore short packets
plain_packet.setPayloadLength(len);
// set payload type
if(param->dev.getType() == TYPE_TUN)
@@ -257,8 +262,11 @@ void receiver(void* p)
encrypted_packet.setLength(MAX_PACKET_LENGTH);
// read packet from socket
- u_int32_t len = param->src.recv(encrypted_packet.getBuf(), encrypted_packet.getLength(), remote_end);
- if(len < EncryptedPacket::getHeaderLength())
+ int len = param->src.recv(encrypted_packet.getBuf(), encrypted_packet.getLength(), remote_end);
+ if(len < 0)
+ continue; // silently ignore socket recv errors, this is probably no good idea...
+
+ if(static_cast<u_int32_t>(len) < EncryptedPacket::getHeaderLength())
continue; // ignore short packets
encrypted_packet.setLength(len);
@@ -319,7 +327,7 @@ void receiver(void* p)
}
catch(std::runtime_error& e)
{
- cLog.msg(Log::PRIO_ERR) << "sender thread died due to an uncaught runtime_error: " << e.what();
+ cLog.msg(Log::PRIO_ERR) << "receiver thread died due to an uncaught runtime_error: " << e.what();
}
catch(std::exception& e)
{
diff --git a/src/anytun.suo b/src/anytun.suo
index 99739ec..d111921 100644
--- a/src/anytun.suo
+++ b/src/anytun.suo
Binary files differ
diff --git a/src/encryptedPacket.cpp b/src/encryptedPacket.cpp
index 692d221..9a1e063 100644
--- a/src/encryptedPacket.cpp
+++ b/src/encryptedPacket.cpp
@@ -137,7 +137,7 @@ void EncryptedPacket::reinit()
if(length_ < (sizeof(struct HeaderStruct))) {
header_ = NULL;
- throw std::runtime_error("packet can't be initialized, buffer is too small");
+ throw std::runtime_error("encrypted packet can't be initialized, buffer is too small");
}
if(auth_tag_)
diff --git a/src/plainPacket.cpp b/src/plainPacket.cpp
index 6d06b3f..6863c48 100644
--- a/src/plainPacket.cpp
+++ b/src/plainPacket.cpp
@@ -103,7 +103,7 @@ void PlainPacket::reinit()
if(length_ < (sizeof(payload_type_t))) {
payload_type_ = NULL;
- throw std::runtime_error("packet can't be initialized, buffer is too small");
+ throw std::runtime_error("plain packet can't be initialized, buffer is too small");
}
}
diff --git a/src/tunDevice.h b/src/tunDevice.h
index c07f813..7fb1355 100644
--- a/src/tunDevice.h
+++ b/src/tunDevice.h
@@ -49,11 +49,15 @@ public:
int read(u_int8_t* buf, u_int32_t len);
int write(u_int8_t* buf, u_int32_t len);
- std::string getActualName() { return actual_name_.c_str(); }
+ std::string getActualName() { return actual_name_; }
device_type_t getType() { return conf_.type_; }
std::string getTypeString()
{
+#ifndef _MSC_VER
if(fd_ < 0)
+#else
+ if(handle_ == INVALID_HANDLE_VALUE)
+#endif
return "";
switch(conf_.type_)
@@ -74,8 +78,9 @@ private:
void do_ifconfig();
int fix_return(int ret, size_t pi_length);
+#ifndef _MSC_VER
int fd_;
-#ifdef _MSC_VER
+#else
HANDLE handle_;
#endif
diff --git a/src/win32/tunDevice.cpp b/src/win32/tunDevice.cpp
index af9b194..559d520 100644
--- a/src/win32/tunDevice.cpp
+++ b/src/win32/tunDevice.cpp
@@ -88,9 +88,6 @@ TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifc
std::stringstream tapname;
tapname << USERMODEDEVICEDIR << adapterid << TAPSUFFIX;
-
- cLog.msg(Log::PRIO_DEBUG) << "'" << tapname.str() << "'";
-
handle_ = CreateFile(tapname.str().c_str(), GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
if(handle_ == INVALID_HANDLE_VALUE) {
std::stringstream msg;
@@ -98,6 +95,9 @@ TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifc
throw std::runtime_error(msg.str());
}
+ conf_.type_ = TYPE_TAP;
+ actual_name_ = adapterid;
+
int status = true;
if(!DeviceIoControl(handle_, TAP_IOCTL_SET_MEDIA_STATUS, &status, sizeof(status), &status, sizeof(status), &len, NULL)) {
std::stringstream msg;
@@ -105,8 +105,6 @@ TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifc
throw std::runtime_error(msg.str());
}
- conf_.type_ = TYPE_TAP;
-
if(ifcfg_lp != "" && ifcfg_rnmp != "")
do_ifconfig();
}
@@ -140,17 +138,17 @@ int TunDevice::read(u_int8_t* buf, u_int32_t len)
return -1;
}
}
- else
+ else {
cLog.msg(Log::PRIO_ERR) << "Error while reading from device: " << LogErrno(GetLastError());
-
- return -1;
+ return -1;
+ }
}
return lenout;
}
int TunDevice::write(u_int8_t* buf, u_int32_t len)
{
- DWORD lenout;
+ DWORD lenout;
OVERLAPPED overlapped;
overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
overlapped.Offset = 0;
@@ -166,12 +164,12 @@ int TunDevice::write(u_int8_t* buf, u_int32_t len)
return -1;
}
}
- else
+ else {
cLog.msg(Log::PRIO_ERR) << "Error while writing to device: " << LogErrno(GetLastError());
-
- return -1;
+ return -1;
+ }
}
- return lenout;
+ return lenout;
}
void TunDevice::init_post()