1cffc2a7aSchristos /* 2cffc2a7aSchristos * Copyright (c) 2004,2005 Damien Miller <djm@mindrot.org> 3cffc2a7aSchristos * 4cffc2a7aSchristos * Permission to use, copy, modify, and distribute this software for any 5cffc2a7aSchristos * purpose with or without fee is hereby granted, provided that the above 6cffc2a7aSchristos * copyright notice and this permission notice appear in all copies. 7cffc2a7aSchristos * 8cffc2a7aSchristos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9cffc2a7aSchristos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10cffc2a7aSchristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11cffc2a7aSchristos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12cffc2a7aSchristos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13cffc2a7aSchristos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14cffc2a7aSchristos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15cffc2a7aSchristos */ 16cffc2a7aSchristos 17cffc2a7aSchristos /* Address handling routines */ 18cffc2a7aSchristos 19cffc2a7aSchristos #ifndef _ADDR_H 20cffc2a7aSchristos #define _ADDR_H 21cffc2a7aSchristos 22cffc2a7aSchristos #include <sys/socket.h> 23cffc2a7aSchristos #include <netinet/in.h> 24cffc2a7aSchristos 25cffc2a7aSchristos struct xaddr { 26cffc2a7aSchristos sa_family_t af; 27cffc2a7aSchristos union { 28cffc2a7aSchristos struct in_addr v4; 29cffc2a7aSchristos struct in6_addr v6; 30cffc2a7aSchristos u_int8_t addr8[16]; 31cffc2a7aSchristos u_int16_t addr16[8]; 32cffc2a7aSchristos u_int32_t addr32[4]; 33cffc2a7aSchristos } xa; /* 128-bit address */ 34cffc2a7aSchristos u_int32_t scope_id; /* iface scope id for v6 */ 35cffc2a7aSchristos #define v4 xa.v4 36cffc2a7aSchristos #define v6 xa.v6 37cffc2a7aSchristos #define addr8 xa.addr8 38cffc2a7aSchristos #define addr16 xa.addr16 39cffc2a7aSchristos #define addr32 xa.addr32 40cffc2a7aSchristos }; 41cffc2a7aSchristos 42cffc2a7aSchristos int addr_unicast_masklen(int af); 43cffc2a7aSchristos int addr_xaddr_to_sa(const struct xaddr *xa, struct sockaddr *sa, 44cffc2a7aSchristos socklen_t *len, u_int16_t port); 45cffc2a7aSchristos int addr_sa_to_xaddr(struct sockaddr *sa, socklen_t slen, struct xaddr *xa); 46cffc2a7aSchristos int addr_netmask(int af, u_int l, struct xaddr *n); 47cffc2a7aSchristos int addr_hostmask(int af, u_int l, struct xaddr *n); 48cffc2a7aSchristos int addr_invert(struct xaddr *n); 49cffc2a7aSchristos int addr_pton(const char *p, struct xaddr *n); 50cffc2a7aSchristos int addr_sa_pton(const char *h, const char *s, struct sockaddr *sa, 51cffc2a7aSchristos socklen_t slen); 52cffc2a7aSchristos int addr_pton_cidr(const char *p, struct xaddr *n, u_int *l); 53cffc2a7aSchristos int addr_ntop(const struct xaddr *n, char *p, size_t len); 54cffc2a7aSchristos int addr_and(struct xaddr *dst, const struct xaddr *a, const struct xaddr *b); 55*673be2cbSchristos int addr_or(struct xaddr *dst, const struct xaddr *a, const struct xaddr *b); 56cffc2a7aSchristos int addr_cmp(const struct xaddr *a, const struct xaddr *b); 57cffc2a7aSchristos int addr_is_all0s(const struct xaddr *n); 58cffc2a7aSchristos int addr_host_is_all0s(const struct xaddr *n, u_int masklen); 59*673be2cbSchristos int addr_host_to_all0s(struct xaddr *a, u_int masklen); 60*673be2cbSchristos int addr_host_to_all1s(struct xaddr *a, u_int masklen); 61cffc2a7aSchristos int addr_netmatch(const struct xaddr *host, const struct xaddr *net, 62cffc2a7aSchristos u_int masklen); 63*673be2cbSchristos void addr_increment(struct xaddr *a); 64cffc2a7aSchristos #endif /* _ADDR_H */ 65