Add inet_ntop(3).
This commit is contained in:
parent
e5eed18cf4
commit
ba3b6d386e
|
@ -16,6 +16,7 @@ CXXFLAGS=-std=gnu++11 -fno-exceptions -fno-rtti
|
||||||
ASFLAGS=
|
ASFLAGS=
|
||||||
|
|
||||||
FREEOBJS=\
|
FREEOBJS=\
|
||||||
|
arpa/inet/inet_ntop.o \
|
||||||
assert/__assert.o \
|
assert/__assert.o \
|
||||||
c++/c++.o \
|
c++/c++.o \
|
||||||
c++/op-new.o \
|
c++/op-new.o \
|
||||||
|
@ -316,7 +317,6 @@ wctype/wctype.o \
|
||||||
HOSTEDOBJS=\
|
HOSTEDOBJS=\
|
||||||
arpa/inet/inet_addr.o \
|
arpa/inet/inet_addr.o \
|
||||||
arpa/inet/inet_ntoa.o \
|
arpa/inet/inet_ntoa.o \
|
||||||
arpa/inet/inet_ntop.o \
|
|
||||||
arpa/inet/inet_pton.o \
|
arpa/inet/inet_pton.o \
|
||||||
blf/blowfish.o \
|
blf/blowfish.o \
|
||||||
$(CPUDIR)/fork.o \
|
$(CPUDIR)/fork.o \
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2013 Jonas 'Sortie' Termansen.
|
* Copyright (c) 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
|
||||||
|
@ -14,25 +14,48 @@
|
||||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
*
|
*
|
||||||
* arpa/inet/inet_ntop.c
|
* arpa/inet/inet_ntop.c
|
||||||
* Internet address manipulation routines.
|
* Convert network address to string.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
const char* inet_ntop(int af,
|
const char* inet_ntop(int af,
|
||||||
const void* restrict src,
|
const void* restrict src,
|
||||||
char* restrict dst,
|
char* restrict dst,
|
||||||
socklen_t size)
|
socklen_t size)
|
||||||
{
|
{
|
||||||
(void) af;
|
if ( af == AF_INET )
|
||||||
(void) src;
|
{
|
||||||
(void) dst;
|
const unsigned char* ip = (const unsigned char*) src;
|
||||||
(void) size;
|
int len = snprintf(dst, size, "%u.%u.%u.%u",
|
||||||
fprintf(stderr, "%s is not implemented yet, aborting.\n", __func__);
|
ip[0], ip[1], ip[2], ip[3]);
|
||||||
abort();
|
if ( len < 0 )
|
||||||
|
return NULL;
|
||||||
|
if ( size <= (size_t) len )
|
||||||
|
return errno = ENOSPC, (const char*) NULL;
|
||||||
|
return dst;
|
||||||
|
}
|
||||||
|
else if ( af == AF_INET6 )
|
||||||
|
{
|
||||||
|
// TODO: Support for :: syntax.
|
||||||
|
// TODO: Support for x:x:x:x:x:x:d.d.d.d syntax.
|
||||||
|
const unsigned char* ip = (const unsigned char*) src;
|
||||||
|
int len = snprintf(dst, size, "%x:%x:%x:%x:%x:%x:%x:%x",
|
||||||
|
ip[0] << 8 | ip[1], ip[2] << 8 | ip[3],
|
||||||
|
ip[4] << 8 | ip[5], ip[6] << 8 | ip[7],
|
||||||
|
ip[8] << 8 | ip[9], ip[10] << 8 | ip[11],
|
||||||
|
ip[12] << 8 | ip[13], ip[14] << 8 | ip[15]);
|
||||||
|
if ( len < 0 )
|
||||||
|
return NULL;
|
||||||
|
if ( size <= (size_t) len )
|
||||||
|
return errno = ENOSPC, (const char*) NULL;
|
||||||
|
return dst;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return errno = EAFNOSUPPORT, NULL;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue