From 723255f142848c2b309bbb0c93242eb6e1a9ae6c Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Sat, 12 Feb 2011 17:18:33 +0100 Subject: [PATCH] Use inet_ntop(), thanks to pavelo for telling me about this --- src/linux.cc | 18 +++++++++++++++++- src/net_stat.cc | 35 ++++------------------------------- src/net_stat.h | 2 +- 3 files changed, 22 insertions(+), 33 deletions(-) diff --git a/src/linux.cc b/src/linux.cc index 8db40a69..fc343518 100644 --- a/src/linux.cc +++ b/src/linux.cc @@ -347,6 +347,21 @@ void print_gateway_ip(struct text_object *obj, char *p, int p_max_size) snprintf(p, p_max_size, "%s", gw_info.ip); } +u_int8_t hextobyte(const char in[2]) { + u_int8_t out=0; + char currentchar; + + for(int i=0; i<2; i++) { + currentchar=in[i]; + if(currentchar <= '9') currentchar -= '0'; + else if(currentchar <= 'A') currentchar -= 'A' - 10; + else currentchar -= 'a' - 10; + out<<=4; + out+=currentchar; + } + return out; +} + int update_net_stats(void) { FILE *net_dev_fp; @@ -590,7 +605,8 @@ int update_net_stats(void) lastv6->next = (struct v6addr *) malloc(sizeof(struct v6addr)); lastv6 = lastv6->next; } - strncpy(lastv6->addr, v6addr, 32); + for(int i=0; i<16; i++) + lastv6->addr.s6_addr[i]=hextobyte(v6addr+2*i); lastv6->netmask = netmask; switch(scope) { case 0: //global diff --git a/src/net_stat.cc b/src/net_stat.cc index 18eaca3c..11ecd07c 100644 --- a/src/net_stat.cc +++ b/src/net_stat.cc @@ -236,6 +236,7 @@ void print_v6addrs(struct text_object *obj, char *p, int p_max_size) { struct net_stat *ns = (struct net_stat *)obj->data.opaque; char *current_char = p; + char tempaddress[INET6_ADDRSTRLEN]; struct v6addr *current_v6 = ns->v6addrs; if (!ns) @@ -248,37 +249,9 @@ void print_v6addrs(struct text_object *obj, char *p, int p_max_size) return; } while(current_v6) { - char extracompress = 0; //0 until the first '0000', 1 after the first '0000', 2 after the first non-'0000' after a '0000' - for(int i=0; i<8; i++) { //loop trough the 8 parts of the ipv6 - //skip the zeros in front of each part - int j=0; - while(j<4) if(*(current_v6->addr+(i*4)+j) == '0') j++; else break; - if(j==4) { - if(*(current_v6->addr+(i*4)+3) == '0') { //4 zeros - switch(extracompress) { - case 0: //first time this happens, so replace by '::' - *current_char = ':'; - current_char++; - if(current_char == p+1) { //to make sure there is no ':::' when the first '0000' isn't at the beginning - *current_char = ':'; - current_char++; - } - extracompress=1; - continue; - case 1: //again, ignore them, they are included in the '::' - continue; - default: //again, but only remove the first 3 zero's because extra-compression may no longer be applied - j=3; //but if there are 4 zero's, keep the last - } - } else j=0; //no zeros to skip - } - if(extracompress == 1) extracompress = 2; - //place each part followed by : in the ip - strncpy(current_char, current_v6->addr+(i*4)+j, 4-j); - *(current_char+4-j)=':'; - current_char+=5-j; - } - current_char--; + inet_ntop(AF_INET6, &(current_v6->addr), tempaddress, INET6_ADDRSTRLEN); + strcpy(current_char, tempaddress); + current_char+=strlen(current_char); //netmask if(ns->v6show_nm) { char netmaskstr[5]; //max 5 chars (/128 + null-terminator) diff --git a/src/net_stat.h b/src/net_stat.h index 3838deef..5446785f 100644 --- a/src/net_stat.h +++ b/src/net_stat.h @@ -35,7 +35,7 @@ #ifdef BUILD_IPV6 struct v6addr { - char addr[32]; + struct in6_addr addr; unsigned int netmask; char scope; struct v6addr *next;