- Got rid of newmodule.c
[python/dscho.git] / Modules / socketmodule.h
blob936acac5b9d6197a9bd7c7e5b400797a6ea70998
1 /* Socket module header file */
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
11 #else /* MS_WINDOWS */
12 #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
21 # include <winsock.h>
22 #endif
23 #endif
25 #ifdef HAVE_SYS_UN_H
26 # include <sys/un.h>
27 #else
28 # undef AF_UNIX
29 #endif
31 #ifdef HAVE_NETPACKET_PACKET_H
32 # include <sys/ioctl.h>
33 # include <net/if.h>
34 # include <netpacket/packet.h>
35 #endif
37 #ifndef Py__SOCKET_H
38 #define Py__SOCKET_H
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
43 /* Python module and C API name */
44 #define PySocket_MODULE_NAME "_socket"
45 #define PySocket_CAPI_NAME "CAPI"
47 /* Abstract the socket file descriptor type */
48 #ifdef MS_WINDOWS
49 typedef 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
56 typedef int SOCKET_T;
57 # define SIZEOF_SOCKET_T SIZEOF_INT
58 #endif
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. */
64 typedef 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;
83 PyObject *(*errorhandler)(void); /* Error handler; checks
84 errno, returns NULL and
85 sets a Python exception */
86 double sock_timeout; /* Operation timeout in seconds;
87 0.0 means non-blocking */
88 } PySocketSockObject;
90 /* --- C API ----------------------------------------------------*/
92 /* Short explanation of what this C API export mechanism does
93 and how it works:
95 The _ssl module needs access to the type object defined in
96 the _socket module. Since cross-DLL linking introduces a lot of
97 problems on many platforms, the "trick" is to wrap the
98 C API of a module in a struct which then gets exported to
99 other modules via a PyCObject.
101 The code in socketmodule.c defines this struct (which currently
102 only contains the type object reference, but could very
103 well also include other C APIs needed by other modules)
104 and exports it as PyCObject via the module dictionary
105 under the name "CAPI".
107 Other modules can now include the socketmodule.h file
108 which defines the needed C APIs to import and set up
109 a static copy of this struct in the importing module.
111 After initialization, the importing module can then
112 access the C APIs from the _socket module by simply
113 referring to the static struct, e.g.
115 Load _socket module and its C API; this sets up the global
116 PySocketModule:
118 if (PySocketModule_ImportModuleAndAPI())
119 return;
122 Now use the C API as if it were defined in the using
123 module:
125 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
127 PySocketModule.Sock_Type,
129 (PyObject*)&Sock,
130 &key_file, &cert_file))
131 return NULL;
133 Support could easily be extended to export more C APIs/symbols
134 this way. Currently, only the type object is exported,
135 other candidates would be socket constructors and socket
136 access functions.
140 /* C API for usage by other Python modules */
141 typedef struct {
142 PyTypeObject *Sock_Type;
143 } PySocketModule_APIObject;
145 /* XXX The net effect of the following appears to be to define a function
146 XXX named PySocketModule_APIObject in _ssl.c. It's unclear why it isn't
147 XXX defined there directly.
149 >>> It's defined here because other modules might also want to use
150 >>> the C API.
153 #ifndef PySocket_BUILDING_SOCKET
155 /* --- C API ----------------------------------------------------*/
157 /* Interfacestructure to C API for other modules.
158 Call PySocketModule_ImportModuleAndAPI() to initialize this
159 structure. After that usage is simple:
161 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
162 &PySocketModule.Sock_Type, (PyObject*)&Sock,
163 &key_file, &cert_file))
164 return NULL;
168 static
169 PySocketModule_APIObject PySocketModule;
171 /* You *must* call this before using any of the functions in
172 PySocketModule and check its outcome; otherwise all accesses will
173 result in a segfault. Returns 0 on success. */
175 #ifndef DPRINTF
176 # define DPRINTF if (0) printf
177 #endif
179 static
180 int PySocketModule_ImportModuleAndAPI(void)
182 PyObject *mod = 0, *v = 0;
183 char *apimodule = PySocket_MODULE_NAME;
184 char *apiname = PySocket_CAPI_NAME;
185 void *api;
187 DPRINTF("Importing the %s C API...\n", apimodule);
188 mod = PyImport_ImportModule(apimodule);
189 if (mod == NULL)
190 goto onError;
191 DPRINTF(" %s package found\n", apimodule);
192 v = PyObject_GetAttrString(mod, apiname);
193 if (v == NULL)
194 goto onError;
195 Py_DECREF(mod);
196 DPRINTF(" API object %s found\n", apiname);
197 api = PyCObject_AsVoidPtr(v);
198 if (api == NULL)
199 goto onError;
200 Py_DECREF(v);
201 memcpy(&PySocketModule, api, sizeof(PySocketModule));
202 DPRINTF(" API object loaded and initialized.\n");
203 return 0;
205 onError:
206 DPRINTF(" not found.\n");
207 Py_XDECREF(mod);
208 Py_XDECREF(v);
209 return -1;
212 #endif /* !PySocket_BUILDING_SOCKET */
214 #ifdef __cplusplus
216 #endif
217 #endif /* !Py__SOCKET_H */