sockets.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright 1995-2022 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_NODECC)
  83. # include <socket.h>
  84. # include <in.h>
  85. # include <inet.h>
  86. # else
  87. # include <sys/socket.h>
  88. # if !defined(NO_SYS_UN_H) && defined(AF_UNIX) && !defined(OPENSSL_NO_UNIX_SOCK)
  89. # include <sys/un.h>
  90. # ifndef UNIX_PATH_MAX
  91. # define UNIX_PATH_MAX sizeof(((struct sockaddr_un *)NULL)->sun_path)
  92. # endif
  93. # endif
  94. # ifdef FILIO_H
  95. # include <sys/filio.h> /* FIONBIO in some SVR4, e.g. unixware, solaris */
  96. # endif
  97. # include <netinet/in.h>
  98. # include <arpa/inet.h>
  99. # include <netinet/tcp.h>
  100. # endif
  101. # ifdef OPENSSL_SYS_AIX
  102. # include <sys/select.h>
  103. # endif
  104. # ifndef VMS
  105. # include <sys/ioctl.h>
  106. # else
  107. # if !defined(TCPIP_TYPE_SOCKETSHR) && defined(__VMS_VER) && (__VMS_VER > 70000000)
  108. /* ioctl is only in VMS > 7.0 and when socketshr is not used */
  109. # include <sys/ioctl.h>
  110. # endif
  111. # include <unixio.h>
  112. # if defined(TCPIP_TYPE_SOCKETSHR)
  113. # include <socketshr.h>
  114. # endif
  115. # endif
  116. # ifndef INVALID_SOCKET
  117. # define INVALID_SOCKET (-1)
  118. # endif
  119. # endif
  120. /*
  121. * Some IPv6 implementations are broken, you can disable them in known
  122. * bad versions.
  123. */
  124. # if !defined(OPENSSL_USE_IPV6)
  125. # if defined(AF_INET6)
  126. # define OPENSSL_USE_IPV6 1
  127. # else
  128. # define OPENSSL_USE_IPV6 0
  129. # endif
  130. # endif
  131. /*
  132. * Some platforms define AF_UNIX, but don't support it
  133. */
  134. # if !defined(OPENSSL_NO_UNIX_SOCK)
  135. # if !defined(AF_UNIX) || defined(NO_SYS_UN_H)
  136. # define OPENSSL_NO_UNIX_SOCK
  137. # endif
  138. # endif
  139. # define get_last_socket_error() errno
  140. # define clear_socket_error() errno=0
  141. # if defined(OPENSSL_SYS_WINDOWS)
  142. # undef get_last_socket_error
  143. # undef clear_socket_error
  144. # define get_last_socket_error() WSAGetLastError()
  145. # define clear_socket_error() WSASetLastError(0)
  146. # define readsocket(s,b,n) recv((s),(b),(n),0)
  147. # define writesocket(s,b,n) send((s),(b),(n),0)
  148. # elif defined(__DJGPP__)
  149. # define closesocket(s) close_s(s)
  150. # define readsocket(s,b,n) read_s(s,b,n)
  151. # define writesocket(s,b,n) send(s,b,n,0)
  152. # elif defined(OPENSSL_SYS_VMS)
  153. # define ioctlsocket(a,b,c) ioctl(a,b,c)
  154. # define closesocket(s) close(s)
  155. # define readsocket(s,b,n) recv((s),(b),(n),0)
  156. # define writesocket(s,b,n) send((s),(b),(n),0)
  157. # elif defined(OPENSSL_SYS_VXWORKS)
  158. # define ioctlsocket(a,b,c) ioctl((a),(b),(int)(c))
  159. # define closesocket(s) close(s)
  160. # define readsocket(s,b,n) read((s),(b),(n))
  161. # define writesocket(s,b,n) write((s),(char *)(b),(n))
  162. # elif defined(OPENSSL_SYS_TANDEM)
  163. # if defined(OPENSSL_TANDEM_FLOSS)
  164. # include <floss.h(floss_read, floss_write)>
  165. # define readsocket(s,b,n) floss_read((s),(b),(n))
  166. # define writesocket(s,b,n) floss_write((s),(b),(n))
  167. # else
  168. # define readsocket(s,b,n) read((s),(b),(n))
  169. # define writesocket(s,b,n) write((s),(b),(n))
  170. # endif
  171. # define ioctlsocket(a,b,c) ioctl(a,b,c)
  172. # define closesocket(s) close(s)
  173. # else
  174. # define ioctlsocket(a,b,c) ioctl(a,b,c)
  175. # define closesocket(s) close(s)
  176. # define readsocket(s,b,n) read((s),(b),(n))
  177. # define writesocket(s,b,n) write((s),(b),(n))
  178. # endif
  179. /* also in apps/include/apps.h */
  180. # if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WINCE)
  181. # define openssl_fdset(a, b) FD_SET((unsigned int)(a), b)
  182. # else
  183. # define openssl_fdset(a, b) FD_SET(a, b)
  184. # endif
  185. #endif