| 440 | } |
| 441 | |
| 442 | static int getservicenameinfo(const struct sockaddr *sa, |
| 443 | char *service, |
| 444 | size_t servicelen, |
| 445 | int flags) |
| 446 | { |
| 447 | int ret = -1; |
| 448 | int port = ntohs(((struct sockaddr_in *)sa)->sin_port); |
| 449 | |
| 450 | if (!(flags & NI_NUMERICSERV)) { |
| 451 | struct servent *se = getservbyport( |
| 452 | port, |
| 453 | (flags & NI_DGRAM) ? "udp" : "tcp"); |
| 454 | if (se && se->s_name) { |
| 455 | /* Service name looked up successfully. */ |
| 456 | ret = snprintf(service, servicelen, "%s", se->s_name); |
| 457 | if (ret < 0 || (size_t)ret >= servicelen) { |
| 458 | return EAI_MEMORY; |
| 459 | } |
| 460 | return 0; |
| 461 | } |
| 462 | /* Otherwise just fall into the numeric service code... */ |
| 463 | } |
| 464 | ret = snprintf(service, servicelen, "%d", port); |
| 465 | if (ret < 0 || (size_t)ret >= servicelen) { |
| 466 | return EAI_MEMORY; |
| 467 | } |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | * Convert an ipv4 address to a hostname. |