added inet_pton and inet_ntop
This commit is contained in:
parent
389807c6a3
commit
45ff5c4ee6
@ -31,6 +31,9 @@ extern "C" {
|
||||
int inet_aton(const char *cp, struct in_addr *inp);
|
||||
char* inet_ntoa(struct in_addr in);
|
||||
|
||||
const char *inet_ntop(int af, const void *restrict src, char *restrict dst, socklen_t size);
|
||||
int inet_pton(int af, const char *restrict src, void *restrict dst);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
28
libctru/source/services/soc/soc_inet_ntop.c
Normal file
28
libctru/source/services/soc/soc_inet_ntop.c
Normal file
@ -0,0 +1,28 @@
|
||||
#include "soc_common.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
static const char *inet_ntop4(const void *restrict src, char *restrict dst, socklen_t size)
|
||||
{
|
||||
const u8 * ip = src;
|
||||
if(size < INET_ADDRSTRLEN)
|
||||
{
|
||||
errno = ENOSPC;
|
||||
return NULL;
|
||||
}
|
||||
snprintf(dst,size,"%hhu.%hhu.%hhu.%hhu",ip[0], ip[1], ip[2], ip[3]);
|
||||
return dst;
|
||||
}
|
||||
|
||||
|
||||
const char *inet_ntop(int af, const void *restrict src, char *restrict dst, socklen_t size)
|
||||
{
|
||||
if(af == AF_INET)
|
||||
{
|
||||
return inet_ntop4(src,dst,size);
|
||||
}
|
||||
// only support IPv4
|
||||
errno = EAFNOSUPPORT;
|
||||
return NULL;
|
||||
}
|
24
libctru/source/services/soc/soc_inet_pton.c
Normal file
24
libctru/source/services/soc/soc_inet_pton.c
Normal file
@ -0,0 +1,24 @@
|
||||
#include "soc_common.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
static int inet_pton4(const char *restrict src, void *restrict dst)
|
||||
{
|
||||
u8 ip[4];
|
||||
if(sscanf(src,"%hhu.%hhu.%hhu.%hhu",&ip[0], &ip[1], &ip[2], &ip[3]) != 4) return 0;
|
||||
*(u32*)dst = *(u32*)ip;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int inet_pton(int af, const char *restrict src, void *restrict dst)
|
||||
{
|
||||
if(af == AF_INET)
|
||||
{
|
||||
return inet_pton4(src,dst);
|
||||
}
|
||||
// only support IPv4
|
||||
errno = EAFNOSUPPORT;
|
||||
return -1;
|
||||
}
|
Loading…
Reference in New Issue
Block a user