better ip and route assignment

This commit is contained in:
Friedrich Schöller 2009-08-04 16:28:59 +02:00
parent 207f93a795
commit de03a9b272
4 changed files with 14 additions and 8 deletions

View File

@ -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();

View File

@ -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();
}

15
tun.cpp
View File

@ -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
}

2
tun.h
View File

@ -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];