--- libelwix/inc/elwix/anet.h 2013/01/17 10:05:35 1.1 +++ libelwix/inc/elwix/anet.h 2020/05/27 15:03:28 1.16 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: anet.h,v 1.1 2013/01/17 10:05:35 misho Exp $ +* $Id: anet.h,v 1.16 2020/05/27 15:03:28 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 +Copyright 2004 - 2020 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -48,7 +48,7 @@ SUCH DAMAGE. struct e_ether_addr { - u_int8_t ether_addr_octet[6]; + u_int8_t octet[6]; }; typedef struct e_ether_addr ether_addr_t; @@ -58,19 +58,55 @@ typedef union { struct sockaddr_un sun; struct sockaddr_in sin; struct sockaddr_in6 sin6; +#ifndef __linux__ struct sockaddr_dl sdl; +#else + struct sockaddr_ll sll; +#endif } sockaddr_t; #define E_SOCKADDR_INIT { .ss = { 0 } } +#define E_SOCKADDR_MAX MIN(sizeof(sockaddr_t), 0xff) +typedef union { + struct in4_addr { + u_char s4_addr[sizeof(in_addr_t)]; + } in4; + struct in_addr in; + struct in6_addr in6; +} inaddr_t; +typedef struct { + sockaddr_t addr; + inaddr_t mask; +} netaddr_t; +#define E_CIDRMASK(x) htonl((((1 << (x)) - 1) << (32 - (x))) & 0xFFFFFFFF) + + /* * e_usleep() - usleep() replacement for ELWIX * * @usec = microseconds for sleep * return: -1 interrupted by signal or 0 ok */ -inline int e_usleep(unsigned int usec); +int e_usleep(unsigned int usec); +#ifndef __linux__ /* + * e_link_addr() - String ethernet address to link address + * + * @mac = ethernet address + * @sdl = link address + * return: -1 error or 0 ok + */ +int e_link_addr(const char *mac, struct sockaddr_dl * __restrict sdl); +/* + * e_link_ntoa() - String ethernet address from link address + * + * @sdl = link address + * return: =NULL error or !=NULL ethernet address, should be e_free() + */ +char *e_link_ntoa(const struct sockaddr_dl *sdl); +#endif +/* * e_ether_ntoa() - Convert ethernet address to string * * @n = ethernet address structure, like struct ether_addr @@ -78,7 +114,7 @@ inline int e_usleep(unsigned int usec); * @len = string length * return: NULL error or !=NULL string a */ -inline char *e_ether_ntoa(const struct e_ether_addr *n, char * __restrict a, int len); +char *e_ether_ntoa(const ether_addr_t * __restrict n, char * __restrict a, int len); /* * e_ether_aton() - Convert string to ethernet address * @@ -86,14 +122,14 @@ inline char *e_ether_ntoa(const struct e_ether_addr *n * @e = ethernet address structure, like struct ether_addr * return: NULL error or !=NULL ethernet address structure */ -inline struct e_ether_addr *e_ether_aton(const char *a, struct e_ether_addr *e); +ether_addr_t *e_ether_aton(const char *a, ether_addr_t * __restrict e); /* * e_n2port() - Extract port from network structure * * @addr = Address * return: 0 not supported family type or port number */ -inline unsigned short e_n2port(sockaddr_t * __restrict addr); +unsigned short e_n2port(sockaddr_t * __restrict addr); /* * e_n2addr() - Extract address from network structure * @@ -108,11 +144,18 @@ const char *e_n2addr(sockaddr_t * __restrict addr, ait * @psHost = Hostname * @port = Port * @addr = Network address structure - * return: NULL error or !=NULL network structure + * return: 0 is error or >0 length of network structure */ -sockaddr_t *e_gethostbyname(const char *psHost, unsigned short port, +socklen_t e_gethostbyname(const char *psHost, unsigned short port, sockaddr_t * __restrict addr); /* + * e_addrlen() - Get address length from network structure + * + * @addr = address + * return: 0 is error or >0 length of network structure + */ +socklen_t e_addrlen(const sockaddr_t *addr); +/* * e_addrcmp() - Compare network addresses * * @a = 1st address @@ -121,6 +164,77 @@ sockaddr_t *e_gethostbyname(const char *psHost, unsign * return: 0 is equal or !=0 is different */ int e_addrcmp(sockaddr_t * __restrict a, sockaddr_t * __restrict b, int p); +/* + * e_innet() - Test address match in network + * + * @net = network + * @addr = address + * return: -1 error, 0 match or 1 not match + */ +int e_innet(netaddr_t * __restrict net, inaddr_t * __restrict addr); +/* + * e_getnet() - Get network from string + * + * @net = Network string (format: ) + * return: NULL error or !=NULL network should be e_free() + */ +netaddr_t *e_getnet(const char *net); +/* + * e_ether_addr() - Get or set ethernet address from interface name + * + * @ifname = interface name + * @addr = if addr is !=NULL then set new ethernet address + * return: NULL error or !=NULL get current ethernet address should be e_free() + */ +ether_addr_t *e_ether_addr(const char *ifname, ether_addr_t * __restrict addr); +/* + * e_get1stiface() - Get first interface of host + * + * @szIface = interface string buffer + * @iflen = size of interface buffer + * return: -1 error or 0 ok + */ +int e_get1stiface(char *szIface, int iflen); +#ifndef __linux__ +/* + * e_getifacebyname() - Get interface and make network structure + * + * @psIface = Interface, if =NULL first interface + * @addr = Network address structure + * return: NULL error or !=NULL network structure + */ +sockaddr_t *e_getifacebyname(const char *psIface, sockaddr_t * __restrict addr); +/* + * e_getlinkbyname() - Get host ethernet address and make network structure + * + * @psHost = Host ethernet address + * @addr = Network address structure + * return: NULL error or !=NULL network structure + */ +sockaddr_t *e_getlinkbyname(const char *psHost, sockaddr_t * __restrict addr); +/* + * e_getlinkbyether() - Get ethernet address and make network structure + * + * @mac = Ethernet address + * @idx = Interface index + * @type = Interface type + * @iface = Interface name + * @addr = Network address structure + * return: NULL error or !=NULL network structure + */ +sockaddr_t *e_getlinkbyether(const ether_addr_t * __restrict mac, unsigned short idx, + unsigned char type, const char *iface, sockaddr_t * __restrict addr); +#define e_getlinkbymac(_mac, _addr) e_getlinkbyether((_mac), 0, 0, NULL, (_addr)) +#endif + +/* + * e_network() - Get network from address string + * + * @csAddr = Address string with CIDR mask /xx + * @net = Network information structure + * return: -1 error, 1 nothing for return or 0 ok + */ +int e_network(const char *csAddr, netaddr_t * __restrict net); #endif