From de03a9b2721eea5885980b046fda56dbaf9fc201 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Friedrich=20Sch=C3=B6ller?= Date: Tue, 4 Aug 2009 16:28:59 +0200 Subject: [PATCH] better ip and route assignment --- client.cpp | 3 ++- server.cpp | 2 +- tun.cpp | 15 ++++++++++----- tun.h | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/client.cpp b/client.cpp index 67bdb2c..29db981 100644 --- a/client.cpp +++ b/client.cpp @@ -127,7 +127,8 @@ bool Client::handleEchoData(const TunnelHeader &header, int dataLength, uint32_t syslog(LOG_INFO, "connection established"); - tun->setIp(ntohl(*(uint32_t *)echoReceivePayloadBuffer())); + uint32_t ip = ntohl(*(uint32_t *)echoReceivePayloadBuffer()); + tun->setIp(ip, (ip & 0xffffff00) + 1, false); state = STATE_ESTABLISHED; dropPrivileges(); diff --git a/server.cpp b/server.cpp index 8dd43a5..a76e8e2 100644 --- a/server.cpp +++ b/server.cpp @@ -36,7 +36,7 @@ Server::Server(int tunnelMtu, const char *deviceName, const char *passphrase, ui this->network = network & 0xffffff00; this->pollTimeout = pollTimeout; - tun->setIp(this->network + 1); + tun->setIp(this->network + 1, this->network + 2, true); dropPrivileges(); } diff --git a/tun.cpp b/tun.cpp index 3293a70..09ce3a1 100644 --- a/tun.cpp +++ b/tun.cpp @@ -61,19 +61,24 @@ Tun::~Tun() tun_close(fd, device); } -void Tun::setIp(uint32_t ip) +void Tun::setIp(uint32_t ip, uint32_t destIp, bool includeSubnet) { char cmdline[512]; string ips = Utility::formatIp(ip); + string destIps = Utility::formatIp(destIp); + + snprintf(cmdline, sizeof(cmdline), "/sbin/ifconfig %s %s %s netmask 255.255.255.255", device, ips.c_str(), destIps.c_str()); - snprintf(cmdline, sizeof(cmdline), "/sbin/ifconfig %s %s %s netmask 255.255.255.0", device, ips.c_str(), ips.c_str()); if (system(cmdline) != 0) syslog(LOG_ERR, "could not set tun device ip address"); #ifndef LINUX - snprintf(cmdline, sizeof(cmdline), "/sbin/route add %s/24 %s", ips.c_str(), ips.c_str()); - if (system(cmdline) != 0) - syslog(LOG_ERR, "could not add route"); + if (includeSubnet) + { + snprintf(cmdline, sizeof(cmdline), "/sbin/route add %s/24 %s", destIps.c_str(), destIps.c_str()); + if (system(cmdline) != 0) + syslog(LOG_ERR, "could not add route"); + } #endif } diff --git a/tun.h b/tun.h index 9097729..a6675d2 100644 --- a/tun.h +++ b/tun.h @@ -38,7 +38,7 @@ public: void write(const char *buffer, int length); - void setIp(uint32_t ip); + void setIp(uint32_t ip, uint32_t destIp, bool includeSubnet); protected: char device[VTUN_DEV_LEN];