blob: 285c1fe6e55025a1b61c900fa8a7139ca0fc3ada [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#if _MSC_VER >= 1300
17# include <winsock2.h>
18# include <ws2tcpip.h>
Christian Heimes04ae9162008-01-04 15:23:30 +000019# include <MSTcpIP.h> /* for SIO_RCVALL */
Martin v. Löwis272cb402002-03-01 08:31:07 +000020# define HAVE_ADDRINFO
21# define HAVE_SOCKADDR_STORAGE
22# define HAVE_GETADDRINFO
23# define HAVE_GETNAMEINFO
24# define ENABLE_IPV6
25#else
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000026# include <winsock.h>
27#endif
Martin v. Löwis272cb402002-03-01 08:31:07 +000028#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000029
30#ifdef HAVE_SYS_UN_H
31# include <sys/un.h>
32#else
33# undef AF_UNIX
34#endif
35
Martin v. Löwis11017b12006-01-14 18:12:57 +000036#ifdef HAVE_LINUX_NETLINK_H
Neal Norwitz65851662006-01-16 04:31:40 +000037# ifdef HAVE_ASM_TYPES_H
38# include <asm/types.h>
39# endif
Martin v. Löwis11017b12006-01-14 18:12:57 +000040# include <linux/netlink.h>
41#else
42# undef AF_NETLINK
43#endif
44
Martin v. Löwis12af0482004-01-31 12:34:17 +000045#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>
Martin v. Löwis45423a72007-02-14 10:07:37 +000050#include <bluetooth/hci.h>
Martin v. Löwis12af0482004-01-31 12:34:17 +000051#endif
52
Hye-Shik Chang96c44652004-02-02 08:48:45 +000053#ifdef HAVE_BLUETOOTH_H
54#include <bluetooth.h>
55#endif
56
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000057#ifdef HAVE_NETPACKET_PACKET_H
58# include <sys/ioctl.h>
59# include <net/if.h>
60# include <netpacket/packet.h>
61#endif
62
Christian Heimesfb2d25a2008-01-07 16:12:44 +000063#ifdef HAVE_LINUX_TIPC_H
64# include <linux/tipc.h>
65#endif
66
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000067#ifndef Py__SOCKET_H
68#define Py__SOCKET_H
69#ifdef __cplusplus
70extern "C" {
71#endif
72
73/* Python module and C API name */
74#define PySocket_MODULE_NAME "_socket"
75#define PySocket_CAPI_NAME "CAPI"
76
77/* Abstract the socket file descriptor type */
78#ifdef MS_WINDOWS
79typedef SOCKET SOCKET_T;
80# ifdef MS_WIN64
81# define SIZEOF_SOCKET_T 8
82# else
83# define SIZEOF_SOCKET_T 4
84# endif
85#else
86typedef int SOCKET_T;
87# define SIZEOF_SOCKET_T SIZEOF_INT
88#endif
89
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000090/* Socket address */
91typedef union sock_addr {
92 struct sockaddr_in in;
93#ifdef AF_UNIX
94 struct sockaddr_un un;
95#endif
Martin v. Löwis11017b12006-01-14 18:12:57 +000096#ifdef AF_NETLINK
97 struct sockaddr_nl nl;
98#endif
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +000099#ifdef ENABLE_IPV6
100 struct sockaddr_in6 in6;
101 struct sockaddr_storage storage;
102#endif
103#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
104 struct sockaddr_l2 bt_l2;
105 struct sockaddr_rc bt_rc;
106 struct sockaddr_sco bt_sco;
Martin v. Löwis45423a72007-02-14 10:07:37 +0000107 struct sockaddr_hci bt_hci;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000108#endif
109#ifdef HAVE_NETPACKET_PACKET_H
110 struct sockaddr_ll ll;
111#endif
112} sock_addr_t;
113
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000114/* The object holding a socket. It holds some extra information,
115 like the address family, which is used to decode socket address
116 arguments properly. */
117
118typedef struct {
119 PyObject_HEAD
120 SOCKET_T sock_fd; /* Socket file descriptor */
121 int sock_family; /* Address family, e.g., AF_INET */
122 int sock_type; /* Socket type, e.g., SOCK_STREAM */
123 int sock_proto; /* Protocol type, usually 0 */
Tim Peters6f5505a2002-02-17 03:58:51 +0000124 PyObject *(*errorhandler)(void); /* Error handler; checks
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000125 errno, returns NULL and
126 sets a Python exception */
Guido van Rossum11ba0942002-06-13 15:07:44 +0000127 double sock_timeout; /* Operation timeout in seconds;
128 0.0 means non-blocking */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000129} PySocketSockObject;
130
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000131/* --- C API ----------------------------------------------------*/
132
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000133/* Short explanation of what this C API export mechanism does
134 and how it works:
135
136 The _ssl module needs access to the type object defined in
137 the _socket module. Since cross-DLL linking introduces a lot of
138 problems on many platforms, the "trick" is to wrap the
139 C API of a module in a struct which then gets exported to
140 other modules via a PyCObject.
141
142 The code in socketmodule.c defines this struct (which currently
143 only contains the type object reference, but could very
144 well also include other C APIs needed by other modules)
145 and exports it as PyCObject via the module dictionary
146 under the name "CAPI".
147
148 Other modules can now include the socketmodule.h file
149 which defines the needed C APIs to import and set up
150 a static copy of this struct in the importing module.
151
152 After initialization, the importing module can then
153 access the C APIs from the _socket module by simply
154 referring to the static struct, e.g.
155
156 Load _socket module and its C API; this sets up the global
157 PySocketModule:
158
159 if (PySocketModule_ImportModuleAndAPI())
160 return;
161
162
163 Now use the C API as if it were defined in the using
164 module:
165
166 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
167
168 PySocketModule.Sock_Type,
169
170 (PyObject*)&Sock,
171 &key_file, &cert_file))
172 return NULL;
173
174 Support could easily be extended to export more C APIs/symbols
175 this way. Currently, only the type object is exported,
176 other candidates would be socket constructors and socket
177 access functions.
178
179*/
180
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000181/* C API for usage by other Python modules */
182typedef struct {
Tim Peters6f5505a2002-02-17 03:58:51 +0000183 PyTypeObject *Sock_Type;
Brett Cannon06c34792004-03-23 23:16:54 +0000184 PyObject *error;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000185} PySocketModule_APIObject;
Tim Peters6f5505a2002-02-17 03:58:51 +0000186
187/* XXX The net effect of the following appears to be to define a function
188 XXX named PySocketModule_APIObject in _ssl.c. It's unclear why it isn't
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000189 XXX defined there directly.
190
191 >>> It's defined here because other modules might also want to use
192 >>> the C API.
193
194*/
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000195#ifndef PySocket_BUILDING_SOCKET
196
197/* --- C API ----------------------------------------------------*/
198
199/* Interfacestructure to C API for other modules.
Guido van Rossumbe8db072002-06-07 02:27:50 +0000200 Call PySocketModule_ImportModuleAndAPI() to initialize this
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000201 structure. After that usage is simple:
202
203 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
204 &PySocketModule.Sock_Type, (PyObject*)&Sock,
205 &key_file, &cert_file))
206 return NULL;
207 ...
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000208*/
209
Tim Peters6f5505a2002-02-17 03:58:51 +0000210static
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000211PySocketModule_APIObject PySocketModule;
212
213/* You *must* call this before using any of the functions in
214 PySocketModule and check its outcome; otherwise all accesses will
215 result in a segfault. Returns 0 on success. */
216
217#ifndef DPRINTF
218# define DPRINTF if (0) printf
219#endif
220
221static
222int PySocketModule_ImportModuleAndAPI(void)
223{
Tim Peters6f5505a2002-02-17 03:58:51 +0000224 PyObject *mod = 0, *v = 0;
225 char *apimodule = PySocket_MODULE_NAME;
226 char *apiname = PySocket_CAPI_NAME;
227 void *api;
228
229 DPRINTF("Importing the %s C API...\n", apimodule);
Christian Heimes000a0742008-01-03 22:16:32 +0000230 mod = PyImport_ImportModuleNoBlock(apimodule);
Tim Peters6f5505a2002-02-17 03:58:51 +0000231 if (mod == NULL)
232 goto onError;
233 DPRINTF(" %s package found\n", apimodule);
234 v = PyObject_GetAttrString(mod, apiname);
235 if (v == NULL)
236 goto onError;
237 Py_DECREF(mod);
238 DPRINTF(" API object %s found\n", apiname);
239 api = PyCObject_AsVoidPtr(v);
240 if (api == NULL)
241 goto onError;
242 Py_DECREF(v);
243 memcpy(&PySocketModule, api, sizeof(PySocketModule));
244 DPRINTF(" API object loaded and initialized.\n");
245 return 0;
246
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000247 onError:
Tim Peters6f5505a2002-02-17 03:58:51 +0000248 DPRINTF(" not found.\n");
249 Py_XDECREF(mod);
250 Py_XDECREF(v);
251 return -1;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000252}
253
Tim Peters6f5505a2002-02-17 03:58:51 +0000254#endif /* !PySocket_BUILDING_SOCKET */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000255
256#ifdef __cplusplus
257}
258#endif
259#endif /* !Py__SOCKET_H */