Add getnameinfo(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2016-11-27 21:20:03 +01:00
parent 5de36cf449
commit 50266bb724
1 changed files with 49 additions and 11 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013 Jonas 'Sortie' Termansen. * Copyright (c) 2013, 2016 Jonas 'Sortie' Termansen.
* *
* Permission to use, copy, modify, and distribute this software for any * Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -17,10 +17,16 @@
* Address-to-name translation in protocol-independent manner. * Address-to-name translation in protocol-independent manner.
*/ */
#include <netdb.h> #include <sys/socket.h>
#include <arpa/inet.h>
#include <endian.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
int getnameinfo(const struct sockaddr* restrict sa, int getnameinfo(const struct sockaddr* restrict sa,
socklen_t salen, socklen_t salen,
@ -30,13 +36,45 @@ int getnameinfo(const struct sockaddr* restrict sa,
socklen_t servlen, socklen_t servlen,
int flags) int flags)
{ {
(void) sa; if ( flags & ~(NI_NOFQDN | NI_NUMERICHOST | NI_NAMEREQD | NI_NUMERICSERV |
(void) salen; NI_NUMERICSCOPE | NI_DGRAM) )
(void) host; return EAI_BADFLAGS;
(void) hostlen; int af;
(void) serv; if ( salen == sizeof(struct sockaddr_in) &&
(void) servlen; ((struct sockaddr_in*) sa)->sin_family == AF_INET )
(void) flags; af = AF_INET;
fprintf(stderr, "%s is not implemented, aborting.\n", __func__); else if ( salen == sizeof(struct sockaddr_in6) &&
abort(); ((struct sockaddr_in6*) sa)->sin6_family == AF_INET6 )
af = AF_INET6;
else
return EAI_FAMILY;
if ( !host && !serv )
return EAI_NONAME;
if ( host )
{
if ( flags & NI_NAMEREQD )
return EAI_NONAME;
const void* addr;
if ( af == AF_INET )
addr = &(((struct sockaddr_in*) sa)->sin_addr);
else if ( af == AF_INET6 )
addr = &(((struct sockaddr_in6*) sa)->sin6_addr);
else
return EAI_FAMILY;
if ( !inet_ntop(af, addr, host, hostlen) )
return errno == ENOSPC ? EAI_OVERFLOW : EAI_NONAME;
}
if ( serv )
{
in_port_t port;
if ( af == AF_INET )
port = be16toh(((struct sockaddr_in*) sa)->sin_port);
else if ( af == AF_INET6 )
port = be16toh(((struct sockaddr_in6*) sa)->sin6_port);
else
return EAI_FAMILY;
if ( servlen <= (size_t) snprintf(serv, servlen, "%u", port) )
return EAI_OVERFLOW;
}
return 0;
} }