blob: 8515499b0286164626189ad1a1925b0634192941 [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>
11# if !(defined(__BEOS__) || defined(__CYGWIN__) || (defined(PYOS_OS2) && defined(PYCC_VACPP)))
12# 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'Arca4dd2e22008-06-13 00:42:22 +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 Pitrouc83ea132010-05-09 14:46:46 +000020 * any version information.
Amaury Forgeot d'Arcd25cdc32008-06-14 08:36:07 +000021 * I use SIO_GET_MULTICAST_FILTER to detect a decent SDK.
Amaury Forgeot d'Arca4dd2e22008-06-13 00:42:22 +000022 */
Amaury Forgeot d'Arcd25cdc32008-06-14 08:36:07 +000023# ifdef SIO_GET_MULTICAST_FILTER
Amaury Forgeot d'Arca4dd2e22008-06-13 00:42:22 +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>
Martin v. Löwis45423a72007-02-14 10:07:37 +000055#include <bluetooth/hci.h>
Martin v. Löwis12af0482004-01-31 12:34:17 +000056#endif
57
Hye-Shik Chang96c44652004-02-02 08:48:45 +000058#ifdef HAVE_BLUETOOTH_H
59#include <bluetooth.h>
60#endif
61
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000062#ifdef HAVE_NETPACKET_PACKET_H
63# include <sys/ioctl.h>
64# include <net/if.h>
65# include <netpacket/packet.h>
66#endif
67
Christian Heimesfb2d25a2008-01-07 16:12:44 +000068#ifdef HAVE_LINUX_TIPC_H
69# include <linux/tipc.h>
70#endif
71
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000072#ifndef Py__SOCKET_H
73#define Py__SOCKET_H
74#ifdef __cplusplus
75extern "C" {
76#endif
77
78/* Python module and C API name */
Antoine Pitrouc83ea132010-05-09 14:46:46 +000079#define PySocket_MODULE_NAME "_socket"
80#define PySocket_CAPI_NAME "CAPI"
Larry Hastings402b73f2010-03-25 00:54:54 +000081#define PySocket_CAPSULE_NAME (PySocket_MODULE_NAME "." PySocket_CAPI_NAME)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000082
83/* Abstract the socket file descriptor type */
84#ifdef MS_WINDOWS
85typedef SOCKET SOCKET_T;
Antoine Pitrouc83ea132010-05-09 14:46:46 +000086# ifdef MS_WIN64
87# define SIZEOF_SOCKET_T 8
88# else
89# define SIZEOF_SOCKET_T 4
90# endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000091#else
92typedef int SOCKET_T;
Antoine Pitrouc83ea132010-05-09 14:46:46 +000093# define SIZEOF_SOCKET_T SIZEOF_INT
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000094#endif
95
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000096/* Socket address */
97typedef union sock_addr {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000098 struct sockaddr_in in;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000099#ifdef AF_UNIX
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000100 struct sockaddr_un un;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000101#endif
Martin v. Löwis11017b12006-01-14 18:12:57 +0000102#ifdef AF_NETLINK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000103 struct sockaddr_nl nl;
Martin v. Löwis11017b12006-01-14 18:12:57 +0000104#endif
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000105#ifdef ENABLE_IPV6
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000106 struct sockaddr_in6 in6;
107 struct sockaddr_storage storage;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000108#endif
109#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000110 struct sockaddr_l2 bt_l2;
111 struct sockaddr_rc bt_rc;
112 struct sockaddr_sco bt_sco;
113 struct sockaddr_hci bt_hci;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000114#endif
115#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000116 struct sockaddr_ll ll;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000117#endif
118} sock_addr_t;
119
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000120/* The object holding a socket. It holds some extra information,
121 like the address family, which is used to decode socket address
122 arguments properly. */
123
124typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000125 PyObject_HEAD
126 SOCKET_T sock_fd; /* Socket file descriptor */
127 int sock_family; /* Address family, e.g., AF_INET */
128 int sock_type; /* Socket type, e.g., SOCK_STREAM */
129 int sock_proto; /* Protocol type, usually 0 */
130 PyObject *(*errorhandler)(void); /* Error handler; checks
131 errno, returns NULL and
132 sets a Python exception */
133 double sock_timeout; /* Operation timeout in seconds;
134 0.0 means non-blocking */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000135} PySocketSockObject;
136
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000137/* --- C API ----------------------------------------------------*/
138
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000139/* Short explanation of what this C API export mechanism does
140 and how it works:
141
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000142 The _ssl module needs access to the type object defined in
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000143 the _socket module. Since cross-DLL linking introduces a lot of
144 problems on many platforms, the "trick" is to wrap the
145 C API of a module in a struct which then gets exported to
Larry Hastings402b73f2010-03-25 00:54:54 +0000146 other modules via a PyCapsule.
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000147
148 The code in socketmodule.c defines this struct (which currently
149 only contains the type object reference, but could very
150 well also include other C APIs needed by other modules)
Larry Hastings402b73f2010-03-25 00:54:54 +0000151 and exports it as PyCapsule via the module dictionary
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000152 under the name "CAPI".
153
154 Other modules can now include the socketmodule.h file
155 which defines the needed C APIs to import and set up
156 a static copy of this struct in the importing module.
157
158 After initialization, the importing module can then
159 access the C APIs from the _socket module by simply
160 referring to the static struct, e.g.
161
162 Load _socket module and its C API; this sets up the global
163 PySocketModule:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000164
165 if (PySocketModule_ImportModuleAndAPI())
166 return;
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000167
168
169 Now use the C API as if it were defined in the using
170 module:
171
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000172 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000173
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000174 PySocketModule.Sock_Type,
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000175
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000176 (PyObject*)&Sock,
177 &key_file, &cert_file))
178 return NULL;
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000179
180 Support could easily be extended to export more C APIs/symbols
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000181 this way. Currently, only the type object is exported,
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000182 other candidates would be socket constructors and socket
183 access functions.
184
185*/
186
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000187/* C API for usage by other Python modules */
188typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000189 PyTypeObject *Sock_Type;
190 PyObject *error;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000191} PySocketModule_APIObject;
Tim Peters6f5505a2002-02-17 03:58:51 +0000192
193/* XXX The net effect of the following appears to be to define a function
194 XXX named PySocketModule_APIObject in _ssl.c. It's unclear why it isn't
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000195 XXX defined there directly.
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000196
197 >>> It's defined here because other modules might also want to use
198 >>> the C API.
199
200*/
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000201#ifndef PySocket_BUILDING_SOCKET
202
203/* --- C API ----------------------------------------------------*/
204
205/* Interfacestructure to C API for other modules.
Guido van Rossumbe8db072002-06-07 02:27:50 +0000206 Call PySocketModule_ImportModuleAndAPI() to initialize this
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000207 structure. After that usage is simple:
208
209 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
210 &PySocketModule.Sock_Type, (PyObject*)&Sock,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000211 &key_file, &cert_file))
212 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000213 ...
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000214*/
215
Tim Peters6f5505a2002-02-17 03:58:51 +0000216static
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000217PySocketModule_APIObject PySocketModule;
218
219/* You *must* call this before using any of the functions in
220 PySocketModule and check its outcome; otherwise all accesses will
221 result in a segfault. Returns 0 on success. */
222
223#ifndef DPRINTF
224# define DPRINTF if (0) printf
225#endif
226
227static
228int PySocketModule_ImportModuleAndAPI(void)
229{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000230 void *api;
Tim Peters6f5505a2002-02-17 03:58:51 +0000231
Larry Hastings402b73f2010-03-25 00:54:54 +0000232 DPRINTF(" Loading capsule %s\n", PySocket_CAPSULE_NAME);
233 api = PyCapsule_Import(PySocket_CAPSULE_NAME, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000234 if (api == NULL)
235 goto onError;
236 memcpy(&PySocketModule, api, sizeof(PySocketModule));
237 DPRINTF(" API object loaded and initialized.\n");
238 return 0;
Tim Peters6f5505a2002-02-17 03:58:51 +0000239
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000240 onError:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000241 DPRINTF(" not found.\n");
242 return -1;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000243}
244
Tim Peters6f5505a2002-02-17 03:58:51 +0000245#endif /* !PySocket_BUILDING_SOCKET */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000246
247#ifdef __cplusplus
248}
249#endif
250#endif /* !Py__SOCKET_H */