ng_ipaddr.h 2.9 KB

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