mirror of
https://github.com/norohind/hans.git
synced 2025-04-14 21:50:33 +03:00
better ip and route assignment
This commit is contained in:
parent
207f93a795
commit
de03a9b272
@ -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();
|
||||
|
@ -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
15
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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user