forked from premiere/premiere-libtorrent
using if_indextoname instead of RTM_GETLINK to fill the interface name in enum_net_interfaces (#2253)
using if_indextoname instead of RTM_GETLINK to fill the interface name in enum_net_interfaces. removed IFA_LABEL switch case, removed ip_interface::mtu, master rebase
This commit is contained in:
parent
58f91e67fb
commit
66734e2e5b
|
@ -61,7 +61,6 @@ namespace libtorrent {
|
|||
address interface_address;
|
||||
address netmask;
|
||||
char name[64];
|
||||
int mtu;
|
||||
// an interface is preferred if its address is
|
||||
// not tentative/duplicate/deprecated
|
||||
bool preferred = true;
|
||||
|
|
|
@ -296,39 +296,7 @@ namespace {
|
|||
return true;
|
||||
}
|
||||
|
||||
int parse_nl_link(nlmsghdr* nl_hdr, ip_interface* link_info)
|
||||
{
|
||||
ifinfomsg* link_msg = reinterpret_cast<ifinfomsg*>(NLMSG_DATA(nl_hdr));
|
||||
|
||||
link_info->name[0] = 0;
|
||||
link_info->mtu = 0;
|
||||
|
||||
int rt_len = int(IFLA_PAYLOAD(nl_hdr));
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wcast-align"
|
||||
#endif
|
||||
for (rtattr* rt_attr = reinterpret_cast<rtattr*>(IFLA_RTA(link_msg));
|
||||
RTA_OK(rt_attr, rt_len); rt_attr = RTA_NEXT(rt_attr, rt_len))
|
||||
{
|
||||
switch(rt_attr->rta_type)
|
||||
{
|
||||
case IFLA_MTU:
|
||||
link_info->mtu = int(*reinterpret_cast<unsigned int*>(RTA_DATA(rt_attr)));
|
||||
break;
|
||||
case IFLA_IFNAME:
|
||||
strncpy(link_info->name, reinterpret_cast<char*>(RTA_DATA(rt_attr)), sizeof(link_info->name));
|
||||
break;
|
||||
}
|
||||
}
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
|
||||
return link_msg->ifi_index;
|
||||
}
|
||||
|
||||
bool parse_nl_address(std::map<int, ip_interface> const& link_info, nlmsghdr* nl_hdr, ip_interface* ip_info)
|
||||
bool parse_nl_address(nlmsghdr* nl_hdr, ip_interface* ip_info)
|
||||
{
|
||||
ifaddrmsg* addr_msg = reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(nl_hdr));
|
||||
|
||||
|
@ -369,9 +337,6 @@ namespace {
|
|||
}
|
||||
}
|
||||
|
||||
// initialize name to be empty
|
||||
ip_info->name[0] = '\0';
|
||||
|
||||
int rt_len = int(IFA_PAYLOAD(nl_hdr));
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic push
|
||||
|
@ -397,24 +362,14 @@ namespace {
|
|||
ip_info->interface_address = inaddr_to_address(reinterpret_cast<in_addr*>(RTA_DATA(rt_attr)));
|
||||
}
|
||||
break;
|
||||
case IFA_LABEL:
|
||||
strncpy(ip_info->name, reinterpret_cast<char*>(RTA_DATA(rt_attr)), sizeof(ip_info->name));
|
||||
break;
|
||||
}
|
||||
}
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
|
||||
auto ifi_info = link_info.find(int(addr_msg->ifa_index));
|
||||
if (ifi_info != link_info.end())
|
||||
{
|
||||
ip_info->mtu = ifi_info->second.mtu;
|
||||
// for some reason IPv6 entries don't include an IFA_LABEL attribute
|
||||
// so get it from the link in that case
|
||||
if (ip_info->name[0] == '\0')
|
||||
strncpy(ip_info->name, ifi_info->second.name, sizeof(ip_info->name));
|
||||
}
|
||||
static_assert(sizeof(ip_info->name) >= IF_NAMESIZE, "not enough space in ip_interface::name");
|
||||
if_indextoname(addr_msg->ifa_index, ip_info->name);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -599,7 +554,6 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl
|
|||
wan.interface_address = ip;
|
||||
wan.netmask = address_v4::from_string("255.255.255.255");
|
||||
std::strcpy(wan.name, "eth0");
|
||||
wan.mtu = ios.sim().config().path_mtu(ip, ip);
|
||||
ret.push_back(wan);
|
||||
}
|
||||
#elif TORRENT_USE_NETLINK
|
||||
|
@ -610,13 +564,9 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl
|
|||
return ret;
|
||||
}
|
||||
|
||||
std::uint32_t seq = 0;
|
||||
std::map<int, ip_interface> link_info;
|
||||
|
||||
{
|
||||
char msg[NL_BUFSIZE] = {};
|
||||
nlmsghdr* nl_msg = reinterpret_cast<nlmsghdr*>(msg);
|
||||
int len = nl_dump_request(sock, RTM_GETLINK, seq++, AF_UNSPEC, msg, sizeof(ifinfomsg));
|
||||
int len = nl_dump_request(sock, RTM_GETADDR, 0, AF_PACKET, msg, sizeof(ifaddrmsg));
|
||||
if (len < 0)
|
||||
{
|
||||
ec = error_code(errno, system_category());
|
||||
|
@ -633,40 +583,11 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl
|
|||
for (; NLMSG_OK(nl_msg, len); nl_msg = NLMSG_NEXT(nl_msg, len))
|
||||
{
|
||||
ip_interface iface;
|
||||
int ifi_index = parse_nl_link(nl_msg, &iface);
|
||||
if (ifi_index >= 0) link_info.emplace(ifi_index, iface);
|
||||
if (parse_nl_address(nl_msg, &iface)) ret.push_back(iface);
|
||||
}
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
}
|
||||
|
||||
{
|
||||
char msg[NL_BUFSIZE] = {};
|
||||
nlmsghdr* nl_msg = reinterpret_cast<nlmsghdr*>(msg);
|
||||
int len = nl_dump_request(sock, RTM_GETADDR, seq++, AF_PACKET, msg, sizeof(ifaddrmsg));
|
||||
if (len < 0)
|
||||
{
|
||||
ec = error_code(errno, system_category());
|
||||
close(sock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wcast-align"
|
||||
// NLMSG_OK uses signed/unsigned compare in the same expression
|
||||
#pragma clang diagnostic ignored "-Wsign-compare"
|
||||
#endif
|
||||
for (; NLMSG_OK(nl_msg, len); nl_msg = NLMSG_NEXT(nl_msg, len))
|
||||
{
|
||||
ip_interface iface;
|
||||
if (parse_nl_address(link_info, nl_msg, &iface)) ret.push_back(iface);
|
||||
}
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
}
|
||||
|
||||
close(sock);
|
||||
#elif TORRENT_USE_IFADDRS
|
||||
|
@ -701,7 +622,6 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl
|
|||
|
||||
// ignore errors here. This is best-effort
|
||||
ioctl(s, siocgifmtu, &req);
|
||||
iface.mtu = req.ifr_mtu;
|
||||
ret.push_back(iface);
|
||||
}
|
||||
}
|
||||
|
@ -760,11 +680,6 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl
|
|||
close(s);
|
||||
return ret;
|
||||
}
|
||||
#ifndef TORRENT_OS2
|
||||
iface.mtu = req.ifr_mtu;
|
||||
#else
|
||||
iface.mtu = req.ifr_metric; // according to tcp/ip reference
|
||||
#endif
|
||||
|
||||
std::memset(&req, 0, sizeof(req));
|
||||
std::strncpy(req.ifr_name, item.ifr_name, IF_NAMESIZE - 1);
|
||||
|
@ -832,7 +747,6 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl
|
|||
ip_interface r;
|
||||
std::strncpy(r.name, adapter->AdapterName, sizeof(r.name));
|
||||
r.name[sizeof(r.name)-1] = 0;
|
||||
r.mtu = adapter->Mtu;
|
||||
for (IP_ADAPTER_UNICAST_ADDRESS* unicast = adapter->FirstUnicastAddress;
|
||||
unicast; unicast = unicast->Next)
|
||||
{
|
||||
|
@ -877,7 +791,6 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl
|
|||
iface.netmask = sockaddr_to_address(&buffer[i].iiNetmask.Address
|
||||
, iface.interface_address.is_v4() ? AF_INET : AF_INET6);
|
||||
iface.name[0] = 0;
|
||||
iface.mtu = 1500; // how to get the MTU?
|
||||
ret.push_back(iface);
|
||||
}
|
||||
|
||||
|
@ -898,7 +811,6 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl
|
|||
{
|
||||
iface.interface_address = i->endpoint().address();
|
||||
iface.name[0] = '\0';
|
||||
iface.mtu = 1500;
|
||||
if (iface.interface_address.is_v4())
|
||||
iface.netmask = address_v4::netmask(iface.interface_address.to_v4());
|
||||
ret.push_back(iface);
|
||||
|
|
|
@ -86,14 +86,12 @@ int main()
|
|||
for (std::vector<ip_interface>::const_iterator i = net.begin()
|
||||
, end(net.end()); i != end; ++i)
|
||||
{
|
||||
std::printf("%-30s%-45s%-20s%-8d%s%s%s\n"
|
||||
std::printf("%-30s%-45s%-20s%s%s%s\n"
|
||||
, i->interface_address.to_string(ec).c_str()
|
||||
, i->netmask.to_string(ec).c_str()
|
||||
, i->name
|
||||
, i->mtu
|
||||
, (i->interface_address.is_multicast()?"multicast ":"")
|
||||
, (is_local(i->interface_address)?"local ":"")
|
||||
, (is_loopback(i->interface_address)?"loopback ":""));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue