File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / inc / elwix / anet.h
Revision 1.9.4.4: download - view: text, annotated - select for diffs - revision graph
Sat May 14 19:55:52 2016 UTC (8 years, 1 month ago) by misho
Branches: elwix3_9
Diff to: branchpoint 1.9: preferred, unified
finish BSD OS set mac address

    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.9.4.4 2016/05/14 19:55:52 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 - 2016
   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: #endif
   64: } sockaddr_t;
   65: #define E_SOCKADDR_INIT	{ .ss = { 0 } }
   66: 
   67: typedef union {
   68: 	union {
   69: 		in_addr_t	s_addr;
   70: 		u_char		s4_addr[sizeof(in_addr_t)];
   71: 	}		in;
   72: 	struct in6_addr	in6;
   73: } inaddr_t;
   74: 
   75: typedef struct {
   76: 	sockaddr_t	addr;
   77: 	inaddr_t	mask;
   78: } netaddr_t;
   79: #define E_CIDRMASK(x)	htonl((((1 << (x)) - 1) << (32 - (x))) & 0xFFFFFFFF)
   80: 
   81: 
   82: /*
   83:  * e_usleep() - usleep() replacement for ELWIX
   84:  *
   85:  * @usec = microseconds for sleep
   86:  * return: -1 interrupted by signal or 0 ok
   87:  */
   88: int e_usleep(unsigned int usec);
   89: #ifndef __linux__
   90: /*
   91:  * e_link_addr() - String ethernet address to link address
   92:  *
   93:  * @mac = ethernet address
   94:  * @sdl = link address
   95:  * return: -1 error or 0 ok
   96:  */
   97: int e_link_addr(const char *mac, struct sockaddr_dl * __restrict sdl);
   98: /*
   99:  * e_link_ntoa() - String ethernet address from link address
  100:  *
  101:  * @sdl = link address
  102:  * return: =NULL error or !=NULL ethernet address, should be e_free()
  103:  */
  104: char *e_link_ntoa(const struct sockaddr_dl *sdl);
  105: #endif
  106: /*
  107:  * e_ether_ntoa() - Convert ethernet address to string
  108:  *
  109:  * @n = ethernet address structure, like struct ether_addr
  110:  * @a = string
  111:  * @len = string length
  112:  * return: NULL error or !=NULL string a
  113:  */
  114: char *e_ether_ntoa(const ether_addr_t * __restrict n, char * __restrict a, int len);
  115: /*
  116:  * e_ether_aton() - Convert string to ethernet address
  117:  *
  118:  * @a = string
  119:  * @e = ethernet address structure, like struct ether_addr
  120:  * return: NULL error or !=NULL ethernet address structure
  121:  */
  122: ether_addr_t *e_ether_aton(const char *a, ether_addr_t * __restrict e);
  123: /*
  124:  * e_n2port() - Extract port from network structure
  125:  *
  126:  * @addr = Address
  127:  * return: 0 not supported family type or port number
  128:  */
  129: unsigned short e_n2port(sockaddr_t * __restrict addr);
  130: /*
  131:  * e_n2addr() - Extract address from network structure
  132:  *
  133:  * @addr = Address
  134:  * @val = Value for store string address
  135:  * return: NULL error or !=NULL string address from val
  136:  */
  137: const char *e_n2addr(sockaddr_t * __restrict addr, ait_val_t * __restrict val);
  138: /*
  139:  * e_gethostbyname() - Get host and port and make network structure
  140:  *
  141:  * @psHost = Hostname
  142:  * @port = Port
  143:  * @addr = Network address structure
  144:  * return: NULL error or !=NULL network structure
  145:  */
  146: sockaddr_t *e_gethostbyname(const char *psHost, unsigned short port, 
  147: 		sockaddr_t * __restrict addr);
  148: /*
  149:  * e_addrcmp() - Compare network addresses
  150:  *
  151:  * @a = 1st address
  152:  * @b = 2nd address
  153:  * @p = compare and ports, if family is AF_INET or AF_INET6
  154:  * return: 0 is equal or !=0 is different
  155:  */
  156: int e_addrcmp(sockaddr_t * __restrict a, sockaddr_t * __restrict b, int p);
  157: /*
  158:  * e_innet() - Test address match in network
  159:  *
  160:  * @net = network
  161:  * @addr = address
  162:  * return: -1 error, 0 match or 1 not match
  163:  */
  164: int e_innet(netaddr_t * __restrict net, inaddr_t * __restrict addr);
  165: /*
  166:  * e_getnet() - Get network from string
  167:  *
  168:  * @net = Network string (format: <net[/cidr]>)
  169:  * return: NULL error or !=NULL network should be e_free()
  170:  */
  171: netaddr_t *e_getnet(const char *net);
  172: /*
  173:  * e_ether_addr() - Get or set ethernet address from interface name
  174:  *
  175:  * @ifname = interface name
  176:  * @addr = if addr is !=NULL then set new ethernet address
  177:  * return: NULL error or !=NULL get current ethernet address should be e_free()
  178:  */
  179: ether_addr_t *e_ether_addr(const char *ifname, ether_addr_t * __restrict addr);
  180: /*
  181:  * e_get1stiface() - Get first interface of host
  182:  *
  183:  * @szIface = interface string buffer
  184:  * @iflen = size of interface buffer
  185:  * return: -1 error or 0 ok
  186:  */
  187: int e_get1stiface(char *szIface, int iflen);
  188: #ifndef __linux__
  189: /*
  190:  * e_getifacebyname() - Get interface and make network structure
  191:  *
  192:  * @psIface = Interface, if =NULL first interface
  193:  * @addr = Network address structure
  194:  * return: NULL error or !=NULL network structure
  195:  */
  196: sockaddr_t *e_getifacebyname(const char *psIface, sockaddr_t * __restrict addr);
  197: /*
  198:  * e_getlinkbyname() - Get host ethernet address and make network structure
  199:  *
  200:  * @psHost = Host ethernet address
  201:  * @addr = Network address structure
  202:  * return: NULL error or !=NULL network structure
  203:  */
  204: sockaddr_t *e_getlinkbyname(const char *psHost, sockaddr_t * __restrict addr);
  205: /*
  206:  * e_getlinkbyether() - Get ethernet address and make network structure
  207:  *
  208:  * @mac = Ethernet address
  209:  * @idx = Interface index
  210:  * @type = Interface type
  211:  * @iface = Interface name
  212:  * @addr = Network address structure
  213:  * return: NULL error or !=NULL network structure
  214:  */
  215: sockaddr_t *e_getlinkbyether(const ether_addr_t * __restrict mac, unsigned short idx, 
  216: 		unsigned char type, const char *iface, sockaddr_t * __restrict addr);
  217: #define e_getlinkbymac(_mac, _addr)	e_getlinkbyether((_mac), 0, 0, NULL, (_addr))
  218: #endif
  219: 
  220: 
  221: #endif

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