ng_ipaddr.h 2.8 KB

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