ng_ipaddr.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * (c) 2008 Florian Westphal <fw@strlen.de>, public domain.
  3. */
  4. #ifndef NG_IPADDR_HDR
  5. #define NG_IPADDR_HDR
  6. #include "portab.h"
  7. /**
  8. * @file
  9. * Functions for AF_ agnostic ipv4/ipv6 handling (header).
  10. */
  11. #include <assert.h>
  12. #include <sys/socket.h>
  13. #include <netinet/in.h>
  14. #ifdef HAVE_ARPA_INET_H
  15. # include <arpa/inet.h>
  16. #else
  17. # define PF_INET AF_INET
  18. #endif
  19. #ifdef WANT_IPV6
  20. #define NG_INET_ADDRSTRLEN INET6_ADDRSTRLEN
  21. #else
  22. #define NG_INET_ADDRSTRLEN 16
  23. #endif
  24. #ifdef WANT_IPV6
  25. typedef union {
  26. struct sockaddr sa;
  27. struct sockaddr_in sin4;
  28. struct sockaddr_in6 sin6;
  29. } ng_ipaddr_t;
  30. #else
  31. /* assume compiler can't deal with typedef struct {... */
  32. struct NG_IP_ADDR_DONTUSE {
  33. struct sockaddr_in sin4;
  34. };
  35. typedef struct NG_IP_ADDR_DONTUSE ng_ipaddr_t;
  36. #endif
  37. static inline int
  38. ng_ipaddr_af(const ng_ipaddr_t *a)
  39. {
  40. #ifdef WANT_IPV6
  41. return a->sa.sa_family;
  42. #else
  43. assert(a->sin4.sin_family == 0 || a->sin4.sin_family == AF_INET);
  44. return a->sin4.sin_family;
  45. #endif
  46. }
  47. static inline socklen_t
  48. ng_ipaddr_salen(const ng_ipaddr_t *a)
  49. {
  50. #ifdef WANT_IPV6
  51. assert(a->sa.sa_family == AF_INET || a->sa.sa_family == AF_INET6);
  52. if (a->sa.sa_family == AF_INET6)
  53. return (socklen_t)sizeof(a->sin6);
  54. #endif
  55. assert(a->sin4.sin_family == AF_INET);
  56. return (socklen_t)sizeof(a->sin4);
  57. }
  58. static inline UINT16
  59. ng_ipaddr_getport(const ng_ipaddr_t *a)
  60. {
  61. #ifdef WANT_IPV6
  62. int af = a->sa.sa_family;
  63. assert(af == AF_INET || af == AF_INET6);
  64. if (af == AF_INET6)
  65. return ntohs(a->sin6.sin6_port);
  66. #endif /* WANT_IPV6 */
  67. assert(a->sin4.sin_family == AF_INET);
  68. return ntohs(a->sin4.sin_port);
  69. }
  70. /*
  71. * init a ng_ipaddr_t object.
  72. * @param addr: pointer to ng_ipaddr_t to initialize.
  73. * @param ip_str: ip address in dotted-decimal (ipv4) or hexadecimal (ipv6) notation
  74. * @param port: transport layer port number to use.
  75. */
  76. GLOBAL bool ng_ipaddr_init PARAMS((ng_ipaddr_t *addr, const char *ip_str, UINT16 port));
  77. /* set sin4/sin6_port, depending on a->sa_family */
  78. GLOBAL void ng_ipaddr_setport PARAMS((ng_ipaddr_t *a, UINT16 port));
  79. /* return true if a and b have the same IP address. If a and b have different AF, return false. */
  80. GLOBAL bool ng_ipaddr_ipequal PARAMS((const ng_ipaddr_t *a, const ng_ipaddr_t *b));
  81. #ifdef WANT_IPV6
  82. /* convert struct sockaddr to string, returns pointer to static buffer */
  83. GLOBAL const char *ng_ipaddr_tostr PARAMS((const ng_ipaddr_t *addr));
  84. /* convert struct sockaddr to string. dest must be NG_INET_ADDRSTRLEN bytes long */
  85. GLOBAL bool ng_ipaddr_tostr_r PARAMS((const ng_ipaddr_t *addr, char *dest));
  86. #else
  87. static inline const char*
  88. ng_ipaddr_tostr(const ng_ipaddr_t *addr)
  89. {
  90. return inet_ntoa(addr->sin4.sin_addr);
  91. }
  92. static inline bool
  93. ng_ipaddr_tostr_r(const ng_ipaddr_t *addr, char *d)
  94. {
  95. strlcpy(d, inet_ntoa(addr->sin4.sin_addr), NG_INET_ADDRSTRLEN);
  96. return true;
  97. }
  98. #endif
  99. #endif
  100. /* -eof- */