comparison Modules/socketmodule.h @ 61467:f197407bdba2 3.1

Recorded merge of revisions 81032 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ................ r81032 | antoine.pitrou | 2010-05-09 17:52:27 +0200 (dim., 09 mai 2010) | 9 lines Recorded merge of revisions 81029 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r81029 | antoine.pitrou | 2010-05-09 16:46:46 +0200 (dim., 09 mai 2010) | 3 lines Untabify C files. Will watch buildbots. ........ ................
author Antoine Pitrou <solipsis@pitrou.net>
date Sun, 09 May 2010 16:14:21 +0000
parents 5c8b2238a2f5
children 8f6e41c3a218
comparison
equal deleted inserted replaced
61459:f8c19c5f5dd2 61467:f197407bdba2
15 #else /* MS_WINDOWS */ 15 #else /* MS_WINDOWS */
16 # include <winsock2.h> 16 # include <winsock2.h>
17 # include <ws2tcpip.h> 17 # include <ws2tcpip.h>
18 /* VC6 is shipped with old platform headers, and does not have MSTcpIP.h 18 /* VC6 is shipped with old platform headers, and does not have MSTcpIP.h
19 * Separate SDKs have all the functions we want, but older ones don't have 19 * Separate SDKs have all the functions we want, but older ones don't have
20 * any version information. 20 * any version information.
21 * I use SIO_GET_MULTICAST_FILTER to detect a decent SDK. 21 * I use SIO_GET_MULTICAST_FILTER to detect a decent SDK.
22 */ 22 */
23 # ifdef SIO_GET_MULTICAST_FILTER 23 # ifdef SIO_GET_MULTICAST_FILTER
24 # include <MSTcpIP.h> /* for SIO_RCVALL */ 24 # include <MSTcpIP.h> /* for SIO_RCVALL */
25 # define HAVE_ADDRINFO 25 # define HAVE_ADDRINFO
74 #ifdef __cplusplus 74 #ifdef __cplusplus
75 extern "C" { 75 extern "C" {
76 #endif 76 #endif
77 77
78 /* Python module and C API name */ 78 /* Python module and C API name */
79 #define PySocket_MODULE_NAME "_socket" 79 #define PySocket_MODULE_NAME "_socket"
80 #define PySocket_CAPI_NAME "CAPI" 80 #define PySocket_CAPI_NAME "CAPI"
81 #define PySocket_CAPSULE_NAME PySocket_MODULE_NAME "." PySocket_CAPI_NAME 81 #define PySocket_CAPSULE_NAME PySocket_MODULE_NAME "." PySocket_CAPI_NAME
82 82
83 /* Abstract the socket file descriptor type */ 83 /* Abstract the socket file descriptor type */
84 #ifdef MS_WINDOWS 84 #ifdef MS_WINDOWS
85 typedef SOCKET SOCKET_T; 85 typedef SOCKET SOCKET_T;
86 # ifdef MS_WIN64 86 # ifdef MS_WIN64
87 # define SIZEOF_SOCKET_T 8 87 # define SIZEOF_SOCKET_T 8
88 # else 88 # else
89 # define SIZEOF_SOCKET_T 4 89 # define SIZEOF_SOCKET_T 4
90 # endif 90 # endif
91 #else 91 #else
92 typedef int SOCKET_T; 92 typedef int SOCKET_T;
93 # define SIZEOF_SOCKET_T SIZEOF_INT 93 # define SIZEOF_SOCKET_T SIZEOF_INT
94 #endif 94 #endif
95 95
96 /* Socket address */ 96 /* Socket address */
97 typedef union sock_addr { 97 typedef union sock_addr {
98 struct sockaddr_in in; 98 struct sockaddr_in in;
99 #ifdef AF_UNIX 99 #ifdef AF_UNIX
100 struct sockaddr_un un; 100 struct sockaddr_un un;
101 #endif 101 #endif
102 #ifdef AF_NETLINK 102 #ifdef AF_NETLINK
103 struct sockaddr_nl nl; 103 struct sockaddr_nl nl;
104 #endif 104 #endif
105 #ifdef ENABLE_IPV6 105 #ifdef ENABLE_IPV6
106 struct sockaddr_in6 in6; 106 struct sockaddr_in6 in6;
107 struct sockaddr_storage storage; 107 struct sockaddr_storage storage;
108 #endif 108 #endif
109 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H 109 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
110 struct sockaddr_l2 bt_l2; 110 struct sockaddr_l2 bt_l2;
111 struct sockaddr_rc bt_rc; 111 struct sockaddr_rc bt_rc;
112 struct sockaddr_sco bt_sco; 112 struct sockaddr_sco bt_sco;
113 struct sockaddr_hci bt_hci; 113 struct sockaddr_hci bt_hci;
114 #endif 114 #endif
115 #ifdef HAVE_NETPACKET_PACKET_H 115 #ifdef HAVE_NETPACKET_PACKET_H
116 struct sockaddr_ll ll; 116 struct sockaddr_ll ll;
117 #endif 117 #endif
118 } sock_addr_t; 118 } sock_addr_t;
119 119
120 /* The object holding a socket. It holds some extra information, 120 /* The object holding a socket. It holds some extra information,
121 like the address family, which is used to decode socket address 121 like the address family, which is used to decode socket address
122 arguments properly. */ 122 arguments properly. */
123 123
124 typedef struct { 124 typedef struct {
125 PyObject_HEAD 125 PyObject_HEAD
126 SOCKET_T sock_fd; /* Socket file descriptor */ 126 SOCKET_T sock_fd; /* Socket file descriptor */
127 int sock_family; /* Address family, e.g., AF_INET */ 127 int sock_family; /* Address family, e.g., AF_INET */
128 int sock_type; /* Socket type, e.g., SOCK_STREAM */ 128 int sock_type; /* Socket type, e.g., SOCK_STREAM */
129 int sock_proto; /* Protocol type, usually 0 */ 129 int sock_proto; /* Protocol type, usually 0 */
130 PyObject *(*errorhandler)(void); /* Error handler; checks 130 PyObject *(*errorhandler)(void); /* Error handler; checks
131 errno, returns NULL and 131 errno, returns NULL and
132 sets a Python exception */ 132 sets a Python exception */
133 double sock_timeout; /* Operation timeout in seconds; 133 double sock_timeout; /* Operation timeout in seconds;
134 0.0 means non-blocking */ 134 0.0 means non-blocking */
135 } PySocketSockObject; 135 } PySocketSockObject;
136 136
137 /* --- C API ----------------------------------------------------*/ 137 /* --- C API ----------------------------------------------------*/
138 138
139 /* Short explanation of what this C API export mechanism does 139 /* Short explanation of what this C API export mechanism does
140 and how it works: 140 and how it works:
141 141
142 The _ssl module needs access to the type object defined in 142 The _ssl module needs access to the type object defined in
143 the _socket module. Since cross-DLL linking introduces a lot of 143 the _socket module. Since cross-DLL linking introduces a lot of
144 problems on many platforms, the "trick" is to wrap the 144 problems on many platforms, the "trick" is to wrap the
145 C API of a module in a struct which then gets exported to 145 C API of a module in a struct which then gets exported to
146 other modules via a PyCapsule. 146 other modules via a PyCapsule.
147 147
159 access the C APIs from the _socket module by simply 159 access the C APIs from the _socket module by simply
160 referring to the static struct, e.g. 160 referring to the static struct, e.g.
161 161
162 Load _socket module and its C API; this sets up the global 162 Load _socket module and its C API; this sets up the global
163 PySocketModule: 163 PySocketModule:
164 164
165 if (PySocketModule_ImportModuleAndAPI()) 165 if (PySocketModule_ImportModuleAndAPI())
166 return; 166 return;
167 167
168 168
169 Now use the C API as if it were defined in the using 169 Now use the C API as if it were defined in the using
170 module: 170 module:
171 171
172 if (!PyArg_ParseTuple(args, "O!|zz:ssl", 172 if (!PyArg_ParseTuple(args, "O!|zz:ssl",
173 173
174 PySocketModule.Sock_Type, 174 PySocketModule.Sock_Type,
175 175
176 (PyObject*)&Sock, 176 (PyObject*)&Sock,
177 &key_file, &cert_file)) 177 &key_file, &cert_file))
178 return NULL; 178 return NULL;
179 179
180 Support could easily be extended to export more C APIs/symbols 180 Support could easily be extended to export more C APIs/symbols
181 this way. Currently, only the type object is exported, 181 this way. Currently, only the type object is exported,
182 other candidates would be socket constructors and socket 182 other candidates would be socket constructors and socket
183 access functions. 183 access functions.
184 184
185 */ 185 */
186 186
187 /* C API for usage by other Python modules */ 187 /* C API for usage by other Python modules */
188 typedef struct { 188 typedef struct {
189 PyTypeObject *Sock_Type; 189 PyTypeObject *Sock_Type;
190 PyObject *error; 190 PyObject *error;
191 } PySocketModule_APIObject; 191 } PySocketModule_APIObject;
192 192
193 #define PySocketModule_ImportModuleAndAPI() PyCapsule_Import(PySocket_CAPSULE_NAME, 1) 193 #define PySocketModule_ImportModuleAndAPI() PyCapsule_Import(PySocket_CAPSULE_NAME, 1)
194 194
195 #ifdef __cplusplus 195 #ifdef __cplusplus