smol bug fixes

This commit is contained in:
malloc 2019-02-01 10:08:16 -06:00
parent ffe0d9d9f8
commit 8e80275652
2 changed files with 11 additions and 12 deletions

View file

@ -2,8 +2,8 @@
#include "util/ipaddr.h" #include "util/ipaddr.h"
int main(int argc, char** argv) { 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"); 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; return 0;
} }

View file

@ -215,10 +215,10 @@ char* glv_ipv6_ntoa(const ipaddr_t* addr) {
return NULL; return NULL;
char* ip = malloc(43 * sizeof(char)); 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) { for(i = 0; i < 8; ++i) {
if(addr->addr[i] == 0) if(addr->addr[i] == 0 && i < 7)
++curr_gap_len; ++curr_gap_len;
else { else {
if(curr_gap_len > largest_gap_len && curr_gap_len > 1) { 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) { for(i = 0; i < 8; ++i) {
if(i == 7) if(i == largest_gap_pos) {
sprintf(ip, "%s%x", ip, addr->addr[i]); sprintf(ip, (i == 0 ? "::" : "%s:") , ip);
else { i += largest_gap_len - 1;
if(i == largest_gap_pos) { jmp = 1;
sprintf(ip, "%s:", ip); } else if(!(jmp && i == 7)) {
i += largest_gap_len - 1; sprintf(ip, (i == 7 ? "%s%x" : "%s%x:"), ip, addr->addr[i]);
} else jmp = 0;
sprintf(ip, "%s%x:", ip, addr->addr[i]);
} }
} }