diff --git a/libc/dns/getaddrinfo.c b/libc/dns/getaddrinfo.c index 215e6eb91..103f8f764 100644 --- a/libc/dns/getaddrinfo.c +++ b/libc/dns/getaddrinfo.c @@ -21,12 +21,15 @@ #include "libc/dns/dns.h" #include "libc/dns/hoststxt.h" #include "libc/dns/resolvconf.h" +#include "libc/dns/servicestxt.h" #include "libc/fmt/conv.h" #include "libc/mem/mem.h" +#include "libc/runtime/gc.h" #include "libc/sock/sock.h" #include "libc/str/str.h" #include "libc/sysv/consts/af.h" #include "libc/sysv/consts/inaddr.h" +#include "libc/sysv/consts/sock.h" #include "libc/sysv/errfuns.h" /** @@ -43,11 +46,26 @@ int getaddrinfo(const char *name, const char *service, const struct addrinfo *hints, struct addrinfo **res) { int rc, port; const char *canon; + size_t protolen; struct addrinfo *ai; + char proto[32]; port = 0; + if (!name && !service) return EAI_NONAME; - if (service && (port = parseport(service)) == -1) return EAI_NONAME; if (!name && (hints->ai_flags & AI_CANONNAME)) return EAI_BADFLAGS; + if (service) { + if (hints->ai_socktype == SOCK_STREAM) + strncpy(proto, "tcp", sizeof(proto)); + else if (hints->ai_socktype == SOCK_DGRAM) + strncpy(proto, "udp", sizeof(proto)); + else /* ai_socktype == 0 */ + proto[0] = '\0'; + + if ((port = LookupServicesByName(service, proto, sizeof(proto), NULL, 0, + NULL)) == -1) { + if ((port = parseport(service)) == -1) return EAI_NONAME; + } + } if (!(ai = newaddrinfo(port))) return EAI_MEMORY; if (service) ai->ai_addr4->sin_port = htons(port); if (hints) { diff --git a/libc/dns/getnameinfo.c b/libc/dns/getnameinfo.c index 3cbbc860c..10a62f5ae 100644 --- a/libc/dns/getnameinfo.c +++ b/libc/dns/getnameinfo.c @@ -31,6 +31,7 @@ #include "libc/dns/dns.h" #include "libc/dns/hoststxt.h" #include "libc/dns/resolvconf.h" +#include "libc/dns/servicestxt.h" #include "libc/fmt/conv.h" #include "libc/fmt/fmt.h" #include "libc/fmt/itoa.h" @@ -58,7 +59,7 @@ int getnameinfo(const struct sockaddr *addr, socklen_t addrlen, char *name, socklen_t namelen, char *service, socklen_t servicelen, int flags) { char *p, rdomain[1 + sizeof "255.255.255.255.in-addr.arpa"]; - char info[512]; + char info[NI_MAXHOST + 1]; int rc, port; uint8_t *ip; unsigned int valid_flags; @@ -102,8 +103,10 @@ int getnameinfo(const struct sockaddr *addr, socklen_t addrlen, char *name, port = ntohs(((struct sockaddr_in *)addr)->sin_port); info[0] = '\0'; if (service != NULL && servicelen != 0) { - itoa(port, info, 10); - /* TODO: reverse lookup on /etc/services to get name of service */ + if ((flags & NI_NUMERICSERV) || + LookupServicesByPort(port, ((flags & NI_DGRAM) ? "udp" : "tcp"), 4, + info, sizeof(info), NULL) == -1) + itoa(port, info, 10); if (strlen(info) + 1 > servicelen) return EAI_OVERFLOW; strcpy(service, info); }