sockets.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_INTERNAL_SOCKETS_H
  10. # define OSSL_INTERNAL_SOCKETS_H
  11. # pragma once
  12. # include <openssl/opensslconf.h>
  13. # if defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)
  14. # define NO_SYS_PARAM_H
  15. # endif
  16. # ifdef WIN32
  17. # define NO_SYS_UN_H
  18. # endif
  19. # ifdef OPENSSL_SYS_VMS
  20. # define NO_SYS_PARAM_H
  21. # define NO_SYS_UN_H
  22. # endif
  23. # ifdef OPENSSL_NO_SOCK
  24. # elif defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
  25. # if defined(__DJGPP__)
  26. # define WATT32
  27. # define WATT32_NO_OLDIES
  28. # include <sys/socket.h>
  29. # include <sys/un.h>
  30. # include <tcp.h>
  31. # include <netdb.h>
  32. # include <arpa/inet.h>
  33. # include <netinet/tcp.h>
  34. # elif defined(_WIN32_WCE) && _WIN32_WCE<410
  35. # define getservbyname _masked_declaration_getservbyname
  36. # endif
  37. # if !defined(IPPROTO_IP)
  38. /* winsock[2].h was included already? */
  39. # include <winsock.h>
  40. # endif
  41. # ifdef getservbyname
  42. /* this is used to be wcecompat/include/winsock_extras.h */
  43. # undef getservbyname
  44. struct servent *PASCAL getservbyname(const char *, const char *);
  45. # endif
  46. # ifdef _WIN64
  47. /*
  48. * Even though sizeof(SOCKET) is 8, it's safe to cast it to int, because
  49. * the value constitutes an index in per-process table of limited size
  50. * and not a real pointer. And we also depend on fact that all processors
  51. * Windows run on happen to be two's-complement, which allows to
  52. * interchange INVALID_SOCKET and -1.
  53. */
  54. # define socket(d,t,p) ((int)socket(d,t,p))
  55. # define accept(s,f,l) ((int)accept(s,f,l))
  56. # endif
  57. /* Windows have other names for shutdown() reasons */
  58. # ifndef SHUT_RD
  59. # define SHUT_RD SD_RECEIVE
  60. # endif
  61. # ifndef SHUT_WR
  62. # define SHUT_WR SD_SEND
  63. # endif
  64. # ifndef SHUT_RDWR
  65. # define SHUT_RDWR SD_BOTH
  66. # endif
  67. # else
  68. # if defined(__APPLE__)
  69. /*
  70. * This must be defined before including <netinet/in6.h> to get
  71. * IPV6_RECVPKTINFO
  72. */
  73. # define __APPLE_USE_RFC_3542
  74. # endif
  75. # ifndef NO_SYS_PARAM_H
  76. # include <sys/param.h>
  77. # endif
  78. # ifdef OPENSSL_SYS_VXWORKS
  79. # include <time.h>
  80. # endif
  81. # include <netdb.h>
  82. # if defined(OPENSSL_SYS_VMS)
  83. typedef size_t socklen_t; /* Currently appears to be missing on VMS */
  84. # endif
  85. # if defined(OPENSSL_SYS_VMS_NODECC)
  86. # include <socket.h>
  87. # include <in.h>
  88. # include <inet.h>
  89. # else
  90. # include <sys/socket.h>
  91. # if !defined(NO_SYS_UN_H) && defined(AF_UNIX) && !defined(OPENSSL_NO_UNIX_SOCK)
  92. # include <sys/un.h>
  93. # ifndef UNIX_PATH_MAX
  94. # define UNIX_PATH_MAX sizeof(((struct sockaddr_un *)NULL)->sun_path)
  95. # endif
  96. # endif
  97. # ifdef FILIO_H
  98. # include <sys/filio.h> /* FIONBIO in some SVR4, e.g. unixware, solaris */
  99. # endif
  100. # include <netinet/in.h>
  101. # include <arpa/inet.h>
  102. # include <netinet/tcp.h>
  103. # endif
  104. # ifdef OPENSSL_SYS_AIX
  105. # include <sys/select.h>
  106. # endif
  107. # ifdef OPENSSL_SYS_UNIX
  108. # ifndef OPENSSL_SYS_TANDEM
  109. # include <poll.h>
  110. # endif
  111. # include <errno.h>
  112. # endif
  113. # ifndef VMS
  114. # include <sys/ioctl.h>
  115. # else
  116. # if !defined(TCPIP_TYPE_SOCKETSHR) && defined(__VMS_VER) && (__VMS_VER > 70000000)
  117. /* ioctl is only in VMS > 7.0 and when socketshr is not used */
  118. # include <sys/ioctl.h>
  119. # endif
  120. # include <unixio.h>
  121. # if defined(TCPIP_TYPE_SOCKETSHR)
  122. # include <socketshr.h>
  123. # endif
  124. # endif
  125. # ifndef INVALID_SOCKET
  126. # define INVALID_SOCKET (-1)
  127. # endif
  128. # endif
  129. /*
  130. * Some IPv6 implementations are broken, you can disable them in known
  131. * bad versions.
  132. */
  133. # if !defined(OPENSSL_USE_IPV6)
  134. # if defined(AF_INET6)
  135. # define OPENSSL_USE_IPV6 1
  136. # else
  137. # define OPENSSL_USE_IPV6 0
  138. # endif
  139. # endif
  140. /*
  141. * Some platforms define AF_UNIX, but don't support it
  142. */
  143. # if !defined(OPENSSL_NO_UNIX_SOCK)
  144. # if !defined(AF_UNIX) || defined(NO_SYS_UN_H)
  145. # define OPENSSL_NO_UNIX_SOCK
  146. # endif
  147. # endif
  148. # define get_last_socket_error() errno
  149. # define clear_socket_error() errno=0
  150. # define get_last_socket_error_is_eintr() (get_last_socket_error() == EINTR)
  151. # if defined(OPENSSL_SYS_WINDOWS)
  152. # undef get_last_socket_error
  153. # undef clear_socket_error
  154. # undef get_last_socket_error_is_eintr
  155. # define get_last_socket_error() WSAGetLastError()
  156. # define clear_socket_error() WSASetLastError(0)
  157. # define get_last_socket_error_is_eintr() (get_last_socket_error() == WSAEINTR)
  158. # define readsocket(s,b,n) recv((s),(b),(n),0)
  159. # define writesocket(s,b,n) send((s),(b),(n),0)
  160. # elif defined(__DJGPP__)
  161. # define closesocket(s) close_s(s)
  162. # define readsocket(s,b,n) read_s(s,b,n)
  163. # define writesocket(s,b,n) send(s,b,n,0)
  164. # elif defined(OPENSSL_SYS_VMS)
  165. # define ioctlsocket(a,b,c) ioctl(a,b,c)
  166. # define closesocket(s) close(s)
  167. # define readsocket(s,b,n) recv((s),(b),(n),0)
  168. # define writesocket(s,b,n) send((s),(b),(n),0)
  169. # elif defined(OPENSSL_SYS_VXWORKS)
  170. # define ioctlsocket(a,b,c) ioctl((a),(b),(int)(c))
  171. # define closesocket(s) close(s)
  172. # define readsocket(s,b,n) read((s),(b),(n))
  173. # define writesocket(s,b,n) write((s),(char *)(b),(n))
  174. # elif defined(OPENSSL_SYS_TANDEM)
  175. # define readsocket(s,b,n) read((s),(b),(n))
  176. # define writesocket(s,b,n) write((s),(b),(n))
  177. # define ioctlsocket(a,b,c) ioctl(a,b,c)
  178. # define closesocket(s) close(s)
  179. # else
  180. # define ioctlsocket(a,b,c) ioctl(a,b,c)
  181. # define closesocket(s) close(s)
  182. # define readsocket(s,b,n) read((s),(b),(n))
  183. # define writesocket(s,b,n) write((s),(b),(n))
  184. # endif
  185. /* also in apps/include/apps.h */
  186. # if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WINCE)
  187. # define openssl_fdset(a, b) FD_SET((unsigned int)(a), b)
  188. # else
  189. # define openssl_fdset(a, b) FD_SET(a, b)
  190. # endif
  191. #endif