socket.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJ_COMPAT_SOCKET_H__
  20. #define __PJ_COMPAT_SOCKET_H__
  21. /**
  22. * @file socket.h
  23. * @brief Provides all socket related functions,data types, error codes, etc.
  24. */
  25. #if defined(PJ_HAS_WINSOCK2_H) && PJ_HAS_WINSOCK2_H != 0
  26. # include <winsock2.h>
  27. #endif
  28. #if defined(PJ_HAS_WINSOCK_H) && PJ_HAS_WINSOCK_H != 0
  29. # include <winsock.h>
  30. #endif
  31. #if defined(PJ_HAS_WS2TCPIP_H) && PJ_HAS_WS2TCPIP_H != 0
  32. # include <ws2tcpip.h>
  33. #endif
  34. #if (defined(PJ_WIN32_UWP) && PJ_WIN32_UWP!=0)
  35. # include <in6addr.h>
  36. #endif
  37. /*
  38. * IPv6 for Visual Studio's
  39. *
  40. * = Visual Studio 6 =
  41. *
  42. * Visual Studio 6 does not ship with IPv6 support, so you MUST
  43. * download and install IPv6 Tehnology Preview (IPv6Kit) from:
  44. * http://msdn.microsoft.com/downloads/sdks/platform/tpipv6/ReadMe.asp
  45. * Then put IPv6Kit\inc in your Visual Studio include path.
  46. *
  47. * In addition, by default IPv6Kit does not want to install on
  48. * Windows 2000 SP4. Please see:
  49. * http://msdn.microsoft.com/downloads/sdks/platform/tpipv6/faq.asp
  50. * on how to install IPv6Kit on Win2K SP4.
  51. *
  52. *
  53. * = Visual Studio 2003, 2005 (including Express) =
  54. *
  55. * These VS uses Microsoft Platform SDK for Windows Server 2003 SP1, and
  56. * it has built-in IPv6 support.
  57. */
  58. #if defined(_MSC_VER) && defined(PJ_HAS_IPV6) && PJ_HAS_IPV6!=0
  59. # ifndef s_addr
  60. # define s_addr S_un.S_addr
  61. # endif
  62. # if !defined(IPPROTO_IPV6) && (_WIN32_WINNT == 0x0500)
  63. /* Need to download and install IPv6Kit for this platform.
  64. * Please see the comments above about Visual Studio 6.
  65. */
  66. # include <tpipv6.h>
  67. # endif
  68. # define PJ_SOCK_HAS_GETADDRINFO 1
  69. #endif /* _MSC_VER */
  70. #if defined(PJ_HAS_SYS_TYPES_H) && PJ_HAS_SYS_TYPES_H != 0
  71. # include <sys/types.h>
  72. #endif
  73. #if defined(PJ_HAS_SYS_SOCKET_H) && PJ_HAS_SYS_SOCKET_H != 0
  74. # include <sys/socket.h>
  75. #endif
  76. #if defined(PJ_HAS_LINUX_SOCKET_H) && PJ_HAS_LINUX_SOCKET_H != 0
  77. # include <linux/socket.h>
  78. #endif
  79. #if defined(PJ_HAS_SYS_SELECT_H) && PJ_HAS_SYS_SELECT_H != 0
  80. # include <sys/select.h>
  81. #endif
  82. #if defined(PJ_HAS_NETINET_IN_H) && PJ_HAS_NETINET_IN_H != 0
  83. # include <netinet/in.h>
  84. #endif
  85. #if defined(PJ_HAS_NETINET_IN_SYSTM_H) && PJ_HAS_NETINET_IN_SYSTM_H != 0
  86. /* Required to include netinet/ip.h in FreeBSD 7.0 */
  87. # include <netinet/in_systm.h>
  88. #endif
  89. #if defined(PJ_HAS_NETINET_IP_H) && PJ_HAS_NETINET_IP_H != 0
  90. /* To pull in IPTOS_* constants */
  91. # include <netinet/ip.h>
  92. #endif
  93. #if defined(PJ_HAS_NETINET_TCP_H) && PJ_HAS_NETINET_TCP_H != 0
  94. /* To pull in TCP_NODELAY constants */
  95. # include <netinet/tcp.h>
  96. #endif
  97. #if defined(PJ_HAS_NET_IF_H) && PJ_HAS_NET_IF_H != 0
  98. /* For interface enumeration in ip_helper */
  99. # include <net/if.h>
  100. #endif
  101. #if defined(PJ_HAS_IFADDRS_H) && PJ_HAS_IFADDRS_H != 0
  102. /* Interface enum with getifaddrs() which works with IPv6 */
  103. # include <ifaddrs.h>
  104. #endif
  105. #if defined(PJ_HAS_ARPA_INET_H) && PJ_HAS_ARPA_INET_H != 0
  106. # include <arpa/inet.h>
  107. #endif
  108. #if defined(PJ_HAS_SYS_IOCTL_H) && PJ_HAS_SYS_IOCTL_H != 0
  109. # include <sys/ioctl.h> /* FBIONBIO */
  110. #endif
  111. #if defined(PJ_HAS_ERRNO_H) && PJ_HAS_ERRNO_H != 0
  112. # include <errno.h>
  113. #endif
  114. #if defined(PJ_HAS_NETDB_H) && PJ_HAS_NETDB_H != 0
  115. # include <netdb.h>
  116. #endif
  117. #if defined(PJ_HAS_UNISTD_H) && PJ_HAS_UNISTD_H != 0
  118. # include <unistd.h>
  119. #endif
  120. #if defined(PJ_HAS_SYS_FILIO_H) && PJ_HAS_SYS_FILIO_H != 0
  121. # include <sys/filio.h>
  122. #endif
  123. #if defined(PJ_HAS_SYS_SOCKIO_H) && PJ_HAS_SYS_SOCKIO_H != 0
  124. # include <sys/sockio.h>
  125. #endif
  126. /*
  127. * Define common errors.
  128. */
  129. #if (defined(PJ_WIN32) && PJ_WIN32!=0) || \
  130. (defined(PJ_WIN32_WINCE) && PJ_WIN32_WINCE!=0) || \
  131. (defined(PJ_WIN64) && PJ_WIN64!=0)
  132. # define OSERR_EWOULDBLOCK WSAEWOULDBLOCK
  133. # define OSERR_EINPROGRESS WSAEINPROGRESS
  134. # define OSERR_ECONNRESET WSAECONNRESET
  135. # define OSERR_ENOTCONN WSAENOTCONN
  136. # define OSERR_EAFNOSUPPORT WSAEAFNOSUPPORT
  137. # define OSERR_ENOPROTOOPT WSAENOPROTOOPT
  138. #elif defined(PJ_SYMBIAN) && PJ_SYMBIAN!=0
  139. # define OSERR_EWOULDBLOCK -1
  140. # define OSERR_EINPROGRESS -1
  141. # define OSERR_ECONNRESET -1
  142. # define OSERR_ENOTCONN -1
  143. # define OSERR_EAFNOSUPPORT -1
  144. # define OSERR_ENOPROTOOPT -1
  145. #else
  146. # define OSERR_EWOULDBLOCK EWOULDBLOCK
  147. # define OSERR_EINPROGRESS EINPROGRESS
  148. # define OSERR_ECONNRESET ECONNRESET
  149. # define OSERR_ENOTCONN ENOTCONN
  150. # define OSERR_EAFNOSUPPORT EAFNOSUPPORT
  151. # define OSERR_ENOPROTOOPT ENOPROTOOPT
  152. #endif
  153. /*
  154. * And undefine these..
  155. * Note (see issue #2311): unfortunately, this may cause build failure
  156. * to anyone who uses these standard macros.
  157. */
  158. //#undef s_addr
  159. //#undef s6_addr
  160. //#undef sin_zero
  161. /*
  162. * This will finally be obsoleted, since it should be declared in
  163. * os_auto.h
  164. */
  165. #if !defined(PJ_HAS_SOCKLEN_T) || PJ_HAS_SOCKLEN_T==0
  166. typedef int socklen_t;
  167. #endif
  168. /* Regarding sin_len member of sockaddr_in:
  169. * BSD systems (including MacOS X requires that the sin_len member of
  170. * sockaddr_in be set to sizeof(sockaddr_in), while other systems (Windows
  171. * and Linux included) do not.
  172. *
  173. * To maintain compatibility between systems, PJLIB will automatically
  174. * set this field before invoking native OS socket API, and it will
  175. * always reset the field to zero before returning pj_sockaddr_in to
  176. * application (such as in pj_getsockname() and pj_recvfrom()).
  177. *
  178. * Application MUST always set this field to zero.
  179. *
  180. * This way we can avoid hard to find problem such as when the socket
  181. * address is used as hash table key.
  182. */
  183. #if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
  184. # define PJ_SOCKADDR_SET_LEN(addr,len) (((pj_addr_hdr*)(addr))->sa_zero_len=(len))
  185. # define PJ_SOCKADDR_RESET_LEN(addr) (((pj_addr_hdr*)(addr))->sa_zero_len=0)
  186. #else
  187. # define PJ_SOCKADDR_SET_LEN(addr,len)
  188. # define PJ_SOCKADDR_RESET_LEN(addr)
  189. #endif
  190. #endif /* __PJ_COMPAT_SOCKET_H__ */