getaddrinfo.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*-------------------------------------------------------------------------
  2. *
  3. * getaddrinfo.h
  4. * Support getaddrinfo() on platforms that don't have it.
  5. *
  6. * Note: we use our own routines on platforms that don't HAVE_STRUCT_ADDRINFO,
  7. * whether or not the library routine getaddrinfo() can be found. This
  8. * policy is needed because on some platforms a manually installed libbind.a
  9. * may provide getaddrinfo(), yet the system headers may not provide the
  10. * struct definitions needed to call it. To avoid conflict with the libbind
  11. * definition in such cases, we rename our routines to pg_xxx() via macros.
  12. *
  13. * This code will also work on platforms where struct addrinfo is defined
  14. * in the system headers but no getaddrinfo() can be located.
  15. *
  16. * Copyright (c) 2003-2016, PostgreSQL Global Development Group
  17. *
  18. * src/include/getaddrinfo.h
  19. *
  20. *-------------------------------------------------------------------------
  21. */
  22. #ifndef GETADDRINFO_H
  23. #define GETADDRINFO_H
  24. #include <sys/socket.h>
  25. #include <netdb.h>
  26. /* Various macros that ought to be in <netdb.h>, but might not be */
  27. #ifndef EAI_FAIL
  28. #ifndef WIN32
  29. #define EAI_BADFLAGS (-1)
  30. #define EAI_NONAME (-2)
  31. #define EAI_AGAIN (-3)
  32. #define EAI_FAIL (-4)
  33. #define EAI_FAMILY (-6)
  34. #define EAI_SOCKTYPE (-7)
  35. #define EAI_SERVICE (-8)
  36. #define EAI_MEMORY (-10)
  37. #define EAI_SYSTEM (-11)
  38. #else /* WIN32 */
  39. #ifdef WIN32_ONLY_COMPILER
  40. #ifndef WSA_NOT_ENOUGH_MEMORY
  41. #define WSA_NOT_ENOUGH_MEMORY (WSAENOBUFS)
  42. #endif
  43. #ifndef __BORLANDC__
  44. #define WSATYPE_NOT_FOUND (WSABASEERR+109)
  45. #endif
  46. #endif
  47. #define EAI_AGAIN WSATRY_AGAIN
  48. #define EAI_BADFLAGS WSAEINVAL
  49. #define EAI_FAIL WSANO_RECOVERY
  50. #define EAI_FAMILY WSAEAFNOSUPPORT
  51. #define EAI_MEMORY WSA_NOT_ENOUGH_MEMORY
  52. #define EAI_NODATA WSANO_DATA
  53. #define EAI_NONAME WSAHOST_NOT_FOUND
  54. #define EAI_SERVICE WSATYPE_NOT_FOUND
  55. #define EAI_SOCKTYPE WSAESOCKTNOSUPPORT
  56. #endif /* !WIN32 */
  57. #endif /* !EAI_FAIL */
  58. #ifndef AI_PASSIVE
  59. #define AI_PASSIVE 0x0001
  60. #endif
  61. #ifndef AI_NUMERICHOST
  62. /*
  63. * some platforms don't support AI_NUMERICHOST; define as zero if using
  64. * the system version of getaddrinfo...
  65. */
  66. #if defined(HAVE_STRUCT_ADDRINFO) && defined(HAVE_GETADDRINFO)
  67. #define AI_NUMERICHOST 0
  68. #else
  69. #define AI_NUMERICHOST 0x0004
  70. #endif
  71. #endif
  72. #ifndef NI_NUMERICHOST
  73. #define NI_NUMERICHOST 1
  74. #endif
  75. #ifndef NI_NUMERICSERV
  76. #define NI_NUMERICSERV 2
  77. #endif
  78. #ifndef NI_NAMEREQD
  79. #define NI_NAMEREQD 4
  80. #endif
  81. #ifndef NI_MAXHOST
  82. #define NI_MAXHOST 1025
  83. #endif
  84. #ifndef NI_MAXSERV
  85. #define NI_MAXSERV 32
  86. #endif
  87. #ifndef HAVE_STRUCT_ADDRINFO
  88. #ifndef WIN32
  89. struct addrinfo
  90. {
  91. int ai_flags;
  92. int ai_family;
  93. int ai_socktype;
  94. int ai_protocol;
  95. size_t ai_addrlen;
  96. struct sockaddr *ai_addr;
  97. char *ai_canonname;
  98. struct addrinfo *ai_next;
  99. };
  100. #else
  101. /*
  102. * The order of the structure elements on Win32 doesn't match the
  103. * order specified in the standard, but we have to match it for
  104. * IPv6 to work.
  105. */
  106. struct addrinfo
  107. {
  108. int ai_flags;
  109. int ai_family;
  110. int ai_socktype;
  111. int ai_protocol;
  112. size_t ai_addrlen;
  113. char *ai_canonname;
  114. struct sockaddr *ai_addr;
  115. struct addrinfo *ai_next;
  116. };
  117. #endif
  118. #endif /* HAVE_STRUCT_ADDRINFO */
  119. #ifndef HAVE_GETADDRINFO
  120. /* Rename private copies per comments above */
  121. #ifdef getaddrinfo
  122. #undef getaddrinfo
  123. #endif
  124. #define getaddrinfo pg_getaddrinfo
  125. #ifdef freeaddrinfo
  126. #undef freeaddrinfo
  127. #endif
  128. #define freeaddrinfo pg_freeaddrinfo
  129. #ifdef gai_strerror
  130. #undef gai_strerror
  131. #endif
  132. #define gai_strerror pg_gai_strerror
  133. #ifdef getnameinfo
  134. #undef getnameinfo
  135. #endif
  136. #define getnameinfo pg_getnameinfo
  137. extern int getaddrinfo(const char *node, const char *service,
  138. const struct addrinfo * hints, struct addrinfo ** res);
  139. extern void freeaddrinfo(struct addrinfo * res);
  140. extern const char *gai_strerror(int errcode);
  141. extern int getnameinfo(const struct sockaddr * sa, int salen,
  142. char *node, int nodelen,
  143. char *service, int servicelen, int flags);
  144. #endif /* HAVE_GETADDRINFO */
  145. #endif /* GETADDRINFO_H */