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 # include <MSTcpIP.h> /* for SIO_RCVALL */
20 # define HAVE_ADDRINFO
21 # define HAVE_SOCKADDR_STORAGE
22 # define HAVE_GETADDRINFO
23 # define HAVE_GETNAMEINFO
36 #ifdef HAVE_LINUX_NETLINK_H
37 # ifdef HAVE_ASM_TYPES_H
38 # include <asm/types.h>
40 # include <linux/netlink.h>
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>
53 #ifdef HAVE_BLUETOOTH_H
54 #include <bluetooth.h>
57 #ifdef HAVE_NETPACKET_PACKET_H
58 # include <sys/ioctl.h>
60 # include <netpacket/packet.h>
63 #ifdef HAVE_LINUX_TIPC_H
64 # include <linux/tipc.h>
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 */
79 typedef SOCKET SOCKET_T
;
81 # define SIZEOF_SOCKET_T 8
83 # define SIZEOF_SOCKET_T 4
87 # define SIZEOF_SOCKET_T SIZEOF_INT
91 typedef union sock_addr
{
92 struct sockaddr_in in
;
94 struct sockaddr_un un
;
97 struct sockaddr_nl nl
;
100 struct sockaddr_in6 in6
;
101 struct sockaddr_storage storage
;
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
;
109 #ifdef HAVE_NETPACKET_PACKET_H
110 struct sockaddr_ll ll
;
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. */
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
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
159 if (PySocketModule_ImportModuleAndAPI())
163 Now use the C API as if it were defined in the using
166 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
168 PySocketModule.Sock_Type,
171 &key_file, &cert_file))
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
181 /* C API for usage by other Python modules */
183 PyTypeObject
*Sock_Type
;
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
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))
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. */
218 # define DPRINTF if (0) printf
222 int PySocketModule_ImportModuleAndAPI(void)
224 PyObject
*mod
= 0, *v
= 0;
225 char *apimodule
= PySocket_MODULE_NAME
;
226 char *apiname
= PySocket_CAPI_NAME
;
229 DPRINTF("Importing the %s C API...\n", apimodule
);
230 mod
= PyImport_ImportModuleNoBlock(apimodule
);
233 DPRINTF(" %s package found\n", apimodule
);
234 v
= PyObject_GetAttrString(mod
, apiname
);
238 DPRINTF(" API object %s found\n", apiname
);
239 api
= PyCObject_AsVoidPtr(v
);
243 memcpy(&PySocketModule
, api
, sizeof(PySocketModule
));
244 DPRINTF(" API object loaded and initialized.\n");
248 DPRINTF(" not found.\n");
254 #endif /* !PySocket_BUILDING_SOCKET */
259 #endif /* !Py__SOCKET_H */