1 | n/a | /* Socket module */ |
---|
2 | n/a | |
---|
3 | n/a | /* |
---|
4 | n/a | |
---|
5 | n/a | This module provides an interface to Berkeley socket IPC. |
---|
6 | n/a | |
---|
7 | n/a | Limitations: |
---|
8 | n/a | |
---|
9 | n/a | - Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a |
---|
10 | n/a | portable manner, though AF_PACKET, AF_NETLINK and AF_TIPC are supported |
---|
11 | n/a | under Linux. |
---|
12 | n/a | - No read/write operations (use sendall/recv or makefile instead). |
---|
13 | n/a | - Additional restrictions apply on some non-Unix platforms (compensated |
---|
14 | n/a | for by socket.py). |
---|
15 | n/a | |
---|
16 | n/a | Module interface: |
---|
17 | n/a | |
---|
18 | n/a | - socket.error: exception raised for socket specific errors, alias for OSError |
---|
19 | n/a | - socket.gaierror: exception raised for getaddrinfo/getnameinfo errors, |
---|
20 | n/a | a subclass of socket.error |
---|
21 | n/a | - socket.herror: exception raised for gethostby* errors, |
---|
22 | n/a | a subclass of socket.error |
---|
23 | n/a | - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd') |
---|
24 | n/a | - socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...]) |
---|
25 | n/a | - socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com') |
---|
26 | n/a | - socket.getprotobyname(protocolname) --> protocol number |
---|
27 | n/a | - socket.getservbyname(servicename[, protocolname]) --> port number |
---|
28 | n/a | - socket.getservbyport(portnumber[, protocolname]) --> service name |
---|
29 | n/a | - socket.socket([family[, type [, proto, fileno]]]) --> new socket object |
---|
30 | n/a | (fileno specifies a pre-existing socket file descriptor) |
---|
31 | n/a | - socket.socketpair([family[, type [, proto]]]) --> (socket, socket) |
---|
32 | n/a | - socket.ntohs(16 bit value) --> new int object |
---|
33 | n/a | - socket.ntohl(32 bit value) --> new int object |
---|
34 | n/a | - socket.htons(16 bit value) --> new int object |
---|
35 | n/a | - socket.htonl(32 bit value) --> new int object |
---|
36 | n/a | - socket.getaddrinfo(host, port [, family, type, proto, flags]) |
---|
37 | n/a | --> List of (family, type, proto, canonname, sockaddr) |
---|
38 | n/a | - socket.getnameinfo(sockaddr, flags) --> (host, port) |
---|
39 | n/a | - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h> |
---|
40 | n/a | - socket.has_ipv6: boolean value indicating if IPv6 is supported |
---|
41 | n/a | - socket.inet_aton(IP address) -> 32-bit packed IP representation |
---|
42 | n/a | - socket.inet_ntoa(packed IP) -> IP address string |
---|
43 | n/a | - socket.getdefaulttimeout() -> None | float |
---|
44 | n/a | - socket.setdefaulttimeout(None | float) |
---|
45 | n/a | - socket.if_nameindex() -> list of tuples (if_index, if_name) |
---|
46 | n/a | - socket.if_nametoindex(name) -> corresponding interface index |
---|
47 | n/a | - socket.if_indextoname(index) -> corresponding interface name |
---|
48 | n/a | - an Internet socket address is a pair (hostname, port) |
---|
49 | n/a | where hostname can be anything recognized by gethostbyname() |
---|
50 | n/a | (including the dd.dd.dd.dd notation) and port is in host byte order |
---|
51 | n/a | - where a hostname is returned, the dd.dd.dd.dd notation is used |
---|
52 | n/a | - a UNIX domain socket address is a string specifying the pathname |
---|
53 | n/a | - an AF_PACKET socket address is a tuple containing a string |
---|
54 | n/a | specifying the ethernet interface and an integer specifying |
---|
55 | n/a | the Ethernet protocol number to be received. For example: |
---|
56 | n/a | ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple |
---|
57 | n/a | specify packet-type and ha-type/addr. |
---|
58 | n/a | - an AF_TIPC socket address is expressed as |
---|
59 | n/a | (addr_type, v1, v2, v3 [, scope]); where addr_type can be one of: |
---|
60 | n/a | TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID; |
---|
61 | n/a | and scope can be one of: |
---|
62 | n/a | TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE. |
---|
63 | n/a | The meaning of v1, v2 and v3 depends on the value of addr_type: |
---|
64 | n/a | if addr_type is TIPC_ADDR_NAME: |
---|
65 | n/a | v1 is the server type |
---|
66 | n/a | v2 is the port identifier |
---|
67 | n/a | v3 is ignored |
---|
68 | n/a | if addr_type is TIPC_ADDR_NAMESEQ: |
---|
69 | n/a | v1 is the server type |
---|
70 | n/a | v2 is the lower port number |
---|
71 | n/a | v3 is the upper port number |
---|
72 | n/a | if addr_type is TIPC_ADDR_ID: |
---|
73 | n/a | v1 is the node |
---|
74 | n/a | v2 is the ref |
---|
75 | n/a | v3 is ignored |
---|
76 | n/a | |
---|
77 | n/a | |
---|
78 | n/a | Local naming conventions: |
---|
79 | n/a | |
---|
80 | n/a | - names starting with sock_ are socket object methods |
---|
81 | n/a | - names starting with socket_ are module-level functions |
---|
82 | n/a | - names starting with PySocket are exported through socketmodule.h |
---|
83 | n/a | |
---|
84 | n/a | */ |
---|
85 | n/a | |
---|
86 | n/a | #ifdef __APPLE__ |
---|
87 | n/a | #include <AvailabilityMacros.h> |
---|
88 | n/a | /* for getaddrinfo thread safety test on old versions of OS X */ |
---|
89 | n/a | #ifndef MAC_OS_X_VERSION_10_5 |
---|
90 | n/a | #define MAC_OS_X_VERSION_10_5 1050 |
---|
91 | n/a | #endif |
---|
92 | n/a | /* |
---|
93 | n/a | * inet_aton is not available on OSX 10.3, yet we want to use a binary |
---|
94 | n/a | * that was build on 10.4 or later to work on that release, weak linking |
---|
95 | n/a | * comes to the rescue. |
---|
96 | n/a | */ |
---|
97 | n/a | # pragma weak inet_aton |
---|
98 | n/a | #endif |
---|
99 | n/a | |
---|
100 | n/a | #include "Python.h" |
---|
101 | n/a | #include "structmember.h" |
---|
102 | n/a | |
---|
103 | n/a | /* Socket object documentation */ |
---|
104 | n/a | PyDoc_STRVAR(sock_doc, |
---|
105 | n/a | "socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None) -> socket object\n\ |
---|
106 | n/a | \n\ |
---|
107 | n/a | Open a socket of the given type. The family argument specifies the\n\ |
---|
108 | n/a | address family; it defaults to AF_INET. The type argument specifies\n\ |
---|
109 | n/a | whether this is a stream (SOCK_STREAM, this is the default)\n\ |
---|
110 | n/a | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\ |
---|
111 | n/a | specifying the default protocol. Keyword arguments are accepted.\n\ |
---|
112 | n/a | The socket is created as non-inheritable.\n\ |
---|
113 | n/a | \n\ |
---|
114 | n/a | A socket object represents one endpoint of a network connection.\n\ |
---|
115 | n/a | \n\ |
---|
116 | n/a | Methods of socket objects (keyword arguments not allowed):\n\ |
---|
117 | n/a | \n\ |
---|
118 | n/a | _accept() -- accept connection, returning new socket fd and client address\n\ |
---|
119 | n/a | bind(addr) -- bind the socket to a local address\n\ |
---|
120 | n/a | close() -- close the socket\n\ |
---|
121 | n/a | connect(addr) -- connect the socket to a remote address\n\ |
---|
122 | n/a | connect_ex(addr) -- connect, return an error code instead of an exception\n\ |
---|
123 | n/a | dup() -- return a new socket fd duplicated from fileno()\n\ |
---|
124 | n/a | fileno() -- return underlying file descriptor\n\ |
---|
125 | n/a | getpeername() -- return remote address [*]\n\ |
---|
126 | n/a | getsockname() -- return local address\n\ |
---|
127 | n/a | getsockopt(level, optname[, buflen]) -- get socket options\n\ |
---|
128 | n/a | gettimeout() -- return timeout or None\n\ |
---|
129 | n/a | listen([n]) -- start listening for incoming connections\n\ |
---|
130 | n/a | recv(buflen[, flags]) -- receive data\n\ |
---|
131 | n/a | recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\ |
---|
132 | n/a | recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\ |
---|
133 | n/a | recvfrom_into(buffer[, nbytes, [, flags])\n\ |
---|
134 | n/a | -- receive data and sender\'s address (into a buffer)\n\ |
---|
135 | n/a | sendall(data[, flags]) -- send all data\n\ |
---|
136 | n/a | send(data[, flags]) -- send data, may not send all of it\n\ |
---|
137 | n/a | sendto(data[, flags], addr) -- send data to a given address\n\ |
---|
138 | n/a | setblocking(0 | 1) -- set or clear the blocking I/O flag\n\ |
---|
139 | n/a | setsockopt(level, optname, value[, optlen]) -- set socket options\n\ |
---|
140 | n/a | settimeout(None | float) -- set or clear the timeout\n\ |
---|
141 | n/a | shutdown(how) -- shut down traffic in one or both directions\n\ |
---|
142 | n/a | if_nameindex() -- return all network interface indices and names\n\ |
---|
143 | n/a | if_nametoindex(name) -- return the corresponding interface index\n\ |
---|
144 | n/a | if_indextoname(index) -- return the corresponding interface name\n\ |
---|
145 | n/a | \n\ |
---|
146 | n/a | [*] not available on all platforms!"); |
---|
147 | n/a | |
---|
148 | n/a | /* XXX This is a terrible mess of platform-dependent preprocessor hacks. |
---|
149 | n/a | I hope some day someone can clean this up please... */ |
---|
150 | n/a | |
---|
151 | n/a | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure |
---|
152 | n/a | script doesn't get this right, so we hardcode some platform checks below. |
---|
153 | n/a | On the other hand, not all Linux versions agree, so there the settings |
---|
154 | n/a | computed by the configure script are needed! */ |
---|
155 | n/a | |
---|
156 | n/a | #ifndef __linux__ |
---|
157 | n/a | # undef HAVE_GETHOSTBYNAME_R_3_ARG |
---|
158 | n/a | # undef HAVE_GETHOSTBYNAME_R_5_ARG |
---|
159 | n/a | # undef HAVE_GETHOSTBYNAME_R_6_ARG |
---|
160 | n/a | #endif |
---|
161 | n/a | |
---|
162 | n/a | #if defined(__OpenBSD__) |
---|
163 | n/a | # include <sys/uio.h> |
---|
164 | n/a | #endif |
---|
165 | n/a | |
---|
166 | n/a | #if !defined(WITH_THREAD) |
---|
167 | n/a | # undef HAVE_GETHOSTBYNAME_R |
---|
168 | n/a | #endif |
---|
169 | n/a | |
---|
170 | n/a | #if defined(__ANDROID__) && __ANDROID_API__ < 23 |
---|
171 | n/a | # undef HAVE_GETHOSTBYNAME_R |
---|
172 | n/a | #endif |
---|
173 | n/a | |
---|
174 | n/a | #ifdef HAVE_GETHOSTBYNAME_R |
---|
175 | n/a | # if defined(_AIX) && !defined(_LINUX_SOURCE_COMPAT) |
---|
176 | n/a | # define HAVE_GETHOSTBYNAME_R_3_ARG |
---|
177 | n/a | # elif defined(__sun) || defined(__sgi) |
---|
178 | n/a | # define HAVE_GETHOSTBYNAME_R_5_ARG |
---|
179 | n/a | # elif defined(__linux__) |
---|
180 | n/a | /* Rely on the configure script */ |
---|
181 | n/a | # elif defined(_LINUX_SOURCE_COMPAT) /* Linux compatibility on AIX */ |
---|
182 | n/a | # define HAVE_GETHOSTBYNAME_R_6_ARG |
---|
183 | n/a | # else |
---|
184 | n/a | # undef HAVE_GETHOSTBYNAME_R |
---|
185 | n/a | # endif |
---|
186 | n/a | #endif |
---|
187 | n/a | |
---|
188 | n/a | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \ |
---|
189 | n/a | !defined(MS_WINDOWS) |
---|
190 | n/a | # define USE_GETHOSTBYNAME_LOCK |
---|
191 | n/a | #endif |
---|
192 | n/a | |
---|
193 | n/a | /* To use __FreeBSD_version, __OpenBSD__, and __NetBSD_Version__ */ |
---|
194 | n/a | #ifdef HAVE_SYS_PARAM_H |
---|
195 | n/a | #include <sys/param.h> |
---|
196 | n/a | #endif |
---|
197 | n/a | /* On systems on which getaddrinfo() is believed to not be thread-safe, |
---|
198 | n/a | (this includes the getaddrinfo emulation) protect access with a lock. |
---|
199 | n/a | |
---|
200 | n/a | getaddrinfo is thread-safe on Mac OS X 10.5 and later. Originally it was |
---|
201 | n/a | a mix of code including an unsafe implementation from an old BSD's |
---|
202 | n/a | libresolv. In 10.5 Apple reimplemented it as a safe IPC call to the |
---|
203 | n/a | mDNSResponder process. 10.5 is the first be UNIX '03 certified, which |
---|
204 | n/a | includes the requirement that getaddrinfo be thread-safe. See issue #25924. |
---|
205 | n/a | |
---|
206 | n/a | It's thread-safe in OpenBSD starting with 5.4, released Nov 2013: |
---|
207 | n/a | http://www.openbsd.org/plus54.html |
---|
208 | n/a | |
---|
209 | n/a | It's thread-safe in NetBSD starting with 4.0, released Dec 2007: |
---|
210 | n/a | |
---|
211 | n/a | http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/net/getaddrinfo.c.diff?r1=1.82&r2=1.83 |
---|
212 | n/a | */ |
---|
213 | n/a | #if defined(WITH_THREAD) && ( \ |
---|
214 | n/a | (defined(__APPLE__) && \ |
---|
215 | n/a | MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5) || \ |
---|
216 | n/a | (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \ |
---|
217 | n/a | (defined(__OpenBSD__) && OpenBSD+0 < 201311) || \ |
---|
218 | n/a | (defined(__NetBSD__) && __NetBSD_Version__+0 < 400000000) || \ |
---|
219 | n/a | !defined(HAVE_GETADDRINFO)) |
---|
220 | n/a | #define USE_GETADDRINFO_LOCK |
---|
221 | n/a | #endif |
---|
222 | n/a | |
---|
223 | n/a | #ifdef USE_GETADDRINFO_LOCK |
---|
224 | n/a | #define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1); |
---|
225 | n/a | #define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock); |
---|
226 | n/a | #else |
---|
227 | n/a | #define ACQUIRE_GETADDRINFO_LOCK |
---|
228 | n/a | #define RELEASE_GETADDRINFO_LOCK |
---|
229 | n/a | #endif |
---|
230 | n/a | |
---|
231 | n/a | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
---|
232 | n/a | # include "pythread.h" |
---|
233 | n/a | #endif |
---|
234 | n/a | |
---|
235 | n/a | #if defined(PYCC_VACPP) |
---|
236 | n/a | # include <types.h> |
---|
237 | n/a | # include <io.h> |
---|
238 | n/a | # include <sys/ioctl.h> |
---|
239 | n/a | # include <utils.h> |
---|
240 | n/a | # include <ctype.h> |
---|
241 | n/a | #endif |
---|
242 | n/a | |
---|
243 | n/a | #ifdef __APPLE__ |
---|
244 | n/a | # include <sys/ioctl.h> |
---|
245 | n/a | #endif |
---|
246 | n/a | |
---|
247 | n/a | |
---|
248 | n/a | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI |
---|
249 | n/a | /* make sure that the reentrant (gethostbyaddr_r etc) |
---|
250 | n/a | functions are declared correctly if compiling with |
---|
251 | n/a | MIPSPro 7.x in ANSI C mode (default) */ |
---|
252 | n/a | |
---|
253 | n/a | /* XXX Using _SGIAPI is the wrong thing, |
---|
254 | n/a | but I don't know what the right thing is. */ |
---|
255 | n/a | #undef _SGIAPI /* to avoid warning */ |
---|
256 | n/a | #define _SGIAPI 1 |
---|
257 | n/a | |
---|
258 | n/a | #undef _XOPEN_SOURCE |
---|
259 | n/a | #include <sys/socket.h> |
---|
260 | n/a | #include <sys/types.h> |
---|
261 | n/a | #include <netinet/in.h> |
---|
262 | n/a | #ifdef _SS_ALIGNSIZE |
---|
263 | n/a | #define HAVE_GETADDRINFO 1 |
---|
264 | n/a | #define HAVE_GETNAMEINFO 1 |
---|
265 | n/a | #endif |
---|
266 | n/a | |
---|
267 | n/a | #define HAVE_INET_PTON |
---|
268 | n/a | #include <netdb.h> |
---|
269 | n/a | #endif |
---|
270 | n/a | |
---|
271 | n/a | /* Irix 6.5 fails to define this variable at all. This is needed |
---|
272 | n/a | for both GCC and SGI's compiler. I'd say that the SGI headers |
---|
273 | n/a | are just busted. Same thing for Solaris. */ |
---|
274 | n/a | #if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN) |
---|
275 | n/a | #define INET_ADDRSTRLEN 16 |
---|
276 | n/a | #endif |
---|
277 | n/a | |
---|
278 | n/a | /* Generic includes */ |
---|
279 | n/a | #ifdef HAVE_SYS_TYPES_H |
---|
280 | n/a | #include <sys/types.h> |
---|
281 | n/a | #endif |
---|
282 | n/a | |
---|
283 | n/a | #ifdef HAVE_SYS_SOCKET_H |
---|
284 | n/a | #include <sys/socket.h> |
---|
285 | n/a | #endif |
---|
286 | n/a | |
---|
287 | n/a | #ifdef HAVE_NET_IF_H |
---|
288 | n/a | #include <net/if.h> |
---|
289 | n/a | #endif |
---|
290 | n/a | |
---|
291 | n/a | #ifdef HAVE_SOCKADDR_ALG |
---|
292 | n/a | #include <linux/if_alg.h> |
---|
293 | n/a | #ifndef AF_ALG |
---|
294 | n/a | #define AF_ALG 38 |
---|
295 | n/a | #endif |
---|
296 | n/a | #ifndef SOL_ALG |
---|
297 | n/a | #define SOL_ALG 279 |
---|
298 | n/a | #endif |
---|
299 | n/a | |
---|
300 | n/a | /* Linux 3.19 */ |
---|
301 | n/a | #ifndef ALG_SET_AEAD_ASSOCLEN |
---|
302 | n/a | #define ALG_SET_AEAD_ASSOCLEN 4 |
---|
303 | n/a | #endif |
---|
304 | n/a | #ifndef ALG_SET_AEAD_AUTHSIZE |
---|
305 | n/a | #define ALG_SET_AEAD_AUTHSIZE 5 |
---|
306 | n/a | #endif |
---|
307 | n/a | /* Linux 4.8 */ |
---|
308 | n/a | #ifndef ALG_SET_PUBKEY |
---|
309 | n/a | #define ALG_SET_PUBKEY 6 |
---|
310 | n/a | #endif |
---|
311 | n/a | |
---|
312 | n/a | #ifndef ALG_OP_SIGN |
---|
313 | n/a | #define ALG_OP_SIGN 2 |
---|
314 | n/a | #endif |
---|
315 | n/a | #ifndef ALG_OP_VERIFY |
---|
316 | n/a | #define ALG_OP_VERIFY 3 |
---|
317 | n/a | #endif |
---|
318 | n/a | |
---|
319 | n/a | #endif /* HAVE_SOCKADDR_ALG */ |
---|
320 | n/a | |
---|
321 | n/a | /* Generic socket object definitions and includes */ |
---|
322 | n/a | #define PySocket_BUILDING_SOCKET |
---|
323 | n/a | #include "socketmodule.h" |
---|
324 | n/a | |
---|
325 | n/a | /* Addressing includes */ |
---|
326 | n/a | |
---|
327 | n/a | #ifndef MS_WINDOWS |
---|
328 | n/a | |
---|
329 | n/a | /* Non-MS WINDOWS includes */ |
---|
330 | n/a | # include <netdb.h> |
---|
331 | n/a | # include <unistd.h> |
---|
332 | n/a | |
---|
333 | n/a | /* Headers needed for inet_ntoa() and inet_addr() */ |
---|
334 | n/a | # include <arpa/inet.h> |
---|
335 | n/a | |
---|
336 | n/a | # include <fcntl.h> |
---|
337 | n/a | |
---|
338 | n/a | #else |
---|
339 | n/a | |
---|
340 | n/a | /* MS_WINDOWS includes */ |
---|
341 | n/a | # ifdef HAVE_FCNTL_H |
---|
342 | n/a | # include <fcntl.h> |
---|
343 | n/a | # endif |
---|
344 | n/a | |
---|
345 | n/a | #if defined(_MSC_VER) && _MSC_VER >= 1800 |
---|
346 | n/a | /* Provides the IsWindows7SP1OrGreater() function */ |
---|
347 | n/a | #include <VersionHelpers.h> |
---|
348 | n/a | #endif |
---|
349 | n/a | |
---|
350 | n/a | #endif |
---|
351 | n/a | |
---|
352 | n/a | #include <stddef.h> |
---|
353 | n/a | |
---|
354 | n/a | #ifndef O_NONBLOCK |
---|
355 | n/a | # define O_NONBLOCK O_NDELAY |
---|
356 | n/a | #endif |
---|
357 | n/a | |
---|
358 | n/a | /* include Python's addrinfo.h unless it causes trouble */ |
---|
359 | n/a | #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE) |
---|
360 | n/a | /* Do not include addinfo.h on some newer IRIX versions. |
---|
361 | n/a | * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21, |
---|
362 | n/a | * for example, but not by 6.5.10. |
---|
363 | n/a | */ |
---|
364 | n/a | #elif defined(_MSC_VER) && _MSC_VER>1201 |
---|
365 | n/a | /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and |
---|
366 | n/a | * EAI_* constants are defined in (the already included) ws2tcpip.h. |
---|
367 | n/a | */ |
---|
368 | n/a | #else |
---|
369 | n/a | # include "addrinfo.h" |
---|
370 | n/a | #endif |
---|
371 | n/a | |
---|
372 | n/a | #ifndef HAVE_INET_PTON |
---|
373 | n/a | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
---|
374 | n/a | int inet_pton(int af, const char *src, void *dst); |
---|
375 | n/a | const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); |
---|
376 | n/a | #endif |
---|
377 | n/a | #endif |
---|
378 | n/a | |
---|
379 | n/a | #ifdef __APPLE__ |
---|
380 | n/a | /* On OS X, getaddrinfo returns no error indication of lookup |
---|
381 | n/a | failure, so we must use the emulation instead of the libinfo |
---|
382 | n/a | implementation. Unfortunately, performing an autoconf test |
---|
383 | n/a | for this bug would require DNS access for the machine performing |
---|
384 | n/a | the configuration, which is not acceptable. Therefore, we |
---|
385 | n/a | determine the bug just by checking for __APPLE__. If this bug |
---|
386 | n/a | gets ever fixed, perhaps checking for sys/version.h would be |
---|
387 | n/a | appropriate, which is 10/0 on the system with the bug. */ |
---|
388 | n/a | #ifndef HAVE_GETNAMEINFO |
---|
389 | n/a | /* This bug seems to be fixed in Jaguar. Ths easiest way I could |
---|
390 | n/a | Find to check for Jaguar is that it has getnameinfo(), which |
---|
391 | n/a | older releases don't have */ |
---|
392 | n/a | #undef HAVE_GETADDRINFO |
---|
393 | n/a | #endif |
---|
394 | n/a | |
---|
395 | n/a | #ifdef HAVE_INET_ATON |
---|
396 | n/a | #define USE_INET_ATON_WEAKLINK |
---|
397 | n/a | #endif |
---|
398 | n/a | |
---|
399 | n/a | #endif |
---|
400 | n/a | |
---|
401 | n/a | /* I know this is a bad practice, but it is the easiest... */ |
---|
402 | n/a | #if !defined(HAVE_GETADDRINFO) |
---|
403 | n/a | /* avoid clashes with the C library definition of the symbol. */ |
---|
404 | n/a | #define getaddrinfo fake_getaddrinfo |
---|
405 | n/a | #define gai_strerror fake_gai_strerror |
---|
406 | n/a | #define freeaddrinfo fake_freeaddrinfo |
---|
407 | n/a | #include "getaddrinfo.c" |
---|
408 | n/a | #endif |
---|
409 | n/a | #if !defined(HAVE_GETNAMEINFO) |
---|
410 | n/a | #define getnameinfo fake_getnameinfo |
---|
411 | n/a | #include "getnameinfo.c" |
---|
412 | n/a | #endif |
---|
413 | n/a | |
---|
414 | n/a | #ifdef MS_WINDOWS |
---|
415 | n/a | #define SOCKETCLOSE closesocket |
---|
416 | n/a | #endif |
---|
417 | n/a | |
---|
418 | n/a | #ifdef MS_WIN32 |
---|
419 | n/a | #undef EAFNOSUPPORT |
---|
420 | n/a | #define EAFNOSUPPORT WSAEAFNOSUPPORT |
---|
421 | n/a | #define snprintf _snprintf |
---|
422 | n/a | #endif |
---|
423 | n/a | |
---|
424 | n/a | #ifndef SOCKETCLOSE |
---|
425 | n/a | #define SOCKETCLOSE close |
---|
426 | n/a | #endif |
---|
427 | n/a | |
---|
428 | n/a | #if (defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)) && !defined(__NetBSD__) && !defined(__DragonFly__) |
---|
429 | n/a | #define USE_BLUETOOTH 1 |
---|
430 | n/a | #if defined(__FreeBSD__) |
---|
431 | n/a | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP |
---|
432 | n/a | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM |
---|
433 | n/a | #define BTPROTO_HCI BLUETOOTH_PROTO_HCI |
---|
434 | n/a | #define SOL_HCI SOL_HCI_RAW |
---|
435 | n/a | #define HCI_FILTER SO_HCI_RAW_FILTER |
---|
436 | n/a | #define sockaddr_l2 sockaddr_l2cap |
---|
437 | n/a | #define sockaddr_rc sockaddr_rfcomm |
---|
438 | n/a | #define hci_dev hci_node |
---|
439 | n/a | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb) |
---|
440 | n/a | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb) |
---|
441 | n/a | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
---|
442 | n/a | #elif defined(__NetBSD__) || defined(__DragonFly__) |
---|
443 | n/a | #define sockaddr_l2 sockaddr_bt |
---|
444 | n/a | #define sockaddr_rc sockaddr_bt |
---|
445 | n/a | #define sockaddr_hci sockaddr_bt |
---|
446 | n/a | #define sockaddr_sco sockaddr_bt |
---|
447 | n/a | #define SOL_HCI BTPROTO_HCI |
---|
448 | n/a | #define HCI_DATA_DIR SO_HCI_DIRECTION |
---|
449 | n/a | #define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb) |
---|
450 | n/a | #define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb) |
---|
451 | n/a | #define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb) |
---|
452 | n/a | #define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb) |
---|
453 | n/a | #else |
---|
454 | n/a | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb) |
---|
455 | n/a | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb) |
---|
456 | n/a | #define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb) |
---|
457 | n/a | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb) |
---|
458 | n/a | #endif |
---|
459 | n/a | #endif |
---|
460 | n/a | |
---|
461 | n/a | /* Convert "sock_addr_t *" to "struct sockaddr *". */ |
---|
462 | n/a | #define SAS2SA(x) (&((x)->sa)) |
---|
463 | n/a | |
---|
464 | n/a | /* |
---|
465 | n/a | * Constants for getnameinfo() |
---|
466 | n/a | */ |
---|
467 | n/a | #if !defined(NI_MAXHOST) |
---|
468 | n/a | #define NI_MAXHOST 1025 |
---|
469 | n/a | #endif |
---|
470 | n/a | #if !defined(NI_MAXSERV) |
---|
471 | n/a | #define NI_MAXSERV 32 |
---|
472 | n/a | #endif |
---|
473 | n/a | |
---|
474 | n/a | #ifndef INVALID_SOCKET /* MS defines this */ |
---|
475 | n/a | #define INVALID_SOCKET (-1) |
---|
476 | n/a | #endif |
---|
477 | n/a | |
---|
478 | n/a | #ifndef INADDR_NONE |
---|
479 | n/a | #define INADDR_NONE (-1) |
---|
480 | n/a | #endif |
---|
481 | n/a | |
---|
482 | n/a | /* XXX There's a problem here: *static* functions are not supposed to have |
---|
483 | n/a | a Py prefix (or use CapitalizedWords). Later... */ |
---|
484 | n/a | |
---|
485 | n/a | /* Global variable holding the exception type for errors detected |
---|
486 | n/a | by this module (but not argument type or memory errors, etc.). */ |
---|
487 | n/a | static PyObject *socket_herror; |
---|
488 | n/a | static PyObject *socket_gaierror; |
---|
489 | n/a | static PyObject *socket_timeout; |
---|
490 | n/a | |
---|
491 | n/a | /* A forward reference to the socket type object. |
---|
492 | n/a | The sock_type variable contains pointers to various functions, |
---|
493 | n/a | some of which call new_sockobject(), which uses sock_type, so |
---|
494 | n/a | there has to be a circular reference. */ |
---|
495 | n/a | static PyTypeObject sock_type; |
---|
496 | n/a | |
---|
497 | n/a | #if defined(HAVE_POLL_H) |
---|
498 | n/a | #include <poll.h> |
---|
499 | n/a | #elif defined(HAVE_SYS_POLL_H) |
---|
500 | n/a | #include <sys/poll.h> |
---|
501 | n/a | #endif |
---|
502 | n/a | |
---|
503 | n/a | /* Largest value to try to store in a socklen_t (used when handling |
---|
504 | n/a | ancillary data). POSIX requires socklen_t to hold at least |
---|
505 | n/a | (2**31)-1 and recommends against storing larger values, but |
---|
506 | n/a | socklen_t was originally int in the BSD interface, so to be on the |
---|
507 | n/a | safe side we use the smaller of (2**31)-1 and INT_MAX. */ |
---|
508 | n/a | #if INT_MAX > 0x7fffffff |
---|
509 | n/a | #define SOCKLEN_T_LIMIT 0x7fffffff |
---|
510 | n/a | #else |
---|
511 | n/a | #define SOCKLEN_T_LIMIT INT_MAX |
---|
512 | n/a | #endif |
---|
513 | n/a | |
---|
514 | n/a | #ifdef HAVE_POLL |
---|
515 | n/a | /* Instead of select(), we'll use poll() since poll() works on any fd. */ |
---|
516 | n/a | #define IS_SELECTABLE(s) 1 |
---|
517 | n/a | /* Can we call select() with this socket without a buffer overrun? */ |
---|
518 | n/a | #else |
---|
519 | n/a | /* If there's no timeout left, we don't have to call select, so it's a safe, |
---|
520 | n/a | * little white lie. */ |
---|
521 | n/a | #define IS_SELECTABLE(s) (_PyIsSelectable_fd((s)->sock_fd) || (s)->sock_timeout <= 0) |
---|
522 | n/a | #endif |
---|
523 | n/a | |
---|
524 | n/a | static PyObject* |
---|
525 | n/a | select_error(void) |
---|
526 | n/a | { |
---|
527 | n/a | PyErr_SetString(PyExc_OSError, "unable to select on socket"); |
---|
528 | n/a | return NULL; |
---|
529 | n/a | } |
---|
530 | n/a | |
---|
531 | n/a | #ifdef MS_WINDOWS |
---|
532 | n/a | #ifndef WSAEAGAIN |
---|
533 | n/a | #define WSAEAGAIN WSAEWOULDBLOCK |
---|
534 | n/a | #endif |
---|
535 | n/a | #define CHECK_ERRNO(expected) \ |
---|
536 | n/a | (WSAGetLastError() == WSA ## expected) |
---|
537 | n/a | #else |
---|
538 | n/a | #define CHECK_ERRNO(expected) \ |
---|
539 | n/a | (errno == expected) |
---|
540 | n/a | #endif |
---|
541 | n/a | |
---|
542 | n/a | #ifdef MS_WINDOWS |
---|
543 | n/a | # define GET_SOCK_ERROR WSAGetLastError() |
---|
544 | n/a | # define SET_SOCK_ERROR(err) WSASetLastError(err) |
---|
545 | n/a | # define SOCK_TIMEOUT_ERR WSAEWOULDBLOCK |
---|
546 | n/a | # define SOCK_INPROGRESS_ERR WSAEWOULDBLOCK |
---|
547 | n/a | #else |
---|
548 | n/a | # define GET_SOCK_ERROR errno |
---|
549 | n/a | # define SET_SOCK_ERROR(err) do { errno = err; } while (0) |
---|
550 | n/a | # define SOCK_TIMEOUT_ERR EWOULDBLOCK |
---|
551 | n/a | # define SOCK_INPROGRESS_ERR EINPROGRESS |
---|
552 | n/a | #endif |
---|
553 | n/a | |
---|
554 | n/a | |
---|
555 | n/a | #ifdef MS_WINDOWS |
---|
556 | n/a | /* Does WSASocket() support the WSA_FLAG_NO_HANDLE_INHERIT flag? */ |
---|
557 | n/a | static int support_wsa_no_inherit = -1; |
---|
558 | n/a | #endif |
---|
559 | n/a | |
---|
560 | n/a | /* Convenience function to raise an error according to errno |
---|
561 | n/a | and return a NULL pointer from a function. */ |
---|
562 | n/a | |
---|
563 | n/a | static PyObject * |
---|
564 | n/a | set_error(void) |
---|
565 | n/a | { |
---|
566 | n/a | #ifdef MS_WINDOWS |
---|
567 | n/a | int err_no = WSAGetLastError(); |
---|
568 | n/a | /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which |
---|
569 | n/a | recognizes the error codes used by both GetLastError() and |
---|
570 | n/a | WSAGetLastError */ |
---|
571 | n/a | if (err_no) |
---|
572 | n/a | return PyErr_SetExcFromWindowsErr(PyExc_OSError, err_no); |
---|
573 | n/a | #endif |
---|
574 | n/a | |
---|
575 | n/a | return PyErr_SetFromErrno(PyExc_OSError); |
---|
576 | n/a | } |
---|
577 | n/a | |
---|
578 | n/a | |
---|
579 | n/a | static PyObject * |
---|
580 | n/a | set_herror(int h_error) |
---|
581 | n/a | { |
---|
582 | n/a | PyObject *v; |
---|
583 | n/a | |
---|
584 | n/a | #ifdef HAVE_HSTRERROR |
---|
585 | n/a | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error)); |
---|
586 | n/a | #else |
---|
587 | n/a | v = Py_BuildValue("(is)", h_error, "host not found"); |
---|
588 | n/a | #endif |
---|
589 | n/a | if (v != NULL) { |
---|
590 | n/a | PyErr_SetObject(socket_herror, v); |
---|
591 | n/a | Py_DECREF(v); |
---|
592 | n/a | } |
---|
593 | n/a | |
---|
594 | n/a | return NULL; |
---|
595 | n/a | } |
---|
596 | n/a | |
---|
597 | n/a | |
---|
598 | n/a | static PyObject * |
---|
599 | n/a | set_gaierror(int error) |
---|
600 | n/a | { |
---|
601 | n/a | PyObject *v; |
---|
602 | n/a | |
---|
603 | n/a | #ifdef EAI_SYSTEM |
---|
604 | n/a | /* EAI_SYSTEM is not available on Windows XP. */ |
---|
605 | n/a | if (error == EAI_SYSTEM) |
---|
606 | n/a | return set_error(); |
---|
607 | n/a | #endif |
---|
608 | n/a | |
---|
609 | n/a | #ifdef HAVE_GAI_STRERROR |
---|
610 | n/a | v = Py_BuildValue("(is)", error, gai_strerror(error)); |
---|
611 | n/a | #else |
---|
612 | n/a | v = Py_BuildValue("(is)", error, "getaddrinfo failed"); |
---|
613 | n/a | #endif |
---|
614 | n/a | if (v != NULL) { |
---|
615 | n/a | PyErr_SetObject(socket_gaierror, v); |
---|
616 | n/a | Py_DECREF(v); |
---|
617 | n/a | } |
---|
618 | n/a | |
---|
619 | n/a | return NULL; |
---|
620 | n/a | } |
---|
621 | n/a | |
---|
622 | n/a | /* Function to perform the setting of socket blocking mode |
---|
623 | n/a | internally. block = (1 | 0). */ |
---|
624 | n/a | static int |
---|
625 | n/a | internal_setblocking(PySocketSockObject *s, int block) |
---|
626 | n/a | { |
---|
627 | n/a | int result = -1; |
---|
628 | n/a | #ifdef MS_WINDOWS |
---|
629 | n/a | u_long arg; |
---|
630 | n/a | #endif |
---|
631 | n/a | #if !defined(MS_WINDOWS) \ |
---|
632 | n/a | && !((defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO))) |
---|
633 | n/a | int delay_flag, new_delay_flag; |
---|
634 | n/a | #endif |
---|
635 | n/a | #ifdef SOCK_NONBLOCK |
---|
636 | n/a | if (block) |
---|
637 | n/a | s->sock_type &= (~SOCK_NONBLOCK); |
---|
638 | n/a | else |
---|
639 | n/a | s->sock_type |= SOCK_NONBLOCK; |
---|
640 | n/a | #endif |
---|
641 | n/a | |
---|
642 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
643 | n/a | #ifndef MS_WINDOWS |
---|
644 | n/a | #if (defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)) |
---|
645 | n/a | block = !block; |
---|
646 | n/a | if (ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block) == -1) |
---|
647 | n/a | goto done; |
---|
648 | n/a | #else |
---|
649 | n/a | delay_flag = fcntl(s->sock_fd, F_GETFL, 0); |
---|
650 | n/a | if (delay_flag == -1) |
---|
651 | n/a | goto done; |
---|
652 | n/a | if (block) |
---|
653 | n/a | new_delay_flag = delay_flag & (~O_NONBLOCK); |
---|
654 | n/a | else |
---|
655 | n/a | new_delay_flag = delay_flag | O_NONBLOCK; |
---|
656 | n/a | if (new_delay_flag != delay_flag) |
---|
657 | n/a | if (fcntl(s->sock_fd, F_SETFL, new_delay_flag) == -1) |
---|
658 | n/a | goto done; |
---|
659 | n/a | #endif |
---|
660 | n/a | #else /* MS_WINDOWS */ |
---|
661 | n/a | arg = !block; |
---|
662 | n/a | if (ioctlsocket(s->sock_fd, FIONBIO, &arg) != 0) |
---|
663 | n/a | goto done; |
---|
664 | n/a | #endif /* MS_WINDOWS */ |
---|
665 | n/a | |
---|
666 | n/a | result = 0; |
---|
667 | n/a | |
---|
668 | n/a | done: |
---|
669 | n/a | ; /* necessary for --without-threads flag */ |
---|
670 | n/a | Py_END_ALLOW_THREADS |
---|
671 | n/a | |
---|
672 | n/a | if (result) { |
---|
673 | n/a | #ifndef MS_WINDOWS |
---|
674 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
675 | n/a | #else |
---|
676 | n/a | PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
---|
677 | n/a | #endif |
---|
678 | n/a | } |
---|
679 | n/a | |
---|
680 | n/a | return result; |
---|
681 | n/a | } |
---|
682 | n/a | |
---|
683 | n/a | static int |
---|
684 | n/a | internal_select(PySocketSockObject *s, int writing, _PyTime_t interval, |
---|
685 | n/a | int connect) |
---|
686 | n/a | { |
---|
687 | n/a | int n; |
---|
688 | n/a | #ifdef HAVE_POLL |
---|
689 | n/a | struct pollfd pollfd; |
---|
690 | n/a | _PyTime_t ms; |
---|
691 | n/a | #else |
---|
692 | n/a | fd_set fds, efds; |
---|
693 | n/a | struct timeval tv, *tvp; |
---|
694 | n/a | #endif |
---|
695 | n/a | |
---|
696 | n/a | #ifdef WITH_THREAD |
---|
697 | n/a | /* must be called with the GIL held */ |
---|
698 | n/a | assert(PyGILState_Check()); |
---|
699 | n/a | #endif |
---|
700 | n/a | |
---|
701 | n/a | /* Error condition is for output only */ |
---|
702 | n/a | assert(!(connect && !writing)); |
---|
703 | n/a | |
---|
704 | n/a | /* Guard against closed socket */ |
---|
705 | n/a | if (s->sock_fd == INVALID_SOCKET) |
---|
706 | n/a | return 0; |
---|
707 | n/a | |
---|
708 | n/a | /* Prefer poll, if available, since you can poll() any fd |
---|
709 | n/a | * which can't be done with select(). */ |
---|
710 | n/a | #ifdef HAVE_POLL |
---|
711 | n/a | pollfd.fd = s->sock_fd; |
---|
712 | n/a | pollfd.events = writing ? POLLOUT : POLLIN; |
---|
713 | n/a | if (connect) { |
---|
714 | n/a | /* On Windows, the socket becomes writable on connection success, |
---|
715 | n/a | but a connection failure is notified as an error. On POSIX, the |
---|
716 | n/a | socket becomes writable on connection success or on connection |
---|
717 | n/a | failure. */ |
---|
718 | n/a | pollfd.events |= POLLERR; |
---|
719 | n/a | } |
---|
720 | n/a | |
---|
721 | n/a | /* s->sock_timeout is in seconds, timeout in ms */ |
---|
722 | n/a | ms = _PyTime_AsMilliseconds(interval, _PyTime_ROUND_CEILING); |
---|
723 | n/a | assert(ms <= INT_MAX); |
---|
724 | n/a | |
---|
725 | n/a | Py_BEGIN_ALLOW_THREADS; |
---|
726 | n/a | n = poll(&pollfd, 1, (int)ms); |
---|
727 | n/a | Py_END_ALLOW_THREADS; |
---|
728 | n/a | #else |
---|
729 | n/a | if (interval >= 0) { |
---|
730 | n/a | _PyTime_AsTimeval_noraise(interval, &tv, _PyTime_ROUND_CEILING); |
---|
731 | n/a | tvp = &tv; |
---|
732 | n/a | } |
---|
733 | n/a | else |
---|
734 | n/a | tvp = NULL; |
---|
735 | n/a | |
---|
736 | n/a | FD_ZERO(&fds); |
---|
737 | n/a | FD_SET(s->sock_fd, &fds); |
---|
738 | n/a | FD_ZERO(&efds); |
---|
739 | n/a | if (connect) { |
---|
740 | n/a | /* On Windows, the socket becomes writable on connection success, |
---|
741 | n/a | but a connection failure is notified as an error. On POSIX, the |
---|
742 | n/a | socket becomes writable on connection success or on connection |
---|
743 | n/a | failure. */ |
---|
744 | n/a | FD_SET(s->sock_fd, &efds); |
---|
745 | n/a | } |
---|
746 | n/a | |
---|
747 | n/a | /* See if the socket is ready */ |
---|
748 | n/a | Py_BEGIN_ALLOW_THREADS; |
---|
749 | n/a | if (writing) |
---|
750 | n/a | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
---|
751 | n/a | NULL, &fds, &efds, tvp); |
---|
752 | n/a | else |
---|
753 | n/a | n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), |
---|
754 | n/a | &fds, NULL, &efds, tvp); |
---|
755 | n/a | Py_END_ALLOW_THREADS; |
---|
756 | n/a | #endif |
---|
757 | n/a | |
---|
758 | n/a | if (n < 0) |
---|
759 | n/a | return -1; |
---|
760 | n/a | if (n == 0) |
---|
761 | n/a | return 1; |
---|
762 | n/a | return 0; |
---|
763 | n/a | } |
---|
764 | n/a | |
---|
765 | n/a | /* Call a socket function. |
---|
766 | n/a | |
---|
767 | n/a | On error, raise an exception and return -1 if err is set, or fill err and |
---|
768 | n/a | return -1 otherwise. If a signal was received and the signal handler raised |
---|
769 | n/a | an exception, return -1, and set err to -1 if err is set. |
---|
770 | n/a | |
---|
771 | n/a | On success, return 0, and set err to 0 if err is set. |
---|
772 | n/a | |
---|
773 | n/a | If the socket has a timeout, wait until the socket is ready before calling |
---|
774 | n/a | the function: wait until the socket is writable if writing is nonzero, wait |
---|
775 | n/a | until the socket received data otherwise. |
---|
776 | n/a | |
---|
777 | n/a | If the socket function is interrupted by a signal (failed with EINTR): retry |
---|
778 | n/a | the function, except if the signal handler raised an exception (PEP 475). |
---|
779 | n/a | |
---|
780 | n/a | When the function is retried, recompute the timeout using a monotonic clock. |
---|
781 | n/a | |
---|
782 | n/a | sock_call_ex() must be called with the GIL held. The socket function is |
---|
783 | n/a | called with the GIL released. */ |
---|
784 | n/a | static int |
---|
785 | n/a | sock_call_ex(PySocketSockObject *s, |
---|
786 | n/a | int writing, |
---|
787 | n/a | int (*sock_func) (PySocketSockObject *s, void *data), |
---|
788 | n/a | void *data, |
---|
789 | n/a | int connect, |
---|
790 | n/a | int *err, |
---|
791 | n/a | _PyTime_t timeout) |
---|
792 | n/a | { |
---|
793 | n/a | int has_timeout = (timeout > 0); |
---|
794 | n/a | _PyTime_t deadline = 0; |
---|
795 | n/a | int deadline_initialized = 0; |
---|
796 | n/a | int res; |
---|
797 | n/a | |
---|
798 | n/a | #ifdef WITH_THREAD |
---|
799 | n/a | /* sock_call() must be called with the GIL held. */ |
---|
800 | n/a | assert(PyGILState_Check()); |
---|
801 | n/a | #endif |
---|
802 | n/a | |
---|
803 | n/a | /* outer loop to retry select() when select() is interrupted by a signal |
---|
804 | n/a | or to retry select()+sock_func() on false positive (see above) */ |
---|
805 | n/a | while (1) { |
---|
806 | n/a | /* For connect(), poll even for blocking socket. The connection |
---|
807 | n/a | runs asynchronously. */ |
---|
808 | n/a | if (has_timeout || connect) { |
---|
809 | n/a | if (has_timeout) { |
---|
810 | n/a | _PyTime_t interval; |
---|
811 | n/a | |
---|
812 | n/a | if (deadline_initialized) { |
---|
813 | n/a | /* recompute the timeout */ |
---|
814 | n/a | interval = deadline - _PyTime_GetMonotonicClock(); |
---|
815 | n/a | } |
---|
816 | n/a | else { |
---|
817 | n/a | deadline_initialized = 1; |
---|
818 | n/a | deadline = _PyTime_GetMonotonicClock() + timeout; |
---|
819 | n/a | interval = timeout; |
---|
820 | n/a | } |
---|
821 | n/a | |
---|
822 | n/a | if (interval >= 0) |
---|
823 | n/a | res = internal_select(s, writing, interval, connect); |
---|
824 | n/a | else |
---|
825 | n/a | res = 1; |
---|
826 | n/a | } |
---|
827 | n/a | else { |
---|
828 | n/a | res = internal_select(s, writing, timeout, connect); |
---|
829 | n/a | } |
---|
830 | n/a | |
---|
831 | n/a | if (res == -1) { |
---|
832 | n/a | if (err) |
---|
833 | n/a | *err = GET_SOCK_ERROR; |
---|
834 | n/a | |
---|
835 | n/a | if (CHECK_ERRNO(EINTR)) { |
---|
836 | n/a | /* select() was interrupted by a signal */ |
---|
837 | n/a | if (PyErr_CheckSignals()) { |
---|
838 | n/a | if (err) |
---|
839 | n/a | *err = -1; |
---|
840 | n/a | return -1; |
---|
841 | n/a | } |
---|
842 | n/a | |
---|
843 | n/a | /* retry select() */ |
---|
844 | n/a | continue; |
---|
845 | n/a | } |
---|
846 | n/a | |
---|
847 | n/a | /* select() failed */ |
---|
848 | n/a | s->errorhandler(); |
---|
849 | n/a | return -1; |
---|
850 | n/a | } |
---|
851 | n/a | |
---|
852 | n/a | if (res == 1) { |
---|
853 | n/a | if (err) |
---|
854 | n/a | *err = SOCK_TIMEOUT_ERR; |
---|
855 | n/a | else |
---|
856 | n/a | PyErr_SetString(socket_timeout, "timed out"); |
---|
857 | n/a | return -1; |
---|
858 | n/a | } |
---|
859 | n/a | |
---|
860 | n/a | /* the socket is ready */ |
---|
861 | n/a | } |
---|
862 | n/a | |
---|
863 | n/a | /* inner loop to retry sock_func() when sock_func() is interrupted |
---|
864 | n/a | by a signal */ |
---|
865 | n/a | while (1) { |
---|
866 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
867 | n/a | res = sock_func(s, data); |
---|
868 | n/a | Py_END_ALLOW_THREADS |
---|
869 | n/a | |
---|
870 | n/a | if (res) { |
---|
871 | n/a | /* sock_func() succeeded */ |
---|
872 | n/a | if (err) |
---|
873 | n/a | *err = 0; |
---|
874 | n/a | return 0; |
---|
875 | n/a | } |
---|
876 | n/a | |
---|
877 | n/a | if (err) |
---|
878 | n/a | *err = GET_SOCK_ERROR; |
---|
879 | n/a | |
---|
880 | n/a | if (!CHECK_ERRNO(EINTR)) |
---|
881 | n/a | break; |
---|
882 | n/a | |
---|
883 | n/a | /* sock_func() was interrupted by a signal */ |
---|
884 | n/a | if (PyErr_CheckSignals()) { |
---|
885 | n/a | if (err) |
---|
886 | n/a | *err = -1; |
---|
887 | n/a | return -1; |
---|
888 | n/a | } |
---|
889 | n/a | |
---|
890 | n/a | /* retry sock_func() */ |
---|
891 | n/a | } |
---|
892 | n/a | |
---|
893 | n/a | if (s->sock_timeout > 0 |
---|
894 | n/a | && (CHECK_ERRNO(EWOULDBLOCK) || CHECK_ERRNO(EAGAIN))) { |
---|
895 | n/a | /* False positive: sock_func() failed with EWOULDBLOCK or EAGAIN. |
---|
896 | n/a | |
---|
897 | n/a | For example, select() could indicate a socket is ready for |
---|
898 | n/a | reading, but the data then discarded by the OS because of a |
---|
899 | n/a | wrong checksum. |
---|
900 | n/a | |
---|
901 | n/a | Loop on select() to recheck for socket readyness. */ |
---|
902 | n/a | continue; |
---|
903 | n/a | } |
---|
904 | n/a | |
---|
905 | n/a | /* sock_func() failed */ |
---|
906 | n/a | if (!err) |
---|
907 | n/a | s->errorhandler(); |
---|
908 | n/a | /* else: err was already set before */ |
---|
909 | n/a | return -1; |
---|
910 | n/a | } |
---|
911 | n/a | } |
---|
912 | n/a | |
---|
913 | n/a | static int |
---|
914 | n/a | sock_call(PySocketSockObject *s, |
---|
915 | n/a | int writing, |
---|
916 | n/a | int (*func) (PySocketSockObject *s, void *data), |
---|
917 | n/a | void *data) |
---|
918 | n/a | { |
---|
919 | n/a | return sock_call_ex(s, writing, func, data, 0, NULL, s->sock_timeout); |
---|
920 | n/a | } |
---|
921 | n/a | |
---|
922 | n/a | |
---|
923 | n/a | /* Initialize a new socket object. */ |
---|
924 | n/a | |
---|
925 | n/a | /* Default timeout for new sockets */ |
---|
926 | n/a | static _PyTime_t defaulttimeout = _PYTIME_FROMSECONDS(-1); |
---|
927 | n/a | |
---|
928 | n/a | static int |
---|
929 | n/a | init_sockobject(PySocketSockObject *s, |
---|
930 | n/a | SOCKET_T fd, int family, int type, int proto) |
---|
931 | n/a | { |
---|
932 | n/a | s->sock_fd = fd; |
---|
933 | n/a | s->sock_family = family; |
---|
934 | n/a | s->sock_type = type; |
---|
935 | n/a | s->sock_proto = proto; |
---|
936 | n/a | |
---|
937 | n/a | s->errorhandler = &set_error; |
---|
938 | n/a | #ifdef SOCK_NONBLOCK |
---|
939 | n/a | if (type & SOCK_NONBLOCK) |
---|
940 | n/a | s->sock_timeout = 0; |
---|
941 | n/a | else |
---|
942 | n/a | #endif |
---|
943 | n/a | { |
---|
944 | n/a | s->sock_timeout = defaulttimeout; |
---|
945 | n/a | if (defaulttimeout >= 0) { |
---|
946 | n/a | if (internal_setblocking(s, 0) == -1) { |
---|
947 | n/a | return -1; |
---|
948 | n/a | } |
---|
949 | n/a | } |
---|
950 | n/a | } |
---|
951 | n/a | return 0; |
---|
952 | n/a | } |
---|
953 | n/a | |
---|
954 | n/a | |
---|
955 | n/a | /* Create a new socket object. |
---|
956 | n/a | This just creates the object and initializes it. |
---|
957 | n/a | If the creation fails, return NULL and set an exception (implicit |
---|
958 | n/a | in NEWOBJ()). */ |
---|
959 | n/a | |
---|
960 | n/a | static PySocketSockObject * |
---|
961 | n/a | new_sockobject(SOCKET_T fd, int family, int type, int proto) |
---|
962 | n/a | { |
---|
963 | n/a | PySocketSockObject *s; |
---|
964 | n/a | s = (PySocketSockObject *) |
---|
965 | n/a | PyType_GenericNew(&sock_type, NULL, NULL); |
---|
966 | n/a | if (s == NULL) |
---|
967 | n/a | return NULL; |
---|
968 | n/a | if (init_sockobject(s, fd, family, type, proto) == -1) { |
---|
969 | n/a | Py_DECREF(s); |
---|
970 | n/a | return NULL; |
---|
971 | n/a | } |
---|
972 | n/a | return s; |
---|
973 | n/a | } |
---|
974 | n/a | |
---|
975 | n/a | |
---|
976 | n/a | /* Lock to allow python interpreter to continue, but only allow one |
---|
977 | n/a | thread to be in gethostbyname or getaddrinfo */ |
---|
978 | n/a | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
---|
979 | n/a | static PyThread_type_lock netdb_lock; |
---|
980 | n/a | #endif |
---|
981 | n/a | |
---|
982 | n/a | |
---|
983 | n/a | /* Convert a string specifying a host name or one of a few symbolic |
---|
984 | n/a | names to a numeric IP address. This usually calls gethostbyname() |
---|
985 | n/a | to do the work; the names "" and "<broadcast>" are special. |
---|
986 | n/a | Return the length (IPv4 should be 4 bytes), or negative if |
---|
987 | n/a | an error occurred; then an exception is raised. */ |
---|
988 | n/a | |
---|
989 | n/a | static int |
---|
990 | n/a | setipaddr(const char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af) |
---|
991 | n/a | { |
---|
992 | n/a | struct addrinfo hints, *res; |
---|
993 | n/a | int error; |
---|
994 | n/a | |
---|
995 | n/a | memset((void *) addr_ret, '\0', sizeof(*addr_ret)); |
---|
996 | n/a | if (name[0] == '\0') { |
---|
997 | n/a | int siz; |
---|
998 | n/a | memset(&hints, 0, sizeof(hints)); |
---|
999 | n/a | hints.ai_family = af; |
---|
1000 | n/a | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
---|
1001 | n/a | hints.ai_flags = AI_PASSIVE; |
---|
1002 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1003 | n/a | ACQUIRE_GETADDRINFO_LOCK |
---|
1004 | n/a | error = getaddrinfo(NULL, "0", &hints, &res); |
---|
1005 | n/a | Py_END_ALLOW_THREADS |
---|
1006 | n/a | /* We assume that those thread-unsafe getaddrinfo() versions |
---|
1007 | n/a | *are* safe regarding their return value, ie. that a |
---|
1008 | n/a | subsequent call to getaddrinfo() does not destroy the |
---|
1009 | n/a | outcome of the first call. */ |
---|
1010 | n/a | RELEASE_GETADDRINFO_LOCK |
---|
1011 | n/a | if (error) { |
---|
1012 | n/a | set_gaierror(error); |
---|
1013 | n/a | return -1; |
---|
1014 | n/a | } |
---|
1015 | n/a | switch (res->ai_family) { |
---|
1016 | n/a | case AF_INET: |
---|
1017 | n/a | siz = 4; |
---|
1018 | n/a | break; |
---|
1019 | n/a | #ifdef ENABLE_IPV6 |
---|
1020 | n/a | case AF_INET6: |
---|
1021 | n/a | siz = 16; |
---|
1022 | n/a | break; |
---|
1023 | n/a | #endif |
---|
1024 | n/a | default: |
---|
1025 | n/a | freeaddrinfo(res); |
---|
1026 | n/a | PyErr_SetString(PyExc_OSError, |
---|
1027 | n/a | "unsupported address family"); |
---|
1028 | n/a | return -1; |
---|
1029 | n/a | } |
---|
1030 | n/a | if (res->ai_next) { |
---|
1031 | n/a | freeaddrinfo(res); |
---|
1032 | n/a | PyErr_SetString(PyExc_OSError, |
---|
1033 | n/a | "wildcard resolved to multiple address"); |
---|
1034 | n/a | return -1; |
---|
1035 | n/a | } |
---|
1036 | n/a | if (res->ai_addrlen < addr_ret_size) |
---|
1037 | n/a | addr_ret_size = res->ai_addrlen; |
---|
1038 | n/a | memcpy(addr_ret, res->ai_addr, addr_ret_size); |
---|
1039 | n/a | freeaddrinfo(res); |
---|
1040 | n/a | return siz; |
---|
1041 | n/a | } |
---|
1042 | n/a | /* special-case broadcast - inet_addr() below can return INADDR_NONE for |
---|
1043 | n/a | * this */ |
---|
1044 | n/a | if (strcmp(name, "255.255.255.255") == 0 || |
---|
1045 | n/a | strcmp(name, "<broadcast>") == 0) { |
---|
1046 | n/a | struct sockaddr_in *sin; |
---|
1047 | n/a | if (af != AF_INET && af != AF_UNSPEC) { |
---|
1048 | n/a | PyErr_SetString(PyExc_OSError, |
---|
1049 | n/a | "address family mismatched"); |
---|
1050 | n/a | return -1; |
---|
1051 | n/a | } |
---|
1052 | n/a | sin = (struct sockaddr_in *)addr_ret; |
---|
1053 | n/a | memset((void *) sin, '\0', sizeof(*sin)); |
---|
1054 | n/a | sin->sin_family = AF_INET; |
---|
1055 | n/a | #ifdef HAVE_SOCKADDR_SA_LEN |
---|
1056 | n/a | sin->sin_len = sizeof(*sin); |
---|
1057 | n/a | #endif |
---|
1058 | n/a | sin->sin_addr.s_addr = INADDR_BROADCAST; |
---|
1059 | n/a | return sizeof(sin->sin_addr); |
---|
1060 | n/a | } |
---|
1061 | n/a | |
---|
1062 | n/a | /* avoid a name resolution in case of numeric address */ |
---|
1063 | n/a | #ifdef HAVE_INET_PTON |
---|
1064 | n/a | /* check for an IPv4 address */ |
---|
1065 | n/a | if (af == AF_UNSPEC || af == AF_INET) { |
---|
1066 | n/a | struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret; |
---|
1067 | n/a | memset(sin, 0, sizeof(*sin)); |
---|
1068 | n/a | if (inet_pton(AF_INET, name, &sin->sin_addr) > 0) { |
---|
1069 | n/a | sin->sin_family = AF_INET; |
---|
1070 | n/a | #ifdef HAVE_SOCKADDR_SA_LEN |
---|
1071 | n/a | sin->sin_len = sizeof(*sin); |
---|
1072 | n/a | #endif |
---|
1073 | n/a | return 4; |
---|
1074 | n/a | } |
---|
1075 | n/a | } |
---|
1076 | n/a | #ifdef ENABLE_IPV6 |
---|
1077 | n/a | /* check for an IPv6 address - if the address contains a scope ID, we |
---|
1078 | n/a | * fallback to getaddrinfo(), which can handle translation from interface |
---|
1079 | n/a | * name to interface index */ |
---|
1080 | n/a | if ((af == AF_UNSPEC || af == AF_INET6) && !strchr(name, '%')) { |
---|
1081 | n/a | struct sockaddr_in6 *sin = (struct sockaddr_in6 *)addr_ret; |
---|
1082 | n/a | memset(sin, 0, sizeof(*sin)); |
---|
1083 | n/a | if (inet_pton(AF_INET6, name, &sin->sin6_addr) > 0) { |
---|
1084 | n/a | sin->sin6_family = AF_INET6; |
---|
1085 | n/a | #ifdef HAVE_SOCKADDR_SA_LEN |
---|
1086 | n/a | sin->sin6_len = sizeof(*sin); |
---|
1087 | n/a | #endif |
---|
1088 | n/a | return 16; |
---|
1089 | n/a | } |
---|
1090 | n/a | } |
---|
1091 | n/a | #endif /* ENABLE_IPV6 */ |
---|
1092 | n/a | #else /* HAVE_INET_PTON */ |
---|
1093 | n/a | /* check for an IPv4 address */ |
---|
1094 | n/a | if (af == AF_INET || af == AF_UNSPEC) { |
---|
1095 | n/a | struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret; |
---|
1096 | n/a | memset(sin, 0, sizeof(*sin)); |
---|
1097 | n/a | if ((sin->sin_addr.s_addr = inet_addr(name)) != INADDR_NONE) { |
---|
1098 | n/a | sin->sin_family = AF_INET; |
---|
1099 | n/a | #ifdef HAVE_SOCKADDR_SA_LEN |
---|
1100 | n/a | sin->sin_len = sizeof(*sin); |
---|
1101 | n/a | #endif |
---|
1102 | n/a | return 4; |
---|
1103 | n/a | } |
---|
1104 | n/a | } |
---|
1105 | n/a | #endif /* HAVE_INET_PTON */ |
---|
1106 | n/a | |
---|
1107 | n/a | /* perform a name resolution */ |
---|
1108 | n/a | memset(&hints, 0, sizeof(hints)); |
---|
1109 | n/a | hints.ai_family = af; |
---|
1110 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1111 | n/a | ACQUIRE_GETADDRINFO_LOCK |
---|
1112 | n/a | error = getaddrinfo(name, NULL, &hints, &res); |
---|
1113 | n/a | #if defined(__digital__) && defined(__unix__) |
---|
1114 | n/a | if (error == EAI_NONAME && af == AF_UNSPEC) { |
---|
1115 | n/a | /* On Tru64 V5.1, numeric-to-addr conversion fails |
---|
1116 | n/a | if no address family is given. Assume IPv4 for now.*/ |
---|
1117 | n/a | hints.ai_family = AF_INET; |
---|
1118 | n/a | error = getaddrinfo(name, NULL, &hints, &res); |
---|
1119 | n/a | } |
---|
1120 | n/a | #endif |
---|
1121 | n/a | Py_END_ALLOW_THREADS |
---|
1122 | n/a | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
---|
1123 | n/a | if (error) { |
---|
1124 | n/a | set_gaierror(error); |
---|
1125 | n/a | return -1; |
---|
1126 | n/a | } |
---|
1127 | n/a | if (res->ai_addrlen < addr_ret_size) |
---|
1128 | n/a | addr_ret_size = res->ai_addrlen; |
---|
1129 | n/a | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size); |
---|
1130 | n/a | freeaddrinfo(res); |
---|
1131 | n/a | switch (addr_ret->sa_family) { |
---|
1132 | n/a | case AF_INET: |
---|
1133 | n/a | return 4; |
---|
1134 | n/a | #ifdef ENABLE_IPV6 |
---|
1135 | n/a | case AF_INET6: |
---|
1136 | n/a | return 16; |
---|
1137 | n/a | #endif |
---|
1138 | n/a | default: |
---|
1139 | n/a | PyErr_SetString(PyExc_OSError, "unknown address family"); |
---|
1140 | n/a | return -1; |
---|
1141 | n/a | } |
---|
1142 | n/a | } |
---|
1143 | n/a | |
---|
1144 | n/a | |
---|
1145 | n/a | /* Create a string object representing an IP address. |
---|
1146 | n/a | This is always a string of the form 'dd.dd.dd.dd' (with variable |
---|
1147 | n/a | size numbers). */ |
---|
1148 | n/a | |
---|
1149 | n/a | static PyObject * |
---|
1150 | n/a | makeipaddr(struct sockaddr *addr, int addrlen) |
---|
1151 | n/a | { |
---|
1152 | n/a | char buf[NI_MAXHOST]; |
---|
1153 | n/a | int error; |
---|
1154 | n/a | |
---|
1155 | n/a | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
---|
1156 | n/a | NI_NUMERICHOST); |
---|
1157 | n/a | if (error) { |
---|
1158 | n/a | set_gaierror(error); |
---|
1159 | n/a | return NULL; |
---|
1160 | n/a | } |
---|
1161 | n/a | return PyUnicode_FromString(buf); |
---|
1162 | n/a | } |
---|
1163 | n/a | |
---|
1164 | n/a | |
---|
1165 | n/a | #ifdef USE_BLUETOOTH |
---|
1166 | n/a | /* Convert a string representation of a Bluetooth address into a numeric |
---|
1167 | n/a | address. Returns the length (6), or raises an exception and returns -1 if |
---|
1168 | n/a | an error occurred. */ |
---|
1169 | n/a | |
---|
1170 | n/a | static int |
---|
1171 | n/a | setbdaddr(const char *name, bdaddr_t *bdaddr) |
---|
1172 | n/a | { |
---|
1173 | n/a | unsigned int b0, b1, b2, b3, b4, b5; |
---|
1174 | n/a | char ch; |
---|
1175 | n/a | int n; |
---|
1176 | n/a | |
---|
1177 | n/a | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c", |
---|
1178 | n/a | &b5, &b4, &b3, &b2, &b1, &b0, &ch); |
---|
1179 | n/a | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) { |
---|
1180 | n/a | bdaddr->b[0] = b0; |
---|
1181 | n/a | bdaddr->b[1] = b1; |
---|
1182 | n/a | bdaddr->b[2] = b2; |
---|
1183 | n/a | bdaddr->b[3] = b3; |
---|
1184 | n/a | bdaddr->b[4] = b4; |
---|
1185 | n/a | bdaddr->b[5] = b5; |
---|
1186 | n/a | return 6; |
---|
1187 | n/a | } else { |
---|
1188 | n/a | PyErr_SetString(PyExc_OSError, "bad bluetooth address"); |
---|
1189 | n/a | return -1; |
---|
1190 | n/a | } |
---|
1191 | n/a | } |
---|
1192 | n/a | |
---|
1193 | n/a | /* Create a string representation of the Bluetooth address. This is always a |
---|
1194 | n/a | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal |
---|
1195 | n/a | value (zero padded if necessary). */ |
---|
1196 | n/a | |
---|
1197 | n/a | static PyObject * |
---|
1198 | n/a | makebdaddr(bdaddr_t *bdaddr) |
---|
1199 | n/a | { |
---|
1200 | n/a | char buf[(6 * 2) + 5 + 1]; |
---|
1201 | n/a | |
---|
1202 | n/a | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
---|
1203 | n/a | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3], |
---|
1204 | n/a | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]); |
---|
1205 | n/a | return PyUnicode_FromString(buf); |
---|
1206 | n/a | } |
---|
1207 | n/a | #endif |
---|
1208 | n/a | |
---|
1209 | n/a | |
---|
1210 | n/a | /* Create an object representing the given socket address, |
---|
1211 | n/a | suitable for passing it back to bind(), connect() etc. |
---|
1212 | n/a | The family field of the sockaddr structure is inspected |
---|
1213 | n/a | to determine what kind of address it really is. */ |
---|
1214 | n/a | |
---|
1215 | n/a | /*ARGSUSED*/ |
---|
1216 | n/a | static PyObject * |
---|
1217 | n/a | makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto) |
---|
1218 | n/a | { |
---|
1219 | n/a | if (addrlen == 0) { |
---|
1220 | n/a | /* No address -- may be recvfrom() from known socket */ |
---|
1221 | n/a | Py_RETURN_NONE; |
---|
1222 | n/a | } |
---|
1223 | n/a | |
---|
1224 | n/a | switch (addr->sa_family) { |
---|
1225 | n/a | |
---|
1226 | n/a | case AF_INET: |
---|
1227 | n/a | { |
---|
1228 | n/a | struct sockaddr_in *a; |
---|
1229 | n/a | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
---|
1230 | n/a | PyObject *ret = NULL; |
---|
1231 | n/a | if (addrobj) { |
---|
1232 | n/a | a = (struct sockaddr_in *)addr; |
---|
1233 | n/a | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port)); |
---|
1234 | n/a | Py_DECREF(addrobj); |
---|
1235 | n/a | } |
---|
1236 | n/a | return ret; |
---|
1237 | n/a | } |
---|
1238 | n/a | |
---|
1239 | n/a | #if defined(AF_UNIX) |
---|
1240 | n/a | case AF_UNIX: |
---|
1241 | n/a | { |
---|
1242 | n/a | struct sockaddr_un *a = (struct sockaddr_un *) addr; |
---|
1243 | n/a | #ifdef __linux__ |
---|
1244 | n/a | if (a->sun_path[0] == 0) { /* Linux abstract namespace */ |
---|
1245 | n/a | addrlen -= offsetof(struct sockaddr_un, sun_path); |
---|
1246 | n/a | return PyBytes_FromStringAndSize(a->sun_path, addrlen); |
---|
1247 | n/a | } |
---|
1248 | n/a | else |
---|
1249 | n/a | #endif /* linux */ |
---|
1250 | n/a | { |
---|
1251 | n/a | /* regular NULL-terminated string */ |
---|
1252 | n/a | return PyUnicode_DecodeFSDefault(a->sun_path); |
---|
1253 | n/a | } |
---|
1254 | n/a | } |
---|
1255 | n/a | #endif /* AF_UNIX */ |
---|
1256 | n/a | |
---|
1257 | n/a | #if defined(AF_NETLINK) |
---|
1258 | n/a | case AF_NETLINK: |
---|
1259 | n/a | { |
---|
1260 | n/a | struct sockaddr_nl *a = (struct sockaddr_nl *) addr; |
---|
1261 | n/a | return Py_BuildValue("II", a->nl_pid, a->nl_groups); |
---|
1262 | n/a | } |
---|
1263 | n/a | #endif /* AF_NETLINK */ |
---|
1264 | n/a | |
---|
1265 | n/a | #ifdef ENABLE_IPV6 |
---|
1266 | n/a | case AF_INET6: |
---|
1267 | n/a | { |
---|
1268 | n/a | struct sockaddr_in6 *a; |
---|
1269 | n/a | PyObject *addrobj = makeipaddr(addr, sizeof(*a)); |
---|
1270 | n/a | PyObject *ret = NULL; |
---|
1271 | n/a | if (addrobj) { |
---|
1272 | n/a | a = (struct sockaddr_in6 *)addr; |
---|
1273 | n/a | ret = Py_BuildValue("OiII", |
---|
1274 | n/a | addrobj, |
---|
1275 | n/a | ntohs(a->sin6_port), |
---|
1276 | n/a | ntohl(a->sin6_flowinfo), |
---|
1277 | n/a | a->sin6_scope_id); |
---|
1278 | n/a | Py_DECREF(addrobj); |
---|
1279 | n/a | } |
---|
1280 | n/a | return ret; |
---|
1281 | n/a | } |
---|
1282 | n/a | #endif |
---|
1283 | n/a | |
---|
1284 | n/a | #ifdef USE_BLUETOOTH |
---|
1285 | n/a | case AF_BLUETOOTH: |
---|
1286 | n/a | switch (proto) { |
---|
1287 | n/a | |
---|
1288 | n/a | case BTPROTO_L2CAP: |
---|
1289 | n/a | { |
---|
1290 | n/a | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr; |
---|
1291 | n/a | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr)); |
---|
1292 | n/a | PyObject *ret = NULL; |
---|
1293 | n/a | if (addrobj) { |
---|
1294 | n/a | ret = Py_BuildValue("Oi", |
---|
1295 | n/a | addrobj, |
---|
1296 | n/a | _BT_L2_MEMB(a, psm)); |
---|
1297 | n/a | Py_DECREF(addrobj); |
---|
1298 | n/a | } |
---|
1299 | n/a | return ret; |
---|
1300 | n/a | } |
---|
1301 | n/a | |
---|
1302 | n/a | case BTPROTO_RFCOMM: |
---|
1303 | n/a | { |
---|
1304 | n/a | struct sockaddr_rc *a = (struct sockaddr_rc *) addr; |
---|
1305 | n/a | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr)); |
---|
1306 | n/a | PyObject *ret = NULL; |
---|
1307 | n/a | if (addrobj) { |
---|
1308 | n/a | ret = Py_BuildValue("Oi", |
---|
1309 | n/a | addrobj, |
---|
1310 | n/a | _BT_RC_MEMB(a, channel)); |
---|
1311 | n/a | Py_DECREF(addrobj); |
---|
1312 | n/a | } |
---|
1313 | n/a | return ret; |
---|
1314 | n/a | } |
---|
1315 | n/a | |
---|
1316 | n/a | case BTPROTO_HCI: |
---|
1317 | n/a | { |
---|
1318 | n/a | struct sockaddr_hci *a = (struct sockaddr_hci *) addr; |
---|
1319 | n/a | #if defined(__NetBSD__) || defined(__DragonFly__) |
---|
1320 | n/a | return makebdaddr(&_BT_HCI_MEMB(a, bdaddr)); |
---|
1321 | n/a | #else |
---|
1322 | n/a | PyObject *ret = NULL; |
---|
1323 | n/a | ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); |
---|
1324 | n/a | return ret; |
---|
1325 | n/a | #endif |
---|
1326 | n/a | } |
---|
1327 | n/a | |
---|
1328 | n/a | #if !defined(__FreeBSD__) |
---|
1329 | n/a | case BTPROTO_SCO: |
---|
1330 | n/a | { |
---|
1331 | n/a | struct sockaddr_sco *a = (struct sockaddr_sco *) addr; |
---|
1332 | n/a | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); |
---|
1333 | n/a | } |
---|
1334 | n/a | #endif |
---|
1335 | n/a | |
---|
1336 | n/a | default: |
---|
1337 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
1338 | n/a | "Unknown Bluetooth protocol"); |
---|
1339 | n/a | return NULL; |
---|
1340 | n/a | } |
---|
1341 | n/a | #endif |
---|
1342 | n/a | |
---|
1343 | n/a | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME) |
---|
1344 | n/a | case AF_PACKET: |
---|
1345 | n/a | { |
---|
1346 | n/a | struct sockaddr_ll *a = (struct sockaddr_ll *)addr; |
---|
1347 | n/a | char *ifname = ""; |
---|
1348 | n/a | struct ifreq ifr; |
---|
1349 | n/a | /* need to look up interface name give index */ |
---|
1350 | n/a | if (a->sll_ifindex) { |
---|
1351 | n/a | ifr.ifr_ifindex = a->sll_ifindex; |
---|
1352 | n/a | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
---|
1353 | n/a | ifname = ifr.ifr_name; |
---|
1354 | n/a | } |
---|
1355 | n/a | return Py_BuildValue("shbhy#", |
---|
1356 | n/a | ifname, |
---|
1357 | n/a | ntohs(a->sll_protocol), |
---|
1358 | n/a | a->sll_pkttype, |
---|
1359 | n/a | a->sll_hatype, |
---|
1360 | n/a | a->sll_addr, |
---|
1361 | n/a | a->sll_halen); |
---|
1362 | n/a | } |
---|
1363 | n/a | #endif |
---|
1364 | n/a | |
---|
1365 | n/a | #ifdef HAVE_LINUX_TIPC_H |
---|
1366 | n/a | case AF_TIPC: |
---|
1367 | n/a | { |
---|
1368 | n/a | struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr; |
---|
1369 | n/a | if (a->addrtype == TIPC_ADDR_NAMESEQ) { |
---|
1370 | n/a | return Py_BuildValue("IIIII", |
---|
1371 | n/a | a->addrtype, |
---|
1372 | n/a | a->addr.nameseq.type, |
---|
1373 | n/a | a->addr.nameseq.lower, |
---|
1374 | n/a | a->addr.nameseq.upper, |
---|
1375 | n/a | a->scope); |
---|
1376 | n/a | } else if (a->addrtype == TIPC_ADDR_NAME) { |
---|
1377 | n/a | return Py_BuildValue("IIIII", |
---|
1378 | n/a | a->addrtype, |
---|
1379 | n/a | a->addr.name.name.type, |
---|
1380 | n/a | a->addr.name.name.instance, |
---|
1381 | n/a | a->addr.name.name.instance, |
---|
1382 | n/a | a->scope); |
---|
1383 | n/a | } else if (a->addrtype == TIPC_ADDR_ID) { |
---|
1384 | n/a | return Py_BuildValue("IIIII", |
---|
1385 | n/a | a->addrtype, |
---|
1386 | n/a | a->addr.id.node, |
---|
1387 | n/a | a->addr.id.ref, |
---|
1388 | n/a | 0, |
---|
1389 | n/a | a->scope); |
---|
1390 | n/a | } else { |
---|
1391 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
1392 | n/a | "Invalid address type"); |
---|
1393 | n/a | return NULL; |
---|
1394 | n/a | } |
---|
1395 | n/a | } |
---|
1396 | n/a | #endif |
---|
1397 | n/a | |
---|
1398 | n/a | #ifdef AF_CAN |
---|
1399 | n/a | case AF_CAN: |
---|
1400 | n/a | { |
---|
1401 | n/a | struct sockaddr_can *a = (struct sockaddr_can *)addr; |
---|
1402 | n/a | char *ifname = ""; |
---|
1403 | n/a | struct ifreq ifr; |
---|
1404 | n/a | /* need to look up interface name given index */ |
---|
1405 | n/a | if (a->can_ifindex) { |
---|
1406 | n/a | ifr.ifr_ifindex = a->can_ifindex; |
---|
1407 | n/a | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0) |
---|
1408 | n/a | ifname = ifr.ifr_name; |
---|
1409 | n/a | } |
---|
1410 | n/a | |
---|
1411 | n/a | return Py_BuildValue("O&h", PyUnicode_DecodeFSDefault, |
---|
1412 | n/a | ifname, |
---|
1413 | n/a | a->can_family); |
---|
1414 | n/a | } |
---|
1415 | n/a | #endif |
---|
1416 | n/a | |
---|
1417 | n/a | #ifdef PF_SYSTEM |
---|
1418 | n/a | case PF_SYSTEM: |
---|
1419 | n/a | switch(proto) { |
---|
1420 | n/a | #ifdef SYSPROTO_CONTROL |
---|
1421 | n/a | case SYSPROTO_CONTROL: |
---|
1422 | n/a | { |
---|
1423 | n/a | struct sockaddr_ctl *a = (struct sockaddr_ctl *)addr; |
---|
1424 | n/a | return Py_BuildValue("(II)", a->sc_id, a->sc_unit); |
---|
1425 | n/a | } |
---|
1426 | n/a | #endif |
---|
1427 | n/a | default: |
---|
1428 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
1429 | n/a | "Invalid address type"); |
---|
1430 | n/a | return 0; |
---|
1431 | n/a | } |
---|
1432 | n/a | #endif |
---|
1433 | n/a | |
---|
1434 | n/a | #ifdef HAVE_SOCKADDR_ALG |
---|
1435 | n/a | case AF_ALG: |
---|
1436 | n/a | { |
---|
1437 | n/a | struct sockaddr_alg *a = (struct sockaddr_alg *)addr; |
---|
1438 | n/a | return Py_BuildValue("s#s#HH", |
---|
1439 | n/a | a->salg_type, |
---|
1440 | n/a | strnlen((const char*)a->salg_type, |
---|
1441 | n/a | sizeof(a->salg_type)), |
---|
1442 | n/a | a->salg_name, |
---|
1443 | n/a | strnlen((const char*)a->salg_name, |
---|
1444 | n/a | sizeof(a->salg_name)), |
---|
1445 | n/a | a->salg_feat, |
---|
1446 | n/a | a->salg_mask); |
---|
1447 | n/a | } |
---|
1448 | n/a | #endif |
---|
1449 | n/a | |
---|
1450 | n/a | /* More cases here... */ |
---|
1451 | n/a | |
---|
1452 | n/a | default: |
---|
1453 | n/a | /* If we don't know the address family, don't raise an |
---|
1454 | n/a | exception -- return it as an (int, bytes) tuple. */ |
---|
1455 | n/a | return Py_BuildValue("iy#", |
---|
1456 | n/a | addr->sa_family, |
---|
1457 | n/a | addr->sa_data, |
---|
1458 | n/a | sizeof(addr->sa_data)); |
---|
1459 | n/a | |
---|
1460 | n/a | } |
---|
1461 | n/a | } |
---|
1462 | n/a | |
---|
1463 | n/a | /* Helper for getsockaddrarg: bypass IDNA for ASCII-only host names |
---|
1464 | n/a | (in particular, numeric IP addresses). */ |
---|
1465 | n/a | struct maybe_idna { |
---|
1466 | n/a | PyObject *obj; |
---|
1467 | n/a | char *buf; |
---|
1468 | n/a | }; |
---|
1469 | n/a | |
---|
1470 | n/a | static void |
---|
1471 | n/a | idna_cleanup(struct maybe_idna *data) |
---|
1472 | n/a | { |
---|
1473 | n/a | Py_CLEAR(data->obj); |
---|
1474 | n/a | } |
---|
1475 | n/a | |
---|
1476 | n/a | static int |
---|
1477 | n/a | idna_converter(PyObject *obj, struct maybe_idna *data) |
---|
1478 | n/a | { |
---|
1479 | n/a | size_t len; |
---|
1480 | n/a | PyObject *obj2; |
---|
1481 | n/a | if (obj == NULL) { |
---|
1482 | n/a | idna_cleanup(data); |
---|
1483 | n/a | return 1; |
---|
1484 | n/a | } |
---|
1485 | n/a | data->obj = NULL; |
---|
1486 | n/a | len = -1; |
---|
1487 | n/a | if (PyBytes_Check(obj)) { |
---|
1488 | n/a | data->buf = PyBytes_AsString(obj); |
---|
1489 | n/a | len = PyBytes_Size(obj); |
---|
1490 | n/a | } |
---|
1491 | n/a | else if (PyByteArray_Check(obj)) { |
---|
1492 | n/a | data->buf = PyByteArray_AsString(obj); |
---|
1493 | n/a | len = PyByteArray_Size(obj); |
---|
1494 | n/a | } |
---|
1495 | n/a | else if (PyUnicode_Check(obj)) { |
---|
1496 | n/a | if (PyUnicode_READY(obj) == 0 && PyUnicode_IS_COMPACT_ASCII(obj)) { |
---|
1497 | n/a | data->buf = PyUnicode_DATA(obj); |
---|
1498 | n/a | len = PyUnicode_GET_LENGTH(obj); |
---|
1499 | n/a | } |
---|
1500 | n/a | else { |
---|
1501 | n/a | obj2 = PyUnicode_AsEncodedString(obj, "idna", NULL); |
---|
1502 | n/a | if (!obj2) { |
---|
1503 | n/a | PyErr_SetString(PyExc_TypeError, "encoding of hostname failed"); |
---|
1504 | n/a | return 0; |
---|
1505 | n/a | } |
---|
1506 | n/a | assert(PyBytes_Check(obj2)); |
---|
1507 | n/a | data->obj = obj2; |
---|
1508 | n/a | data->buf = PyBytes_AS_STRING(obj2); |
---|
1509 | n/a | len = PyBytes_GET_SIZE(obj2); |
---|
1510 | n/a | } |
---|
1511 | n/a | } |
---|
1512 | n/a | else { |
---|
1513 | n/a | PyErr_Format(PyExc_TypeError, "str, bytes or bytearray expected, not %s", |
---|
1514 | n/a | obj->ob_type->tp_name); |
---|
1515 | n/a | return 0; |
---|
1516 | n/a | } |
---|
1517 | n/a | if (strlen(data->buf) != len) { |
---|
1518 | n/a | Py_CLEAR(data->obj); |
---|
1519 | n/a | PyErr_SetString(PyExc_TypeError, "host name must not contain null character"); |
---|
1520 | n/a | return 0; |
---|
1521 | n/a | } |
---|
1522 | n/a | return Py_CLEANUP_SUPPORTED; |
---|
1523 | n/a | } |
---|
1524 | n/a | |
---|
1525 | n/a | /* Parse a socket address argument according to the socket object's |
---|
1526 | n/a | address family. Return 1 if the address was in the proper format, |
---|
1527 | n/a | 0 of not. The address is returned through addr_ret, its length |
---|
1528 | n/a | through len_ret. */ |
---|
1529 | n/a | |
---|
1530 | n/a | static int |
---|
1531 | n/a | getsockaddrarg(PySocketSockObject *s, PyObject *args, |
---|
1532 | n/a | struct sockaddr *addr_ret, int *len_ret) |
---|
1533 | n/a | { |
---|
1534 | n/a | switch (s->sock_family) { |
---|
1535 | n/a | |
---|
1536 | n/a | #if defined(AF_UNIX) |
---|
1537 | n/a | case AF_UNIX: |
---|
1538 | n/a | { |
---|
1539 | n/a | struct sockaddr_un* addr; |
---|
1540 | n/a | Py_buffer path; |
---|
1541 | n/a | int retval = 0; |
---|
1542 | n/a | |
---|
1543 | n/a | /* PEP 383. Not using PyUnicode_FSConverter since we need to |
---|
1544 | n/a | allow embedded nulls on Linux. */ |
---|
1545 | n/a | if (PyUnicode_Check(args)) { |
---|
1546 | n/a | if ((args = PyUnicode_EncodeFSDefault(args)) == NULL) |
---|
1547 | n/a | return 0; |
---|
1548 | n/a | } |
---|
1549 | n/a | else |
---|
1550 | n/a | Py_INCREF(args); |
---|
1551 | n/a | if (!PyArg_Parse(args, "y*", &path)) { |
---|
1552 | n/a | Py_DECREF(args); |
---|
1553 | n/a | return retval; |
---|
1554 | n/a | } |
---|
1555 | n/a | assert(path.len >= 0); |
---|
1556 | n/a | |
---|
1557 | n/a | addr = (struct sockaddr_un*)addr_ret; |
---|
1558 | n/a | #ifdef __linux__ |
---|
1559 | n/a | if (path.len > 0 && *(const char *)path.buf == 0) { |
---|
1560 | n/a | /* Linux abstract namespace extension */ |
---|
1561 | n/a | if ((size_t)path.len > sizeof addr->sun_path) { |
---|
1562 | n/a | PyErr_SetString(PyExc_OSError, |
---|
1563 | n/a | "AF_UNIX path too long"); |
---|
1564 | n/a | goto unix_out; |
---|
1565 | n/a | } |
---|
1566 | n/a | } |
---|
1567 | n/a | else |
---|
1568 | n/a | #endif /* linux */ |
---|
1569 | n/a | { |
---|
1570 | n/a | /* regular NULL-terminated string */ |
---|
1571 | n/a | if ((size_t)path.len >= sizeof addr->sun_path) { |
---|
1572 | n/a | PyErr_SetString(PyExc_OSError, |
---|
1573 | n/a | "AF_UNIX path too long"); |
---|
1574 | n/a | goto unix_out; |
---|
1575 | n/a | } |
---|
1576 | n/a | addr->sun_path[path.len] = 0; |
---|
1577 | n/a | } |
---|
1578 | n/a | addr->sun_family = s->sock_family; |
---|
1579 | n/a | memcpy(addr->sun_path, path.buf, path.len); |
---|
1580 | n/a | *len_ret = path.len + offsetof(struct sockaddr_un, sun_path); |
---|
1581 | n/a | retval = 1; |
---|
1582 | n/a | unix_out: |
---|
1583 | n/a | PyBuffer_Release(&path); |
---|
1584 | n/a | Py_DECREF(args); |
---|
1585 | n/a | return retval; |
---|
1586 | n/a | } |
---|
1587 | n/a | #endif /* AF_UNIX */ |
---|
1588 | n/a | |
---|
1589 | n/a | #if defined(AF_NETLINK) |
---|
1590 | n/a | case AF_NETLINK: |
---|
1591 | n/a | { |
---|
1592 | n/a | struct sockaddr_nl* addr; |
---|
1593 | n/a | int pid, groups; |
---|
1594 | n/a | addr = (struct sockaddr_nl *)addr_ret; |
---|
1595 | n/a | if (!PyTuple_Check(args)) { |
---|
1596 | n/a | PyErr_Format( |
---|
1597 | n/a | PyExc_TypeError, |
---|
1598 | n/a | "getsockaddrarg: " |
---|
1599 | n/a | "AF_NETLINK address must be tuple, not %.500s", |
---|
1600 | n/a | Py_TYPE(args)->tp_name); |
---|
1601 | n/a | return 0; |
---|
1602 | n/a | } |
---|
1603 | n/a | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups)) |
---|
1604 | n/a | return 0; |
---|
1605 | n/a | addr->nl_family = AF_NETLINK; |
---|
1606 | n/a | addr->nl_pid = pid; |
---|
1607 | n/a | addr->nl_groups = groups; |
---|
1608 | n/a | *len_ret = sizeof(*addr); |
---|
1609 | n/a | return 1; |
---|
1610 | n/a | } |
---|
1611 | n/a | #endif |
---|
1612 | n/a | |
---|
1613 | n/a | #ifdef AF_RDS |
---|
1614 | n/a | case AF_RDS: |
---|
1615 | n/a | /* RDS sockets use sockaddr_in: fall-through */ |
---|
1616 | n/a | #endif |
---|
1617 | n/a | |
---|
1618 | n/a | case AF_INET: |
---|
1619 | n/a | { |
---|
1620 | n/a | struct sockaddr_in* addr; |
---|
1621 | n/a | struct maybe_idna host = {NULL, NULL}; |
---|
1622 | n/a | int port, result; |
---|
1623 | n/a | if (!PyTuple_Check(args)) { |
---|
1624 | n/a | PyErr_Format( |
---|
1625 | n/a | PyExc_TypeError, |
---|
1626 | n/a | "getsockaddrarg: " |
---|
1627 | n/a | "AF_INET address must be tuple, not %.500s", |
---|
1628 | n/a | Py_TYPE(args)->tp_name); |
---|
1629 | n/a | return 0; |
---|
1630 | n/a | } |
---|
1631 | n/a | if (!PyArg_ParseTuple(args, "O&i:getsockaddrarg", |
---|
1632 | n/a | idna_converter, &host, &port)) |
---|
1633 | n/a | return 0; |
---|
1634 | n/a | addr=(struct sockaddr_in*)addr_ret; |
---|
1635 | n/a | result = setipaddr(host.buf, (struct sockaddr *)addr, |
---|
1636 | n/a | sizeof(*addr), AF_INET); |
---|
1637 | n/a | idna_cleanup(&host); |
---|
1638 | n/a | if (result < 0) |
---|
1639 | n/a | return 0; |
---|
1640 | n/a | if (port < 0 || port > 0xffff) { |
---|
1641 | n/a | PyErr_SetString( |
---|
1642 | n/a | PyExc_OverflowError, |
---|
1643 | n/a | "getsockaddrarg: port must be 0-65535."); |
---|
1644 | n/a | return 0; |
---|
1645 | n/a | } |
---|
1646 | n/a | addr->sin_family = AF_INET; |
---|
1647 | n/a | addr->sin_port = htons((short)port); |
---|
1648 | n/a | *len_ret = sizeof *addr; |
---|
1649 | n/a | return 1; |
---|
1650 | n/a | } |
---|
1651 | n/a | |
---|
1652 | n/a | #ifdef ENABLE_IPV6 |
---|
1653 | n/a | case AF_INET6: |
---|
1654 | n/a | { |
---|
1655 | n/a | struct sockaddr_in6* addr; |
---|
1656 | n/a | struct maybe_idna host = {NULL, NULL}; |
---|
1657 | n/a | int port, result; |
---|
1658 | n/a | unsigned int flowinfo, scope_id; |
---|
1659 | n/a | flowinfo = scope_id = 0; |
---|
1660 | n/a | if (!PyTuple_Check(args)) { |
---|
1661 | n/a | PyErr_Format( |
---|
1662 | n/a | PyExc_TypeError, |
---|
1663 | n/a | "getsockaddrarg: " |
---|
1664 | n/a | "AF_INET6 address must be tuple, not %.500s", |
---|
1665 | n/a | Py_TYPE(args)->tp_name); |
---|
1666 | n/a | return 0; |
---|
1667 | n/a | } |
---|
1668 | n/a | if (!PyArg_ParseTuple(args, "O&i|II", |
---|
1669 | n/a | idna_converter, &host, &port, &flowinfo, |
---|
1670 | n/a | &scope_id)) { |
---|
1671 | n/a | return 0; |
---|
1672 | n/a | } |
---|
1673 | n/a | addr = (struct sockaddr_in6*)addr_ret; |
---|
1674 | n/a | result = setipaddr(host.buf, (struct sockaddr *)addr, |
---|
1675 | n/a | sizeof(*addr), AF_INET6); |
---|
1676 | n/a | idna_cleanup(&host); |
---|
1677 | n/a | if (result < 0) |
---|
1678 | n/a | return 0; |
---|
1679 | n/a | if (port < 0 || port > 0xffff) { |
---|
1680 | n/a | PyErr_SetString( |
---|
1681 | n/a | PyExc_OverflowError, |
---|
1682 | n/a | "getsockaddrarg: port must be 0-65535."); |
---|
1683 | n/a | return 0; |
---|
1684 | n/a | } |
---|
1685 | n/a | if (flowinfo > 0xfffff) { |
---|
1686 | n/a | PyErr_SetString( |
---|
1687 | n/a | PyExc_OverflowError, |
---|
1688 | n/a | "getsockaddrarg: flowinfo must be 0-1048575."); |
---|
1689 | n/a | return 0; |
---|
1690 | n/a | } |
---|
1691 | n/a | addr->sin6_family = s->sock_family; |
---|
1692 | n/a | addr->sin6_port = htons((short)port); |
---|
1693 | n/a | addr->sin6_flowinfo = htonl(flowinfo); |
---|
1694 | n/a | addr->sin6_scope_id = scope_id; |
---|
1695 | n/a | *len_ret = sizeof *addr; |
---|
1696 | n/a | return 1; |
---|
1697 | n/a | } |
---|
1698 | n/a | #endif |
---|
1699 | n/a | |
---|
1700 | n/a | #ifdef USE_BLUETOOTH |
---|
1701 | n/a | case AF_BLUETOOTH: |
---|
1702 | n/a | { |
---|
1703 | n/a | switch (s->sock_proto) { |
---|
1704 | n/a | case BTPROTO_L2CAP: |
---|
1705 | n/a | { |
---|
1706 | n/a | struct sockaddr_l2 *addr; |
---|
1707 | n/a | char *straddr; |
---|
1708 | n/a | |
---|
1709 | n/a | addr = (struct sockaddr_l2 *)addr_ret; |
---|
1710 | n/a | memset(addr, 0, sizeof(struct sockaddr_l2)); |
---|
1711 | n/a | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH; |
---|
1712 | n/a | if (!PyArg_ParseTuple(args, "si", &straddr, |
---|
1713 | n/a | &_BT_L2_MEMB(addr, psm))) { |
---|
1714 | n/a | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
---|
1715 | n/a | "wrong format"); |
---|
1716 | n/a | return 0; |
---|
1717 | n/a | } |
---|
1718 | n/a | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) |
---|
1719 | n/a | return 0; |
---|
1720 | n/a | |
---|
1721 | n/a | *len_ret = sizeof *addr; |
---|
1722 | n/a | return 1; |
---|
1723 | n/a | } |
---|
1724 | n/a | case BTPROTO_RFCOMM: |
---|
1725 | n/a | { |
---|
1726 | n/a | struct sockaddr_rc *addr; |
---|
1727 | n/a | char *straddr; |
---|
1728 | n/a | |
---|
1729 | n/a | addr = (struct sockaddr_rc *)addr_ret; |
---|
1730 | n/a | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; |
---|
1731 | n/a | if (!PyArg_ParseTuple(args, "si", &straddr, |
---|
1732 | n/a | &_BT_RC_MEMB(addr, channel))) { |
---|
1733 | n/a | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
---|
1734 | n/a | "wrong format"); |
---|
1735 | n/a | return 0; |
---|
1736 | n/a | } |
---|
1737 | n/a | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) |
---|
1738 | n/a | return 0; |
---|
1739 | n/a | |
---|
1740 | n/a | *len_ret = sizeof *addr; |
---|
1741 | n/a | return 1; |
---|
1742 | n/a | } |
---|
1743 | n/a | case BTPROTO_HCI: |
---|
1744 | n/a | { |
---|
1745 | n/a | struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; |
---|
1746 | n/a | #if defined(__NetBSD__) || defined(__DragonFly__) |
---|
1747 | n/a | char *straddr = PyBytes_AS_STRING(args); |
---|
1748 | n/a | |
---|
1749 | n/a | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
---|
1750 | n/a | if (straddr == NULL) { |
---|
1751 | n/a | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
---|
1752 | n/a | "wrong format"); |
---|
1753 | n/a | return 0; |
---|
1754 | n/a | } |
---|
1755 | n/a | if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0) |
---|
1756 | n/a | return 0; |
---|
1757 | n/a | #else |
---|
1758 | n/a | _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; |
---|
1759 | n/a | if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { |
---|
1760 | n/a | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
---|
1761 | n/a | "wrong format"); |
---|
1762 | n/a | return 0; |
---|
1763 | n/a | } |
---|
1764 | n/a | #endif |
---|
1765 | n/a | *len_ret = sizeof *addr; |
---|
1766 | n/a | return 1; |
---|
1767 | n/a | } |
---|
1768 | n/a | #if !defined(__FreeBSD__) |
---|
1769 | n/a | case BTPROTO_SCO: |
---|
1770 | n/a | { |
---|
1771 | n/a | struct sockaddr_sco *addr; |
---|
1772 | n/a | char *straddr; |
---|
1773 | n/a | |
---|
1774 | n/a | addr = (struct sockaddr_sco *)addr_ret; |
---|
1775 | n/a | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; |
---|
1776 | n/a | if (!PyBytes_Check(args)) { |
---|
1777 | n/a | PyErr_SetString(PyExc_OSError, "getsockaddrarg: " |
---|
1778 | n/a | "wrong format"); |
---|
1779 | n/a | return 0; |
---|
1780 | n/a | } |
---|
1781 | n/a | straddr = PyBytes_AS_STRING(args); |
---|
1782 | n/a | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0) |
---|
1783 | n/a | return 0; |
---|
1784 | n/a | |
---|
1785 | n/a | *len_ret = sizeof *addr; |
---|
1786 | n/a | return 1; |
---|
1787 | n/a | } |
---|
1788 | n/a | #endif |
---|
1789 | n/a | default: |
---|
1790 | n/a | PyErr_SetString(PyExc_OSError, "getsockaddrarg: unknown Bluetooth protocol"); |
---|
1791 | n/a | return 0; |
---|
1792 | n/a | } |
---|
1793 | n/a | } |
---|
1794 | n/a | #endif |
---|
1795 | n/a | |
---|
1796 | n/a | #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX) |
---|
1797 | n/a | case AF_PACKET: |
---|
1798 | n/a | { |
---|
1799 | n/a | struct sockaddr_ll* addr; |
---|
1800 | n/a | struct ifreq ifr; |
---|
1801 | n/a | char *interfaceName; |
---|
1802 | n/a | int protoNumber; |
---|
1803 | n/a | int hatype = 0; |
---|
1804 | n/a | int pkttype = 0; |
---|
1805 | n/a | Py_buffer haddr = {NULL, NULL}; |
---|
1806 | n/a | |
---|
1807 | n/a | if (!PyTuple_Check(args)) { |
---|
1808 | n/a | PyErr_Format( |
---|
1809 | n/a | PyExc_TypeError, |
---|
1810 | n/a | "getsockaddrarg: " |
---|
1811 | n/a | "AF_PACKET address must be tuple, not %.500s", |
---|
1812 | n/a | Py_TYPE(args)->tp_name); |
---|
1813 | n/a | return 0; |
---|
1814 | n/a | } |
---|
1815 | n/a | if (!PyArg_ParseTuple(args, "si|iiy*", &interfaceName, |
---|
1816 | n/a | &protoNumber, &pkttype, &hatype, |
---|
1817 | n/a | &haddr)) |
---|
1818 | n/a | return 0; |
---|
1819 | n/a | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name)); |
---|
1820 | n/a | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
---|
1821 | n/a | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
---|
1822 | n/a | s->errorhandler(); |
---|
1823 | n/a | PyBuffer_Release(&haddr); |
---|
1824 | n/a | return 0; |
---|
1825 | n/a | } |
---|
1826 | n/a | if (haddr.buf && haddr.len > 8) { |
---|
1827 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
1828 | n/a | "Hardware address must be 8 bytes or less"); |
---|
1829 | n/a | PyBuffer_Release(&haddr); |
---|
1830 | n/a | return 0; |
---|
1831 | n/a | } |
---|
1832 | n/a | if (protoNumber < 0 || protoNumber > 0xffff) { |
---|
1833 | n/a | PyErr_SetString( |
---|
1834 | n/a | PyExc_OverflowError, |
---|
1835 | n/a | "getsockaddrarg: protoNumber must be 0-65535."); |
---|
1836 | n/a | PyBuffer_Release(&haddr); |
---|
1837 | n/a | return 0; |
---|
1838 | n/a | } |
---|
1839 | n/a | addr = (struct sockaddr_ll*)addr_ret; |
---|
1840 | n/a | addr->sll_family = AF_PACKET; |
---|
1841 | n/a | addr->sll_protocol = htons((short)protoNumber); |
---|
1842 | n/a | addr->sll_ifindex = ifr.ifr_ifindex; |
---|
1843 | n/a | addr->sll_pkttype = pkttype; |
---|
1844 | n/a | addr->sll_hatype = hatype; |
---|
1845 | n/a | if (haddr.buf) { |
---|
1846 | n/a | memcpy(&addr->sll_addr, haddr.buf, haddr.len); |
---|
1847 | n/a | addr->sll_halen = haddr.len; |
---|
1848 | n/a | } |
---|
1849 | n/a | else |
---|
1850 | n/a | addr->sll_halen = 0; |
---|
1851 | n/a | *len_ret = sizeof *addr; |
---|
1852 | n/a | PyBuffer_Release(&haddr); |
---|
1853 | n/a | return 1; |
---|
1854 | n/a | } |
---|
1855 | n/a | #endif |
---|
1856 | n/a | |
---|
1857 | n/a | #ifdef HAVE_LINUX_TIPC_H |
---|
1858 | n/a | case AF_TIPC: |
---|
1859 | n/a | { |
---|
1860 | n/a | unsigned int atype, v1, v2, v3; |
---|
1861 | n/a | unsigned int scope = TIPC_CLUSTER_SCOPE; |
---|
1862 | n/a | struct sockaddr_tipc *addr; |
---|
1863 | n/a | |
---|
1864 | n/a | if (!PyTuple_Check(args)) { |
---|
1865 | n/a | PyErr_Format( |
---|
1866 | n/a | PyExc_TypeError, |
---|
1867 | n/a | "getsockaddrarg: " |
---|
1868 | n/a | "AF_TIPC address must be tuple, not %.500s", |
---|
1869 | n/a | Py_TYPE(args)->tp_name); |
---|
1870 | n/a | return 0; |
---|
1871 | n/a | } |
---|
1872 | n/a | |
---|
1873 | n/a | if (!PyArg_ParseTuple(args, |
---|
1874 | n/a | "IIII|I;Invalid TIPC address format", |
---|
1875 | n/a | &atype, &v1, &v2, &v3, &scope)) |
---|
1876 | n/a | return 0; |
---|
1877 | n/a | |
---|
1878 | n/a | addr = (struct sockaddr_tipc *) addr_ret; |
---|
1879 | n/a | memset(addr, 0, sizeof(struct sockaddr_tipc)); |
---|
1880 | n/a | |
---|
1881 | n/a | addr->family = AF_TIPC; |
---|
1882 | n/a | addr->scope = scope; |
---|
1883 | n/a | addr->addrtype = atype; |
---|
1884 | n/a | |
---|
1885 | n/a | if (atype == TIPC_ADDR_NAMESEQ) { |
---|
1886 | n/a | addr->addr.nameseq.type = v1; |
---|
1887 | n/a | addr->addr.nameseq.lower = v2; |
---|
1888 | n/a | addr->addr.nameseq.upper = v3; |
---|
1889 | n/a | } else if (atype == TIPC_ADDR_NAME) { |
---|
1890 | n/a | addr->addr.name.name.type = v1; |
---|
1891 | n/a | addr->addr.name.name.instance = v2; |
---|
1892 | n/a | } else if (atype == TIPC_ADDR_ID) { |
---|
1893 | n/a | addr->addr.id.node = v1; |
---|
1894 | n/a | addr->addr.id.ref = v2; |
---|
1895 | n/a | } else { |
---|
1896 | n/a | /* Shouldn't happen */ |
---|
1897 | n/a | PyErr_SetString(PyExc_TypeError, "Invalid address type"); |
---|
1898 | n/a | return 0; |
---|
1899 | n/a | } |
---|
1900 | n/a | |
---|
1901 | n/a | *len_ret = sizeof(*addr); |
---|
1902 | n/a | |
---|
1903 | n/a | return 1; |
---|
1904 | n/a | } |
---|
1905 | n/a | #endif |
---|
1906 | n/a | |
---|
1907 | n/a | #if defined(AF_CAN) && defined(CAN_RAW) && defined(CAN_BCM) |
---|
1908 | n/a | case AF_CAN: |
---|
1909 | n/a | switch (s->sock_proto) { |
---|
1910 | n/a | case CAN_RAW: |
---|
1911 | n/a | /* fall-through */ |
---|
1912 | n/a | case CAN_BCM: |
---|
1913 | n/a | { |
---|
1914 | n/a | struct sockaddr_can *addr; |
---|
1915 | n/a | PyObject *interfaceName; |
---|
1916 | n/a | struct ifreq ifr; |
---|
1917 | n/a | Py_ssize_t len; |
---|
1918 | n/a | |
---|
1919 | n/a | addr = (struct sockaddr_can *)addr_ret; |
---|
1920 | n/a | |
---|
1921 | n/a | if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter, |
---|
1922 | n/a | &interfaceName)) |
---|
1923 | n/a | return 0; |
---|
1924 | n/a | |
---|
1925 | n/a | len = PyBytes_GET_SIZE(interfaceName); |
---|
1926 | n/a | |
---|
1927 | n/a | if (len == 0) { |
---|
1928 | n/a | ifr.ifr_ifindex = 0; |
---|
1929 | n/a | } else if ((size_t)len < sizeof(ifr.ifr_name)) { |
---|
1930 | n/a | strncpy(ifr.ifr_name, PyBytes_AS_STRING(interfaceName), sizeof(ifr.ifr_name)); |
---|
1931 | n/a | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0'; |
---|
1932 | n/a | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) { |
---|
1933 | n/a | s->errorhandler(); |
---|
1934 | n/a | Py_DECREF(interfaceName); |
---|
1935 | n/a | return 0; |
---|
1936 | n/a | } |
---|
1937 | n/a | } else { |
---|
1938 | n/a | PyErr_SetString(PyExc_OSError, |
---|
1939 | n/a | "AF_CAN interface name too long"); |
---|
1940 | n/a | Py_DECREF(interfaceName); |
---|
1941 | n/a | return 0; |
---|
1942 | n/a | } |
---|
1943 | n/a | |
---|
1944 | n/a | addr->can_family = AF_CAN; |
---|
1945 | n/a | addr->can_ifindex = ifr.ifr_ifindex; |
---|
1946 | n/a | |
---|
1947 | n/a | *len_ret = sizeof(*addr); |
---|
1948 | n/a | Py_DECREF(interfaceName); |
---|
1949 | n/a | return 1; |
---|
1950 | n/a | } |
---|
1951 | n/a | default: |
---|
1952 | n/a | PyErr_SetString(PyExc_OSError, |
---|
1953 | n/a | "getsockaddrarg: unsupported CAN protocol"); |
---|
1954 | n/a | return 0; |
---|
1955 | n/a | } |
---|
1956 | n/a | #endif |
---|
1957 | n/a | |
---|
1958 | n/a | #ifdef PF_SYSTEM |
---|
1959 | n/a | case PF_SYSTEM: |
---|
1960 | n/a | switch (s->sock_proto) { |
---|
1961 | n/a | #ifdef SYSPROTO_CONTROL |
---|
1962 | n/a | case SYSPROTO_CONTROL: |
---|
1963 | n/a | { |
---|
1964 | n/a | struct sockaddr_ctl *addr; |
---|
1965 | n/a | |
---|
1966 | n/a | addr = (struct sockaddr_ctl *)addr_ret; |
---|
1967 | n/a | addr->sc_family = AF_SYSTEM; |
---|
1968 | n/a | addr->ss_sysaddr = AF_SYS_CONTROL; |
---|
1969 | n/a | |
---|
1970 | n/a | if (PyUnicode_Check(args)) { |
---|
1971 | n/a | struct ctl_info info; |
---|
1972 | n/a | PyObject *ctl_name; |
---|
1973 | n/a | |
---|
1974 | n/a | if (!PyArg_Parse(args, "O&", |
---|
1975 | n/a | PyUnicode_FSConverter, &ctl_name)) { |
---|
1976 | n/a | return 0; |
---|
1977 | n/a | } |
---|
1978 | n/a | |
---|
1979 | n/a | if (PyBytes_GET_SIZE(ctl_name) > (Py_ssize_t)sizeof(info.ctl_name)) { |
---|
1980 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
1981 | n/a | "provided string is too long"); |
---|
1982 | n/a | Py_DECREF(ctl_name); |
---|
1983 | n/a | return 0; |
---|
1984 | n/a | } |
---|
1985 | n/a | strncpy(info.ctl_name, PyBytes_AS_STRING(ctl_name), |
---|
1986 | n/a | sizeof(info.ctl_name)); |
---|
1987 | n/a | Py_DECREF(ctl_name); |
---|
1988 | n/a | |
---|
1989 | n/a | if (ioctl(s->sock_fd, CTLIOCGINFO, &info)) { |
---|
1990 | n/a | PyErr_SetString(PyExc_OSError, |
---|
1991 | n/a | "cannot find kernel control with provided name"); |
---|
1992 | n/a | return 0; |
---|
1993 | n/a | } |
---|
1994 | n/a | |
---|
1995 | n/a | addr->sc_id = info.ctl_id; |
---|
1996 | n/a | addr->sc_unit = 0; |
---|
1997 | n/a | } else if (!PyArg_ParseTuple(args, "II", |
---|
1998 | n/a | &(addr->sc_id), &(addr->sc_unit))) { |
---|
1999 | n/a | PyErr_SetString(PyExc_TypeError, "getsockaddrarg: " |
---|
2000 | n/a | "expected str or tuple of two ints"); |
---|
2001 | n/a | |
---|
2002 | n/a | return 0; |
---|
2003 | n/a | } |
---|
2004 | n/a | |
---|
2005 | n/a | *len_ret = sizeof(*addr); |
---|
2006 | n/a | return 1; |
---|
2007 | n/a | } |
---|
2008 | n/a | #endif |
---|
2009 | n/a | default: |
---|
2010 | n/a | PyErr_SetString(PyExc_OSError, |
---|
2011 | n/a | "getsockaddrarg: unsupported PF_SYSTEM protocol"); |
---|
2012 | n/a | return 0; |
---|
2013 | n/a | } |
---|
2014 | n/a | #endif |
---|
2015 | n/a | #ifdef HAVE_SOCKADDR_ALG |
---|
2016 | n/a | case AF_ALG: |
---|
2017 | n/a | { |
---|
2018 | n/a | struct sockaddr_alg *sa; |
---|
2019 | n/a | char *type; |
---|
2020 | n/a | char *name; |
---|
2021 | n/a | sa = (struct sockaddr_alg *)addr_ret; |
---|
2022 | n/a | |
---|
2023 | n/a | memset(sa, 0, sizeof(*sa)); |
---|
2024 | n/a | sa->salg_family = AF_ALG; |
---|
2025 | n/a | |
---|
2026 | n/a | if (!PyArg_ParseTuple(args, "ss|HH:getsockaddrarg", |
---|
2027 | n/a | &type, &name, &sa->salg_feat, &sa->salg_mask)) |
---|
2028 | n/a | return 0; |
---|
2029 | n/a | /* sockaddr_alg has fixed-sized char arrays for type and name */ |
---|
2030 | n/a | if (strlen(type) > sizeof(sa->salg_type)) { |
---|
2031 | n/a | PyErr_SetString(PyExc_ValueError, "AF_ALG type too long."); |
---|
2032 | n/a | return 0; |
---|
2033 | n/a | } |
---|
2034 | n/a | strncpy((char *)sa->salg_type, type, sizeof(sa->salg_type)); |
---|
2035 | n/a | if (strlen(name) > sizeof(sa->salg_name)) { |
---|
2036 | n/a | PyErr_SetString(PyExc_ValueError, "AF_ALG name too long."); |
---|
2037 | n/a | return 0; |
---|
2038 | n/a | } |
---|
2039 | n/a | strncpy((char *)sa->salg_name, name, sizeof(sa->salg_name)); |
---|
2040 | n/a | |
---|
2041 | n/a | *len_ret = sizeof(*sa); |
---|
2042 | n/a | return 1; |
---|
2043 | n/a | } |
---|
2044 | n/a | #endif |
---|
2045 | n/a | |
---|
2046 | n/a | /* More cases here... */ |
---|
2047 | n/a | |
---|
2048 | n/a | default: |
---|
2049 | n/a | PyErr_SetString(PyExc_OSError, "getsockaddrarg: bad family"); |
---|
2050 | n/a | return 0; |
---|
2051 | n/a | |
---|
2052 | n/a | } |
---|
2053 | n/a | } |
---|
2054 | n/a | |
---|
2055 | n/a | |
---|
2056 | n/a | /* Get the address length according to the socket object's address family. |
---|
2057 | n/a | Return 1 if the family is known, 0 otherwise. The length is returned |
---|
2058 | n/a | through len_ret. */ |
---|
2059 | n/a | |
---|
2060 | n/a | static int |
---|
2061 | n/a | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) |
---|
2062 | n/a | { |
---|
2063 | n/a | switch (s->sock_family) { |
---|
2064 | n/a | |
---|
2065 | n/a | #if defined(AF_UNIX) |
---|
2066 | n/a | case AF_UNIX: |
---|
2067 | n/a | { |
---|
2068 | n/a | *len_ret = sizeof (struct sockaddr_un); |
---|
2069 | n/a | return 1; |
---|
2070 | n/a | } |
---|
2071 | n/a | #endif /* AF_UNIX */ |
---|
2072 | n/a | |
---|
2073 | n/a | #if defined(AF_NETLINK) |
---|
2074 | n/a | case AF_NETLINK: |
---|
2075 | n/a | { |
---|
2076 | n/a | *len_ret = sizeof (struct sockaddr_nl); |
---|
2077 | n/a | return 1; |
---|
2078 | n/a | } |
---|
2079 | n/a | #endif |
---|
2080 | n/a | |
---|
2081 | n/a | #ifdef AF_RDS |
---|
2082 | n/a | case AF_RDS: |
---|
2083 | n/a | /* RDS sockets use sockaddr_in: fall-through */ |
---|
2084 | n/a | #endif |
---|
2085 | n/a | |
---|
2086 | n/a | case AF_INET: |
---|
2087 | n/a | { |
---|
2088 | n/a | *len_ret = sizeof (struct sockaddr_in); |
---|
2089 | n/a | return 1; |
---|
2090 | n/a | } |
---|
2091 | n/a | |
---|
2092 | n/a | #ifdef ENABLE_IPV6 |
---|
2093 | n/a | case AF_INET6: |
---|
2094 | n/a | { |
---|
2095 | n/a | *len_ret = sizeof (struct sockaddr_in6); |
---|
2096 | n/a | return 1; |
---|
2097 | n/a | } |
---|
2098 | n/a | #endif |
---|
2099 | n/a | |
---|
2100 | n/a | #ifdef USE_BLUETOOTH |
---|
2101 | n/a | case AF_BLUETOOTH: |
---|
2102 | n/a | { |
---|
2103 | n/a | switch(s->sock_proto) |
---|
2104 | n/a | { |
---|
2105 | n/a | |
---|
2106 | n/a | case BTPROTO_L2CAP: |
---|
2107 | n/a | *len_ret = sizeof (struct sockaddr_l2); |
---|
2108 | n/a | return 1; |
---|
2109 | n/a | case BTPROTO_RFCOMM: |
---|
2110 | n/a | *len_ret = sizeof (struct sockaddr_rc); |
---|
2111 | n/a | return 1; |
---|
2112 | n/a | case BTPROTO_HCI: |
---|
2113 | n/a | *len_ret = sizeof (struct sockaddr_hci); |
---|
2114 | n/a | return 1; |
---|
2115 | n/a | #if !defined(__FreeBSD__) |
---|
2116 | n/a | case BTPROTO_SCO: |
---|
2117 | n/a | *len_ret = sizeof (struct sockaddr_sco); |
---|
2118 | n/a | return 1; |
---|
2119 | n/a | #endif |
---|
2120 | n/a | default: |
---|
2121 | n/a | PyErr_SetString(PyExc_OSError, "getsockaddrlen: " |
---|
2122 | n/a | "unknown BT protocol"); |
---|
2123 | n/a | return 0; |
---|
2124 | n/a | |
---|
2125 | n/a | } |
---|
2126 | n/a | } |
---|
2127 | n/a | #endif |
---|
2128 | n/a | |
---|
2129 | n/a | #ifdef HAVE_NETPACKET_PACKET_H |
---|
2130 | n/a | case AF_PACKET: |
---|
2131 | n/a | { |
---|
2132 | n/a | *len_ret = sizeof (struct sockaddr_ll); |
---|
2133 | n/a | return 1; |
---|
2134 | n/a | } |
---|
2135 | n/a | #endif |
---|
2136 | n/a | |
---|
2137 | n/a | #ifdef HAVE_LINUX_TIPC_H |
---|
2138 | n/a | case AF_TIPC: |
---|
2139 | n/a | { |
---|
2140 | n/a | *len_ret = sizeof (struct sockaddr_tipc); |
---|
2141 | n/a | return 1; |
---|
2142 | n/a | } |
---|
2143 | n/a | #endif |
---|
2144 | n/a | |
---|
2145 | n/a | #ifdef AF_CAN |
---|
2146 | n/a | case AF_CAN: |
---|
2147 | n/a | { |
---|
2148 | n/a | *len_ret = sizeof (struct sockaddr_can); |
---|
2149 | n/a | return 1; |
---|
2150 | n/a | } |
---|
2151 | n/a | #endif |
---|
2152 | n/a | |
---|
2153 | n/a | #ifdef PF_SYSTEM |
---|
2154 | n/a | case PF_SYSTEM: |
---|
2155 | n/a | switch(s->sock_proto) { |
---|
2156 | n/a | #ifdef SYSPROTO_CONTROL |
---|
2157 | n/a | case SYSPROTO_CONTROL: |
---|
2158 | n/a | *len_ret = sizeof (struct sockaddr_ctl); |
---|
2159 | n/a | return 1; |
---|
2160 | n/a | #endif |
---|
2161 | n/a | default: |
---|
2162 | n/a | PyErr_SetString(PyExc_OSError, "getsockaddrlen: " |
---|
2163 | n/a | "unknown PF_SYSTEM protocol"); |
---|
2164 | n/a | return 0; |
---|
2165 | n/a | } |
---|
2166 | n/a | #endif |
---|
2167 | n/a | #ifdef HAVE_SOCKADDR_ALG |
---|
2168 | n/a | case AF_ALG: |
---|
2169 | n/a | { |
---|
2170 | n/a | *len_ret = sizeof (struct sockaddr_alg); |
---|
2171 | n/a | return 1; |
---|
2172 | n/a | } |
---|
2173 | n/a | #endif |
---|
2174 | n/a | |
---|
2175 | n/a | /* More cases here... */ |
---|
2176 | n/a | |
---|
2177 | n/a | default: |
---|
2178 | n/a | PyErr_SetString(PyExc_OSError, "getsockaddrlen: bad family"); |
---|
2179 | n/a | return 0; |
---|
2180 | n/a | |
---|
2181 | n/a | } |
---|
2182 | n/a | } |
---|
2183 | n/a | |
---|
2184 | n/a | |
---|
2185 | n/a | /* Support functions for the sendmsg() and recvmsg[_into]() methods. |
---|
2186 | n/a | Currently, these methods are only compiled if the RFC 2292/3542 |
---|
2187 | n/a | CMSG_LEN() macro is available. Older systems seem to have used |
---|
2188 | n/a | sizeof(struct cmsghdr) + (length) where CMSG_LEN() is used now, so |
---|
2189 | n/a | it may be possible to define CMSG_LEN() that way if it's not |
---|
2190 | n/a | provided. Some architectures might need extra padding after the |
---|
2191 | n/a | cmsghdr, however, and CMSG_LEN() would have to take account of |
---|
2192 | n/a | this. */ |
---|
2193 | n/a | #ifdef CMSG_LEN |
---|
2194 | n/a | /* If length is in range, set *result to CMSG_LEN(length) and return |
---|
2195 | n/a | true; otherwise, return false. */ |
---|
2196 | n/a | static int |
---|
2197 | n/a | get_CMSG_LEN(size_t length, size_t *result) |
---|
2198 | n/a | { |
---|
2199 | n/a | size_t tmp; |
---|
2200 | n/a | |
---|
2201 | n/a | if (length > (SOCKLEN_T_LIMIT - CMSG_LEN(0))) |
---|
2202 | n/a | return 0; |
---|
2203 | n/a | tmp = CMSG_LEN(length); |
---|
2204 | n/a | if (tmp > SOCKLEN_T_LIMIT || tmp < length) |
---|
2205 | n/a | return 0; |
---|
2206 | n/a | *result = tmp; |
---|
2207 | n/a | return 1; |
---|
2208 | n/a | } |
---|
2209 | n/a | |
---|
2210 | n/a | #ifdef CMSG_SPACE |
---|
2211 | n/a | /* If length is in range, set *result to CMSG_SPACE(length) and return |
---|
2212 | n/a | true; otherwise, return false. */ |
---|
2213 | n/a | static int |
---|
2214 | n/a | get_CMSG_SPACE(size_t length, size_t *result) |
---|
2215 | n/a | { |
---|
2216 | n/a | size_t tmp; |
---|
2217 | n/a | |
---|
2218 | n/a | /* Use CMSG_SPACE(1) here in order to take account of the padding |
---|
2219 | n/a | necessary before *and* after the data. */ |
---|
2220 | n/a | if (length > (SOCKLEN_T_LIMIT - CMSG_SPACE(1))) |
---|
2221 | n/a | return 0; |
---|
2222 | n/a | tmp = CMSG_SPACE(length); |
---|
2223 | n/a | if (tmp > SOCKLEN_T_LIMIT || tmp < length) |
---|
2224 | n/a | return 0; |
---|
2225 | n/a | *result = tmp; |
---|
2226 | n/a | return 1; |
---|
2227 | n/a | } |
---|
2228 | n/a | #endif |
---|
2229 | n/a | |
---|
2230 | n/a | /* Return true iff msg->msg_controllen is valid, cmsgh is a valid |
---|
2231 | n/a | pointer in msg->msg_control with at least "space" bytes after it, |
---|
2232 | n/a | and its cmsg_len member inside the buffer. */ |
---|
2233 | n/a | static int |
---|
2234 | n/a | cmsg_min_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t space) |
---|
2235 | n/a | { |
---|
2236 | n/a | size_t cmsg_offset; |
---|
2237 | n/a | static const size_t cmsg_len_end = (offsetof(struct cmsghdr, cmsg_len) + |
---|
2238 | n/a | sizeof(cmsgh->cmsg_len)); |
---|
2239 | n/a | |
---|
2240 | n/a | /* Note that POSIX allows msg_controllen to be of signed type. */ |
---|
2241 | n/a | if (cmsgh == NULL || msg->msg_control == NULL) |
---|
2242 | n/a | return 0; |
---|
2243 | n/a | /* Note that POSIX allows msg_controllen to be of a signed type. This is |
---|
2244 | n/a | annoying under OS X as it's unsigned there and so it triggers a |
---|
2245 | n/a | tautological comparison warning under Clang when compared against 0. |
---|
2246 | n/a | Since the check is valid on other platforms, silence the warning under |
---|
2247 | n/a | Clang. */ |
---|
2248 | n/a | #ifdef __clang__ |
---|
2249 | n/a | #pragma clang diagnostic push |
---|
2250 | n/a | #pragma clang diagnostic ignored "-Wtautological-compare" |
---|
2251 | n/a | #endif |
---|
2252 | n/a | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
---|
2253 | n/a | #pragma GCC diagnostic push |
---|
2254 | n/a | #pragma GCC diagnostic ignored "-Wtype-limits" |
---|
2255 | n/a | #endif |
---|
2256 | n/a | if (msg->msg_controllen < 0) |
---|
2257 | n/a | return 0; |
---|
2258 | n/a | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
---|
2259 | n/a | #pragma GCC diagnostic pop |
---|
2260 | n/a | #endif |
---|
2261 | n/a | #ifdef __clang__ |
---|
2262 | n/a | #pragma clang diagnostic pop |
---|
2263 | n/a | #endif |
---|
2264 | n/a | if (space < cmsg_len_end) |
---|
2265 | n/a | space = cmsg_len_end; |
---|
2266 | n/a | cmsg_offset = (char *)cmsgh - (char *)msg->msg_control; |
---|
2267 | n/a | return (cmsg_offset <= (size_t)-1 - space && |
---|
2268 | n/a | cmsg_offset + space <= msg->msg_controllen); |
---|
2269 | n/a | } |
---|
2270 | n/a | |
---|
2271 | n/a | /* If pointer CMSG_DATA(cmsgh) is in buffer msg->msg_control, set |
---|
2272 | n/a | *space to number of bytes following it in the buffer and return |
---|
2273 | n/a | true; otherwise, return false. Assumes cmsgh, msg->msg_control and |
---|
2274 | n/a | msg->msg_controllen are valid. */ |
---|
2275 | n/a | static int |
---|
2276 | n/a | get_cmsg_data_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *space) |
---|
2277 | n/a | { |
---|
2278 | n/a | size_t data_offset; |
---|
2279 | n/a | char *data_ptr; |
---|
2280 | n/a | |
---|
2281 | n/a | if ((data_ptr = (char *)CMSG_DATA(cmsgh)) == NULL) |
---|
2282 | n/a | return 0; |
---|
2283 | n/a | data_offset = data_ptr - (char *)msg->msg_control; |
---|
2284 | n/a | if (data_offset > msg->msg_controllen) |
---|
2285 | n/a | return 0; |
---|
2286 | n/a | *space = msg->msg_controllen - data_offset; |
---|
2287 | n/a | return 1; |
---|
2288 | n/a | } |
---|
2289 | n/a | |
---|
2290 | n/a | /* If cmsgh is invalid or not contained in the buffer pointed to by |
---|
2291 | n/a | msg->msg_control, return -1. If cmsgh is valid and its associated |
---|
2292 | n/a | data is entirely contained in the buffer, set *data_len to the |
---|
2293 | n/a | length of the associated data and return 0. If only part of the |
---|
2294 | n/a | associated data is contained in the buffer but cmsgh is otherwise |
---|
2295 | n/a | valid, set *data_len to the length contained in the buffer and |
---|
2296 | n/a | return 1. */ |
---|
2297 | n/a | static int |
---|
2298 | n/a | get_cmsg_data_len(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *data_len) |
---|
2299 | n/a | { |
---|
2300 | n/a | size_t space, cmsg_data_len; |
---|
2301 | n/a | |
---|
2302 | n/a | if (!cmsg_min_space(msg, cmsgh, CMSG_LEN(0)) || |
---|
2303 | n/a | cmsgh->cmsg_len < CMSG_LEN(0)) |
---|
2304 | n/a | return -1; |
---|
2305 | n/a | cmsg_data_len = cmsgh->cmsg_len - CMSG_LEN(0); |
---|
2306 | n/a | if (!get_cmsg_data_space(msg, cmsgh, &space)) |
---|
2307 | n/a | return -1; |
---|
2308 | n/a | if (space >= cmsg_data_len) { |
---|
2309 | n/a | *data_len = cmsg_data_len; |
---|
2310 | n/a | return 0; |
---|
2311 | n/a | } |
---|
2312 | n/a | *data_len = space; |
---|
2313 | n/a | return 1; |
---|
2314 | n/a | } |
---|
2315 | n/a | #endif /* CMSG_LEN */ |
---|
2316 | n/a | |
---|
2317 | n/a | |
---|
2318 | n/a | struct sock_accept { |
---|
2319 | n/a | socklen_t *addrlen; |
---|
2320 | n/a | sock_addr_t *addrbuf; |
---|
2321 | n/a | SOCKET_T result; |
---|
2322 | n/a | }; |
---|
2323 | n/a | |
---|
2324 | n/a | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
---|
2325 | n/a | /* accept4() is available on Linux 2.6.28+ and glibc 2.10 */ |
---|
2326 | n/a | static int accept4_works = -1; |
---|
2327 | n/a | #endif |
---|
2328 | n/a | |
---|
2329 | n/a | static int |
---|
2330 | n/a | sock_accept_impl(PySocketSockObject *s, void *data) |
---|
2331 | n/a | { |
---|
2332 | n/a | struct sock_accept *ctx = data; |
---|
2333 | n/a | struct sockaddr *addr = SAS2SA(ctx->addrbuf); |
---|
2334 | n/a | socklen_t *paddrlen = ctx->addrlen; |
---|
2335 | n/a | #ifdef HAVE_SOCKADDR_ALG |
---|
2336 | n/a | /* AF_ALG does not support accept() with addr and raises |
---|
2337 | n/a | * ECONNABORTED instead. */ |
---|
2338 | n/a | if (s->sock_family == AF_ALG) { |
---|
2339 | n/a | addr = NULL; |
---|
2340 | n/a | paddrlen = NULL; |
---|
2341 | n/a | *ctx->addrlen = 0; |
---|
2342 | n/a | } |
---|
2343 | n/a | #endif |
---|
2344 | n/a | |
---|
2345 | n/a | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
---|
2346 | n/a | if (accept4_works != 0) { |
---|
2347 | n/a | ctx->result = accept4(s->sock_fd, addr, paddrlen, |
---|
2348 | n/a | SOCK_CLOEXEC); |
---|
2349 | n/a | if (ctx->result == INVALID_SOCKET && accept4_works == -1) { |
---|
2350 | n/a | /* On Linux older than 2.6.28, accept4() fails with ENOSYS */ |
---|
2351 | n/a | accept4_works = (errno != ENOSYS); |
---|
2352 | n/a | } |
---|
2353 | n/a | } |
---|
2354 | n/a | if (accept4_works == 0) |
---|
2355 | n/a | ctx->result = accept(s->sock_fd, addr, paddrlen); |
---|
2356 | n/a | #else |
---|
2357 | n/a | ctx->result = accept(s->sock_fd, addr, paddrlen); |
---|
2358 | n/a | #endif |
---|
2359 | n/a | |
---|
2360 | n/a | #ifdef MS_WINDOWS |
---|
2361 | n/a | return (ctx->result != INVALID_SOCKET); |
---|
2362 | n/a | #else |
---|
2363 | n/a | return (ctx->result >= 0); |
---|
2364 | n/a | #endif |
---|
2365 | n/a | } |
---|
2366 | n/a | |
---|
2367 | n/a | /* s._accept() -> (fd, address) */ |
---|
2368 | n/a | |
---|
2369 | n/a | static PyObject * |
---|
2370 | n/a | sock_accept(PySocketSockObject *s) |
---|
2371 | n/a | { |
---|
2372 | n/a | sock_addr_t addrbuf; |
---|
2373 | n/a | SOCKET_T newfd; |
---|
2374 | n/a | socklen_t addrlen; |
---|
2375 | n/a | PyObject *sock = NULL; |
---|
2376 | n/a | PyObject *addr = NULL; |
---|
2377 | n/a | PyObject *res = NULL; |
---|
2378 | n/a | struct sock_accept ctx; |
---|
2379 | n/a | |
---|
2380 | n/a | if (!getsockaddrlen(s, &addrlen)) |
---|
2381 | n/a | return NULL; |
---|
2382 | n/a | memset(&addrbuf, 0, addrlen); |
---|
2383 | n/a | |
---|
2384 | n/a | if (!IS_SELECTABLE(s)) |
---|
2385 | n/a | return select_error(); |
---|
2386 | n/a | |
---|
2387 | n/a | ctx.addrlen = &addrlen; |
---|
2388 | n/a | ctx.addrbuf = &addrbuf; |
---|
2389 | n/a | if (sock_call(s, 0, sock_accept_impl, &ctx) < 0) |
---|
2390 | n/a | return NULL; |
---|
2391 | n/a | newfd = ctx.result; |
---|
2392 | n/a | |
---|
2393 | n/a | #ifdef MS_WINDOWS |
---|
2394 | n/a | if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) { |
---|
2395 | n/a | PyErr_SetFromWindowsErr(0); |
---|
2396 | n/a | SOCKETCLOSE(newfd); |
---|
2397 | n/a | goto finally; |
---|
2398 | n/a | } |
---|
2399 | n/a | #else |
---|
2400 | n/a | |
---|
2401 | n/a | #if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) |
---|
2402 | n/a | if (!accept4_works) |
---|
2403 | n/a | #endif |
---|
2404 | n/a | { |
---|
2405 | n/a | if (_Py_set_inheritable(newfd, 0, NULL) < 0) { |
---|
2406 | n/a | SOCKETCLOSE(newfd); |
---|
2407 | n/a | goto finally; |
---|
2408 | n/a | } |
---|
2409 | n/a | } |
---|
2410 | n/a | #endif |
---|
2411 | n/a | |
---|
2412 | n/a | sock = PyLong_FromSocket_t(newfd); |
---|
2413 | n/a | if (sock == NULL) { |
---|
2414 | n/a | SOCKETCLOSE(newfd); |
---|
2415 | n/a | goto finally; |
---|
2416 | n/a | } |
---|
2417 | n/a | |
---|
2418 | n/a | addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
---|
2419 | n/a | addrlen, s->sock_proto); |
---|
2420 | n/a | if (addr == NULL) |
---|
2421 | n/a | goto finally; |
---|
2422 | n/a | |
---|
2423 | n/a | res = PyTuple_Pack(2, sock, addr); |
---|
2424 | n/a | |
---|
2425 | n/a | finally: |
---|
2426 | n/a | Py_XDECREF(sock); |
---|
2427 | n/a | Py_XDECREF(addr); |
---|
2428 | n/a | return res; |
---|
2429 | n/a | } |
---|
2430 | n/a | |
---|
2431 | n/a | PyDoc_STRVAR(accept_doc, |
---|
2432 | n/a | "_accept() -> (integer, address info)\n\ |
---|
2433 | n/a | \n\ |
---|
2434 | n/a | Wait for an incoming connection. Return a new socket file descriptor\n\ |
---|
2435 | n/a | representing the connection, and the address of the client.\n\ |
---|
2436 | n/a | For IP sockets, the address info is a pair (hostaddr, port)."); |
---|
2437 | n/a | |
---|
2438 | n/a | /* s.setblocking(flag) method. Argument: |
---|
2439 | n/a | False -- non-blocking mode; same as settimeout(0) |
---|
2440 | n/a | True -- blocking mode; same as settimeout(None) |
---|
2441 | n/a | */ |
---|
2442 | n/a | |
---|
2443 | n/a | static PyObject * |
---|
2444 | n/a | sock_setblocking(PySocketSockObject *s, PyObject *arg) |
---|
2445 | n/a | { |
---|
2446 | n/a | long block; |
---|
2447 | n/a | |
---|
2448 | n/a | block = PyLong_AsLong(arg); |
---|
2449 | n/a | if (block == -1 && PyErr_Occurred()) |
---|
2450 | n/a | return NULL; |
---|
2451 | n/a | |
---|
2452 | n/a | s->sock_timeout = _PyTime_FromSeconds(block ? -1 : 0); |
---|
2453 | n/a | if (internal_setblocking(s, block) == -1) { |
---|
2454 | n/a | return NULL; |
---|
2455 | n/a | } |
---|
2456 | n/a | Py_RETURN_NONE; |
---|
2457 | n/a | } |
---|
2458 | n/a | |
---|
2459 | n/a | PyDoc_STRVAR(setblocking_doc, |
---|
2460 | n/a | "setblocking(flag)\n\ |
---|
2461 | n/a | \n\ |
---|
2462 | n/a | Set the socket to blocking (flag is true) or non-blocking (false).\n\ |
---|
2463 | n/a | setblocking(True) is equivalent to settimeout(None);\n\ |
---|
2464 | n/a | setblocking(False) is equivalent to settimeout(0.0)."); |
---|
2465 | n/a | |
---|
2466 | n/a | static int |
---|
2467 | n/a | socket_parse_timeout(_PyTime_t *timeout, PyObject *timeout_obj) |
---|
2468 | n/a | { |
---|
2469 | n/a | #ifdef MS_WINDOWS |
---|
2470 | n/a | struct timeval tv; |
---|
2471 | n/a | #endif |
---|
2472 | n/a | #ifndef HAVE_POLL |
---|
2473 | n/a | _PyTime_t ms; |
---|
2474 | n/a | #endif |
---|
2475 | n/a | int overflow = 0; |
---|
2476 | n/a | |
---|
2477 | n/a | if (timeout_obj == Py_None) { |
---|
2478 | n/a | *timeout = _PyTime_FromSeconds(-1); |
---|
2479 | n/a | return 0; |
---|
2480 | n/a | } |
---|
2481 | n/a | |
---|
2482 | n/a | if (_PyTime_FromSecondsObject(timeout, |
---|
2483 | n/a | timeout_obj, _PyTime_ROUND_CEILING) < 0) |
---|
2484 | n/a | return -1; |
---|
2485 | n/a | |
---|
2486 | n/a | if (*timeout < 0) { |
---|
2487 | n/a | PyErr_SetString(PyExc_ValueError, "Timeout value out of range"); |
---|
2488 | n/a | return -1; |
---|
2489 | n/a | } |
---|
2490 | n/a | |
---|
2491 | n/a | #ifdef MS_WINDOWS |
---|
2492 | n/a | overflow |= (_PyTime_AsTimeval(*timeout, &tv, _PyTime_ROUND_CEILING) < 0); |
---|
2493 | n/a | #endif |
---|
2494 | n/a | #ifndef HAVE_POLL |
---|
2495 | n/a | ms = _PyTime_AsMilliseconds(*timeout, _PyTime_ROUND_CEILING); |
---|
2496 | n/a | overflow |= (ms > INT_MAX); |
---|
2497 | n/a | #endif |
---|
2498 | n/a | if (overflow) { |
---|
2499 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
2500 | n/a | "timeout doesn't fit into C timeval"); |
---|
2501 | n/a | return -1; |
---|
2502 | n/a | } |
---|
2503 | n/a | |
---|
2504 | n/a | return 0; |
---|
2505 | n/a | } |
---|
2506 | n/a | |
---|
2507 | n/a | /* s.settimeout(timeout) method. Argument: |
---|
2508 | n/a | None -- no timeout, blocking mode; same as setblocking(True) |
---|
2509 | n/a | 0.0 -- non-blocking mode; same as setblocking(False) |
---|
2510 | n/a | > 0 -- timeout mode; operations time out after timeout seconds |
---|
2511 | n/a | < 0 -- illegal; raises an exception |
---|
2512 | n/a | */ |
---|
2513 | n/a | static PyObject * |
---|
2514 | n/a | sock_settimeout(PySocketSockObject *s, PyObject *arg) |
---|
2515 | n/a | { |
---|
2516 | n/a | _PyTime_t timeout; |
---|
2517 | n/a | |
---|
2518 | n/a | if (socket_parse_timeout(&timeout, arg) < 0) |
---|
2519 | n/a | return NULL; |
---|
2520 | n/a | |
---|
2521 | n/a | s->sock_timeout = timeout; |
---|
2522 | n/a | if (internal_setblocking(s, timeout < 0) == -1) { |
---|
2523 | n/a | return NULL; |
---|
2524 | n/a | } |
---|
2525 | n/a | Py_RETURN_NONE; |
---|
2526 | n/a | } |
---|
2527 | n/a | |
---|
2528 | n/a | PyDoc_STRVAR(settimeout_doc, |
---|
2529 | n/a | "settimeout(timeout)\n\ |
---|
2530 | n/a | \n\ |
---|
2531 | n/a | Set a timeout on socket operations. 'timeout' can be a float,\n\ |
---|
2532 | n/a | giving in seconds, or None. Setting a timeout of None disables\n\ |
---|
2533 | n/a | the timeout feature and is equivalent to setblocking(1).\n\ |
---|
2534 | n/a | Setting a timeout of zero is the same as setblocking(0)."); |
---|
2535 | n/a | |
---|
2536 | n/a | /* s.gettimeout() method. |
---|
2537 | n/a | Returns the timeout associated with a socket. */ |
---|
2538 | n/a | static PyObject * |
---|
2539 | n/a | sock_gettimeout(PySocketSockObject *s) |
---|
2540 | n/a | { |
---|
2541 | n/a | if (s->sock_timeout < 0) { |
---|
2542 | n/a | Py_RETURN_NONE; |
---|
2543 | n/a | } |
---|
2544 | n/a | else { |
---|
2545 | n/a | double seconds = _PyTime_AsSecondsDouble(s->sock_timeout); |
---|
2546 | n/a | return PyFloat_FromDouble(seconds); |
---|
2547 | n/a | } |
---|
2548 | n/a | } |
---|
2549 | n/a | |
---|
2550 | n/a | PyDoc_STRVAR(gettimeout_doc, |
---|
2551 | n/a | "gettimeout() -> timeout\n\ |
---|
2552 | n/a | \n\ |
---|
2553 | n/a | Returns the timeout in seconds (float) associated with socket \n\ |
---|
2554 | n/a | operations. A timeout of None indicates that timeouts on socket \n\ |
---|
2555 | n/a | operations are disabled."); |
---|
2556 | n/a | |
---|
2557 | n/a | /* s.setsockopt() method. |
---|
2558 | n/a | With an integer third argument, sets an integer optval with optlen=4. |
---|
2559 | n/a | With None as third argument and an integer fourth argument, set |
---|
2560 | n/a | optval=NULL with unsigned int as optlen. |
---|
2561 | n/a | With a string third argument, sets an option from a buffer; |
---|
2562 | n/a | use optional built-in module 'struct' to encode the string. |
---|
2563 | n/a | */ |
---|
2564 | n/a | |
---|
2565 | n/a | static PyObject * |
---|
2566 | n/a | sock_setsockopt(PySocketSockObject *s, PyObject *args) |
---|
2567 | n/a | { |
---|
2568 | n/a | int level; |
---|
2569 | n/a | int optname; |
---|
2570 | n/a | int res; |
---|
2571 | n/a | Py_buffer optval; |
---|
2572 | n/a | int flag; |
---|
2573 | n/a | unsigned int optlen; |
---|
2574 | n/a | PyObject *none; |
---|
2575 | n/a | |
---|
2576 | n/a | /* setsockopt(level, opt, flag) */ |
---|
2577 | n/a | if (PyArg_ParseTuple(args, "iii:setsockopt", |
---|
2578 | n/a | &level, &optname, &flag)) { |
---|
2579 | n/a | res = setsockopt(s->sock_fd, level, optname, |
---|
2580 | n/a | (char*)&flag, sizeof flag); |
---|
2581 | n/a | goto done; |
---|
2582 | n/a | } |
---|
2583 | n/a | |
---|
2584 | n/a | PyErr_Clear(); |
---|
2585 | n/a | /* setsockopt(level, opt, None, flag) */ |
---|
2586 | n/a | if (PyArg_ParseTuple(args, "iiO!I:setsockopt", |
---|
2587 | n/a | &level, &optname, Py_TYPE(Py_None), &none, &optlen)) { |
---|
2588 | n/a | assert(sizeof(socklen_t) >= sizeof(unsigned int)); |
---|
2589 | n/a | res = setsockopt(s->sock_fd, level, optname, |
---|
2590 | n/a | NULL, (socklen_t)optlen); |
---|
2591 | n/a | goto done; |
---|
2592 | n/a | } |
---|
2593 | n/a | |
---|
2594 | n/a | PyErr_Clear(); |
---|
2595 | n/a | /* setsockopt(level, opt, buffer) */ |
---|
2596 | n/a | if (!PyArg_ParseTuple(args, "iiy*:setsockopt", |
---|
2597 | n/a | &level, &optname, &optval)) |
---|
2598 | n/a | return NULL; |
---|
2599 | n/a | |
---|
2600 | n/a | #ifdef MS_WINDOWS |
---|
2601 | n/a | if (optval.len > INT_MAX) { |
---|
2602 | n/a | PyBuffer_Release(&optval); |
---|
2603 | n/a | PyErr_Format(PyExc_OverflowError, |
---|
2604 | n/a | "socket option is larger than %i bytes", |
---|
2605 | n/a | INT_MAX); |
---|
2606 | n/a | return NULL; |
---|
2607 | n/a | } |
---|
2608 | n/a | res = setsockopt(s->sock_fd, level, optname, |
---|
2609 | n/a | optval.buf, (int)optval.len); |
---|
2610 | n/a | #else |
---|
2611 | n/a | res = setsockopt(s->sock_fd, level, optname, optval.buf, optval.len); |
---|
2612 | n/a | #endif |
---|
2613 | n/a | PyBuffer_Release(&optval); |
---|
2614 | n/a | |
---|
2615 | n/a | done: |
---|
2616 | n/a | if (res < 0) { |
---|
2617 | n/a | return s->errorhandler(); |
---|
2618 | n/a | } |
---|
2619 | n/a | |
---|
2620 | n/a | Py_RETURN_NONE; |
---|
2621 | n/a | } |
---|
2622 | n/a | |
---|
2623 | n/a | PyDoc_STRVAR(setsockopt_doc, |
---|
2624 | n/a | "setsockopt(level, option, value: int)\n\ |
---|
2625 | n/a | setsockopt(level, option, value: buffer)\n\ |
---|
2626 | n/a | setsockopt(level, option, None, optlen: int)\n\ |
---|
2627 | n/a | \n\ |
---|
2628 | n/a | Set a socket option. See the Unix manual for level and option.\n\ |
---|
2629 | n/a | The value argument can either be an integer, a string buffer, or \n\ |
---|
2630 | n/a | None, optlen."); |
---|
2631 | n/a | |
---|
2632 | n/a | |
---|
2633 | n/a | /* s.getsockopt() method. |
---|
2634 | n/a | With two arguments, retrieves an integer option. |
---|
2635 | n/a | With a third integer argument, retrieves a string buffer of that size; |
---|
2636 | n/a | use optional built-in module 'struct' to decode the string. */ |
---|
2637 | n/a | |
---|
2638 | n/a | static PyObject * |
---|
2639 | n/a | sock_getsockopt(PySocketSockObject *s, PyObject *args) |
---|
2640 | n/a | { |
---|
2641 | n/a | int level; |
---|
2642 | n/a | int optname; |
---|
2643 | n/a | int res; |
---|
2644 | n/a | PyObject *buf; |
---|
2645 | n/a | socklen_t buflen = 0; |
---|
2646 | n/a | |
---|
2647 | n/a | if (!PyArg_ParseTuple(args, "ii|i:getsockopt", |
---|
2648 | n/a | &level, &optname, &buflen)) |
---|
2649 | n/a | return NULL; |
---|
2650 | n/a | |
---|
2651 | n/a | if (buflen == 0) { |
---|
2652 | n/a | int flag = 0; |
---|
2653 | n/a | socklen_t flagsize = sizeof flag; |
---|
2654 | n/a | res = getsockopt(s->sock_fd, level, optname, |
---|
2655 | n/a | (void *)&flag, &flagsize); |
---|
2656 | n/a | if (res < 0) |
---|
2657 | n/a | return s->errorhandler(); |
---|
2658 | n/a | return PyLong_FromLong(flag); |
---|
2659 | n/a | } |
---|
2660 | n/a | if (buflen <= 0 || buflen > 1024) { |
---|
2661 | n/a | PyErr_SetString(PyExc_OSError, |
---|
2662 | n/a | "getsockopt buflen out of range"); |
---|
2663 | n/a | return NULL; |
---|
2664 | n/a | } |
---|
2665 | n/a | buf = PyBytes_FromStringAndSize((char *)NULL, buflen); |
---|
2666 | n/a | if (buf == NULL) |
---|
2667 | n/a | return NULL; |
---|
2668 | n/a | res = getsockopt(s->sock_fd, level, optname, |
---|
2669 | n/a | (void *)PyBytes_AS_STRING(buf), &buflen); |
---|
2670 | n/a | if (res < 0) { |
---|
2671 | n/a | Py_DECREF(buf); |
---|
2672 | n/a | return s->errorhandler(); |
---|
2673 | n/a | } |
---|
2674 | n/a | _PyBytes_Resize(&buf, buflen); |
---|
2675 | n/a | return buf; |
---|
2676 | n/a | } |
---|
2677 | n/a | |
---|
2678 | n/a | PyDoc_STRVAR(getsockopt_doc, |
---|
2679 | n/a | "getsockopt(level, option[, buffersize]) -> value\n\ |
---|
2680 | n/a | \n\ |
---|
2681 | n/a | Get a socket option. See the Unix manual for level and option.\n\ |
---|
2682 | n/a | If a nonzero buffersize argument is given, the return value is a\n\ |
---|
2683 | n/a | string of that length; otherwise it is an integer."); |
---|
2684 | n/a | |
---|
2685 | n/a | |
---|
2686 | n/a | /* s.bind(sockaddr) method */ |
---|
2687 | n/a | |
---|
2688 | n/a | static PyObject * |
---|
2689 | n/a | sock_bind(PySocketSockObject *s, PyObject *addro) |
---|
2690 | n/a | { |
---|
2691 | n/a | sock_addr_t addrbuf; |
---|
2692 | n/a | int addrlen; |
---|
2693 | n/a | int res; |
---|
2694 | n/a | |
---|
2695 | n/a | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
---|
2696 | n/a | return NULL; |
---|
2697 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
2698 | n/a | res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen); |
---|
2699 | n/a | Py_END_ALLOW_THREADS |
---|
2700 | n/a | if (res < 0) |
---|
2701 | n/a | return s->errorhandler(); |
---|
2702 | n/a | Py_RETURN_NONE; |
---|
2703 | n/a | } |
---|
2704 | n/a | |
---|
2705 | n/a | PyDoc_STRVAR(bind_doc, |
---|
2706 | n/a | "bind(address)\n\ |
---|
2707 | n/a | \n\ |
---|
2708 | n/a | Bind the socket to a local address. For IP sockets, the address is a\n\ |
---|
2709 | n/a | pair (host, port); the host must refer to the local host. For raw packet\n\ |
---|
2710 | n/a | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"); |
---|
2711 | n/a | |
---|
2712 | n/a | |
---|
2713 | n/a | /* s.close() method. |
---|
2714 | n/a | Set the file descriptor to -1 so operations tried subsequently |
---|
2715 | n/a | will surely fail. */ |
---|
2716 | n/a | |
---|
2717 | n/a | static PyObject * |
---|
2718 | n/a | sock_close(PySocketSockObject *s) |
---|
2719 | n/a | { |
---|
2720 | n/a | SOCKET_T fd; |
---|
2721 | n/a | int res; |
---|
2722 | n/a | |
---|
2723 | n/a | fd = s->sock_fd; |
---|
2724 | n/a | if (fd != INVALID_SOCKET) { |
---|
2725 | n/a | s->sock_fd = INVALID_SOCKET; |
---|
2726 | n/a | |
---|
2727 | n/a | /* We do not want to retry upon EINTR: see |
---|
2728 | n/a | http://lwn.net/Articles/576478/ and |
---|
2729 | n/a | http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
---|
2730 | n/a | for more details. */ |
---|
2731 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
2732 | n/a | res = SOCKETCLOSE(fd); |
---|
2733 | n/a | Py_END_ALLOW_THREADS |
---|
2734 | n/a | if (res < 0) { |
---|
2735 | n/a | return s->errorhandler(); |
---|
2736 | n/a | } |
---|
2737 | n/a | } |
---|
2738 | n/a | Py_RETURN_NONE; |
---|
2739 | n/a | } |
---|
2740 | n/a | |
---|
2741 | n/a | PyDoc_STRVAR(close_doc, |
---|
2742 | n/a | "close()\n\ |
---|
2743 | n/a | \n\ |
---|
2744 | n/a | Close the socket. It cannot be used after this call."); |
---|
2745 | n/a | |
---|
2746 | n/a | static PyObject * |
---|
2747 | n/a | sock_detach(PySocketSockObject *s) |
---|
2748 | n/a | { |
---|
2749 | n/a | SOCKET_T fd = s->sock_fd; |
---|
2750 | n/a | s->sock_fd = INVALID_SOCKET; |
---|
2751 | n/a | return PyLong_FromSocket_t(fd); |
---|
2752 | n/a | } |
---|
2753 | n/a | |
---|
2754 | n/a | PyDoc_STRVAR(detach_doc, |
---|
2755 | n/a | "detach()\n\ |
---|
2756 | n/a | \n\ |
---|
2757 | n/a | Close the socket object without closing the underlying file descriptor.\n\ |
---|
2758 | n/a | The object cannot be used after this call, but the file descriptor\n\ |
---|
2759 | n/a | can be reused for other purposes. The file descriptor is returned."); |
---|
2760 | n/a | |
---|
2761 | n/a | static int |
---|
2762 | n/a | sock_connect_impl(PySocketSockObject *s, void* Py_UNUSED(data)) |
---|
2763 | n/a | { |
---|
2764 | n/a | int err; |
---|
2765 | n/a | socklen_t size = sizeof err; |
---|
2766 | n/a | |
---|
2767 | n/a | if (getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, (void *)&err, &size)) { |
---|
2768 | n/a | /* getsockopt() failed */ |
---|
2769 | n/a | return 0; |
---|
2770 | n/a | } |
---|
2771 | n/a | |
---|
2772 | n/a | if (err == EISCONN) |
---|
2773 | n/a | return 1; |
---|
2774 | n/a | if (err != 0) { |
---|
2775 | n/a | /* sock_call_ex() uses GET_SOCK_ERROR() to get the error code */ |
---|
2776 | n/a | SET_SOCK_ERROR(err); |
---|
2777 | n/a | return 0; |
---|
2778 | n/a | } |
---|
2779 | n/a | return 1; |
---|
2780 | n/a | } |
---|
2781 | n/a | |
---|
2782 | n/a | static int |
---|
2783 | n/a | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, |
---|
2784 | n/a | int raise) |
---|
2785 | n/a | { |
---|
2786 | n/a | int res, err, wait_connect; |
---|
2787 | n/a | |
---|
2788 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
2789 | n/a | res = connect(s->sock_fd, addr, addrlen); |
---|
2790 | n/a | Py_END_ALLOW_THREADS |
---|
2791 | n/a | |
---|
2792 | n/a | if (!res) { |
---|
2793 | n/a | /* connect() succeeded, the socket is connected */ |
---|
2794 | n/a | return 0; |
---|
2795 | n/a | } |
---|
2796 | n/a | |
---|
2797 | n/a | /* connect() failed */ |
---|
2798 | n/a | |
---|
2799 | n/a | /* save error, PyErr_CheckSignals() can replace it */ |
---|
2800 | n/a | err = GET_SOCK_ERROR; |
---|
2801 | n/a | if (CHECK_ERRNO(EINTR)) { |
---|
2802 | n/a | if (PyErr_CheckSignals()) |
---|
2803 | n/a | return -1; |
---|
2804 | n/a | |
---|
2805 | n/a | /* Issue #23618: when connect() fails with EINTR, the connection is |
---|
2806 | n/a | running asynchronously. |
---|
2807 | n/a | |
---|
2808 | n/a | If the socket is blocking or has a timeout, wait until the |
---|
2809 | n/a | connection completes, fails or timed out using select(), and then |
---|
2810 | n/a | get the connection status using getsockopt(SO_ERROR). |
---|
2811 | n/a | |
---|
2812 | n/a | If the socket is non-blocking, raise InterruptedError. The caller is |
---|
2813 | n/a | responsible to wait until the connection completes, fails or timed |
---|
2814 | n/a | out (it's the case in asyncio for example). */ |
---|
2815 | n/a | wait_connect = (s->sock_timeout != 0 && IS_SELECTABLE(s)); |
---|
2816 | n/a | } |
---|
2817 | n/a | else { |
---|
2818 | n/a | wait_connect = (s->sock_timeout > 0 && err == SOCK_INPROGRESS_ERR |
---|
2819 | n/a | && IS_SELECTABLE(s)); |
---|
2820 | n/a | } |
---|
2821 | n/a | |
---|
2822 | n/a | if (!wait_connect) { |
---|
2823 | n/a | if (raise) { |
---|
2824 | n/a | /* restore error, maybe replaced by PyErr_CheckSignals() */ |
---|
2825 | n/a | SET_SOCK_ERROR(err); |
---|
2826 | n/a | s->errorhandler(); |
---|
2827 | n/a | return -1; |
---|
2828 | n/a | } |
---|
2829 | n/a | else |
---|
2830 | n/a | return err; |
---|
2831 | n/a | } |
---|
2832 | n/a | |
---|
2833 | n/a | if (raise) { |
---|
2834 | n/a | /* socket.connect() raises an exception on error */ |
---|
2835 | n/a | if (sock_call_ex(s, 1, sock_connect_impl, NULL, |
---|
2836 | n/a | 1, NULL, s->sock_timeout) < 0) |
---|
2837 | n/a | return -1; |
---|
2838 | n/a | } |
---|
2839 | n/a | else { |
---|
2840 | n/a | /* socket.connect_ex() returns the error code on error */ |
---|
2841 | n/a | if (sock_call_ex(s, 1, sock_connect_impl, NULL, |
---|
2842 | n/a | 1, &err, s->sock_timeout) < 0) |
---|
2843 | n/a | return err; |
---|
2844 | n/a | } |
---|
2845 | n/a | return 0; |
---|
2846 | n/a | } |
---|
2847 | n/a | |
---|
2848 | n/a | /* s.connect(sockaddr) method */ |
---|
2849 | n/a | |
---|
2850 | n/a | static PyObject * |
---|
2851 | n/a | sock_connect(PySocketSockObject *s, PyObject *addro) |
---|
2852 | n/a | { |
---|
2853 | n/a | sock_addr_t addrbuf; |
---|
2854 | n/a | int addrlen; |
---|
2855 | n/a | int res; |
---|
2856 | n/a | |
---|
2857 | n/a | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
---|
2858 | n/a | return NULL; |
---|
2859 | n/a | |
---|
2860 | n/a | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, 1); |
---|
2861 | n/a | if (res < 0) |
---|
2862 | n/a | return NULL; |
---|
2863 | n/a | |
---|
2864 | n/a | Py_RETURN_NONE; |
---|
2865 | n/a | } |
---|
2866 | n/a | |
---|
2867 | n/a | PyDoc_STRVAR(connect_doc, |
---|
2868 | n/a | "connect(address)\n\ |
---|
2869 | n/a | \n\ |
---|
2870 | n/a | Connect the socket to a remote address. For IP sockets, the address\n\ |
---|
2871 | n/a | is a pair (host, port)."); |
---|
2872 | n/a | |
---|
2873 | n/a | |
---|
2874 | n/a | /* s.connect_ex(sockaddr) method */ |
---|
2875 | n/a | |
---|
2876 | n/a | static PyObject * |
---|
2877 | n/a | sock_connect_ex(PySocketSockObject *s, PyObject *addro) |
---|
2878 | n/a | { |
---|
2879 | n/a | sock_addr_t addrbuf; |
---|
2880 | n/a | int addrlen; |
---|
2881 | n/a | int res; |
---|
2882 | n/a | |
---|
2883 | n/a | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) |
---|
2884 | n/a | return NULL; |
---|
2885 | n/a | |
---|
2886 | n/a | res = internal_connect(s, SAS2SA(&addrbuf), addrlen, 0); |
---|
2887 | n/a | if (res < 0) |
---|
2888 | n/a | return NULL; |
---|
2889 | n/a | |
---|
2890 | n/a | return PyLong_FromLong((long) res); |
---|
2891 | n/a | } |
---|
2892 | n/a | |
---|
2893 | n/a | PyDoc_STRVAR(connect_ex_doc, |
---|
2894 | n/a | "connect_ex(address) -> errno\n\ |
---|
2895 | n/a | \n\ |
---|
2896 | n/a | This is like connect(address), but returns an error code (the errno value)\n\ |
---|
2897 | n/a | instead of raising an exception when an error occurs."); |
---|
2898 | n/a | |
---|
2899 | n/a | |
---|
2900 | n/a | /* s.fileno() method */ |
---|
2901 | n/a | |
---|
2902 | n/a | static PyObject * |
---|
2903 | n/a | sock_fileno(PySocketSockObject *s) |
---|
2904 | n/a | { |
---|
2905 | n/a | return PyLong_FromSocket_t(s->sock_fd); |
---|
2906 | n/a | } |
---|
2907 | n/a | |
---|
2908 | n/a | PyDoc_STRVAR(fileno_doc, |
---|
2909 | n/a | "fileno() -> integer\n\ |
---|
2910 | n/a | \n\ |
---|
2911 | n/a | Return the integer file descriptor of the socket."); |
---|
2912 | n/a | |
---|
2913 | n/a | |
---|
2914 | n/a | /* s.getsockname() method */ |
---|
2915 | n/a | |
---|
2916 | n/a | static PyObject * |
---|
2917 | n/a | sock_getsockname(PySocketSockObject *s) |
---|
2918 | n/a | { |
---|
2919 | n/a | sock_addr_t addrbuf; |
---|
2920 | n/a | int res; |
---|
2921 | n/a | socklen_t addrlen; |
---|
2922 | n/a | |
---|
2923 | n/a | if (!getsockaddrlen(s, &addrlen)) |
---|
2924 | n/a | return NULL; |
---|
2925 | n/a | memset(&addrbuf, 0, addrlen); |
---|
2926 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
2927 | n/a | res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
---|
2928 | n/a | Py_END_ALLOW_THREADS |
---|
2929 | n/a | if (res < 0) |
---|
2930 | n/a | return s->errorhandler(); |
---|
2931 | n/a | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
---|
2932 | n/a | s->sock_proto); |
---|
2933 | n/a | } |
---|
2934 | n/a | |
---|
2935 | n/a | PyDoc_STRVAR(getsockname_doc, |
---|
2936 | n/a | "getsockname() -> address info\n\ |
---|
2937 | n/a | \n\ |
---|
2938 | n/a | Return the address of the local endpoint. For IP sockets, the address\n\ |
---|
2939 | n/a | info is a pair (hostaddr, port)."); |
---|
2940 | n/a | |
---|
2941 | n/a | |
---|
2942 | n/a | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */ |
---|
2943 | n/a | /* s.getpeername() method */ |
---|
2944 | n/a | |
---|
2945 | n/a | static PyObject * |
---|
2946 | n/a | sock_getpeername(PySocketSockObject *s) |
---|
2947 | n/a | { |
---|
2948 | n/a | sock_addr_t addrbuf; |
---|
2949 | n/a | int res; |
---|
2950 | n/a | socklen_t addrlen; |
---|
2951 | n/a | |
---|
2952 | n/a | if (!getsockaddrlen(s, &addrlen)) |
---|
2953 | n/a | return NULL; |
---|
2954 | n/a | memset(&addrbuf, 0, addrlen); |
---|
2955 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
2956 | n/a | res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen); |
---|
2957 | n/a | Py_END_ALLOW_THREADS |
---|
2958 | n/a | if (res < 0) |
---|
2959 | n/a | return s->errorhandler(); |
---|
2960 | n/a | return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
---|
2961 | n/a | s->sock_proto); |
---|
2962 | n/a | } |
---|
2963 | n/a | |
---|
2964 | n/a | PyDoc_STRVAR(getpeername_doc, |
---|
2965 | n/a | "getpeername() -> address info\n\ |
---|
2966 | n/a | \n\ |
---|
2967 | n/a | Return the address of the remote endpoint. For IP sockets, the address\n\ |
---|
2968 | n/a | info is a pair (hostaddr, port)."); |
---|
2969 | n/a | |
---|
2970 | n/a | #endif /* HAVE_GETPEERNAME */ |
---|
2971 | n/a | |
---|
2972 | n/a | |
---|
2973 | n/a | /* s.listen(n) method */ |
---|
2974 | n/a | |
---|
2975 | n/a | static PyObject * |
---|
2976 | n/a | sock_listen(PySocketSockObject *s, PyObject *args) |
---|
2977 | n/a | { |
---|
2978 | n/a | /* We try to choose a default backlog high enough to avoid connection drops |
---|
2979 | n/a | * for common workloads, yet not too high to limit resource usage. */ |
---|
2980 | n/a | int backlog = Py_MIN(SOMAXCONN, 128); |
---|
2981 | n/a | int res; |
---|
2982 | n/a | |
---|
2983 | n/a | if (!PyArg_ParseTuple(args, "|i:listen", &backlog)) |
---|
2984 | n/a | return NULL; |
---|
2985 | n/a | |
---|
2986 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
2987 | n/a | /* To avoid problems on systems that don't allow a negative backlog |
---|
2988 | n/a | * (which doesn't make sense anyway) we force a minimum value of 0. */ |
---|
2989 | n/a | if (backlog < 0) |
---|
2990 | n/a | backlog = 0; |
---|
2991 | n/a | res = listen(s->sock_fd, backlog); |
---|
2992 | n/a | Py_END_ALLOW_THREADS |
---|
2993 | n/a | if (res < 0) |
---|
2994 | n/a | return s->errorhandler(); |
---|
2995 | n/a | Py_RETURN_NONE; |
---|
2996 | n/a | } |
---|
2997 | n/a | |
---|
2998 | n/a | PyDoc_STRVAR(listen_doc, |
---|
2999 | n/a | "listen([backlog])\n\ |
---|
3000 | n/a | \n\ |
---|
3001 | n/a | Enable a server to accept connections. If backlog is specified, it must be\n\ |
---|
3002 | n/a | at least 0 (if it is lower, it is set to 0); it specifies the number of\n\ |
---|
3003 | n/a | unaccepted connections that the system will allow before refusing new\n\ |
---|
3004 | n/a | connections. If not specified, a default reasonable value is chosen."); |
---|
3005 | n/a | |
---|
3006 | n/a | struct sock_recv { |
---|
3007 | n/a | char *cbuf; |
---|
3008 | n/a | Py_ssize_t len; |
---|
3009 | n/a | int flags; |
---|
3010 | n/a | Py_ssize_t result; |
---|
3011 | n/a | }; |
---|
3012 | n/a | |
---|
3013 | n/a | static int |
---|
3014 | n/a | sock_recv_impl(PySocketSockObject *s, void *data) |
---|
3015 | n/a | { |
---|
3016 | n/a | struct sock_recv *ctx = data; |
---|
3017 | n/a | |
---|
3018 | n/a | #ifdef MS_WINDOWS |
---|
3019 | n/a | if (ctx->len > INT_MAX) |
---|
3020 | n/a | ctx->len = INT_MAX; |
---|
3021 | n/a | ctx->result = recv(s->sock_fd, ctx->cbuf, (int)ctx->len, ctx->flags); |
---|
3022 | n/a | #else |
---|
3023 | n/a | ctx->result = recv(s->sock_fd, ctx->cbuf, ctx->len, ctx->flags); |
---|
3024 | n/a | #endif |
---|
3025 | n/a | return (ctx->result >= 0); |
---|
3026 | n/a | } |
---|
3027 | n/a | |
---|
3028 | n/a | |
---|
3029 | n/a | /* |
---|
3030 | n/a | * This is the guts of the recv() and recv_into() methods, which reads into a |
---|
3031 | n/a | * char buffer. If you have any inc/dec ref to do to the objects that contain |
---|
3032 | n/a | * the buffer, do it in the caller. This function returns the number of bytes |
---|
3033 | n/a | * successfully read. If there was an error, it returns -1. Note that it is |
---|
3034 | n/a | * also possible that we return a number of bytes smaller than the request |
---|
3035 | n/a | * bytes. |
---|
3036 | n/a | */ |
---|
3037 | n/a | |
---|
3038 | n/a | static Py_ssize_t |
---|
3039 | n/a | sock_recv_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags) |
---|
3040 | n/a | { |
---|
3041 | n/a | struct sock_recv ctx; |
---|
3042 | n/a | |
---|
3043 | n/a | if (!IS_SELECTABLE(s)) { |
---|
3044 | n/a | select_error(); |
---|
3045 | n/a | return -1; |
---|
3046 | n/a | } |
---|
3047 | n/a | if (len == 0) { |
---|
3048 | n/a | /* If 0 bytes were requested, do nothing. */ |
---|
3049 | n/a | return 0; |
---|
3050 | n/a | } |
---|
3051 | n/a | |
---|
3052 | n/a | ctx.cbuf = cbuf; |
---|
3053 | n/a | ctx.len = len; |
---|
3054 | n/a | ctx.flags = flags; |
---|
3055 | n/a | if (sock_call(s, 0, sock_recv_impl, &ctx) < 0) |
---|
3056 | n/a | return -1; |
---|
3057 | n/a | |
---|
3058 | n/a | return ctx.result; |
---|
3059 | n/a | } |
---|
3060 | n/a | |
---|
3061 | n/a | |
---|
3062 | n/a | /* s.recv(nbytes [,flags]) method */ |
---|
3063 | n/a | |
---|
3064 | n/a | static PyObject * |
---|
3065 | n/a | sock_recv(PySocketSockObject *s, PyObject *args) |
---|
3066 | n/a | { |
---|
3067 | n/a | Py_ssize_t recvlen, outlen; |
---|
3068 | n/a | int flags = 0; |
---|
3069 | n/a | PyObject *buf; |
---|
3070 | n/a | |
---|
3071 | n/a | if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags)) |
---|
3072 | n/a | return NULL; |
---|
3073 | n/a | |
---|
3074 | n/a | if (recvlen < 0) { |
---|
3075 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
3076 | n/a | "negative buffersize in recv"); |
---|
3077 | n/a | return NULL; |
---|
3078 | n/a | } |
---|
3079 | n/a | |
---|
3080 | n/a | /* Allocate a new string. */ |
---|
3081 | n/a | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
---|
3082 | n/a | if (buf == NULL) |
---|
3083 | n/a | return NULL; |
---|
3084 | n/a | |
---|
3085 | n/a | /* Call the guts */ |
---|
3086 | n/a | outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags); |
---|
3087 | n/a | if (outlen < 0) { |
---|
3088 | n/a | /* An error occurred, release the string and return an |
---|
3089 | n/a | error. */ |
---|
3090 | n/a | Py_DECREF(buf); |
---|
3091 | n/a | return NULL; |
---|
3092 | n/a | } |
---|
3093 | n/a | if (outlen != recvlen) { |
---|
3094 | n/a | /* We did not read as many bytes as we anticipated, resize the |
---|
3095 | n/a | string if possible and be successful. */ |
---|
3096 | n/a | _PyBytes_Resize(&buf, outlen); |
---|
3097 | n/a | } |
---|
3098 | n/a | |
---|
3099 | n/a | return buf; |
---|
3100 | n/a | } |
---|
3101 | n/a | |
---|
3102 | n/a | PyDoc_STRVAR(recv_doc, |
---|
3103 | n/a | "recv(buffersize[, flags]) -> data\n\ |
---|
3104 | n/a | \n\ |
---|
3105 | n/a | Receive up to buffersize bytes from the socket. For the optional flags\n\ |
---|
3106 | n/a | argument, see the Unix manual. When no data is available, block until\n\ |
---|
3107 | n/a | at least one byte is available or until the remote end is closed. When\n\ |
---|
3108 | n/a | the remote end is closed and all data is read, return the empty string."); |
---|
3109 | n/a | |
---|
3110 | n/a | |
---|
3111 | n/a | /* s.recv_into(buffer, [nbytes [,flags]]) method */ |
---|
3112 | n/a | |
---|
3113 | n/a | static PyObject* |
---|
3114 | n/a | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds) |
---|
3115 | n/a | { |
---|
3116 | n/a | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
---|
3117 | n/a | |
---|
3118 | n/a | int flags = 0; |
---|
3119 | n/a | Py_buffer pbuf; |
---|
3120 | n/a | char *buf; |
---|
3121 | n/a | Py_ssize_t buflen, readlen, recvlen = 0; |
---|
3122 | n/a | |
---|
3123 | n/a | /* Get the buffer's memory */ |
---|
3124 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist, |
---|
3125 | n/a | &pbuf, &recvlen, &flags)) |
---|
3126 | n/a | return NULL; |
---|
3127 | n/a | buf = pbuf.buf; |
---|
3128 | n/a | buflen = pbuf.len; |
---|
3129 | n/a | |
---|
3130 | n/a | if (recvlen < 0) { |
---|
3131 | n/a | PyBuffer_Release(&pbuf); |
---|
3132 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
3133 | n/a | "negative buffersize in recv_into"); |
---|
3134 | n/a | return NULL; |
---|
3135 | n/a | } |
---|
3136 | n/a | if (recvlen == 0) { |
---|
3137 | n/a | /* If nbytes was not specified, use the buffer's length */ |
---|
3138 | n/a | recvlen = buflen; |
---|
3139 | n/a | } |
---|
3140 | n/a | |
---|
3141 | n/a | /* Check if the buffer is large enough */ |
---|
3142 | n/a | if (buflen < recvlen) { |
---|
3143 | n/a | PyBuffer_Release(&pbuf); |
---|
3144 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
3145 | n/a | "buffer too small for requested bytes"); |
---|
3146 | n/a | return NULL; |
---|
3147 | n/a | } |
---|
3148 | n/a | |
---|
3149 | n/a | /* Call the guts */ |
---|
3150 | n/a | readlen = sock_recv_guts(s, buf, recvlen, flags); |
---|
3151 | n/a | if (readlen < 0) { |
---|
3152 | n/a | /* Return an error. */ |
---|
3153 | n/a | PyBuffer_Release(&pbuf); |
---|
3154 | n/a | return NULL; |
---|
3155 | n/a | } |
---|
3156 | n/a | |
---|
3157 | n/a | PyBuffer_Release(&pbuf); |
---|
3158 | n/a | /* Return the number of bytes read. Note that we do not do anything |
---|
3159 | n/a | special here in the case that readlen < recvlen. */ |
---|
3160 | n/a | return PyLong_FromSsize_t(readlen); |
---|
3161 | n/a | } |
---|
3162 | n/a | |
---|
3163 | n/a | PyDoc_STRVAR(recv_into_doc, |
---|
3164 | n/a | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\ |
---|
3165 | n/a | \n\ |
---|
3166 | n/a | A version of recv() that stores its data into a buffer rather than creating \n\ |
---|
3167 | n/a | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\ |
---|
3168 | n/a | is not specified (or 0), receive up to the size available in the given buffer.\n\ |
---|
3169 | n/a | \n\ |
---|
3170 | n/a | See recv() for documentation about the flags."); |
---|
3171 | n/a | |
---|
3172 | n/a | struct sock_recvfrom { |
---|
3173 | n/a | char* cbuf; |
---|
3174 | n/a | Py_ssize_t len; |
---|
3175 | n/a | int flags; |
---|
3176 | n/a | socklen_t *addrlen; |
---|
3177 | n/a | sock_addr_t *addrbuf; |
---|
3178 | n/a | Py_ssize_t result; |
---|
3179 | n/a | }; |
---|
3180 | n/a | |
---|
3181 | n/a | static int |
---|
3182 | n/a | sock_recvfrom_impl(PySocketSockObject *s, void *data) |
---|
3183 | n/a | { |
---|
3184 | n/a | struct sock_recvfrom *ctx = data; |
---|
3185 | n/a | |
---|
3186 | n/a | memset(ctx->addrbuf, 0, *ctx->addrlen); |
---|
3187 | n/a | |
---|
3188 | n/a | #ifdef MS_WINDOWS |
---|
3189 | n/a | if (ctx->len > INT_MAX) |
---|
3190 | n/a | ctx->len = INT_MAX; |
---|
3191 | n/a | ctx->result = recvfrom(s->sock_fd, ctx->cbuf, (int)ctx->len, ctx->flags, |
---|
3192 | n/a | SAS2SA(ctx->addrbuf), ctx->addrlen); |
---|
3193 | n/a | #else |
---|
3194 | n/a | ctx->result = recvfrom(s->sock_fd, ctx->cbuf, ctx->len, ctx->flags, |
---|
3195 | n/a | SAS2SA(ctx->addrbuf), ctx->addrlen); |
---|
3196 | n/a | #endif |
---|
3197 | n/a | return (ctx->result >= 0); |
---|
3198 | n/a | } |
---|
3199 | n/a | |
---|
3200 | n/a | |
---|
3201 | n/a | /* |
---|
3202 | n/a | * This is the guts of the recvfrom() and recvfrom_into() methods, which reads |
---|
3203 | n/a | * into a char buffer. If you have any inc/def ref to do to the objects that |
---|
3204 | n/a | * contain the buffer, do it in the caller. This function returns the number |
---|
3205 | n/a | * of bytes successfully read. If there was an error, it returns -1. Note |
---|
3206 | n/a | * that it is also possible that we return a number of bytes smaller than the |
---|
3207 | n/a | * request bytes. |
---|
3208 | n/a | * |
---|
3209 | n/a | * 'addr' is a return value for the address object. Note that you must decref |
---|
3210 | n/a | * it yourself. |
---|
3211 | n/a | */ |
---|
3212 | n/a | static Py_ssize_t |
---|
3213 | n/a | sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags, |
---|
3214 | n/a | PyObject** addr) |
---|
3215 | n/a | { |
---|
3216 | n/a | sock_addr_t addrbuf; |
---|
3217 | n/a | socklen_t addrlen; |
---|
3218 | n/a | struct sock_recvfrom ctx; |
---|
3219 | n/a | |
---|
3220 | n/a | *addr = NULL; |
---|
3221 | n/a | |
---|
3222 | n/a | if (!getsockaddrlen(s, &addrlen)) |
---|
3223 | n/a | return -1; |
---|
3224 | n/a | |
---|
3225 | n/a | if (!IS_SELECTABLE(s)) { |
---|
3226 | n/a | select_error(); |
---|
3227 | n/a | return -1; |
---|
3228 | n/a | } |
---|
3229 | n/a | |
---|
3230 | n/a | ctx.cbuf = cbuf; |
---|
3231 | n/a | ctx.len = len; |
---|
3232 | n/a | ctx.flags = flags; |
---|
3233 | n/a | ctx.addrbuf = &addrbuf; |
---|
3234 | n/a | ctx.addrlen = &addrlen; |
---|
3235 | n/a | if (sock_call(s, 0, sock_recvfrom_impl, &ctx) < 0) |
---|
3236 | n/a | return -1; |
---|
3237 | n/a | |
---|
3238 | n/a | *addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen, |
---|
3239 | n/a | s->sock_proto); |
---|
3240 | n/a | if (*addr == NULL) |
---|
3241 | n/a | return -1; |
---|
3242 | n/a | |
---|
3243 | n/a | return ctx.result; |
---|
3244 | n/a | } |
---|
3245 | n/a | |
---|
3246 | n/a | /* s.recvfrom(nbytes [,flags]) method */ |
---|
3247 | n/a | |
---|
3248 | n/a | static PyObject * |
---|
3249 | n/a | sock_recvfrom(PySocketSockObject *s, PyObject *args) |
---|
3250 | n/a | { |
---|
3251 | n/a | PyObject *buf = NULL; |
---|
3252 | n/a | PyObject *addr = NULL; |
---|
3253 | n/a | PyObject *ret = NULL; |
---|
3254 | n/a | int flags = 0; |
---|
3255 | n/a | Py_ssize_t recvlen, outlen; |
---|
3256 | n/a | |
---|
3257 | n/a | if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags)) |
---|
3258 | n/a | return NULL; |
---|
3259 | n/a | |
---|
3260 | n/a | if (recvlen < 0) { |
---|
3261 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
3262 | n/a | "negative buffersize in recvfrom"); |
---|
3263 | n/a | return NULL; |
---|
3264 | n/a | } |
---|
3265 | n/a | |
---|
3266 | n/a | buf = PyBytes_FromStringAndSize((char *) 0, recvlen); |
---|
3267 | n/a | if (buf == NULL) |
---|
3268 | n/a | return NULL; |
---|
3269 | n/a | |
---|
3270 | n/a | outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf), |
---|
3271 | n/a | recvlen, flags, &addr); |
---|
3272 | n/a | if (outlen < 0) { |
---|
3273 | n/a | goto finally; |
---|
3274 | n/a | } |
---|
3275 | n/a | |
---|
3276 | n/a | if (outlen != recvlen) { |
---|
3277 | n/a | /* We did not read as many bytes as we anticipated, resize the |
---|
3278 | n/a | string if possible and be successful. */ |
---|
3279 | n/a | if (_PyBytes_Resize(&buf, outlen) < 0) |
---|
3280 | n/a | /* Oopsy, not so successful after all. */ |
---|
3281 | n/a | goto finally; |
---|
3282 | n/a | } |
---|
3283 | n/a | |
---|
3284 | n/a | ret = PyTuple_Pack(2, buf, addr); |
---|
3285 | n/a | |
---|
3286 | n/a | finally: |
---|
3287 | n/a | Py_XDECREF(buf); |
---|
3288 | n/a | Py_XDECREF(addr); |
---|
3289 | n/a | return ret; |
---|
3290 | n/a | } |
---|
3291 | n/a | |
---|
3292 | n/a | PyDoc_STRVAR(recvfrom_doc, |
---|
3293 | n/a | "recvfrom(buffersize[, flags]) -> (data, address info)\n\ |
---|
3294 | n/a | \n\ |
---|
3295 | n/a | Like recv(buffersize, flags) but also return the sender's address info."); |
---|
3296 | n/a | |
---|
3297 | n/a | |
---|
3298 | n/a | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ |
---|
3299 | n/a | |
---|
3300 | n/a | static PyObject * |
---|
3301 | n/a | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds) |
---|
3302 | n/a | { |
---|
3303 | n/a | static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; |
---|
3304 | n/a | |
---|
3305 | n/a | int flags = 0; |
---|
3306 | n/a | Py_buffer pbuf; |
---|
3307 | n/a | char *buf; |
---|
3308 | n/a | Py_ssize_t readlen, buflen, recvlen = 0; |
---|
3309 | n/a | |
---|
3310 | n/a | PyObject *addr = NULL; |
---|
3311 | n/a | |
---|
3312 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into", |
---|
3313 | n/a | kwlist, &pbuf, |
---|
3314 | n/a | &recvlen, &flags)) |
---|
3315 | n/a | return NULL; |
---|
3316 | n/a | buf = pbuf.buf; |
---|
3317 | n/a | buflen = pbuf.len; |
---|
3318 | n/a | |
---|
3319 | n/a | if (recvlen < 0) { |
---|
3320 | n/a | PyBuffer_Release(&pbuf); |
---|
3321 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
3322 | n/a | "negative buffersize in recvfrom_into"); |
---|
3323 | n/a | return NULL; |
---|
3324 | n/a | } |
---|
3325 | n/a | if (recvlen == 0) { |
---|
3326 | n/a | /* If nbytes was not specified, use the buffer's length */ |
---|
3327 | n/a | recvlen = buflen; |
---|
3328 | n/a | } else if (recvlen > buflen) { |
---|
3329 | n/a | PyBuffer_Release(&pbuf); |
---|
3330 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
3331 | n/a | "nbytes is greater than the length of the buffer"); |
---|
3332 | n/a | return NULL; |
---|
3333 | n/a | } |
---|
3334 | n/a | |
---|
3335 | n/a | readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr); |
---|
3336 | n/a | if (readlen < 0) { |
---|
3337 | n/a | PyBuffer_Release(&pbuf); |
---|
3338 | n/a | /* Return an error */ |
---|
3339 | n/a | Py_XDECREF(addr); |
---|
3340 | n/a | return NULL; |
---|
3341 | n/a | } |
---|
3342 | n/a | |
---|
3343 | n/a | PyBuffer_Release(&pbuf); |
---|
3344 | n/a | /* Return the number of bytes read and the address. Note that we do |
---|
3345 | n/a | not do anything special here in the case that readlen < recvlen. */ |
---|
3346 | n/a | return Py_BuildValue("nN", readlen, addr); |
---|
3347 | n/a | } |
---|
3348 | n/a | |
---|
3349 | n/a | PyDoc_STRVAR(recvfrom_into_doc, |
---|
3350 | n/a | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\ |
---|
3351 | n/a | \n\ |
---|
3352 | n/a | Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info."); |
---|
3353 | n/a | |
---|
3354 | n/a | /* The sendmsg() and recvmsg[_into]() methods require a working |
---|
3355 | n/a | CMSG_LEN(). See the comment near get_CMSG_LEN(). */ |
---|
3356 | n/a | #ifdef CMSG_LEN |
---|
3357 | n/a | struct sock_recvmsg { |
---|
3358 | n/a | struct msghdr *msg; |
---|
3359 | n/a | int flags; |
---|
3360 | n/a | ssize_t result; |
---|
3361 | n/a | }; |
---|
3362 | n/a | |
---|
3363 | n/a | static int |
---|
3364 | n/a | sock_recvmsg_impl(PySocketSockObject *s, void *data) |
---|
3365 | n/a | { |
---|
3366 | n/a | struct sock_recvmsg *ctx = data; |
---|
3367 | n/a | |
---|
3368 | n/a | ctx->result = recvmsg(s->sock_fd, ctx->msg, ctx->flags); |
---|
3369 | n/a | return (ctx->result >= 0); |
---|
3370 | n/a | } |
---|
3371 | n/a | |
---|
3372 | n/a | /* |
---|
3373 | n/a | * Call recvmsg() with the supplied iovec structures, flags, and |
---|
3374 | n/a | * ancillary data buffer size (controllen). Returns the tuple return |
---|
3375 | n/a | * value for recvmsg() or recvmsg_into(), with the first item provided |
---|
3376 | n/a | * by the supplied makeval() function. makeval() will be called with |
---|
3377 | n/a | * the length read and makeval_data as arguments, and must return a |
---|
3378 | n/a | * new reference (which will be decrefed if there is a subsequent |
---|
3379 | n/a | * error). On error, closes any file descriptors received via |
---|
3380 | n/a | * SCM_RIGHTS. |
---|
3381 | n/a | */ |
---|
3382 | n/a | static PyObject * |
---|
3383 | n/a | sock_recvmsg_guts(PySocketSockObject *s, struct iovec *iov, int iovlen, |
---|
3384 | n/a | int flags, Py_ssize_t controllen, |
---|
3385 | n/a | PyObject *(*makeval)(ssize_t, void *), void *makeval_data) |
---|
3386 | n/a | { |
---|
3387 | n/a | sock_addr_t addrbuf; |
---|
3388 | n/a | socklen_t addrbuflen; |
---|
3389 | n/a | struct msghdr msg = {0}; |
---|
3390 | n/a | PyObject *cmsg_list = NULL, *retval = NULL; |
---|
3391 | n/a | void *controlbuf = NULL; |
---|
3392 | n/a | struct cmsghdr *cmsgh; |
---|
3393 | n/a | size_t cmsgdatalen = 0; |
---|
3394 | n/a | int cmsg_status; |
---|
3395 | n/a | struct sock_recvmsg ctx; |
---|
3396 | n/a | |
---|
3397 | n/a | /* XXX: POSIX says that msg_name and msg_namelen "shall be |
---|
3398 | n/a | ignored" when the socket is connected (Linux fills them in |
---|
3399 | n/a | anyway for AF_UNIX sockets at least). Normally msg_namelen |
---|
3400 | n/a | seems to be set to 0 if there's no address, but try to |
---|
3401 | n/a | initialize msg_name to something that won't be mistaken for a |
---|
3402 | n/a | real address if that doesn't happen. */ |
---|
3403 | n/a | if (!getsockaddrlen(s, &addrbuflen)) |
---|
3404 | n/a | return NULL; |
---|
3405 | n/a | memset(&addrbuf, 0, addrbuflen); |
---|
3406 | n/a | SAS2SA(&addrbuf)->sa_family = AF_UNSPEC; |
---|
3407 | n/a | |
---|
3408 | n/a | if (controllen < 0 || controllen > SOCKLEN_T_LIMIT) { |
---|
3409 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
3410 | n/a | "invalid ancillary data buffer length"); |
---|
3411 | n/a | return NULL; |
---|
3412 | n/a | } |
---|
3413 | n/a | if (controllen > 0 && (controlbuf = PyMem_Malloc(controllen)) == NULL) |
---|
3414 | n/a | return PyErr_NoMemory(); |
---|
3415 | n/a | |
---|
3416 | n/a | /* Make the system call. */ |
---|
3417 | n/a | if (!IS_SELECTABLE(s)) { |
---|
3418 | n/a | select_error(); |
---|
3419 | n/a | goto finally; |
---|
3420 | n/a | } |
---|
3421 | n/a | |
---|
3422 | n/a | msg.msg_name = SAS2SA(&addrbuf); |
---|
3423 | n/a | msg.msg_namelen = addrbuflen; |
---|
3424 | n/a | msg.msg_iov = iov; |
---|
3425 | n/a | msg.msg_iovlen = iovlen; |
---|
3426 | n/a | msg.msg_control = controlbuf; |
---|
3427 | n/a | msg.msg_controllen = controllen; |
---|
3428 | n/a | |
---|
3429 | n/a | ctx.msg = &msg; |
---|
3430 | n/a | ctx.flags = flags; |
---|
3431 | n/a | if (sock_call(s, 0, sock_recvmsg_impl, &ctx) < 0) |
---|
3432 | n/a | goto finally; |
---|
3433 | n/a | |
---|
3434 | n/a | /* Make list of (level, type, data) tuples from control messages. */ |
---|
3435 | n/a | if ((cmsg_list = PyList_New(0)) == NULL) |
---|
3436 | n/a | goto err_closefds; |
---|
3437 | n/a | /* Check for empty ancillary data as old CMSG_FIRSTHDR() |
---|
3438 | n/a | implementations didn't do so. */ |
---|
3439 | n/a | for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL); |
---|
3440 | n/a | cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) { |
---|
3441 | n/a | PyObject *bytes, *tuple; |
---|
3442 | n/a | int tmp; |
---|
3443 | n/a | |
---|
3444 | n/a | cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen); |
---|
3445 | n/a | if (cmsg_status != 0) { |
---|
3446 | n/a | if (PyErr_WarnEx(PyExc_RuntimeWarning, |
---|
3447 | n/a | "received malformed or improperly-truncated " |
---|
3448 | n/a | "ancillary data", 1) == -1) |
---|
3449 | n/a | goto err_closefds; |
---|
3450 | n/a | } |
---|
3451 | n/a | if (cmsg_status < 0) |
---|
3452 | n/a | break; |
---|
3453 | n/a | if (cmsgdatalen > PY_SSIZE_T_MAX) { |
---|
3454 | n/a | PyErr_SetString(PyExc_OSError, "control message too long"); |
---|
3455 | n/a | goto err_closefds; |
---|
3456 | n/a | } |
---|
3457 | n/a | |
---|
3458 | n/a | bytes = PyBytes_FromStringAndSize((char *)CMSG_DATA(cmsgh), |
---|
3459 | n/a | cmsgdatalen); |
---|
3460 | n/a | tuple = Py_BuildValue("iiN", (int)cmsgh->cmsg_level, |
---|
3461 | n/a | (int)cmsgh->cmsg_type, bytes); |
---|
3462 | n/a | if (tuple == NULL) |
---|
3463 | n/a | goto err_closefds; |
---|
3464 | n/a | tmp = PyList_Append(cmsg_list, tuple); |
---|
3465 | n/a | Py_DECREF(tuple); |
---|
3466 | n/a | if (tmp != 0) |
---|
3467 | n/a | goto err_closefds; |
---|
3468 | n/a | |
---|
3469 | n/a | if (cmsg_status != 0) |
---|
3470 | n/a | break; |
---|
3471 | n/a | } |
---|
3472 | n/a | |
---|
3473 | n/a | retval = Py_BuildValue("NOiN", |
---|
3474 | n/a | (*makeval)(ctx.result, makeval_data), |
---|
3475 | n/a | cmsg_list, |
---|
3476 | n/a | (int)msg.msg_flags, |
---|
3477 | n/a | makesockaddr(s->sock_fd, SAS2SA(&addrbuf), |
---|
3478 | n/a | ((msg.msg_namelen > addrbuflen) ? |
---|
3479 | n/a | addrbuflen : msg.msg_namelen), |
---|
3480 | n/a | s->sock_proto)); |
---|
3481 | n/a | if (retval == NULL) |
---|
3482 | n/a | goto err_closefds; |
---|
3483 | n/a | |
---|
3484 | n/a | finally: |
---|
3485 | n/a | Py_XDECREF(cmsg_list); |
---|
3486 | n/a | PyMem_Free(controlbuf); |
---|
3487 | n/a | return retval; |
---|
3488 | n/a | |
---|
3489 | n/a | err_closefds: |
---|
3490 | n/a | #ifdef SCM_RIGHTS |
---|
3491 | n/a | /* Close all descriptors coming from SCM_RIGHTS, so they don't leak. */ |
---|
3492 | n/a | for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL); |
---|
3493 | n/a | cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) { |
---|
3494 | n/a | cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen); |
---|
3495 | n/a | if (cmsg_status < 0) |
---|
3496 | n/a | break; |
---|
3497 | n/a | if (cmsgh->cmsg_level == SOL_SOCKET && |
---|
3498 | n/a | cmsgh->cmsg_type == SCM_RIGHTS) { |
---|
3499 | n/a | size_t numfds; |
---|
3500 | n/a | int *fdp; |
---|
3501 | n/a | |
---|
3502 | n/a | numfds = cmsgdatalen / sizeof(int); |
---|
3503 | n/a | fdp = (int *)CMSG_DATA(cmsgh); |
---|
3504 | n/a | while (numfds-- > 0) |
---|
3505 | n/a | close(*fdp++); |
---|
3506 | n/a | } |
---|
3507 | n/a | if (cmsg_status != 0) |
---|
3508 | n/a | break; |
---|
3509 | n/a | } |
---|
3510 | n/a | #endif /* SCM_RIGHTS */ |
---|
3511 | n/a | goto finally; |
---|
3512 | n/a | } |
---|
3513 | n/a | |
---|
3514 | n/a | |
---|
3515 | n/a | static PyObject * |
---|
3516 | n/a | makeval_recvmsg(ssize_t received, void *data) |
---|
3517 | n/a | { |
---|
3518 | n/a | PyObject **buf = data; |
---|
3519 | n/a | |
---|
3520 | n/a | if (received < PyBytes_GET_SIZE(*buf)) |
---|
3521 | n/a | _PyBytes_Resize(buf, received); |
---|
3522 | n/a | Py_XINCREF(*buf); |
---|
3523 | n/a | return *buf; |
---|
3524 | n/a | } |
---|
3525 | n/a | |
---|
3526 | n/a | /* s.recvmsg(bufsize[, ancbufsize[, flags]]) method */ |
---|
3527 | n/a | |
---|
3528 | n/a | static PyObject * |
---|
3529 | n/a | sock_recvmsg(PySocketSockObject *s, PyObject *args) |
---|
3530 | n/a | { |
---|
3531 | n/a | Py_ssize_t bufsize, ancbufsize = 0; |
---|
3532 | n/a | int flags = 0; |
---|
3533 | n/a | struct iovec iov; |
---|
3534 | n/a | PyObject *buf = NULL, *retval = NULL; |
---|
3535 | n/a | |
---|
3536 | n/a | if (!PyArg_ParseTuple(args, "n|ni:recvmsg", &bufsize, &ancbufsize, &flags)) |
---|
3537 | n/a | return NULL; |
---|
3538 | n/a | |
---|
3539 | n/a | if (bufsize < 0) { |
---|
3540 | n/a | PyErr_SetString(PyExc_ValueError, "negative buffer size in recvmsg()"); |
---|
3541 | n/a | return NULL; |
---|
3542 | n/a | } |
---|
3543 | n/a | if ((buf = PyBytes_FromStringAndSize(NULL, bufsize)) == NULL) |
---|
3544 | n/a | return NULL; |
---|
3545 | n/a | iov.iov_base = PyBytes_AS_STRING(buf); |
---|
3546 | n/a | iov.iov_len = bufsize; |
---|
3547 | n/a | |
---|
3548 | n/a | /* Note that we're passing a pointer to *our pointer* to the bytes |
---|
3549 | n/a | object here (&buf); makeval_recvmsg() may incref the object, or |
---|
3550 | n/a | deallocate it and set our pointer to NULL. */ |
---|
3551 | n/a | retval = sock_recvmsg_guts(s, &iov, 1, flags, ancbufsize, |
---|
3552 | n/a | &makeval_recvmsg, &buf); |
---|
3553 | n/a | Py_XDECREF(buf); |
---|
3554 | n/a | return retval; |
---|
3555 | n/a | } |
---|
3556 | n/a | |
---|
3557 | n/a | PyDoc_STRVAR(recvmsg_doc, |
---|
3558 | n/a | "recvmsg(bufsize[, ancbufsize[, flags]]) -> (data, ancdata, msg_flags, address)\n\ |
---|
3559 | n/a | \n\ |
---|
3560 | n/a | Receive normal data (up to bufsize bytes) and ancillary data from the\n\ |
---|
3561 | n/a | socket. The ancbufsize argument sets the size in bytes of the\n\ |
---|
3562 | n/a | internal buffer used to receive the ancillary data; it defaults to 0,\n\ |
---|
3563 | n/a | meaning that no ancillary data will be received. Appropriate buffer\n\ |
---|
3564 | n/a | sizes for ancillary data can be calculated using CMSG_SPACE() or\n\ |
---|
3565 | n/a | CMSG_LEN(), and items which do not fit into the buffer might be\n\ |
---|
3566 | n/a | truncated or discarded. The flags argument defaults to 0 and has the\n\ |
---|
3567 | n/a | same meaning as for recv().\n\ |
---|
3568 | n/a | \n\ |
---|
3569 | n/a | The return value is a 4-tuple: (data, ancdata, msg_flags, address).\n\ |
---|
3570 | n/a | The data item is a bytes object holding the non-ancillary data\n\ |
---|
3571 | n/a | received. The ancdata item is a list of zero or more tuples\n\ |
---|
3572 | n/a | (cmsg_level, cmsg_type, cmsg_data) representing the ancillary data\n\ |
---|
3573 | n/a | (control messages) received: cmsg_level and cmsg_type are integers\n\ |
---|
3574 | n/a | specifying the protocol level and protocol-specific type respectively,\n\ |
---|
3575 | n/a | and cmsg_data is a bytes object holding the associated data. The\n\ |
---|
3576 | n/a | msg_flags item is the bitwise OR of various flags indicating\n\ |
---|
3577 | n/a | conditions on the received message; see your system documentation for\n\ |
---|
3578 | n/a | details. If the receiving socket is unconnected, address is the\n\ |
---|
3579 | n/a | address of the sending socket, if available; otherwise, its value is\n\ |
---|
3580 | n/a | unspecified.\n\ |
---|
3581 | n/a | \n\ |
---|
3582 | n/a | If recvmsg() raises an exception after the system call returns, it\n\ |
---|
3583 | n/a | will first attempt to close any file descriptors received via the\n\ |
---|
3584 | n/a | SCM_RIGHTS mechanism."); |
---|
3585 | n/a | |
---|
3586 | n/a | |
---|
3587 | n/a | static PyObject * |
---|
3588 | n/a | makeval_recvmsg_into(ssize_t received, void *data) |
---|
3589 | n/a | { |
---|
3590 | n/a | return PyLong_FromSsize_t(received); |
---|
3591 | n/a | } |
---|
3592 | n/a | |
---|
3593 | n/a | /* s.recvmsg_into(buffers[, ancbufsize[, flags]]) method */ |
---|
3594 | n/a | |
---|
3595 | n/a | static PyObject * |
---|
3596 | n/a | sock_recvmsg_into(PySocketSockObject *s, PyObject *args) |
---|
3597 | n/a | { |
---|
3598 | n/a | Py_ssize_t ancbufsize = 0; |
---|
3599 | n/a | int flags = 0; |
---|
3600 | n/a | struct iovec *iovs = NULL; |
---|
3601 | n/a | Py_ssize_t i, nitems, nbufs = 0; |
---|
3602 | n/a | Py_buffer *bufs = NULL; |
---|
3603 | n/a | PyObject *buffers_arg, *fast, *retval = NULL; |
---|
3604 | n/a | |
---|
3605 | n/a | if (!PyArg_ParseTuple(args, "O|ni:recvmsg_into", |
---|
3606 | n/a | &buffers_arg, &ancbufsize, &flags)) |
---|
3607 | n/a | return NULL; |
---|
3608 | n/a | |
---|
3609 | n/a | if ((fast = PySequence_Fast(buffers_arg, |
---|
3610 | n/a | "recvmsg_into() argument 1 must be an " |
---|
3611 | n/a | "iterable")) == NULL) |
---|
3612 | n/a | return NULL; |
---|
3613 | n/a | nitems = PySequence_Fast_GET_SIZE(fast); |
---|
3614 | n/a | if (nitems > INT_MAX) { |
---|
3615 | n/a | PyErr_SetString(PyExc_OSError, "recvmsg_into() argument 1 is too long"); |
---|
3616 | n/a | goto finally; |
---|
3617 | n/a | } |
---|
3618 | n/a | |
---|
3619 | n/a | /* Fill in an iovec for each item, and save the Py_buffer |
---|
3620 | n/a | structs to release afterwards. */ |
---|
3621 | n/a | if (nitems > 0 && ((iovs = PyMem_New(struct iovec, nitems)) == NULL || |
---|
3622 | n/a | (bufs = PyMem_New(Py_buffer, nitems)) == NULL)) { |
---|
3623 | n/a | PyErr_NoMemory(); |
---|
3624 | n/a | goto finally; |
---|
3625 | n/a | } |
---|
3626 | n/a | for (; nbufs < nitems; nbufs++) { |
---|
3627 | n/a | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(fast, nbufs), |
---|
3628 | n/a | "w*;recvmsg_into() argument 1 must be an iterable " |
---|
3629 | n/a | "of single-segment read-write buffers", |
---|
3630 | n/a | &bufs[nbufs])) |
---|
3631 | n/a | goto finally; |
---|
3632 | n/a | iovs[nbufs].iov_base = bufs[nbufs].buf; |
---|
3633 | n/a | iovs[nbufs].iov_len = bufs[nbufs].len; |
---|
3634 | n/a | } |
---|
3635 | n/a | |
---|
3636 | n/a | retval = sock_recvmsg_guts(s, iovs, nitems, flags, ancbufsize, |
---|
3637 | n/a | &makeval_recvmsg_into, NULL); |
---|
3638 | n/a | finally: |
---|
3639 | n/a | for (i = 0; i < nbufs; i++) |
---|
3640 | n/a | PyBuffer_Release(&bufs[i]); |
---|
3641 | n/a | PyMem_Free(bufs); |
---|
3642 | n/a | PyMem_Free(iovs); |
---|
3643 | n/a | Py_DECREF(fast); |
---|
3644 | n/a | return retval; |
---|
3645 | n/a | } |
---|
3646 | n/a | |
---|
3647 | n/a | PyDoc_STRVAR(recvmsg_into_doc, |
---|
3648 | n/a | "recvmsg_into(buffers[, ancbufsize[, flags]]) -> (nbytes, ancdata, msg_flags, address)\n\ |
---|
3649 | n/a | \n\ |
---|
3650 | n/a | Receive normal data and ancillary data from the socket, scattering the\n\ |
---|
3651 | n/a | non-ancillary data into a series of buffers. The buffers argument\n\ |
---|
3652 | n/a | must be an iterable of objects that export writable buffers\n\ |
---|
3653 | n/a | (e.g. bytearray objects); these will be filled with successive chunks\n\ |
---|
3654 | n/a | of the non-ancillary data until it has all been written or there are\n\ |
---|
3655 | n/a | no more buffers. The ancbufsize argument sets the size in bytes of\n\ |
---|
3656 | n/a | the internal buffer used to receive the ancillary data; it defaults to\n\ |
---|
3657 | n/a | 0, meaning that no ancillary data will be received. Appropriate\n\ |
---|
3658 | n/a | buffer sizes for ancillary data can be calculated using CMSG_SPACE()\n\ |
---|
3659 | n/a | or CMSG_LEN(), and items which do not fit into the buffer might be\n\ |
---|
3660 | n/a | truncated or discarded. The flags argument defaults to 0 and has the\n\ |
---|
3661 | n/a | same meaning as for recv().\n\ |
---|
3662 | n/a | \n\ |
---|
3663 | n/a | The return value is a 4-tuple: (nbytes, ancdata, msg_flags, address).\n\ |
---|
3664 | n/a | The nbytes item is the total number of bytes of non-ancillary data\n\ |
---|
3665 | n/a | written into the buffers. The ancdata item is a list of zero or more\n\ |
---|
3666 | n/a | tuples (cmsg_level, cmsg_type, cmsg_data) representing the ancillary\n\ |
---|
3667 | n/a | data (control messages) received: cmsg_level and cmsg_type are\n\ |
---|
3668 | n/a | integers specifying the protocol level and protocol-specific type\n\ |
---|
3669 | n/a | respectively, and cmsg_data is a bytes object holding the associated\n\ |
---|
3670 | n/a | data. The msg_flags item is the bitwise OR of various flags\n\ |
---|
3671 | n/a | indicating conditions on the received message; see your system\n\ |
---|
3672 | n/a | documentation for details. If the receiving socket is unconnected,\n\ |
---|
3673 | n/a | address is the address of the sending socket, if available; otherwise,\n\ |
---|
3674 | n/a | its value is unspecified.\n\ |
---|
3675 | n/a | \n\ |
---|
3676 | n/a | If recvmsg_into() raises an exception after the system call returns,\n\ |
---|
3677 | n/a | it will first attempt to close any file descriptors received via the\n\ |
---|
3678 | n/a | SCM_RIGHTS mechanism."); |
---|
3679 | n/a | #endif /* CMSG_LEN */ |
---|
3680 | n/a | |
---|
3681 | n/a | |
---|
3682 | n/a | struct sock_send { |
---|
3683 | n/a | char *buf; |
---|
3684 | n/a | Py_ssize_t len; |
---|
3685 | n/a | int flags; |
---|
3686 | n/a | Py_ssize_t result; |
---|
3687 | n/a | }; |
---|
3688 | n/a | |
---|
3689 | n/a | static int |
---|
3690 | n/a | sock_send_impl(PySocketSockObject *s, void *data) |
---|
3691 | n/a | { |
---|
3692 | n/a | struct sock_send *ctx = data; |
---|
3693 | n/a | |
---|
3694 | n/a | #ifdef MS_WINDOWS |
---|
3695 | n/a | if (ctx->len > INT_MAX) |
---|
3696 | n/a | ctx->len = INT_MAX; |
---|
3697 | n/a | ctx->result = send(s->sock_fd, ctx->buf, (int)ctx->len, ctx->flags); |
---|
3698 | n/a | #else |
---|
3699 | n/a | ctx->result = send(s->sock_fd, ctx->buf, ctx->len, ctx->flags); |
---|
3700 | n/a | #endif |
---|
3701 | n/a | return (ctx->result >= 0); |
---|
3702 | n/a | } |
---|
3703 | n/a | |
---|
3704 | n/a | /* s.send(data [,flags]) method */ |
---|
3705 | n/a | |
---|
3706 | n/a | static PyObject * |
---|
3707 | n/a | sock_send(PySocketSockObject *s, PyObject *args) |
---|
3708 | n/a | { |
---|
3709 | n/a | int flags = 0; |
---|
3710 | n/a | Py_buffer pbuf; |
---|
3711 | n/a | struct sock_send ctx; |
---|
3712 | n/a | |
---|
3713 | n/a | if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags)) |
---|
3714 | n/a | return NULL; |
---|
3715 | n/a | |
---|
3716 | n/a | if (!IS_SELECTABLE(s)) { |
---|
3717 | n/a | PyBuffer_Release(&pbuf); |
---|
3718 | n/a | return select_error(); |
---|
3719 | n/a | } |
---|
3720 | n/a | ctx.buf = pbuf.buf; |
---|
3721 | n/a | ctx.len = pbuf.len; |
---|
3722 | n/a | ctx.flags = flags; |
---|
3723 | n/a | if (sock_call(s, 1, sock_send_impl, &ctx) < 0) { |
---|
3724 | n/a | PyBuffer_Release(&pbuf); |
---|
3725 | n/a | return NULL; |
---|
3726 | n/a | } |
---|
3727 | n/a | PyBuffer_Release(&pbuf); |
---|
3728 | n/a | |
---|
3729 | n/a | return PyLong_FromSsize_t(ctx.result); |
---|
3730 | n/a | } |
---|
3731 | n/a | |
---|
3732 | n/a | PyDoc_STRVAR(send_doc, |
---|
3733 | n/a | "send(data[, flags]) -> count\n\ |
---|
3734 | n/a | \n\ |
---|
3735 | n/a | Send a data string to the socket. For the optional flags\n\ |
---|
3736 | n/a | argument, see the Unix manual. Return the number of bytes\n\ |
---|
3737 | n/a | sent; this may be less than len(data) if the network is busy."); |
---|
3738 | n/a | |
---|
3739 | n/a | |
---|
3740 | n/a | /* s.sendall(data [,flags]) method */ |
---|
3741 | n/a | |
---|
3742 | n/a | static PyObject * |
---|
3743 | n/a | sock_sendall(PySocketSockObject *s, PyObject *args) |
---|
3744 | n/a | { |
---|
3745 | n/a | char *buf; |
---|
3746 | n/a | Py_ssize_t len, n; |
---|
3747 | n/a | int flags = 0; |
---|
3748 | n/a | Py_buffer pbuf; |
---|
3749 | n/a | struct sock_send ctx; |
---|
3750 | n/a | int has_timeout = (s->sock_timeout > 0); |
---|
3751 | n/a | _PyTime_t interval = s->sock_timeout; |
---|
3752 | n/a | _PyTime_t deadline = 0; |
---|
3753 | n/a | int deadline_initialized = 0; |
---|
3754 | n/a | PyObject *res = NULL; |
---|
3755 | n/a | |
---|
3756 | n/a | if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags)) |
---|
3757 | n/a | return NULL; |
---|
3758 | n/a | buf = pbuf.buf; |
---|
3759 | n/a | len = pbuf.len; |
---|
3760 | n/a | |
---|
3761 | n/a | if (!IS_SELECTABLE(s)) { |
---|
3762 | n/a | PyBuffer_Release(&pbuf); |
---|
3763 | n/a | return select_error(); |
---|
3764 | n/a | } |
---|
3765 | n/a | |
---|
3766 | n/a | do { |
---|
3767 | n/a | if (has_timeout) { |
---|
3768 | n/a | if (deadline_initialized) { |
---|
3769 | n/a | /* recompute the timeout */ |
---|
3770 | n/a | interval = deadline - _PyTime_GetMonotonicClock(); |
---|
3771 | n/a | } |
---|
3772 | n/a | else { |
---|
3773 | n/a | deadline_initialized = 1; |
---|
3774 | n/a | deadline = _PyTime_GetMonotonicClock() + s->sock_timeout; |
---|
3775 | n/a | } |
---|
3776 | n/a | |
---|
3777 | n/a | if (interval <= 0) { |
---|
3778 | n/a | PyErr_SetString(socket_timeout, "timed out"); |
---|
3779 | n/a | goto done; |
---|
3780 | n/a | } |
---|
3781 | n/a | } |
---|
3782 | n/a | |
---|
3783 | n/a | ctx.buf = buf; |
---|
3784 | n/a | ctx.len = len; |
---|
3785 | n/a | ctx.flags = flags; |
---|
3786 | n/a | if (sock_call_ex(s, 1, sock_send_impl, &ctx, 0, NULL, interval) < 0) |
---|
3787 | n/a | goto done; |
---|
3788 | n/a | n = ctx.result; |
---|
3789 | n/a | assert(n >= 0); |
---|
3790 | n/a | |
---|
3791 | n/a | buf += n; |
---|
3792 | n/a | len -= n; |
---|
3793 | n/a | |
---|
3794 | n/a | /* We must run our signal handlers before looping again. |
---|
3795 | n/a | send() can return a successful partial write when it is |
---|
3796 | n/a | interrupted, so we can't restrict ourselves to EINTR. */ |
---|
3797 | n/a | if (PyErr_CheckSignals()) |
---|
3798 | n/a | goto done; |
---|
3799 | n/a | } while (len > 0); |
---|
3800 | n/a | PyBuffer_Release(&pbuf); |
---|
3801 | n/a | |
---|
3802 | n/a | Py_INCREF(Py_None); |
---|
3803 | n/a | res = Py_None; |
---|
3804 | n/a | |
---|
3805 | n/a | done: |
---|
3806 | n/a | PyBuffer_Release(&pbuf); |
---|
3807 | n/a | return res; |
---|
3808 | n/a | } |
---|
3809 | n/a | |
---|
3810 | n/a | PyDoc_STRVAR(sendall_doc, |
---|
3811 | n/a | "sendall(data[, flags])\n\ |
---|
3812 | n/a | \n\ |
---|
3813 | n/a | Send a data string to the socket. For the optional flags\n\ |
---|
3814 | n/a | argument, see the Unix manual. This calls send() repeatedly\n\ |
---|
3815 | n/a | until all data is sent. If an error occurs, it's impossible\n\ |
---|
3816 | n/a | to tell how much data has been sent."); |
---|
3817 | n/a | |
---|
3818 | n/a | |
---|
3819 | n/a | struct sock_sendto { |
---|
3820 | n/a | char *buf; |
---|
3821 | n/a | Py_ssize_t len; |
---|
3822 | n/a | int flags; |
---|
3823 | n/a | int addrlen; |
---|
3824 | n/a | sock_addr_t *addrbuf; |
---|
3825 | n/a | Py_ssize_t result; |
---|
3826 | n/a | }; |
---|
3827 | n/a | |
---|
3828 | n/a | static int |
---|
3829 | n/a | sock_sendto_impl(PySocketSockObject *s, void *data) |
---|
3830 | n/a | { |
---|
3831 | n/a | struct sock_sendto *ctx = data; |
---|
3832 | n/a | |
---|
3833 | n/a | #ifdef MS_WINDOWS |
---|
3834 | n/a | if (ctx->len > INT_MAX) |
---|
3835 | n/a | ctx->len = INT_MAX; |
---|
3836 | n/a | ctx->result = sendto(s->sock_fd, ctx->buf, (int)ctx->len, ctx->flags, |
---|
3837 | n/a | SAS2SA(ctx->addrbuf), ctx->addrlen); |
---|
3838 | n/a | #else |
---|
3839 | n/a | ctx->result = sendto(s->sock_fd, ctx->buf, ctx->len, ctx->flags, |
---|
3840 | n/a | SAS2SA(ctx->addrbuf), ctx->addrlen); |
---|
3841 | n/a | #endif |
---|
3842 | n/a | return (ctx->result >= 0); |
---|
3843 | n/a | } |
---|
3844 | n/a | |
---|
3845 | n/a | /* s.sendto(data, [flags,] sockaddr) method */ |
---|
3846 | n/a | |
---|
3847 | n/a | static PyObject * |
---|
3848 | n/a | sock_sendto(PySocketSockObject *s, PyObject *args) |
---|
3849 | n/a | { |
---|
3850 | n/a | Py_buffer pbuf; |
---|
3851 | n/a | PyObject *addro; |
---|
3852 | n/a | Py_ssize_t arglen; |
---|
3853 | n/a | sock_addr_t addrbuf; |
---|
3854 | n/a | int addrlen, flags; |
---|
3855 | n/a | struct sock_sendto ctx; |
---|
3856 | n/a | |
---|
3857 | n/a | flags = 0; |
---|
3858 | n/a | arglen = PyTuple_Size(args); |
---|
3859 | n/a | switch (arglen) { |
---|
3860 | n/a | case 2: |
---|
3861 | n/a | if (!PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro)) { |
---|
3862 | n/a | return NULL; |
---|
3863 | n/a | } |
---|
3864 | n/a | break; |
---|
3865 | n/a | case 3: |
---|
3866 | n/a | if (!PyArg_ParseTuple(args, "y*iO:sendto", |
---|
3867 | n/a | &pbuf, &flags, &addro)) { |
---|
3868 | n/a | return NULL; |
---|
3869 | n/a | } |
---|
3870 | n/a | break; |
---|
3871 | n/a | default: |
---|
3872 | n/a | PyErr_Format(PyExc_TypeError, |
---|
3873 | n/a | "sendto() takes 2 or 3 arguments (%d given)", |
---|
3874 | n/a | arglen); |
---|
3875 | n/a | return NULL; |
---|
3876 | n/a | } |
---|
3877 | n/a | |
---|
3878 | n/a | if (!IS_SELECTABLE(s)) { |
---|
3879 | n/a | PyBuffer_Release(&pbuf); |
---|
3880 | n/a | return select_error(); |
---|
3881 | n/a | } |
---|
3882 | n/a | |
---|
3883 | n/a | if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) { |
---|
3884 | n/a | PyBuffer_Release(&pbuf); |
---|
3885 | n/a | return NULL; |
---|
3886 | n/a | } |
---|
3887 | n/a | |
---|
3888 | n/a | ctx.buf = pbuf.buf; |
---|
3889 | n/a | ctx.len = pbuf.len; |
---|
3890 | n/a | ctx.flags = flags; |
---|
3891 | n/a | ctx.addrlen = addrlen; |
---|
3892 | n/a | ctx.addrbuf = &addrbuf; |
---|
3893 | n/a | if (sock_call(s, 1, sock_sendto_impl, &ctx) < 0) { |
---|
3894 | n/a | PyBuffer_Release(&pbuf); |
---|
3895 | n/a | return NULL; |
---|
3896 | n/a | } |
---|
3897 | n/a | PyBuffer_Release(&pbuf); |
---|
3898 | n/a | |
---|
3899 | n/a | return PyLong_FromSsize_t(ctx.result); |
---|
3900 | n/a | } |
---|
3901 | n/a | |
---|
3902 | n/a | PyDoc_STRVAR(sendto_doc, |
---|
3903 | n/a | "sendto(data[, flags], address) -> count\n\ |
---|
3904 | n/a | \n\ |
---|
3905 | n/a | Like send(data, flags) but allows specifying the destination address.\n\ |
---|
3906 | n/a | For IP sockets, the address is a pair (hostaddr, port)."); |
---|
3907 | n/a | |
---|
3908 | n/a | |
---|
3909 | n/a | /* The sendmsg() and recvmsg[_into]() methods require a working |
---|
3910 | n/a | CMSG_LEN(). See the comment near get_CMSG_LEN(). */ |
---|
3911 | n/a | #ifdef CMSG_LEN |
---|
3912 | n/a | struct sock_sendmsg { |
---|
3913 | n/a | struct msghdr *msg; |
---|
3914 | n/a | int flags; |
---|
3915 | n/a | ssize_t result; |
---|
3916 | n/a | }; |
---|
3917 | n/a | |
---|
3918 | n/a | static int |
---|
3919 | n/a | sock_sendmsg_iovec(PySocketSockObject *s, PyObject *data_arg, |
---|
3920 | n/a | struct msghdr *msg, |
---|
3921 | n/a | Py_buffer **databufsout, Py_ssize_t *ndatabufsout) { |
---|
3922 | n/a | Py_ssize_t ndataparts, ndatabufs = 0; |
---|
3923 | n/a | int result = -1; |
---|
3924 | n/a | struct iovec *iovs = NULL; |
---|
3925 | n/a | PyObject *data_fast = NULL; |
---|
3926 | n/a | Py_buffer *databufs = NULL; |
---|
3927 | n/a | |
---|
3928 | n/a | /* Fill in an iovec for each message part, and save the Py_buffer |
---|
3929 | n/a | structs to release afterwards. */ |
---|
3930 | n/a | data_fast = PySequence_Fast(data_arg, |
---|
3931 | n/a | "sendmsg() argument 1 must be an " |
---|
3932 | n/a | "iterable"); |
---|
3933 | n/a | if (data_fast == NULL) { |
---|
3934 | n/a | goto finally; |
---|
3935 | n/a | } |
---|
3936 | n/a | |
---|
3937 | n/a | ndataparts = PySequence_Fast_GET_SIZE(data_fast); |
---|
3938 | n/a | if (ndataparts > INT_MAX) { |
---|
3939 | n/a | PyErr_SetString(PyExc_OSError, "sendmsg() argument 1 is too long"); |
---|
3940 | n/a | goto finally; |
---|
3941 | n/a | } |
---|
3942 | n/a | |
---|
3943 | n/a | msg->msg_iovlen = ndataparts; |
---|
3944 | n/a | if (ndataparts > 0) { |
---|
3945 | n/a | iovs = PyMem_New(struct iovec, ndataparts); |
---|
3946 | n/a | if (iovs == NULL) { |
---|
3947 | n/a | PyErr_NoMemory(); |
---|
3948 | n/a | goto finally; |
---|
3949 | n/a | } |
---|
3950 | n/a | msg->msg_iov = iovs; |
---|
3951 | n/a | |
---|
3952 | n/a | databufs = PyMem_New(Py_buffer, ndataparts); |
---|
3953 | n/a | if (databufs == NULL) { |
---|
3954 | n/a | PyErr_NoMemory(); |
---|
3955 | n/a | goto finally; |
---|
3956 | n/a | } |
---|
3957 | n/a | } |
---|
3958 | n/a | for (; ndatabufs < ndataparts; ndatabufs++) { |
---|
3959 | n/a | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(data_fast, ndatabufs), |
---|
3960 | n/a | "y*;sendmsg() argument 1 must be an iterable of " |
---|
3961 | n/a | "bytes-like objects", |
---|
3962 | n/a | &databufs[ndatabufs])) |
---|
3963 | n/a | goto finally; |
---|
3964 | n/a | iovs[ndatabufs].iov_base = databufs[ndatabufs].buf; |
---|
3965 | n/a | iovs[ndatabufs].iov_len = databufs[ndatabufs].len; |
---|
3966 | n/a | } |
---|
3967 | n/a | result = 0; |
---|
3968 | n/a | finally: |
---|
3969 | n/a | *databufsout = databufs; |
---|
3970 | n/a | *ndatabufsout = ndatabufs; |
---|
3971 | n/a | Py_XDECREF(data_fast); |
---|
3972 | n/a | return result; |
---|
3973 | n/a | } |
---|
3974 | n/a | |
---|
3975 | n/a | static int |
---|
3976 | n/a | sock_sendmsg_impl(PySocketSockObject *s, void *data) |
---|
3977 | n/a | { |
---|
3978 | n/a | struct sock_sendmsg *ctx = data; |
---|
3979 | n/a | |
---|
3980 | n/a | ctx->result = sendmsg(s->sock_fd, ctx->msg, ctx->flags); |
---|
3981 | n/a | return (ctx->result >= 0); |
---|
3982 | n/a | } |
---|
3983 | n/a | |
---|
3984 | n/a | /* s.sendmsg(buffers[, ancdata[, flags[, address]]]) method */ |
---|
3985 | n/a | |
---|
3986 | n/a | static PyObject * |
---|
3987 | n/a | sock_sendmsg(PySocketSockObject *s, PyObject *args) |
---|
3988 | n/a | { |
---|
3989 | n/a | Py_ssize_t i, ndatabufs = 0, ncmsgs, ncmsgbufs = 0; |
---|
3990 | n/a | Py_buffer *databufs = NULL; |
---|
3991 | n/a | sock_addr_t addrbuf; |
---|
3992 | n/a | struct msghdr msg; |
---|
3993 | n/a | struct cmsginfo { |
---|
3994 | n/a | int level; |
---|
3995 | n/a | int type; |
---|
3996 | n/a | Py_buffer data; |
---|
3997 | n/a | } *cmsgs = NULL; |
---|
3998 | n/a | void *controlbuf = NULL; |
---|
3999 | n/a | size_t controllen, controllen_last; |
---|
4000 | n/a | int addrlen, flags = 0; |
---|
4001 | n/a | PyObject *data_arg, *cmsg_arg = NULL, *addr_arg = NULL, |
---|
4002 | n/a | *cmsg_fast = NULL, *retval = NULL; |
---|
4003 | n/a | struct sock_sendmsg ctx; |
---|
4004 | n/a | |
---|
4005 | n/a | if (!PyArg_ParseTuple(args, "O|OiO:sendmsg", |
---|
4006 | n/a | &data_arg, &cmsg_arg, &flags, &addr_arg)) { |
---|
4007 | n/a | return NULL; |
---|
4008 | n/a | } |
---|
4009 | n/a | |
---|
4010 | n/a | memset(&msg, 0, sizeof(msg)); |
---|
4011 | n/a | |
---|
4012 | n/a | /* Parse destination address. */ |
---|
4013 | n/a | if (addr_arg != NULL && addr_arg != Py_None) { |
---|
4014 | n/a | if (!getsockaddrarg(s, addr_arg, SAS2SA(&addrbuf), &addrlen)) |
---|
4015 | n/a | goto finally; |
---|
4016 | n/a | msg.msg_name = &addrbuf; |
---|
4017 | n/a | msg.msg_namelen = addrlen; |
---|
4018 | n/a | } |
---|
4019 | n/a | |
---|
4020 | n/a | /* Fill in an iovec for each message part, and save the Py_buffer |
---|
4021 | n/a | structs to release afterwards. */ |
---|
4022 | n/a | if (sock_sendmsg_iovec(s, data_arg, &msg, &databufs, &ndatabufs) == -1) { |
---|
4023 | n/a | goto finally; |
---|
4024 | n/a | } |
---|
4025 | n/a | |
---|
4026 | n/a | if (cmsg_arg == NULL) |
---|
4027 | n/a | ncmsgs = 0; |
---|
4028 | n/a | else { |
---|
4029 | n/a | if ((cmsg_fast = PySequence_Fast(cmsg_arg, |
---|
4030 | n/a | "sendmsg() argument 2 must be an " |
---|
4031 | n/a | "iterable")) == NULL) |
---|
4032 | n/a | goto finally; |
---|
4033 | n/a | ncmsgs = PySequence_Fast_GET_SIZE(cmsg_fast); |
---|
4034 | n/a | } |
---|
4035 | n/a | |
---|
4036 | n/a | #ifndef CMSG_SPACE |
---|
4037 | n/a | if (ncmsgs > 1) { |
---|
4038 | n/a | PyErr_SetString(PyExc_OSError, |
---|
4039 | n/a | "sending multiple control messages is not supported " |
---|
4040 | n/a | "on this system"); |
---|
4041 | n/a | goto finally; |
---|
4042 | n/a | } |
---|
4043 | n/a | #endif |
---|
4044 | n/a | /* Save level, type and Py_buffer for each control message, |
---|
4045 | n/a | and calculate total size. */ |
---|
4046 | n/a | if (ncmsgs > 0 && (cmsgs = PyMem_New(struct cmsginfo, ncmsgs)) == NULL) { |
---|
4047 | n/a | PyErr_NoMemory(); |
---|
4048 | n/a | goto finally; |
---|
4049 | n/a | } |
---|
4050 | n/a | controllen = controllen_last = 0; |
---|
4051 | n/a | while (ncmsgbufs < ncmsgs) { |
---|
4052 | n/a | size_t bufsize, space; |
---|
4053 | n/a | |
---|
4054 | n/a | if (!PyArg_Parse(PySequence_Fast_GET_ITEM(cmsg_fast, ncmsgbufs), |
---|
4055 | n/a | "(iiy*):[sendmsg() ancillary data items]", |
---|
4056 | n/a | &cmsgs[ncmsgbufs].level, |
---|
4057 | n/a | &cmsgs[ncmsgbufs].type, |
---|
4058 | n/a | &cmsgs[ncmsgbufs].data)) |
---|
4059 | n/a | goto finally; |
---|
4060 | n/a | bufsize = cmsgs[ncmsgbufs++].data.len; |
---|
4061 | n/a | |
---|
4062 | n/a | #ifdef CMSG_SPACE |
---|
4063 | n/a | if (!get_CMSG_SPACE(bufsize, &space)) { |
---|
4064 | n/a | #else |
---|
4065 | n/a | if (!get_CMSG_LEN(bufsize, &space)) { |
---|
4066 | n/a | #endif |
---|
4067 | n/a | PyErr_SetString(PyExc_OSError, "ancillary data item too large"); |
---|
4068 | n/a | goto finally; |
---|
4069 | n/a | } |
---|
4070 | n/a | controllen += space; |
---|
4071 | n/a | if (controllen > SOCKLEN_T_LIMIT || controllen < controllen_last) { |
---|
4072 | n/a | PyErr_SetString(PyExc_OSError, "too much ancillary data"); |
---|
4073 | n/a | goto finally; |
---|
4074 | n/a | } |
---|
4075 | n/a | controllen_last = controllen; |
---|
4076 | n/a | } |
---|
4077 | n/a | |
---|
4078 | n/a | /* Construct ancillary data block from control message info. */ |
---|
4079 | n/a | if (ncmsgbufs > 0) { |
---|
4080 | n/a | struct cmsghdr *cmsgh = NULL; |
---|
4081 | n/a | |
---|
4082 | n/a | controlbuf = PyMem_Malloc(controllen); |
---|
4083 | n/a | if (controlbuf == NULL) { |
---|
4084 | n/a | PyErr_NoMemory(); |
---|
4085 | n/a | goto finally; |
---|
4086 | n/a | } |
---|
4087 | n/a | msg.msg_control = controlbuf; |
---|
4088 | n/a | |
---|
4089 | n/a | msg.msg_controllen = controllen; |
---|
4090 | n/a | |
---|
4091 | n/a | /* Need to zero out the buffer as a workaround for glibc's |
---|
4092 | n/a | CMSG_NXTHDR() implementation. After getting the pointer to |
---|
4093 | n/a | the next header, it checks its (uninitialized) cmsg_len |
---|
4094 | n/a | member to see if the "message" fits in the buffer, and |
---|
4095 | n/a | returns NULL if it doesn't. Zero-filling the buffer |
---|
4096 | n/a | ensures that this doesn't happen. */ |
---|
4097 | n/a | memset(controlbuf, 0, controllen); |
---|
4098 | n/a | |
---|
4099 | n/a | for (i = 0; i < ncmsgbufs; i++) { |
---|
4100 | n/a | size_t msg_len, data_len = cmsgs[i].data.len; |
---|
4101 | n/a | int enough_space = 0; |
---|
4102 | n/a | |
---|
4103 | n/a | cmsgh = (i == 0) ? CMSG_FIRSTHDR(&msg) : CMSG_NXTHDR(&msg, cmsgh); |
---|
4104 | n/a | if (cmsgh == NULL) { |
---|
4105 | n/a | PyErr_Format(PyExc_RuntimeError, |
---|
4106 | n/a | "unexpected NULL result from %s()", |
---|
4107 | n/a | (i == 0) ? "CMSG_FIRSTHDR" : "CMSG_NXTHDR"); |
---|
4108 | n/a | goto finally; |
---|
4109 | n/a | } |
---|
4110 | n/a | if (!get_CMSG_LEN(data_len, &msg_len)) { |
---|
4111 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
4112 | n/a | "item size out of range for CMSG_LEN()"); |
---|
4113 | n/a | goto finally; |
---|
4114 | n/a | } |
---|
4115 | n/a | if (cmsg_min_space(&msg, cmsgh, msg_len)) { |
---|
4116 | n/a | size_t space; |
---|
4117 | n/a | |
---|
4118 | n/a | cmsgh->cmsg_len = msg_len; |
---|
4119 | n/a | if (get_cmsg_data_space(&msg, cmsgh, &space)) |
---|
4120 | n/a | enough_space = (space >= data_len); |
---|
4121 | n/a | } |
---|
4122 | n/a | if (!enough_space) { |
---|
4123 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
4124 | n/a | "ancillary data does not fit in calculated " |
---|
4125 | n/a | "space"); |
---|
4126 | n/a | goto finally; |
---|
4127 | n/a | } |
---|
4128 | n/a | cmsgh->cmsg_level = cmsgs[i].level; |
---|
4129 | n/a | cmsgh->cmsg_type = cmsgs[i].type; |
---|
4130 | n/a | memcpy(CMSG_DATA(cmsgh), cmsgs[i].data.buf, data_len); |
---|
4131 | n/a | } |
---|
4132 | n/a | } |
---|
4133 | n/a | |
---|
4134 | n/a | /* Make the system call. */ |
---|
4135 | n/a | if (!IS_SELECTABLE(s)) { |
---|
4136 | n/a | select_error(); |
---|
4137 | n/a | goto finally; |
---|
4138 | n/a | } |
---|
4139 | n/a | |
---|
4140 | n/a | ctx.msg = &msg; |
---|
4141 | n/a | ctx.flags = flags; |
---|
4142 | n/a | if (sock_call(s, 1, sock_sendmsg_impl, &ctx) < 0) |
---|
4143 | n/a | goto finally; |
---|
4144 | n/a | |
---|
4145 | n/a | retval = PyLong_FromSsize_t(ctx.result); |
---|
4146 | n/a | |
---|
4147 | n/a | finally: |
---|
4148 | n/a | PyMem_Free(controlbuf); |
---|
4149 | n/a | for (i = 0; i < ncmsgbufs; i++) |
---|
4150 | n/a | PyBuffer_Release(&cmsgs[i].data); |
---|
4151 | n/a | PyMem_Free(cmsgs); |
---|
4152 | n/a | Py_XDECREF(cmsg_fast); |
---|
4153 | n/a | PyMem_Free(msg.msg_iov); |
---|
4154 | n/a | for (i = 0; i < ndatabufs; i++) { |
---|
4155 | n/a | PyBuffer_Release(&databufs[i]); |
---|
4156 | n/a | } |
---|
4157 | n/a | PyMem_Free(databufs); |
---|
4158 | n/a | return retval; |
---|
4159 | n/a | } |
---|
4160 | n/a | |
---|
4161 | n/a | PyDoc_STRVAR(sendmsg_doc, |
---|
4162 | n/a | "sendmsg(buffers[, ancdata[, flags[, address]]]) -> count\n\ |
---|
4163 | n/a | \n\ |
---|
4164 | n/a | Send normal and ancillary data to the socket, gathering the\n\ |
---|
4165 | n/a | non-ancillary data from a series of buffers and concatenating it into\n\ |
---|
4166 | n/a | a single message. The buffers argument specifies the non-ancillary\n\ |
---|
4167 | n/a | data as an iterable of bytes-like objects (e.g. bytes objects).\n\ |
---|
4168 | n/a | The ancdata argument specifies the ancillary data (control messages)\n\ |
---|
4169 | n/a | as an iterable of zero or more tuples (cmsg_level, cmsg_type,\n\ |
---|
4170 | n/a | cmsg_data), where cmsg_level and cmsg_type are integers specifying the\n\ |
---|
4171 | n/a | protocol level and protocol-specific type respectively, and cmsg_data\n\ |
---|
4172 | n/a | is a bytes-like object holding the associated data. The flags\n\ |
---|
4173 | n/a | argument defaults to 0 and has the same meaning as for send(). If\n\ |
---|
4174 | n/a | address is supplied and not None, it sets a destination address for\n\ |
---|
4175 | n/a | the message. The return value is the number of bytes of non-ancillary\n\ |
---|
4176 | n/a | data sent."); |
---|
4177 | n/a | #endif /* CMSG_LEN */ |
---|
4178 | n/a | |
---|
4179 | n/a | #ifdef HAVE_SOCKADDR_ALG |
---|
4180 | n/a | static PyObject* |
---|
4181 | n/a | sock_sendmsg_afalg(PySocketSockObject *self, PyObject *args, PyObject *kwds) |
---|
4182 | n/a | { |
---|
4183 | n/a | PyObject *retval = NULL; |
---|
4184 | n/a | |
---|
4185 | n/a | Py_ssize_t i, ndatabufs = 0; |
---|
4186 | n/a | Py_buffer *databufs = NULL; |
---|
4187 | n/a | PyObject *data_arg = NULL; |
---|
4188 | n/a | |
---|
4189 | n/a | Py_buffer iv = {NULL, NULL}; |
---|
4190 | n/a | |
---|
4191 | n/a | PyObject *opobj = NULL; |
---|
4192 | n/a | int op = -1; |
---|
4193 | n/a | |
---|
4194 | n/a | PyObject *assoclenobj = NULL; |
---|
4195 | n/a | int assoclen = -1; |
---|
4196 | n/a | |
---|
4197 | n/a | unsigned int *uiptr; |
---|
4198 | n/a | int flags = 0; |
---|
4199 | n/a | |
---|
4200 | n/a | struct msghdr msg; |
---|
4201 | n/a | struct cmsghdr *header = NULL; |
---|
4202 | n/a | struct af_alg_iv *alg_iv = NULL; |
---|
4203 | n/a | struct sock_sendmsg ctx; |
---|
4204 | n/a | Py_ssize_t controllen; |
---|
4205 | n/a | void *controlbuf = NULL; |
---|
4206 | n/a | static char *keywords[] = {"msg", "op", "iv", "assoclen", "flags", 0}; |
---|
4207 | n/a | |
---|
4208 | n/a | if (self->sock_family != AF_ALG) { |
---|
4209 | n/a | PyErr_SetString(PyExc_OSError, |
---|
4210 | n/a | "algset is only supported for AF_ALG"); |
---|
4211 | n/a | return NULL; |
---|
4212 | n/a | } |
---|
4213 | n/a | |
---|
4214 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
---|
4215 | n/a | "|O$O!y*O!i:sendmsg_afalg", keywords, |
---|
4216 | n/a | &data_arg, |
---|
4217 | n/a | &PyLong_Type, &opobj, &iv, |
---|
4218 | n/a | &PyLong_Type, &assoclenobj, &flags)) { |
---|
4219 | n/a | return NULL; |
---|
4220 | n/a | } |
---|
4221 | n/a | |
---|
4222 | n/a | memset(&msg, 0, sizeof(msg)); |
---|
4223 | n/a | |
---|
4224 | n/a | /* op is a required, keyword-only argument >= 0 */ |
---|
4225 | n/a | if (opobj != NULL) { |
---|
4226 | n/a | op = _PyLong_AsInt(opobj); |
---|
4227 | n/a | } |
---|
4228 | n/a | if (op < 0) { |
---|
4229 | n/a | /* override exception from _PyLong_AsInt() */ |
---|
4230 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
4231 | n/a | "Invalid or missing argument 'op'"); |
---|
4232 | n/a | goto finally; |
---|
4233 | n/a | } |
---|
4234 | n/a | /* assoclen is optional but must be >= 0 */ |
---|
4235 | n/a | if (assoclenobj != NULL) { |
---|
4236 | n/a | assoclen = _PyLong_AsInt(assoclenobj); |
---|
4237 | n/a | if (assoclen == -1 && PyErr_Occurred()) { |
---|
4238 | n/a | goto finally; |
---|
4239 | n/a | } |
---|
4240 | n/a | if (assoclen < 0) { |
---|
4241 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
4242 | n/a | "assoclen must be positive"); |
---|
4243 | n/a | goto finally; |
---|
4244 | n/a | } |
---|
4245 | n/a | } |
---|
4246 | n/a | |
---|
4247 | n/a | controllen = CMSG_SPACE(4); |
---|
4248 | n/a | if (iv.buf != NULL) { |
---|
4249 | n/a | controllen += CMSG_SPACE(sizeof(*alg_iv) + iv.len); |
---|
4250 | n/a | } |
---|
4251 | n/a | if (assoclen >= 0) { |
---|
4252 | n/a | controllen += CMSG_SPACE(4); |
---|
4253 | n/a | } |
---|
4254 | n/a | |
---|
4255 | n/a | controlbuf = PyMem_Malloc(controllen); |
---|
4256 | n/a | if (controlbuf == NULL) { |
---|
4257 | n/a | PyErr_NoMemory(); |
---|
4258 | n/a | goto finally; |
---|
4259 | n/a | } |
---|
4260 | n/a | memset(controlbuf, 0, controllen); |
---|
4261 | n/a | |
---|
4262 | n/a | msg.msg_controllen = controllen; |
---|
4263 | n/a | msg.msg_control = controlbuf; |
---|
4264 | n/a | |
---|
4265 | n/a | /* Fill in an iovec for each message part, and save the Py_buffer |
---|
4266 | n/a | structs to release afterwards. */ |
---|
4267 | n/a | if (data_arg != NULL) { |
---|
4268 | n/a | if (sock_sendmsg_iovec(self, data_arg, &msg, &databufs, &ndatabufs) == -1) { |
---|
4269 | n/a | goto finally; |
---|
4270 | n/a | } |
---|
4271 | n/a | } |
---|
4272 | n/a | |
---|
4273 | n/a | /* set operation to encrypt or decrypt */ |
---|
4274 | n/a | header = CMSG_FIRSTHDR(&msg); |
---|
4275 | n/a | if (header == NULL) { |
---|
4276 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
4277 | n/a | "unexpected NULL result from CMSG_FIRSTHDR"); |
---|
4278 | n/a | goto finally; |
---|
4279 | n/a | } |
---|
4280 | n/a | header->cmsg_level = SOL_ALG; |
---|
4281 | n/a | header->cmsg_type = ALG_SET_OP; |
---|
4282 | n/a | header->cmsg_len = CMSG_LEN(4); |
---|
4283 | n/a | uiptr = (void*)CMSG_DATA(header); |
---|
4284 | n/a | *uiptr = (unsigned int)op; |
---|
4285 | n/a | |
---|
4286 | n/a | /* set initialization vector */ |
---|
4287 | n/a | if (iv.buf != NULL) { |
---|
4288 | n/a | header = CMSG_NXTHDR(&msg, header); |
---|
4289 | n/a | if (header == NULL) { |
---|
4290 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
4291 | n/a | "unexpected NULL result from CMSG_NXTHDR(iv)"); |
---|
4292 | n/a | goto finally; |
---|
4293 | n/a | } |
---|
4294 | n/a | header->cmsg_level = SOL_ALG; |
---|
4295 | n/a | header->cmsg_type = ALG_SET_IV; |
---|
4296 | n/a | header->cmsg_len = CMSG_SPACE(sizeof(*alg_iv) + iv.len); |
---|
4297 | n/a | alg_iv = (void*)CMSG_DATA(header); |
---|
4298 | n/a | alg_iv->ivlen = iv.len; |
---|
4299 | n/a | memcpy(alg_iv->iv, iv.buf, iv.len); |
---|
4300 | n/a | } |
---|
4301 | n/a | |
---|
4302 | n/a | /* set length of associated data for AEAD */ |
---|
4303 | n/a | if (assoclen >= 0) { |
---|
4304 | n/a | header = CMSG_NXTHDR(&msg, header); |
---|
4305 | n/a | if (header == NULL) { |
---|
4306 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
4307 | n/a | "unexpected NULL result from CMSG_NXTHDR(assoc)"); |
---|
4308 | n/a | goto finally; |
---|
4309 | n/a | } |
---|
4310 | n/a | header->cmsg_level = SOL_ALG; |
---|
4311 | n/a | header->cmsg_type = ALG_SET_AEAD_ASSOCLEN; |
---|
4312 | n/a | header->cmsg_len = CMSG_LEN(4); |
---|
4313 | n/a | uiptr = (void*)CMSG_DATA(header); |
---|
4314 | n/a | *uiptr = (unsigned int)assoclen; |
---|
4315 | n/a | } |
---|
4316 | n/a | |
---|
4317 | n/a | ctx.msg = &msg; |
---|
4318 | n/a | ctx.flags = flags; |
---|
4319 | n/a | if (sock_call(self, 1, sock_sendmsg_impl, &ctx) < 0) { |
---|
4320 | n/a | goto finally; |
---|
4321 | n/a | } |
---|
4322 | n/a | |
---|
4323 | n/a | retval = PyLong_FromSsize_t(ctx.result); |
---|
4324 | n/a | |
---|
4325 | n/a | finally: |
---|
4326 | n/a | PyMem_Free(controlbuf); |
---|
4327 | n/a | if (iv.buf != NULL) { |
---|
4328 | n/a | PyBuffer_Release(&iv); |
---|
4329 | n/a | } |
---|
4330 | n/a | PyMem_Free(msg.msg_iov); |
---|
4331 | n/a | for (i = 0; i < ndatabufs; i++) { |
---|
4332 | n/a | PyBuffer_Release(&databufs[i]); |
---|
4333 | n/a | } |
---|
4334 | n/a | PyMem_Free(databufs); |
---|
4335 | n/a | return retval; |
---|
4336 | n/a | } |
---|
4337 | n/a | |
---|
4338 | n/a | PyDoc_STRVAR(sendmsg_afalg_doc, |
---|
4339 | n/a | "sendmsg_afalg([msg], *, op[, iv[, assoclen[, flags=MSG_MORE]]])\n\ |
---|
4340 | n/a | \n\ |
---|
4341 | n/a | Set operation mode, IV and length of associated data for an AF_ALG\n\ |
---|
4342 | n/a | operation socket."); |
---|
4343 | n/a | #endif |
---|
4344 | n/a | |
---|
4345 | n/a | /* s.shutdown(how) method */ |
---|
4346 | n/a | |
---|
4347 | n/a | static PyObject * |
---|
4348 | n/a | sock_shutdown(PySocketSockObject *s, PyObject *arg) |
---|
4349 | n/a | { |
---|
4350 | n/a | int how; |
---|
4351 | n/a | int res; |
---|
4352 | n/a | |
---|
4353 | n/a | how = _PyLong_AsInt(arg); |
---|
4354 | n/a | if (how == -1 && PyErr_Occurred()) |
---|
4355 | n/a | return NULL; |
---|
4356 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
4357 | n/a | res = shutdown(s->sock_fd, how); |
---|
4358 | n/a | Py_END_ALLOW_THREADS |
---|
4359 | n/a | if (res < 0) |
---|
4360 | n/a | return s->errorhandler(); |
---|
4361 | n/a | Py_RETURN_NONE; |
---|
4362 | n/a | } |
---|
4363 | n/a | |
---|
4364 | n/a | PyDoc_STRVAR(shutdown_doc, |
---|
4365 | n/a | "shutdown(flag)\n\ |
---|
4366 | n/a | \n\ |
---|
4367 | n/a | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\ |
---|
4368 | n/a | of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR)."); |
---|
4369 | n/a | |
---|
4370 | n/a | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
---|
4371 | n/a | static PyObject* |
---|
4372 | n/a | sock_ioctl(PySocketSockObject *s, PyObject *arg) |
---|
4373 | n/a | { |
---|
4374 | n/a | unsigned long cmd = SIO_RCVALL; |
---|
4375 | n/a | PyObject *argO; |
---|
4376 | n/a | DWORD recv; |
---|
4377 | n/a | |
---|
4378 | n/a | if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO)) |
---|
4379 | n/a | return NULL; |
---|
4380 | n/a | |
---|
4381 | n/a | switch (cmd) { |
---|
4382 | n/a | case SIO_RCVALL: { |
---|
4383 | n/a | unsigned int option = RCVALL_ON; |
---|
4384 | n/a | if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option)) |
---|
4385 | n/a | return NULL; |
---|
4386 | n/a | if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option), |
---|
4387 | n/a | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
---|
4388 | n/a | return set_error(); |
---|
4389 | n/a | } |
---|
4390 | n/a | return PyLong_FromUnsignedLong(recv); } |
---|
4391 | n/a | case SIO_KEEPALIVE_VALS: { |
---|
4392 | n/a | struct tcp_keepalive ka; |
---|
4393 | n/a | if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd, |
---|
4394 | n/a | &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval)) |
---|
4395 | n/a | return NULL; |
---|
4396 | n/a | if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka), |
---|
4397 | n/a | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
---|
4398 | n/a | return set_error(); |
---|
4399 | n/a | } |
---|
4400 | n/a | return PyLong_FromUnsignedLong(recv); } |
---|
4401 | n/a | #if defined(SIO_LOOPBACK_FAST_PATH) |
---|
4402 | n/a | case SIO_LOOPBACK_FAST_PATH: { |
---|
4403 | n/a | unsigned int option; |
---|
4404 | n/a | if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option)) |
---|
4405 | n/a | return NULL; |
---|
4406 | n/a | if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option), |
---|
4407 | n/a | NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) { |
---|
4408 | n/a | return set_error(); |
---|
4409 | n/a | } |
---|
4410 | n/a | return PyLong_FromUnsignedLong(recv); } |
---|
4411 | n/a | #endif |
---|
4412 | n/a | default: |
---|
4413 | n/a | PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd); |
---|
4414 | n/a | return NULL; |
---|
4415 | n/a | } |
---|
4416 | n/a | } |
---|
4417 | n/a | PyDoc_STRVAR(sock_ioctl_doc, |
---|
4418 | n/a | "ioctl(cmd, option) -> long\n\ |
---|
4419 | n/a | \n\ |
---|
4420 | n/a | Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\ |
---|
4421 | n/a | SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\ |
---|
4422 | n/a | SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval).\n\ |
---|
4423 | n/a | SIO_LOOPBACK_FAST_PATH: 'option' is a boolean value, and is disabled by default"); |
---|
4424 | n/a | #endif |
---|
4425 | n/a | |
---|
4426 | n/a | #if defined(MS_WINDOWS) |
---|
4427 | n/a | static PyObject* |
---|
4428 | n/a | sock_share(PySocketSockObject *s, PyObject *arg) |
---|
4429 | n/a | { |
---|
4430 | n/a | WSAPROTOCOL_INFO info; |
---|
4431 | n/a | DWORD processId; |
---|
4432 | n/a | int result; |
---|
4433 | n/a | |
---|
4434 | n/a | if (!PyArg_ParseTuple(arg, "I", &processId)) |
---|
4435 | n/a | return NULL; |
---|
4436 | n/a | |
---|
4437 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
4438 | n/a | result = WSADuplicateSocket(s->sock_fd, processId, &info); |
---|
4439 | n/a | Py_END_ALLOW_THREADS |
---|
4440 | n/a | if (result == SOCKET_ERROR) |
---|
4441 | n/a | return set_error(); |
---|
4442 | n/a | return PyBytes_FromStringAndSize((const char*)&info, sizeof(info)); |
---|
4443 | n/a | } |
---|
4444 | n/a | PyDoc_STRVAR(sock_share_doc, |
---|
4445 | n/a | "share(process_id) -> bytes\n\ |
---|
4446 | n/a | \n\ |
---|
4447 | n/a | Share the socket with another process. The target process id\n\ |
---|
4448 | n/a | must be provided and the resulting bytes object passed to the target\n\ |
---|
4449 | n/a | process. There the shared socket can be instantiated by calling\n\ |
---|
4450 | n/a | socket.fromshare()."); |
---|
4451 | n/a | |
---|
4452 | n/a | |
---|
4453 | n/a | #endif |
---|
4454 | n/a | |
---|
4455 | n/a | /* List of methods for socket objects */ |
---|
4456 | n/a | |
---|
4457 | n/a | static PyMethodDef sock_methods[] = { |
---|
4458 | n/a | {"_accept", (PyCFunction)sock_accept, METH_NOARGS, |
---|
4459 | n/a | accept_doc}, |
---|
4460 | n/a | {"bind", (PyCFunction)sock_bind, METH_O, |
---|
4461 | n/a | bind_doc}, |
---|
4462 | n/a | {"close", (PyCFunction)sock_close, METH_NOARGS, |
---|
4463 | n/a | close_doc}, |
---|
4464 | n/a | {"connect", (PyCFunction)sock_connect, METH_O, |
---|
4465 | n/a | connect_doc}, |
---|
4466 | n/a | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O, |
---|
4467 | n/a | connect_ex_doc}, |
---|
4468 | n/a | {"detach", (PyCFunction)sock_detach, METH_NOARGS, |
---|
4469 | n/a | detach_doc}, |
---|
4470 | n/a | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS, |
---|
4471 | n/a | fileno_doc}, |
---|
4472 | n/a | #ifdef HAVE_GETPEERNAME |
---|
4473 | n/a | {"getpeername", (PyCFunction)sock_getpeername, |
---|
4474 | n/a | METH_NOARGS, getpeername_doc}, |
---|
4475 | n/a | #endif |
---|
4476 | n/a | {"getsockname", (PyCFunction)sock_getsockname, |
---|
4477 | n/a | METH_NOARGS, getsockname_doc}, |
---|
4478 | n/a | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS, |
---|
4479 | n/a | getsockopt_doc}, |
---|
4480 | n/a | #if defined(MS_WINDOWS) && defined(SIO_RCVALL) |
---|
4481 | n/a | {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS, |
---|
4482 | n/a | sock_ioctl_doc}, |
---|
4483 | n/a | #endif |
---|
4484 | n/a | #if defined(MS_WINDOWS) |
---|
4485 | n/a | {"share", (PyCFunction)sock_share, METH_VARARGS, |
---|
4486 | n/a | sock_share_doc}, |
---|
4487 | n/a | #endif |
---|
4488 | n/a | {"listen", (PyCFunction)sock_listen, METH_VARARGS, |
---|
4489 | n/a | listen_doc}, |
---|
4490 | n/a | {"recv", (PyCFunction)sock_recv, METH_VARARGS, |
---|
4491 | n/a | recv_doc}, |
---|
4492 | n/a | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS, |
---|
4493 | n/a | recv_into_doc}, |
---|
4494 | n/a | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS, |
---|
4495 | n/a | recvfrom_doc}, |
---|
4496 | n/a | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS, |
---|
4497 | n/a | recvfrom_into_doc}, |
---|
4498 | n/a | {"send", (PyCFunction)sock_send, METH_VARARGS, |
---|
4499 | n/a | send_doc}, |
---|
4500 | n/a | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS, |
---|
4501 | n/a | sendall_doc}, |
---|
4502 | n/a | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS, |
---|
4503 | n/a | sendto_doc}, |
---|
4504 | n/a | {"setblocking", (PyCFunction)sock_setblocking, METH_O, |
---|
4505 | n/a | setblocking_doc}, |
---|
4506 | n/a | {"settimeout", (PyCFunction)sock_settimeout, METH_O, |
---|
4507 | n/a | settimeout_doc}, |
---|
4508 | n/a | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS, |
---|
4509 | n/a | gettimeout_doc}, |
---|
4510 | n/a | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS, |
---|
4511 | n/a | setsockopt_doc}, |
---|
4512 | n/a | {"shutdown", (PyCFunction)sock_shutdown, METH_O, |
---|
4513 | n/a | shutdown_doc}, |
---|
4514 | n/a | #ifdef CMSG_LEN |
---|
4515 | n/a | {"recvmsg", (PyCFunction)sock_recvmsg, METH_VARARGS, |
---|
4516 | n/a | recvmsg_doc}, |
---|
4517 | n/a | {"recvmsg_into", (PyCFunction)sock_recvmsg_into, METH_VARARGS, |
---|
4518 | n/a | recvmsg_into_doc,}, |
---|
4519 | n/a | {"sendmsg", (PyCFunction)sock_sendmsg, METH_VARARGS, |
---|
4520 | n/a | sendmsg_doc}, |
---|
4521 | n/a | #endif |
---|
4522 | n/a | #ifdef HAVE_SOCKADDR_ALG |
---|
4523 | n/a | {"sendmsg_afalg", (PyCFunction)sock_sendmsg_afalg, METH_VARARGS | METH_KEYWORDS, |
---|
4524 | n/a | sendmsg_afalg_doc}, |
---|
4525 | n/a | #endif |
---|
4526 | n/a | {NULL, NULL} /* sentinel */ |
---|
4527 | n/a | }; |
---|
4528 | n/a | |
---|
4529 | n/a | /* SockObject members */ |
---|
4530 | n/a | static PyMemberDef sock_memberlist[] = { |
---|
4531 | n/a | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"}, |
---|
4532 | n/a | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"}, |
---|
4533 | n/a | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"}, |
---|
4534 | n/a | {0}, |
---|
4535 | n/a | }; |
---|
4536 | n/a | |
---|
4537 | n/a | static PyGetSetDef sock_getsetlist[] = { |
---|
4538 | n/a | {"timeout", (getter)sock_gettimeout, NULL, PyDoc_STR("the socket timeout")}, |
---|
4539 | n/a | {NULL} /* sentinel */ |
---|
4540 | n/a | }; |
---|
4541 | n/a | |
---|
4542 | n/a | /* Deallocate a socket object in response to the last Py_DECREF(). |
---|
4543 | n/a | First close the file description. */ |
---|
4544 | n/a | |
---|
4545 | n/a | static void |
---|
4546 | n/a | sock_finalize(PySocketSockObject *s) |
---|
4547 | n/a | { |
---|
4548 | n/a | SOCKET_T fd; |
---|
4549 | n/a | PyObject *error_type, *error_value, *error_traceback; |
---|
4550 | n/a | |
---|
4551 | n/a | /* Save the current exception, if any. */ |
---|
4552 | n/a | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
---|
4553 | n/a | |
---|
4554 | n/a | if (s->sock_fd != INVALID_SOCKET) { |
---|
4555 | n/a | if (PyErr_ResourceWarning((PyObject *)s, 1, "unclosed %R", s)) { |
---|
4556 | n/a | /* Spurious errors can appear at shutdown */ |
---|
4557 | n/a | if (PyErr_ExceptionMatches(PyExc_Warning)) { |
---|
4558 | n/a | PyErr_WriteUnraisable((PyObject *)s); |
---|
4559 | n/a | } |
---|
4560 | n/a | } |
---|
4561 | n/a | |
---|
4562 | n/a | /* Only close the socket *after* logging the ResourceWarning warning |
---|
4563 | n/a | to allow the logger to call socket methods like |
---|
4564 | n/a | socket.getsockname(). If the socket is closed before, socket |
---|
4565 | n/a | methods fails with the EBADF error. */ |
---|
4566 | n/a | fd = s->sock_fd; |
---|
4567 | n/a | s->sock_fd = INVALID_SOCKET; |
---|
4568 | n/a | |
---|
4569 | n/a | /* We do not want to retry upon EINTR: see sock_close() */ |
---|
4570 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
4571 | n/a | (void) SOCKETCLOSE(fd); |
---|
4572 | n/a | Py_END_ALLOW_THREADS |
---|
4573 | n/a | } |
---|
4574 | n/a | |
---|
4575 | n/a | /* Restore the saved exception. */ |
---|
4576 | n/a | PyErr_Restore(error_type, error_value, error_traceback); |
---|
4577 | n/a | } |
---|
4578 | n/a | |
---|
4579 | n/a | static void |
---|
4580 | n/a | sock_dealloc(PySocketSockObject *s) |
---|
4581 | n/a | { |
---|
4582 | n/a | if (PyObject_CallFinalizerFromDealloc((PyObject *)s) < 0) |
---|
4583 | n/a | return; |
---|
4584 | n/a | |
---|
4585 | n/a | Py_TYPE(s)->tp_free((PyObject *)s); |
---|
4586 | n/a | } |
---|
4587 | n/a | |
---|
4588 | n/a | |
---|
4589 | n/a | static PyObject * |
---|
4590 | n/a | sock_repr(PySocketSockObject *s) |
---|
4591 | n/a | { |
---|
4592 | n/a | long sock_fd; |
---|
4593 | n/a | /* On Windows, this test is needed because SOCKET_T is unsigned */ |
---|
4594 | n/a | if (s->sock_fd == INVALID_SOCKET) { |
---|
4595 | n/a | sock_fd = -1; |
---|
4596 | n/a | } |
---|
4597 | n/a | #if SIZEOF_SOCKET_T > SIZEOF_LONG |
---|
4598 | n/a | else if (s->sock_fd > LONG_MAX) { |
---|
4599 | n/a | /* this can occur on Win64, and actually there is a special |
---|
4600 | n/a | ugly printf formatter for decimal pointer length integer |
---|
4601 | n/a | printing, only bother if necessary*/ |
---|
4602 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
4603 | n/a | "no printf formatter to display " |
---|
4604 | n/a | "the socket descriptor in decimal"); |
---|
4605 | n/a | return NULL; |
---|
4606 | n/a | } |
---|
4607 | n/a | #endif |
---|
4608 | n/a | else |
---|
4609 | n/a | sock_fd = (long)s->sock_fd; |
---|
4610 | n/a | return PyUnicode_FromFormat( |
---|
4611 | n/a | "<socket object, fd=%ld, family=%d, type=%d, proto=%d>", |
---|
4612 | n/a | sock_fd, s->sock_family, |
---|
4613 | n/a | s->sock_type, |
---|
4614 | n/a | s->sock_proto); |
---|
4615 | n/a | } |
---|
4616 | n/a | |
---|
4617 | n/a | |
---|
4618 | n/a | /* Create a new, uninitialized socket object. */ |
---|
4619 | n/a | |
---|
4620 | n/a | static PyObject * |
---|
4621 | n/a | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
4622 | n/a | { |
---|
4623 | n/a | PyObject *new; |
---|
4624 | n/a | |
---|
4625 | n/a | new = type->tp_alloc(type, 0); |
---|
4626 | n/a | if (new != NULL) { |
---|
4627 | n/a | ((PySocketSockObject *)new)->sock_fd = INVALID_SOCKET; |
---|
4628 | n/a | ((PySocketSockObject *)new)->sock_timeout = _PyTime_FromSeconds(-1); |
---|
4629 | n/a | ((PySocketSockObject *)new)->errorhandler = &set_error; |
---|
4630 | n/a | } |
---|
4631 | n/a | return new; |
---|
4632 | n/a | } |
---|
4633 | n/a | |
---|
4634 | n/a | |
---|
4635 | n/a | /* Initialize a new socket object. */ |
---|
4636 | n/a | |
---|
4637 | n/a | #ifdef SOCK_CLOEXEC |
---|
4638 | n/a | /* socket() and socketpair() fail with EINVAL on Linux kernel older |
---|
4639 | n/a | * than 2.6.27 if SOCK_CLOEXEC flag is set in the socket type. */ |
---|
4640 | n/a | static int sock_cloexec_works = -1; |
---|
4641 | n/a | #endif |
---|
4642 | n/a | |
---|
4643 | n/a | /*ARGSUSED*/ |
---|
4644 | n/a | static int |
---|
4645 | n/a | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds) |
---|
4646 | n/a | { |
---|
4647 | n/a | PySocketSockObject *s = (PySocketSockObject *)self; |
---|
4648 | n/a | PyObject *fdobj = NULL; |
---|
4649 | n/a | SOCKET_T fd = INVALID_SOCKET; |
---|
4650 | n/a | int family = AF_INET, type = SOCK_STREAM, proto = 0; |
---|
4651 | n/a | static char *keywords[] = {"family", "type", "proto", "fileno", 0}; |
---|
4652 | n/a | #ifndef MS_WINDOWS |
---|
4653 | n/a | #ifdef SOCK_CLOEXEC |
---|
4654 | n/a | int *atomic_flag_works = &sock_cloexec_works; |
---|
4655 | n/a | #else |
---|
4656 | n/a | int *atomic_flag_works = NULL; |
---|
4657 | n/a | #endif |
---|
4658 | n/a | #endif |
---|
4659 | n/a | |
---|
4660 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
---|
4661 | n/a | "|iiiO:socket", keywords, |
---|
4662 | n/a | &family, &type, &proto, &fdobj)) |
---|
4663 | n/a | return -1; |
---|
4664 | n/a | |
---|
4665 | n/a | if (fdobj != NULL && fdobj != Py_None) { |
---|
4666 | n/a | #ifdef MS_WINDOWS |
---|
4667 | n/a | /* recreate a socket that was duplicated */ |
---|
4668 | n/a | if (PyBytes_Check(fdobj)) { |
---|
4669 | n/a | WSAPROTOCOL_INFO info; |
---|
4670 | n/a | if (PyBytes_GET_SIZE(fdobj) != sizeof(info)) { |
---|
4671 | n/a | PyErr_Format(PyExc_ValueError, |
---|
4672 | n/a | "socket descriptor string has wrong size, " |
---|
4673 | n/a | "should be %zu bytes.", sizeof(info)); |
---|
4674 | n/a | return -1; |
---|
4675 | n/a | } |
---|
4676 | n/a | memcpy(&info, PyBytes_AS_STRING(fdobj), sizeof(info)); |
---|
4677 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
4678 | n/a | fd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, |
---|
4679 | n/a | FROM_PROTOCOL_INFO, &info, 0, WSA_FLAG_OVERLAPPED); |
---|
4680 | n/a | Py_END_ALLOW_THREADS |
---|
4681 | n/a | if (fd == INVALID_SOCKET) { |
---|
4682 | n/a | set_error(); |
---|
4683 | n/a | return -1; |
---|
4684 | n/a | } |
---|
4685 | n/a | family = info.iAddressFamily; |
---|
4686 | n/a | type = info.iSocketType; |
---|
4687 | n/a | proto = info.iProtocol; |
---|
4688 | n/a | } |
---|
4689 | n/a | else |
---|
4690 | n/a | #endif |
---|
4691 | n/a | { |
---|
4692 | n/a | fd = PyLong_AsSocket_t(fdobj); |
---|
4693 | n/a | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
---|
4694 | n/a | return -1; |
---|
4695 | n/a | if (fd == INVALID_SOCKET) { |
---|
4696 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
4697 | n/a | "can't use invalid socket value"); |
---|
4698 | n/a | return -1; |
---|
4699 | n/a | } |
---|
4700 | n/a | } |
---|
4701 | n/a | } |
---|
4702 | n/a | else { |
---|
4703 | n/a | #ifdef MS_WINDOWS |
---|
4704 | n/a | /* Windows implementation */ |
---|
4705 | n/a | #ifndef WSA_FLAG_NO_HANDLE_INHERIT |
---|
4706 | n/a | #define WSA_FLAG_NO_HANDLE_INHERIT 0x80 |
---|
4707 | n/a | #endif |
---|
4708 | n/a | |
---|
4709 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
4710 | n/a | if (support_wsa_no_inherit) { |
---|
4711 | n/a | fd = WSASocket(family, type, proto, |
---|
4712 | n/a | NULL, 0, |
---|
4713 | n/a | WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT); |
---|
4714 | n/a | if (fd == INVALID_SOCKET) { |
---|
4715 | n/a | /* Windows 7 or Windows 2008 R2 without SP1 or the hotfix */ |
---|
4716 | n/a | support_wsa_no_inherit = 0; |
---|
4717 | n/a | fd = socket(family, type, proto); |
---|
4718 | n/a | } |
---|
4719 | n/a | } |
---|
4720 | n/a | else { |
---|
4721 | n/a | fd = socket(family, type, proto); |
---|
4722 | n/a | } |
---|
4723 | n/a | Py_END_ALLOW_THREADS |
---|
4724 | n/a | |
---|
4725 | n/a | if (fd == INVALID_SOCKET) { |
---|
4726 | n/a | set_error(); |
---|
4727 | n/a | return -1; |
---|
4728 | n/a | } |
---|
4729 | n/a | |
---|
4730 | n/a | if (!support_wsa_no_inherit) { |
---|
4731 | n/a | if (!SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0)) { |
---|
4732 | n/a | closesocket(fd); |
---|
4733 | n/a | PyErr_SetFromWindowsErr(0); |
---|
4734 | n/a | return -1; |
---|
4735 | n/a | } |
---|
4736 | n/a | } |
---|
4737 | n/a | #else |
---|
4738 | n/a | /* UNIX */ |
---|
4739 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
4740 | n/a | #ifdef SOCK_CLOEXEC |
---|
4741 | n/a | if (sock_cloexec_works != 0) { |
---|
4742 | n/a | fd = socket(family, type | SOCK_CLOEXEC, proto); |
---|
4743 | n/a | if (sock_cloexec_works == -1) { |
---|
4744 | n/a | if (fd >= 0) { |
---|
4745 | n/a | sock_cloexec_works = 1; |
---|
4746 | n/a | } |
---|
4747 | n/a | else if (errno == EINVAL) { |
---|
4748 | n/a | /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */ |
---|
4749 | n/a | sock_cloexec_works = 0; |
---|
4750 | n/a | fd = socket(family, type, proto); |
---|
4751 | n/a | } |
---|
4752 | n/a | } |
---|
4753 | n/a | } |
---|
4754 | n/a | else |
---|
4755 | n/a | #endif |
---|
4756 | n/a | { |
---|
4757 | n/a | fd = socket(family, type, proto); |
---|
4758 | n/a | } |
---|
4759 | n/a | Py_END_ALLOW_THREADS |
---|
4760 | n/a | |
---|
4761 | n/a | if (fd == INVALID_SOCKET) { |
---|
4762 | n/a | set_error(); |
---|
4763 | n/a | return -1; |
---|
4764 | n/a | } |
---|
4765 | n/a | |
---|
4766 | n/a | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
---|
4767 | n/a | SOCKETCLOSE(fd); |
---|
4768 | n/a | return -1; |
---|
4769 | n/a | } |
---|
4770 | n/a | #endif |
---|
4771 | n/a | } |
---|
4772 | n/a | if (init_sockobject(s, fd, family, type, proto) == -1) { |
---|
4773 | n/a | SOCKETCLOSE(fd); |
---|
4774 | n/a | return -1; |
---|
4775 | n/a | } |
---|
4776 | n/a | |
---|
4777 | n/a | return 0; |
---|
4778 | n/a | |
---|
4779 | n/a | } |
---|
4780 | n/a | |
---|
4781 | n/a | |
---|
4782 | n/a | /* Type object for socket objects. */ |
---|
4783 | n/a | |
---|
4784 | n/a | static PyTypeObject sock_type = { |
---|
4785 | n/a | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
---|
4786 | n/a | "_socket.socket", /* tp_name */ |
---|
4787 | n/a | sizeof(PySocketSockObject), /* tp_basicsize */ |
---|
4788 | n/a | 0, /* tp_itemsize */ |
---|
4789 | n/a | (destructor)sock_dealloc, /* tp_dealloc */ |
---|
4790 | n/a | 0, /* tp_print */ |
---|
4791 | n/a | 0, /* tp_getattr */ |
---|
4792 | n/a | 0, /* tp_setattr */ |
---|
4793 | n/a | 0, /* tp_reserved */ |
---|
4794 | n/a | (reprfunc)sock_repr, /* tp_repr */ |
---|
4795 | n/a | 0, /* tp_as_number */ |
---|
4796 | n/a | 0, /* tp_as_sequence */ |
---|
4797 | n/a | 0, /* tp_as_mapping */ |
---|
4798 | n/a | 0, /* tp_hash */ |
---|
4799 | n/a | 0, /* tp_call */ |
---|
4800 | n/a | 0, /* tp_str */ |
---|
4801 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
4802 | n/a | 0, /* tp_setattro */ |
---|
4803 | n/a | 0, /* tp_as_buffer */ |
---|
4804 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
---|
4805 | n/a | | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */ |
---|
4806 | n/a | sock_doc, /* tp_doc */ |
---|
4807 | n/a | 0, /* tp_traverse */ |
---|
4808 | n/a | 0, /* tp_clear */ |
---|
4809 | n/a | 0, /* tp_richcompare */ |
---|
4810 | n/a | 0, /* tp_weaklistoffset */ |
---|
4811 | n/a | 0, /* tp_iter */ |
---|
4812 | n/a | 0, /* tp_iternext */ |
---|
4813 | n/a | sock_methods, /* tp_methods */ |
---|
4814 | n/a | sock_memberlist, /* tp_members */ |
---|
4815 | n/a | sock_getsetlist, /* tp_getset */ |
---|
4816 | n/a | 0, /* tp_base */ |
---|
4817 | n/a | 0, /* tp_dict */ |
---|
4818 | n/a | 0, /* tp_descr_get */ |
---|
4819 | n/a | 0, /* tp_descr_set */ |
---|
4820 | n/a | 0, /* tp_dictoffset */ |
---|
4821 | n/a | sock_initobj, /* tp_init */ |
---|
4822 | n/a | PyType_GenericAlloc, /* tp_alloc */ |
---|
4823 | n/a | sock_new, /* tp_new */ |
---|
4824 | n/a | PyObject_Del, /* tp_free */ |
---|
4825 | n/a | 0, /* tp_is_gc */ |
---|
4826 | n/a | 0, /* tp_bases */ |
---|
4827 | n/a | 0, /* tp_mro */ |
---|
4828 | n/a | 0, /* tp_cache */ |
---|
4829 | n/a | 0, /* tp_subclasses */ |
---|
4830 | n/a | 0, /* tp_weaklist */ |
---|
4831 | n/a | 0, /* tp_del */ |
---|
4832 | n/a | 0, /* tp_version_tag */ |
---|
4833 | n/a | (destructor)sock_finalize, /* tp_finalize */ |
---|
4834 | n/a | }; |
---|
4835 | n/a | |
---|
4836 | n/a | |
---|
4837 | n/a | /* Python interface to gethostname(). */ |
---|
4838 | n/a | |
---|
4839 | n/a | /*ARGSUSED*/ |
---|
4840 | n/a | static PyObject * |
---|
4841 | n/a | socket_gethostname(PyObject *self, PyObject *unused) |
---|
4842 | n/a | { |
---|
4843 | n/a | #ifdef MS_WINDOWS |
---|
4844 | n/a | /* Don't use winsock's gethostname, as this returns the ANSI |
---|
4845 | n/a | version of the hostname, whereas we need a Unicode string. |
---|
4846 | n/a | Otherwise, gethostname apparently also returns the DNS name. */ |
---|
4847 | n/a | wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1]; |
---|
4848 | n/a | DWORD size = Py_ARRAY_LENGTH(buf); |
---|
4849 | n/a | wchar_t *name; |
---|
4850 | n/a | PyObject *result; |
---|
4851 | n/a | |
---|
4852 | n/a | if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size)) |
---|
4853 | n/a | return PyUnicode_FromWideChar(buf, size); |
---|
4854 | n/a | |
---|
4855 | n/a | if (GetLastError() != ERROR_MORE_DATA) |
---|
4856 | n/a | return PyErr_SetFromWindowsErr(0); |
---|
4857 | n/a | |
---|
4858 | n/a | if (size == 0) |
---|
4859 | n/a | return PyUnicode_New(0, 0); |
---|
4860 | n/a | |
---|
4861 | n/a | /* MSDN says ERROR_MORE_DATA may occur because DNS allows longer |
---|
4862 | n/a | names */ |
---|
4863 | n/a | name = PyMem_New(wchar_t, size); |
---|
4864 | n/a | if (!name) { |
---|
4865 | n/a | PyErr_NoMemory(); |
---|
4866 | n/a | return NULL; |
---|
4867 | n/a | } |
---|
4868 | n/a | if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname, |
---|
4869 | n/a | name, |
---|
4870 | n/a | &size)) |
---|
4871 | n/a | { |
---|
4872 | n/a | PyMem_Free(name); |
---|
4873 | n/a | return PyErr_SetFromWindowsErr(0); |
---|
4874 | n/a | } |
---|
4875 | n/a | |
---|
4876 | n/a | result = PyUnicode_FromWideChar(name, size); |
---|
4877 | n/a | PyMem_Free(name); |
---|
4878 | n/a | return result; |
---|
4879 | n/a | #else |
---|
4880 | n/a | char buf[1024]; |
---|
4881 | n/a | int res; |
---|
4882 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
4883 | n/a | res = gethostname(buf, (int) sizeof buf - 1); |
---|
4884 | n/a | Py_END_ALLOW_THREADS |
---|
4885 | n/a | if (res < 0) |
---|
4886 | n/a | return set_error(); |
---|
4887 | n/a | buf[sizeof buf - 1] = '\0'; |
---|
4888 | n/a | return PyUnicode_DecodeFSDefault(buf); |
---|
4889 | n/a | #endif |
---|
4890 | n/a | } |
---|
4891 | n/a | |
---|
4892 | n/a | PyDoc_STRVAR(gethostname_doc, |
---|
4893 | n/a | "gethostname() -> string\n\ |
---|
4894 | n/a | \n\ |
---|
4895 | n/a | Return the current host name."); |
---|
4896 | n/a | |
---|
4897 | n/a | #ifdef HAVE_SETHOSTNAME |
---|
4898 | n/a | PyDoc_STRVAR(sethostname_doc, |
---|
4899 | n/a | "sethostname(name)\n\n\ |
---|
4900 | n/a | Sets the hostname to name."); |
---|
4901 | n/a | |
---|
4902 | n/a | static PyObject * |
---|
4903 | n/a | socket_sethostname(PyObject *self, PyObject *args) |
---|
4904 | n/a | { |
---|
4905 | n/a | PyObject *hnobj; |
---|
4906 | n/a | Py_buffer buf; |
---|
4907 | n/a | int res, flag = 0; |
---|
4908 | n/a | |
---|
4909 | n/a | #ifdef _AIX |
---|
4910 | n/a | /* issue #18259, not declared in any useful header file */ |
---|
4911 | n/a | extern int sethostname(const char *, size_t); |
---|
4912 | n/a | #endif |
---|
4913 | n/a | |
---|
4914 | n/a | if (!PyArg_ParseTuple(args, "S:sethostname", &hnobj)) { |
---|
4915 | n/a | PyErr_Clear(); |
---|
4916 | n/a | if (!PyArg_ParseTuple(args, "O&:sethostname", |
---|
4917 | n/a | PyUnicode_FSConverter, &hnobj)) |
---|
4918 | n/a | return NULL; |
---|
4919 | n/a | flag = 1; |
---|
4920 | n/a | } |
---|
4921 | n/a | res = PyObject_GetBuffer(hnobj, &buf, PyBUF_SIMPLE); |
---|
4922 | n/a | if (!res) { |
---|
4923 | n/a | res = sethostname(buf.buf, buf.len); |
---|
4924 | n/a | PyBuffer_Release(&buf); |
---|
4925 | n/a | } |
---|
4926 | n/a | if (flag) |
---|
4927 | n/a | Py_DECREF(hnobj); |
---|
4928 | n/a | if (res) |
---|
4929 | n/a | return set_error(); |
---|
4930 | n/a | Py_RETURN_NONE; |
---|
4931 | n/a | } |
---|
4932 | n/a | #endif |
---|
4933 | n/a | |
---|
4934 | n/a | /* Python interface to gethostbyname(name). */ |
---|
4935 | n/a | |
---|
4936 | n/a | /*ARGSUSED*/ |
---|
4937 | n/a | static PyObject * |
---|
4938 | n/a | socket_gethostbyname(PyObject *self, PyObject *args) |
---|
4939 | n/a | { |
---|
4940 | n/a | char *name; |
---|
4941 | n/a | sock_addr_t addrbuf; |
---|
4942 | n/a | PyObject *ret = NULL; |
---|
4943 | n/a | |
---|
4944 | n/a | if (!PyArg_ParseTuple(args, "et:gethostbyname", "idna", &name)) |
---|
4945 | n/a | return NULL; |
---|
4946 | n/a | if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0) |
---|
4947 | n/a | goto finally; |
---|
4948 | n/a | ret = makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in)); |
---|
4949 | n/a | finally: |
---|
4950 | n/a | PyMem_Free(name); |
---|
4951 | n/a | return ret; |
---|
4952 | n/a | } |
---|
4953 | n/a | |
---|
4954 | n/a | PyDoc_STRVAR(gethostbyname_doc, |
---|
4955 | n/a | "gethostbyname(host) -> address\n\ |
---|
4956 | n/a | \n\ |
---|
4957 | n/a | Return the IP address (a string of the form '255.255.255.255') for a host."); |
---|
4958 | n/a | |
---|
4959 | n/a | |
---|
4960 | n/a | static PyObject* |
---|
4961 | n/a | sock_decode_hostname(const char *name) |
---|
4962 | n/a | { |
---|
4963 | n/a | #ifdef MS_WINDOWS |
---|
4964 | n/a | /* Issue #26227: gethostbyaddr() returns a string encoded |
---|
4965 | n/a | * to the ANSI code page */ |
---|
4966 | n/a | return PyUnicode_DecodeFSDefault(name); |
---|
4967 | n/a | #else |
---|
4968 | n/a | /* Decode from UTF-8 */ |
---|
4969 | n/a | return PyUnicode_FromString(name); |
---|
4970 | n/a | #endif |
---|
4971 | n/a | } |
---|
4972 | n/a | |
---|
4973 | n/a | /* Convenience function common to gethostbyname_ex and gethostbyaddr */ |
---|
4974 | n/a | |
---|
4975 | n/a | static PyObject * |
---|
4976 | n/a | gethost_common(struct hostent *h, struct sockaddr *addr, size_t alen, int af) |
---|
4977 | n/a | { |
---|
4978 | n/a | char **pch; |
---|
4979 | n/a | PyObject *rtn_tuple = (PyObject *)NULL; |
---|
4980 | n/a | PyObject *name_list = (PyObject *)NULL; |
---|
4981 | n/a | PyObject *addr_list = (PyObject *)NULL; |
---|
4982 | n/a | PyObject *tmp; |
---|
4983 | n/a | PyObject *name; |
---|
4984 | n/a | |
---|
4985 | n/a | if (h == NULL) { |
---|
4986 | n/a | /* Let's get real error message to return */ |
---|
4987 | n/a | set_herror(h_errno); |
---|
4988 | n/a | return NULL; |
---|
4989 | n/a | } |
---|
4990 | n/a | |
---|
4991 | n/a | if (h->h_addrtype != af) { |
---|
4992 | n/a | /* Let's get real error message to return */ |
---|
4993 | n/a | errno = EAFNOSUPPORT; |
---|
4994 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
4995 | n/a | return NULL; |
---|
4996 | n/a | } |
---|
4997 | n/a | |
---|
4998 | n/a | switch (af) { |
---|
4999 | n/a | |
---|
5000 | n/a | case AF_INET: |
---|
5001 | n/a | if (alen < sizeof(struct sockaddr_in)) |
---|
5002 | n/a | return NULL; |
---|
5003 | n/a | break; |
---|
5004 | n/a | |
---|
5005 | n/a | #ifdef ENABLE_IPV6 |
---|
5006 | n/a | case AF_INET6: |
---|
5007 | n/a | if (alen < sizeof(struct sockaddr_in6)) |
---|
5008 | n/a | return NULL; |
---|
5009 | n/a | break; |
---|
5010 | n/a | #endif |
---|
5011 | n/a | |
---|
5012 | n/a | } |
---|
5013 | n/a | |
---|
5014 | n/a | if ((name_list = PyList_New(0)) == NULL) |
---|
5015 | n/a | goto err; |
---|
5016 | n/a | |
---|
5017 | n/a | if ((addr_list = PyList_New(0)) == NULL) |
---|
5018 | n/a | goto err; |
---|
5019 | n/a | |
---|
5020 | n/a | /* SF #1511317: h_aliases can be NULL */ |
---|
5021 | n/a | if (h->h_aliases) { |
---|
5022 | n/a | for (pch = h->h_aliases; *pch != NULL; pch++) { |
---|
5023 | n/a | int status; |
---|
5024 | n/a | tmp = PyUnicode_FromString(*pch); |
---|
5025 | n/a | if (tmp == NULL) |
---|
5026 | n/a | goto err; |
---|
5027 | n/a | |
---|
5028 | n/a | status = PyList_Append(name_list, tmp); |
---|
5029 | n/a | Py_DECREF(tmp); |
---|
5030 | n/a | |
---|
5031 | n/a | if (status) |
---|
5032 | n/a | goto err; |
---|
5033 | n/a | } |
---|
5034 | n/a | } |
---|
5035 | n/a | |
---|
5036 | n/a | for (pch = h->h_addr_list; *pch != NULL; pch++) { |
---|
5037 | n/a | int status; |
---|
5038 | n/a | |
---|
5039 | n/a | switch (af) { |
---|
5040 | n/a | |
---|
5041 | n/a | case AF_INET: |
---|
5042 | n/a | { |
---|
5043 | n/a | struct sockaddr_in sin; |
---|
5044 | n/a | memset(&sin, 0, sizeof(sin)); |
---|
5045 | n/a | sin.sin_family = af; |
---|
5046 | n/a | #ifdef HAVE_SOCKADDR_SA_LEN |
---|
5047 | n/a | sin.sin_len = sizeof(sin); |
---|
5048 | n/a | #endif |
---|
5049 | n/a | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr)); |
---|
5050 | n/a | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); |
---|
5051 | n/a | |
---|
5052 | n/a | if (pch == h->h_addr_list && alen >= sizeof(sin)) |
---|
5053 | n/a | memcpy((char *) addr, &sin, sizeof(sin)); |
---|
5054 | n/a | break; |
---|
5055 | n/a | } |
---|
5056 | n/a | |
---|
5057 | n/a | #ifdef ENABLE_IPV6 |
---|
5058 | n/a | case AF_INET6: |
---|
5059 | n/a | { |
---|
5060 | n/a | struct sockaddr_in6 sin6; |
---|
5061 | n/a | memset(&sin6, 0, sizeof(sin6)); |
---|
5062 | n/a | sin6.sin6_family = af; |
---|
5063 | n/a | #ifdef HAVE_SOCKADDR_SA_LEN |
---|
5064 | n/a | sin6.sin6_len = sizeof(sin6); |
---|
5065 | n/a | #endif |
---|
5066 | n/a | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr)); |
---|
5067 | n/a | tmp = makeipaddr((struct sockaddr *)&sin6, |
---|
5068 | n/a | sizeof(sin6)); |
---|
5069 | n/a | |
---|
5070 | n/a | if (pch == h->h_addr_list && alen >= sizeof(sin6)) |
---|
5071 | n/a | memcpy((char *) addr, &sin6, sizeof(sin6)); |
---|
5072 | n/a | break; |
---|
5073 | n/a | } |
---|
5074 | n/a | #endif |
---|
5075 | n/a | |
---|
5076 | n/a | default: /* can't happen */ |
---|
5077 | n/a | PyErr_SetString(PyExc_OSError, |
---|
5078 | n/a | "unsupported address family"); |
---|
5079 | n/a | return NULL; |
---|
5080 | n/a | } |
---|
5081 | n/a | |
---|
5082 | n/a | if (tmp == NULL) |
---|
5083 | n/a | goto err; |
---|
5084 | n/a | |
---|
5085 | n/a | status = PyList_Append(addr_list, tmp); |
---|
5086 | n/a | Py_DECREF(tmp); |
---|
5087 | n/a | |
---|
5088 | n/a | if (status) |
---|
5089 | n/a | goto err; |
---|
5090 | n/a | } |
---|
5091 | n/a | |
---|
5092 | n/a | name = sock_decode_hostname(h->h_name); |
---|
5093 | n/a | if (name == NULL) |
---|
5094 | n/a | goto err; |
---|
5095 | n/a | rtn_tuple = Py_BuildValue("NOO", name, name_list, addr_list); |
---|
5096 | n/a | |
---|
5097 | n/a | err: |
---|
5098 | n/a | Py_XDECREF(name_list); |
---|
5099 | n/a | Py_XDECREF(addr_list); |
---|
5100 | n/a | return rtn_tuple; |
---|
5101 | n/a | } |
---|
5102 | n/a | |
---|
5103 | n/a | |
---|
5104 | n/a | /* Python interface to gethostbyname_ex(name). */ |
---|
5105 | n/a | |
---|
5106 | n/a | /*ARGSUSED*/ |
---|
5107 | n/a | static PyObject * |
---|
5108 | n/a | socket_gethostbyname_ex(PyObject *self, PyObject *args) |
---|
5109 | n/a | { |
---|
5110 | n/a | char *name; |
---|
5111 | n/a | struct hostent *h; |
---|
5112 | n/a | sock_addr_t addr; |
---|
5113 | n/a | struct sockaddr *sa; |
---|
5114 | n/a | PyObject *ret = NULL; |
---|
5115 | n/a | #ifdef HAVE_GETHOSTBYNAME_R |
---|
5116 | n/a | struct hostent hp_allocated; |
---|
5117 | n/a | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
---|
5118 | n/a | struct hostent_data data; |
---|
5119 | n/a | #else |
---|
5120 | n/a | char buf[16384]; |
---|
5121 | n/a | int buf_len = (sizeof buf) - 1; |
---|
5122 | n/a | int errnop; |
---|
5123 | n/a | #endif |
---|
5124 | n/a | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
---|
5125 | n/a | int result; |
---|
5126 | n/a | #endif |
---|
5127 | n/a | #endif /* HAVE_GETHOSTBYNAME_R */ |
---|
5128 | n/a | |
---|
5129 | n/a | if (!PyArg_ParseTuple(args, "et:gethostbyname_ex", "idna", &name)) |
---|
5130 | n/a | return NULL; |
---|
5131 | n/a | if (setipaddr(name, SAS2SA(&addr), sizeof(addr), AF_INET) < 0) |
---|
5132 | n/a | goto finally; |
---|
5133 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
5134 | n/a | #ifdef HAVE_GETHOSTBYNAME_R |
---|
5135 | n/a | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
---|
5136 | n/a | gethostbyname_r(name, &hp_allocated, buf, buf_len, |
---|
5137 | n/a | &h, &errnop); |
---|
5138 | n/a | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
---|
5139 | n/a | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); |
---|
5140 | n/a | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
---|
5141 | n/a | memset((void *) &data, '\0', sizeof(data)); |
---|
5142 | n/a | result = gethostbyname_r(name, &hp_allocated, &data); |
---|
5143 | n/a | h = (result != 0) ? NULL : &hp_allocated; |
---|
5144 | n/a | #endif |
---|
5145 | n/a | #else /* not HAVE_GETHOSTBYNAME_R */ |
---|
5146 | n/a | #ifdef USE_GETHOSTBYNAME_LOCK |
---|
5147 | n/a | PyThread_acquire_lock(netdb_lock, 1); |
---|
5148 | n/a | #endif |
---|
5149 | n/a | h = gethostbyname(name); |
---|
5150 | n/a | #endif /* HAVE_GETHOSTBYNAME_R */ |
---|
5151 | n/a | Py_END_ALLOW_THREADS |
---|
5152 | n/a | /* Some C libraries would require addr.__ss_family instead of |
---|
5153 | n/a | addr.ss_family. |
---|
5154 | n/a | Therefore, we cast the sockaddr_storage into sockaddr to |
---|
5155 | n/a | access sa_family. */ |
---|
5156 | n/a | sa = SAS2SA(&addr); |
---|
5157 | n/a | ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), |
---|
5158 | n/a | sa->sa_family); |
---|
5159 | n/a | #ifdef USE_GETHOSTBYNAME_LOCK |
---|
5160 | n/a | PyThread_release_lock(netdb_lock); |
---|
5161 | n/a | #endif |
---|
5162 | n/a | finally: |
---|
5163 | n/a | PyMem_Free(name); |
---|
5164 | n/a | return ret; |
---|
5165 | n/a | } |
---|
5166 | n/a | |
---|
5167 | n/a | PyDoc_STRVAR(ghbn_ex_doc, |
---|
5168 | n/a | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\ |
---|
5169 | n/a | \n\ |
---|
5170 | n/a | Return the true host name, a list of aliases, and a list of IP addresses,\n\ |
---|
5171 | n/a | for a host. The host argument is a string giving a host name or IP number."); |
---|
5172 | n/a | |
---|
5173 | n/a | |
---|
5174 | n/a | /* Python interface to gethostbyaddr(IP). */ |
---|
5175 | n/a | |
---|
5176 | n/a | /*ARGSUSED*/ |
---|
5177 | n/a | static PyObject * |
---|
5178 | n/a | socket_gethostbyaddr(PyObject *self, PyObject *args) |
---|
5179 | n/a | { |
---|
5180 | n/a | sock_addr_t addr; |
---|
5181 | n/a | struct sockaddr *sa = SAS2SA(&addr); |
---|
5182 | n/a | char *ip_num; |
---|
5183 | n/a | struct hostent *h; |
---|
5184 | n/a | PyObject *ret = NULL; |
---|
5185 | n/a | #ifdef HAVE_GETHOSTBYNAME_R |
---|
5186 | n/a | struct hostent hp_allocated; |
---|
5187 | n/a | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
---|
5188 | n/a | struct hostent_data data; |
---|
5189 | n/a | #else |
---|
5190 | n/a | /* glibcs up to 2.10 assume that the buf argument to |
---|
5191 | n/a | gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc |
---|
5192 | n/a | does not ensure. The attribute below instructs the compiler |
---|
5193 | n/a | to maintain this alignment. */ |
---|
5194 | n/a | char buf[16384] Py_ALIGNED(8); |
---|
5195 | n/a | int buf_len = (sizeof buf) - 1; |
---|
5196 | n/a | int errnop; |
---|
5197 | n/a | #endif |
---|
5198 | n/a | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG |
---|
5199 | n/a | int result; |
---|
5200 | n/a | #endif |
---|
5201 | n/a | #endif /* HAVE_GETHOSTBYNAME_R */ |
---|
5202 | n/a | char *ap; |
---|
5203 | n/a | int al; |
---|
5204 | n/a | int af; |
---|
5205 | n/a | |
---|
5206 | n/a | if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num)) |
---|
5207 | n/a | return NULL; |
---|
5208 | n/a | af = AF_UNSPEC; |
---|
5209 | n/a | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0) |
---|
5210 | n/a | goto finally; |
---|
5211 | n/a | af = sa->sa_family; |
---|
5212 | n/a | ap = NULL; |
---|
5213 | n/a | /* al = 0; */ |
---|
5214 | n/a | switch (af) { |
---|
5215 | n/a | case AF_INET: |
---|
5216 | n/a | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr; |
---|
5217 | n/a | al = sizeof(((struct sockaddr_in *)sa)->sin_addr); |
---|
5218 | n/a | break; |
---|
5219 | n/a | #ifdef ENABLE_IPV6 |
---|
5220 | n/a | case AF_INET6: |
---|
5221 | n/a | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr; |
---|
5222 | n/a | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); |
---|
5223 | n/a | break; |
---|
5224 | n/a | #endif |
---|
5225 | n/a | default: |
---|
5226 | n/a | PyErr_SetString(PyExc_OSError, "unsupported address family"); |
---|
5227 | n/a | goto finally; |
---|
5228 | n/a | } |
---|
5229 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
5230 | n/a | #ifdef HAVE_GETHOSTBYNAME_R |
---|
5231 | n/a | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) |
---|
5232 | n/a | gethostbyaddr_r(ap, al, af, |
---|
5233 | n/a | &hp_allocated, buf, buf_len, |
---|
5234 | n/a | &h, &errnop); |
---|
5235 | n/a | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) |
---|
5236 | n/a | h = gethostbyaddr_r(ap, al, af, |
---|
5237 | n/a | &hp_allocated, buf, buf_len, &errnop); |
---|
5238 | n/a | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ |
---|
5239 | n/a | memset((void *) &data, '\0', sizeof(data)); |
---|
5240 | n/a | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); |
---|
5241 | n/a | h = (result != 0) ? NULL : &hp_allocated; |
---|
5242 | n/a | #endif |
---|
5243 | n/a | #else /* not HAVE_GETHOSTBYNAME_R */ |
---|
5244 | n/a | #ifdef USE_GETHOSTBYNAME_LOCK |
---|
5245 | n/a | PyThread_acquire_lock(netdb_lock, 1); |
---|
5246 | n/a | #endif |
---|
5247 | n/a | h = gethostbyaddr(ap, al, af); |
---|
5248 | n/a | #endif /* HAVE_GETHOSTBYNAME_R */ |
---|
5249 | n/a | Py_END_ALLOW_THREADS |
---|
5250 | n/a | ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), af); |
---|
5251 | n/a | #ifdef USE_GETHOSTBYNAME_LOCK |
---|
5252 | n/a | PyThread_release_lock(netdb_lock); |
---|
5253 | n/a | #endif |
---|
5254 | n/a | finally: |
---|
5255 | n/a | PyMem_Free(ip_num); |
---|
5256 | n/a | return ret; |
---|
5257 | n/a | } |
---|
5258 | n/a | |
---|
5259 | n/a | PyDoc_STRVAR(gethostbyaddr_doc, |
---|
5260 | n/a | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\ |
---|
5261 | n/a | \n\ |
---|
5262 | n/a | Return the true host name, a list of aliases, and a list of IP addresses,\n\ |
---|
5263 | n/a | for a host. The host argument is a string giving a host name or IP number."); |
---|
5264 | n/a | |
---|
5265 | n/a | |
---|
5266 | n/a | /* Python interface to getservbyname(name). |
---|
5267 | n/a | This only returns the port number, since the other info is already |
---|
5268 | n/a | known or not useful (like the list of aliases). */ |
---|
5269 | n/a | |
---|
5270 | n/a | /*ARGSUSED*/ |
---|
5271 | n/a | static PyObject * |
---|
5272 | n/a | socket_getservbyname(PyObject *self, PyObject *args) |
---|
5273 | n/a | { |
---|
5274 | n/a | char *name, *proto=NULL; |
---|
5275 | n/a | struct servent *sp; |
---|
5276 | n/a | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) |
---|
5277 | n/a | return NULL; |
---|
5278 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
5279 | n/a | sp = getservbyname(name, proto); |
---|
5280 | n/a | Py_END_ALLOW_THREADS |
---|
5281 | n/a | if (sp == NULL) { |
---|
5282 | n/a | PyErr_SetString(PyExc_OSError, "service/proto not found"); |
---|
5283 | n/a | return NULL; |
---|
5284 | n/a | } |
---|
5285 | n/a | return PyLong_FromLong((long) ntohs(sp->s_port)); |
---|
5286 | n/a | } |
---|
5287 | n/a | |
---|
5288 | n/a | PyDoc_STRVAR(getservbyname_doc, |
---|
5289 | n/a | "getservbyname(servicename[, protocolname]) -> integer\n\ |
---|
5290 | n/a | \n\ |
---|
5291 | n/a | Return a port number from a service name and protocol name.\n\ |
---|
5292 | n/a | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
---|
5293 | n/a | otherwise any protocol will match."); |
---|
5294 | n/a | |
---|
5295 | n/a | |
---|
5296 | n/a | /* Python interface to getservbyport(port). |
---|
5297 | n/a | This only returns the service name, since the other info is already |
---|
5298 | n/a | known or not useful (like the list of aliases). */ |
---|
5299 | n/a | |
---|
5300 | n/a | /*ARGSUSED*/ |
---|
5301 | n/a | static PyObject * |
---|
5302 | n/a | socket_getservbyport(PyObject *self, PyObject *args) |
---|
5303 | n/a | { |
---|
5304 | n/a | int port; |
---|
5305 | n/a | char *proto=NULL; |
---|
5306 | n/a | struct servent *sp; |
---|
5307 | n/a | if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) |
---|
5308 | n/a | return NULL; |
---|
5309 | n/a | if (port < 0 || port > 0xffff) { |
---|
5310 | n/a | PyErr_SetString( |
---|
5311 | n/a | PyExc_OverflowError, |
---|
5312 | n/a | "getservbyport: port must be 0-65535."); |
---|
5313 | n/a | return NULL; |
---|
5314 | n/a | } |
---|
5315 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
5316 | n/a | sp = getservbyport(htons((short)port), proto); |
---|
5317 | n/a | Py_END_ALLOW_THREADS |
---|
5318 | n/a | if (sp == NULL) { |
---|
5319 | n/a | PyErr_SetString(PyExc_OSError, "port/proto not found"); |
---|
5320 | n/a | return NULL; |
---|
5321 | n/a | } |
---|
5322 | n/a | return PyUnicode_FromString(sp->s_name); |
---|
5323 | n/a | } |
---|
5324 | n/a | |
---|
5325 | n/a | PyDoc_STRVAR(getservbyport_doc, |
---|
5326 | n/a | "getservbyport(port[, protocolname]) -> string\n\ |
---|
5327 | n/a | \n\ |
---|
5328 | n/a | Return the service name from a port number and protocol name.\n\ |
---|
5329 | n/a | The optional protocol name, if given, should be 'tcp' or 'udp',\n\ |
---|
5330 | n/a | otherwise any protocol will match."); |
---|
5331 | n/a | |
---|
5332 | n/a | /* Python interface to getprotobyname(name). |
---|
5333 | n/a | This only returns the protocol number, since the other info is |
---|
5334 | n/a | already known or not useful (like the list of aliases). */ |
---|
5335 | n/a | |
---|
5336 | n/a | /*ARGSUSED*/ |
---|
5337 | n/a | static PyObject * |
---|
5338 | n/a | socket_getprotobyname(PyObject *self, PyObject *args) |
---|
5339 | n/a | { |
---|
5340 | n/a | char *name; |
---|
5341 | n/a | struct protoent *sp; |
---|
5342 | n/a | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) |
---|
5343 | n/a | return NULL; |
---|
5344 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
5345 | n/a | sp = getprotobyname(name); |
---|
5346 | n/a | Py_END_ALLOW_THREADS |
---|
5347 | n/a | if (sp == NULL) { |
---|
5348 | n/a | PyErr_SetString(PyExc_OSError, "protocol not found"); |
---|
5349 | n/a | return NULL; |
---|
5350 | n/a | } |
---|
5351 | n/a | return PyLong_FromLong((long) sp->p_proto); |
---|
5352 | n/a | } |
---|
5353 | n/a | |
---|
5354 | n/a | PyDoc_STRVAR(getprotobyname_doc, |
---|
5355 | n/a | "getprotobyname(name) -> integer\n\ |
---|
5356 | n/a | \n\ |
---|
5357 | n/a | Return the protocol number for the named protocol. (Rarely used.)"); |
---|
5358 | n/a | |
---|
5359 | n/a | |
---|
5360 | n/a | #ifndef NO_DUP |
---|
5361 | n/a | /* dup() function for socket fds */ |
---|
5362 | n/a | |
---|
5363 | n/a | static PyObject * |
---|
5364 | n/a | socket_dup(PyObject *self, PyObject *fdobj) |
---|
5365 | n/a | { |
---|
5366 | n/a | SOCKET_T fd, newfd; |
---|
5367 | n/a | PyObject *newfdobj; |
---|
5368 | n/a | #ifdef MS_WINDOWS |
---|
5369 | n/a | WSAPROTOCOL_INFO info; |
---|
5370 | n/a | #endif |
---|
5371 | n/a | |
---|
5372 | n/a | fd = PyLong_AsSocket_t(fdobj); |
---|
5373 | n/a | if (fd == (SOCKET_T)(-1) && PyErr_Occurred()) |
---|
5374 | n/a | return NULL; |
---|
5375 | n/a | |
---|
5376 | n/a | #ifdef MS_WINDOWS |
---|
5377 | n/a | if (WSADuplicateSocket(fd, GetCurrentProcessId(), &info)) |
---|
5378 | n/a | return set_error(); |
---|
5379 | n/a | |
---|
5380 | n/a | newfd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, |
---|
5381 | n/a | FROM_PROTOCOL_INFO, |
---|
5382 | n/a | &info, 0, WSA_FLAG_OVERLAPPED); |
---|
5383 | n/a | if (newfd == INVALID_SOCKET) |
---|
5384 | n/a | return set_error(); |
---|
5385 | n/a | |
---|
5386 | n/a | if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) { |
---|
5387 | n/a | closesocket(newfd); |
---|
5388 | n/a | PyErr_SetFromWindowsErr(0); |
---|
5389 | n/a | return NULL; |
---|
5390 | n/a | } |
---|
5391 | n/a | #else |
---|
5392 | n/a | /* On UNIX, dup can be used to duplicate the file descriptor of a socket */ |
---|
5393 | n/a | newfd = _Py_dup(fd); |
---|
5394 | n/a | if (newfd == INVALID_SOCKET) |
---|
5395 | n/a | return NULL; |
---|
5396 | n/a | #endif |
---|
5397 | n/a | |
---|
5398 | n/a | newfdobj = PyLong_FromSocket_t(newfd); |
---|
5399 | n/a | if (newfdobj == NULL) |
---|
5400 | n/a | SOCKETCLOSE(newfd); |
---|
5401 | n/a | return newfdobj; |
---|
5402 | n/a | } |
---|
5403 | n/a | |
---|
5404 | n/a | PyDoc_STRVAR(dup_doc, |
---|
5405 | n/a | "dup(integer) -> integer\n\ |
---|
5406 | n/a | \n\ |
---|
5407 | n/a | Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\ |
---|
5408 | n/a | sockets; on some platforms os.dup() won't work for socket file descriptors."); |
---|
5409 | n/a | #endif |
---|
5410 | n/a | |
---|
5411 | n/a | |
---|
5412 | n/a | #ifdef HAVE_SOCKETPAIR |
---|
5413 | n/a | /* Create a pair of sockets using the socketpair() function. |
---|
5414 | n/a | Arguments as for socket() except the default family is AF_UNIX if |
---|
5415 | n/a | defined on the platform; otherwise, the default is AF_INET. */ |
---|
5416 | n/a | |
---|
5417 | n/a | /*ARGSUSED*/ |
---|
5418 | n/a | static PyObject * |
---|
5419 | n/a | socket_socketpair(PyObject *self, PyObject *args) |
---|
5420 | n/a | { |
---|
5421 | n/a | PySocketSockObject *s0 = NULL, *s1 = NULL; |
---|
5422 | n/a | SOCKET_T sv[2]; |
---|
5423 | n/a | int family, type = SOCK_STREAM, proto = 0; |
---|
5424 | n/a | PyObject *res = NULL; |
---|
5425 | n/a | #ifdef SOCK_CLOEXEC |
---|
5426 | n/a | int *atomic_flag_works = &sock_cloexec_works; |
---|
5427 | n/a | #else |
---|
5428 | n/a | int *atomic_flag_works = NULL; |
---|
5429 | n/a | #endif |
---|
5430 | n/a | int ret; |
---|
5431 | n/a | |
---|
5432 | n/a | #if defined(AF_UNIX) |
---|
5433 | n/a | family = AF_UNIX; |
---|
5434 | n/a | #else |
---|
5435 | n/a | family = AF_INET; |
---|
5436 | n/a | #endif |
---|
5437 | n/a | if (!PyArg_ParseTuple(args, "|iii:socketpair", |
---|
5438 | n/a | &family, &type, &proto)) |
---|
5439 | n/a | return NULL; |
---|
5440 | n/a | |
---|
5441 | n/a | /* Create a pair of socket fds */ |
---|
5442 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
5443 | n/a | #ifdef SOCK_CLOEXEC |
---|
5444 | n/a | if (sock_cloexec_works != 0) { |
---|
5445 | n/a | ret = socketpair(family, type | SOCK_CLOEXEC, proto, sv); |
---|
5446 | n/a | if (sock_cloexec_works == -1) { |
---|
5447 | n/a | if (ret >= 0) { |
---|
5448 | n/a | sock_cloexec_works = 1; |
---|
5449 | n/a | } |
---|
5450 | n/a | else if (errno == EINVAL) { |
---|
5451 | n/a | /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */ |
---|
5452 | n/a | sock_cloexec_works = 0; |
---|
5453 | n/a | ret = socketpair(family, type, proto, sv); |
---|
5454 | n/a | } |
---|
5455 | n/a | } |
---|
5456 | n/a | } |
---|
5457 | n/a | else |
---|
5458 | n/a | #endif |
---|
5459 | n/a | { |
---|
5460 | n/a | ret = socketpair(family, type, proto, sv); |
---|
5461 | n/a | } |
---|
5462 | n/a | Py_END_ALLOW_THREADS |
---|
5463 | n/a | |
---|
5464 | n/a | if (ret < 0) |
---|
5465 | n/a | return set_error(); |
---|
5466 | n/a | |
---|
5467 | n/a | if (_Py_set_inheritable(sv[0], 0, atomic_flag_works) < 0) |
---|
5468 | n/a | goto finally; |
---|
5469 | n/a | if (_Py_set_inheritable(sv[1], 0, atomic_flag_works) < 0) |
---|
5470 | n/a | goto finally; |
---|
5471 | n/a | |
---|
5472 | n/a | s0 = new_sockobject(sv[0], family, type, proto); |
---|
5473 | n/a | if (s0 == NULL) |
---|
5474 | n/a | goto finally; |
---|
5475 | n/a | s1 = new_sockobject(sv[1], family, type, proto); |
---|
5476 | n/a | if (s1 == NULL) |
---|
5477 | n/a | goto finally; |
---|
5478 | n/a | res = PyTuple_Pack(2, s0, s1); |
---|
5479 | n/a | |
---|
5480 | n/a | finally: |
---|
5481 | n/a | if (res == NULL) { |
---|
5482 | n/a | if (s0 == NULL) |
---|
5483 | n/a | SOCKETCLOSE(sv[0]); |
---|
5484 | n/a | if (s1 == NULL) |
---|
5485 | n/a | SOCKETCLOSE(sv[1]); |
---|
5486 | n/a | } |
---|
5487 | n/a | Py_XDECREF(s0); |
---|
5488 | n/a | Py_XDECREF(s1); |
---|
5489 | n/a | return res; |
---|
5490 | n/a | } |
---|
5491 | n/a | |
---|
5492 | n/a | PyDoc_STRVAR(socketpair_doc, |
---|
5493 | n/a | "socketpair([family[, type [, proto]]]) -> (socket object, socket object)\n\ |
---|
5494 | n/a | \n\ |
---|
5495 | n/a | Create a pair of socket objects from the sockets returned by the platform\n\ |
---|
5496 | n/a | socketpair() function.\n\ |
---|
5497 | n/a | The arguments are the same as for socket() except the default family is\n\ |
---|
5498 | n/a | AF_UNIX if defined on the platform; otherwise, the default is AF_INET."); |
---|
5499 | n/a | |
---|
5500 | n/a | #endif /* HAVE_SOCKETPAIR */ |
---|
5501 | n/a | |
---|
5502 | n/a | |
---|
5503 | n/a | static PyObject * |
---|
5504 | n/a | socket_ntohs(PyObject *self, PyObject *args) |
---|
5505 | n/a | { |
---|
5506 | n/a | int x; |
---|
5507 | n/a | |
---|
5508 | n/a | if (!PyArg_ParseTuple(args, "i:ntohs", &x)) { |
---|
5509 | n/a | return NULL; |
---|
5510 | n/a | } |
---|
5511 | n/a | if (x < 0) { |
---|
5512 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
5513 | n/a | "ntohs: can't convert negative Python int to C " |
---|
5514 | n/a | "16-bit unsigned integer"); |
---|
5515 | n/a | return NULL; |
---|
5516 | n/a | } |
---|
5517 | n/a | if (x > 0xffff) { |
---|
5518 | n/a | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
---|
5519 | n/a | "ntohs: Python int too large to convert to C " |
---|
5520 | n/a | "16-bit unsigned integer (The silent truncation " |
---|
5521 | n/a | "is deprecated)", |
---|
5522 | n/a | 1)) { |
---|
5523 | n/a | return NULL; |
---|
5524 | n/a | } |
---|
5525 | n/a | } |
---|
5526 | n/a | return PyLong_FromUnsignedLong(ntohs((unsigned short)x)); |
---|
5527 | n/a | } |
---|
5528 | n/a | |
---|
5529 | n/a | PyDoc_STRVAR(ntohs_doc, |
---|
5530 | n/a | "ntohs(integer) -> integer\n\ |
---|
5531 | n/a | \n\ |
---|
5532 | n/a | Convert a 16-bit unsigned integer from network to host byte order.\n\ |
---|
5533 | n/a | Note that in case the received integer does not fit in 16-bit unsigned\n\ |
---|
5534 | n/a | integer, but does fit in a positive C int, it is silently truncated to\n\ |
---|
5535 | n/a | 16-bit unsigned integer.\n\ |
---|
5536 | n/a | However, this silent truncation feature is deprecated, and will raise an \n\ |
---|
5537 | n/a | exception in future versions of Python."); |
---|
5538 | n/a | |
---|
5539 | n/a | |
---|
5540 | n/a | static PyObject * |
---|
5541 | n/a | socket_ntohl(PyObject *self, PyObject *arg) |
---|
5542 | n/a | { |
---|
5543 | n/a | unsigned long x; |
---|
5544 | n/a | |
---|
5545 | n/a | if (PyLong_Check(arg)) { |
---|
5546 | n/a | x = PyLong_AsUnsignedLong(arg); |
---|
5547 | n/a | if (x == (unsigned long) -1 && PyErr_Occurred()) |
---|
5548 | n/a | return NULL; |
---|
5549 | n/a | #if SIZEOF_LONG > 4 |
---|
5550 | n/a | { |
---|
5551 | n/a | unsigned long y; |
---|
5552 | n/a | /* only want the trailing 32 bits */ |
---|
5553 | n/a | y = x & 0xFFFFFFFFUL; |
---|
5554 | n/a | if (y ^ x) |
---|
5555 | n/a | return PyErr_Format(PyExc_OverflowError, |
---|
5556 | n/a | "int larger than 32 bits"); |
---|
5557 | n/a | x = y; |
---|
5558 | n/a | } |
---|
5559 | n/a | #endif |
---|
5560 | n/a | } |
---|
5561 | n/a | else |
---|
5562 | n/a | return PyErr_Format(PyExc_TypeError, |
---|
5563 | n/a | "expected int, %s found", |
---|
5564 | n/a | Py_TYPE(arg)->tp_name); |
---|
5565 | n/a | return PyLong_FromUnsignedLong(ntohl(x)); |
---|
5566 | n/a | } |
---|
5567 | n/a | |
---|
5568 | n/a | PyDoc_STRVAR(ntohl_doc, |
---|
5569 | n/a | "ntohl(integer) -> integer\n\ |
---|
5570 | n/a | \n\ |
---|
5571 | n/a | Convert a 32-bit integer from network to host byte order."); |
---|
5572 | n/a | |
---|
5573 | n/a | |
---|
5574 | n/a | static PyObject * |
---|
5575 | n/a | socket_htons(PyObject *self, PyObject *args) |
---|
5576 | n/a | { |
---|
5577 | n/a | int x; |
---|
5578 | n/a | |
---|
5579 | n/a | if (!PyArg_ParseTuple(args, "i:htons", &x)) { |
---|
5580 | n/a | return NULL; |
---|
5581 | n/a | } |
---|
5582 | n/a | if (x < 0) { |
---|
5583 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
5584 | n/a | "htons: can't convert negative Python int to C " |
---|
5585 | n/a | "16-bit unsigned integer"); |
---|
5586 | n/a | return NULL; |
---|
5587 | n/a | } |
---|
5588 | n/a | if (x > 0xffff) { |
---|
5589 | n/a | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
---|
5590 | n/a | "htons: Python int too large to convert to C " |
---|
5591 | n/a | "16-bit unsigned integer (The silent truncation " |
---|
5592 | n/a | "is deprecated)", |
---|
5593 | n/a | 1)) { |
---|
5594 | n/a | return NULL; |
---|
5595 | n/a | } |
---|
5596 | n/a | } |
---|
5597 | n/a | return PyLong_FromUnsignedLong(htons((unsigned short)x)); |
---|
5598 | n/a | } |
---|
5599 | n/a | |
---|
5600 | n/a | PyDoc_STRVAR(htons_doc, |
---|
5601 | n/a | "htons(integer) -> integer\n\ |
---|
5602 | n/a | \n\ |
---|
5603 | n/a | Convert a 16-bit unsigned integer from host to network byte order.\n\ |
---|
5604 | n/a | Note that in case the received integer does not fit in 16-bit unsigned\n\ |
---|
5605 | n/a | integer, but does fit in a positive C int, it is silently truncated to\n\ |
---|
5606 | n/a | 16-bit unsigned integer.\n\ |
---|
5607 | n/a | However, this silent truncation feature is deprecated, and will raise an \n\ |
---|
5608 | n/a | exception in future versions of Python."); |
---|
5609 | n/a | |
---|
5610 | n/a | |
---|
5611 | n/a | static PyObject * |
---|
5612 | n/a | socket_htonl(PyObject *self, PyObject *arg) |
---|
5613 | n/a | { |
---|
5614 | n/a | unsigned long x; |
---|
5615 | n/a | |
---|
5616 | n/a | if (PyLong_Check(arg)) { |
---|
5617 | n/a | x = PyLong_AsUnsignedLong(arg); |
---|
5618 | n/a | if (x == (unsigned long) -1 && PyErr_Occurred()) |
---|
5619 | n/a | return NULL; |
---|
5620 | n/a | #if SIZEOF_LONG > 4 |
---|
5621 | n/a | { |
---|
5622 | n/a | unsigned long y; |
---|
5623 | n/a | /* only want the trailing 32 bits */ |
---|
5624 | n/a | y = x & 0xFFFFFFFFUL; |
---|
5625 | n/a | if (y ^ x) |
---|
5626 | n/a | return PyErr_Format(PyExc_OverflowError, |
---|
5627 | n/a | "int larger than 32 bits"); |
---|
5628 | n/a | x = y; |
---|
5629 | n/a | } |
---|
5630 | n/a | #endif |
---|
5631 | n/a | } |
---|
5632 | n/a | else |
---|
5633 | n/a | return PyErr_Format(PyExc_TypeError, |
---|
5634 | n/a | "expected int, %s found", |
---|
5635 | n/a | Py_TYPE(arg)->tp_name); |
---|
5636 | n/a | return PyLong_FromUnsignedLong(htonl((unsigned long)x)); |
---|
5637 | n/a | } |
---|
5638 | n/a | |
---|
5639 | n/a | PyDoc_STRVAR(htonl_doc, |
---|
5640 | n/a | "htonl(integer) -> integer\n\ |
---|
5641 | n/a | \n\ |
---|
5642 | n/a | Convert a 32-bit integer from host to network byte order."); |
---|
5643 | n/a | |
---|
5644 | n/a | /* socket.inet_aton() and socket.inet_ntoa() functions. */ |
---|
5645 | n/a | |
---|
5646 | n/a | PyDoc_STRVAR(inet_aton_doc, |
---|
5647 | n/a | "inet_aton(string) -> bytes giving packed 32-bit IP representation\n\ |
---|
5648 | n/a | \n\ |
---|
5649 | n/a | Convert an IP address in string format (123.45.67.89) to the 32-bit packed\n\ |
---|
5650 | n/a | binary format used in low-level network functions."); |
---|
5651 | n/a | |
---|
5652 | n/a | static PyObject* |
---|
5653 | n/a | socket_inet_aton(PyObject *self, PyObject *args) |
---|
5654 | n/a | { |
---|
5655 | n/a | #ifdef HAVE_INET_ATON |
---|
5656 | n/a | struct in_addr buf; |
---|
5657 | n/a | #endif |
---|
5658 | n/a | |
---|
5659 | n/a | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
---|
5660 | n/a | #if (SIZEOF_INT != 4) |
---|
5661 | n/a | #error "Not sure if in_addr_t exists and int is not 32-bits." |
---|
5662 | n/a | #endif |
---|
5663 | n/a | /* Have to use inet_addr() instead */ |
---|
5664 | n/a | unsigned int packed_addr; |
---|
5665 | n/a | #endif |
---|
5666 | n/a | char *ip_addr; |
---|
5667 | n/a | |
---|
5668 | n/a | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) |
---|
5669 | n/a | return NULL; |
---|
5670 | n/a | |
---|
5671 | n/a | |
---|
5672 | n/a | #ifdef HAVE_INET_ATON |
---|
5673 | n/a | |
---|
5674 | n/a | #ifdef USE_INET_ATON_WEAKLINK |
---|
5675 | n/a | if (inet_aton != NULL) { |
---|
5676 | n/a | #endif |
---|
5677 | n/a | if (inet_aton(ip_addr, &buf)) |
---|
5678 | n/a | return PyBytes_FromStringAndSize((char *)(&buf), |
---|
5679 | n/a | sizeof(buf)); |
---|
5680 | n/a | |
---|
5681 | n/a | PyErr_SetString(PyExc_OSError, |
---|
5682 | n/a | "illegal IP address string passed to inet_aton"); |
---|
5683 | n/a | return NULL; |
---|
5684 | n/a | |
---|
5685 | n/a | #ifdef USE_INET_ATON_WEAKLINK |
---|
5686 | n/a | } else { |
---|
5687 | n/a | #endif |
---|
5688 | n/a | |
---|
5689 | n/a | #endif |
---|
5690 | n/a | |
---|
5691 | n/a | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) |
---|
5692 | n/a | |
---|
5693 | n/a | /* special-case this address as inet_addr might return INADDR_NONE |
---|
5694 | n/a | * for this */ |
---|
5695 | n/a | if (strcmp(ip_addr, "255.255.255.255") == 0) { |
---|
5696 | n/a | packed_addr = INADDR_BROADCAST; |
---|
5697 | n/a | } else { |
---|
5698 | n/a | |
---|
5699 | n/a | packed_addr = inet_addr(ip_addr); |
---|
5700 | n/a | |
---|
5701 | n/a | if (packed_addr == INADDR_NONE) { /* invalid address */ |
---|
5702 | n/a | PyErr_SetString(PyExc_OSError, |
---|
5703 | n/a | "illegal IP address string passed to inet_aton"); |
---|
5704 | n/a | return NULL; |
---|
5705 | n/a | } |
---|
5706 | n/a | } |
---|
5707 | n/a | return PyBytes_FromStringAndSize((char *) &packed_addr, |
---|
5708 | n/a | sizeof(packed_addr)); |
---|
5709 | n/a | |
---|
5710 | n/a | #ifdef USE_INET_ATON_WEAKLINK |
---|
5711 | n/a | } |
---|
5712 | n/a | #endif |
---|
5713 | n/a | |
---|
5714 | n/a | #endif |
---|
5715 | n/a | } |
---|
5716 | n/a | |
---|
5717 | n/a | PyDoc_STRVAR(inet_ntoa_doc, |
---|
5718 | n/a | "inet_ntoa(packed_ip) -> ip_address_string\n\ |
---|
5719 | n/a | \n\ |
---|
5720 | n/a | Convert an IP address from 32-bit packed binary format to string format"); |
---|
5721 | n/a | |
---|
5722 | n/a | static PyObject* |
---|
5723 | n/a | socket_inet_ntoa(PyObject *self, PyObject *args) |
---|
5724 | n/a | { |
---|
5725 | n/a | Py_buffer packed_ip; |
---|
5726 | n/a | struct in_addr packed_addr; |
---|
5727 | n/a | |
---|
5728 | n/a | if (!PyArg_ParseTuple(args, "y*:inet_ntoa", &packed_ip)) { |
---|
5729 | n/a | return NULL; |
---|
5730 | n/a | } |
---|
5731 | n/a | |
---|
5732 | n/a | if (packed_ip.len != sizeof(packed_addr)) { |
---|
5733 | n/a | PyErr_SetString(PyExc_OSError, |
---|
5734 | n/a | "packed IP wrong length for inet_ntoa"); |
---|
5735 | n/a | PyBuffer_Release(&packed_ip); |
---|
5736 | n/a | return NULL; |
---|
5737 | n/a | } |
---|
5738 | n/a | |
---|
5739 | n/a | memcpy(&packed_addr, packed_ip.buf, packed_ip.len); |
---|
5740 | n/a | PyBuffer_Release(&packed_ip); |
---|
5741 | n/a | |
---|
5742 | n/a | return PyUnicode_FromString(inet_ntoa(packed_addr)); |
---|
5743 | n/a | } |
---|
5744 | n/a | |
---|
5745 | n/a | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
---|
5746 | n/a | |
---|
5747 | n/a | PyDoc_STRVAR(inet_pton_doc, |
---|
5748 | n/a | "inet_pton(af, ip) -> packed IP address string\n\ |
---|
5749 | n/a | \n\ |
---|
5750 | n/a | Convert an IP address from string format to a packed string suitable\n\ |
---|
5751 | n/a | for use with low-level network functions."); |
---|
5752 | n/a | |
---|
5753 | n/a | #endif |
---|
5754 | n/a | |
---|
5755 | n/a | #ifdef HAVE_INET_PTON |
---|
5756 | n/a | |
---|
5757 | n/a | static PyObject * |
---|
5758 | n/a | socket_inet_pton(PyObject *self, PyObject *args) |
---|
5759 | n/a | { |
---|
5760 | n/a | int af; |
---|
5761 | n/a | char* ip; |
---|
5762 | n/a | int retval; |
---|
5763 | n/a | #ifdef ENABLE_IPV6 |
---|
5764 | n/a | char packed[Py_MAX(sizeof(struct in_addr), sizeof(struct in6_addr))]; |
---|
5765 | n/a | #else |
---|
5766 | n/a | char packed[sizeof(struct in_addr)]; |
---|
5767 | n/a | #endif |
---|
5768 | n/a | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
---|
5769 | n/a | return NULL; |
---|
5770 | n/a | } |
---|
5771 | n/a | |
---|
5772 | n/a | #if !defined(ENABLE_IPV6) && defined(AF_INET6) |
---|
5773 | n/a | if(af == AF_INET6) { |
---|
5774 | n/a | PyErr_SetString(PyExc_OSError, |
---|
5775 | n/a | "can't use AF_INET6, IPv6 is disabled"); |
---|
5776 | n/a | return NULL; |
---|
5777 | n/a | } |
---|
5778 | n/a | #endif |
---|
5779 | n/a | |
---|
5780 | n/a | retval = inet_pton(af, ip, packed); |
---|
5781 | n/a | if (retval < 0) { |
---|
5782 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
5783 | n/a | return NULL; |
---|
5784 | n/a | } else if (retval == 0) { |
---|
5785 | n/a | PyErr_SetString(PyExc_OSError, |
---|
5786 | n/a | "illegal IP address string passed to inet_pton"); |
---|
5787 | n/a | return NULL; |
---|
5788 | n/a | } else if (af == AF_INET) { |
---|
5789 | n/a | return PyBytes_FromStringAndSize(packed, |
---|
5790 | n/a | sizeof(struct in_addr)); |
---|
5791 | n/a | #ifdef ENABLE_IPV6 |
---|
5792 | n/a | } else if (af == AF_INET6) { |
---|
5793 | n/a | return PyBytes_FromStringAndSize(packed, |
---|
5794 | n/a | sizeof(struct in6_addr)); |
---|
5795 | n/a | #endif |
---|
5796 | n/a | } else { |
---|
5797 | n/a | PyErr_SetString(PyExc_OSError, "unknown address family"); |
---|
5798 | n/a | return NULL; |
---|
5799 | n/a | } |
---|
5800 | n/a | } |
---|
5801 | n/a | #elif defined(MS_WINDOWS) |
---|
5802 | n/a | |
---|
5803 | n/a | static PyObject * |
---|
5804 | n/a | socket_inet_pton(PyObject *self, PyObject *args) |
---|
5805 | n/a | { |
---|
5806 | n/a | int af; |
---|
5807 | n/a | char* ip; |
---|
5808 | n/a | struct sockaddr_in6 addr; |
---|
5809 | n/a | INT ret, size; |
---|
5810 | n/a | |
---|
5811 | n/a | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { |
---|
5812 | n/a | return NULL; |
---|
5813 | n/a | } |
---|
5814 | n/a | |
---|
5815 | n/a | size = sizeof(addr); |
---|
5816 | n/a | ret = WSAStringToAddressA(ip, af, NULL, (LPSOCKADDR)&addr, &size); |
---|
5817 | n/a | |
---|
5818 | n/a | if (ret) { |
---|
5819 | n/a | PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
---|
5820 | n/a | return NULL; |
---|
5821 | n/a | } else if(af == AF_INET) { |
---|
5822 | n/a | struct sockaddr_in *addr4 = (struct sockaddr_in*)&addr; |
---|
5823 | n/a | return PyBytes_FromStringAndSize((const char *)&(addr4->sin_addr), |
---|
5824 | n/a | sizeof(addr4->sin_addr)); |
---|
5825 | n/a | } else if (af == AF_INET6) { |
---|
5826 | n/a | return PyBytes_FromStringAndSize((const char *)&(addr.sin6_addr), |
---|
5827 | n/a | sizeof(addr.sin6_addr)); |
---|
5828 | n/a | } else { |
---|
5829 | n/a | PyErr_SetString(PyExc_OSError, "unknown address family"); |
---|
5830 | n/a | return NULL; |
---|
5831 | n/a | } |
---|
5832 | n/a | } |
---|
5833 | n/a | |
---|
5834 | n/a | #endif |
---|
5835 | n/a | |
---|
5836 | n/a | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
---|
5837 | n/a | |
---|
5838 | n/a | PyDoc_STRVAR(inet_ntop_doc, |
---|
5839 | n/a | "inet_ntop(af, packed_ip) -> string formatted IP address\n\ |
---|
5840 | n/a | \n\ |
---|
5841 | n/a | Convert a packed IP address of the given family to string format."); |
---|
5842 | n/a | |
---|
5843 | n/a | #endif |
---|
5844 | n/a | |
---|
5845 | n/a | |
---|
5846 | n/a | #ifdef HAVE_INET_PTON |
---|
5847 | n/a | static PyObject * |
---|
5848 | n/a | socket_inet_ntop(PyObject *self, PyObject *args) |
---|
5849 | n/a | { |
---|
5850 | n/a | int af; |
---|
5851 | n/a | Py_buffer packed_ip; |
---|
5852 | n/a | const char* retval; |
---|
5853 | n/a | #ifdef ENABLE_IPV6 |
---|
5854 | n/a | char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
---|
5855 | n/a | #else |
---|
5856 | n/a | char ip[INET_ADDRSTRLEN + 1]; |
---|
5857 | n/a | #endif |
---|
5858 | n/a | |
---|
5859 | n/a | /* Guarantee NUL-termination for PyUnicode_FromString() below */ |
---|
5860 | n/a | memset((void *) &ip[0], '\0', sizeof(ip)); |
---|
5861 | n/a | |
---|
5862 | n/a | if (!PyArg_ParseTuple(args, "iy*:inet_ntop", &af, &packed_ip)) { |
---|
5863 | n/a | return NULL; |
---|
5864 | n/a | } |
---|
5865 | n/a | |
---|
5866 | n/a | if (af == AF_INET) { |
---|
5867 | n/a | if (packed_ip.len != sizeof(struct in_addr)) { |
---|
5868 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
5869 | n/a | "invalid length of packed IP address string"); |
---|
5870 | n/a | PyBuffer_Release(&packed_ip); |
---|
5871 | n/a | return NULL; |
---|
5872 | n/a | } |
---|
5873 | n/a | #ifdef ENABLE_IPV6 |
---|
5874 | n/a | } else if (af == AF_INET6) { |
---|
5875 | n/a | if (packed_ip.len != sizeof(struct in6_addr)) { |
---|
5876 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
5877 | n/a | "invalid length of packed IP address string"); |
---|
5878 | n/a | PyBuffer_Release(&packed_ip); |
---|
5879 | n/a | return NULL; |
---|
5880 | n/a | } |
---|
5881 | n/a | #endif |
---|
5882 | n/a | } else { |
---|
5883 | n/a | PyErr_Format(PyExc_ValueError, |
---|
5884 | n/a | "unknown address family %d", af); |
---|
5885 | n/a | PyBuffer_Release(&packed_ip); |
---|
5886 | n/a | return NULL; |
---|
5887 | n/a | } |
---|
5888 | n/a | |
---|
5889 | n/a | retval = inet_ntop(af, packed_ip.buf, ip, sizeof(ip)); |
---|
5890 | n/a | PyBuffer_Release(&packed_ip); |
---|
5891 | n/a | if (!retval) { |
---|
5892 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
5893 | n/a | return NULL; |
---|
5894 | n/a | } else { |
---|
5895 | n/a | return PyUnicode_FromString(retval); |
---|
5896 | n/a | } |
---|
5897 | n/a | } |
---|
5898 | n/a | |
---|
5899 | n/a | #elif defined(MS_WINDOWS) |
---|
5900 | n/a | |
---|
5901 | n/a | static PyObject * |
---|
5902 | n/a | socket_inet_ntop(PyObject *self, PyObject *args) |
---|
5903 | n/a | { |
---|
5904 | n/a | int af; |
---|
5905 | n/a | Py_buffer packed_ip; |
---|
5906 | n/a | struct sockaddr_in6 addr; |
---|
5907 | n/a | DWORD addrlen, ret, retlen; |
---|
5908 | n/a | #ifdef ENABLE_IPV6 |
---|
5909 | n/a | char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; |
---|
5910 | n/a | #else |
---|
5911 | n/a | char ip[INET_ADDRSTRLEN + 1]; |
---|
5912 | n/a | #endif |
---|
5913 | n/a | |
---|
5914 | n/a | /* Guarantee NUL-termination for PyUnicode_FromString() below */ |
---|
5915 | n/a | memset((void *) &ip[0], '\0', sizeof(ip)); |
---|
5916 | n/a | |
---|
5917 | n/a | if (!PyArg_ParseTuple(args, "iy*:inet_ntop", &af, &packed_ip)) { |
---|
5918 | n/a | return NULL; |
---|
5919 | n/a | } |
---|
5920 | n/a | |
---|
5921 | n/a | if (af == AF_INET) { |
---|
5922 | n/a | struct sockaddr_in * addr4 = (struct sockaddr_in *)&addr; |
---|
5923 | n/a | |
---|
5924 | n/a | if (packed_ip.len != sizeof(struct in_addr)) { |
---|
5925 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
5926 | n/a | "invalid length of packed IP address string"); |
---|
5927 | n/a | PyBuffer_Release(&packed_ip); |
---|
5928 | n/a | return NULL; |
---|
5929 | n/a | } |
---|
5930 | n/a | memset(addr4, 0, sizeof(struct sockaddr_in)); |
---|
5931 | n/a | addr4->sin_family = AF_INET; |
---|
5932 | n/a | memcpy(&(addr4->sin_addr), packed_ip.buf, sizeof(addr4->sin_addr)); |
---|
5933 | n/a | addrlen = sizeof(struct sockaddr_in); |
---|
5934 | n/a | } else if (af == AF_INET6) { |
---|
5935 | n/a | if (packed_ip.len != sizeof(struct in6_addr)) { |
---|
5936 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
5937 | n/a | "invalid length of packed IP address string"); |
---|
5938 | n/a | PyBuffer_Release(&packed_ip); |
---|
5939 | n/a | return NULL; |
---|
5940 | n/a | } |
---|
5941 | n/a | |
---|
5942 | n/a | memset(&addr, 0, sizeof(addr)); |
---|
5943 | n/a | addr.sin6_family = AF_INET6; |
---|
5944 | n/a | memcpy(&(addr.sin6_addr), packed_ip.buf, sizeof(addr.sin6_addr)); |
---|
5945 | n/a | addrlen = sizeof(addr); |
---|
5946 | n/a | } else { |
---|
5947 | n/a | PyErr_Format(PyExc_ValueError, |
---|
5948 | n/a | "unknown address family %d", af); |
---|
5949 | n/a | PyBuffer_Release(&packed_ip); |
---|
5950 | n/a | return NULL; |
---|
5951 | n/a | } |
---|
5952 | n/a | PyBuffer_Release(&packed_ip); |
---|
5953 | n/a | |
---|
5954 | n/a | retlen = sizeof(ip); |
---|
5955 | n/a | ret = WSAAddressToStringA((struct sockaddr*)&addr, addrlen, NULL, |
---|
5956 | n/a | ip, &retlen); |
---|
5957 | n/a | |
---|
5958 | n/a | if (ret) { |
---|
5959 | n/a | PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
---|
5960 | n/a | return NULL; |
---|
5961 | n/a | } else { |
---|
5962 | n/a | return PyUnicode_FromString(ip); |
---|
5963 | n/a | } |
---|
5964 | n/a | } |
---|
5965 | n/a | |
---|
5966 | n/a | #endif /* HAVE_INET_PTON */ |
---|
5967 | n/a | |
---|
5968 | n/a | /* Python interface to getaddrinfo(host, port). */ |
---|
5969 | n/a | |
---|
5970 | n/a | /*ARGSUSED*/ |
---|
5971 | n/a | static PyObject * |
---|
5972 | n/a | socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs) |
---|
5973 | n/a | { |
---|
5974 | n/a | static char* kwnames[] = {"host", "port", "family", "type", "proto", |
---|
5975 | n/a | "flags", 0}; |
---|
5976 | n/a | struct addrinfo hints, *res; |
---|
5977 | n/a | struct addrinfo *res0 = NULL; |
---|
5978 | n/a | PyObject *hobj = NULL; |
---|
5979 | n/a | PyObject *pobj = (PyObject *)NULL; |
---|
5980 | n/a | char pbuf[30]; |
---|
5981 | n/a | const char *hptr, *pptr; |
---|
5982 | n/a | int family, socktype, protocol, flags; |
---|
5983 | n/a | int error; |
---|
5984 | n/a | PyObject *all = (PyObject *)NULL; |
---|
5985 | n/a | PyObject *idna = NULL; |
---|
5986 | n/a | |
---|
5987 | n/a | socktype = protocol = flags = 0; |
---|
5988 | n/a | family = AF_UNSPEC; |
---|
5989 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iiii:getaddrinfo", |
---|
5990 | n/a | kwnames, &hobj, &pobj, &family, &socktype, |
---|
5991 | n/a | &protocol, &flags)) { |
---|
5992 | n/a | return NULL; |
---|
5993 | n/a | } |
---|
5994 | n/a | if (hobj == Py_None) { |
---|
5995 | n/a | hptr = NULL; |
---|
5996 | n/a | } else if (PyUnicode_Check(hobj)) { |
---|
5997 | n/a | idna = PyUnicode_AsEncodedString(hobj, "idna", NULL); |
---|
5998 | n/a | if (!idna) |
---|
5999 | n/a | return NULL; |
---|
6000 | n/a | assert(PyBytes_Check(idna)); |
---|
6001 | n/a | hptr = PyBytes_AS_STRING(idna); |
---|
6002 | n/a | } else if (PyBytes_Check(hobj)) { |
---|
6003 | n/a | hptr = PyBytes_AsString(hobj); |
---|
6004 | n/a | } else { |
---|
6005 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
6006 | n/a | "getaddrinfo() argument 1 must be string or None"); |
---|
6007 | n/a | return NULL; |
---|
6008 | n/a | } |
---|
6009 | n/a | if (PyLong_CheckExact(pobj)) { |
---|
6010 | n/a | long value = PyLong_AsLong(pobj); |
---|
6011 | n/a | if (value == -1 && PyErr_Occurred()) |
---|
6012 | n/a | goto err; |
---|
6013 | n/a | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value); |
---|
6014 | n/a | pptr = pbuf; |
---|
6015 | n/a | } else if (PyUnicode_Check(pobj)) { |
---|
6016 | n/a | pptr = PyUnicode_AsUTF8(pobj); |
---|
6017 | n/a | if (pptr == NULL) |
---|
6018 | n/a | goto err; |
---|
6019 | n/a | } else if (PyBytes_Check(pobj)) { |
---|
6020 | n/a | pptr = PyBytes_AS_STRING(pobj); |
---|
6021 | n/a | } else if (pobj == Py_None) { |
---|
6022 | n/a | pptr = (char *)NULL; |
---|
6023 | n/a | } else { |
---|
6024 | n/a | PyErr_SetString(PyExc_OSError, "Int or String expected"); |
---|
6025 | n/a | goto err; |
---|
6026 | n/a | } |
---|
6027 | n/a | #if defined(__APPLE__) && defined(AI_NUMERICSERV) |
---|
6028 | n/a | if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) { |
---|
6029 | n/a | /* On OSX upto at least OSX 10.8 getaddrinfo crashes |
---|
6030 | n/a | * if AI_NUMERICSERV is set and the servname is NULL or "0". |
---|
6031 | n/a | * This workaround avoids a segfault in libsystem. |
---|
6032 | n/a | */ |
---|
6033 | n/a | pptr = "00"; |
---|
6034 | n/a | } |
---|
6035 | n/a | #endif |
---|
6036 | n/a | memset(&hints, 0, sizeof(hints)); |
---|
6037 | n/a | hints.ai_family = family; |
---|
6038 | n/a | hints.ai_socktype = socktype; |
---|
6039 | n/a | hints.ai_protocol = protocol; |
---|
6040 | n/a | hints.ai_flags = flags; |
---|
6041 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
6042 | n/a | ACQUIRE_GETADDRINFO_LOCK |
---|
6043 | n/a | error = getaddrinfo(hptr, pptr, &hints, &res0); |
---|
6044 | n/a | Py_END_ALLOW_THREADS |
---|
6045 | n/a | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
---|
6046 | n/a | if (error) { |
---|
6047 | n/a | set_gaierror(error); |
---|
6048 | n/a | goto err; |
---|
6049 | n/a | } |
---|
6050 | n/a | |
---|
6051 | n/a | all = PyList_New(0); |
---|
6052 | n/a | if (all == NULL) |
---|
6053 | n/a | goto err; |
---|
6054 | n/a | for (res = res0; res; res = res->ai_next) { |
---|
6055 | n/a | PyObject *single; |
---|
6056 | n/a | PyObject *addr = |
---|
6057 | n/a | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); |
---|
6058 | n/a | if (addr == NULL) |
---|
6059 | n/a | goto err; |
---|
6060 | n/a | single = Py_BuildValue("iiisO", res->ai_family, |
---|
6061 | n/a | res->ai_socktype, res->ai_protocol, |
---|
6062 | n/a | res->ai_canonname ? res->ai_canonname : "", |
---|
6063 | n/a | addr); |
---|
6064 | n/a | Py_DECREF(addr); |
---|
6065 | n/a | if (single == NULL) |
---|
6066 | n/a | goto err; |
---|
6067 | n/a | |
---|
6068 | n/a | if (PyList_Append(all, single)) |
---|
6069 | n/a | goto err; |
---|
6070 | n/a | Py_XDECREF(single); |
---|
6071 | n/a | } |
---|
6072 | n/a | Py_XDECREF(idna); |
---|
6073 | n/a | if (res0) |
---|
6074 | n/a | freeaddrinfo(res0); |
---|
6075 | n/a | return all; |
---|
6076 | n/a | err: |
---|
6077 | n/a | Py_XDECREF(all); |
---|
6078 | n/a | Py_XDECREF(idna); |
---|
6079 | n/a | if (res0) |
---|
6080 | n/a | freeaddrinfo(res0); |
---|
6081 | n/a | return (PyObject *)NULL; |
---|
6082 | n/a | } |
---|
6083 | n/a | |
---|
6084 | n/a | PyDoc_STRVAR(getaddrinfo_doc, |
---|
6085 | n/a | "getaddrinfo(host, port [, family, type, proto, flags])\n\ |
---|
6086 | n/a | -> list of (family, type, proto, canonname, sockaddr)\n\ |
---|
6087 | n/a | \n\ |
---|
6088 | n/a | Resolve host and port into addrinfo struct."); |
---|
6089 | n/a | |
---|
6090 | n/a | /* Python interface to getnameinfo(sa, flags). */ |
---|
6091 | n/a | |
---|
6092 | n/a | /*ARGSUSED*/ |
---|
6093 | n/a | static PyObject * |
---|
6094 | n/a | socket_getnameinfo(PyObject *self, PyObject *args) |
---|
6095 | n/a | { |
---|
6096 | n/a | PyObject *sa = (PyObject *)NULL; |
---|
6097 | n/a | int flags; |
---|
6098 | n/a | char *hostp; |
---|
6099 | n/a | int port; |
---|
6100 | n/a | unsigned int flowinfo, scope_id; |
---|
6101 | n/a | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; |
---|
6102 | n/a | struct addrinfo hints, *res = NULL; |
---|
6103 | n/a | int error; |
---|
6104 | n/a | PyObject *ret = (PyObject *)NULL; |
---|
6105 | n/a | PyObject *name; |
---|
6106 | n/a | |
---|
6107 | n/a | flags = flowinfo = scope_id = 0; |
---|
6108 | n/a | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) |
---|
6109 | n/a | return NULL; |
---|
6110 | n/a | if (!PyTuple_Check(sa)) { |
---|
6111 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
6112 | n/a | "getnameinfo() argument 1 must be a tuple"); |
---|
6113 | n/a | return NULL; |
---|
6114 | n/a | } |
---|
6115 | n/a | if (!PyArg_ParseTuple(sa, "si|II", |
---|
6116 | n/a | &hostp, &port, &flowinfo, &scope_id)) |
---|
6117 | n/a | return NULL; |
---|
6118 | n/a | if (flowinfo > 0xfffff) { |
---|
6119 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
6120 | n/a | "getsockaddrarg: flowinfo must be 0-1048575."); |
---|
6121 | n/a | return NULL; |
---|
6122 | n/a | } |
---|
6123 | n/a | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); |
---|
6124 | n/a | memset(&hints, 0, sizeof(hints)); |
---|
6125 | n/a | hints.ai_family = AF_UNSPEC; |
---|
6126 | n/a | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ |
---|
6127 | n/a | hints.ai_flags = AI_NUMERICHOST; /* don't do any name resolution */ |
---|
6128 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
6129 | n/a | ACQUIRE_GETADDRINFO_LOCK |
---|
6130 | n/a | error = getaddrinfo(hostp, pbuf, &hints, &res); |
---|
6131 | n/a | Py_END_ALLOW_THREADS |
---|
6132 | n/a | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ |
---|
6133 | n/a | if (error) { |
---|
6134 | n/a | set_gaierror(error); |
---|
6135 | n/a | goto fail; |
---|
6136 | n/a | } |
---|
6137 | n/a | if (res->ai_next) { |
---|
6138 | n/a | PyErr_SetString(PyExc_OSError, |
---|
6139 | n/a | "sockaddr resolved to multiple addresses"); |
---|
6140 | n/a | goto fail; |
---|
6141 | n/a | } |
---|
6142 | n/a | switch (res->ai_family) { |
---|
6143 | n/a | case AF_INET: |
---|
6144 | n/a | { |
---|
6145 | n/a | if (PyTuple_GET_SIZE(sa) != 2) { |
---|
6146 | n/a | PyErr_SetString(PyExc_OSError, |
---|
6147 | n/a | "IPv4 sockaddr must be 2 tuple"); |
---|
6148 | n/a | goto fail; |
---|
6149 | n/a | } |
---|
6150 | n/a | break; |
---|
6151 | n/a | } |
---|
6152 | n/a | #ifdef ENABLE_IPV6 |
---|
6153 | n/a | case AF_INET6: |
---|
6154 | n/a | { |
---|
6155 | n/a | struct sockaddr_in6 *sin6; |
---|
6156 | n/a | sin6 = (struct sockaddr_in6 *)res->ai_addr; |
---|
6157 | n/a | sin6->sin6_flowinfo = htonl(flowinfo); |
---|
6158 | n/a | sin6->sin6_scope_id = scope_id; |
---|
6159 | n/a | break; |
---|
6160 | n/a | } |
---|
6161 | n/a | #endif |
---|
6162 | n/a | } |
---|
6163 | n/a | error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen, |
---|
6164 | n/a | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); |
---|
6165 | n/a | if (error) { |
---|
6166 | n/a | set_gaierror(error); |
---|
6167 | n/a | goto fail; |
---|
6168 | n/a | } |
---|
6169 | n/a | |
---|
6170 | n/a | name = sock_decode_hostname(hbuf); |
---|
6171 | n/a | if (name == NULL) |
---|
6172 | n/a | goto fail; |
---|
6173 | n/a | ret = Py_BuildValue("Ns", name, pbuf); |
---|
6174 | n/a | |
---|
6175 | n/a | fail: |
---|
6176 | n/a | if (res) |
---|
6177 | n/a | freeaddrinfo(res); |
---|
6178 | n/a | return ret; |
---|
6179 | n/a | } |
---|
6180 | n/a | |
---|
6181 | n/a | PyDoc_STRVAR(getnameinfo_doc, |
---|
6182 | n/a | "getnameinfo(sockaddr, flags) --> (host, port)\n\ |
---|
6183 | n/a | \n\ |
---|
6184 | n/a | Get host and port for a sockaddr."); |
---|
6185 | n/a | |
---|
6186 | n/a | |
---|
6187 | n/a | /* Python API to getting and setting the default timeout value. */ |
---|
6188 | n/a | |
---|
6189 | n/a | static PyObject * |
---|
6190 | n/a | socket_getdefaulttimeout(PyObject *self) |
---|
6191 | n/a | { |
---|
6192 | n/a | if (defaulttimeout < 0) { |
---|
6193 | n/a | Py_RETURN_NONE; |
---|
6194 | n/a | } |
---|
6195 | n/a | else { |
---|
6196 | n/a | double seconds = _PyTime_AsSecondsDouble(defaulttimeout); |
---|
6197 | n/a | return PyFloat_FromDouble(seconds); |
---|
6198 | n/a | } |
---|
6199 | n/a | } |
---|
6200 | n/a | |
---|
6201 | n/a | PyDoc_STRVAR(getdefaulttimeout_doc, |
---|
6202 | n/a | "getdefaulttimeout() -> timeout\n\ |
---|
6203 | n/a | \n\ |
---|
6204 | n/a | Returns the default timeout in seconds (float) for new socket objects.\n\ |
---|
6205 | n/a | A value of None indicates that new socket objects have no timeout.\n\ |
---|
6206 | n/a | When the socket module is first imported, the default is None."); |
---|
6207 | n/a | |
---|
6208 | n/a | static PyObject * |
---|
6209 | n/a | socket_setdefaulttimeout(PyObject *self, PyObject *arg) |
---|
6210 | n/a | { |
---|
6211 | n/a | _PyTime_t timeout; |
---|
6212 | n/a | |
---|
6213 | n/a | if (socket_parse_timeout(&timeout, arg) < 0) |
---|
6214 | n/a | return NULL; |
---|
6215 | n/a | |
---|
6216 | n/a | defaulttimeout = timeout; |
---|
6217 | n/a | |
---|
6218 | n/a | Py_RETURN_NONE; |
---|
6219 | n/a | } |
---|
6220 | n/a | |
---|
6221 | n/a | PyDoc_STRVAR(setdefaulttimeout_doc, |
---|
6222 | n/a | "setdefaulttimeout(timeout)\n\ |
---|
6223 | n/a | \n\ |
---|
6224 | n/a | Set the default timeout in seconds (float) for new socket objects.\n\ |
---|
6225 | n/a | A value of None indicates that new socket objects have no timeout.\n\ |
---|
6226 | n/a | When the socket module is first imported, the default is None."); |
---|
6227 | n/a | |
---|
6228 | n/a | #ifdef HAVE_IF_NAMEINDEX |
---|
6229 | n/a | /* Python API for getting interface indices and names */ |
---|
6230 | n/a | |
---|
6231 | n/a | static PyObject * |
---|
6232 | n/a | socket_if_nameindex(PyObject *self, PyObject *arg) |
---|
6233 | n/a | { |
---|
6234 | n/a | PyObject *list; |
---|
6235 | n/a | int i; |
---|
6236 | n/a | struct if_nameindex *ni; |
---|
6237 | n/a | |
---|
6238 | n/a | ni = if_nameindex(); |
---|
6239 | n/a | if (ni == NULL) { |
---|
6240 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
6241 | n/a | return NULL; |
---|
6242 | n/a | } |
---|
6243 | n/a | |
---|
6244 | n/a | list = PyList_New(0); |
---|
6245 | n/a | if (list == NULL) { |
---|
6246 | n/a | if_freenameindex(ni); |
---|
6247 | n/a | return NULL; |
---|
6248 | n/a | } |
---|
6249 | n/a | |
---|
6250 | n/a | for (i = 0; ni[i].if_index != 0 && i < INT_MAX; i++) { |
---|
6251 | n/a | PyObject *ni_tuple = Py_BuildValue("IO&", |
---|
6252 | n/a | ni[i].if_index, PyUnicode_DecodeFSDefault, ni[i].if_name); |
---|
6253 | n/a | |
---|
6254 | n/a | if (ni_tuple == NULL || PyList_Append(list, ni_tuple) == -1) { |
---|
6255 | n/a | Py_XDECREF(ni_tuple); |
---|
6256 | n/a | Py_DECREF(list); |
---|
6257 | n/a | if_freenameindex(ni); |
---|
6258 | n/a | return NULL; |
---|
6259 | n/a | } |
---|
6260 | n/a | Py_DECREF(ni_tuple); |
---|
6261 | n/a | } |
---|
6262 | n/a | |
---|
6263 | n/a | if_freenameindex(ni); |
---|
6264 | n/a | return list; |
---|
6265 | n/a | } |
---|
6266 | n/a | |
---|
6267 | n/a | PyDoc_STRVAR(if_nameindex_doc, |
---|
6268 | n/a | "if_nameindex()\n\ |
---|
6269 | n/a | \n\ |
---|
6270 | n/a | Returns a list of network interface information (index, name) tuples."); |
---|
6271 | n/a | |
---|
6272 | n/a | static PyObject * |
---|
6273 | n/a | socket_if_nametoindex(PyObject *self, PyObject *args) |
---|
6274 | n/a | { |
---|
6275 | n/a | PyObject *oname; |
---|
6276 | n/a | unsigned long index; |
---|
6277 | n/a | |
---|
6278 | n/a | if (!PyArg_ParseTuple(args, "O&:if_nametoindex", |
---|
6279 | n/a | PyUnicode_FSConverter, &oname)) |
---|
6280 | n/a | return NULL; |
---|
6281 | n/a | |
---|
6282 | n/a | index = if_nametoindex(PyBytes_AS_STRING(oname)); |
---|
6283 | n/a | Py_DECREF(oname); |
---|
6284 | n/a | if (index == 0) { |
---|
6285 | n/a | /* if_nametoindex() doesn't set errno */ |
---|
6286 | n/a | PyErr_SetString(PyExc_OSError, "no interface with this name"); |
---|
6287 | n/a | return NULL; |
---|
6288 | n/a | } |
---|
6289 | n/a | |
---|
6290 | n/a | return PyLong_FromUnsignedLong(index); |
---|
6291 | n/a | } |
---|
6292 | n/a | |
---|
6293 | n/a | PyDoc_STRVAR(if_nametoindex_doc, |
---|
6294 | n/a | "if_nametoindex(if_name)\n\ |
---|
6295 | n/a | \n\ |
---|
6296 | n/a | Returns the interface index corresponding to the interface name if_name."); |
---|
6297 | n/a | |
---|
6298 | n/a | static PyObject * |
---|
6299 | n/a | socket_if_indextoname(PyObject *self, PyObject *arg) |
---|
6300 | n/a | { |
---|
6301 | n/a | unsigned long index; |
---|
6302 | n/a | char name[IF_NAMESIZE + 1]; |
---|
6303 | n/a | |
---|
6304 | n/a | index = PyLong_AsUnsignedLong(arg); |
---|
6305 | n/a | if (index == (unsigned long) -1) |
---|
6306 | n/a | return NULL; |
---|
6307 | n/a | |
---|
6308 | n/a | if (if_indextoname(index, name) == NULL) { |
---|
6309 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
6310 | n/a | return NULL; |
---|
6311 | n/a | } |
---|
6312 | n/a | |
---|
6313 | n/a | return PyUnicode_DecodeFSDefault(name); |
---|
6314 | n/a | } |
---|
6315 | n/a | |
---|
6316 | n/a | PyDoc_STRVAR(if_indextoname_doc, |
---|
6317 | n/a | "if_indextoname(if_index)\n\ |
---|
6318 | n/a | \n\ |
---|
6319 | n/a | Returns the interface name corresponding to the interface index if_index."); |
---|
6320 | n/a | |
---|
6321 | n/a | #endif /* HAVE_IF_NAMEINDEX */ |
---|
6322 | n/a | |
---|
6323 | n/a | |
---|
6324 | n/a | #ifdef CMSG_LEN |
---|
6325 | n/a | /* Python interface to CMSG_LEN(length). */ |
---|
6326 | n/a | |
---|
6327 | n/a | static PyObject * |
---|
6328 | n/a | socket_CMSG_LEN(PyObject *self, PyObject *args) |
---|
6329 | n/a | { |
---|
6330 | n/a | Py_ssize_t length; |
---|
6331 | n/a | size_t result; |
---|
6332 | n/a | |
---|
6333 | n/a | if (!PyArg_ParseTuple(args, "n:CMSG_LEN", &length)) |
---|
6334 | n/a | return NULL; |
---|
6335 | n/a | if (length < 0 || !get_CMSG_LEN(length, &result)) { |
---|
6336 | n/a | PyErr_Format(PyExc_OverflowError, "CMSG_LEN() argument out of range"); |
---|
6337 | n/a | return NULL; |
---|
6338 | n/a | } |
---|
6339 | n/a | return PyLong_FromSize_t(result); |
---|
6340 | n/a | } |
---|
6341 | n/a | |
---|
6342 | n/a | PyDoc_STRVAR(CMSG_LEN_doc, |
---|
6343 | n/a | "CMSG_LEN(length) -> control message length\n\ |
---|
6344 | n/a | \n\ |
---|
6345 | n/a | Return the total length, without trailing padding, of an ancillary\n\ |
---|
6346 | n/a | data item with associated data of the given length. This value can\n\ |
---|
6347 | n/a | often be used as the buffer size for recvmsg() to receive a single\n\ |
---|
6348 | n/a | item of ancillary data, but RFC 3542 requires portable applications to\n\ |
---|
6349 | n/a | use CMSG_SPACE() and thus include space for padding, even when the\n\ |
---|
6350 | n/a | item will be the last in the buffer. Raises OverflowError if length\n\ |
---|
6351 | n/a | is outside the permissible range of values."); |
---|
6352 | n/a | |
---|
6353 | n/a | |
---|
6354 | n/a | #ifdef CMSG_SPACE |
---|
6355 | n/a | /* Python interface to CMSG_SPACE(length). */ |
---|
6356 | n/a | |
---|
6357 | n/a | static PyObject * |
---|
6358 | n/a | socket_CMSG_SPACE(PyObject *self, PyObject *args) |
---|
6359 | n/a | { |
---|
6360 | n/a | Py_ssize_t length; |
---|
6361 | n/a | size_t result; |
---|
6362 | n/a | |
---|
6363 | n/a | if (!PyArg_ParseTuple(args, "n:CMSG_SPACE", &length)) |
---|
6364 | n/a | return NULL; |
---|
6365 | n/a | if (length < 0 || !get_CMSG_SPACE(length, &result)) { |
---|
6366 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
6367 | n/a | "CMSG_SPACE() argument out of range"); |
---|
6368 | n/a | return NULL; |
---|
6369 | n/a | } |
---|
6370 | n/a | return PyLong_FromSize_t(result); |
---|
6371 | n/a | } |
---|
6372 | n/a | |
---|
6373 | n/a | PyDoc_STRVAR(CMSG_SPACE_doc, |
---|
6374 | n/a | "CMSG_SPACE(length) -> buffer size\n\ |
---|
6375 | n/a | \n\ |
---|
6376 | n/a | Return the buffer size needed for recvmsg() to receive an ancillary\n\ |
---|
6377 | n/a | data item with associated data of the given length, along with any\n\ |
---|
6378 | n/a | trailing padding. The buffer space needed to receive multiple items\n\ |
---|
6379 | n/a | is the sum of the CMSG_SPACE() values for their associated data\n\ |
---|
6380 | n/a | lengths. Raises OverflowError if length is outside the permissible\n\ |
---|
6381 | n/a | range of values."); |
---|
6382 | n/a | #endif /* CMSG_SPACE */ |
---|
6383 | n/a | #endif /* CMSG_LEN */ |
---|
6384 | n/a | |
---|
6385 | n/a | |
---|
6386 | n/a | /* List of functions exported by this module. */ |
---|
6387 | n/a | |
---|
6388 | n/a | static PyMethodDef socket_methods[] = { |
---|
6389 | n/a | {"gethostbyname", socket_gethostbyname, |
---|
6390 | n/a | METH_VARARGS, gethostbyname_doc}, |
---|
6391 | n/a | {"gethostbyname_ex", socket_gethostbyname_ex, |
---|
6392 | n/a | METH_VARARGS, ghbn_ex_doc}, |
---|
6393 | n/a | {"gethostbyaddr", socket_gethostbyaddr, |
---|
6394 | n/a | METH_VARARGS, gethostbyaddr_doc}, |
---|
6395 | n/a | {"gethostname", socket_gethostname, |
---|
6396 | n/a | METH_NOARGS, gethostname_doc}, |
---|
6397 | n/a | #ifdef HAVE_SETHOSTNAME |
---|
6398 | n/a | {"sethostname", socket_sethostname, |
---|
6399 | n/a | METH_VARARGS, sethostname_doc}, |
---|
6400 | n/a | #endif |
---|
6401 | n/a | {"getservbyname", socket_getservbyname, |
---|
6402 | n/a | METH_VARARGS, getservbyname_doc}, |
---|
6403 | n/a | {"getservbyport", socket_getservbyport, |
---|
6404 | n/a | METH_VARARGS, getservbyport_doc}, |
---|
6405 | n/a | {"getprotobyname", socket_getprotobyname, |
---|
6406 | n/a | METH_VARARGS, getprotobyname_doc}, |
---|
6407 | n/a | #ifndef NO_DUP |
---|
6408 | n/a | {"dup", socket_dup, |
---|
6409 | n/a | METH_O, dup_doc}, |
---|
6410 | n/a | #endif |
---|
6411 | n/a | #ifdef HAVE_SOCKETPAIR |
---|
6412 | n/a | {"socketpair", socket_socketpair, |
---|
6413 | n/a | METH_VARARGS, socketpair_doc}, |
---|
6414 | n/a | #endif |
---|
6415 | n/a | {"ntohs", socket_ntohs, |
---|
6416 | n/a | METH_VARARGS, ntohs_doc}, |
---|
6417 | n/a | {"ntohl", socket_ntohl, |
---|
6418 | n/a | METH_O, ntohl_doc}, |
---|
6419 | n/a | {"htons", socket_htons, |
---|
6420 | n/a | METH_VARARGS, htons_doc}, |
---|
6421 | n/a | {"htonl", socket_htonl, |
---|
6422 | n/a | METH_O, htonl_doc}, |
---|
6423 | n/a | {"inet_aton", socket_inet_aton, |
---|
6424 | n/a | METH_VARARGS, inet_aton_doc}, |
---|
6425 | n/a | {"inet_ntoa", socket_inet_ntoa, |
---|
6426 | n/a | METH_VARARGS, inet_ntoa_doc}, |
---|
6427 | n/a | #if defined(HAVE_INET_PTON) || defined(MS_WINDOWS) |
---|
6428 | n/a | {"inet_pton", socket_inet_pton, |
---|
6429 | n/a | METH_VARARGS, inet_pton_doc}, |
---|
6430 | n/a | {"inet_ntop", socket_inet_ntop, |
---|
6431 | n/a | METH_VARARGS, inet_ntop_doc}, |
---|
6432 | n/a | #endif |
---|
6433 | n/a | {"getaddrinfo", (PyCFunction)socket_getaddrinfo, |
---|
6434 | n/a | METH_VARARGS | METH_KEYWORDS, getaddrinfo_doc}, |
---|
6435 | n/a | {"getnameinfo", socket_getnameinfo, |
---|
6436 | n/a | METH_VARARGS, getnameinfo_doc}, |
---|
6437 | n/a | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout, |
---|
6438 | n/a | METH_NOARGS, getdefaulttimeout_doc}, |
---|
6439 | n/a | {"setdefaulttimeout", socket_setdefaulttimeout, |
---|
6440 | n/a | METH_O, setdefaulttimeout_doc}, |
---|
6441 | n/a | #ifdef HAVE_IF_NAMEINDEX |
---|
6442 | n/a | {"if_nameindex", socket_if_nameindex, |
---|
6443 | n/a | METH_NOARGS, if_nameindex_doc}, |
---|
6444 | n/a | {"if_nametoindex", socket_if_nametoindex, |
---|
6445 | n/a | METH_VARARGS, if_nametoindex_doc}, |
---|
6446 | n/a | {"if_indextoname", socket_if_indextoname, |
---|
6447 | n/a | METH_O, if_indextoname_doc}, |
---|
6448 | n/a | #endif |
---|
6449 | n/a | #ifdef CMSG_LEN |
---|
6450 | n/a | {"CMSG_LEN", socket_CMSG_LEN, |
---|
6451 | n/a | METH_VARARGS, CMSG_LEN_doc}, |
---|
6452 | n/a | #ifdef CMSG_SPACE |
---|
6453 | n/a | {"CMSG_SPACE", socket_CMSG_SPACE, |
---|
6454 | n/a | METH_VARARGS, CMSG_SPACE_doc}, |
---|
6455 | n/a | #endif |
---|
6456 | n/a | #endif |
---|
6457 | n/a | {NULL, NULL} /* Sentinel */ |
---|
6458 | n/a | }; |
---|
6459 | n/a | |
---|
6460 | n/a | |
---|
6461 | n/a | #ifdef MS_WINDOWS |
---|
6462 | n/a | #define OS_INIT_DEFINED |
---|
6463 | n/a | |
---|
6464 | n/a | /* Additional initialization and cleanup for Windows */ |
---|
6465 | n/a | |
---|
6466 | n/a | static void |
---|
6467 | n/a | os_cleanup(void) |
---|
6468 | n/a | { |
---|
6469 | n/a | WSACleanup(); |
---|
6470 | n/a | } |
---|
6471 | n/a | |
---|
6472 | n/a | static int |
---|
6473 | n/a | os_init(void) |
---|
6474 | n/a | { |
---|
6475 | n/a | WSADATA WSAData; |
---|
6476 | n/a | int ret; |
---|
6477 | n/a | ret = WSAStartup(0x0101, &WSAData); |
---|
6478 | n/a | switch (ret) { |
---|
6479 | n/a | case 0: /* No error */ |
---|
6480 | n/a | Py_AtExit(os_cleanup); |
---|
6481 | n/a | return 1; /* Success */ |
---|
6482 | n/a | case WSASYSNOTREADY: |
---|
6483 | n/a | PyErr_SetString(PyExc_ImportError, |
---|
6484 | n/a | "WSAStartup failed: network not ready"); |
---|
6485 | n/a | break; |
---|
6486 | n/a | case WSAVERNOTSUPPORTED: |
---|
6487 | n/a | case WSAEINVAL: |
---|
6488 | n/a | PyErr_SetString( |
---|
6489 | n/a | PyExc_ImportError, |
---|
6490 | n/a | "WSAStartup failed: requested version not supported"); |
---|
6491 | n/a | break; |
---|
6492 | n/a | default: |
---|
6493 | n/a | PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret); |
---|
6494 | n/a | break; |
---|
6495 | n/a | } |
---|
6496 | n/a | return 0; /* Failure */ |
---|
6497 | n/a | } |
---|
6498 | n/a | |
---|
6499 | n/a | #endif /* MS_WINDOWS */ |
---|
6500 | n/a | |
---|
6501 | n/a | |
---|
6502 | n/a | |
---|
6503 | n/a | #ifndef OS_INIT_DEFINED |
---|
6504 | n/a | static int |
---|
6505 | n/a | os_init(void) |
---|
6506 | n/a | { |
---|
6507 | n/a | return 1; /* Success */ |
---|
6508 | n/a | } |
---|
6509 | n/a | #endif |
---|
6510 | n/a | |
---|
6511 | n/a | |
---|
6512 | n/a | /* C API table - always add new things to the end for binary |
---|
6513 | n/a | compatibility. */ |
---|
6514 | n/a | static |
---|
6515 | n/a | PySocketModule_APIObject PySocketModuleAPI = |
---|
6516 | n/a | { |
---|
6517 | n/a | &sock_type, |
---|
6518 | n/a | NULL, |
---|
6519 | n/a | NULL |
---|
6520 | n/a | }; |
---|
6521 | n/a | |
---|
6522 | n/a | |
---|
6523 | n/a | /* Initialize the _socket module. |
---|
6524 | n/a | |
---|
6525 | n/a | This module is actually called "_socket", and there's a wrapper |
---|
6526 | n/a | "socket.py" which implements some additional functionality. |
---|
6527 | n/a | The import of "_socket" may fail with an ImportError exception if |
---|
6528 | n/a | os-specific initialization fails. On Windows, this does WINSOCK |
---|
6529 | n/a | initialization. When WINSOCK is initialized successfully, a call to |
---|
6530 | n/a | WSACleanup() is scheduled to be made at exit time. |
---|
6531 | n/a | */ |
---|
6532 | n/a | |
---|
6533 | n/a | PyDoc_STRVAR(socket_doc, |
---|
6534 | n/a | "Implementation module for socket operations.\n\ |
---|
6535 | n/a | \n\ |
---|
6536 | n/a | See the socket module for documentation."); |
---|
6537 | n/a | |
---|
6538 | n/a | static struct PyModuleDef socketmodule = { |
---|
6539 | n/a | PyModuleDef_HEAD_INIT, |
---|
6540 | n/a | PySocket_MODULE_NAME, |
---|
6541 | n/a | socket_doc, |
---|
6542 | n/a | -1, |
---|
6543 | n/a | socket_methods, |
---|
6544 | n/a | NULL, |
---|
6545 | n/a | NULL, |
---|
6546 | n/a | NULL, |
---|
6547 | n/a | NULL |
---|
6548 | n/a | }; |
---|
6549 | n/a | |
---|
6550 | n/a | PyMODINIT_FUNC |
---|
6551 | n/a | PyInit__socket(void) |
---|
6552 | n/a | { |
---|
6553 | n/a | PyObject *m, *has_ipv6; |
---|
6554 | n/a | |
---|
6555 | n/a | if (!os_init()) |
---|
6556 | n/a | return NULL; |
---|
6557 | n/a | |
---|
6558 | n/a | #ifdef MS_WINDOWS |
---|
6559 | n/a | if (support_wsa_no_inherit == -1) { |
---|
6560 | n/a | #if defined(_MSC_VER) && _MSC_VER >= 1800 |
---|
6561 | n/a | support_wsa_no_inherit = IsWindows7SP1OrGreater(); |
---|
6562 | n/a | #else |
---|
6563 | n/a | DWORD version = GetVersion(); |
---|
6564 | n/a | DWORD major = (DWORD)LOBYTE(LOWORD(version)); |
---|
6565 | n/a | DWORD minor = (DWORD)HIBYTE(LOWORD(version)); |
---|
6566 | n/a | /* need Windows 7 SP1, 2008 R2 SP1 or later */ |
---|
6567 | n/a | support_wsa_no_inherit = major > 6 || (major == 6 && minor >= 1); |
---|
6568 | n/a | #endif |
---|
6569 | n/a | } |
---|
6570 | n/a | #endif |
---|
6571 | n/a | |
---|
6572 | n/a | Py_TYPE(&sock_type) = &PyType_Type; |
---|
6573 | n/a | m = PyModule_Create(&socketmodule); |
---|
6574 | n/a | if (m == NULL) |
---|
6575 | n/a | return NULL; |
---|
6576 | n/a | |
---|
6577 | n/a | Py_INCREF(PyExc_OSError); |
---|
6578 | n/a | PySocketModuleAPI.error = PyExc_OSError; |
---|
6579 | n/a | Py_INCREF(PyExc_OSError); |
---|
6580 | n/a | PyModule_AddObject(m, "error", PyExc_OSError); |
---|
6581 | n/a | socket_herror = PyErr_NewException("socket.herror", |
---|
6582 | n/a | PyExc_OSError, NULL); |
---|
6583 | n/a | if (socket_herror == NULL) |
---|
6584 | n/a | return NULL; |
---|
6585 | n/a | Py_INCREF(socket_herror); |
---|
6586 | n/a | PyModule_AddObject(m, "herror", socket_herror); |
---|
6587 | n/a | socket_gaierror = PyErr_NewException("socket.gaierror", PyExc_OSError, |
---|
6588 | n/a | NULL); |
---|
6589 | n/a | if (socket_gaierror == NULL) |
---|
6590 | n/a | return NULL; |
---|
6591 | n/a | Py_INCREF(socket_gaierror); |
---|
6592 | n/a | PyModule_AddObject(m, "gaierror", socket_gaierror); |
---|
6593 | n/a | socket_timeout = PyErr_NewException("socket.timeout", |
---|
6594 | n/a | PyExc_OSError, NULL); |
---|
6595 | n/a | if (socket_timeout == NULL) |
---|
6596 | n/a | return NULL; |
---|
6597 | n/a | PySocketModuleAPI.timeout_error = socket_timeout; |
---|
6598 | n/a | Py_INCREF(socket_timeout); |
---|
6599 | n/a | PyModule_AddObject(m, "timeout", socket_timeout); |
---|
6600 | n/a | Py_INCREF((PyObject *)&sock_type); |
---|
6601 | n/a | if (PyModule_AddObject(m, "SocketType", |
---|
6602 | n/a | (PyObject *)&sock_type) != 0) |
---|
6603 | n/a | return NULL; |
---|
6604 | n/a | Py_INCREF((PyObject *)&sock_type); |
---|
6605 | n/a | if (PyModule_AddObject(m, "socket", |
---|
6606 | n/a | (PyObject *)&sock_type) != 0) |
---|
6607 | n/a | return NULL; |
---|
6608 | n/a | |
---|
6609 | n/a | #ifdef ENABLE_IPV6 |
---|
6610 | n/a | has_ipv6 = Py_True; |
---|
6611 | n/a | #else |
---|
6612 | n/a | has_ipv6 = Py_False; |
---|
6613 | n/a | #endif |
---|
6614 | n/a | Py_INCREF(has_ipv6); |
---|
6615 | n/a | PyModule_AddObject(m, "has_ipv6", has_ipv6); |
---|
6616 | n/a | |
---|
6617 | n/a | /* Export C API */ |
---|
6618 | n/a | if (PyModule_AddObject(m, PySocket_CAPI_NAME, |
---|
6619 | n/a | PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL) |
---|
6620 | n/a | ) != 0) |
---|
6621 | n/a | return NULL; |
---|
6622 | n/a | |
---|
6623 | n/a | /* Address families (we only support AF_INET and AF_UNIX) */ |
---|
6624 | n/a | #ifdef AF_UNSPEC |
---|
6625 | n/a | PyModule_AddIntMacro(m, AF_UNSPEC); |
---|
6626 | n/a | #endif |
---|
6627 | n/a | PyModule_AddIntMacro(m, AF_INET); |
---|
6628 | n/a | #if defined(AF_UNIX) |
---|
6629 | n/a | PyModule_AddIntMacro(m, AF_UNIX); |
---|
6630 | n/a | #endif /* AF_UNIX */ |
---|
6631 | n/a | #ifdef AF_AX25 |
---|
6632 | n/a | /* Amateur Radio AX.25 */ |
---|
6633 | n/a | PyModule_AddIntMacro(m, AF_AX25); |
---|
6634 | n/a | #endif |
---|
6635 | n/a | #ifdef AF_IPX |
---|
6636 | n/a | PyModule_AddIntMacro(m, AF_IPX); /* Novell IPX */ |
---|
6637 | n/a | #endif |
---|
6638 | n/a | #ifdef AF_APPLETALK |
---|
6639 | n/a | /* Appletalk DDP */ |
---|
6640 | n/a | PyModule_AddIntMacro(m, AF_APPLETALK); |
---|
6641 | n/a | #endif |
---|
6642 | n/a | #ifdef AF_NETROM |
---|
6643 | n/a | /* Amateur radio NetROM */ |
---|
6644 | n/a | PyModule_AddIntMacro(m, AF_NETROM); |
---|
6645 | n/a | #endif |
---|
6646 | n/a | #ifdef AF_BRIDGE |
---|
6647 | n/a | /* Multiprotocol bridge */ |
---|
6648 | n/a | PyModule_AddIntMacro(m, AF_BRIDGE); |
---|
6649 | n/a | #endif |
---|
6650 | n/a | #ifdef AF_ATMPVC |
---|
6651 | n/a | /* ATM PVCs */ |
---|
6652 | n/a | PyModule_AddIntMacro(m, AF_ATMPVC); |
---|
6653 | n/a | #endif |
---|
6654 | n/a | #ifdef AF_AAL5 |
---|
6655 | n/a | /* Reserved for Werner's ATM */ |
---|
6656 | n/a | PyModule_AddIntMacro(m, AF_AAL5); |
---|
6657 | n/a | #endif |
---|
6658 | n/a | #ifdef HAVE_SOCKADDR_ALG |
---|
6659 | n/a | PyModule_AddIntMacro(m, AF_ALG); /* Linux crypto */ |
---|
6660 | n/a | #endif |
---|
6661 | n/a | #ifdef AF_X25 |
---|
6662 | n/a | /* Reserved for X.25 project */ |
---|
6663 | n/a | PyModule_AddIntMacro(m, AF_X25); |
---|
6664 | n/a | #endif |
---|
6665 | n/a | #ifdef AF_INET6 |
---|
6666 | n/a | PyModule_AddIntMacro(m, AF_INET6); /* IP version 6 */ |
---|
6667 | n/a | #endif |
---|
6668 | n/a | #ifdef AF_ROSE |
---|
6669 | n/a | /* Amateur Radio X.25 PLP */ |
---|
6670 | n/a | PyModule_AddIntMacro(m, AF_ROSE); |
---|
6671 | n/a | #endif |
---|
6672 | n/a | #ifdef AF_DECnet |
---|
6673 | n/a | /* Reserved for DECnet project */ |
---|
6674 | n/a | PyModule_AddIntMacro(m, AF_DECnet); |
---|
6675 | n/a | #endif |
---|
6676 | n/a | #ifdef AF_NETBEUI |
---|
6677 | n/a | /* Reserved for 802.2LLC project */ |
---|
6678 | n/a | PyModule_AddIntMacro(m, AF_NETBEUI); |
---|
6679 | n/a | #endif |
---|
6680 | n/a | #ifdef AF_SECURITY |
---|
6681 | n/a | /* Security callback pseudo AF */ |
---|
6682 | n/a | PyModule_AddIntMacro(m, AF_SECURITY); |
---|
6683 | n/a | #endif |
---|
6684 | n/a | #ifdef AF_KEY |
---|
6685 | n/a | /* PF_KEY key management API */ |
---|
6686 | n/a | PyModule_AddIntMacro(m, AF_KEY); |
---|
6687 | n/a | #endif |
---|
6688 | n/a | #ifdef AF_NETLINK |
---|
6689 | n/a | /* */ |
---|
6690 | n/a | PyModule_AddIntMacro(m, AF_NETLINK); |
---|
6691 | n/a | PyModule_AddIntMacro(m, NETLINK_ROUTE); |
---|
6692 | n/a | #ifdef NETLINK_SKIP |
---|
6693 | n/a | PyModule_AddIntMacro(m, NETLINK_SKIP); |
---|
6694 | n/a | #endif |
---|
6695 | n/a | #ifdef NETLINK_W1 |
---|
6696 | n/a | PyModule_AddIntMacro(m, NETLINK_W1); |
---|
6697 | n/a | #endif |
---|
6698 | n/a | PyModule_AddIntMacro(m, NETLINK_USERSOCK); |
---|
6699 | n/a | PyModule_AddIntMacro(m, NETLINK_FIREWALL); |
---|
6700 | n/a | #ifdef NETLINK_TCPDIAG |
---|
6701 | n/a | PyModule_AddIntMacro(m, NETLINK_TCPDIAG); |
---|
6702 | n/a | #endif |
---|
6703 | n/a | #ifdef NETLINK_NFLOG |
---|
6704 | n/a | PyModule_AddIntMacro(m, NETLINK_NFLOG); |
---|
6705 | n/a | #endif |
---|
6706 | n/a | #ifdef NETLINK_XFRM |
---|
6707 | n/a | PyModule_AddIntMacro(m, NETLINK_XFRM); |
---|
6708 | n/a | #endif |
---|
6709 | n/a | #ifdef NETLINK_ARPD |
---|
6710 | n/a | PyModule_AddIntMacro(m, NETLINK_ARPD); |
---|
6711 | n/a | #endif |
---|
6712 | n/a | #ifdef NETLINK_ROUTE6 |
---|
6713 | n/a | PyModule_AddIntMacro(m, NETLINK_ROUTE6); |
---|
6714 | n/a | #endif |
---|
6715 | n/a | PyModule_AddIntMacro(m, NETLINK_IP6_FW); |
---|
6716 | n/a | #ifdef NETLINK_DNRTMSG |
---|
6717 | n/a | PyModule_AddIntMacro(m, NETLINK_DNRTMSG); |
---|
6718 | n/a | #endif |
---|
6719 | n/a | #ifdef NETLINK_TAPBASE |
---|
6720 | n/a | PyModule_AddIntMacro(m, NETLINK_TAPBASE); |
---|
6721 | n/a | #endif |
---|
6722 | n/a | #ifdef NETLINK_CRYPTO |
---|
6723 | n/a | PyModule_AddIntMacro(m, NETLINK_CRYPTO); |
---|
6724 | n/a | #endif |
---|
6725 | n/a | #endif /* AF_NETLINK */ |
---|
6726 | n/a | #ifdef AF_ROUTE |
---|
6727 | n/a | /* Alias to emulate 4.4BSD */ |
---|
6728 | n/a | PyModule_AddIntMacro(m, AF_ROUTE); |
---|
6729 | n/a | #endif |
---|
6730 | n/a | #ifdef AF_LINK |
---|
6731 | n/a | PyModule_AddIntMacro(m, AF_LINK); |
---|
6732 | n/a | #endif |
---|
6733 | n/a | #ifdef AF_ASH |
---|
6734 | n/a | /* Ash */ |
---|
6735 | n/a | PyModule_AddIntMacro(m, AF_ASH); |
---|
6736 | n/a | #endif |
---|
6737 | n/a | #ifdef AF_ECONET |
---|
6738 | n/a | /* Acorn Econet */ |
---|
6739 | n/a | PyModule_AddIntMacro(m, AF_ECONET); |
---|
6740 | n/a | #endif |
---|
6741 | n/a | #ifdef AF_ATMSVC |
---|
6742 | n/a | /* ATM SVCs */ |
---|
6743 | n/a | PyModule_AddIntMacro(m, AF_ATMSVC); |
---|
6744 | n/a | #endif |
---|
6745 | n/a | #ifdef AF_SNA |
---|
6746 | n/a | /* Linux SNA Project (nutters!) */ |
---|
6747 | n/a | PyModule_AddIntMacro(m, AF_SNA); |
---|
6748 | n/a | #endif |
---|
6749 | n/a | #ifdef AF_IRDA |
---|
6750 | n/a | /* IRDA sockets */ |
---|
6751 | n/a | PyModule_AddIntMacro(m, AF_IRDA); |
---|
6752 | n/a | #endif |
---|
6753 | n/a | #ifdef AF_PPPOX |
---|
6754 | n/a | /* PPPoX sockets */ |
---|
6755 | n/a | PyModule_AddIntMacro(m, AF_PPPOX); |
---|
6756 | n/a | #endif |
---|
6757 | n/a | #ifdef AF_WANPIPE |
---|
6758 | n/a | /* Wanpipe API Sockets */ |
---|
6759 | n/a | PyModule_AddIntMacro(m, AF_WANPIPE); |
---|
6760 | n/a | #endif |
---|
6761 | n/a | #ifdef AF_LLC |
---|
6762 | n/a | /* Linux LLC */ |
---|
6763 | n/a | PyModule_AddIntMacro(m, AF_LLC); |
---|
6764 | n/a | #endif |
---|
6765 | n/a | |
---|
6766 | n/a | #ifdef USE_BLUETOOTH |
---|
6767 | n/a | PyModule_AddIntMacro(m, AF_BLUETOOTH); |
---|
6768 | n/a | PyModule_AddIntMacro(m, BTPROTO_L2CAP); |
---|
6769 | n/a | PyModule_AddIntMacro(m, BTPROTO_HCI); |
---|
6770 | n/a | PyModule_AddIntMacro(m, SOL_HCI); |
---|
6771 | n/a | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
---|
6772 | n/a | PyModule_AddIntMacro(m, HCI_FILTER); |
---|
6773 | n/a | #endif |
---|
6774 | n/a | #if !defined(__FreeBSD__) |
---|
6775 | n/a | #if !defined(__NetBSD__) && !defined(__DragonFly__) |
---|
6776 | n/a | PyModule_AddIntMacro(m, HCI_TIME_STAMP); |
---|
6777 | n/a | #endif |
---|
6778 | n/a | PyModule_AddIntMacro(m, HCI_DATA_DIR); |
---|
6779 | n/a | PyModule_AddIntMacro(m, BTPROTO_SCO); |
---|
6780 | n/a | #endif |
---|
6781 | n/a | PyModule_AddIntMacro(m, BTPROTO_RFCOMM); |
---|
6782 | n/a | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00"); |
---|
6783 | n/a | PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF"); |
---|
6784 | n/a | #endif |
---|
6785 | n/a | |
---|
6786 | n/a | #ifdef AF_CAN |
---|
6787 | n/a | /* Controller Area Network */ |
---|
6788 | n/a | PyModule_AddIntMacro(m, AF_CAN); |
---|
6789 | n/a | #endif |
---|
6790 | n/a | #ifdef PF_CAN |
---|
6791 | n/a | /* Controller Area Network */ |
---|
6792 | n/a | PyModule_AddIntMacro(m, PF_CAN); |
---|
6793 | n/a | #endif |
---|
6794 | n/a | |
---|
6795 | n/a | /* Reliable Datagram Sockets */ |
---|
6796 | n/a | #ifdef AF_RDS |
---|
6797 | n/a | PyModule_AddIntMacro(m, AF_RDS); |
---|
6798 | n/a | #endif |
---|
6799 | n/a | #ifdef PF_RDS |
---|
6800 | n/a | PyModule_AddIntMacro(m, PF_RDS); |
---|
6801 | n/a | #endif |
---|
6802 | n/a | |
---|
6803 | n/a | /* Kernel event messages */ |
---|
6804 | n/a | #ifdef PF_SYSTEM |
---|
6805 | n/a | PyModule_AddIntMacro(m, PF_SYSTEM); |
---|
6806 | n/a | #endif |
---|
6807 | n/a | #ifdef AF_SYSTEM |
---|
6808 | n/a | PyModule_AddIntMacro(m, AF_SYSTEM); |
---|
6809 | n/a | #endif |
---|
6810 | n/a | |
---|
6811 | n/a | #ifdef AF_PACKET |
---|
6812 | n/a | PyModule_AddIntMacro(m, AF_PACKET); |
---|
6813 | n/a | #endif |
---|
6814 | n/a | #ifdef PF_PACKET |
---|
6815 | n/a | PyModule_AddIntMacro(m, PF_PACKET); |
---|
6816 | n/a | #endif |
---|
6817 | n/a | #ifdef PACKET_HOST |
---|
6818 | n/a | PyModule_AddIntMacro(m, PACKET_HOST); |
---|
6819 | n/a | #endif |
---|
6820 | n/a | #ifdef PACKET_BROADCAST |
---|
6821 | n/a | PyModule_AddIntMacro(m, PACKET_BROADCAST); |
---|
6822 | n/a | #endif |
---|
6823 | n/a | #ifdef PACKET_MULTICAST |
---|
6824 | n/a | PyModule_AddIntMacro(m, PACKET_MULTICAST); |
---|
6825 | n/a | #endif |
---|
6826 | n/a | #ifdef PACKET_OTHERHOST |
---|
6827 | n/a | PyModule_AddIntMacro(m, PACKET_OTHERHOST); |
---|
6828 | n/a | #endif |
---|
6829 | n/a | #ifdef PACKET_OUTGOING |
---|
6830 | n/a | PyModule_AddIntMacro(m, PACKET_OUTGOING); |
---|
6831 | n/a | #endif |
---|
6832 | n/a | #ifdef PACKET_LOOPBACK |
---|
6833 | n/a | PyModule_AddIntMacro(m, PACKET_LOOPBACK); |
---|
6834 | n/a | #endif |
---|
6835 | n/a | #ifdef PACKET_FASTROUTE |
---|
6836 | n/a | PyModule_AddIntMacro(m, PACKET_FASTROUTE); |
---|
6837 | n/a | #endif |
---|
6838 | n/a | |
---|
6839 | n/a | #ifdef HAVE_LINUX_TIPC_H |
---|
6840 | n/a | PyModule_AddIntMacro(m, AF_TIPC); |
---|
6841 | n/a | |
---|
6842 | n/a | /* for addresses */ |
---|
6843 | n/a | PyModule_AddIntMacro(m, TIPC_ADDR_NAMESEQ); |
---|
6844 | n/a | PyModule_AddIntMacro(m, TIPC_ADDR_NAME); |
---|
6845 | n/a | PyModule_AddIntMacro(m, TIPC_ADDR_ID); |
---|
6846 | n/a | |
---|
6847 | n/a | PyModule_AddIntMacro(m, TIPC_ZONE_SCOPE); |
---|
6848 | n/a | PyModule_AddIntMacro(m, TIPC_CLUSTER_SCOPE); |
---|
6849 | n/a | PyModule_AddIntMacro(m, TIPC_NODE_SCOPE); |
---|
6850 | n/a | |
---|
6851 | n/a | /* for setsockopt() */ |
---|
6852 | n/a | PyModule_AddIntMacro(m, SOL_TIPC); |
---|
6853 | n/a | PyModule_AddIntMacro(m, TIPC_IMPORTANCE); |
---|
6854 | n/a | PyModule_AddIntMacro(m, TIPC_SRC_DROPPABLE); |
---|
6855 | n/a | PyModule_AddIntMacro(m, TIPC_DEST_DROPPABLE); |
---|
6856 | n/a | PyModule_AddIntMacro(m, TIPC_CONN_TIMEOUT); |
---|
6857 | n/a | |
---|
6858 | n/a | PyModule_AddIntMacro(m, TIPC_LOW_IMPORTANCE); |
---|
6859 | n/a | PyModule_AddIntMacro(m, TIPC_MEDIUM_IMPORTANCE); |
---|
6860 | n/a | PyModule_AddIntMacro(m, TIPC_HIGH_IMPORTANCE); |
---|
6861 | n/a | PyModule_AddIntMacro(m, TIPC_CRITICAL_IMPORTANCE); |
---|
6862 | n/a | |
---|
6863 | n/a | /* for subscriptions */ |
---|
6864 | n/a | PyModule_AddIntMacro(m, TIPC_SUB_PORTS); |
---|
6865 | n/a | PyModule_AddIntMacro(m, TIPC_SUB_SERVICE); |
---|
6866 | n/a | #ifdef TIPC_SUB_CANCEL |
---|
6867 | n/a | /* doesn't seem to be available everywhere */ |
---|
6868 | n/a | PyModule_AddIntMacro(m, TIPC_SUB_CANCEL); |
---|
6869 | n/a | #endif |
---|
6870 | n/a | PyModule_AddIntMacro(m, TIPC_WAIT_FOREVER); |
---|
6871 | n/a | PyModule_AddIntMacro(m, TIPC_PUBLISHED); |
---|
6872 | n/a | PyModule_AddIntMacro(m, TIPC_WITHDRAWN); |
---|
6873 | n/a | PyModule_AddIntMacro(m, TIPC_SUBSCR_TIMEOUT); |
---|
6874 | n/a | PyModule_AddIntMacro(m, TIPC_CFG_SRV); |
---|
6875 | n/a | PyModule_AddIntMacro(m, TIPC_TOP_SRV); |
---|
6876 | n/a | #endif |
---|
6877 | n/a | |
---|
6878 | n/a | #ifdef HAVE_SOCKADDR_ALG |
---|
6879 | n/a | /* Socket options */ |
---|
6880 | n/a | PyModule_AddIntMacro(m, ALG_SET_KEY); |
---|
6881 | n/a | PyModule_AddIntMacro(m, ALG_SET_IV); |
---|
6882 | n/a | PyModule_AddIntMacro(m, ALG_SET_OP); |
---|
6883 | n/a | PyModule_AddIntMacro(m, ALG_SET_AEAD_ASSOCLEN); |
---|
6884 | n/a | PyModule_AddIntMacro(m, ALG_SET_AEAD_AUTHSIZE); |
---|
6885 | n/a | PyModule_AddIntMacro(m, ALG_SET_PUBKEY); |
---|
6886 | n/a | |
---|
6887 | n/a | /* Operations */ |
---|
6888 | n/a | PyModule_AddIntMacro(m, ALG_OP_DECRYPT); |
---|
6889 | n/a | PyModule_AddIntMacro(m, ALG_OP_ENCRYPT); |
---|
6890 | n/a | PyModule_AddIntMacro(m, ALG_OP_SIGN); |
---|
6891 | n/a | PyModule_AddIntMacro(m, ALG_OP_VERIFY); |
---|
6892 | n/a | #endif |
---|
6893 | n/a | |
---|
6894 | n/a | /* Socket types */ |
---|
6895 | n/a | PyModule_AddIntMacro(m, SOCK_STREAM); |
---|
6896 | n/a | PyModule_AddIntMacro(m, SOCK_DGRAM); |
---|
6897 | n/a | /* We have incomplete socket support. */ |
---|
6898 | n/a | #ifdef SOCK_RAW |
---|
6899 | n/a | /* SOCK_RAW is marked as optional in the POSIX specification */ |
---|
6900 | n/a | PyModule_AddIntMacro(m, SOCK_RAW); |
---|
6901 | n/a | #endif |
---|
6902 | n/a | PyModule_AddIntMacro(m, SOCK_SEQPACKET); |
---|
6903 | n/a | #if defined(SOCK_RDM) |
---|
6904 | n/a | PyModule_AddIntMacro(m, SOCK_RDM); |
---|
6905 | n/a | #endif |
---|
6906 | n/a | #ifdef SOCK_CLOEXEC |
---|
6907 | n/a | PyModule_AddIntMacro(m, SOCK_CLOEXEC); |
---|
6908 | n/a | #endif |
---|
6909 | n/a | #ifdef SOCK_NONBLOCK |
---|
6910 | n/a | PyModule_AddIntMacro(m, SOCK_NONBLOCK); |
---|
6911 | n/a | #endif |
---|
6912 | n/a | |
---|
6913 | n/a | #ifdef SO_DEBUG |
---|
6914 | n/a | PyModule_AddIntMacro(m, SO_DEBUG); |
---|
6915 | n/a | #endif |
---|
6916 | n/a | #ifdef SO_ACCEPTCONN |
---|
6917 | n/a | PyModule_AddIntMacro(m, SO_ACCEPTCONN); |
---|
6918 | n/a | #endif |
---|
6919 | n/a | #ifdef SO_REUSEADDR |
---|
6920 | n/a | PyModule_AddIntMacro(m, SO_REUSEADDR); |
---|
6921 | n/a | #endif |
---|
6922 | n/a | #ifdef SO_EXCLUSIVEADDRUSE |
---|
6923 | n/a | PyModule_AddIntMacro(m, SO_EXCLUSIVEADDRUSE); |
---|
6924 | n/a | #endif |
---|
6925 | n/a | |
---|
6926 | n/a | #ifdef SO_KEEPALIVE |
---|
6927 | n/a | PyModule_AddIntMacro(m, SO_KEEPALIVE); |
---|
6928 | n/a | #endif |
---|
6929 | n/a | #ifdef SO_DONTROUTE |
---|
6930 | n/a | PyModule_AddIntMacro(m, SO_DONTROUTE); |
---|
6931 | n/a | #endif |
---|
6932 | n/a | #ifdef SO_BROADCAST |
---|
6933 | n/a | PyModule_AddIntMacro(m, SO_BROADCAST); |
---|
6934 | n/a | #endif |
---|
6935 | n/a | #ifdef SO_USELOOPBACK |
---|
6936 | n/a | PyModule_AddIntMacro(m, SO_USELOOPBACK); |
---|
6937 | n/a | #endif |
---|
6938 | n/a | #ifdef SO_LINGER |
---|
6939 | n/a | PyModule_AddIntMacro(m, SO_LINGER); |
---|
6940 | n/a | #endif |
---|
6941 | n/a | #ifdef SO_OOBINLINE |
---|
6942 | n/a | PyModule_AddIntMacro(m, SO_OOBINLINE); |
---|
6943 | n/a | #endif |
---|
6944 | n/a | #ifndef __GNU__ |
---|
6945 | n/a | #ifdef SO_REUSEPORT |
---|
6946 | n/a | PyModule_AddIntMacro(m, SO_REUSEPORT); |
---|
6947 | n/a | #endif |
---|
6948 | n/a | #endif |
---|
6949 | n/a | #ifdef SO_SNDBUF |
---|
6950 | n/a | PyModule_AddIntMacro(m, SO_SNDBUF); |
---|
6951 | n/a | #endif |
---|
6952 | n/a | #ifdef SO_RCVBUF |
---|
6953 | n/a | PyModule_AddIntMacro(m, SO_RCVBUF); |
---|
6954 | n/a | #endif |
---|
6955 | n/a | #ifdef SO_SNDLOWAT |
---|
6956 | n/a | PyModule_AddIntMacro(m, SO_SNDLOWAT); |
---|
6957 | n/a | #endif |
---|
6958 | n/a | #ifdef SO_RCVLOWAT |
---|
6959 | n/a | PyModule_AddIntMacro(m, SO_RCVLOWAT); |
---|
6960 | n/a | #endif |
---|
6961 | n/a | #ifdef SO_SNDTIMEO |
---|
6962 | n/a | PyModule_AddIntMacro(m, SO_SNDTIMEO); |
---|
6963 | n/a | #endif |
---|
6964 | n/a | #ifdef SO_RCVTIMEO |
---|
6965 | n/a | PyModule_AddIntMacro(m, SO_RCVTIMEO); |
---|
6966 | n/a | #endif |
---|
6967 | n/a | #ifdef SO_ERROR |
---|
6968 | n/a | PyModule_AddIntMacro(m, SO_ERROR); |
---|
6969 | n/a | #endif |
---|
6970 | n/a | #ifdef SO_TYPE |
---|
6971 | n/a | PyModule_AddIntMacro(m, SO_TYPE); |
---|
6972 | n/a | #endif |
---|
6973 | n/a | #ifdef SO_SETFIB |
---|
6974 | n/a | PyModule_AddIntMacro(m, SO_SETFIB); |
---|
6975 | n/a | #endif |
---|
6976 | n/a | #ifdef SO_PASSCRED |
---|
6977 | n/a | PyModule_AddIntMacro(m, SO_PASSCRED); |
---|
6978 | n/a | #endif |
---|
6979 | n/a | #ifdef SO_PEERCRED |
---|
6980 | n/a | PyModule_AddIntMacro(m, SO_PEERCRED); |
---|
6981 | n/a | #endif |
---|
6982 | n/a | #ifdef LOCAL_PEERCRED |
---|
6983 | n/a | PyModule_AddIntMacro(m, LOCAL_PEERCRED); |
---|
6984 | n/a | #endif |
---|
6985 | n/a | #ifdef SO_PASSSEC |
---|
6986 | n/a | PyModule_AddIntMacro(m, SO_PASSSEC); |
---|
6987 | n/a | #endif |
---|
6988 | n/a | #ifdef SO_PEERSEC |
---|
6989 | n/a | PyModule_AddIntMacro(m, SO_PEERSEC); |
---|
6990 | n/a | #endif |
---|
6991 | n/a | #ifdef SO_BINDTODEVICE |
---|
6992 | n/a | PyModule_AddIntMacro(m, SO_BINDTODEVICE); |
---|
6993 | n/a | #endif |
---|
6994 | n/a | #ifdef SO_PRIORITY |
---|
6995 | n/a | PyModule_AddIntMacro(m, SO_PRIORITY); |
---|
6996 | n/a | #endif |
---|
6997 | n/a | #ifdef SO_MARK |
---|
6998 | n/a | PyModule_AddIntMacro(m, SO_MARK); |
---|
6999 | n/a | #endif |
---|
7000 | n/a | #ifdef SO_DOMAIN |
---|
7001 | n/a | PyModule_AddIntMacro(m, SO_DOMAIN); |
---|
7002 | n/a | #endif |
---|
7003 | n/a | #ifdef SO_PROTOCOL |
---|
7004 | n/a | PyModule_AddIntMacro(m, SO_PROTOCOL); |
---|
7005 | n/a | #endif |
---|
7006 | n/a | |
---|
7007 | n/a | /* Maximum number of connections for "listen" */ |
---|
7008 | n/a | #ifdef SOMAXCONN |
---|
7009 | n/a | PyModule_AddIntMacro(m, SOMAXCONN); |
---|
7010 | n/a | #else |
---|
7011 | n/a | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */ |
---|
7012 | n/a | #endif |
---|
7013 | n/a | |
---|
7014 | n/a | /* Ancillary message types */ |
---|
7015 | n/a | #ifdef SCM_RIGHTS |
---|
7016 | n/a | PyModule_AddIntMacro(m, SCM_RIGHTS); |
---|
7017 | n/a | #endif |
---|
7018 | n/a | #ifdef SCM_CREDENTIALS |
---|
7019 | n/a | PyModule_AddIntMacro(m, SCM_CREDENTIALS); |
---|
7020 | n/a | #endif |
---|
7021 | n/a | #ifdef SCM_CREDS |
---|
7022 | n/a | PyModule_AddIntMacro(m, SCM_CREDS); |
---|
7023 | n/a | #endif |
---|
7024 | n/a | |
---|
7025 | n/a | /* Flags for send, recv */ |
---|
7026 | n/a | #ifdef MSG_OOB |
---|
7027 | n/a | PyModule_AddIntMacro(m, MSG_OOB); |
---|
7028 | n/a | #endif |
---|
7029 | n/a | #ifdef MSG_PEEK |
---|
7030 | n/a | PyModule_AddIntMacro(m, MSG_PEEK); |
---|
7031 | n/a | #endif |
---|
7032 | n/a | #ifdef MSG_DONTROUTE |
---|
7033 | n/a | PyModule_AddIntMacro(m, MSG_DONTROUTE); |
---|
7034 | n/a | #endif |
---|
7035 | n/a | #ifdef MSG_DONTWAIT |
---|
7036 | n/a | PyModule_AddIntMacro(m, MSG_DONTWAIT); |
---|
7037 | n/a | #endif |
---|
7038 | n/a | #ifdef MSG_EOR |
---|
7039 | n/a | PyModule_AddIntMacro(m, MSG_EOR); |
---|
7040 | n/a | #endif |
---|
7041 | n/a | #ifdef MSG_TRUNC |
---|
7042 | n/a | PyModule_AddIntMacro(m, MSG_TRUNC); |
---|
7043 | n/a | #endif |
---|
7044 | n/a | #ifdef MSG_CTRUNC |
---|
7045 | n/a | PyModule_AddIntMacro(m, MSG_CTRUNC); |
---|
7046 | n/a | #endif |
---|
7047 | n/a | #ifdef MSG_WAITALL |
---|
7048 | n/a | PyModule_AddIntMacro(m, MSG_WAITALL); |
---|
7049 | n/a | #endif |
---|
7050 | n/a | #ifdef MSG_BTAG |
---|
7051 | n/a | PyModule_AddIntMacro(m, MSG_BTAG); |
---|
7052 | n/a | #endif |
---|
7053 | n/a | #ifdef MSG_ETAG |
---|
7054 | n/a | PyModule_AddIntMacro(m, MSG_ETAG); |
---|
7055 | n/a | #endif |
---|
7056 | n/a | #ifdef MSG_NOSIGNAL |
---|
7057 | n/a | PyModule_AddIntMacro(m, MSG_NOSIGNAL); |
---|
7058 | n/a | #endif |
---|
7059 | n/a | #ifdef MSG_NOTIFICATION |
---|
7060 | n/a | PyModule_AddIntMacro(m, MSG_NOTIFICATION); |
---|
7061 | n/a | #endif |
---|
7062 | n/a | #ifdef MSG_CMSG_CLOEXEC |
---|
7063 | n/a | PyModule_AddIntMacro(m, MSG_CMSG_CLOEXEC); |
---|
7064 | n/a | #endif |
---|
7065 | n/a | #ifdef MSG_ERRQUEUE |
---|
7066 | n/a | PyModule_AddIntMacro(m, MSG_ERRQUEUE); |
---|
7067 | n/a | #endif |
---|
7068 | n/a | #ifdef MSG_CONFIRM |
---|
7069 | n/a | PyModule_AddIntMacro(m, MSG_CONFIRM); |
---|
7070 | n/a | #endif |
---|
7071 | n/a | #ifdef MSG_MORE |
---|
7072 | n/a | PyModule_AddIntMacro(m, MSG_MORE); |
---|
7073 | n/a | #endif |
---|
7074 | n/a | #ifdef MSG_EOF |
---|
7075 | n/a | PyModule_AddIntMacro(m, MSG_EOF); |
---|
7076 | n/a | #endif |
---|
7077 | n/a | #ifdef MSG_BCAST |
---|
7078 | n/a | PyModule_AddIntMacro(m, MSG_BCAST); |
---|
7079 | n/a | #endif |
---|
7080 | n/a | #ifdef MSG_MCAST |
---|
7081 | n/a | PyModule_AddIntMacro(m, MSG_MCAST); |
---|
7082 | n/a | #endif |
---|
7083 | n/a | #ifdef MSG_FASTOPEN |
---|
7084 | n/a | PyModule_AddIntMacro(m, MSG_FASTOPEN); |
---|
7085 | n/a | #endif |
---|
7086 | n/a | |
---|
7087 | n/a | /* Protocol level and numbers, usable for [gs]etsockopt */ |
---|
7088 | n/a | #ifdef SOL_SOCKET |
---|
7089 | n/a | PyModule_AddIntMacro(m, SOL_SOCKET); |
---|
7090 | n/a | #endif |
---|
7091 | n/a | #ifdef SOL_IP |
---|
7092 | n/a | PyModule_AddIntMacro(m, SOL_IP); |
---|
7093 | n/a | #else |
---|
7094 | n/a | PyModule_AddIntConstant(m, "SOL_IP", 0); |
---|
7095 | n/a | #endif |
---|
7096 | n/a | #ifdef SOL_IPX |
---|
7097 | n/a | PyModule_AddIntMacro(m, SOL_IPX); |
---|
7098 | n/a | #endif |
---|
7099 | n/a | #ifdef SOL_AX25 |
---|
7100 | n/a | PyModule_AddIntMacro(m, SOL_AX25); |
---|
7101 | n/a | #endif |
---|
7102 | n/a | #ifdef SOL_ATALK |
---|
7103 | n/a | PyModule_AddIntMacro(m, SOL_ATALK); |
---|
7104 | n/a | #endif |
---|
7105 | n/a | #ifdef SOL_NETROM |
---|
7106 | n/a | PyModule_AddIntMacro(m, SOL_NETROM); |
---|
7107 | n/a | #endif |
---|
7108 | n/a | #ifdef SOL_ROSE |
---|
7109 | n/a | PyModule_AddIntMacro(m, SOL_ROSE); |
---|
7110 | n/a | #endif |
---|
7111 | n/a | #ifdef SOL_TCP |
---|
7112 | n/a | PyModule_AddIntMacro(m, SOL_TCP); |
---|
7113 | n/a | #else |
---|
7114 | n/a | PyModule_AddIntConstant(m, "SOL_TCP", 6); |
---|
7115 | n/a | #endif |
---|
7116 | n/a | #ifdef SOL_UDP |
---|
7117 | n/a | PyModule_AddIntMacro(m, SOL_UDP); |
---|
7118 | n/a | #else |
---|
7119 | n/a | PyModule_AddIntConstant(m, "SOL_UDP", 17); |
---|
7120 | n/a | #endif |
---|
7121 | n/a | #ifdef SOL_CAN_BASE |
---|
7122 | n/a | PyModule_AddIntMacro(m, SOL_CAN_BASE); |
---|
7123 | n/a | #endif |
---|
7124 | n/a | #ifdef SOL_CAN_RAW |
---|
7125 | n/a | PyModule_AddIntMacro(m, SOL_CAN_RAW); |
---|
7126 | n/a | PyModule_AddIntMacro(m, CAN_RAW); |
---|
7127 | n/a | #endif |
---|
7128 | n/a | #ifdef HAVE_LINUX_CAN_H |
---|
7129 | n/a | PyModule_AddIntMacro(m, CAN_EFF_FLAG); |
---|
7130 | n/a | PyModule_AddIntMacro(m, CAN_RTR_FLAG); |
---|
7131 | n/a | PyModule_AddIntMacro(m, CAN_ERR_FLAG); |
---|
7132 | n/a | |
---|
7133 | n/a | PyModule_AddIntMacro(m, CAN_SFF_MASK); |
---|
7134 | n/a | PyModule_AddIntMacro(m, CAN_EFF_MASK); |
---|
7135 | n/a | PyModule_AddIntMacro(m, CAN_ERR_MASK); |
---|
7136 | n/a | #endif |
---|
7137 | n/a | #ifdef HAVE_LINUX_CAN_RAW_H |
---|
7138 | n/a | PyModule_AddIntMacro(m, CAN_RAW_FILTER); |
---|
7139 | n/a | PyModule_AddIntMacro(m, CAN_RAW_ERR_FILTER); |
---|
7140 | n/a | PyModule_AddIntMacro(m, CAN_RAW_LOOPBACK); |
---|
7141 | n/a | PyModule_AddIntMacro(m, CAN_RAW_RECV_OWN_MSGS); |
---|
7142 | n/a | #endif |
---|
7143 | n/a | #ifdef HAVE_LINUX_CAN_RAW_FD_FRAMES |
---|
7144 | n/a | PyModule_AddIntMacro(m, CAN_RAW_FD_FRAMES); |
---|
7145 | n/a | #endif |
---|
7146 | n/a | #ifdef HAVE_LINUX_CAN_BCM_H |
---|
7147 | n/a | PyModule_AddIntMacro(m, CAN_BCM); |
---|
7148 | n/a | PyModule_AddIntConstant(m, "CAN_BCM_TX_SETUP", TX_SETUP); |
---|
7149 | n/a | PyModule_AddIntConstant(m, "CAN_BCM_TX_DELETE", TX_DELETE); |
---|
7150 | n/a | PyModule_AddIntConstant(m, "CAN_BCM_TX_READ", TX_READ); |
---|
7151 | n/a | PyModule_AddIntConstant(m, "CAN_BCM_TX_SEND", TX_SEND); |
---|
7152 | n/a | PyModule_AddIntConstant(m, "CAN_BCM_RX_SETUP", RX_SETUP); |
---|
7153 | n/a | PyModule_AddIntConstant(m, "CAN_BCM_RX_DELETE", RX_DELETE); |
---|
7154 | n/a | PyModule_AddIntConstant(m, "CAN_BCM_RX_READ", RX_READ); |
---|
7155 | n/a | PyModule_AddIntConstant(m, "CAN_BCM_TX_STATUS", TX_STATUS); |
---|
7156 | n/a | PyModule_AddIntConstant(m, "CAN_BCM_TX_EXPIRED", TX_EXPIRED); |
---|
7157 | n/a | PyModule_AddIntConstant(m, "CAN_BCM_RX_STATUS", RX_STATUS); |
---|
7158 | n/a | PyModule_AddIntConstant(m, "CAN_BCM_RX_TIMEOUT", RX_TIMEOUT); |
---|
7159 | n/a | PyModule_AddIntConstant(m, "CAN_BCM_RX_CHANGED", RX_CHANGED); |
---|
7160 | n/a | #endif |
---|
7161 | n/a | #ifdef SOL_RDS |
---|
7162 | n/a | PyModule_AddIntMacro(m, SOL_RDS); |
---|
7163 | n/a | #endif |
---|
7164 | n/a | #ifdef HAVE_SOCKADDR_ALG |
---|
7165 | n/a | PyModule_AddIntMacro(m, SOL_ALG); |
---|
7166 | n/a | #endif |
---|
7167 | n/a | #ifdef RDS_CANCEL_SENT_TO |
---|
7168 | n/a | PyModule_AddIntMacro(m, RDS_CANCEL_SENT_TO); |
---|
7169 | n/a | #endif |
---|
7170 | n/a | #ifdef RDS_GET_MR |
---|
7171 | n/a | PyModule_AddIntMacro(m, RDS_GET_MR); |
---|
7172 | n/a | #endif |
---|
7173 | n/a | #ifdef RDS_FREE_MR |
---|
7174 | n/a | PyModule_AddIntMacro(m, RDS_FREE_MR); |
---|
7175 | n/a | #endif |
---|
7176 | n/a | #ifdef RDS_RECVERR |
---|
7177 | n/a | PyModule_AddIntMacro(m, RDS_RECVERR); |
---|
7178 | n/a | #endif |
---|
7179 | n/a | #ifdef RDS_CONG_MONITOR |
---|
7180 | n/a | PyModule_AddIntMacro(m, RDS_CONG_MONITOR); |
---|
7181 | n/a | #endif |
---|
7182 | n/a | #ifdef RDS_GET_MR_FOR_DEST |
---|
7183 | n/a | PyModule_AddIntMacro(m, RDS_GET_MR_FOR_DEST); |
---|
7184 | n/a | #endif |
---|
7185 | n/a | #ifdef IPPROTO_IP |
---|
7186 | n/a | PyModule_AddIntMacro(m, IPPROTO_IP); |
---|
7187 | n/a | #else |
---|
7188 | n/a | PyModule_AddIntConstant(m, "IPPROTO_IP", 0); |
---|
7189 | n/a | #endif |
---|
7190 | n/a | #ifdef IPPROTO_HOPOPTS |
---|
7191 | n/a | PyModule_AddIntMacro(m, IPPROTO_HOPOPTS); |
---|
7192 | n/a | #endif |
---|
7193 | n/a | #ifdef IPPROTO_ICMP |
---|
7194 | n/a | PyModule_AddIntMacro(m, IPPROTO_ICMP); |
---|
7195 | n/a | #else |
---|
7196 | n/a | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1); |
---|
7197 | n/a | #endif |
---|
7198 | n/a | #ifdef IPPROTO_IGMP |
---|
7199 | n/a | PyModule_AddIntMacro(m, IPPROTO_IGMP); |
---|
7200 | n/a | #endif |
---|
7201 | n/a | #ifdef IPPROTO_GGP |
---|
7202 | n/a | PyModule_AddIntMacro(m, IPPROTO_GGP); |
---|
7203 | n/a | #endif |
---|
7204 | n/a | #ifdef IPPROTO_IPV4 |
---|
7205 | n/a | PyModule_AddIntMacro(m, IPPROTO_IPV4); |
---|
7206 | n/a | #endif |
---|
7207 | n/a | #ifdef IPPROTO_IPV6 |
---|
7208 | n/a | PyModule_AddIntMacro(m, IPPROTO_IPV6); |
---|
7209 | n/a | #endif |
---|
7210 | n/a | #ifdef IPPROTO_IPIP |
---|
7211 | n/a | PyModule_AddIntMacro(m, IPPROTO_IPIP); |
---|
7212 | n/a | #endif |
---|
7213 | n/a | #ifdef IPPROTO_TCP |
---|
7214 | n/a | PyModule_AddIntMacro(m, IPPROTO_TCP); |
---|
7215 | n/a | #else |
---|
7216 | n/a | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6); |
---|
7217 | n/a | #endif |
---|
7218 | n/a | #ifdef IPPROTO_EGP |
---|
7219 | n/a | PyModule_AddIntMacro(m, IPPROTO_EGP); |
---|
7220 | n/a | #endif |
---|
7221 | n/a | #ifdef IPPROTO_PUP |
---|
7222 | n/a | PyModule_AddIntMacro(m, IPPROTO_PUP); |
---|
7223 | n/a | #endif |
---|
7224 | n/a | #ifdef IPPROTO_UDP |
---|
7225 | n/a | PyModule_AddIntMacro(m, IPPROTO_UDP); |
---|
7226 | n/a | #else |
---|
7227 | n/a | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17); |
---|
7228 | n/a | #endif |
---|
7229 | n/a | #ifdef IPPROTO_IDP |
---|
7230 | n/a | PyModule_AddIntMacro(m, IPPROTO_IDP); |
---|
7231 | n/a | #endif |
---|
7232 | n/a | #ifdef IPPROTO_HELLO |
---|
7233 | n/a | PyModule_AddIntMacro(m, IPPROTO_HELLO); |
---|
7234 | n/a | #endif |
---|
7235 | n/a | #ifdef IPPROTO_ND |
---|
7236 | n/a | PyModule_AddIntMacro(m, IPPROTO_ND); |
---|
7237 | n/a | #endif |
---|
7238 | n/a | #ifdef IPPROTO_TP |
---|
7239 | n/a | PyModule_AddIntMacro(m, IPPROTO_TP); |
---|
7240 | n/a | #endif |
---|
7241 | n/a | #ifdef IPPROTO_IPV6 |
---|
7242 | n/a | PyModule_AddIntMacro(m, IPPROTO_IPV6); |
---|
7243 | n/a | #endif |
---|
7244 | n/a | #ifdef IPPROTO_ROUTING |
---|
7245 | n/a | PyModule_AddIntMacro(m, IPPROTO_ROUTING); |
---|
7246 | n/a | #endif |
---|
7247 | n/a | #ifdef IPPROTO_FRAGMENT |
---|
7248 | n/a | PyModule_AddIntMacro(m, IPPROTO_FRAGMENT); |
---|
7249 | n/a | #endif |
---|
7250 | n/a | #ifdef IPPROTO_RSVP |
---|
7251 | n/a | PyModule_AddIntMacro(m, IPPROTO_RSVP); |
---|
7252 | n/a | #endif |
---|
7253 | n/a | #ifdef IPPROTO_GRE |
---|
7254 | n/a | PyModule_AddIntMacro(m, IPPROTO_GRE); |
---|
7255 | n/a | #endif |
---|
7256 | n/a | #ifdef IPPROTO_ESP |
---|
7257 | n/a | PyModule_AddIntMacro(m, IPPROTO_ESP); |
---|
7258 | n/a | #endif |
---|
7259 | n/a | #ifdef IPPROTO_AH |
---|
7260 | n/a | PyModule_AddIntMacro(m, IPPROTO_AH); |
---|
7261 | n/a | #endif |
---|
7262 | n/a | #ifdef IPPROTO_MOBILE |
---|
7263 | n/a | PyModule_AddIntMacro(m, IPPROTO_MOBILE); |
---|
7264 | n/a | #endif |
---|
7265 | n/a | #ifdef IPPROTO_ICMPV6 |
---|
7266 | n/a | PyModule_AddIntMacro(m, IPPROTO_ICMPV6); |
---|
7267 | n/a | #endif |
---|
7268 | n/a | #ifdef IPPROTO_NONE |
---|
7269 | n/a | PyModule_AddIntMacro(m, IPPROTO_NONE); |
---|
7270 | n/a | #endif |
---|
7271 | n/a | #ifdef IPPROTO_DSTOPTS |
---|
7272 | n/a | PyModule_AddIntMacro(m, IPPROTO_DSTOPTS); |
---|
7273 | n/a | #endif |
---|
7274 | n/a | #ifdef IPPROTO_XTP |
---|
7275 | n/a | PyModule_AddIntMacro(m, IPPROTO_XTP); |
---|
7276 | n/a | #endif |
---|
7277 | n/a | #ifdef IPPROTO_EON |
---|
7278 | n/a | PyModule_AddIntMacro(m, IPPROTO_EON); |
---|
7279 | n/a | #endif |
---|
7280 | n/a | #ifdef IPPROTO_PIM |
---|
7281 | n/a | PyModule_AddIntMacro(m, IPPROTO_PIM); |
---|
7282 | n/a | #endif |
---|
7283 | n/a | #ifdef IPPROTO_IPCOMP |
---|
7284 | n/a | PyModule_AddIntMacro(m, IPPROTO_IPCOMP); |
---|
7285 | n/a | #endif |
---|
7286 | n/a | #ifdef IPPROTO_VRRP |
---|
7287 | n/a | PyModule_AddIntMacro(m, IPPROTO_VRRP); |
---|
7288 | n/a | #endif |
---|
7289 | n/a | #ifdef IPPROTO_SCTP |
---|
7290 | n/a | PyModule_AddIntMacro(m, IPPROTO_SCTP); |
---|
7291 | n/a | #endif |
---|
7292 | n/a | #ifdef IPPROTO_BIP |
---|
7293 | n/a | PyModule_AddIntMacro(m, IPPROTO_BIP); |
---|
7294 | n/a | #endif |
---|
7295 | n/a | /**/ |
---|
7296 | n/a | #ifdef IPPROTO_RAW |
---|
7297 | n/a | PyModule_AddIntMacro(m, IPPROTO_RAW); |
---|
7298 | n/a | #else |
---|
7299 | n/a | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255); |
---|
7300 | n/a | #endif |
---|
7301 | n/a | #ifdef IPPROTO_MAX |
---|
7302 | n/a | PyModule_AddIntMacro(m, IPPROTO_MAX); |
---|
7303 | n/a | #endif |
---|
7304 | n/a | |
---|
7305 | n/a | #ifdef SYSPROTO_CONTROL |
---|
7306 | n/a | PyModule_AddIntMacro(m, SYSPROTO_CONTROL); |
---|
7307 | n/a | #endif |
---|
7308 | n/a | |
---|
7309 | n/a | /* Some port configuration */ |
---|
7310 | n/a | #ifdef IPPORT_RESERVED |
---|
7311 | n/a | PyModule_AddIntMacro(m, IPPORT_RESERVED); |
---|
7312 | n/a | #else |
---|
7313 | n/a | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024); |
---|
7314 | n/a | #endif |
---|
7315 | n/a | #ifdef IPPORT_USERRESERVED |
---|
7316 | n/a | PyModule_AddIntMacro(m, IPPORT_USERRESERVED); |
---|
7317 | n/a | #else |
---|
7318 | n/a | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000); |
---|
7319 | n/a | #endif |
---|
7320 | n/a | |
---|
7321 | n/a | /* Some reserved IP v.4 addresses */ |
---|
7322 | n/a | #ifdef INADDR_ANY |
---|
7323 | n/a | PyModule_AddIntMacro(m, INADDR_ANY); |
---|
7324 | n/a | #else |
---|
7325 | n/a | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000); |
---|
7326 | n/a | #endif |
---|
7327 | n/a | #ifdef INADDR_BROADCAST |
---|
7328 | n/a | PyModule_AddIntMacro(m, INADDR_BROADCAST); |
---|
7329 | n/a | #else |
---|
7330 | n/a | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff); |
---|
7331 | n/a | #endif |
---|
7332 | n/a | #ifdef INADDR_LOOPBACK |
---|
7333 | n/a | PyModule_AddIntMacro(m, INADDR_LOOPBACK); |
---|
7334 | n/a | #else |
---|
7335 | n/a | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001); |
---|
7336 | n/a | #endif |
---|
7337 | n/a | #ifdef INADDR_UNSPEC_GROUP |
---|
7338 | n/a | PyModule_AddIntMacro(m, INADDR_UNSPEC_GROUP); |
---|
7339 | n/a | #else |
---|
7340 | n/a | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000); |
---|
7341 | n/a | #endif |
---|
7342 | n/a | #ifdef INADDR_ALLHOSTS_GROUP |
---|
7343 | n/a | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", |
---|
7344 | n/a | INADDR_ALLHOSTS_GROUP); |
---|
7345 | n/a | #else |
---|
7346 | n/a | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001); |
---|
7347 | n/a | #endif |
---|
7348 | n/a | #ifdef INADDR_MAX_LOCAL_GROUP |
---|
7349 | n/a | PyModule_AddIntMacro(m, INADDR_MAX_LOCAL_GROUP); |
---|
7350 | n/a | #else |
---|
7351 | n/a | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff); |
---|
7352 | n/a | #endif |
---|
7353 | n/a | #ifdef INADDR_NONE |
---|
7354 | n/a | PyModule_AddIntMacro(m, INADDR_NONE); |
---|
7355 | n/a | #else |
---|
7356 | n/a | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff); |
---|
7357 | n/a | #endif |
---|
7358 | n/a | |
---|
7359 | n/a | /* IPv4 [gs]etsockopt options */ |
---|
7360 | n/a | #ifdef IP_OPTIONS |
---|
7361 | n/a | PyModule_AddIntMacro(m, IP_OPTIONS); |
---|
7362 | n/a | #endif |
---|
7363 | n/a | #ifdef IP_HDRINCL |
---|
7364 | n/a | PyModule_AddIntMacro(m, IP_HDRINCL); |
---|
7365 | n/a | #endif |
---|
7366 | n/a | #ifdef IP_TOS |
---|
7367 | n/a | PyModule_AddIntMacro(m, IP_TOS); |
---|
7368 | n/a | #endif |
---|
7369 | n/a | #ifdef IP_TTL |
---|
7370 | n/a | PyModule_AddIntMacro(m, IP_TTL); |
---|
7371 | n/a | #endif |
---|
7372 | n/a | #ifdef IP_RECVOPTS |
---|
7373 | n/a | PyModule_AddIntMacro(m, IP_RECVOPTS); |
---|
7374 | n/a | #endif |
---|
7375 | n/a | #ifdef IP_RECVRETOPTS |
---|
7376 | n/a | PyModule_AddIntMacro(m, IP_RECVRETOPTS); |
---|
7377 | n/a | #endif |
---|
7378 | n/a | #ifdef IP_RECVDSTADDR |
---|
7379 | n/a | PyModule_AddIntMacro(m, IP_RECVDSTADDR); |
---|
7380 | n/a | #endif |
---|
7381 | n/a | #ifdef IP_RETOPTS |
---|
7382 | n/a | PyModule_AddIntMacro(m, IP_RETOPTS); |
---|
7383 | n/a | #endif |
---|
7384 | n/a | #ifdef IP_MULTICAST_IF |
---|
7385 | n/a | PyModule_AddIntMacro(m, IP_MULTICAST_IF); |
---|
7386 | n/a | #endif |
---|
7387 | n/a | #ifdef IP_MULTICAST_TTL |
---|
7388 | n/a | PyModule_AddIntMacro(m, IP_MULTICAST_TTL); |
---|
7389 | n/a | #endif |
---|
7390 | n/a | #ifdef IP_MULTICAST_LOOP |
---|
7391 | n/a | PyModule_AddIntMacro(m, IP_MULTICAST_LOOP); |
---|
7392 | n/a | #endif |
---|
7393 | n/a | #ifdef IP_ADD_MEMBERSHIP |
---|
7394 | n/a | PyModule_AddIntMacro(m, IP_ADD_MEMBERSHIP); |
---|
7395 | n/a | #endif |
---|
7396 | n/a | #ifdef IP_DROP_MEMBERSHIP |
---|
7397 | n/a | PyModule_AddIntMacro(m, IP_DROP_MEMBERSHIP); |
---|
7398 | n/a | #endif |
---|
7399 | n/a | #ifdef IP_DEFAULT_MULTICAST_TTL |
---|
7400 | n/a | PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_TTL); |
---|
7401 | n/a | #endif |
---|
7402 | n/a | #ifdef IP_DEFAULT_MULTICAST_LOOP |
---|
7403 | n/a | PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_LOOP); |
---|
7404 | n/a | #endif |
---|
7405 | n/a | #ifdef IP_MAX_MEMBERSHIPS |
---|
7406 | n/a | PyModule_AddIntMacro(m, IP_MAX_MEMBERSHIPS); |
---|
7407 | n/a | #endif |
---|
7408 | n/a | #ifdef IP_TRANSPARENT |
---|
7409 | n/a | PyModule_AddIntMacro(m, IP_TRANSPARENT); |
---|
7410 | n/a | #endif |
---|
7411 | n/a | |
---|
7412 | n/a | /* IPv6 [gs]etsockopt options, defined in RFC2553 */ |
---|
7413 | n/a | #ifdef IPV6_JOIN_GROUP |
---|
7414 | n/a | PyModule_AddIntMacro(m, IPV6_JOIN_GROUP); |
---|
7415 | n/a | #endif |
---|
7416 | n/a | #ifdef IPV6_LEAVE_GROUP |
---|
7417 | n/a | PyModule_AddIntMacro(m, IPV6_LEAVE_GROUP); |
---|
7418 | n/a | #endif |
---|
7419 | n/a | #ifdef IPV6_MULTICAST_HOPS |
---|
7420 | n/a | PyModule_AddIntMacro(m, IPV6_MULTICAST_HOPS); |
---|
7421 | n/a | #endif |
---|
7422 | n/a | #ifdef IPV6_MULTICAST_IF |
---|
7423 | n/a | PyModule_AddIntMacro(m, IPV6_MULTICAST_IF); |
---|
7424 | n/a | #endif |
---|
7425 | n/a | #ifdef IPV6_MULTICAST_LOOP |
---|
7426 | n/a | PyModule_AddIntMacro(m, IPV6_MULTICAST_LOOP); |
---|
7427 | n/a | #endif |
---|
7428 | n/a | #ifdef IPV6_UNICAST_HOPS |
---|
7429 | n/a | PyModule_AddIntMacro(m, IPV6_UNICAST_HOPS); |
---|
7430 | n/a | #endif |
---|
7431 | n/a | /* Additional IPV6 socket options, defined in RFC 3493 */ |
---|
7432 | n/a | #ifdef IPV6_V6ONLY |
---|
7433 | n/a | PyModule_AddIntMacro(m, IPV6_V6ONLY); |
---|
7434 | n/a | #endif |
---|
7435 | n/a | /* Advanced IPV6 socket options, from RFC 3542 */ |
---|
7436 | n/a | #ifdef IPV6_CHECKSUM |
---|
7437 | n/a | PyModule_AddIntMacro(m, IPV6_CHECKSUM); |
---|
7438 | n/a | #endif |
---|
7439 | n/a | #ifdef IPV6_DONTFRAG |
---|
7440 | n/a | PyModule_AddIntMacro(m, IPV6_DONTFRAG); |
---|
7441 | n/a | #endif |
---|
7442 | n/a | #ifdef IPV6_DSTOPTS |
---|
7443 | n/a | PyModule_AddIntMacro(m, IPV6_DSTOPTS); |
---|
7444 | n/a | #endif |
---|
7445 | n/a | #ifdef IPV6_HOPLIMIT |
---|
7446 | n/a | PyModule_AddIntMacro(m, IPV6_HOPLIMIT); |
---|
7447 | n/a | #endif |
---|
7448 | n/a | #ifdef IPV6_HOPOPTS |
---|
7449 | n/a | PyModule_AddIntMacro(m, IPV6_HOPOPTS); |
---|
7450 | n/a | #endif |
---|
7451 | n/a | #ifdef IPV6_NEXTHOP |
---|
7452 | n/a | PyModule_AddIntMacro(m, IPV6_NEXTHOP); |
---|
7453 | n/a | #endif |
---|
7454 | n/a | #ifdef IPV6_PATHMTU |
---|
7455 | n/a | PyModule_AddIntMacro(m, IPV6_PATHMTU); |
---|
7456 | n/a | #endif |
---|
7457 | n/a | #ifdef IPV6_PKTINFO |
---|
7458 | n/a | PyModule_AddIntMacro(m, IPV6_PKTINFO); |
---|
7459 | n/a | #endif |
---|
7460 | n/a | #ifdef IPV6_RECVDSTOPTS |
---|
7461 | n/a | PyModule_AddIntMacro(m, IPV6_RECVDSTOPTS); |
---|
7462 | n/a | #endif |
---|
7463 | n/a | #ifdef IPV6_RECVHOPLIMIT |
---|
7464 | n/a | PyModule_AddIntMacro(m, IPV6_RECVHOPLIMIT); |
---|
7465 | n/a | #endif |
---|
7466 | n/a | #ifdef IPV6_RECVHOPOPTS |
---|
7467 | n/a | PyModule_AddIntMacro(m, IPV6_RECVHOPOPTS); |
---|
7468 | n/a | #endif |
---|
7469 | n/a | #ifdef IPV6_RECVPKTINFO |
---|
7470 | n/a | PyModule_AddIntMacro(m, IPV6_RECVPKTINFO); |
---|
7471 | n/a | #endif |
---|
7472 | n/a | #ifdef IPV6_RECVRTHDR |
---|
7473 | n/a | PyModule_AddIntMacro(m, IPV6_RECVRTHDR); |
---|
7474 | n/a | #endif |
---|
7475 | n/a | #ifdef IPV6_RECVTCLASS |
---|
7476 | n/a | PyModule_AddIntMacro(m, IPV6_RECVTCLASS); |
---|
7477 | n/a | #endif |
---|
7478 | n/a | #ifdef IPV6_RTHDR |
---|
7479 | n/a | PyModule_AddIntMacro(m, IPV6_RTHDR); |
---|
7480 | n/a | #endif |
---|
7481 | n/a | #ifdef IPV6_RTHDRDSTOPTS |
---|
7482 | n/a | PyModule_AddIntMacro(m, IPV6_RTHDRDSTOPTS); |
---|
7483 | n/a | #endif |
---|
7484 | n/a | #ifdef IPV6_RTHDR_TYPE_0 |
---|
7485 | n/a | PyModule_AddIntMacro(m, IPV6_RTHDR_TYPE_0); |
---|
7486 | n/a | #endif |
---|
7487 | n/a | #ifdef IPV6_RECVPATHMTU |
---|
7488 | n/a | PyModule_AddIntMacro(m, IPV6_RECVPATHMTU); |
---|
7489 | n/a | #endif |
---|
7490 | n/a | #ifdef IPV6_TCLASS |
---|
7491 | n/a | PyModule_AddIntMacro(m, IPV6_TCLASS); |
---|
7492 | n/a | #endif |
---|
7493 | n/a | #ifdef IPV6_USE_MIN_MTU |
---|
7494 | n/a | PyModule_AddIntMacro(m, IPV6_USE_MIN_MTU); |
---|
7495 | n/a | #endif |
---|
7496 | n/a | |
---|
7497 | n/a | /* TCP options */ |
---|
7498 | n/a | #ifdef TCP_NODELAY |
---|
7499 | n/a | PyModule_AddIntMacro(m, TCP_NODELAY); |
---|
7500 | n/a | #endif |
---|
7501 | n/a | #ifdef TCP_MAXSEG |
---|
7502 | n/a | PyModule_AddIntMacro(m, TCP_MAXSEG); |
---|
7503 | n/a | #endif |
---|
7504 | n/a | #ifdef TCP_CORK |
---|
7505 | n/a | PyModule_AddIntMacro(m, TCP_CORK); |
---|
7506 | n/a | #endif |
---|
7507 | n/a | #ifdef TCP_KEEPIDLE |
---|
7508 | n/a | PyModule_AddIntMacro(m, TCP_KEEPIDLE); |
---|
7509 | n/a | #endif |
---|
7510 | n/a | #ifdef TCP_KEEPINTVL |
---|
7511 | n/a | PyModule_AddIntMacro(m, TCP_KEEPINTVL); |
---|
7512 | n/a | #endif |
---|
7513 | n/a | #ifdef TCP_KEEPCNT |
---|
7514 | n/a | PyModule_AddIntMacro(m, TCP_KEEPCNT); |
---|
7515 | n/a | #endif |
---|
7516 | n/a | #ifdef TCP_SYNCNT |
---|
7517 | n/a | PyModule_AddIntMacro(m, TCP_SYNCNT); |
---|
7518 | n/a | #endif |
---|
7519 | n/a | #ifdef TCP_LINGER2 |
---|
7520 | n/a | PyModule_AddIntMacro(m, TCP_LINGER2); |
---|
7521 | n/a | #endif |
---|
7522 | n/a | #ifdef TCP_DEFER_ACCEPT |
---|
7523 | n/a | PyModule_AddIntMacro(m, TCP_DEFER_ACCEPT); |
---|
7524 | n/a | #endif |
---|
7525 | n/a | #ifdef TCP_WINDOW_CLAMP |
---|
7526 | n/a | PyModule_AddIntMacro(m, TCP_WINDOW_CLAMP); |
---|
7527 | n/a | #endif |
---|
7528 | n/a | #ifdef TCP_INFO |
---|
7529 | n/a | PyModule_AddIntMacro(m, TCP_INFO); |
---|
7530 | n/a | #endif |
---|
7531 | n/a | #ifdef TCP_QUICKACK |
---|
7532 | n/a | PyModule_AddIntMacro(m, TCP_QUICKACK); |
---|
7533 | n/a | #endif |
---|
7534 | n/a | #ifdef TCP_FASTOPEN |
---|
7535 | n/a | PyModule_AddIntMacro(m, TCP_FASTOPEN); |
---|
7536 | n/a | #endif |
---|
7537 | n/a | #ifdef TCP_CONGESTION |
---|
7538 | n/a | PyModule_AddIntMacro(m, TCP_CONGESTION); |
---|
7539 | n/a | #endif |
---|
7540 | n/a | #ifdef TCP_USER_TIMEOUT |
---|
7541 | n/a | PyModule_AddIntMacro(m, TCP_USER_TIMEOUT); |
---|
7542 | n/a | #endif |
---|
7543 | n/a | |
---|
7544 | n/a | /* IPX options */ |
---|
7545 | n/a | #ifdef IPX_TYPE |
---|
7546 | n/a | PyModule_AddIntMacro(m, IPX_TYPE); |
---|
7547 | n/a | #endif |
---|
7548 | n/a | |
---|
7549 | n/a | /* Reliable Datagram Sockets */ |
---|
7550 | n/a | #ifdef RDS_CMSG_RDMA_ARGS |
---|
7551 | n/a | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_ARGS); |
---|
7552 | n/a | #endif |
---|
7553 | n/a | #ifdef RDS_CMSG_RDMA_DEST |
---|
7554 | n/a | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_DEST); |
---|
7555 | n/a | #endif |
---|
7556 | n/a | #ifdef RDS_CMSG_RDMA_MAP |
---|
7557 | n/a | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_MAP); |
---|
7558 | n/a | #endif |
---|
7559 | n/a | #ifdef RDS_CMSG_RDMA_STATUS |
---|
7560 | n/a | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_STATUS); |
---|
7561 | n/a | #endif |
---|
7562 | n/a | #ifdef RDS_CMSG_RDMA_UPDATE |
---|
7563 | n/a | PyModule_AddIntMacro(m, RDS_CMSG_RDMA_UPDATE); |
---|
7564 | n/a | #endif |
---|
7565 | n/a | #ifdef RDS_RDMA_READWRITE |
---|
7566 | n/a | PyModule_AddIntMacro(m, RDS_RDMA_READWRITE); |
---|
7567 | n/a | #endif |
---|
7568 | n/a | #ifdef RDS_RDMA_FENCE |
---|
7569 | n/a | PyModule_AddIntMacro(m, RDS_RDMA_FENCE); |
---|
7570 | n/a | #endif |
---|
7571 | n/a | #ifdef RDS_RDMA_INVALIDATE |
---|
7572 | n/a | PyModule_AddIntMacro(m, RDS_RDMA_INVALIDATE); |
---|
7573 | n/a | #endif |
---|
7574 | n/a | #ifdef RDS_RDMA_USE_ONCE |
---|
7575 | n/a | PyModule_AddIntMacro(m, RDS_RDMA_USE_ONCE); |
---|
7576 | n/a | #endif |
---|
7577 | n/a | #ifdef RDS_RDMA_DONTWAIT |
---|
7578 | n/a | PyModule_AddIntMacro(m, RDS_RDMA_DONTWAIT); |
---|
7579 | n/a | #endif |
---|
7580 | n/a | #ifdef RDS_RDMA_NOTIFY_ME |
---|
7581 | n/a | PyModule_AddIntMacro(m, RDS_RDMA_NOTIFY_ME); |
---|
7582 | n/a | #endif |
---|
7583 | n/a | #ifdef RDS_RDMA_SILENT |
---|
7584 | n/a | PyModule_AddIntMacro(m, RDS_RDMA_SILENT); |
---|
7585 | n/a | #endif |
---|
7586 | n/a | |
---|
7587 | n/a | /* get{addr,name}info parameters */ |
---|
7588 | n/a | #ifdef EAI_ADDRFAMILY |
---|
7589 | n/a | PyModule_AddIntMacro(m, EAI_ADDRFAMILY); |
---|
7590 | n/a | #endif |
---|
7591 | n/a | #ifdef EAI_AGAIN |
---|
7592 | n/a | PyModule_AddIntMacro(m, EAI_AGAIN); |
---|
7593 | n/a | #endif |
---|
7594 | n/a | #ifdef EAI_BADFLAGS |
---|
7595 | n/a | PyModule_AddIntMacro(m, EAI_BADFLAGS); |
---|
7596 | n/a | #endif |
---|
7597 | n/a | #ifdef EAI_FAIL |
---|
7598 | n/a | PyModule_AddIntMacro(m, EAI_FAIL); |
---|
7599 | n/a | #endif |
---|
7600 | n/a | #ifdef EAI_FAMILY |
---|
7601 | n/a | PyModule_AddIntMacro(m, EAI_FAMILY); |
---|
7602 | n/a | #endif |
---|
7603 | n/a | #ifdef EAI_MEMORY |
---|
7604 | n/a | PyModule_AddIntMacro(m, EAI_MEMORY); |
---|
7605 | n/a | #endif |
---|
7606 | n/a | #ifdef EAI_NODATA |
---|
7607 | n/a | PyModule_AddIntMacro(m, EAI_NODATA); |
---|
7608 | n/a | #endif |
---|
7609 | n/a | #ifdef EAI_NONAME |
---|
7610 | n/a | PyModule_AddIntMacro(m, EAI_NONAME); |
---|
7611 | n/a | #endif |
---|
7612 | n/a | #ifdef EAI_OVERFLOW |
---|
7613 | n/a | PyModule_AddIntMacro(m, EAI_OVERFLOW); |
---|
7614 | n/a | #endif |
---|
7615 | n/a | #ifdef EAI_SERVICE |
---|
7616 | n/a | PyModule_AddIntMacro(m, EAI_SERVICE); |
---|
7617 | n/a | #endif |
---|
7618 | n/a | #ifdef EAI_SOCKTYPE |
---|
7619 | n/a | PyModule_AddIntMacro(m, EAI_SOCKTYPE); |
---|
7620 | n/a | #endif |
---|
7621 | n/a | #ifdef EAI_SYSTEM |
---|
7622 | n/a | PyModule_AddIntMacro(m, EAI_SYSTEM); |
---|
7623 | n/a | #endif |
---|
7624 | n/a | #ifdef EAI_BADHINTS |
---|
7625 | n/a | PyModule_AddIntMacro(m, EAI_BADHINTS); |
---|
7626 | n/a | #endif |
---|
7627 | n/a | #ifdef EAI_PROTOCOL |
---|
7628 | n/a | PyModule_AddIntMacro(m, EAI_PROTOCOL); |
---|
7629 | n/a | #endif |
---|
7630 | n/a | #ifdef EAI_MAX |
---|
7631 | n/a | PyModule_AddIntMacro(m, EAI_MAX); |
---|
7632 | n/a | #endif |
---|
7633 | n/a | #ifdef AI_PASSIVE |
---|
7634 | n/a | PyModule_AddIntMacro(m, AI_PASSIVE); |
---|
7635 | n/a | #endif |
---|
7636 | n/a | #ifdef AI_CANONNAME |
---|
7637 | n/a | PyModule_AddIntMacro(m, AI_CANONNAME); |
---|
7638 | n/a | #endif |
---|
7639 | n/a | #ifdef AI_NUMERICHOST |
---|
7640 | n/a | PyModule_AddIntMacro(m, AI_NUMERICHOST); |
---|
7641 | n/a | #endif |
---|
7642 | n/a | #ifdef AI_NUMERICSERV |
---|
7643 | n/a | PyModule_AddIntMacro(m, AI_NUMERICSERV); |
---|
7644 | n/a | #endif |
---|
7645 | n/a | #ifdef AI_MASK |
---|
7646 | n/a | PyModule_AddIntMacro(m, AI_MASK); |
---|
7647 | n/a | #endif |
---|
7648 | n/a | #ifdef AI_ALL |
---|
7649 | n/a | PyModule_AddIntMacro(m, AI_ALL); |
---|
7650 | n/a | #endif |
---|
7651 | n/a | #ifdef AI_V4MAPPED_CFG |
---|
7652 | n/a | PyModule_AddIntMacro(m, AI_V4MAPPED_CFG); |
---|
7653 | n/a | #endif |
---|
7654 | n/a | #ifdef AI_ADDRCONFIG |
---|
7655 | n/a | PyModule_AddIntMacro(m, AI_ADDRCONFIG); |
---|
7656 | n/a | #endif |
---|
7657 | n/a | #ifdef AI_V4MAPPED |
---|
7658 | n/a | PyModule_AddIntMacro(m, AI_V4MAPPED); |
---|
7659 | n/a | #endif |
---|
7660 | n/a | #ifdef AI_DEFAULT |
---|
7661 | n/a | PyModule_AddIntMacro(m, AI_DEFAULT); |
---|
7662 | n/a | #endif |
---|
7663 | n/a | #ifdef NI_MAXHOST |
---|
7664 | n/a | PyModule_AddIntMacro(m, NI_MAXHOST); |
---|
7665 | n/a | #endif |
---|
7666 | n/a | #ifdef NI_MAXSERV |
---|
7667 | n/a | PyModule_AddIntMacro(m, NI_MAXSERV); |
---|
7668 | n/a | #endif |
---|
7669 | n/a | #ifdef NI_NOFQDN |
---|
7670 | n/a | PyModule_AddIntMacro(m, NI_NOFQDN); |
---|
7671 | n/a | #endif |
---|
7672 | n/a | #ifdef NI_NUMERICHOST |
---|
7673 | n/a | PyModule_AddIntMacro(m, NI_NUMERICHOST); |
---|
7674 | n/a | #endif |
---|
7675 | n/a | #ifdef NI_NAMEREQD |
---|
7676 | n/a | PyModule_AddIntMacro(m, NI_NAMEREQD); |
---|
7677 | n/a | #endif |
---|
7678 | n/a | #ifdef NI_NUMERICSERV |
---|
7679 | n/a | PyModule_AddIntMacro(m, NI_NUMERICSERV); |
---|
7680 | n/a | #endif |
---|
7681 | n/a | #ifdef NI_DGRAM |
---|
7682 | n/a | PyModule_AddIntMacro(m, NI_DGRAM); |
---|
7683 | n/a | #endif |
---|
7684 | n/a | |
---|
7685 | n/a | /* shutdown() parameters */ |
---|
7686 | n/a | #ifdef SHUT_RD |
---|
7687 | n/a | PyModule_AddIntMacro(m, SHUT_RD); |
---|
7688 | n/a | #elif defined(SD_RECEIVE) |
---|
7689 | n/a | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE); |
---|
7690 | n/a | #else |
---|
7691 | n/a | PyModule_AddIntConstant(m, "SHUT_RD", 0); |
---|
7692 | n/a | #endif |
---|
7693 | n/a | #ifdef SHUT_WR |
---|
7694 | n/a | PyModule_AddIntMacro(m, SHUT_WR); |
---|
7695 | n/a | #elif defined(SD_SEND) |
---|
7696 | n/a | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND); |
---|
7697 | n/a | #else |
---|
7698 | n/a | PyModule_AddIntConstant(m, "SHUT_WR", 1); |
---|
7699 | n/a | #endif |
---|
7700 | n/a | #ifdef SHUT_RDWR |
---|
7701 | n/a | PyModule_AddIntMacro(m, SHUT_RDWR); |
---|
7702 | n/a | #elif defined(SD_BOTH) |
---|
7703 | n/a | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH); |
---|
7704 | n/a | #else |
---|
7705 | n/a | PyModule_AddIntConstant(m, "SHUT_RDWR", 2); |
---|
7706 | n/a | #endif |
---|
7707 | n/a | |
---|
7708 | n/a | #ifdef SIO_RCVALL |
---|
7709 | n/a | { |
---|
7710 | n/a | DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS, |
---|
7711 | n/a | #if defined(SIO_LOOPBACK_FAST_PATH) |
---|
7712 | n/a | SIO_LOOPBACK_FAST_PATH |
---|
7713 | n/a | #endif |
---|
7714 | n/a | }; |
---|
7715 | n/a | const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS", |
---|
7716 | n/a | #if defined(SIO_LOOPBACK_FAST_PATH) |
---|
7717 | n/a | "SIO_LOOPBACK_FAST_PATH" |
---|
7718 | n/a | #endif |
---|
7719 | n/a | }; |
---|
7720 | n/a | int i; |
---|
7721 | n/a | for(i = 0; i<Py_ARRAY_LENGTH(codes); ++i) { |
---|
7722 | n/a | PyObject *tmp; |
---|
7723 | n/a | tmp = PyLong_FromUnsignedLong(codes[i]); |
---|
7724 | n/a | if (tmp == NULL) |
---|
7725 | n/a | return NULL; |
---|
7726 | n/a | PyModule_AddObject(m, names[i], tmp); |
---|
7727 | n/a | } |
---|
7728 | n/a | } |
---|
7729 | n/a | PyModule_AddIntMacro(m, RCVALL_OFF); |
---|
7730 | n/a | PyModule_AddIntMacro(m, RCVALL_ON); |
---|
7731 | n/a | PyModule_AddIntMacro(m, RCVALL_SOCKETLEVELONLY); |
---|
7732 | n/a | #ifdef RCVALL_IPLEVEL |
---|
7733 | n/a | PyModule_AddIntMacro(m, RCVALL_IPLEVEL); |
---|
7734 | n/a | #endif |
---|
7735 | n/a | #ifdef RCVALL_MAX |
---|
7736 | n/a | PyModule_AddIntMacro(m, RCVALL_MAX); |
---|
7737 | n/a | #endif |
---|
7738 | n/a | #endif /* _MSTCPIP_ */ |
---|
7739 | n/a | |
---|
7740 | n/a | /* Initialize gethostbyname lock */ |
---|
7741 | n/a | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK) |
---|
7742 | n/a | netdb_lock = PyThread_allocate_lock(); |
---|
7743 | n/a | #endif |
---|
7744 | n/a | return m; |
---|
7745 | n/a | } |
---|
7746 | n/a | |
---|
7747 | n/a | |
---|
7748 | n/a | #ifndef HAVE_INET_PTON |
---|
7749 | n/a | #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN) |
---|
7750 | n/a | |
---|
7751 | n/a | /* Simplistic emulation code for inet_pton that only works for IPv4 */ |
---|
7752 | n/a | /* These are not exposed because they do not set errno properly */ |
---|
7753 | n/a | |
---|
7754 | n/a | int |
---|
7755 | n/a | inet_pton(int af, const char *src, void *dst) |
---|
7756 | n/a | { |
---|
7757 | n/a | if (af == AF_INET) { |
---|
7758 | n/a | #if (SIZEOF_INT != 4) |
---|
7759 | n/a | #error "Not sure if in_addr_t exists and int is not 32-bits." |
---|
7760 | n/a | #endif |
---|
7761 | n/a | unsigned int packed_addr; |
---|
7762 | n/a | packed_addr = inet_addr(src); |
---|
7763 | n/a | if (packed_addr == INADDR_NONE) |
---|
7764 | n/a | return 0; |
---|
7765 | n/a | memcpy(dst, &packed_addr, 4); |
---|
7766 | n/a | return 1; |
---|
7767 | n/a | } |
---|
7768 | n/a | /* Should set errno to EAFNOSUPPORT */ |
---|
7769 | n/a | return -1; |
---|
7770 | n/a | } |
---|
7771 | n/a | |
---|
7772 | n/a | const char * |
---|
7773 | n/a | inet_ntop(int af, const void *src, char *dst, socklen_t size) |
---|
7774 | n/a | { |
---|
7775 | n/a | if (af == AF_INET) { |
---|
7776 | n/a | struct in_addr packed_addr; |
---|
7777 | n/a | if (size < 16) |
---|
7778 | n/a | /* Should set errno to ENOSPC. */ |
---|
7779 | n/a | return NULL; |
---|
7780 | n/a | memcpy(&packed_addr, src, sizeof(packed_addr)); |
---|
7781 | n/a | return strncpy(dst, inet_ntoa(packed_addr), size); |
---|
7782 | n/a | } |
---|
7783 | n/a | /* Should set errno to EAFNOSUPPORT */ |
---|
7784 | n/a | return NULL; |
---|
7785 | n/a | } |
---|
7786 | n/a | |
---|
7787 | n/a | #endif |
---|
7788 | n/a | #endif |
---|