From: Marcel Holtmann Date: Thu, 11 Dec 2008 15:57:12 +0000 (+0100) Subject: Add common index to ident conversion helper X-Git-Tag: accepted/2.0alpha-wayland/20121110.002834~4492 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6fc878f74ce6d37260f29c782cd65a47483083e4;p=profile%2Fivi%2Fconnman.git Add common index to ident conversion helper --- diff --git a/plugins/inet.c b/plugins/inet.c index e14d33e..ab0fcdc 100644 --- a/plugins/inet.c +++ b/plugins/inet.c @@ -26,10 +26,12 @@ #include #include #include +#include #include #include #include #include +#include #include "inet.h" @@ -57,3 +59,49 @@ char *inet_index2name(int index) return strdup(ifr.ifr_name); } + +char *inet_index2ident(int index, const char *prefix) +{ + struct ifreq ifr; + struct ether_addr *eth; + char *str; + int sk, err, len; + + if (index < 0) + return NULL; + + sk = socket(PF_INET, SOCK_DGRAM, 0); + if (sk < 0) + return NULL; + + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_ifindex = index; + + err = ioctl(sk, SIOCGIFNAME, &ifr); + + if (err == 0) + err = ioctl(sk, SIOCGIFHWADDR, &ifr); + + close(sk); + + if (err < 0) + return NULL; + + len = prefix ? strlen(prefix) + 18 : 18; + + str = malloc(len); + if (!str) + return NULL; + + eth = (void *) &ifr.ifr_hwaddr.sa_data; + snprintf(str, len, "%s%02X_%02X_%02X_%02X_%02X_%02X", + prefix ? prefix : "", + eth->ether_addr_octet[0], + eth->ether_addr_octet[1], + eth->ether_addr_octet[2], + eth->ether_addr_octet[3], + eth->ether_addr_octet[4], + eth->ether_addr_octet[5]); + + return str; +} diff --git a/plugins/inet.h b/plugins/inet.h index aafd55b..afa8025 100644 --- a/plugins/inet.h +++ b/plugins/inet.h @@ -20,3 +20,4 @@ */ char *inet_index2name(int index); +char *inet_index2ident(int index, const char *prefix);