From 2754fab78dc7829c2cb44225e80abdc035290cc8 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Fri, 11 Feb 2011 13:05:00 +0100 Subject: [PATCH] Support for scope in $v6addrs --- src/conky.cc | 3 +++ src/linux.cc | 23 +++++++++++++++++++++-- src/net_stat.cc | 12 +++++++++++- src/net_stat.h | 2 ++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/conky.cc b/src/conky.cc index ce9ee7df..1e2f0e4a 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -251,6 +251,9 @@ static void print_version(void) #ifdef BUILD_HTTP << _(" * HTTP\n") #endif +#ifdef BUILD_IPV6 + << _(" * IPv6\n") +#endif /* BUILD_IPV6 */ #ifdef BUILD_IRC << _(" * IRC\n") #endif diff --git a/src/linux.cc b/src/linux.cc index 5b5a5404..8db40a69 100644 --- a/src/linux.cc +++ b/src/linux.cc @@ -575,11 +575,11 @@ int update_net_stats(void) FILE *file; char v6addr[32]; char devname[21]; - unsigned int netmask; + unsigned int netmask, scope; struct net_stat *ns; struct v6addr *lastv6; if ((file = fopen(PROCDIR"/net/if_inet6", "r")) != NULL) { - while (fscanf(file, "%32s %*02x %02x %*02x %*02x %20s\n", v6addr, &netmask, devname) != EOF) { + while (fscanf(file, "%32s %*02x %02x %02x %*02x %20s\n", v6addr, &netmask, &scope, devname) != EOF) { ns = get_net_stat(devname, NULL, NULL); if(ns->v6addrs == NULL) { lastv6 = (struct v6addr *) malloc(sizeof(struct v6addr)); @@ -592,6 +592,25 @@ int update_net_stats(void) } strncpy(lastv6->addr, v6addr, 32); lastv6->netmask = netmask; + switch(scope) { + case 0: //global + lastv6->scope = 'G'; + break; + case 16: //host-local + lastv6->scope = 'H'; + break; + case 32: //link-local + lastv6->scope = 'L'; + break; + case 64: //site-local + lastv6->scope = 'S'; + break; + case 128: //compat + lastv6->scope = 'C'; + break; + default: + lastv6->scope = '?'; + } lastv6->next = NULL; } } diff --git a/src/net_stat.cc b/src/net_stat.cc index c3aa101b..4d8a44b5 100644 --- a/src/net_stat.cc +++ b/src/net_stat.cc @@ -91,6 +91,7 @@ struct net_stat *get_net_stat(const char *dev, void *free_at_crash1, void *free_ void parse_net_stat_arg(struct text_object *obj, const char *arg, void *free_at_crash) { bool shownetmask = false; + bool showscope = false; char dev[21]; //a netdev can only be 20 chars long int i=0; @@ -100,6 +101,7 @@ void parse_net_stat_arg(struct text_object *obj, const char *arg, void *free_at_ if (*arg == '-') { //there are flags for(i=1; arg[i] != ' ' && arg[i] != 0; i++) { if(arg[i]=='n') shownetmask = true; + if(arg[i]=='s') showscope = true; } } sscanf(arg+i, "%20s", dev); @@ -108,6 +110,7 @@ void parse_net_stat_arg(struct text_object *obj, const char *arg, void *free_at_ struct net_stat *netstat = get_net_stat(dev, obj, free_at_crash); #ifdef BUILD_IPV6 netstat->v6show_nm = shownetmask; + netstat->v6show_sc = showscope; #endif /* BUILD_IPV6 */ obj->data.opaque = netstat; } @@ -233,7 +236,9 @@ void print_v6addrs(struct text_object *obj, char *p, int p_max_size) return; if( ! ns->v6addrs) { - if(ns->v6show_nm) strncpy(p, "::/128", p_max_size); else strncpy(p, "::", p_max_size); + strncpy(p, "::", p_max_size); + if(ns->v6show_nm) strcat(p, "/128"); + if(ns->v6show_sc) strcat(p, "(/)"); return; } while(current_v6) { @@ -275,6 +280,11 @@ void print_v6addrs(struct text_object *obj, char *p, int p_max_size) strcpy(current_char, netmaskstr); current_char += strlen(netmaskstr); } + //scope + if(ns->v6show_sc) { + sprintf(current_char, "(%c)", current_v6->scope); + current_char += 3; + } //next (or last) address current_v6 = current_v6->next; if(current_v6) { diff --git a/src/net_stat.h b/src/net_stat.h index bbeed45a..3838deef 100644 --- a/src/net_stat.h +++ b/src/net_stat.h @@ -37,6 +37,7 @@ struct v6addr { char addr[32]; unsigned int netmask; + char scope; struct v6addr *next; }; #endif /* BUILD_IPV6 */ @@ -51,6 +52,7 @@ struct net_stat { #ifdef BUILD_IPV6 struct v6addr *v6addrs; bool v6show_nm; + bool v6show_sc; #endif /* BUILD_IPV6 */ #if defined(__linux__) char addrs[273];