blob: 7684e59cd45462e79fba819dc76685fe29bf1afd [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>
Jesus Cea14c81ab2012-10-05 02:11:36 +020011# if !defined(__CYGWIN__)
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>
Greg Bowser8fbece12019-08-02 16:29:52 -040017
18/*
19 * If Windows has bluetooth support, include bluetooth constants.
20 */
21#ifdef AF_BTH
22# include <ws2bth.h>
23# include <pshpack1.h>
24
25/*
26 * The current implementation assumes the bdaddr in the sockaddr structs
27 * will be a bdaddr_t. We treat this as an opaque type: on *nix systems, it
28 * will be a struct with a single member (an array of six bytes). On windows,
29 * we typedef this to ULONGLONG to match the Windows definition.
30 */
31typedef ULONGLONG bdaddr_t;
32
33/*
34 * Redefine SOCKADDR_BTH to provide names compatible with _BT_RC_MEMB() macros.
35 */
36struct SOCKADDR_BTH_REDEF {
37 union {
38 USHORT addressFamily;
39 USHORT family;
40 };
41
42 union {
43 ULONGLONG btAddr;
44 bdaddr_t bdaddr;
45 };
46
47 GUID serviceClassId;
48
49 union {
50 ULONG port;
51 ULONG channel;
52 };
53
54};
55# include <poppack.h>
56#endif
57
Steve Dower3e96f322015-03-02 08:01:10 -080058/* Windows 'supports' CMSG_LEN, but does not follow the POSIX standard
59 * interface at all, so there is no point including the code that
60 * attempts to use it.
61 */
62# ifdef PySocket_BUILDING_SOCKET
63# undef CMSG_LEN
64# endif
Martin v. Löwis272cb402002-03-01 08:31:07 +000065# include <ws2tcpip.h>
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +000066/* VC6 is shipped with old platform headers, and does not have MSTcpIP.h
67 * Separate SDKs have all the functions we want, but older ones don't have
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 * any version information.
Martin v. Löwisb072cf22008-06-14 11:59:52 +000069 * I use SIO_GET_MULTICAST_FILTER to detect a decent SDK.
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +000070 */
Martin v. Löwisb072cf22008-06-14 11:59:52 +000071# ifdef SIO_GET_MULTICAST_FILTER
erikjanss6cf82552018-07-25 02:41:46 +020072# include <mstcpip.h> /* for SIO_RCVALL */
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +000073# define HAVE_ADDRINFO
74# define HAVE_SOCKADDR_STORAGE
75# define HAVE_GETADDRINFO
76# define HAVE_GETNAMEINFO
77# define ENABLE_IPV6
78# else
79typedef int socklen_t;
80# endif /* IPPROTO_IPV6 */
81#endif /* MS_WINDOWS */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000082
83#ifdef HAVE_SYS_UN_H
84# include <sys/un.h>
85#else
86# undef AF_UNIX
87#endif
88
Martin v. Löwis11017b12006-01-14 18:12:57 +000089#ifdef HAVE_LINUX_NETLINK_H
Neal Norwitz65851662006-01-16 04:31:40 +000090# ifdef HAVE_ASM_TYPES_H
91# include <asm/types.h>
92# endif
Martin v. Löwis11017b12006-01-14 18:12:57 +000093# include <linux/netlink.h>
94#else
95# undef AF_NETLINK
96#endif
97
Bjorn Anderssonbb816512018-09-26 06:47:52 -070098#ifdef HAVE_LINUX_QRTR_H
99# ifdef HAVE_ASM_TYPES_H
100# include <asm/types.h>
101# endif
102# include <linux/qrtr.h>
103#else
104# undef AF_QIPCRTR
105#endif
106
Martin v. Löwis12af0482004-01-31 12:34:17 +0000107#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
108#include <bluetooth/bluetooth.h>
109#include <bluetooth/rfcomm.h>
110#include <bluetooth/l2cap.h>
111#include <bluetooth/sco.h>
Thomas Wouterscf297e42007-02-23 15:07:44 +0000112#include <bluetooth/hci.h>
Martin v. Löwis12af0482004-01-31 12:34:17 +0000113#endif
114
Hye-Shik Chang96c446582004-02-02 08:48:45 +0000115#ifdef HAVE_BLUETOOTH_H
116#include <bluetooth.h>
117#endif
118
Nadeem Vawdad74b5932011-05-15 13:16:22 +0200119#ifdef HAVE_NET_IF_H
120# include <net/if.h>
121#endif
122
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000123#ifdef HAVE_NETPACKET_PACKET_H
124# include <sys/ioctl.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000125# include <netpacket/packet.h>
126#endif
127
Christian Heimes043d6f62008-01-07 17:19:16 +0000128#ifdef HAVE_LINUX_TIPC_H
129# include <linux/tipc.h>
130#endif
131
Charles-François Natali47413c12011-10-06 19:47:44 +0200132#ifdef HAVE_LINUX_CAN_H
Serhiy Storchakad3187152017-11-09 18:00:38 +0200133# include <linux/can.h>
134#else
135# undef AF_CAN
136# undef PF_CAN
Charles-François Natali47413c12011-10-06 19:47:44 +0200137#endif
138
139#ifdef HAVE_LINUX_CAN_RAW_H
140#include <linux/can/raw.h>
141#endif
142
Charles-François Natali773e42d2013-02-05 19:42:01 +0100143#ifdef HAVE_LINUX_CAN_BCM_H
144#include <linux/can/bcm.h>
145#endif
146
Martin v. Löwis9d6c6692012-02-03 17:44:58 +0100147#ifdef HAVE_SYS_SYS_DOMAIN_H
148#include <sys/sys_domain.h>
149#endif
150#ifdef HAVE_SYS_KERN_CONTROL_H
151#include <sys/kern_control.h>
152#endif
153
caaveryeffc12f2017-09-06 18:18:10 -0400154#ifdef HAVE_LINUX_VM_SOCKETS_H
155# include <linux/vm_sockets.h>
156#else
157# undef AF_VSOCK
158#endif
159
Thomas Herzog2d7102e2018-09-02 00:13:57 +0200160#ifdef HAVE_SOCKADDR_ALG
Christian Heimesd37c0682017-02-22 12:12:00 +0100161
Thomas Herzog2d7102e2018-09-02 00:13:57 +0200162# include <linux/if_alg.h>
163# ifndef AF_ALG
164# define AF_ALG 38
165# endif
166# ifndef SOL_ALG
167# define SOL_ALG 279
168# endif
169
170/* Linux 3.19 */
171# ifndef ALG_SET_AEAD_ASSOCLEN
172# define ALG_SET_AEAD_ASSOCLEN 4
173# endif
174# ifndef ALG_SET_AEAD_AUTHSIZE
175# define ALG_SET_AEAD_AUTHSIZE 5
176# endif
177/* Linux 4.8 */
178# ifndef ALG_SET_PUBKEY
179# define ALG_SET_PUBKEY 6
180# endif
181
182# ifndef ALG_OP_SIGN
183# define ALG_OP_SIGN 2
184# endif
185# ifndef ALG_OP_VERIFY
186# define ALG_OP_VERIFY 3
187# endif
Christian Heimesd37c0682017-02-22 12:12:00 +0100188
189#endif /* HAVE_SOCKADDR_ALG */
190
191
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000192#ifndef Py__SOCKET_H
193#define Py__SOCKET_H
194#ifdef __cplusplus
195extern "C" {
196#endif
197
198/* Python module and C API name */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199#define PySocket_MODULE_NAME "_socket"
200#define PySocket_CAPI_NAME "CAPI"
201#define PySocket_CAPSULE_NAME PySocket_MODULE_NAME "." PySocket_CAPI_NAME
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000202
203/* Abstract the socket file descriptor type */
204#ifdef MS_WINDOWS
205typedef SOCKET SOCKET_T;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206# ifdef MS_WIN64
207# define SIZEOF_SOCKET_T 8
208# else
209# define SIZEOF_SOCKET_T 4
210# endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000211#else
212typedef int SOCKET_T;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213# define SIZEOF_SOCKET_T SIZEOF_INT
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000214#endif
215
Antoine Pitrou67c7ce42010-08-28 20:42:55 +0000216#if SIZEOF_SOCKET_T <= SIZEOF_LONG
217#define PyLong_FromSocket_t(fd) PyLong_FromLong((SOCKET_T)(fd))
218#define PyLong_AsSocket_t(fd) (SOCKET_T)PyLong_AsLong(fd)
219#else
220#define PyLong_FromSocket_t(fd) PyLong_FromLongLong((SOCKET_T)(fd))
221#define PyLong_AsSocket_t(fd) (SOCKET_T)PyLong_AsLongLong(fd)
222#endif
223
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000224/* Socket address */
225typedef union sock_addr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 struct sockaddr_in in;
Charles-François Natali8b759652011-12-23 16:44:51 +0100227 struct sockaddr sa;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000228#ifdef AF_UNIX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 struct sockaddr_un un;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000230#endif
Martin v. Löwis11017b12006-01-14 18:12:57 +0000231#ifdef AF_NETLINK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 struct sockaddr_nl nl;
Martin v. Löwis11017b12006-01-14 18:12:57 +0000233#endif
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000234#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 struct sockaddr_in6 in6;
236 struct sockaddr_storage storage;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000237#endif
Pablo Galindo27b33fb2019-10-13 02:03:54 +0100238#if defined(HAVE_BLUETOOTH_H) && defined(__FreeBSD__)
239 struct sockaddr_l2cap bt_l2;
240 struct sockaddr_rfcomm bt_rc;
241 struct sockaddr_sco bt_sco;
242 struct sockaddr_hci bt_hci;
243#elif defined(HAVE_BLUETOOTH_BLUETOOTH_H)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 struct sockaddr_l2 bt_l2;
245 struct sockaddr_rc bt_rc;
246 struct sockaddr_sco bt_sco;
247 struct sockaddr_hci bt_hci;
Greg Bowser8fbece12019-08-02 16:29:52 -0400248#elif defined(MS_WINDOWS)
249 struct SOCKADDR_BTH_REDEF bt_rc;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000250#endif
251#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 struct sockaddr_ll ll;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000253#endif
Charles-François Natali47413c12011-10-06 19:47:44 +0200254#ifdef HAVE_LINUX_CAN_H
255 struct sockaddr_can can;
256#endif
Martin v. Löwis9d6c6692012-02-03 17:44:58 +0100257#ifdef HAVE_SYS_KERN_CONTROL_H
258 struct sockaddr_ctl ctl;
259#endif
Christian Heimesd37c0682017-02-22 12:12:00 +0100260#ifdef HAVE_SOCKADDR_ALG
261 struct sockaddr_alg alg;
262#endif
Bjorn Anderssonbb816512018-09-26 06:47:52 -0700263#ifdef AF_QIPCRTR
264 struct sockaddr_qrtr sq;
265#endif
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +0300266#ifdef AF_VSOCK
caaveryeffc12f2017-09-06 18:18:10 -0400267 struct sockaddr_vm vm;
268#endif
Victor Stinnerd565fb92019-10-10 21:30:20 +0200269#ifdef HAVE_LINUX_TIPC_H
270 struct sockaddr_tipc tipc;
271#endif
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000272} sock_addr_t;
273
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000274/* The object holding a socket. It holds some extra information,
275 like the address family, which is used to decode socket address
276 arguments properly. */
277
278typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 PyObject_HEAD
280 SOCKET_T sock_fd; /* Socket file descriptor */
281 int sock_family; /* Address family, e.g., AF_INET */
282 int sock_type; /* Socket type, e.g., SOCK_STREAM */
283 int sock_proto; /* Protocol type, usually 0 */
284 PyObject *(*errorhandler)(void); /* Error handler; checks
285 errno, returns NULL and
286 sets a Python exception */
Victor Stinner71694d52015-03-28 01:18:54 +0100287 _PyTime_t sock_timeout; /* Operation timeout in seconds;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 0.0 means non-blocking */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000289} PySocketSockObject;
290
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000291/* --- C API ----------------------------------------------------*/
292
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000293/* Short explanation of what this C API export mechanism does
294 and how it works:
295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 The _ssl module needs access to the type object defined in
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000297 the _socket module. Since cross-DLL linking introduces a lot of
298 problems on many platforms, the "trick" is to wrap the
299 C API of a module in a struct which then gets exported to
Benjamin Petersonb173f782009-05-05 22:31:58 +0000300 other modules via a PyCapsule.
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000301
302 The code in socketmodule.c defines this struct (which currently
303 only contains the type object reference, but could very
304 well also include other C APIs needed by other modules)
Benjamin Petersonb173f782009-05-05 22:31:58 +0000305 and exports it as PyCapsule via the module dictionary
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000306 under the name "CAPI".
307
308 Other modules can now include the socketmodule.h file
309 which defines the needed C APIs to import and set up
310 a static copy of this struct in the importing module.
311
312 After initialization, the importing module can then
313 access the C APIs from the _socket module by simply
314 referring to the static struct, e.g.
315
316 Load _socket module and its C API; this sets up the global
317 PySocketModule:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318
319 if (PySocketModule_ImportModuleAndAPI())
320 return;
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000321
322
323 Now use the C API as if it were defined in the using
324 module:
325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 PySocketModule.Sock_Type,
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 (PyObject*)&Sock,
331 &key_file, &cert_file))
332 return NULL;
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000333
334 Support could easily be extended to export more C APIs/symbols
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 this way. Currently, only the type object is exported,
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000336 other candidates would be socket constructors and socket
337 access functions.
338
339*/
340
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000341/* C API for usage by other Python modules */
342typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 PyTypeObject *Sock_Type;
344 PyObject *error;
Antoine Pitrouc4df7842010-12-03 19:59:41 +0000345 PyObject *timeout_error;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000346} PySocketModule_APIObject;
Tim Peters6f5505a2002-02-17 03:58:51 +0000347
Benjamin Peterson81c447f2009-05-06 02:43:58 +0000348#define PySocketModule_ImportModuleAndAPI() PyCapsule_Import(PySocket_CAPSULE_NAME, 1)
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000349
Benjamin Peterson23490202009-08-12 18:11:03 +0000350#ifdef __cplusplus
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000351}
352#endif
353#endif /* !Py__SOCKET_H */