1 /* Socket module header file */
3 /* Includes needed for the sockaddr_* symbols below */
8 # include <sys/socket.h>
10 # include <netinet/in.h>
11 # if !(defined(__BEOS__) || defined(__CYGWIN__) || (defined(PYOS_OS2) && defined(PYCC_VACPP)))
12 # include <netinet/tcp.h>
15 #else /* MS_WINDOWS */
17 # include <winsock2.h>
18 # include <ws2tcpip.h>
19 # define HAVE_ADDRINFO
20 # define HAVE_SOCKADDR_STORAGE
21 # define HAVE_GETADDRINFO
22 # define HAVE_GETNAMEINFO
35 #ifdef HAVE_LINUX_NETLINK_H
36 # ifdef HAVE_ASM_TYPES_H
37 # include <asm/types.h>
39 # include <linux/netlink.h>
44 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
45 #include <bluetooth/bluetooth.h>
46 #include <bluetooth/rfcomm.h>
47 #include <bluetooth/l2cap.h>
48 #include <bluetooth/sco.h>
49 #include <bluetooth/hci.h>
52 #ifdef HAVE_BLUETOOTH_H
53 #include <bluetooth.h>
56 #ifdef HAVE_NETPACKET_PACKET_H
57 # include <sys/ioctl.h>
59 # include <netpacket/packet.h>
68 /* Python module and C API name */
69 #define PySocket_MODULE_NAME "_socket"
70 #define PySocket_CAPI_NAME "CAPI"
72 /* Abstract the socket file descriptor type */
74 typedef SOCKET SOCKET_T
;
76 # define SIZEOF_SOCKET_T 8
78 # define SIZEOF_SOCKET_T 4
82 # define SIZEOF_SOCKET_T SIZEOF_INT
86 typedef union sock_addr
{
87 struct sockaddr_in in
;
89 struct sockaddr_un un
;
92 struct sockaddr_nl nl
;
95 struct sockaddr_in6 in6
;
96 struct sockaddr_storage storage
;
98 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
99 struct sockaddr_l2 bt_l2
;
100 struct sockaddr_rc bt_rc
;
101 struct sockaddr_sco bt_sco
;
102 struct sockaddr_hci bt_hci
;
104 #ifdef HAVE_NETPACKET_PACKET_H
105 struct sockaddr_ll ll
;
109 /* The object holding a socket. It holds some extra information,
110 like the address family, which is used to decode socket address
111 arguments properly. */
115 SOCKET_T sock_fd
; /* Socket file descriptor */
116 int sock_family
; /* Address family, e.g., AF_INET */
117 int sock_type
; /* Socket type, e.g., SOCK_STREAM */
118 int sock_proto
; /* Protocol type, usually 0 */
119 PyObject
*(*errorhandler
)(void); /* Error handler; checks
120 errno, returns NULL and
121 sets a Python exception */
122 double sock_timeout
; /* Operation timeout in seconds;
123 0.0 means non-blocking */
124 } PySocketSockObject
;
126 /* --- C API ----------------------------------------------------*/
128 /* Short explanation of what this C API export mechanism does
131 The _ssl module needs access to the type object defined in
132 the _socket module. Since cross-DLL linking introduces a lot of
133 problems on many platforms, the "trick" is to wrap the
134 C API of a module in a struct which then gets exported to
135 other modules via a PyCObject.
137 The code in socketmodule.c defines this struct (which currently
138 only contains the type object reference, but could very
139 well also include other C APIs needed by other modules)
140 and exports it as PyCObject via the module dictionary
141 under the name "CAPI".
143 Other modules can now include the socketmodule.h file
144 which defines the needed C APIs to import and set up
145 a static copy of this struct in the importing module.
147 After initialization, the importing module can then
148 access the C APIs from the _socket module by simply
149 referring to the static struct, e.g.
151 Load _socket module and its C API; this sets up the global
154 if (PySocketModule_ImportModuleAndAPI())
158 Now use the C API as if it were defined in the using
161 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
163 PySocketModule.Sock_Type,
166 &key_file, &cert_file))
169 Support could easily be extended to export more C APIs/symbols
170 this way. Currently, only the type object is exported,
171 other candidates would be socket constructors and socket
176 /* C API for usage by other Python modules */
178 PyTypeObject
*Sock_Type
;
180 } PySocketModule_APIObject
;
182 /* XXX The net effect of the following appears to be to define a function
183 XXX named PySocketModule_APIObject in _ssl.c. It's unclear why it isn't
184 XXX defined there directly.
186 >>> It's defined here because other modules might also want to use
190 #ifndef PySocket_BUILDING_SOCKET
192 /* --- C API ----------------------------------------------------*/
194 /* Interfacestructure to C API for other modules.
195 Call PySocketModule_ImportModuleAndAPI() to initialize this
196 structure. After that usage is simple:
198 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
199 &PySocketModule.Sock_Type, (PyObject*)&Sock,
200 &key_file, &cert_file))
206 PySocketModule_APIObject PySocketModule
;
208 /* You *must* call this before using any of the functions in
209 PySocketModule and check its outcome; otherwise all accesses will
210 result in a segfault. Returns 0 on success. */
213 # define DPRINTF if (0) printf
217 int PySocketModule_ImportModuleAndAPI(void)
219 PyObject
*mod
= 0, *v
= 0;
220 char *apimodule
= PySocket_MODULE_NAME
;
221 char *apiname
= PySocket_CAPI_NAME
;
224 DPRINTF("Importing the %s C API...\n", apimodule
);
225 mod
= PyImport_ImportModule(apimodule
);
228 DPRINTF(" %s package found\n", apimodule
);
229 v
= PyObject_GetAttrString(mod
, apiname
);
233 DPRINTF(" API object %s found\n", apiname
);
234 api
= PyCObject_AsVoidPtr(v
);
238 memcpy(&PySocketModule
, api
, sizeof(PySocketModule
));
239 DPRINTF(" API object loaded and initialized.\n");
243 DPRINTF(" not found.\n");
249 #endif /* !PySocket_BUILDING_SOCKET */
254 #endif /* !Py__SOCKET_H */