From e9aa46ab32a45bd7fc0ad32573d1db84f5049554 Mon Sep 17 00:00:00 2001 From: David Sommerseth Date: Fri, 30 Jul 2010 19:30:30 +0200 Subject: Improved IPv6 support As the IPv6 protocol allows a single device to have more than one IPv6 address, the previous implementation did not provide all IPv6 information. It would reject all except the last parsed IPv6 address. NOTE: This implementation will break the previous API. This change removes the ethtool.etherinfo.ipv6_address and ethtool.etherinfo.ipv6_netmask members. A new member is added, ethtool.etherinfo.ipv6_addresses (in plural). This contains a tupple list containing of ethtool.etherinfo_ipv6addr objects, one object for each configured IPv6 address on the device. These objects have the following members available: .address - The IPv6 address .netmask - The IPv6 netmask (in bit notation) .scope - A string with the IPv6 address scope Example code: import ethtool devs = ethtool.get_interfaces_info('eth0') for ip6 in devs[0].ipv6_addresses: print "[%s] %s/%i" % (ip6.scope, ip6.address, ip6.netmask) Signed-off-by: David Sommerseth --- python-ethtool/etherinfo.c | 77 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 14 deletions(-) (limited to 'python-ethtool/etherinfo.c') diff --git a/python-ethtool/etherinfo.c b/python-ethtool/etherinfo.c index 7ae5798..68cc2e6 100644 --- a/python-ethtool/etherinfo.c +++ b/python-ethtool/etherinfo.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include "etherinfo_struct.h" @@ -34,6 +35,30 @@ * Internal functions for working with struct etherinfo * */ +#define SET_STR_VALUE(dst, src) { \ + if( dst ) { \ + free(dst); \ + }; \ + dst = strdup(src); \ + } + + +void free_ipv6addresses(struct ipv6address *ptr) { + struct ipv6address *ipv6ptr = ptr; + + if( !ptr ) { + return; + } + + while( ipv6ptr ) { + struct ipv6address *tmp = ipv6ptr->next; + + free(ipv6ptr->address); + free(ipv6ptr); + ipv6ptr = tmp; + } +} + void free_etherinfo(struct etherinfo *ptr) { if( ptr == NULL ) { // Just for safety @@ -51,25 +76,35 @@ void free_etherinfo(struct etherinfo *ptr) if( ptr->ipv4_broadcast ) { free(ptr->ipv4_broadcast); } - if( ptr->ipv6_address ) { - free(ptr->ipv6_address); + if( ptr->ipv6_addresses ) { + free_ipv6addresses(ptr->ipv6_addresses); } free(ptr); } +struct ipv6address * etherinfo_add_ipv6(struct ipv6address *addrptr, const char *addr, int netmask, int scope) { + struct ipv6address *newaddr = NULL; + + newaddr = calloc(1, sizeof(struct ipv6address)+2); + if( !newaddr ) { + fprintf(stderr, "** ERROR ** Could not allocate memory for a new IPv6 address record (%s/%i [%i])", + addr, netmask, scope); + return addrptr; + } + + SET_STR_VALUE(newaddr->address, addr); + newaddr->netmask = netmask; + newaddr->scope = scope; + newaddr->next = addrptr; + return newaddr; +} + /* * libnl callback functions * */ -#define SET_STR_VALUE(dst, src) { \ - if( dst ) { \ - free(dst); \ - }; \ - dst = strdup(src); \ - } - static void callback_nl_link(struct nl_object *obj, void *arg) { struct etherinfo *ethi = (struct etherinfo *) arg; @@ -134,8 +169,10 @@ static void callback_nl_address(struct nl_object *obj, void *arg) SET_STR_VALUE(ethi->ipv4_broadcast, brdcst_str); } } else { - SET_STR_VALUE(ethi->ipv6_address, ip_str); - ethi->ipv6_netmask = rtnl_addr_get_prefixlen((struct rtnl_addr*) obj); + ethi->ipv6_addresses = etherinfo_add_ipv6(ethi->ipv6_addresses, + ip_str, + rtnl_addr_get_prefixlen((struct rtnl_addr*) obj), + rtnl_addr_get_scope((struct rtnl_addr*) obj)); } return; default: @@ -167,9 +204,17 @@ void dump_etherinfo(FILE *fp, struct etherinfo *ptr) } fprintf(fp, "\n"); } - if( ptr->ipv6_address ) { - fprintf(fp, "\tIPv6 address: %s/%i\n", - ptr->ipv6_address, ptr->ipv6_netmask); + if( ptr->ipv6_addresses ) { + struct ipv6address *ipv6 = ptr->ipv6_addresses; + + fprintf(fp, "\tIPv6 addresses:\n"); + for(; ipv6; ipv6 = ipv6->next) { + char scope[66]; + + rtnl_scope2str(ipv6->scope, scope, 64); + fprintf(fp, "\t [%s] %s/%i\n", + scope, ipv6->address, ipv6->netmask); + } } fprintf(fp, "\n"); } @@ -214,6 +259,10 @@ int get_etherinfo(struct etherinfo *ethinf, struct _nlconnection *nlc, nlQuery q break; case NLQRY_ADDR: + /* Remove old IPv6 information we might have */ + free_ipv6addresses(ethinf->ipv6_addresses); + ethinf->ipv6_addresses = NULL; + /* Extract IP address information */ addr_cache = rtnl_addr_alloc_cache(nlc->nlrt_handle); addr = rtnl_addr_alloc(); -- cgit