diff --git a/src/server/main.c b/src/server/main.c index 0ef2347..4bc33f2 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -2,8 +2,8 @@ #include "util/ipaddr.h" int main(int argc, char** argv) { - ipaddr_t a = glv_ip_aton("1234:5678::b00b:d00d:def0/62"); + ipaddr_t a = glv_ip_aton("127.0.0.1/24"); ipaddr_t b = glv_ip_aton("1234:5678::b00b:b00b:def0"); - char* out = glv_ip_ntoa(&a); + char* out = glv_ipv6_ntoa(&a); return 0; } \ No newline at end of file diff --git a/src/server/util/ipaddr.c b/src/server/util/ipaddr.c index fc68d99..6abcd55 100644 --- a/src/server/util/ipaddr.c +++ b/src/server/util/ipaddr.c @@ -215,10 +215,10 @@ char* glv_ipv6_ntoa(const ipaddr_t* addr) { return NULL; char* ip = malloc(43 * sizeof(char)); - int i, largest_gap_pos = -1, largest_gap_len = 0, curr_gap_len = 0; + int i, jmp = 0, largest_gap_pos = -1, largest_gap_len = 0, curr_gap_len = 0; for(i = 0; i < 8; ++i) { - if(addr->addr[i] == 0) + if(addr->addr[i] == 0 && i < 7) ++curr_gap_len; else { if(curr_gap_len > largest_gap_len && curr_gap_len > 1) { @@ -231,14 +231,13 @@ char* glv_ipv6_ntoa(const ipaddr_t* addr) { } for(i = 0; i < 8; ++i) { - if(i == 7) - sprintf(ip, "%s%x", ip, addr->addr[i]); - else { - if(i == largest_gap_pos) { - sprintf(ip, "%s:", ip); - i += largest_gap_len - 1; - } else - sprintf(ip, "%s%x:", ip, addr->addr[i]); + if(i == largest_gap_pos) { + sprintf(ip, (i == 0 ? "::" : "%s:") , ip); + i += largest_gap_len - 1; + jmp = 1; + } else if(!(jmp && i == 7)) { + sprintf(ip, (i == 7 ? "%s%x" : "%s%x:"), ip, addr->addr[i]); + jmp = 0; } }