blob: 4d17f08c7f1aab32d37a603a55467d54f1cc06f1 [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
5# include <sys/socket.h>
6# include <netinet/in.h>
7# if !(defined(__BEOS__) || defined(__CYGWIN__) || (defined(PYOS_OS2) && defined(PYCC_VACPP)))
8# include <netinet/tcp.h>
9# endif
10
11#else /* MS_WINDOWS */
Martin v. Löwis272cb402002-03-01 08:31:07 +000012#if _MSC_VER >= 1300
13# include <winsock2.h>
14# include <ws2tcpip.h>
15# define HAVE_ADDRINFO
16# define HAVE_SOCKADDR_STORAGE
17# define HAVE_GETADDRINFO
18# define HAVE_GETNAMEINFO
19# define ENABLE_IPV6
20#else
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000021# include <winsock.h>
22#endif
Martin v. Löwis272cb402002-03-01 08:31:07 +000023#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000024
25#ifdef HAVE_SYS_UN_H
26# include <sys/un.h>
27#else
28# undef AF_UNIX
29#endif
30
31#ifdef HAVE_NETPACKET_PACKET_H
32# include <sys/ioctl.h>
33# include <net/if.h>
34# include <netpacket/packet.h>
35#endif
36
37#ifndef Py__SOCKET_H
38#define Py__SOCKET_H
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43/* Python module and C API name */
44#define PySocket_MODULE_NAME "_socket"
45#define PySocket_CAPI_NAME "CAPI"
46
47/* Abstract the socket file descriptor type */
48#ifdef MS_WINDOWS
49typedef SOCKET SOCKET_T;
50# ifdef MS_WIN64
51# define SIZEOF_SOCKET_T 8
52# else
53# define SIZEOF_SOCKET_T 4
54# endif
55#else
56typedef int SOCKET_T;
57# define SIZEOF_SOCKET_T SIZEOF_INT
58#endif
59
60/* The object holding a socket. It holds some extra information,
61 like the address family, which is used to decode socket address
62 arguments properly. */
63
64typedef struct {
65 PyObject_HEAD
66 SOCKET_T sock_fd; /* Socket file descriptor */
67 int sock_family; /* Address family, e.g., AF_INET */
68 int sock_type; /* Socket type, e.g., SOCK_STREAM */
69 int sock_proto; /* Protocol type, usually 0 */
70 union sock_addr {
71 struct sockaddr_in in;
72#ifdef AF_UNIX
73 struct sockaddr_un un;
74#endif
75#ifdef ENABLE_IPV6
76 struct sockaddr_in6 in6;
77 struct sockaddr_storage storage;
78#endif
79#ifdef HAVE_NETPACKET_PACKET_H
80 struct sockaddr_ll ll;
81#endif
82 } sock_addr;
Tim Peters6f5505a2002-02-17 03:58:51 +000083 PyObject *(*errorhandler)(void); /* Error handler; checks
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000084 errno, returns NULL and
85 sets a Python exception */
Guido van Rossum67f7a382002-06-06 21:08:16 +000086 int sock_blocking; /* Flag indicated whether the
87 socket is in blocking mode */
88 double sock_timeout; /* Operation timeout value */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000089} PySocketSockObject;
90
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +000091/* --- C API ----------------------------------------------------*/
92
Marc-André Lemburg666e70d2002-02-25 14:45:40 +000093/* Short explanation of what this C API export mechanism does
94 and how it works:
95
96 The _ssl module needs access to the type object defined in
97 the _socket module. Since cross-DLL linking introduces a lot of
98 problems on many platforms, the "trick" is to wrap the
99 C API of a module in a struct which then gets exported to
100 other modules via a PyCObject.
101
102 The code in socketmodule.c defines this struct (which currently
103 only contains the type object reference, but could very
104 well also include other C APIs needed by other modules)
105 and exports it as PyCObject via the module dictionary
106 under the name "CAPI".
107
108 Other modules can now include the socketmodule.h file
109 which defines the needed C APIs to import and set up
110 a static copy of this struct in the importing module.
111
112 After initialization, the importing module can then
113 access the C APIs from the _socket module by simply
114 referring to the static struct, e.g.
115
116 Load _socket module and its C API; this sets up the global
117 PySocketModule:
118
119 if (PySocketModule_ImportModuleAndAPI())
120 return;
121
122
123 Now use the C API as if it were defined in the using
124 module:
125
126 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
127
128 PySocketModule.Sock_Type,
129
130 (PyObject*)&Sock,
131 &key_file, &cert_file))
132 return NULL;
133
134 Support could easily be extended to export more C APIs/symbols
135 this way. Currently, only the type object is exported,
136 other candidates would be socket constructors and socket
137 access functions.
138
139*/
140
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000141/* C API for usage by other Python modules */
142typedef struct {
Tim Peters6f5505a2002-02-17 03:58:51 +0000143 PyTypeObject *Sock_Type;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000144} PySocketModule_APIObject;
Tim Peters6f5505a2002-02-17 03:58:51 +0000145
146/* XXX The net effect of the following appears to be to define a function
147 XXX named PySocketModule_APIObject in _ssl.c. It's unclear why it isn't
Marc-André Lemburg666e70d2002-02-25 14:45:40 +0000148 XXX defined there directly.
149
150 >>> It's defined here because other modules might also want to use
151 >>> the C API.
152
153*/
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000154#ifndef PySocket_BUILDING_SOCKET
155
156/* --- C API ----------------------------------------------------*/
157
158/* Interfacestructure to C API for other modules.
159 Call PySocket_ImportModuleAPI() to initialize this
160 structure. After that usage is simple:
161
162 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
163 &PySocketModule.Sock_Type, (PyObject*)&Sock,
164 &key_file, &cert_file))
165 return NULL;
166 ...
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000167*/
168
Tim Peters6f5505a2002-02-17 03:58:51 +0000169static
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000170PySocketModule_APIObject PySocketModule;
171
172/* You *must* call this before using any of the functions in
173 PySocketModule and check its outcome; otherwise all accesses will
174 result in a segfault. Returns 0 on success. */
175
176#ifndef DPRINTF
177# define DPRINTF if (0) printf
178#endif
179
180static
181int PySocketModule_ImportModuleAndAPI(void)
182{
Tim Peters6f5505a2002-02-17 03:58:51 +0000183 PyObject *mod = 0, *v = 0;
184 char *apimodule = PySocket_MODULE_NAME;
185 char *apiname = PySocket_CAPI_NAME;
186 void *api;
187
188 DPRINTF("Importing the %s C API...\n", apimodule);
189 mod = PyImport_ImportModule(apimodule);
190 if (mod == NULL)
191 goto onError;
192 DPRINTF(" %s package found\n", apimodule);
193 v = PyObject_GetAttrString(mod, apiname);
194 if (v == NULL)
195 goto onError;
196 Py_DECREF(mod);
197 DPRINTF(" API object %s found\n", apiname);
198 api = PyCObject_AsVoidPtr(v);
199 if (api == NULL)
200 goto onError;
201 Py_DECREF(v);
202 memcpy(&PySocketModule, api, sizeof(PySocketModule));
203 DPRINTF(" API object loaded and initialized.\n");
204 return 0;
205
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000206 onError:
Tim Peters6f5505a2002-02-17 03:58:51 +0000207 DPRINTF(" not found.\n");
208 Py_XDECREF(mod);
209 Py_XDECREF(v);
210 return -1;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000211}
212
Tim Peters6f5505a2002-02-17 03:58:51 +0000213#endif /* !PySocket_BUILDING_SOCKET */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000214
215#ifdef __cplusplus
216}
217#endif
218#endif /* !Py__SOCKET_H */