File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / inc / elwix / anet.h
Revision 1.14.12.1: download - view: text, annotated - select for diffs - revision graph
Wed Apr 4 01:22:14 2018 UTC (6 years, 2 months ago) by misho
Branches: elwix4_10
Diff to: branchpoint 1.14: preferred, unified
minor changes in inaddr_t structure

    1: /*************************************************************************
    2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
    3: *  by Michael Pounov <misho@elwix.org>
    4: *
    5: * $Author: misho $
    6: * $Id: anet.h,v 1.14.12.1 2018/04/04 01:22:14 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004 - 2018
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #ifndef __ANET_H
   47: #define __ANET_H
   48: 
   49: 
   50: struct e_ether_addr {
   51: 	u_int8_t octet[6];
   52: };
   53: typedef struct e_ether_addr ether_addr_t;
   54: 
   55: typedef union {
   56: 	struct sockaddr_storage	ss;
   57: 	struct sockaddr		sa;
   58: 	struct sockaddr_un	sun;
   59: 	struct sockaddr_in	sin;
   60: 	struct sockaddr_in6	sin6;
   61: #ifndef __linux__
   62: 	struct sockaddr_dl	sdl;
   63: #else
   64: 	struct sockaddr_ll	sll;
   65: #endif
   66: } sockaddr_t;
   67: #define E_SOCKADDR_INIT	{ .ss = { 0 } }
   68: #define E_SOCKADDR_MAX	MIN(sizeof(sockaddr_t), 0xff)
   69: 
   70: typedef union {
   71: 	struct in4_addr {
   72: 		u_char		s4_addr[sizeof(in_addr_t)];
   73: 	}		in4;
   74: 	struct in_addr	in;
   75: 	struct in6_addr	in6;
   76: } inaddr_t;
   77: 
   78: typedef struct {
   79: 	sockaddr_t	addr;
   80: 	inaddr_t	mask;
   81: } netaddr_t;
   82: #define E_CIDRMASK(x)	htonl((((1 << (x)) - 1) << (32 - (x))) & 0xFFFFFFFF)
   83: 
   84: 
   85: /*
   86:  * e_usleep() - usleep() replacement for ELWIX
   87:  *
   88:  * @usec = microseconds for sleep
   89:  * return: -1 interrupted by signal or 0 ok
   90:  */
   91: int e_usleep(unsigned int usec);
   92: #ifndef __linux__
   93: /*
   94:  * e_link_addr() - String ethernet address to link address
   95:  *
   96:  * @mac = ethernet address
   97:  * @sdl = link address
   98:  * return: -1 error or 0 ok
   99:  */
  100: int e_link_addr(const char *mac, struct sockaddr_dl * __restrict sdl);
  101: /*
  102:  * e_link_ntoa() - String ethernet address from link address
  103:  *
  104:  * @sdl = link address
  105:  * return: =NULL error or !=NULL ethernet address, should be e_free()
  106:  */
  107: char *e_link_ntoa(const struct sockaddr_dl *sdl);
  108: #endif
  109: /*
  110:  * e_ether_ntoa() - Convert ethernet address to string
  111:  *
  112:  * @n = ethernet address structure, like struct ether_addr
  113:  * @a = string
  114:  * @len = string length
  115:  * return: NULL error or !=NULL string a
  116:  */
  117: char *e_ether_ntoa(const ether_addr_t * __restrict n, char * __restrict a, int len);
  118: /*
  119:  * e_ether_aton() - Convert string to ethernet address
  120:  *
  121:  * @a = string
  122:  * @e = ethernet address structure, like struct ether_addr
  123:  * return: NULL error or !=NULL ethernet address structure
  124:  */
  125: ether_addr_t *e_ether_aton(const char *a, ether_addr_t * __restrict e);
  126: /*
  127:  * e_n2port() - Extract port from network structure
  128:  *
  129:  * @addr = Address
  130:  * return: 0 not supported family type or port number
  131:  */
  132: unsigned short e_n2port(sockaddr_t * __restrict addr);
  133: /*
  134:  * e_n2addr() - Extract address from network structure
  135:  *
  136:  * @addr = Address
  137:  * @val = Value for store string address
  138:  * return: NULL error or !=NULL string address from val
  139:  */
  140: const char *e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __restrict val);
  141: /*
  142:  * e_gethostbyname() - Get host and port and make network structure
  143:  *
  144:  * @psHost = Hostname
  145:  * @port = Port
  146:  * @addr = Network address structure
  147:  * return: 0 is error or >0 length of network structure
  148:  */
  149: socklen_t e_gethostbyname(const char *psHost, unsigned short port, 
  150: 		sockaddr_t * __restrict addr);
  151: /*
  152:  * e_addrlen() - Get address length from network structure
  153:  *
  154:  * @addr = address
  155:  * return: 0 is error or >0 length of network structure
  156:  */
  157: socklen_t e_addrlen(const sockaddr_t *addr);
  158: /*
  159:  * e_addrcmp() - Compare network addresses
  160:  *
  161:  * @a = 1st address
  162:  * @b = 2nd address
  163:  * @p = compare and ports, if family is AF_INET or AF_INET6
  164:  * return: 0 is equal or !=0 is different
  165:  */
  166: int e_addrcmp(sockaddr_t * __restrict a, sockaddr_t * __restrict b, int p);
  167: /*
  168:  * e_innet() - Test address match in network
  169:  *
  170:  * @net = network
  171:  * @addr = address
  172:  * return: -1 error, 0 match or 1 not match
  173:  */
  174: int e_innet(netaddr_t * __restrict net, inaddr_t * __restrict addr);
  175: /*
  176:  * e_getnet() - Get network from string
  177:  *
  178:  * @net = Network string (format: <net[/cidr]>)
  179:  * return: NULL error or !=NULL network should be e_free()
  180:  */
  181: netaddr_t *e_getnet(const char *net);
  182: /*
  183:  * e_ether_addr() - Get or set ethernet address from interface name
  184:  *
  185:  * @ifname = interface name
  186:  * @addr = if addr is !=NULL then set new ethernet address
  187:  * return: NULL error or !=NULL get current ethernet address should be e_free()
  188:  */
  189: ether_addr_t *e_ether_addr(const char *ifname, ether_addr_t * __restrict addr);
  190: /*
  191:  * e_get1stiface() - Get first interface of host
  192:  *
  193:  * @szIface = interface string buffer
  194:  * @iflen = size of interface buffer
  195:  * return: -1 error or 0 ok
  196:  */
  197: int e_get1stiface(char *szIface, int iflen);
  198: #ifndef __linux__
  199: /*
  200:  * e_getifacebyname() - Get interface and make network structure
  201:  *
  202:  * @psIface = Interface, if =NULL first interface
  203:  * @addr = Network address structure
  204:  * return: NULL error or !=NULL network structure
  205:  */
  206: sockaddr_t *e_getifacebyname(const char *psIface, sockaddr_t * __restrict addr);
  207: /*
  208:  * e_getlinkbyname() - Get host ethernet address and make network structure
  209:  *
  210:  * @psHost = Host ethernet address
  211:  * @addr = Network address structure
  212:  * return: NULL error or !=NULL network structure
  213:  */
  214: sockaddr_t *e_getlinkbyname(const char *psHost, sockaddr_t * __restrict addr);
  215: /*
  216:  * e_getlinkbyether() - Get ethernet address and make network structure
  217:  *
  218:  * @mac = Ethernet address
  219:  * @idx = Interface index
  220:  * @type = Interface type
  221:  * @iface = Interface name
  222:  * @addr = Network address structure
  223:  * return: NULL error or !=NULL network structure
  224:  */
  225: sockaddr_t *e_getlinkbyether(const ether_addr_t * __restrict mac, unsigned short idx, 
  226: 		unsigned char type, const char *iface, sockaddr_t * __restrict addr);
  227: #define e_getlinkbymac(_mac, _addr)	e_getlinkbyether((_mac), 0, 0, NULL, (_addr))
  228: #endif
  229: 
  230: 
  231: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>