37 #include <arpa/inet.h> 38 #include <sys/ioctl.h> 39 #include <sys/types.h> 40 #include <sys/socket.h> 61 pPlatformService_(pPlatformService),
85 if((tunHandle_ = ::
open(sDevicePath, O_RDWR)) == -1)
89 "TunTap::%s:open:error %s",
101 memset(&ifr, 0,
sizeof(ifr));
104 strncpy(ifr.ifr_name, sDeviceName,
sizeof(ifr.ifr_name));
107 ifr.ifr_flags = IFF_NO_PI | IFF_TAP;
110 if(ioctl(tunHandle_, TUNSETIFF, &ifr) < 0)
114 "TunTap::%s:ioctl:error %s",
123 memset(&ifr, 0,
sizeof(ifr));
126 strncpy(ifr.ifr_name, sDeviceName,
sizeof(ifr.ifr_name));
128 int ctrlsock = socket(AF_INET,SOCK_DGRAM,0);
131 if(ioctl(ctrlsock, SIOCGIFINDEX, &ifr) < 0)
135 "TunTap::%s:getindex:error %s",
144 tunPath_ = sDevicePath;
145 tunName_ = sDeviceName;
147 tunIndex_ = ifr.ifr_ifindex;
158 "TunTap::%s, path %s, name %s, guid %s, index %d",
191 if(arpEnabled ==
false)
196 return set_flags(flags, 1);
203 return set_flags(IFF_UP, -1);
211 std::string sAddress{addr.
str(
false)};
212 std::string sNetMask{mask.
str(
false)};
216 "TunTap::%s, type %s, addr %s, mask %s", __func__,
218 addr.
isIPv6() ?
"ipv6" :
"?",
229 memset(&ifr, 0,
sizeof(ifr));
232 strncpy(ifr.ifr_name, tunName_.c_str(),
sizeof(ifr.ifr_name));
235 ((
struct sockaddr_in_t *) &ifr.ifr_addr)->sin_family = AF_INET;
241 int ctrlsock = socket(AF_INET,SOCK_DGRAM,0);
244 if(ioctl(ctrlsock, SIOCSIFADDR, &ifr) < 0)
248 "TunTap::%s:setaddr:error %s",
260 memset(&ifr, 0,
sizeof(ifr));
263 strncpy(ifr.ifr_name, tunName_.c_str(),
sizeof(ifr.ifr_name));
266 ((
struct sockaddr_in_t *) &ifr.ifr_addr)->sin_family = AF_INET;
271 if(ioctl(ctrlsock, SIOCSIFNETMASK, &ifr) < 0)
275 "TunTap::%s:setmask:error %s",
297 struct in6_addr ifr6_addr;
298 std::uint32_t ifr6_prefixlen;
299 std::uint32_t ifr6_ifindex;
303 struct in6_addr in6_prefix;
306 memset(&ifr6, 0,
sizeof(ifr6));
309 ifr6.ifr6_ifindex = tunIndex_;
312 if(inet_pton(AF_INET6, sNetMask.c_str(), &in6_prefix) < 0)
316 "TunTap::%s:copyprefix:error %s",
326 if(inet_pton(AF_INET6, sAddress.c_str(), &ifr6.ifr6_addr) < 0)
330 "TunTap::%s:copyaddr:error %s",
336 int ctrlsock = socket(AF_INET6,SOCK_DGRAM,0);
340 if(ioctl(ctrlsock, SIOCSIFADDR, &ifr6) < 0)
344 "TunTap::%s:setaddr:error %s",
393 memset(&ifr, 0,
sizeof(ifr));
396 strncpy(ifr.ifr_name, tunName_.c_str(),
sizeof(ifr.ifr_name));
405 int ctrlsock = socket(AF_INET,SOCK_DGRAM,0);
408 if(ioctl(ctrlsock, SIOCSIFHWADDR, &ifr) < 0)
412 "TunTap::%s:sethwaddr:error %s",
434 result =
::writev(tunHandle_, iov, iov_len);
441 "TunTap::%s:write:error %s",
456 result =
::readv(tunHandle_, iov, iov_len);
463 "TunTap::%s:read:error %s",
489 int EMANE::Transports::Virtual::TunTap::set_flags(
int newflags,
int cmd)
495 memset(&ifr, 0,
sizeof(ifr));
498 strncpy(ifr.ifr_name, tunName_.c_str(), IFNAMSIZ);
503 ifr.ifr_flags =(get_flags() | newflags);
508 ifr.ifr_flags =(get_flags() & ~newflags);
513 ifr.ifr_flags = newflags;
517 int ctrlsock = socket(AF_INET,SOCK_DGRAM,0);
520 if(ioctl(ctrlsock, SIOCSIFFLAGS, &ifr) < 0)
524 "TunTap::%s:setflags:error %s",
541 int EMANE::Transports::Virtual::TunTap::get_flags()
547 memset(&ifr, 0,
sizeof(ifr));
550 strncpy(ifr.ifr_name, tunName_.c_str(),
sizeof(ifr.ifr_name));
553 int ctrlsock = socket(AF_INET,SOCK_DGRAM,0);
556 if(ioctl(ctrlsock, SIOCGIFFLAGS, &ifr) < 0)
560 "TunTap::%s:getflags:error %s",
572 return ifr.ifr_flags;
struct EtherAddrWords words
TunTap(PlatformServiceProvider *pPlatformService)
sockaddr * getSockAddr() const
int writev(const struct iovec *, size_t)
std::string str(bool bWithPort=true) const
Definition of the ethernet frame address as an array of 6 bytes or set of 3 words.
int set_ethaddr(const Utils::EtherAddr &)
int readv(struct iovec *, size_t)
const std::uint16_t ETH_ALEN
Ethernet hardware address length.
void addr_to_string(const std::uint8_t *addr, size_t addrlen, const char *delim, char *buf, size_t buflen)
const std::uint16_t ARPHRD_ETHER
Ethernet hardware type.
int set_addr(const INETAddr &, const INETAddr &)
#define LOGGER_STANDARD_LOGGING(logger, level, fmt, args...)
int open(const char *, const char *)
std::uint8_t get_prefixlen(const in6_addr *prefix)