You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
6.7 KiB

24 years ago
  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. # if !(defined(__BEOS__) || defined(__CYGWIN__) || (defined(PYOS_OS2) && defined(PYCC_VACPP)))
  11. # include <netinet/tcp.h>
  12. # endif
  13. #else /* MS_WINDOWS */
  14. # include <winsock2.h>
  15. # include <ws2tcpip.h>
  16. /* VC6 is shipped with old platform headers, and does not have MSTcpIP.h
  17. * Separate SDKs have all the functions we want, but older ones don't have
  18. * any version information.
  19. * I use SIO_GET_MULTICAST_FILTER to detect a decent SDK.
  20. */
  21. # ifdef SIO_GET_MULTICAST_FILTER
  22. # include <MSTcpIP.h> /* for SIO_RCVALL */
  23. # define HAVE_ADDRINFO
  24. # define HAVE_SOCKADDR_STORAGE
  25. # define HAVE_GETADDRINFO
  26. # define HAVE_GETNAMEINFO
  27. # define ENABLE_IPV6
  28. # else
  29. typedef int socklen_t;
  30. # endif /* IPPROTO_IPV6 */
  31. #endif /* MS_WINDOWS */
  32. #ifdef HAVE_SYS_UN_H
  33. # include <sys/un.h>
  34. #else
  35. # undef AF_UNIX
  36. #endif
  37. #ifdef HAVE_LINUX_NETLINK_H
  38. # ifdef HAVE_ASM_TYPES_H
  39. # include <asm/types.h>
  40. # endif
  41. # include <linux/netlink.h>
  42. #else
  43. # undef AF_NETLINK
  44. #endif
  45. #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
  46. #include <bluetooth/bluetooth.h>
  47. #include <bluetooth/rfcomm.h>
  48. #include <bluetooth/l2cap.h>
  49. #include <bluetooth/sco.h>
  50. #include <bluetooth/hci.h>
  51. #endif
  52. #ifdef HAVE_BLUETOOTH_H
  53. #include <bluetooth.h>
  54. #endif
  55. #ifdef HAVE_NETPACKET_PACKET_H
  56. # include <sys/ioctl.h>
  57. # include <net/if.h>
  58. # include <netpacket/packet.h>
  59. #endif
  60. #ifdef HAVE_LINUX_TIPC_H
  61. # include <linux/tipc.h>
  62. #endif
  63. #ifndef Py__SOCKET_H
  64. #define Py__SOCKET_H
  65. #ifdef __cplusplus
  66. extern "C" {
  67. #endif
  68. /* Python module and C API name */
  69. #define PySocket_MODULE_NAME "_socket"
  70. #define PySocket_CAPI_NAME "CAPI"
  71. #define PySocket_CAPSULE_NAME (PySocket_MODULE_NAME "." PySocket_CAPI_NAME)
  72. /* Abstract the socket file descriptor type */
  73. #ifdef MS_WINDOWS
  74. typedef SOCKET SOCKET_T;
  75. # ifdef MS_WIN64
  76. # define SIZEOF_SOCKET_T 8
  77. # else
  78. # define SIZEOF_SOCKET_T 4
  79. # endif
  80. #else
  81. typedef int SOCKET_T;
  82. # define SIZEOF_SOCKET_T SIZEOF_INT
  83. #endif
  84. /* Socket address */
  85. typedef union sock_addr {
  86. struct sockaddr_in in;
  87. #ifdef AF_UNIX
  88. struct sockaddr_un un;
  89. #endif
  90. #ifdef AF_NETLINK
  91. struct sockaddr_nl nl;
  92. #endif
  93. #ifdef ENABLE_IPV6
  94. struct sockaddr_in6 in6;
  95. struct sockaddr_storage storage;
  96. #endif
  97. #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
  98. struct sockaddr_l2 bt_l2;
  99. struct sockaddr_rc bt_rc;
  100. struct sockaddr_sco bt_sco;
  101. struct sockaddr_hci bt_hci;
  102. #endif
  103. #ifdef HAVE_NETPACKET_PACKET_H
  104. struct sockaddr_ll ll;
  105. #endif
  106. } sock_addr_t;
  107. /* The object holding a socket. It holds some extra information,
  108. like the address family, which is used to decode socket address
  109. arguments properly. */
  110. typedef struct {
  111. PyObject_HEAD
  112. SOCKET_T sock_fd; /* Socket file descriptor */
  113. int sock_family; /* Address family, e.g., AF_INET */
  114. int sock_type; /* Socket type, e.g., SOCK_STREAM */
  115. int sock_proto; /* Protocol type, usually 0 */
  116. PyObject *(*errorhandler)(void); /* Error handler; checks
  117. errno, returns NULL and
  118. sets a Python exception */
  119. double sock_timeout; /* Operation timeout in seconds;
  120. 0.0 means non-blocking */
  121. } PySocketSockObject;
  122. /* --- C API ----------------------------------------------------*/
  123. /* Short explanation of what this C API export mechanism does
  124. and how it works:
  125. The _ssl module needs access to the type object defined in
  126. the _socket module. Since cross-DLL linking introduces a lot of
  127. problems on many platforms, the "trick" is to wrap the
  128. C API of a module in a struct which then gets exported to
  129. other modules via a PyCapsule.
  130. The code in socketmodule.c defines this struct (which currently
  131. only contains the type object reference, but could very
  132. well also include other C APIs needed by other modules)
  133. and exports it as PyCapsule via the module dictionary
  134. under the name "CAPI".
  135. Other modules can now include the socketmodule.h file
  136. which defines the needed C APIs to import and set up
  137. a static copy of this struct in the importing module.
  138. After initialization, the importing module can then
  139. access the C APIs from the _socket module by simply
  140. referring to the static struct, e.g.
  141. Load _socket module and its C API; this sets up the global
  142. PySocketModule:
  143. if (PySocketModule_ImportModuleAndAPI())
  144. return;
  145. Now use the C API as if it were defined in the using
  146. module:
  147. if (!PyArg_ParseTuple(args, "O!|zz:ssl",
  148. PySocketModule.Sock_Type,
  149. (PyObject*)&Sock,
  150. &key_file, &cert_file))
  151. return NULL;
  152. Support could easily be extended to export more C APIs/symbols
  153. this way. Currently, only the type object is exported,
  154. other candidates would be socket constructors and socket
  155. access functions.
  156. */
  157. /* C API for usage by other Python modules */
  158. typedef struct {
  159. PyTypeObject *Sock_Type;
  160. PyObject *error;
  161. } PySocketModule_APIObject;
  162. /* XXX The net effect of the following appears to be to define a function
  163. XXX named PySocketModule_APIObject in _ssl.c. It's unclear why it isn't
  164. XXX defined there directly.
  165. >>> It's defined here because other modules might also want to use
  166. >>> the C API.
  167. */
  168. #ifndef PySocket_BUILDING_SOCKET
  169. /* --- C API ----------------------------------------------------*/
  170. /* Interfacestructure to C API for other modules.
  171. Call PySocketModule_ImportModuleAndAPI() to initialize this
  172. structure. After that usage is simple:
  173. if (!PyArg_ParseTuple(args, "O!|zz:ssl",
  174. &PySocketModule.Sock_Type, (PyObject*)&Sock,
  175. &key_file, &cert_file))
  176. return NULL;
  177. ...
  178. */
  179. static
  180. PySocketModule_APIObject PySocketModule;
  181. /* You *must* call this before using any of the functions in
  182. PySocketModule and check its outcome; otherwise all accesses will
  183. result in a segfault. Returns 0 on success. */
  184. #ifndef DPRINTF
  185. # define DPRINTF if (0) printf
  186. #endif
  187. static
  188. int PySocketModule_ImportModuleAndAPI(void)
  189. {
  190. void *api;
  191. DPRINTF(" Loading capsule %s\n", PySocket_CAPSULE_NAME);
  192. api = PyCapsule_Import(PySocket_CAPSULE_NAME, 1);
  193. if (api == NULL)
  194. goto onError;
  195. memcpy(&PySocketModule, api, sizeof(PySocketModule));
  196. DPRINTF(" API object loaded and initialized.\n");
  197. return 0;
  198. onError:
  199. DPRINTF(" not found.\n");
  200. return -1;
  201. }
  202. #endif /* !PySocket_BUILDING_SOCKET */
  203. #ifdef __cplusplus
  204. }
  205. #endif
  206. #endif /* !Py__SOCKET_H */