From 52b56873381e22463dd39df61c8a17f23dfa30d5 Mon Sep 17 00:00:00 2001 From: Joshua Drake Date: Mon, 30 Nov 2015 13:55:03 -0800 Subject: [PATCH] Replaced parse_addr6 with inet_pton --- p0f-client-exim.c | 44 ++++++++++++++++---------------------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/p0f-client-exim.c b/p0f-client-exim.c index 73a3a12..6e00ac3 100644 --- a/p0f-client-exim.c +++ b/p0f-client-exim.c @@ -59,37 +59,25 @@ static void parse_addr4(char* str, u8* ret) { /* Parse IPv6 address into a buffer. */ static void parse_addr6(char* str, u8* ret) { - - u32 seg = 0; - u32 val; - - while (*str) { - - if (seg == 8) { - SAYF("Malformed IPv6 address (too many segments)."); - exit(1); - } - - if (*str == ':') { val = 0; } - else if (sscanf((char*)str, "%x", &val) != 1 || val > 65535) { - SAYF("Malformed IPv6 address (bad octet value)."); - exit(1); - } - - ret[seg * 2] = val >> 8; - ret[seg * 2 + 1] = val; - - seg++; - - while (isxdigit(*str)) str++; - if (*str) str++; - + struct in6_addr ip; + int8_t r = inet_pton(AF_INET6, str, &ip); + if (r == -1) { + SAYF("parse_addr6: error while converting IPv6 address to binary format: %s", strerror(errno)); + } + else if (r == 0) { + SAYF("parse_addr6: passed invalid IPv6 address"); } + memcpy(ret, &ip, 16); - if (seg < 6 || seg > 8) { - SAYF("Malformed IPv6 address (incorrect number of segments parsed)"); - exit(1); +#ifdef _DEBUG + u8 i; + for (i=0; i<8; i++) { + SAYF("%x ", ret[i]); } + SAYF("\n"); +#endif + + return; } -- 2.25.1