This code is also copied to ssh.cpp:updateCoordHost()
| 1323 | |
| 1324 | // This code is also copied to ssh.cpp:updateCoordHost() |
| 1325 | static void |
| 1326 | calcLocalAddr() |
| 1327 | { |
| 1328 | char hostname[HOST_NAME_MAX]; |
| 1329 | |
| 1330 | JASSERT(gethostname(hostname, sizeof hostname) == 0) (JASSERT_ERRNO); |
| 1331 | |
| 1332 | struct addrinfo *result = NULL; |
| 1333 | struct addrinfo *res; |
| 1334 | int error; |
| 1335 | struct addrinfo hints; |
| 1336 | |
| 1337 | memset(&localhostIPAddr, 0, sizeof localhostIPAddr); |
| 1338 | memset(&hints, 0, sizeof(struct addrinfo)); |
| 1339 | hints.ai_family = AF_UNSPEC; // accept AF_INET and AF_INET6 |
| 1340 | hints.ai_socktype = SOCK_STREAM; |
| 1341 | hints.ai_flags = AI_PASSIVE; |
| 1342 | hints.ai_protocol = 0; |
| 1343 | hints.ai_canonname = NULL; |
| 1344 | hints.ai_addr = NULL; |
| 1345 | hints.ai_next = NULL; |
| 1346 | |
| 1347 | // FROM: Wikipedia:CNAME_record: |
| 1348 | // When a DNS resolver encounters a CNAME record while looking for a regular |
| 1349 | // resource record, it will restart the query using the canonical name |
| 1350 | // instead of the original name. (If the resolver is specifically told to |
| 1351 | // look for CNAME records, the canonical name (right-hand side) is returned, |
| 1352 | // rather than restarting the query.) |
| 1353 | hints.ai_flags |= AI_CANONNAME; |
| 1354 | error = getaddrinfo(hostname, NULL, &hints, &result); |
| 1355 | hints.ai_flags ^= AI_CANONNAME; |
| 1356 | if (error == 0 && result) { |
| 1357 | // if hostname was not fully qualified with domainname, replace it with |
| 1358 | // canonname. Otherwise, keep current alias returned from gethostname(). |
| 1359 | if ( Util::strStartsWith(result->ai_canonname, hostname) && |
| 1360 | result->ai_canonname[strlen(hostname)] == '.' && |
| 1361 | strlen(result->ai_canonname) < sizeof(hostname) ) { |
| 1362 | strncpy(hostname, result->ai_canonname, sizeof hostname); |
| 1363 | } |
| 1364 | freeaddrinfo(result); |
| 1365 | } |
| 1366 | // OPTIONAL: If we still don't have a domainname, we could resolve with DNS |
| 1367 | // (similar to 'man 1 host'), but we ont't know if Internet is present. |
| 1368 | |
| 1369 | /* resolve the hostname into a list of addresses */ |
| 1370 | error = getaddrinfo(hostname, NULL, &hints, &result); |
| 1371 | if (error == 0) { |
| 1372 | /* loop over all returned results and do inverse lookup */ |
| 1373 | bool success = false; |
| 1374 | bool at_least_one_match = false; |
| 1375 | char name[NI_MAXHOST] = ""; |
| 1376 | for (res = result; res != NULL; res = res->ai_next) { |
| 1377 | struct sockaddr_in *s = (struct sockaddr_in *)res->ai_addr; |
| 1378 | |
| 1379 | error = getnameinfo(res->ai_addr, |
| 1380 | res->ai_addrlen, |
| 1381 | name, |
| 1382 | NI_MAXHOST, |
no test coverage detected