ng_ipaddr.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. assert(a != NULL);
  41. #ifdef WANT_IPV6
  42. return a->sa.sa_family;
  43. #else
  44. assert(a->sin4.sin_family == 0 || a->sin4.sin_family == AF_INET);
  45. return a->sin4.sin_family;
  46. #endif
  47. }
  48. static inline socklen_t
  49. ng_ipaddr_salen(const ng_ipaddr_t *a)
  50. {
  51. assert(a != NULL);
  52. #ifdef WANT_IPV6
  53. assert(a->sa.sa_family == AF_INET || a->sa.sa_family == AF_INET6);
  54. if (a->sa.sa_family == AF_INET6)
  55. return (socklen_t)sizeof(a->sin6);
  56. #endif
  57. assert(a->sin4.sin_family == AF_INET);
  58. return (socklen_t)sizeof(a->sin4);
  59. }
  60. static inline UINT16
  61. ng_ipaddr_getport(const ng_ipaddr_t *a)
  62. {
  63. #ifdef WANT_IPV6
  64. int af = a->sa.sa_family;
  65. assert(a != NULL);
  66. assert(af == AF_INET || af == AF_INET6);
  67. if (af == AF_INET6)
  68. return ntohs(a->sin6.sin6_port);
  69. #endif /* WANT_IPV6 */
  70. assert(a != NULL);
  71. assert(a->sin4.sin_family == AF_INET);
  72. return ntohs(a->sin4.sin_port);
  73. }
  74. /*
  75. * init a ng_ipaddr_t object.
  76. * @param addr: pointer to ng_ipaddr_t to initialize.
  77. * @param ip_str: ip address in dotted-decimal (ipv4) or hexadecimal (ipv6) notation
  78. * @param port: transport layer port number to use.
  79. */
  80. GLOBAL bool ng_ipaddr_init PARAMS((ng_ipaddr_t *addr, const char *ip_str, UINT16 port));
  81. /* set sin4/sin6_port, depending on a->sa_family */
  82. GLOBAL void ng_ipaddr_setport PARAMS((ng_ipaddr_t *a, UINT16 port));
  83. /* return true if a and b have the same IP address. If a and b have different AF, return false. */
  84. GLOBAL bool ng_ipaddr_ipequal PARAMS((const ng_ipaddr_t *a, const ng_ipaddr_t *b));
  85. #ifdef WANT_IPV6
  86. /* convert struct sockaddr to string, returns pointer to static buffer */
  87. GLOBAL const char *ng_ipaddr_tostr PARAMS((const ng_ipaddr_t *addr));
  88. /* convert struct sockaddr to string. dest must be NG_INET_ADDRSTRLEN bytes long */
  89. GLOBAL bool ng_ipaddr_tostr_r PARAMS((const ng_ipaddr_t *addr, char *dest));
  90. #else
  91. static inline const char*
  92. ng_ipaddr_tostr(const ng_ipaddr_t *addr)
  93. {
  94. assert(addr != NULL);
  95. return inet_ntoa(addr->sin4.sin_addr);
  96. }
  97. static inline bool
  98. ng_ipaddr_tostr_r(const ng_ipaddr_t *addr, char *d)
  99. {
  100. assert(addr != NULL);
  101. assert(d != NULL);
  102. strlcpy(d, inet_ntoa(addr->sin4.sin_addr), NG_INET_ADDRSTRLEN);
  103. return true;
  104. }
  105. #endif
  106. #endif
  107. /* -eof- */