blob: db44fd368d350d5d79c7c463558e881d5e629961 [file] [log] [blame]
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001/* Socket module header file */
2
3/* Includes needed for the sockaddr_* symbols below */
4#ifndef MS_WINDOWS
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00005#ifdef __VMS
6# include <socket.h>
7# else
8# include <sys/socket.h>
9# endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000010# include <netinet/in.h>
Christian Heimesb5daaed2007-11-30 23:34:21 +000011# if !(defined(__CYGWIN__) || (defined(PYOS_OS2) && defined(PYCC_VACPP)))
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000012# include <netinet/tcp.h>
13# endif
14
15#else /* MS_WINDOWS */
Martin v. Löwis272cb402002-03-01 08:31:07 +000016# include <winsock2.h>
17# include <ws2tcpip.h>
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +000018/* VC6 is shipped with old platform headers, and does not have MSTcpIP.h
19 * Separate SDKs have all the functions we want, but older ones don't have
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 * any version information.
Martin v. Löwisb072cf22008-06-14 11:59:52 +000021 * I use SIO_GET_MULTICAST_FILTER to detect a decent SDK.
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +000022 */
Martin v. Löwisb072cf22008-06-14 11:59:52 +000023# ifdef SIO_GET_MULTICAST_FILTER
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +000024# include <MSTcpIP.h> /* for SIO_RCVALL */
25# define HAVE_ADDRINFO
26# define HAVE_SOCKADDR_STORAGE
27# define HAVE_GETADDRINFO
28# define HAVE_GETNAMEINFO
29# define ENABLE_IPV6
30# else
31typedef int socklen_t;
32# endif /* IPPROTO_IPV6 */
33#endif /* MS_WINDOWS */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000034
35#ifdef HAVE_SYS_UN_H
36# include <sys/un.h>
37#else
38# undef AF_UNIX
39#endif
40
Martin v. Löwis11017b12006-01-14 18:12:57 +000041#ifdef HAVE_LINUX_NETLINK_H
Neal Norwitz65851662006-01-16 04:31:40 +000042# ifdef HAVE_ASM_TYPES_H
43# include <asm/types.h>
44# endif
Martin v. Löwis11017b12006-01-14 18:12:57 +000045# include <linux/netlink.h>
46#else
47# undef AF_NETLINK
48#endif
49
Martin v. Löwis12af0482004-01-31 12:34:17 +000050#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
51#include <bluetooth/bluetooth.h>
52#include <bluetooth/rfcomm.h>
53#include <bluetooth/l2cap.h>
54#include <bluetooth/sco.h>
Thomas Wouterscf297e42007-02-23 15:07:44 +000055#include <bluetooth/hci.h>
Martin v. Löwis12af0482004-01-31 12:34:17 +000056#endif
57
Hye-Shik Chang96c446582004-02-02 08:48:45 +000058#ifdef HAVE_BLUETOOTH_H
59#include <bluetooth.h>
60#endif
61
Nadeem Vawdad74b5932011-05-15 13:16:22 +020062#ifdef HAVE_NET_IF_H
63# include <net/if.h>
64#endif
65
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000066#ifdef HAVE_NETPACKET_PACKET_H
67# include <sys/ioctl.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000068# include <netpacket/packet.h>
69#endif
70
Christian Heimes043d6f62008-01-07 17:19:16 +000071#ifdef HAVE_LINUX_TIPC_H
72# include <linux/tipc.h>
73#endif
74
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000075#ifndef Py__SOCKET_H
76#define Py__SOCKET_H
77#ifdef __cplusplus
78extern "C" {
79#endif
80
81/* Python module and C API name */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082#define PySocket_MODULE_NAME "_socket"
83#define PySocket_CAPI_NAME "CAPI"
84#define PySocket_CAPSULE_NAME PySocket_MODULE_NAME "." PySocket_CAPI_NAME
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000085
86/* Abstract the socket file descriptor type */
87#ifdef MS_WINDOWS
88typedef SOCKET SOCKET_T;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089# ifdef MS_WIN64
90# define SIZEOF_SOCKET_T 8
91# else
92# define SIZEOF_SOCKET_T 4
93# endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000094#else
95typedef int SOCKET_T;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096# define SIZEOF_SOCKET_T SIZEOF_INT
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000097#endif
98
Antoine Pitrou67c7ce42010-08-28 20:42:55 +000099#if SIZEOF_SOCKET_T <= SIZEOF_LONG
100#define PyLong_FromSocket_t(fd) PyLong_FromLong((SOCKET_T)(fd))
101#define PyLong_AsSocket_t(fd) (SOCKET_T)PyLong_AsLong(fd)
102#else
103#define PyLong_FromSocket_t(fd) PyLong_FromLongLong((SOCKET_T)(fd))
104#define PyLong_AsSocket_t(fd) (SOCKET_T)PyLong_AsLongLong(fd)
105#endif
106
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000107/* Socket address */
108typedef union sock_addr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 struct sockaddr_in in;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000110#ifdef AF_UNIX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 struct sockaddr_un un;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000112#endif
Martin v. Löwis11017b12006-01-14 18:12:57 +0000113#ifdef AF_NETLINK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 struct sockaddr_nl nl;
Martin v. Löwis11017b12006-01-14 18:12:57 +0000115#endif
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000116#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 struct sockaddr_in6 in6;
118 struct sockaddr_storage storage;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000119#endif
120#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 struct sockaddr_l2 bt_l2;
122 struct sockaddr_rc bt_rc;
123 struct sockaddr_sco bt_sco;
124 struct sockaddr_hci bt_hci;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000125#endif
126#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 struct sockaddr_ll ll;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000128#endif
129} sock_addr_t;
130
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000131/* The object holding a socket. It holds some extra information,
132 like the address family, which is used to decode socket address
133 arguments properly. */
134
135typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 PyObject_HEAD
137 SOCKET_T sock_fd; /* Socket file descriptor */
138 int sock_family; /* Address family, e.g., AF_INET */
139 int sock_type; /* Socket type, e.g., SOCK_STREAM */
140 int sock_proto; /* Protocol type, usually 0 */
141 PyObject *(*errorhandler)(void); /* Error handler; checks
142 errno, returns NULL and
143 sets a Python exception */
144 double sock_timeout; /* Operation timeout in seconds;
145 0.0 means non-blocking */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000146} PySocketSockObject;
147
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000148/* --- C API ----------------------------------------------------*/
149
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000150/* Short explanation of what this C API export mechanism does
151 and how it works:
152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 The _ssl module needs access to the type object defined in
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000154 the _socket module. Since cross-DLL linking introduces a lot of
155 problems on many platforms, the "trick" is to wrap the
156 C API of a module in a struct which then gets exported to
Benjamin Petersonb173f782009-05-05 22:31:58 +0000157 other modules via a PyCapsule.
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000158
159 The code in socketmodule.c defines this struct (which currently
160 only contains the type object reference, but could very
161 well also include other C APIs needed by other modules)
Benjamin Petersonb173f782009-05-05 22:31:58 +0000162 and exports it as PyCapsule via the module dictionary
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000163 under the name "CAPI".
164
165 Other modules can now include the socketmodule.h file
166 which defines the needed C APIs to import and set up
167 a static copy of this struct in the importing module.
168
169 After initialization, the importing module can then
170 access the C APIs from the _socket module by simply
171 referring to the static struct, e.g.
172
173 Load _socket module and its C API; this sets up the global
174 PySocketModule:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175
176 if (PySocketModule_ImportModuleAndAPI())
177 return;
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000178
179
180 Now use the C API as if it were defined in the using
181 module:
182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 PySocketModule.Sock_Type,
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 (PyObject*)&Sock,
188 &key_file, &cert_file))
189 return NULL;
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000190
191 Support could easily be extended to export more C APIs/symbols
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 this way. Currently, only the type object is exported,
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000193 other candidates would be socket constructors and socket
194 access functions.
195
196*/
197
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000198/* C API for usage by other Python modules */
199typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 PyTypeObject *Sock_Type;
201 PyObject *error;
Antoine Pitrouc4df7842010-12-03 19:59:41 +0000202 PyObject *timeout_error;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000203} PySocketModule_APIObject;
Tim Peters6f5505a2002-02-17 03:58:51 +0000204
Benjamin Peterson81c447f2009-05-06 02:43:58 +0000205#define PySocketModule_ImportModuleAndAPI() PyCapsule_Import(PySocket_CAPSULE_NAME, 1)
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000206
Benjamin Peterson23490202009-08-12 18:11:03 +0000207#ifdef __cplusplus
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000208}
209#endif
210#endif /* !Py__SOCKET_H */