Added updates with respect to recent changes to TimedRotatingFileHandler.
[python.git] / Modules / socketmodule.h
blob285c1fe6e55025a1b61c900fa8a7139ca0fc3ada
1 /* Socket module header file */
3 /* Includes needed for the sockaddr_* symbols below */
4 #ifndef MS_WINDOWS
5 #ifdef __VMS
6 # include <socket.h>
7 # else
8 # include <sys/socket.h>
9 # endif
10 # include <netinet/in.h>
11 # if !(defined(__BEOS__) || defined(__CYGWIN__) || (defined(PYOS_OS2) && defined(PYCC_VACPP)))
12 # include <netinet/tcp.h>
13 # endif
15 #else /* MS_WINDOWS */
16 #if _MSC_VER >= 1300
17 # include <winsock2.h>
18 # include <ws2tcpip.h>
19 # include <MSTcpIP.h> /* for SIO_RCVALL */
20 # define HAVE_ADDRINFO
21 # define HAVE_SOCKADDR_STORAGE
22 # define HAVE_GETADDRINFO
23 # define HAVE_GETNAMEINFO
24 # define ENABLE_IPV6
25 #else
26 # include <winsock.h>
27 #endif
28 #endif
30 #ifdef HAVE_SYS_UN_H
31 # include <sys/un.h>
32 #else
33 # undef AF_UNIX
34 #endif
36 #ifdef HAVE_LINUX_NETLINK_H
37 # ifdef HAVE_ASM_TYPES_H
38 # include <asm/types.h>
39 # endif
40 # include <linux/netlink.h>
41 #else
42 # undef AF_NETLINK
43 #endif
45 #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>
50 #include <bluetooth/hci.h>
51 #endif
53 #ifdef HAVE_BLUETOOTH_H
54 #include <bluetooth.h>
55 #endif
57 #ifdef HAVE_NETPACKET_PACKET_H
58 # include <sys/ioctl.h>
59 # include <net/if.h>
60 # include <netpacket/packet.h>
61 #endif
63 #ifdef HAVE_LINUX_TIPC_H
64 # include <linux/tipc.h>
65 #endif
67 #ifndef Py__SOCKET_H
68 #define Py__SOCKET_H
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
73 /* Python module and C API name */
74 #define PySocket_MODULE_NAME "_socket"
75 #define PySocket_CAPI_NAME "CAPI"
77 /* Abstract the socket file descriptor type */
78 #ifdef MS_WINDOWS
79 typedef 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
86 typedef int SOCKET_T;
87 # define SIZEOF_SOCKET_T SIZEOF_INT
88 #endif
90 /* Socket address */
91 typedef union sock_addr {
92 struct sockaddr_in in;
93 #ifdef AF_UNIX
94 struct sockaddr_un un;
95 #endif
96 #ifdef AF_NETLINK
97 struct sockaddr_nl nl;
98 #endif
99 #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;
107 struct sockaddr_hci bt_hci;
108 #endif
109 #ifdef HAVE_NETPACKET_PACKET_H
110 struct sockaddr_ll ll;
111 #endif
112 } sock_addr_t;
114 /* 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. */
118 typedef 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 */
124 PyObject *(*errorhandler)(void); /* Error handler; checks
125 errno, returns NULL and
126 sets a Python exception */
127 double sock_timeout; /* Operation timeout in seconds;
128 0.0 means non-blocking */
129 } PySocketSockObject;
131 /* --- C API ----------------------------------------------------*/
133 /* Short explanation of what this C API export mechanism does
134 and how it works:
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.
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".
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.
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.
156 Load _socket module and its C API; this sets up the global
157 PySocketModule:
159 if (PySocketModule_ImportModuleAndAPI())
160 return;
163 Now use the C API as if it were defined in the using
164 module:
166 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
168 PySocketModule.Sock_Type,
170 (PyObject*)&Sock,
171 &key_file, &cert_file))
172 return NULL;
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.
181 /* C API for usage by other Python modules */
182 typedef struct {
183 PyTypeObject *Sock_Type;
184 PyObject *error;
185 } PySocketModule_APIObject;
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
189 XXX defined there directly.
191 >>> It's defined here because other modules might also want to use
192 >>> the C API.
195 #ifndef PySocket_BUILDING_SOCKET
197 /* --- C API ----------------------------------------------------*/
199 /* Interfacestructure to C API for other modules.
200 Call PySocketModule_ImportModuleAndAPI() to initialize this
201 structure. After that usage is simple:
203 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
204 &PySocketModule.Sock_Type, (PyObject*)&Sock,
205 &key_file, &cert_file))
206 return NULL;
210 static
211 PySocketModule_APIObject PySocketModule;
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. */
217 #ifndef DPRINTF
218 # define DPRINTF if (0) printf
219 #endif
221 static
222 int PySocketModule_ImportModuleAndAPI(void)
224 PyObject *mod = 0, *v = 0;
225 char *apimodule = PySocket_MODULE_NAME;
226 char *apiname = PySocket_CAPI_NAME;
227 void *api;
229 DPRINTF("Importing the %s C API...\n", apimodule);
230 mod = PyImport_ImportModuleNoBlock(apimodule);
231 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;
247 onError:
248 DPRINTF(" not found.\n");
249 Py_XDECREF(mod);
250 Py_XDECREF(v);
251 return -1;
254 #endif /* !PySocket_BUILDING_SOCKET */
256 #ifdef __cplusplus
258 #endif
259 #endif /* !Py__SOCKET_H */