mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Add inet_pton(3).
This commit is contained in:
parent
ba3b6d386e
commit
5de36cf449
2 changed files with 77 additions and 12 deletions
|
@ -17,6 +17,7 @@ ASFLAGS=
|
|||
|
||||
FREEOBJS=\
|
||||
arpa/inet/inet_ntop.o \
|
||||
arpa/inet/inet_pton.o \
|
||||
assert/__assert.o \
|
||||
c++/c++.o \
|
||||
c++/op-new.o \
|
||||
|
@ -317,7 +318,6 @@ wctype/wctype.o \
|
|||
HOSTEDOBJS=\
|
||||
arpa/inet/inet_addr.o \
|
||||
arpa/inet/inet_ntoa.o \
|
||||
arpa/inet/inet_pton.o \
|
||||
blf/blowfish.o \
|
||||
$(CPUDIR)/fork.o \
|
||||
$(CPUDIR)/setjmp.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
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
|
@ -14,21 +14,86 @@
|
|||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* arpa/inet/inet_pton.c
|
||||
* Internet address manipulation routines.
|
||||
* Convert string to network address.
|
||||
*/
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <string.h>
|
||||
|
||||
int inet_pton(int af, const char* restrict src, void* restrict dst)
|
||||
{
|
||||
(void) af;
|
||||
(void) src;
|
||||
(void) dst;
|
||||
fprintf(stderr, "%s is not implemented yet, aborting.\n", __func__);
|
||||
abort();
|
||||
if ( af == AF_INET )
|
||||
{
|
||||
unsigned char ip[4];
|
||||
size_t index = 0;
|
||||
for ( int i = 0; i < 4; i++ )
|
||||
{
|
||||
if ( i && src[index++] != '.' )
|
||||
return 0;
|
||||
if ( src[index] < '0' || '9' < src[index] )
|
||||
return 0;
|
||||
int num = src[index++] - '0';
|
||||
if ( num )
|
||||
{
|
||||
if ( '0' <= src[index] && src[index] <= '9' )
|
||||
{
|
||||
num = 10 * num + src[index++] - '0';
|
||||
if ( '0' <= src[index] && src[index] <= '9' )
|
||||
{
|
||||
num = 10 * num + src[index++] - '0';
|
||||
if ( 256 <= num )
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
ip[i] = num;
|
||||
}
|
||||
if ( src[index] )
|
||||
return 0;
|
||||
memcpy(dst, ip, sizeof(ip));
|
||||
return 1;
|
||||
}
|
||||
else if ( af == AF_INET6 )
|
||||
{
|
||||
unsigned char ip[16];
|
||||
size_t index = 0;
|
||||
// TODO: Support for :: syntax.
|
||||
// TODO: Support for x:x:x:x:x:x:d.d.d.d syntax.
|
||||
for ( int i = 0; i < 8; i++ )
|
||||
{
|
||||
if ( i && src[index++] != ':' )
|
||||
return 0;
|
||||
int num = 0;
|
||||
for ( int j = 0; j < 4; j++ )
|
||||
{
|
||||
int dgt;
|
||||
if ( '0' <= src[index] && src[index] <= '9' )
|
||||
dgt = src[index] - '0';
|
||||
else if ( 'a' <= src[index] && src[index] <= 'f' )
|
||||
dgt = src[index] - 'a' + 10;
|
||||
else if ( 'A' <= src[index] && src[index] <= 'F' )
|
||||
dgt = src[index] - 'A' + 10;
|
||||
else
|
||||
{
|
||||
if ( j == 0 )
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
num = num << 4 | dgt;
|
||||
index++;
|
||||
}
|
||||
ip[2 * i + 0] = num >> 8 & 0xFF;
|
||||
ip[2 * i + 1] = num >> 0 & 0xFF;
|
||||
}
|
||||
if ( src[index] )
|
||||
return 0;
|
||||
memcpy(dst, ip, sizeof(ip));
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
return errno = EAFNOSUPPORT, -1;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue