addr.h 2.2 KB

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