/* 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;
}