blob: bce74c3da8fd909da9a1fdd1a76f8841ba892979 [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>
Steve Dower3e96f322015-03-02 08:01:10 -080017/* Windows 'supports' CMSG_LEN, but does not follow the POSIX standard
18 * interface at all, so there is no point including the code that
19 * attempts to use it.
20 */
21# ifdef PySocket_BUILDING_SOCKET
22# undef CMSG_LEN
23# endif
Martin v. Löwis272cb402002-03-01 08:31:07 +000024# include <ws2tcpip.h>
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +000025/* VC6 is shipped with old platform headers, and does not have MSTcpIP.h
26 * Separate SDKs have all the functions we want, but older ones don't have
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 * any version information.
Martin v. Löwisb072cf22008-06-14 11:59:52 +000028 * I use SIO_GET_MULTICAST_FILTER to detect a decent SDK.
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +000029 */
Martin v. Löwisb072cf22008-06-14 11:59:52 +000030# ifdef SIO_GET_MULTICAST_FILTER
erikjanss6cf82552018-07-25 02:41:46 +020031# include <mstcpip.h> /* for SIO_RCVALL */
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +000032# define HAVE_ADDRINFO
33# define HAVE_SOCKADDR_STORAGE
34# define HAVE_GETADDRINFO
35# define HAVE_GETNAMEINFO
36# define ENABLE_IPV6
37# else
38typedef int socklen_t;
39# endif /* IPPROTO_IPV6 */
40#endif /* MS_WINDOWS */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000041
42#ifdef HAVE_SYS_UN_H
43# include <sys/un.h>
44#else
45# undef AF_UNIX
46#endif
47
Martin v. Löwis11017b12006-01-14 18:12:57 +000048#ifdef HAVE_LINUX_NETLINK_H
Neal Norwitz65851662006-01-16 04:31:40 +000049# ifdef HAVE_ASM_TYPES_H
50# include <asm/types.h>
51# endif
Martin v. Löwis11017b12006-01-14 18:12:57 +000052# include <linux/netlink.h>
53#else
54# undef AF_NETLINK
55#endif
56
Martin v. Löwis12af0482004-01-31 12:34:17 +000057#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
58#include <bluetooth/bluetooth.h>
59#include <bluetooth/rfcomm.h>
60#include <bluetooth/l2cap.h>
61#include <bluetooth/sco.h>
Thomas Wouterscf297e42007-02-23 15:07:44 +000062#include <bluetooth/hci.h>
Martin v. Löwis12af0482004-01-31 12:34:17 +000063#endif
64
Hye-Shik Chang96c446582004-02-02 08:48:45 +000065#ifdef HAVE_BLUETOOTH_H
66#include <bluetooth.h>
67#endif
68
Nadeem Vawdad74b5932011-05-15 13:16:22 +020069#ifdef HAVE_NET_IF_H
70# include <net/if.h>
71#endif
72
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000073#ifdef HAVE_NETPACKET_PACKET_H
74# include <sys/ioctl.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000075# include <netpacket/packet.h>
76#endif
77
Christian Heimes043d6f62008-01-07 17:19:16 +000078#ifdef HAVE_LINUX_TIPC_H
79# include <linux/tipc.h>
80#endif
81
Charles-François Natali47413c12011-10-06 19:47:44 +020082#ifdef HAVE_LINUX_CAN_H
Serhiy Storchakad3187152017-11-09 18:00:38 +020083# include <linux/can.h>
84#else
85# undef AF_CAN
86# undef PF_CAN
Charles-François Natali47413c12011-10-06 19:47:44 +020087#endif
88
89#ifdef HAVE_LINUX_CAN_RAW_H
90#include <linux/can/raw.h>
91#endif
92
Charles-François Natali773e42d2013-02-05 19:42:01 +010093#ifdef HAVE_LINUX_CAN_BCM_H
94#include <linux/can/bcm.h>
95#endif
96
Martin v. Löwis9d6c6692012-02-03 17:44:58 +010097#ifdef HAVE_SYS_SYS_DOMAIN_H
98#include <sys/sys_domain.h>
99#endif
100#ifdef HAVE_SYS_KERN_CONTROL_H
101#include <sys/kern_control.h>
102#endif
103
Christian Heimesd37c0682017-02-22 12:12:00 +0100104#ifdef HAVE_SOCKADDR_ALG
105#include <linux/if_alg.h>
106#ifndef AF_ALG
107#define AF_ALG 38
108#endif
109#ifndef SOL_ALG
110#define SOL_ALG 279
111#endif
112
caaveryeffc12f2017-09-06 18:18:10 -0400113#ifdef HAVE_LINUX_VM_SOCKETS_H
114# include <linux/vm_sockets.h>
115#else
116# undef AF_VSOCK
117#endif
118
Christian Heimesd37c0682017-02-22 12:12:00 +0100119/* Linux 3.19 */
120#ifndef ALG_SET_AEAD_ASSOCLEN
121#define ALG_SET_AEAD_ASSOCLEN 4
122#endif
123#ifndef ALG_SET_AEAD_AUTHSIZE
124#define ALG_SET_AEAD_AUTHSIZE 5
125#endif
126/* Linux 4.8 */
127#ifndef ALG_SET_PUBKEY
128#define ALG_SET_PUBKEY 6
129#endif
130
131#ifndef ALG_OP_SIGN
132#define ALG_OP_SIGN 2
133#endif
134#ifndef ALG_OP_VERIFY
135#define ALG_OP_VERIFY 3
136#endif
137
138#endif /* HAVE_SOCKADDR_ALG */
139
140
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000141#ifndef Py__SOCKET_H
142#define Py__SOCKET_H
143#ifdef __cplusplus
144extern "C" {
145#endif
146
147/* Python module and C API name */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148#define PySocket_MODULE_NAME "_socket"
149#define PySocket_CAPI_NAME "CAPI"
150#define PySocket_CAPSULE_NAME PySocket_MODULE_NAME "." PySocket_CAPI_NAME
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000151
152/* Abstract the socket file descriptor type */
153#ifdef MS_WINDOWS
154typedef SOCKET SOCKET_T;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155# ifdef MS_WIN64
156# define SIZEOF_SOCKET_T 8
157# else
158# define SIZEOF_SOCKET_T 4
159# endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000160#else
161typedef int SOCKET_T;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162# define SIZEOF_SOCKET_T SIZEOF_INT
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000163#endif
164
Antoine Pitrou67c7ce42010-08-28 20:42:55 +0000165#if SIZEOF_SOCKET_T <= SIZEOF_LONG
166#define PyLong_FromSocket_t(fd) PyLong_FromLong((SOCKET_T)(fd))
167#define PyLong_AsSocket_t(fd) (SOCKET_T)PyLong_AsLong(fd)
168#else
169#define PyLong_FromSocket_t(fd) PyLong_FromLongLong((SOCKET_T)(fd))
170#define PyLong_AsSocket_t(fd) (SOCKET_T)PyLong_AsLongLong(fd)
171#endif
172
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000173/* Socket address */
174typedef union sock_addr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 struct sockaddr_in in;
Charles-François Natali8b759652011-12-23 16:44:51 +0100176 struct sockaddr sa;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000177#ifdef AF_UNIX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 struct sockaddr_un un;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000179#endif
Martin v. Löwis11017b12006-01-14 18:12:57 +0000180#ifdef AF_NETLINK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 struct sockaddr_nl nl;
Martin v. Löwis11017b12006-01-14 18:12:57 +0000182#endif
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000183#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 struct sockaddr_in6 in6;
185 struct sockaddr_storage storage;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000186#endif
187#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 struct sockaddr_l2 bt_l2;
189 struct sockaddr_rc bt_rc;
190 struct sockaddr_sco bt_sco;
191 struct sockaddr_hci bt_hci;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000192#endif
193#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 struct sockaddr_ll ll;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000195#endif
Charles-François Natali47413c12011-10-06 19:47:44 +0200196#ifdef HAVE_LINUX_CAN_H
197 struct sockaddr_can can;
198#endif
Martin v. Löwis9d6c6692012-02-03 17:44:58 +0100199#ifdef HAVE_SYS_KERN_CONTROL_H
200 struct sockaddr_ctl ctl;
201#endif
Christian Heimesd37c0682017-02-22 12:12:00 +0100202#ifdef HAVE_SOCKADDR_ALG
203 struct sockaddr_alg alg;
204#endif
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +0300205#ifdef AF_VSOCK
caaveryeffc12f2017-09-06 18:18:10 -0400206 struct sockaddr_vm vm;
207#endif
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000208} sock_addr_t;
209
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000210/* The object holding a socket. It holds some extra information,
211 like the address family, which is used to decode socket address
212 arguments properly. */
213
214typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 PyObject_HEAD
216 SOCKET_T sock_fd; /* Socket file descriptor */
217 int sock_family; /* Address family, e.g., AF_INET */
218 int sock_type; /* Socket type, e.g., SOCK_STREAM */
219 int sock_proto; /* Protocol type, usually 0 */
220 PyObject *(*errorhandler)(void); /* Error handler; checks
221 errno, returns NULL and
222 sets a Python exception */
Victor Stinner71694d52015-03-28 01:18:54 +0100223 _PyTime_t sock_timeout; /* Operation timeout in seconds;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 0.0 means non-blocking */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000225} PySocketSockObject;
226
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000227/* --- C API ----------------------------------------------------*/
228
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000229/* Short explanation of what this C API export mechanism does
230 and how it works:
231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 The _ssl module needs access to the type object defined in
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000233 the _socket module. Since cross-DLL linking introduces a lot of
234 problems on many platforms, the "trick" is to wrap the
235 C API of a module in a struct which then gets exported to
Benjamin Petersonb173f782009-05-05 22:31:58 +0000236 other modules via a PyCapsule.
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000237
238 The code in socketmodule.c defines this struct (which currently
239 only contains the type object reference, but could very
240 well also include other C APIs needed by other modules)
Benjamin Petersonb173f782009-05-05 22:31:58 +0000241 and exports it as PyCapsule via the module dictionary
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000242 under the name "CAPI".
243
244 Other modules can now include the socketmodule.h file
245 which defines the needed C APIs to import and set up
246 a static copy of this struct in the importing module.
247
248 After initialization, the importing module can then
249 access the C APIs from the _socket module by simply
250 referring to the static struct, e.g.
251
252 Load _socket module and its C API; this sets up the global
253 PySocketModule:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254
255 if (PySocketModule_ImportModuleAndAPI())
256 return;
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000257
258
259 Now use the C API as if it were defined in the using
260 module:
261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 PySocketModule.Sock_Type,
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 (PyObject*)&Sock,
267 &key_file, &cert_file))
268 return NULL;
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000269
270 Support could easily be extended to export more C APIs/symbols
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 this way. Currently, only the type object is exported,
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000272 other candidates would be socket constructors and socket
273 access functions.
274
275*/
276
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000277/* C API for usage by other Python modules */
278typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 PyTypeObject *Sock_Type;
280 PyObject *error;
Antoine Pitrouc4df7842010-12-03 19:59:41 +0000281 PyObject *timeout_error;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000282} PySocketModule_APIObject;
Tim Peters6f5505a2002-02-17 03:58:51 +0000283
Benjamin Peterson81c447f2009-05-06 02:43:58 +0000284#define PySocketModule_ImportModuleAndAPI() PyCapsule_Import(PySocket_CAPSULE_NAME, 1)
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000285
Benjamin Peterson23490202009-08-12 18:11:03 +0000286#ifdef __cplusplus
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000287}
288#endif
289#endif /* !Py__SOCKET_H */