if (addrmsg.ifa_index == index) {
InetAddr a;
if (addrmsg.ifa_family == AF_INET6) {
- a.setType(InetAddrType::IPV6);
+ a.type = InetAddrType::IPV6;
} else if (addrmsg.ifa_family == AF_INET) {
- a.setType(InetAddrType::IPV4);
+ a.type = InetAddrType::IPV4;
} else {
const std::string msg = "Unsupported inet family";
LOGE(msg);
std::string toString(const InetAddr& a) {
std::string opts = "/" + std::to_string(a.prefix);
- if (a.getType() == InetAddrType::IPV6) {
+ if (a.type == InetAddrType::IPV6) {
return toString(a.getAddr<in6_addr>()) + opts;
}
- if (a.getType() == InetAddrType::IPV4) {
+ if (a.type == InetAddrType::IPV4) {
return toString(a.getAddr<in_addr>()) + opts;
}
return "";
flags(f)
{
if (a.find(":") != std::string::npos) {
- setType(InetAddrType::IPV6);
+ type = InetAddrType::IPV6;
fromString(a, getAddr<in6_addr>());
} else {
- setType(InetAddrType::IPV4);
+ type = InetAddrType::IPV4;
fromString(a, getAddr<in_addr>());
}
}
NetlinkMessage nlm(RTM_NEWADDR, NLM_F_CREATE | NLM_F_REQUEST | NLM_F_ACK);
ifaddrmsg infoAddr = utils::make_clean<ifaddrmsg>();
infoAddr.ifa_index = getInterfaceIndex(mContainerPid, mIfname);
- infoAddr.ifa_family = addr.getType() == InetAddrType::IPV4 ? AF_INET : AF_INET6;
+ infoAddr.ifa_family = addr.type == InetAddrType::IPV4 ? AF_INET : AF_INET6;
infoAddr.ifa_prefixlen = addr.prefix;
infoAddr.ifa_flags = addr.flags;
nlm.put(infoAddr);
- if (addr.getType() == InetAddrType::IPV6) {
+ if (addr.type == InetAddrType::IPV6) {
nlm.put(IFA_ADDRESS, addr.getAddr<in6_addr>());
nlm.put(IFA_LOCAL, addr.getAddr<in6_addr>());
- } else if (addr.getType() == InetAddrType::IPV4) {
+ } else if (addr.type == InetAddrType::IPV4) {
nlm.put(IFA_ADDRESS, addr.getAddr<in_addr>());
nlm.put(IFA_LOCAL, addr.getAddr<in_addr>());
}
NetlinkMessage nlm(RTM_DELADDR, NLM_F_REQUEST | NLM_F_ACK);
ifaddrmsg infoAddr = utils::make_clean<ifaddrmsg>();
infoAddr.ifa_index = getInterfaceIndex(mContainerPid, mIfname);
- infoAddr.ifa_family = addr.getType() == InetAddrType::IPV4 ? AF_INET : AF_INET6;
+ infoAddr.ifa_family = addr.type == InetAddrType::IPV4 ? AF_INET : AF_INET6;
infoAddr.ifa_prefixlen = addr.prefix;
infoAddr.ifa_flags = addr.flags;
nlm.put(infoAddr);
- if (addr.getType() == InetAddrType::IPV6) {
+ if (addr.type == InetAddrType::IPV6) {
nlm.put(IFA_ADDRESS, addr.getAddr<in6_addr>());
nlm.put(IFA_LOCAL, addr.getAddr<in6_addr>());
- } else if (addr.getType() == InetAddrType::IPV4) {
+ } else if (addr.type == InetAddrType::IPV4) {
nlm.put(IFA_ADDRESS, addr.getAddr<in_addr>());
nlm.put(IFA_LOCAL, addr.getAddr<in_addr>());
}
void NetworkInterface::addRoute(const Route& route, const RoutingTable rt)
{
- InetAddrType type = route.dst.getType();
- if (route.src.getType() != type) {
+ InetAddrType type = route.dst.type;
+ if (route.src.type != type) {
const std::string msg = "Family type must be the same";
LOGE(msg);
throw NetworkException(msg);
void NetworkInterface::delRoute(const Route& route, const RoutingTable rt)
{
- InetAddrType type = route.dst.getType();
- if (route.src.getType() != type) {
+ InetAddrType type = route.dst.type;
+ if (route.src.type != type) {
const std::string msg = "Family type must be the same";
LOGE(msg);
throw NetworkException(msg);
route.table = getRoutingTable(rt.rtm_table);
if (rt.rtm_family == AF_INET6) {
- route.dst.setType(InetAddrType::IPV6);
- route.src.setType(InetAddrType::IPV6);
+ route.dst.type = InetAddrType::IPV6;
+ route.src.type = InetAddrType::IPV6;
} else if (rt.rtm_family == AF_INET) {
- route.dst.setType(InetAddrType::IPV4);
- route.src.setType(InetAddrType::IPV4);
+ route.dst.type = InetAddrType::IPV4;
+ route.src.type = InetAddrType::IPV4;
} else {
const std::string msg = "Unsupported inet family";
LOGE(msg);
switch (attrType) {
case RTA_DST: // 1
case RTA_GATEWAY:// 5
- if (route.dst.getType() == InetAddrType::IPV6) {
+ if (route.dst.type == InetAddrType::IPV6) {
response.fetch(attrType, route.dst.getAddr<in6_addr>());
} else {
response.fetch(attrType, route.dst.getAddr<in_addr>());
break;
case RTA_SRC: // 2
case RTA_PREFSRC:// 7
- if (route.src.getType() == InetAddrType::IPV6) {
+ if (route.src.type == InetAddrType::IPV6) {
response.fetch(attrType, route.src.getAddr<in6_addr>());
route.src.prefix = 128;
} else {
#include <cstring>
#include <string>
#include <vector>
+#include <array>
#include <ostream>
#include <arpa/inet.h>
InetAddr() = default;
InetAddr(const std::string& addr, unsigned prefix, uint32_t flags=0);
- InetAddrType getType() const {
- return static_cast<InetAddrType>(type);
- }
- void setType(InetAddrType t) {
- type = static_cast<int>(t);
- }
-
template<typename T>
T& getAddr() {
- //FIXME return union field after fix of addr type
- char *v = addr;
+ std::uint8_t *v = addr.data();
return *(reinterpret_cast<T*>(v));
}
template<typename T>
const T& getAddr() const {
- //FIXME return union field after fix of addr type
- const char *v = addr;
+ const std::uint8_t *v = addr.data();
return *(reinterpret_cast<const T*>(v));
}
+ InetAddrType type;
unsigned prefix;
uint32_t flags;
(
type,
flags,
- prefix
- //FIXME add when visitor can serialize char[SIZE]
- //addr
+ prefix,
+ addr
)
private:
- //FIXME change to union when visitor can serialize type by istream ostream operators
- char addr[sizeof(in6_addr)];
- //FIXME: change to enum when visitor can serialize type by istream ostream operators
- int type;
+ std::array<std::uint8_t,sizeof(in6_addr)> addr;
};
static inline bool operator==(const in_addr& a, const in_addr& b)
static inline bool operator==(const InetAddr& a, const InetAddr& b)
{
- if (a.getType() == b.getType() && a.prefix == b.prefix) {
- if (a.getType() == InetAddrType::IPV6)
+ if (a.type == b.type && a.prefix == b.prefix) {
+ if (a.type == InetAddrType::IPV6)
return a.getAddr<in6_addr>() == b.getAddr<in6_addr>();
else
return a.getAddr<in_addr>() == b.getAddr<in_addr>();