From 35666ca5c6031b74605cee0a7ffb8d5359a8f46c Mon Sep 17 00:00:00 2001 From: Joshua Drake Date: Thu, 3 Dec 2015 13:52:51 -0800 Subject: [PATCH] Imported inet_pton from musl libc for Cygwin compilation --- p0f-client-exim.c | 89 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 17 deletions(-) diff --git a/p0f-client-exim.c b/p0f-client-exim.c index 9d2711c..ea91e84 100644 --- a/p0f-client-exim.c +++ b/p0f-client-exim.c @@ -32,27 +32,83 @@ #include "../debug.h" #include "../api.h" -/* Parse IPv4 address into a buffer. */ - -static void parse_addr4(char* str, u8* ret) { - - u32 a1, a2, a3, a4; - - if (sscanf(str, "%u.%u.%u.%u", &a1, &a2, &a3, &a4) != 4) { - SAYF("Malformed IPv4 address."); - exit(1); +#ifdef CYGWIN + +#define AF_INET 0 +#define AF_INET6 1 + +//From musl libc +int inet_pton(int af, const char *restrict s, void *restrict a0) +{ + uint16_t ip[8]; + unsigned char *a = a0; + int i, j, v, d, brk=-1, need_v4=0; + + if (af==AF_INET) { + for (i=0; i<4; i++) { + for (v=j=0; j<3 && isdigit(s[j]); j++) + v = 10*v + s[j]-'0'; + if (j==0 || (j>1 && s[0]=='0') || v>255) return 0; + a[i] = v; + if (s[j]==0 && i==3) return 1; + if (s[j]!='.') return 0; + s += j+1; + } + return 0; + } else if (af!=AF_INET6) { + errno = EAFNOSUPPORT; + return -1; } - if (a1 > 255 || a2 > 255 || a3 > 255 || a4 > 255) { - SAYF("Malformed IPv4 address."); - exit(1); + if (*s==':' && *++s!=':') return 0; + + for (i=0; ; i++) { + if (s[0]==':' && brk<0) { + brk=i; + ip[i&7]=0; + if (!*++s) break; + if (i==7) return 0; + continue; + } + for (v=j=0; j<4 && (d=hexval(s[j]))>=0; j++) + v=16*v+d; + if (j==0) return 0; + ip[i&7] = v; + if (!s[j] && (brk>=0 || i==7)) break; + if (i==7) return 0; + if (s[j]!=':') { + if (s[j]!='.' || (i<6 && brk<0)) return 0; + need_v4=1; + i++; + break; + } + s += j+1; + } + if (brk>=0) { + memmove(ip+brk+7-i, ip+brk, 2*(i+1-brk)); + for (j=0; j<7-i; j++) ip[brk+j] = 0; + } + for (j=0; j<8; j++) { + *a++ = ip[j]>>8; + *a++ = ip[j]; } + if (need_v4 && inet_pton(AF_INET, (void *)s, a-4) <= 0) return 0; + return 1; +} +#endif - ret[0] = a1; - ret[1] = a2; - ret[2] = a3; - ret[3] = a4; +/* Parse IPv4 address into a buffer. */ +static void parse_addr4(char* str, u8* ret) { + struct in_addr ip; + int8_t r = inet_pton(AF_INET6, str, &ip); + if (r == -1) { + SAYF("parse_addr4: error while converting IPv6 address to binary format: %s", strerror(errno)); + } + else if (r == 0) { + SAYF("parse_addr4: passed invalid IPv4 address"); + } + memcpy(ret, &ip, 4); } @@ -72,7 +128,6 @@ static void parse_addr6(char* str, u8* ret) { return; } - int main(int argc, char** argv) { u8 tmp[128]; -- 2.25.1