Fix IPv4 edge cases for inet_pton()

This commit is contained in:
staphen 2021-06-24 08:53:24 -04:00
parent 3c9a623b24
commit 5dfbe21747
No known key found for this signature in database
GPG Key ID: E6D757EEF0CE235F

View File

@ -1,4 +1,5 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <ctype.h> #include <ctype.h>
@ -10,7 +11,7 @@ const struct in6_addr in6addr_any = {0};
const struct in6_addr in6addr_loopback = {.__u6_addr32 = {0, 0, 0, __builtin_bswap32(1)}}; const struct in6_addr in6addr_loopback = {.__u6_addr32 = {0, 0, 0, __builtin_bswap32(1)}};
// Adapted from libctru // Adapted from libctru
static int _inetAtonDetail(int *outBase, size_t *outNumBytes, const char *cp, struct in_addr *inp) { static int _inetAtonDetail(const char *cp, struct in_addr *inp) {
int base; int base;
uint32_t val; uint32_t val;
int c; int c;
@ -56,11 +57,7 @@ static int _inetAtonDetail(int *outBase, size_t *outNumBytes, const char *cp, st
else break; else break;
} }
if(c != 0) { if(c != 0) return 0;
*outNumBytes = num_bytes;
*outBase = base;
return 0;
}
switch(num_bytes) { switch(num_bytes) {
case 0: case 0:
@ -88,9 +85,6 @@ static int _inetAtonDetail(int *outBase, size_t *outNumBytes, const char *cp, st
if(inp) if(inp)
inp->s_addr = htonl(val); inp->s_addr = htonl(val);
*outNumBytes = num_bytes;
*outBase = base;
return 1; return 1;
} }
@ -126,12 +120,13 @@ static const char *inet_ntop4(const void *src, char *dst, socklen_t size) {
return dst; return dst;
} }
// Adapted from libctru
static int inet_pton4(const char *src, void *dst) { static int inet_pton4(const char *src, void *dst) {
int base; char ip[4];
size_t numBytes; if(sscanf(src,"%hhu.%hhu.%hhu.%hhu",&ip[0], &ip[1], &ip[2], &ip[3]) != 4) return 0;
int ret = _inetAtonDetail(&base, &numBytes, src, (struct in_addr *)dst); memcpy(dst,ip,4);
return (ret == 1 && base == 10 && numBytes == 3) ? 1 : 0; return 1;
} }
/* Copyright (c) 1996 by Internet Software Consortium. /* Copyright (c) 1996 by Internet Software Consortium.
@ -392,9 +387,7 @@ char *inet_ntoa(struct in_addr in) {
} }
int inet_aton(const char *cp, struct in_addr *inp) { int inet_aton(const char *cp, struct in_addr *inp) {
int base; return _inetAtonDetail(cp, inp);
size_t numBytes;
return _inetAtonDetail(&base, &numBytes, cp, inp);
} }
in_addr_t inet_addr(const char *cp) { in_addr_t inet_addr(const char *cp) {