summaryrefslogtreecommitdiff
path: root/osdep/OSXEthernetTap.cpp
diff options
context:
space:
mode:
authorAdam Ierymenko <adam.ierymenko@gmail.com>2015-11-30 15:39:34 -0800
committerAdam Ierymenko <adam.ierymenko@gmail.com>2015-11-30 15:39:34 -0800
commitf26025058075d91cd69ac892c373fd1d4d7f605d (patch)
tree692b7838328233b3e4013eef9edc6dad9479da2f /osdep/OSXEthernetTap.cpp
parent40a4ba6e39bbc59b320eef2f6839cf30c3f7c8b1 (diff)
downloadinfinitytier-f26025058075d91cd69ac892c373fd1d4d7f605d.tar.gz
infinitytier-f26025058075d91cd69ac892c373fd1d4d7f605d.zip
Simplify IP assignment logic in OSXEthernetTap, also fix for GitHub issue #249
Diffstat (limited to 'osdep/OSXEthernetTap.cpp')
-rw-r--r--osdep/OSXEthernetTap.cpp45
1 files changed, 14 insertions, 31 deletions
diff --git a/osdep/OSXEthernetTap.cpp b/osdep/OSXEthernetTap.cpp
index 6b6f360b..3e43cf95 100644
--- a/osdep/OSXEthernetTap.cpp
+++ b/osdep/OSXEthernetTap.cpp
@@ -143,7 +143,7 @@ static inline int _intl_getifmaddrs(struct _intl_ifmaddrs **pif)
}
free(buf);
buf = NULL;
- }
+ }
} while (buf == NULL);
for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
@@ -475,37 +475,11 @@ bool OSXEthernetTap::enabled() const
return _enabled;
}
-static bool ___removeIp(const std::string &_dev,const InetAddress &ip)
-{
- long cpid = (long)vfork();
- if (cpid == 0) {
- execl("/sbin/ifconfig","/sbin/ifconfig",_dev.c_str(),"inet",ip.toIpString().c_str(),"-alias",(const char *)0);
- _exit(-1);
- } else if (cpid > 0) {
- int exitcode = -1;
- waitpid(cpid,&exitcode,0);
- return (exitcode == 0);
- }
- return false; // never reached, make compiler shut up about return value
-}
-
bool OSXEthernetTap::addIp(const InetAddress &ip)
{
if (!ip)
return false;
- std::vector<InetAddress> allIps(ips());
- if (std::binary_search(allIps.begin(),allIps.end(),ip))
- return true;
-
- // Remove and reconfigure if address is the same but netmask is different
- for(std::vector<InetAddress>::iterator i(allIps.begin());i!=allIps.end();++i) {
- if ((i->ipsEqual(ip))&&(i->netmaskBits() != ip.netmaskBits())) {
- if (___removeIp(_dev,*i))
- break;
- }
- }
-
long cpid = (long)vfork();
if (cpid == 0) {
::execl("/sbin/ifconfig","/sbin/ifconfig",_dev.c_str(),ip.isV4() ? "inet" : "inet6",ip.toString().c_str(),"alias",(const char *)0);
@@ -524,9 +498,18 @@ bool OSXEthernetTap::removeIp(const InetAddress &ip)
if (!ip)
return true;
std::vector<InetAddress> allIps(ips());
- if (!std::binary_search(allIps.begin(),allIps.end(),ip)) {
- if (___removeIp(_dev,ip))
- return true;
+ for(std::vector<InetAddress>::iterator i(allIps.begin());i!=allIps.end();++i) {
+ if (*i == ip) {
+ long cpid = (long)vfork();
+ if (cpid == 0) {
+ execl("/sbin/ifconfig","/sbin/ifconfig",_dev.c_str(),"inet",ip.toIpString().c_str(),"-alias",(const char *)0);
+ _exit(-1);
+ } else if (cpid > 0) {
+ int exitcode = -1;
+ waitpid(cpid,&exitcode,0);
+ return (exitcode == 0);
+ }
+ }
}
return false;
}
@@ -564,7 +547,7 @@ std::vector<InetAddress> OSXEthernetTap::ips() const
freeifaddrs(ifa);
std::sort(r.begin(),r.end());
- std::unique(r.begin(),r.end());
+ r.erase(std::unique(r.begin(),r.end()),r.end());
return r;
}