| 49 | } |
| 50 | |
| 51 | Machine::Machine(char const *the_hostname, sockaddr const *addr) |
| 52 | { |
| 53 | // Initialize the machine IP infomation |
| 54 | bzero(&ip, sizeof(ip)); |
| 55 | bzero(&ip4, sizeof(ip4)); |
| 56 | bzero(&ip6, sizeof(ip6)); |
| 57 | |
| 58 | int status; // return for system calls. |
| 59 | ip_text_buffer ip_strbuf; |
| 60 | char localhost[1024]; |
| 61 | |
| 62 | uuid.initialize(TS_UUID_V4); |
| 63 | ink_release_assert(nullptr != uuid.getString()); // The Process UUID must be available on startup |
| 64 | |
| 65 | if (!ats_is_ip(addr)) { |
| 66 | if (!the_hostname) { |
| 67 | // @c gethostname has a broken interface - there's no way to determine the actual size of |
| 68 | // the host name explicitly - the error case doesn't return the size. The standards based |
| 69 | // limit is 63, or 255 for a FQDN. |
| 70 | auto result = gethostname(localhost, sizeof(localhost)); |
| 71 | ink_release_assert(result == 0); |
| 72 | host_name.assign(localhost, strlen(localhost)); |
| 73 | insert_id(localhost); |
| 74 | } |
| 75 | |
| 76 | #if HAVE_IFADDRS_H |
| 77 | ifaddrs *ifa_addrs = nullptr; |
| 78 | status = getifaddrs(&ifa_addrs); |
| 79 | #else |
| 80 | int s = socket(AF_INET, SOCK_DGRAM, 0); |
| 81 | // This number is hard to determine, but needs to be much larger than |
| 82 | // you would expect. On a normal system with just two interfaces and |
| 83 | // one address / interface the return count is 120. Stack space is |
| 84 | // cheap so it's best to go big. |
| 85 | static constexpr int N_REQ = 1024; |
| 86 | ifconf conf; |
| 87 | ifreq req[N_REQ]; |
| 88 | if (0 <= s) { |
| 89 | conf.ifc_len = sizeof(req); |
| 90 | conf.ifc_req = req; |
| 91 | status = ioctl(s, SIOCGIFCONF, &conf); |
| 92 | } else { |
| 93 | status = -1; |
| 94 | } |
| 95 | #endif |
| 96 | |
| 97 | if (0 != status) { |
| 98 | Warning("Unable to determine local host '%.*s' address information - %s", int(host_name.size()), host_name.data(), |
| 99 | strerror(errno)); |
| 100 | } else { |
| 101 | // Loop through the interface addresses and prefer by type. |
| 102 | enum { |
| 103 | NA, // Not an (IP) Address. |
| 104 | LO, // Loopback. |
| 105 | LL, // Link Local |
| 106 | PR, // Private. |
| 107 | MC, // Multicast. |
| 108 | GL // Global. |
nothing calls this directly
no test coverage detected