socketmodule.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* Socket module header file */
  2. /* Includes needed for the sockaddr_* symbols below */
  3. #ifndef MS_WINDOWS
  4. #ifdef __VMS
  5. # include <socket.h>
  6. # else
  7. # include <sys/socket.h>
  8. # endif
  9. # include <netinet/in.h>
  10. # include <netinet/tcp.h>
  11. #else /* MS_WINDOWS */
  12. # include <winsock2.h>
  13. /*
  14. * If Windows has bluetooth support, include bluetooth constants.
  15. */
  16. #ifdef AF_BTH
  17. # include <ws2bth.h>
  18. # include <pshpack1.h>
  19. /*
  20. * The current implementation assumes the bdaddr in the sockaddr structs
  21. * will be a bdaddr_t. We treat this as an opaque type: on *nix systems, it
  22. * will be a struct with a single member (an array of six bytes). On windows,
  23. * we typedef this to ULONGLONG to match the Windows definition.
  24. */
  25. typedef ULONGLONG bdaddr_t;
  26. /*
  27. * Redefine SOCKADDR_BTH to provide names compatible with _BT_RC_MEMB() macros.
  28. */
  29. struct SOCKADDR_BTH_REDEF {
  30. union {
  31. USHORT addressFamily;
  32. USHORT family;
  33. };
  34. union {
  35. ULONGLONG btAddr;
  36. bdaddr_t bdaddr;
  37. };
  38. GUID serviceClassId;
  39. union {
  40. ULONG port;
  41. ULONG channel;
  42. };
  43. };
  44. # include <poppack.h>
  45. #endif
  46. /* Windows 'supports' CMSG_LEN, but does not follow the POSIX standard
  47. * interface at all, so there is no point including the code that
  48. * attempts to use it.
  49. */
  50. # ifdef PySocket_BUILDING_SOCKET
  51. # undef CMSG_LEN
  52. # endif
  53. # include <ws2tcpip.h>
  54. /* VC6 is shipped with old platform headers, and does not have MSTcpIP.h
  55. * Separate SDKs have all the functions we want, but older ones don't have
  56. * any version information.
  57. * I use SIO_GET_MULTICAST_FILTER to detect a decent SDK.
  58. */
  59. # ifdef SIO_GET_MULTICAST_FILTER
  60. # include <mstcpip.h> /* for SIO_RCVALL */
  61. # define HAVE_ADDRINFO
  62. # define HAVE_SOCKADDR_STORAGE
  63. # define HAVE_GETADDRINFO
  64. # define HAVE_GETNAMEINFO
  65. # define ENABLE_IPV6
  66. # else
  67. typedef int socklen_t;
  68. # endif /* IPPROTO_IPV6 */
  69. /* Remove ifdef once Py_WINVER >= 0x0604
  70. * socket.h only defines AF_HYPERV if _WIN32_WINNT is at that level or higher
  71. * so for now it's just manually defined.
  72. */
  73. # ifndef AF_HYPERV
  74. # define AF_HYPERV 34
  75. # endif
  76. # include <hvsocket.h>
  77. #endif /* MS_WINDOWS */
  78. #ifdef HAVE_SYS_UN_H
  79. # include <sys/un.h>
  80. #else
  81. # undef AF_UNIX
  82. #endif
  83. #ifdef HAVE_LINUX_NETLINK_H
  84. # ifdef HAVE_ASM_TYPES_H
  85. # include <asm/types.h>
  86. # endif
  87. # include <linux/netlink.h>
  88. #else
  89. # undef AF_NETLINK
  90. #endif
  91. #ifdef HAVE_LINUX_QRTR_H
  92. # ifdef HAVE_ASM_TYPES_H
  93. # include <asm/types.h>
  94. # endif
  95. # include <linux/qrtr.h>
  96. #else
  97. # undef AF_QIPCRTR
  98. #endif
  99. #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
  100. #error #include <bluetooth/bluetooth.h>
  101. #error #include <bluetooth/rfcomm.h>
  102. #error #include <bluetooth/l2cap.h>
  103. #error #include <bluetooth/sco.h>
  104. #error #include <bluetooth/hci.h>
  105. #endif
  106. #ifdef HAVE_BLUETOOTH_H
  107. #error #include <bluetooth.h>
  108. #endif
  109. #ifdef HAVE_NET_IF_H
  110. # include <net/if.h>
  111. #endif
  112. #ifdef HAVE_NETPACKET_PACKET_H
  113. # include <sys/ioctl.h>
  114. # include <netpacket/packet.h>
  115. #endif
  116. #ifdef HAVE_LINUX_TIPC_H
  117. # include <linux/tipc.h>
  118. #endif
  119. #ifdef HAVE_LINUX_CAN_H
  120. # include <linux/can.h>
  121. #elif defined(HAVE_NETCAN_CAN_H)
  122. # error #include <netcan/can.h>
  123. #else
  124. # undef AF_CAN
  125. # undef PF_CAN
  126. #endif
  127. #ifdef HAVE_LINUX_CAN_RAW_H
  128. #include <linux/can/raw.h>
  129. #endif
  130. #ifdef HAVE_LINUX_CAN_BCM_H
  131. #include <linux/can/bcm.h>
  132. #endif
  133. #ifdef HAVE_LINUX_CAN_J1939_H
  134. #include <linux/can/j1939.h>
  135. #endif
  136. #ifdef HAVE_SYS_SYS_DOMAIN_H
  137. #include <sys/sys_domain.h>
  138. #endif
  139. #ifdef HAVE_SYS_KERN_CONTROL_H
  140. #include <sys/kern_control.h>
  141. #endif
  142. #ifdef HAVE_LINUX_VM_SOCKETS_H
  143. # include <linux/vm_sockets.h>
  144. #else
  145. # undef AF_VSOCK
  146. #endif
  147. #ifdef HAVE_SOCKADDR_ALG
  148. # include <linux/if_alg.h>
  149. # ifndef AF_ALG
  150. # define AF_ALG 38
  151. # endif
  152. # ifndef SOL_ALG
  153. # define SOL_ALG 279
  154. # endif
  155. /* Linux 3.19 */
  156. # ifndef ALG_SET_AEAD_ASSOCLEN
  157. # define ALG_SET_AEAD_ASSOCLEN 4
  158. # endif
  159. # ifndef ALG_SET_AEAD_AUTHSIZE
  160. # define ALG_SET_AEAD_AUTHSIZE 5
  161. # endif
  162. /* Linux 4.8 */
  163. # ifndef ALG_SET_PUBKEY
  164. # define ALG_SET_PUBKEY 6
  165. # endif
  166. # ifndef ALG_OP_SIGN
  167. # define ALG_OP_SIGN 2
  168. # endif
  169. # ifndef ALG_OP_VERIFY
  170. # define ALG_OP_VERIFY 3
  171. # endif
  172. #endif /* HAVE_SOCKADDR_ALG */
  173. #ifdef __EMSCRIPTEN__
  174. // wasm32-emscripten sockets only support subset of IPv4 and IPv6.
  175. // SCTP protocol crashes runtime.
  176. #ifdef IPPROTO_SCTP
  177. # undef IPPROTO_SCTP
  178. #endif
  179. // setsockopt() fails with ENOPROTOOPT, getsockopt only supports SO_ERROR.
  180. // undef SO_REUSEADDR and SO_REUSEPORT so they cannot be used.
  181. #ifdef SO_REUSEADDR
  182. # undef SO_REUSEADDR
  183. #endif
  184. #ifdef SO_REUSEPORT
  185. # undef SO_REUSEPORT
  186. #endif
  187. #endif // __EMSCRIPTEN__
  188. #ifndef Py__SOCKET_H
  189. #define Py__SOCKET_H
  190. #ifdef __cplusplus
  191. extern "C" {
  192. #endif
  193. /* Python module and C API name */
  194. #define PySocket_MODULE_NAME "_socket"
  195. #define PySocket_CAPI_NAME "CAPI"
  196. #define PySocket_CAPSULE_NAME PySocket_MODULE_NAME "." PySocket_CAPI_NAME
  197. /* Abstract the socket file descriptor type */
  198. #ifdef MS_WINDOWS
  199. typedef SOCKET SOCKET_T;
  200. # ifdef MS_WIN64
  201. # define SIZEOF_SOCKET_T 8
  202. # else
  203. # define SIZEOF_SOCKET_T 4
  204. # endif
  205. #else
  206. typedef int SOCKET_T;
  207. # define SIZEOF_SOCKET_T SIZEOF_INT
  208. #endif
  209. #if SIZEOF_SOCKET_T <= SIZEOF_LONG
  210. #define PyLong_FromSocket_t(fd) PyLong_FromLong((SOCKET_T)(fd))
  211. #define PyLong_AsSocket_t(fd) (SOCKET_T)PyLong_AsLong(fd)
  212. #else
  213. #define PyLong_FromSocket_t(fd) PyLong_FromLongLong((SOCKET_T)(fd))
  214. #define PyLong_AsSocket_t(fd) (SOCKET_T)PyLong_AsLongLong(fd)
  215. #endif
  216. // AF_HYPERV is only supported on Windows
  217. #if defined(AF_HYPERV) && defined(MS_WINDOWS)
  218. # define HAVE_AF_HYPERV
  219. #endif
  220. /* Socket address */
  221. typedef union sock_addr {
  222. struct sockaddr_in in;
  223. struct sockaddr sa;
  224. #ifdef AF_UNIX
  225. struct sockaddr_un un;
  226. #endif
  227. #ifdef AF_NETLINK
  228. struct sockaddr_nl nl;
  229. #endif
  230. #ifdef ENABLE_IPV6
  231. struct sockaddr_in6 in6;
  232. struct sockaddr_storage storage;
  233. #endif
  234. #if defined(HAVE_BLUETOOTH_H) && defined(__FreeBSD__)
  235. struct sockaddr_l2cap bt_l2;
  236. struct sockaddr_rfcomm bt_rc;
  237. struct sockaddr_sco bt_sco;
  238. struct sockaddr_hci bt_hci;
  239. #elif defined(HAVE_BLUETOOTH_BLUETOOTH_H)
  240. struct sockaddr_l2 bt_l2;
  241. struct sockaddr_rc bt_rc;
  242. struct sockaddr_sco bt_sco;
  243. struct sockaddr_hci bt_hci;
  244. #elif defined(MS_WINDOWS)
  245. struct SOCKADDR_BTH_REDEF bt_rc;
  246. #endif
  247. #ifdef HAVE_NETPACKET_PACKET_H
  248. struct sockaddr_ll ll;
  249. #endif
  250. #if defined(HAVE_LINUX_CAN_H) || defined(HAVE_NETCAN_CAN_H)
  251. struct sockaddr_can can;
  252. #endif
  253. #ifdef HAVE_SYS_KERN_CONTROL_H
  254. struct sockaddr_ctl ctl;
  255. #endif
  256. #ifdef HAVE_SOCKADDR_ALG
  257. struct sockaddr_alg alg;
  258. #endif
  259. #ifdef AF_QIPCRTR
  260. struct sockaddr_qrtr sq;
  261. #endif
  262. #ifdef AF_VSOCK
  263. struct sockaddr_vm vm;
  264. #endif
  265. #ifdef HAVE_LINUX_TIPC_H
  266. struct sockaddr_tipc tipc;
  267. #endif
  268. #ifdef HAVE_AF_HYPERV
  269. SOCKADDR_HV hv;
  270. #endif
  271. } sock_addr_t;
  272. /* The object holding a socket. It holds some extra information,
  273. like the address family, which is used to decode socket address
  274. arguments properly. */
  275. typedef struct {
  276. PyObject_HEAD
  277. SOCKET_T sock_fd; /* Socket file descriptor */
  278. int sock_family; /* Address family, e.g., AF_INET */
  279. int sock_type; /* Socket type, e.g., SOCK_STREAM */
  280. int sock_proto; /* Protocol type, usually 0 */
  281. PyObject *(*errorhandler)(void); /* Error handler; checks
  282. errno, returns NULL and
  283. sets a Python exception */
  284. _PyTime_t sock_timeout; /* Operation timeout in seconds;
  285. 0.0 means non-blocking */
  286. struct _socket_state *state;
  287. } PySocketSockObject;
  288. /* --- C API ----------------------------------------------------*/
  289. /* Short explanation of what this C API export mechanism does
  290. and how it works:
  291. The _ssl module needs access to the type object defined in
  292. the _socket module. Since cross-DLL linking introduces a lot of
  293. problems on many platforms, the "trick" is to wrap the
  294. C API of a module in a struct which then gets exported to
  295. other modules via a PyCapsule.
  296. The code in socketmodule.c defines this struct (which currently
  297. only contains the type object reference, but could very
  298. well also include other C APIs needed by other modules)
  299. and exports it as PyCapsule via the module dictionary
  300. under the name "CAPI".
  301. Other modules can now include the socketmodule.h file
  302. which defines the needed C APIs to import and set up
  303. a static copy of this struct in the importing module.
  304. After initialization, the importing module can then
  305. access the C APIs from the _socket module by simply
  306. referring to the static struct, e.g.
  307. Load _socket module and its C API; this sets up the global
  308. PySocketModule:
  309. if (PySocketModule_ImportModuleAndAPI())
  310. return;
  311. Now use the C API as if it were defined in the using
  312. module:
  313. if (!PyArg_ParseTuple(args, "O!|zz:ssl",
  314. PySocketModule.Sock_Type,
  315. (PyObject*)&Sock,
  316. &key_file, &cert_file))
  317. return NULL;
  318. Support could easily be extended to export more C APIs/symbols
  319. this way. Currently, only the type object is exported,
  320. other candidates would be socket constructors and socket
  321. access functions.
  322. */
  323. /* C API for usage by other Python modules.
  324. * Always add new things to the end for binary compatibility. */
  325. typedef struct {
  326. PyTypeObject *Sock_Type;
  327. PyObject *error;
  328. PyObject *timeout_error;
  329. } PySocketModule_APIObject;
  330. #define PySocketModule_ImportModuleAndAPI() PyCapsule_Import(PySocket_CAPSULE_NAME, 1)
  331. #ifdef __cplusplus
  332. }
  333. #endif
  334. #endif /* !Py__SOCKET_H */