ng_ipaddr.h 2.7 KB

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