PageRenderTime 74ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/pegasus/src/Pegasus/Common/Network.h

#
C Header | 272 lines | 126 code | 30 blank | 116 comment | 12 complexity | 4ab166357575e31e598b720e6daf8db9 MD5 | raw file
  1. //%LICENSE////////////////////////////////////////////////////////////////
  2. //
  3. // Licensed to The Open Group (TOG) under one or more contributor license
  4. // agreements. Refer to the OpenPegasusNOTICE.txt file distributed with
  5. // this work for additional information regarding copyright ownership.
  6. // Each contributor licenses this file to you under the OpenPegasus Open
  7. // Source License; you may not use this file except in compliance with the
  8. // License.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a
  11. // copy of this software and associated documentation files (the "Software"),
  12. // to deal in the Software without restriction, including without limitation
  13. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  14. // and/or sell copies of the Software, and to permit persons to whom the
  15. // Software is furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included
  18. // in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  21. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  24. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  25. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  26. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. //////////////////////////////////////////////////////////////////////////
  29. //
  30. //%/////////////////////////////////////////////////////////////////////////////
  31. #ifndef Pegasus_Network_h
  32. #define Pegasus_Network_h
  33. #include <Pegasus/Common/Config.h>
  34. //==============================================================================
  35. //
  36. // Network.h
  37. //
  38. // This file includes network-related system-header files. Please include
  39. // this file directly rather than including system headers directly. If
  40. // special inclusions are necessary for any platform, please add them to
  41. // this file rather than other files. The reason for this file is to limit
  42. // platform-specific conditional compilation expressions to only a few
  43. // well-known header files.
  44. //
  45. //==============================================================================
  46. //------------------------------------------------------------------------------
  47. //
  48. // PEGASUS_OS_TYPE_WINDOWS network system header files
  49. //
  50. //------------------------------------------------------------------------------
  51. #ifdef PEGASUS_OS_TYPE_WINDOWS
  52. # ifdef FD_SETSIZE
  53. # ifndef PEGASUS_WMIMAPPER
  54. # error "<Pegasus/Common/Network.h>: FD_SETSIZE is already defined. \
  55. This file must be included prior to any header file that defines \
  56. FD_SETSIZE, such as <windows.h>, <winsock.h>, or <winsock2.h>."
  57. # else
  58. # undef FD_SETSIZE
  59. # endif
  60. # endif
  61. # define FD_SETSIZE 1024
  62. # include <windows.h>
  63. # ifndef _WINSOCKAPI_
  64. # include <winsock2.h>
  65. # endif
  66. # include <wincrypt.h>
  67. # ifdef PEGASUS_ENABLE_IPV6
  68. # include <ws2tcpip.h>
  69. # endif
  70. #endif
  71. #ifdef PEGASUS_ENABLE_IPV6
  72. # ifdef PEGASUS_HAS_GETIFADDRS
  73. # include <ifaddrs.h>
  74. # endif
  75. #endif
  76. //------------------------------------------------------------------------------
  77. //
  78. // PEGASUS_OS_TYPE_UNIX or PEGASUS_OS_VMS network system header files.
  79. //
  80. //------------------------------------------------------------------------------
  81. #if defined(PEGASUS_OS_TYPE_UNIX) || defined (PEGASUS_OS_VMS)
  82. # include <errno.h>
  83. # include <sys/types.h>
  84. # include <fcntl.h>
  85. # include <netdb.h>
  86. # include <netinet/in.h>
  87. # include <arpa/inet.h>
  88. # include <sys/socket.h>
  89. # include <sys/time.h>
  90. # ifndef PEGASUS_OS_HPUX
  91. # include <net/if.h>
  92. # endif
  93. # include <sys/ioctl.h>
  94. # ifndef PEGASUS_DISABLE_LOCAL_DOMAIN_SOCKET
  95. # include <unistd.h>
  96. # include <sys/un.h>
  97. # endif
  98. # include <unistd.h>
  99. # ifdef PEGASUS_OS_ZOS
  100. # ifndef TCP_NODELAY
  101. # define TCP_NODELAY 1
  102. # endif
  103. # else
  104. # include <netinet/tcp.h>
  105. # endif
  106. #endif
  107. //------------------------------------------------------------------------------
  108. //
  109. // PEGASUS_RETRY_SYSTEM_CALL()
  110. //
  111. // This macro repeats the given system call until it returns something
  112. // other than EINTR.
  113. //
  114. //------------------------------------------------------------------------------
  115. #ifdef PEGASUS_OS_TYPE_WINDOWS
  116. # define PEGASUS_RETRY_SYSTEM_CALL(EXPR, RESULT) RESULT = EXPR
  117. #else
  118. # define PEGASUS_RETRY_SYSTEM_CALL(EXPR, RESULT) \
  119. while (((RESULT = (EXPR)) == -1) && (errno == EINTR))
  120. #endif
  121. //------------------------------------------------------------------------------
  122. //
  123. // PEGASUS_SOCKET_ERROR
  124. //
  125. //------------------------------------------------------------------------------
  126. #ifdef PEGASUS_OS_TYPE_WINDOWS
  127. # define PEGASUS_SOCKET_ERROR SOCKET_ERROR
  128. #else
  129. # define PEGASUS_SOCKET_ERROR (-1)
  130. #endif
  131. //------------------------------------------------------------------------------
  132. //
  133. // PEGASUS_NETWORK_TCPIP_STOPPED
  134. //
  135. // This return code indicates that the transpor layer is
  136. // stopped and the socket is invalid. The socket must created again.
  137. //
  138. //------------------------------------------------------------------------------
  139. #ifdef PEGASUS_OS_ZOS
  140. # define PEGASUS_NETWORK_TCPIP_STOPPED EIO
  141. #else
  142. # define PEGASUS_NETWORK_TCPIP_STOPPED 0
  143. #endif
  144. //------------------------------------------------------------------------------
  145. //
  146. // PEGASUS_NETWORK_TRYAGAIN
  147. //
  148. // This return code indicates that the network function
  149. // should be tried again by the program.
  150. //
  151. //------------------------------------------------------------------------------
  152. #if !defined(PEGASUS_OS_TYPE_WINDOWS)
  153. # define PEGASUS_NETWORK_TRYAGAIN EAGAIN
  154. #else
  155. # define PEGASUS_NETWORK_TRYAGAIN 0
  156. #endif
  157. //------------------------------------------------------------------------------
  158. //
  159. // PEGASUS_NETWORK_EINPROGRESS
  160. //
  161. // This return code indicates that the network function
  162. // is in progress. The application should try select or poll and
  163. // check for successful completion.
  164. //
  165. //------------------------------------------------------------------------------
  166. #if !defined(PEGASUS_OS_TYPE_WINDOWS)
  167. # define PEGASUS_NETWORK_EINPROGRESS EINPROGRESS
  168. #else
  169. # define PEGASUS_NETWORK_EINPROGRESS WSAEWOULDBLOCK
  170. #endif
  171. ////////////////////////////////////////////////////////////////////////////////
  172. //
  173. // getSocketError()
  174. //
  175. ////////////////////////////////////////////////////////////////////////////////
  176. static inline int getSocketError()
  177. {
  178. #ifdef PEGASUS_OS_TYPE_WINDOWS
  179. return WSAGetLastError();
  180. #else
  181. return errno;
  182. #endif
  183. }
  184. //------------------------------------------------------------------------------
  185. //
  186. // PEGASUS_INVALID_SOCKET
  187. //
  188. //------------------------------------------------------------------------------
  189. #ifdef PEGASUS_OS_TYPE_WINDOWS
  190. # define PEGASUS_INVALID_SOCKET INVALID_SOCKET
  191. #else
  192. # define PEGASUS_INVALID_SOCKET (-1)
  193. #endif
  194. //------------------------------------------------------------------------------
  195. //
  196. //
  197. // PEGASUS_INVALID_ADDRESS_FAMILY
  198. //
  199. //------------------------------------------------------------------------------
  200. # ifdef PEGASUS_OS_TYPE_WINDOWS
  201. # define PEGASUS_INVALID_ADDRESS_FAMILY WSAEAFNOSUPPORT
  202. # elif defined(PEGASUS_OS_HPUX)
  203. # define PEGASUS_INVALID_ADDRESS_FAMILY EPROTONOSUPPORT
  204. # else
  205. # define PEGASUS_INVALID_ADDRESS_FAMILY EAFNOSUPPORT
  206. # endif
  207. //------------------------------------------------------------------------------
  208. //
  209. // SocketHandle
  210. //
  211. //------------------------------------------------------------------------------
  212. #ifdef PEGASUS_OS_TYPE_WINDOWS
  213. typedef SOCKET SocketHandle;
  214. #else
  215. typedef int SocketHandle;
  216. #endif
  217. //------------------------------------------------------------------------------
  218. //
  219. // SocketLength
  220. //
  221. //------------------------------------------------------------------------------
  222. #if defined(PEGASUS_OS_SOLARIS) || \
  223. defined(PEGASUS_PLATFORM_TRU64_ALPHA_DECCXX) || \
  224. defined(PEGASUS_PLATFORM_WIN64_IA64_MSVC) || \
  225. defined(PEGASUS_PLATFORM_WIN64_X86_64_MSVC) || \
  226. defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC)
  227. typedef int SocketLength;
  228. #elif defined(PEGASUS_PLATFORM_VMS_ALPHA_DECCXX) || \
  229. defined(PEGASUS_PLATFORM_VMS_IA64_DECCXX)
  230. typedef size_t SocketLength;
  231. #elif defined(PEGASUS_OS_ZOS)
  232. typedef socklen_t SocketLength;
  233. #elif defined(PEGASUS_PLATFORM_HPUX_IA64_ACC) && \
  234. !defined(_XOPEN_SOURCE_EXTENDED)
  235. typedef int SocketLength;
  236. #elif defined(PEGASUS_PLATFORM_HPUX_PARISC_ACC) && \
  237. !defined(_XOPEN_SOURCE_EXTENDED)
  238. typedef int SocketLength;
  239. #else
  240. typedef socklen_t SocketLength;
  241. #endif
  242. #endif /* Pegasus_Network_h */