mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-05 17:30:27 +00:00
Service lookups in getaddrinfo/getnameinfo
getaddrinfo now calls LookupServicesByName to find the port number for the provided service string, with a fallback to parseport as earlier. getnameinfo now calls LookupServicesByPort to find the service name for the obtained port number, with a fallback to itoa as earlier.
This commit is contained in:
parent
2c8f1efc80
commit
1b849d5c49
2 changed files with 25 additions and 4 deletions
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue