--- libaitio/src/Attic/tools.c 2011/06/07 11:49:39 1.5 +++ libaitio/src/Attic/tools.c 2011/10/31 13:53:51 1.6 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: tools.c,v 1.5 2011/06/07 11:49:39 misho Exp $ +* $Id: tools.c,v 1.6 2011/10/31 13:53:51 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -294,4 +294,56 @@ io_FreeNullTerm(char *** __restrict arr) free(*arr); *arr = NULL; } +} + +/* + * io_ether_ntoa() Convert ethernet address to string + * @n = ethernet address structure, like struct ether_addr + * @a = string + * @len = string length + * return: NULL error or !=NULL string a + */ +inline char * +io_ether_ntoa(const struct io_ether_addr *n, char * __restrict a, int len) +{ + if (!n || !a) + return NULL; + + memset(a, 0, len); + if (snprintf(a, len, "%02x:%02x:%02x:%02x:%02x:%02x", + n->ether_addr_octet[0], n->ether_addr_octet[1], + n->ether_addr_octet[2], n->ether_addr_octet[3], + n->ether_addr_octet[4], n->ether_addr_octet[5]) < 17) + return NULL; + + return a; +} + +/* + * io_ether_aton() Convert string to ethernet address + * @a = string + * @e = ethernet address structure, like struct ether_addr + * return: NULL error or !=NULL ethernet address structure + */ +inline struct io_ether_addr * +io_ether_aton(const char *a, struct io_ether_addr *e) +{ + int i; + u_int o0, o1, o2, o3, o4, o5; + + if (!a || !e) + return NULL; + + i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2, &o3, &o4, &o5); + if (i != 6) + return NULL; + + e->ether_addr_octet[0] = o0; + e->ether_addr_octet[1] = o1; + e->ether_addr_octet[2] = o2; + e->ether_addr_octet[3] = o3; + e->ether_addr_octet[4] = o4; + e->ether_addr_octet[5] = o5; + + return e; }