MCPcopy Create free account
hub / github.com/PurpleI2P/i2pd / GetMTUUnix

Function GetMTUUnix

libi2pd/util.cpp:526–578  ·  view source on GitHub ↗

assume unix

Source from the content-addressed store, hash-verified

524 }
525#else // assume unix
526 int GetMTUUnix (const boost::asio::ip::address& localAddress, int fallback)
527 {
528 ifaddrs* ifaddr, *ifa = nullptr;
529 if (getifaddrs(&ifaddr) == -1)
530 {
531 LogPrint(eLogError, "NetIface: Can't call getifaddrs(): ", strerror(errno));
532 return fallback;
533 }
534
535 int family = 0;
536 // look for interface matching local address
537 for (ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next)
538 {
539 if (!ifa->ifa_addr)
540 continue;
541
542 family = ifa->ifa_addr->sa_family;
543 if (family == AF_INET && localAddress.is_v4())
544 {
545 sockaddr_in* sa = (sockaddr_in*) ifa->ifa_addr;
546 if (!memcmp(&sa->sin_addr, localAddress.to_v4().to_bytes().data(), 4))
547 break; // address matches
548 }
549 else if (family == AF_INET6 && localAddress.is_v6())
550 {
551 sockaddr_in6* sa = (sockaddr_in6*) ifa->ifa_addr;
552 if (!memcmp(&sa->sin6_addr, localAddress.to_v6().to_bytes().data(), 16))
553 break; // address matches
554 }
555 }
556 int mtu = fallback;
557 if (ifa && family)
558 { // interface found?
559 int fd = socket(family, SOCK_DGRAM, 0);
560 if (fd > 0)
561 {
562 ifreq ifr;
563 strncpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ-1); // set interface for query
564 if (ioctl(fd, SIOCGIFMTU, &ifr) >= 0)
565 mtu = ifr.ifr_mtu; // MTU
566 else
567 LogPrint (eLogError, "NetIface: Failed to run ioctl: ", strerror(errno));
568 close(fd);
569 }
570 else
571 LogPrint(eLogError, "NetIface: Failed to create datagram socket");
572 }
573 else
574 LogPrint(eLogWarning, "NetIface: Interface for local address", localAddress.to_string(), " not found");
575 freeifaddrs(ifaddr);
576
577 return mtu;
578 }
579#endif // _WIN32
580
581 int GetMTU (const boost::asio::ip::address& localAddress)

Callers 2

GetMTUFunction · 0.85
IsLocalAddressFunction · 0.85

Calls 2

LogPrintFunction · 0.85
to_stringMethod · 0.80

Tested by

no test coverage detected