annotate Modules/socketmodule.h @ 68239:ceec209b26d4 2.4

Close 2.4 branch.
author Georg Brandl <georg@python.org>
date Sat, 05 Mar 2011 14:56:41 +0100
parents 57f630a9de26
children ae4480935016
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
1 /* Socket module header file */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
2
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
3 /* Includes needed for the sockaddr_* symbols below */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
4 #ifndef MS_WINDOWS
28949
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 23815
diff changeset
5 #ifdef __VMS
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 23815
diff changeset
6 # include <socket.h>
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 23815
diff changeset
7 # else
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 23815
diff changeset
8 # include <sys/socket.h>
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 23815
diff changeset
9 # endif
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
10 # include <netinet/in.h>
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
11 # if !(defined(__BEOS__) || defined(__CYGWIN__) || (defined(PYOS_OS2) && defined(PYCC_VACPP)))
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
12 # include <netinet/tcp.h>
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
13 # endif
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
14
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
15 #else /* MS_WINDOWS */
22096
9e5ff6747437 Patch #520062: Support IPv6 with VC.NET.
Martin v. Löwis <martin@v.loewis.de>
parents: 22041
diff changeset
16 #if _MSC_VER >= 1300
9e5ff6747437 Patch #520062: Support IPv6 with VC.NET.
Martin v. Löwis <martin@v.loewis.de>
parents: 22041
diff changeset
17 # include <winsock2.h>
9e5ff6747437 Patch #520062: Support IPv6 with VC.NET.
Martin v. Löwis <martin@v.loewis.de>
parents: 22041
diff changeset
18 # include <ws2tcpip.h>
9e5ff6747437 Patch #520062: Support IPv6 with VC.NET.
Martin v. Löwis <martin@v.loewis.de>
parents: 22041
diff changeset
19 # define HAVE_ADDRINFO
9e5ff6747437 Patch #520062: Support IPv6 with VC.NET.
Martin v. Löwis <martin@v.loewis.de>
parents: 22041
diff changeset
20 # define HAVE_SOCKADDR_STORAGE
9e5ff6747437 Patch #520062: Support IPv6 with VC.NET.
Martin v. Löwis <martin@v.loewis.de>
parents: 22041
diff changeset
21 # define HAVE_GETADDRINFO
9e5ff6747437 Patch #520062: Support IPv6 with VC.NET.
Martin v. Löwis <martin@v.loewis.de>
parents: 22041
diff changeset
22 # define HAVE_GETNAMEINFO
9e5ff6747437 Patch #520062: Support IPv6 with VC.NET.
Martin v. Löwis <martin@v.loewis.de>
parents: 22041
diff changeset
23 # define ENABLE_IPV6
9e5ff6747437 Patch #520062: Support IPv6 with VC.NET.
Martin v. Löwis <martin@v.loewis.de>
parents: 22041
diff changeset
24 #else
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
25 # include <winsock.h>
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
26 #endif
22096
9e5ff6747437 Patch #520062: Support IPv6 with VC.NET.
Martin v. Löwis <martin@v.loewis.de>
parents: 22041
diff changeset
27 #endif
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
28
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
29 #ifdef HAVE_SYS_UN_H
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
30 # include <sys/un.h>
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
31 #else
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
32 # undef AF_UNIX
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
33 #endif
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
34
31441
493707692671 Patch #874083: Bluetooth support for socket module.
Martin v. Löwis <martin@v.loewis.de>
parents: 28949
diff changeset
35 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
493707692671 Patch #874083: Bluetooth support for socket module.
Martin v. Löwis <martin@v.loewis.de>
parents: 28949
diff changeset
36 #include <bluetooth/bluetooth.h>
493707692671 Patch #874083: Bluetooth support for socket module.
Martin v. Löwis <martin@v.loewis.de>
parents: 28949
diff changeset
37 #include <bluetooth/rfcomm.h>
493707692671 Patch #874083: Bluetooth support for socket module.
Martin v. Löwis <martin@v.loewis.de>
parents: 28949
diff changeset
38 #include <bluetooth/l2cap.h>
493707692671 Patch #874083: Bluetooth support for socket module.
Martin v. Löwis <martin@v.loewis.de>
parents: 28949
diff changeset
39 #include <bluetooth/sco.h>
493707692671 Patch #874083: Bluetooth support for socket module.
Martin v. Löwis <martin@v.loewis.de>
parents: 28949
diff changeset
40 #endif
493707692671 Patch #874083: Bluetooth support for socket module.
Martin v. Löwis <martin@v.loewis.de>
parents: 28949
diff changeset
41
31444
a282801b06b6 Add FreeBSD support for bluetooth sockets. (SF Patch #888148)
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31441
diff changeset
42 #ifdef HAVE_BLUETOOTH_H
a282801b06b6 Add FreeBSD support for bluetooth sockets. (SF Patch #888148)
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31441
diff changeset
43 #include <bluetooth.h>
a282801b06b6 Add FreeBSD support for bluetooth sockets. (SF Patch #888148)
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31441
diff changeset
44 #endif
a282801b06b6 Add FreeBSD support for bluetooth sockets. (SF Patch #888148)
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31441
diff changeset
45
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
46 #ifdef HAVE_NETPACKET_PACKET_H
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
47 # include <sys/ioctl.h>
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
48 # include <net/if.h>
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
49 # include <netpacket/packet.h>
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
50 #endif
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
51
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
52 #ifndef Py__SOCKET_H
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
53 #define Py__SOCKET_H
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
54 #ifdef __cplusplus
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
55 extern "C" {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
56 #endif
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
57
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
58 /* Python module and C API name */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
59 #define PySocket_MODULE_NAME "_socket"
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
60 #define PySocket_CAPI_NAME "CAPI"
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
61
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
62 /* Abstract the socket file descriptor type */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
63 #ifdef MS_WINDOWS
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
64 typedef SOCKET SOCKET_T;
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
65 # ifdef MS_WIN64
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
66 # define SIZEOF_SOCKET_T 8
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
67 # else
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
68 # define SIZEOF_SOCKET_T 4
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
69 # endif
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
70 #else
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
71 typedef int SOCKET_T;
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
72 # define SIZEOF_SOCKET_T SIZEOF_INT
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
73 #endif
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
74
35551
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
75 /* Socket address */
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
76 typedef union sock_addr {
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
77 struct sockaddr_in in;
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
78 #ifdef AF_UNIX
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
79 struct sockaddr_un un;
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
80 #endif
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
81 #ifdef ENABLE_IPV6
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
82 struct sockaddr_in6 in6;
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
83 struct sockaddr_storage storage;
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
84 #endif
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
85 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
86 struct sockaddr_l2 bt_l2;
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
87 struct sockaddr_rc bt_rc;
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
88 struct sockaddr_sco bt_sco;
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
89 #endif
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
90 #ifdef HAVE_NETPACKET_PACKET_H
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
91 struct sockaddr_ll ll;
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
92 #endif
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
93 } sock_addr_t;
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
94
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
95 /* The object holding a socket. It holds some extra information,
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
96 like the address family, which is used to decode socket address
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
97 arguments properly. */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
98
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
99 typedef struct {
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
100 PyObject_HEAD
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
101 SOCKET_T sock_fd; /* Socket file descriptor */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
102 int sock_family; /* Address family, e.g., AF_INET */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
103 int sock_type; /* Socket type, e.g., SOCK_STREAM */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
104 int sock_proto; /* Protocol type, usually 0 */
35551
57f630a9de26 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 31790
diff changeset
105 sock_addr_t sock_addr; /* Socket address */
21969
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
106 PyObject *(*errorhandler)(void); /* Error handler; checks
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
107 errno, returns NULL and
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
108 sets a Python exception */
23815
203d1412a7e8 Major overhaul of timeout sockets:
Guido van Rossum <guido@python.org>
parents: 23724
diff changeset
109 double sock_timeout; /* Operation timeout in seconds;
203d1412a7e8 Major overhaul of timeout sockets:
Guido van Rossum <guido@python.org>
parents: 23724
diff changeset
110 0.0 means non-blocking */
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
111 } PySocketSockObject;
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
112
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
113 /* --- C API ----------------------------------------------------*/
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
114
22041
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
115 /* Short explanation of what this C API export mechanism does
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
116 and how it works:
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
117
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
118 The _ssl module needs access to the type object defined in
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
119 the _socket module. Since cross-DLL linking introduces a lot of
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
120 problems on many platforms, the "trick" is to wrap the
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
121 C API of a module in a struct which then gets exported to
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
122 other modules via a PyCObject.
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
123
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
124 The code in socketmodule.c defines this struct (which currently
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
125 only contains the type object reference, but could very
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
126 well also include other C APIs needed by other modules)
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
127 and exports it as PyCObject via the module dictionary
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
128 under the name "CAPI".
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
129
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
130 Other modules can now include the socketmodule.h file
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
131 which defines the needed C APIs to import and set up
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
132 a static copy of this struct in the importing module.
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
133
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
134 After initialization, the importing module can then
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
135 access the C APIs from the _socket module by simply
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
136 referring to the static struct, e.g.
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
137
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
138 Load _socket module and its C API; this sets up the global
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
139 PySocketModule:
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
140
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
141 if (PySocketModule_ImportModuleAndAPI())
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
142 return;
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
143
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
144
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
145 Now use the C API as if it were defined in the using
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
146 module:
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
147
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
148 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
149
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
150 PySocketModule.Sock_Type,
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
151
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
152 (PyObject*)&Sock,
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
153 &key_file, &cert_file))
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
154 return NULL;
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
155
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
156 Support could easily be extended to export more C APIs/symbols
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
157 this way. Currently, only the type object is exported,
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
158 other candidates would be socket constructors and socket
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
159 access functions.
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
160
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
161 */
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
162
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
163 /* C API for usage by other Python modules */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
164 typedef struct {
21969
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
165 PyTypeObject *Sock_Type;
31790
29da8bdfeb4e Make socket.sslerror a subclass of socket.error .
Brett Cannon <bcannon@gmail.com>
parents: 31444
diff changeset
166 PyObject *error;
21969
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
167 } PySocketModule_APIObject;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
168
21969
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
169 /* XXX The net effect of the following appears to be to define a function
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
170 XXX named PySocketModule_APIObject in _ssl.c. It's unclear why it isn't
22041
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
171 XXX defined there directly.
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
172
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
173 >>> It's defined here because other modules might also want to use
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
174 >>> the C API.
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
175
e0a288db022d Add documentation about how the inter-module linking works.
Marc-André Lemburg <mal@egenix.com>
parents: 21970
diff changeset
176 */
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
177 #ifndef PySocket_BUILDING_SOCKET
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
178
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
179 /* --- C API ----------------------------------------------------*/
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
180
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
181 /* Interfacestructure to C API for other modules.
23724
db239ec307e0 Repair a comment.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
182 Call PySocketModule_ImportModuleAndAPI() to initialize this
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
183 structure. After that usage is simple:
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
184
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
185 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
186 &PySocketModule.Sock_Type, (PyObject*)&Sock,
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
187 &key_file, &cert_file))
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
188 return NULL;
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
189 ...
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
190 */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
191
21969
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
192 static
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
193 PySocketModule_APIObject PySocketModule;
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
194
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
195 /* You *must* call this before using any of the functions in
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
196 PySocketModule and check its outcome; otherwise all accesses will
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
197 result in a segfault. Returns 0 on success. */
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
198
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
199 #ifndef DPRINTF
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
200 # define DPRINTF if (0) printf
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
201 #endif
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
202
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
203 static
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
204 int PySocketModule_ImportModuleAndAPI(void)
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
205 {
21969
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
206 PyObject *mod = 0, *v = 0;
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
207 char *apimodule = PySocket_MODULE_NAME;
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
208 char *apiname = PySocket_CAPI_NAME;
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
209 void *api;
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
210
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
211 DPRINTF("Importing the %s C API...\n", apimodule);
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
212 mod = PyImport_ImportModule(apimodule);
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
213 if (mod == NULL)
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
214 goto onError;
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
215 DPRINTF(" %s package found\n", apimodule);
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
216 v = PyObject_GetAttrString(mod, apiname);
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
217 if (v == NULL)
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
218 goto onError;
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
219 Py_DECREF(mod);
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
220 DPRINTF(" API object %s found\n", apiname);
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
221 api = PyCObject_AsVoidPtr(v);
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
222 if (api == NULL)
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
223 goto onError;
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
224 Py_DECREF(v);
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
225 memcpy(&PySocketModule, api, sizeof(PySocketModule));
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
226 DPRINTF(" API object loaded and initialized.\n");
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
227 return 0;
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
228
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
229 onError:
21969
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
230 DPRINTF(" not found.\n");
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
231 Py_XDECREF(mod);
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
232 Py_XDECREF(v);
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
233 return -1;
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
234 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
235
21969
72c95448ee4b For readability, switch to tab indents; was using a mix of tab indents,
Tim Peters <tim.peters@gmail.com>
parents: 21959
diff changeset
236 #endif /* !PySocket_BUILDING_SOCKET */
21959
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
237
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
238 #ifdef __cplusplus
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
239 }
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
240 #endif
3957d155f8c5 Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
241 #endif /* !Py__SOCKET_H */