1 | /* Socket module */
|
---|
2 |
|
---|
3 | /*
|
---|
4 |
|
---|
5 | This module provides an interface to Berkeley socket IPC.
|
---|
6 |
|
---|
7 | Limitations:
|
---|
8 |
|
---|
9 | - Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a
|
---|
10 | portable manner, though AF_PACKET and AF_NETLINK are supported under Linux.
|
---|
11 | - No read/write operations (use sendall/recv or makefile instead).
|
---|
12 | - Additional restrictions apply on some non-Unix platforms (compensated
|
---|
13 | for by socket.py).
|
---|
14 |
|
---|
15 | Module interface:
|
---|
16 |
|
---|
17 | - socket.error: exception raised for socket specific errors
|
---|
18 | - socket.gaierror: exception raised for getaddrinfo/getnameinfo errors,
|
---|
19 | a subclass of socket.error
|
---|
20 | - socket.herror: exception raised for gethostby* errors,
|
---|
21 | a subclass of socket.error
|
---|
22 | - socket.fromfd(fd, family, type[, proto]) --> new socket object (created
|
---|
23 | from an existing file descriptor)
|
---|
24 | - socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
|
---|
25 | - socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])
|
---|
26 | - socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')
|
---|
27 | - socket.getprotobyname(protocolname) --> protocol number
|
---|
28 | - socket.getservbyname(servicename[, protocolname]) --> port number
|
---|
29 | - socket.getservbyport(portnumber[, protocolname]) --> service name
|
---|
30 | - socket.socket([family[, type [, proto]]]) --> new socket object
|
---|
31 | - socket.socketpair([family[, type [, proto]]]) --> (socket, socket)
|
---|
32 | - socket.ntohs(16 bit value) --> new int object
|
---|
33 | - socket.ntohl(32 bit value) --> new int object
|
---|
34 | - socket.htons(16 bit value) --> new int object
|
---|
35 | - socket.htonl(32 bit value) --> new int object
|
---|
36 | - socket.getaddrinfo(host, port [, family, socktype, proto, flags])
|
---|
37 | --> List of (family, socktype, proto, canonname, sockaddr)
|
---|
38 | - socket.getnameinfo(sockaddr, flags) --> (host, port)
|
---|
39 | - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
|
---|
40 | - socket.has_ipv6: boolean value indicating if IPv6 is supported
|
---|
41 | - socket.inet_aton(IP address) -> 32-bit packed IP representation
|
---|
42 | - socket.inet_ntoa(packed IP) -> IP address string
|
---|
43 | - socket.getdefaulttimeout() -> None | float
|
---|
44 | - socket.setdefaulttimeout(None | float)
|
---|
45 | - an Internet socket address is a pair (hostname, port)
|
---|
46 | where hostname can be anything recognized by gethostbyname()
|
---|
47 | (including the dd.dd.dd.dd notation) and port is in host byte order
|
---|
48 | - where a hostname is returned, the dd.dd.dd.dd notation is used
|
---|
49 | - a UNIX domain socket address is a string specifying the pathname
|
---|
50 | - an AF_PACKET socket address is a tuple containing a string
|
---|
51 | specifying the ethernet interface and an integer specifying
|
---|
52 | the Ethernet protocol number to be received. For example:
|
---|
53 | ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple
|
---|
54 | specify packet-type and ha-type/addr.
|
---|
55 |
|
---|
56 | Local naming conventions:
|
---|
57 |
|
---|
58 | - names starting with sock_ are socket object methods
|
---|
59 | - names starting with socket_ are module-level functions
|
---|
60 | - names starting with PySocket are exported through socketmodule.h
|
---|
61 |
|
---|
62 | */
|
---|
63 |
|
---|
64 | #ifdef __APPLE__
|
---|
65 | /*
|
---|
66 | * inet_aton is not available on OSX 10.3, yet we want to use a binary
|
---|
67 | * that was build on 10.4 or later to work on that release, weak linking
|
---|
68 | * comes to the rescue.
|
---|
69 | */
|
---|
70 | # pragma weak inet_aton
|
---|
71 | #endif
|
---|
72 |
|
---|
73 | #include "Python.h"
|
---|
74 | #include "structmember.h"
|
---|
75 |
|
---|
76 | #undef MAX
|
---|
77 | #define MAX(x, y) ((x) < (y) ? (y) : (x))
|
---|
78 |
|
---|
79 | /* Socket object documentation */
|
---|
80 | PyDoc_STRVAR(sock_doc,
|
---|
81 | "socket([family[, type[, proto]]]) -> socket object\n\
|
---|
82 | \n\
|
---|
83 | Open a socket of the given type. The family argument specifies the\n\
|
---|
84 | address family; it defaults to AF_INET. The type argument specifies\n\
|
---|
85 | whether this is a stream (SOCK_STREAM, this is the default)\n\
|
---|
86 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
|
---|
87 | specifying the default protocol. Keyword arguments are accepted.\n\
|
---|
88 | \n\
|
---|
89 | A socket object represents one endpoint of a network connection.\n\
|
---|
90 | \n\
|
---|
91 | Methods of socket objects (keyword arguments not allowed):\n\
|
---|
92 | \n\
|
---|
93 | accept() -- accept a connection, returning new socket and client address\n\
|
---|
94 | bind(addr) -- bind the socket to a local address\n\
|
---|
95 | close() -- close the socket\n\
|
---|
96 | connect(addr) -- connect the socket to a remote address\n\
|
---|
97 | connect_ex(addr) -- connect, return an error code instead of an exception\n\
|
---|
98 | dup() -- return a new socket object identical to the current one [*]\n\
|
---|
99 | fileno() -- return underlying file descriptor\n\
|
---|
100 | getpeername() -- return remote address [*]\n\
|
---|
101 | getsockname() -- return local address\n\
|
---|
102 | getsockopt(level, optname[, buflen]) -- get socket options\n\
|
---|
103 | gettimeout() -- return timeout or None\n\
|
---|
104 | listen(n) -- start listening for incoming connections\n\
|
---|
105 | makefile([mode, [bufsize]]) -- return a file object for the socket [*]\n\
|
---|
106 | recv(buflen[, flags]) -- receive data\n\
|
---|
107 | recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
|
---|
108 | recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\
|
---|
109 | recvfrom_into(buffer[, nbytes, [, flags])\n\
|
---|
110 | -- receive data and sender\'s address (into a buffer)\n\
|
---|
111 | sendall(data[, flags]) -- send all data\n\
|
---|
112 | send(data[, flags]) -- send data, may not send all of it\n\
|
---|
113 | sendto(data[, flags], addr) -- send data to a given address\n\
|
---|
114 | setblocking(0 | 1) -- set or clear the blocking I/O flag\n\
|
---|
115 | setsockopt(level, optname, value) -- set socket options\n\
|
---|
116 | settimeout(None | float) -- set or clear the timeout\n\
|
---|
117 | shutdown(how) -- shut down traffic in one or both directions\n\
|
---|
118 | \n\
|
---|
119 | [*] not available on all platforms!");
|
---|
120 |
|
---|
121 | /* XXX This is a terrible mess of platform-dependent preprocessor hacks.
|
---|
122 | I hope some day someone can clean this up please... */
|
---|
123 |
|
---|
124 | /* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
|
---|
125 | script doesn't get this right, so we hardcode some platform checks below.
|
---|
126 | On the other hand, not all Linux versions agree, so there the settings
|
---|
127 | computed by the configure script are needed! */
|
---|
128 |
|
---|
129 | #ifndef linux
|
---|
130 | # undef HAVE_GETHOSTBYNAME_R_3_ARG
|
---|
131 | # undef HAVE_GETHOSTBYNAME_R_5_ARG
|
---|
132 | # undef HAVE_GETHOSTBYNAME_R_6_ARG
|
---|
133 | #endif
|
---|
134 |
|
---|
135 | #ifndef WITH_THREAD
|
---|
136 | # undef HAVE_GETHOSTBYNAME_R
|
---|
137 | #endif
|
---|
138 |
|
---|
139 | #ifdef HAVE_GETHOSTBYNAME_R
|
---|
140 | # if defined(_AIX) || defined(__osf__)
|
---|
141 | # define HAVE_GETHOSTBYNAME_R_3_ARG
|
---|
142 | # elif defined(__sun) || defined(__sgi)
|
---|
143 | # define HAVE_GETHOSTBYNAME_R_5_ARG
|
---|
144 | # elif defined(linux)
|
---|
145 | /* Rely on the configure script */
|
---|
146 | # else
|
---|
147 | # undef HAVE_GETHOSTBYNAME_R
|
---|
148 | # endif
|
---|
149 | #endif
|
---|
150 |
|
---|
151 | #if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \
|
---|
152 | !defined(MS_WINDOWS)
|
---|
153 | # define USE_GETHOSTBYNAME_LOCK
|
---|
154 | #endif
|
---|
155 |
|
---|
156 | /* To use __FreeBSD_version */
|
---|
157 | #ifdef HAVE_SYS_PARAM_H
|
---|
158 | #include <sys/param.h>
|
---|
159 | #endif
|
---|
160 | /* On systems on which getaddrinfo() is believed to not be thread-safe,
|
---|
161 | (this includes the getaddrinfo emulation) protect access with a lock. */
|
---|
162 | #if defined(WITH_THREAD) && (defined(__APPLE__) || \
|
---|
163 | (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \
|
---|
164 | defined(__OpenBSD__) || defined(__NetBSD__) || \
|
---|
165 | defined(__VMS) || !defined(HAVE_GETADDRINFO))
|
---|
166 | #define USE_GETADDRINFO_LOCK
|
---|
167 | #endif
|
---|
168 |
|
---|
169 | #ifdef USE_GETADDRINFO_LOCK
|
---|
170 | #define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1);
|
---|
171 | #define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock);
|
---|
172 | #else
|
---|
173 | #define ACQUIRE_GETADDRINFO_LOCK
|
---|
174 | #define RELEASE_GETADDRINFO_LOCK
|
---|
175 | #endif
|
---|
176 |
|
---|
177 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
|
---|
178 | # include "pythread.h"
|
---|
179 | #endif
|
---|
180 |
|
---|
181 | #if defined(PYCC_VACPP)
|
---|
182 | # include <types.h>
|
---|
183 | # include <io.h>
|
---|
184 | # include <sys/ioctl.h>
|
---|
185 | # include <utils.h>
|
---|
186 | # include <ctype.h>
|
---|
187 | #endif
|
---|
188 |
|
---|
189 | #if defined(__VMS)
|
---|
190 | # include <ioctl.h>
|
---|
191 | #endif
|
---|
192 |
|
---|
193 | #if defined(PYOS_OS2)
|
---|
194 | # define INCL_DOS
|
---|
195 | # define INCL_DOSERRORS
|
---|
196 | # define INCL_NOPMAPI
|
---|
197 | # include <os2.h>
|
---|
198 | #endif
|
---|
199 |
|
---|
200 | #if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI
|
---|
201 | /* make sure that the reentrant (gethostbyaddr_r etc)
|
---|
202 | functions are declared correctly if compiling with
|
---|
203 | MIPSPro 7.x in ANSI C mode (default) */
|
---|
204 |
|
---|
205 | /* XXX Using _SGIAPI is the wrong thing,
|
---|
206 | but I don't know what the right thing is. */
|
---|
207 | #undef _SGIAPI /* to avoid warning */
|
---|
208 | #define _SGIAPI 1
|
---|
209 |
|
---|
210 | #undef _XOPEN_SOURCE
|
---|
211 | #include <sys/socket.h>
|
---|
212 | #include <sys/types.h>
|
---|
213 | #include <netinet/in.h>
|
---|
214 | #ifdef _SS_ALIGNSIZE
|
---|
215 | #define HAVE_GETADDRINFO 1
|
---|
216 | #define HAVE_GETNAMEINFO 1
|
---|
217 | #endif
|
---|
218 |
|
---|
219 | #define HAVE_INET_PTON
|
---|
220 | #include <netdb.h>
|
---|
221 | #endif
|
---|
222 |
|
---|
223 | /* Irix 6.5 fails to define this variable at all. This is needed
|
---|
224 | for both GCC and SGI's compiler. I'd say that the SGI headers
|
---|
225 | are just busted. Same thing for Solaris. */
|
---|
226 | #if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN)
|
---|
227 | #define INET_ADDRSTRLEN 16
|
---|
228 | #endif
|
---|
229 |
|
---|
230 | /* Generic includes */
|
---|
231 | #ifdef HAVE_SYS_TYPES_H
|
---|
232 | #include <sys/types.h>
|
---|
233 | #endif
|
---|
234 |
|
---|
235 | /* Generic socket object definitions and includes */
|
---|
236 | #define PySocket_BUILDING_SOCKET
|
---|
237 | #include "socketmodule.h"
|
---|
238 |
|
---|
239 | /* Addressing includes */
|
---|
240 |
|
---|
241 | #ifndef MS_WINDOWS
|
---|
242 |
|
---|
243 | /* Non-MS WINDOWS includes */
|
---|
244 | # include <netdb.h>
|
---|
245 |
|
---|
246 | /* Headers needed for inet_ntoa() and inet_addr() */
|
---|
247 | # ifdef __BEOS__
|
---|
248 | # include <net/netdb.h>
|
---|
249 | # elif defined(PYOS_OS2) && defined(PYCC_VACPP)
|
---|
250 | # include <netdb.h>
|
---|
251 | typedef size_t socklen_t;
|
---|
252 | # else
|
---|
253 | # include <arpa/inet.h>
|
---|
254 | # endif
|
---|
255 |
|
---|
256 | # ifndef RISCOS
|
---|
257 | # include <fcntl.h>
|
---|
258 | # else
|
---|
259 | # include <sys/ioctl.h>
|
---|
260 | # include <socklib.h>
|
---|
261 | # define NO_DUP
|
---|
262 | int h_errno; /* not used */
|
---|
263 | # define INET_ADDRSTRLEN 16
|
---|
264 | # endif
|
---|
265 |
|
---|
266 | #else
|
---|
267 |
|
---|
268 | /* MS_WINDOWS includes */
|
---|
269 | # ifdef HAVE_FCNTL_H
|
---|
270 | # include <fcntl.h>
|
---|
271 | # endif
|
---|
272 |
|
---|
273 | #endif
|
---|
274 |
|
---|
275 | #include <stddef.h>
|
---|
276 |
|
---|
277 | #ifndef offsetof
|
---|
278 | # define offsetof(type, member) ((size_t)(&((type *)0)->member))
|
---|
279 | #endif
|
---|
280 |
|
---|
281 | #ifndef O_NONBLOCK
|
---|
282 | # define O_NONBLOCK O_NDELAY
|
---|
283 | #endif
|
---|
284 |
|
---|
285 | /* include Python's addrinfo.h unless it causes trouble */
|
---|
286 | #if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE)
|
---|
287 | /* Do not include addinfo.h on some newer IRIX versions.
|
---|
288 | * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21,
|
---|
289 | * for example, but not by 6.5.10.
|
---|
290 | */
|
---|
291 | #elif defined(_MSC_VER) && _MSC_VER>1201
|
---|
292 | /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and
|
---|
293 | * EAI_* constants are defined in (the already included) ws2tcpip.h.
|
---|
294 | */
|
---|
295 | #else
|
---|
296 | # include "addrinfo.h"
|
---|
297 | #endif
|
---|
298 |
|
---|
299 | #ifndef HAVE_INET_PTON
|
---|
300 | int inet_pton(int af, const char *src, void *dst);
|
---|
301 | const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
|
---|
302 | #endif
|
---|
303 |
|
---|
304 | #ifdef __APPLE__
|
---|
305 | /* On OS X, getaddrinfo returns no error indication of lookup
|
---|
306 | failure, so we must use the emulation instead of the libinfo
|
---|
307 | implementation. Unfortunately, performing an autoconf test
|
---|
308 | for this bug would require DNS access for the machine performing
|
---|
309 | the configuration, which is not acceptable. Therefore, we
|
---|
310 | determine the bug just by checking for __APPLE__. If this bug
|
---|
311 | gets ever fixed, perhaps checking for sys/version.h would be
|
---|
312 | appropriate, which is 10/0 on the system with the bug. */
|
---|
313 | #ifndef HAVE_GETNAMEINFO
|
---|
314 | /* This bug seems to be fixed in Jaguar. Ths easiest way I could
|
---|
315 | Find to check for Jaguar is that it has getnameinfo(), which
|
---|
316 | older releases don't have */
|
---|
317 | #undef HAVE_GETADDRINFO
|
---|
318 | #endif
|
---|
319 |
|
---|
320 | #ifdef HAVE_INET_ATON
|
---|
321 | #define USE_INET_ATON_WEAKLINK
|
---|
322 | #endif
|
---|
323 |
|
---|
324 | #endif
|
---|
325 |
|
---|
326 | /* I know this is a bad practice, but it is the easiest... */
|
---|
327 | #if !defined(HAVE_GETADDRINFO)
|
---|
328 | /* avoid clashes with the C library definition of the symbol. */
|
---|
329 | #define getaddrinfo fake_getaddrinfo
|
---|
330 | #define gai_strerror fake_gai_strerror
|
---|
331 | #define freeaddrinfo fake_freeaddrinfo
|
---|
332 | #include "getaddrinfo.c"
|
---|
333 | #endif
|
---|
334 | #if !defined(HAVE_GETNAMEINFO)
|
---|
335 | #define getnameinfo fake_getnameinfo
|
---|
336 | #include "getnameinfo.c"
|
---|
337 | #endif
|
---|
338 |
|
---|
339 | #if defined(MS_WINDOWS) || defined(__BEOS__)
|
---|
340 | /* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */
|
---|
341 | /* seem to be a few differences in the API */
|
---|
342 | #define SOCKETCLOSE closesocket
|
---|
343 | #define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
|
---|
344 | #endif
|
---|
345 |
|
---|
346 | #ifdef MS_WIN32
|
---|
347 | #define EAFNOSUPPORT WSAEAFNOSUPPORT
|
---|
348 | #define snprintf _snprintf
|
---|
349 | #endif
|
---|
350 |
|
---|
351 | #if defined(PYOS_OS2) && !defined(PYCC_GCC)
|
---|
352 | #define SOCKETCLOSE soclose
|
---|
353 | #define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
|
---|
354 | #endif
|
---|
355 |
|
---|
356 | #ifndef SOCKETCLOSE
|
---|
357 | #define SOCKETCLOSE close
|
---|
358 | #endif
|
---|
359 |
|
---|
360 | #if defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)
|
---|
361 | #define USE_BLUETOOTH 1
|
---|
362 | #if defined(__FreeBSD__)
|
---|
363 | #define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP
|
---|
364 | #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM
|
---|
365 | #define sockaddr_l2 sockaddr_l2cap
|
---|
366 | #define sockaddr_rc sockaddr_rfcomm
|
---|
367 | #define _BT_SOCKADDR_MEMB(s, proto) &((s)->sock_addr)
|
---|
368 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb)
|
---|
369 | #define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb)
|
---|
370 | #elif defined(__NetBSD__)
|
---|
371 | #define sockaddr_l2 sockaddr_bt
|
---|
372 | #define sockaddr_rc sockaddr_bt
|
---|
373 | #define sockaddr_sco sockaddr_bt
|
---|
374 | #define _BT_SOCKADDR_MEMB(s, proto) &((s)->sock_addr)
|
---|
375 | #define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb)
|
---|
376 | #define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb)
|
---|
377 | #define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb)
|
---|
378 | #else
|
---|
379 | #define _BT_SOCKADDR_MEMB(s, proto) (&((s)->sock_addr).bt_##proto)
|
---|
380 | #define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)
|
---|
381 | #define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)
|
---|
382 | #define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)
|
---|
383 | #endif
|
---|
384 | #endif
|
---|
385 |
|
---|
386 | #ifdef __VMS
|
---|
387 | /* TCP/IP Services for VMS uses a maximum send/recv buffer length */
|
---|
388 | #define SEGMENT_SIZE (32 * 1024 -1)
|
---|
389 | #endif
|
---|
390 |
|
---|
391 | /*
|
---|
392 | * Constants for getnameinfo()
|
---|
393 | */
|
---|
394 | #if !defined(NI_MAXHOST)
|
---|
395 | #define NI_MAXHOST 1025
|
---|
396 | #endif
|
---|
397 | #if !defined(NI_MAXSERV)
|
---|
398 | #define NI_MAXSERV 32
|
---|
399 | #endif
|
---|
400 |
|
---|
401 | /* XXX There's a problem here: *static* functions are not supposed to have
|
---|
402 | a Py prefix (or use CapitalizedWords). Later... */
|
---|
403 |
|
---|
404 | /* Global variable holding the exception type for errors detected
|
---|
405 | by this module (but not argument type or memory errors, etc.). */
|
---|
406 | static PyObject *socket_error;
|
---|
407 | static PyObject *socket_herror;
|
---|
408 | static PyObject *socket_gaierror;
|
---|
409 | static PyObject *socket_timeout;
|
---|
410 |
|
---|
411 | #ifdef RISCOS
|
---|
412 | /* Global variable which is !=0 if Python is running in a RISC OS taskwindow */
|
---|
413 | static int taskwindow;
|
---|
414 | #endif
|
---|
415 |
|
---|
416 | /* A forward reference to the socket type object.
|
---|
417 | The sock_type variable contains pointers to various functions,
|
---|
418 | some of which call new_sockobject(), which uses sock_type, so
|
---|
419 | there has to be a circular reference. */
|
---|
420 | static PyTypeObject sock_type;
|
---|
421 |
|
---|
422 | #if defined(HAVE_POLL_H)
|
---|
423 | #include <poll.h>
|
---|
424 | #elif defined(HAVE_SYS_POLL_H)
|
---|
425 | #include <sys/poll.h>
|
---|
426 | #endif
|
---|
427 |
|
---|
428 | #ifdef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE
|
---|
429 | /* Platform can select file descriptors beyond FD_SETSIZE */
|
---|
430 | #define IS_SELECTABLE(s) 1
|
---|
431 | #elif defined(HAVE_POLL)
|
---|
432 | /* Instead of select(), we'll use poll() since poll() works on any fd. */
|
---|
433 | #define IS_SELECTABLE(s) 1
|
---|
434 | /* Can we call select() with this socket without a buffer overrun? */
|
---|
435 | #else
|
---|
436 | /* POSIX says selecting file descriptors beyond FD_SETSIZE
|
---|
437 | has undefined behaviour. If there's no timeout left, we don't have to
|
---|
438 | call select, so it's a safe, little white lie. */
|
---|
439 | #define IS_SELECTABLE(s) ((s)->sock_fd < FD_SETSIZE || s->sock_timeout <= 0.0)
|
---|
440 | #endif
|
---|
441 |
|
---|
442 | static PyObject*
|
---|
443 | select_error(void)
|
---|
444 | {
|
---|
445 | PyErr_SetString(socket_error, "unable to select on socket");
|
---|
446 | return NULL;
|
---|
447 | }
|
---|
448 |
|
---|
449 | /* Convenience function to raise an error according to errno
|
---|
450 | and return a NULL pointer from a function. */
|
---|
451 |
|
---|
452 | static PyObject *
|
---|
453 | set_error(void)
|
---|
454 | {
|
---|
455 | #ifdef MS_WINDOWS
|
---|
456 | int err_no = WSAGetLastError();
|
---|
457 | static struct {
|
---|
458 | int no;
|
---|
459 | const char *msg;
|
---|
460 | } *msgp, msgs[] = {
|
---|
461 | {WSAEINTR, "Interrupted system call"},
|
---|
462 | {WSAEBADF, "Bad file descriptor"},
|
---|
463 | {WSAEACCES, "Permission denied"},
|
---|
464 | {WSAEFAULT, "Bad address"},
|
---|
465 | {WSAEINVAL, "Invalid argument"},
|
---|
466 | {WSAEMFILE, "Too many open files"},
|
---|
467 | {WSAEWOULDBLOCK,
|
---|
468 | "The socket operation could not complete "
|
---|
469 | "without blocking"},
|
---|
470 | {WSAEINPROGRESS, "Operation now in progress"},
|
---|
471 | {WSAEALREADY, "Operation already in progress"},
|
---|
472 | {WSAENOTSOCK, "Socket operation on non-socket"},
|
---|
473 | {WSAEDESTADDRREQ, "Destination address required"},
|
---|
474 | {WSAEMSGSIZE, "Message too long"},
|
---|
475 | {WSAEPROTOTYPE, "Protocol wrong type for socket"},
|
---|
476 | {WSAENOPROTOOPT, "Protocol not available"},
|
---|
477 | {WSAEPROTONOSUPPORT, "Protocol not supported"},
|
---|
478 | {WSAESOCKTNOSUPPORT, "Socket type not supported"},
|
---|
479 | {WSAEOPNOTSUPP, "Operation not supported"},
|
---|
480 | {WSAEPFNOSUPPORT, "Protocol family not supported"},
|
---|
481 | {WSAEAFNOSUPPORT, "Address family not supported"},
|
---|
482 | {WSAEADDRINUSE, "Address already in use"},
|
---|
483 | {WSAEADDRNOTAVAIL, "Can't assign requested address"},
|
---|
484 | {WSAENETDOWN, "Network is down"},
|
---|
485 | {WSAENETUNREACH, "Network is unreachable"},
|
---|
486 | {WSAENETRESET, "Network dropped connection on reset"},
|
---|
487 | {WSAECONNABORTED, "Software caused connection abort"},
|
---|
488 | {WSAECONNRESET, "Connection reset by peer"},
|
---|
489 | {WSAENOBUFS, "No buffer space available"},
|
---|
490 | {WSAEISCONN, "Socket is already connected"},
|
---|
491 | {WSAENOTCONN, "Socket is not connected"},
|
---|
492 | {WSAESHUTDOWN, "Can't send after socket shutdown"},
|
---|
493 | {WSAETOOMANYREFS, "Too many references: can't splice"},
|
---|
494 | {WSAETIMEDOUT, "Operation timed out"},
|
---|
495 | {WSAECONNREFUSED, "Connection refused"},
|
---|
496 | {WSAELOOP, "Too many levels of symbolic links"},
|
---|
497 | {WSAENAMETOOLONG, "File name too long"},
|
---|
498 | {WSAEHOSTDOWN, "Host is down"},
|
---|
499 | {WSAEHOSTUNREACH, "No route to host"},
|
---|
500 | {WSAENOTEMPTY, "Directory not empty"},
|
---|
501 | {WSAEPROCLIM, "Too many processes"},
|
---|
502 | {WSAEUSERS, "Too many users"},
|
---|
503 | {WSAEDQUOT, "Disc quota exceeded"},
|
---|
504 | {WSAESTALE, "Stale NFS file handle"},
|
---|
505 | {WSAEREMOTE, "Too many levels of remote in path"},
|
---|
506 | {WSASYSNOTREADY, "Network subsystem is unvailable"},
|
---|
507 | {WSAVERNOTSUPPORTED, "WinSock version is not supported"},
|
---|
508 | {WSANOTINITIALISED,
|
---|
509 | "Successful WSAStartup() not yet performed"},
|
---|
510 | {WSAEDISCON, "Graceful shutdown in progress"},
|
---|
511 | /* Resolver errors */
|
---|
512 | {WSAHOST_NOT_FOUND, "No such host is known"},
|
---|
513 | {WSATRY_AGAIN, "Host not found, or server failed"},
|
---|
514 | {WSANO_RECOVERY, "Unexpected server error encountered"},
|
---|
515 | {WSANO_DATA, "Valid name without requested data"},
|
---|
516 | {WSANO_ADDRESS, "No address, look for MX record"},
|
---|
517 | {0, NULL}
|
---|
518 | };
|
---|
519 | if (err_no) {
|
---|
520 | PyObject *v;
|
---|
521 | const char *msg = "winsock error";
|
---|
522 |
|
---|
523 | for (msgp = msgs; msgp->msg; msgp++) {
|
---|
524 | if (err_no == msgp->no) {
|
---|
525 | msg = msgp->msg;
|
---|
526 | break;
|
---|
527 | }
|
---|
528 | }
|
---|
529 |
|
---|
530 | v = Py_BuildValue("(is)", err_no, msg);
|
---|
531 | if (v != NULL) {
|
---|
532 | PyErr_SetObject(socket_error, v);
|
---|
533 | Py_DECREF(v);
|
---|
534 | }
|
---|
535 | return NULL;
|
---|
536 | }
|
---|
537 | else
|
---|
538 | #endif
|
---|
539 |
|
---|
540 | #if defined(PYOS_OS2) && !defined(PYCC_GCC)
|
---|
541 | if (sock_errno() != NO_ERROR) {
|
---|
542 | APIRET rc;
|
---|
543 | ULONG msglen;
|
---|
544 | char outbuf[100];
|
---|
545 | int myerrorcode = sock_errno();
|
---|
546 |
|
---|
547 | /* Retrieve socket-related error message from MPTN.MSG file */
|
---|
548 | rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
|
---|
549 | myerrorcode - SOCBASEERR + 26,
|
---|
550 | "mptn.msg",
|
---|
551 | &msglen);
|
---|
552 | if (rc == NO_ERROR) {
|
---|
553 | PyObject *v;
|
---|
554 |
|
---|
555 | /* OS/2 doesn't guarantee a terminator */
|
---|
556 | outbuf[msglen] = '\0';
|
---|
557 | if (strlen(outbuf) > 0) {
|
---|
558 | /* If non-empty msg, trim CRLF */
|
---|
559 | char *lastc = &outbuf[ strlen(outbuf)-1 ];
|
---|
560 | while (lastc > outbuf &&
|
---|
561 | isspace(Py_CHARMASK(*lastc))) {
|
---|
562 | /* Trim trailing whitespace (CRLF) */
|
---|
563 | *lastc-- = '\0';
|
---|
564 | }
|
---|
565 | }
|
---|
566 | v = Py_BuildValue("(is)", myerrorcode, outbuf);
|
---|
567 | if (v != NULL) {
|
---|
568 | PyErr_SetObject(socket_error, v);
|
---|
569 | Py_DECREF(v);
|
---|
570 | }
|
---|
571 | return NULL;
|
---|
572 | }
|
---|
573 | }
|
---|
574 | #endif
|
---|
575 |
|
---|
576 | #if defined(RISCOS)
|
---|
577 | if (_inet_error.errnum != NULL) {
|
---|
578 | PyObject *v;
|
---|
579 | v = Py_BuildValue("(is)", errno, _inet_err());
|
---|
580 | if (v != NULL) {
|
---|
581 | PyErr_SetObject(socket_error, v);
|
---|
582 | Py_DECREF(v);
|
---|
583 | }
|
---|
584 | return NULL;
|
---|
585 | }
|
---|
586 | #endif
|
---|
587 |
|
---|
588 | return PyErr_SetFromErrno(socket_error);
|
---|
589 | }
|
---|
590 |
|
---|
591 |
|
---|
592 | static PyObject *
|
---|
593 | set_herror(int h_error)
|
---|
594 | {
|
---|
595 | PyObject *v;
|
---|
596 |
|
---|
597 | #ifdef HAVE_HSTRERROR
|
---|
598 | v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
|
---|
599 | #else
|
---|
600 | v = Py_BuildValue("(is)", h_error, "host not found");
|
---|
601 | #endif
|
---|
602 | if (v != NULL) {
|
---|
603 | PyErr_SetObject(socket_herror, v);
|
---|
604 | Py_DECREF(v);
|
---|
605 | }
|
---|
606 |
|
---|
607 | return NULL;
|
---|
608 | }
|
---|
609 |
|
---|
610 |
|
---|
611 | static PyObject *
|
---|
612 | set_gaierror(int error)
|
---|
613 | {
|
---|
614 | PyObject *v;
|
---|
615 |
|
---|
616 | #ifdef EAI_SYSTEM
|
---|
617 | /* EAI_SYSTEM is not available on Windows XP. */
|
---|
618 | if (error == EAI_SYSTEM)
|
---|
619 | return set_error();
|
---|
620 | #endif
|
---|
621 |
|
---|
622 | #ifdef HAVE_GAI_STRERROR
|
---|
623 | v = Py_BuildValue("(is)", error, gai_strerror(error));
|
---|
624 | #else
|
---|
625 | v = Py_BuildValue("(is)", error, "getaddrinfo failed");
|
---|
626 | #endif
|
---|
627 | if (v != NULL) {
|
---|
628 | PyErr_SetObject(socket_gaierror, v);
|
---|
629 | Py_DECREF(v);
|
---|
630 | }
|
---|
631 |
|
---|
632 | return NULL;
|
---|
633 | }
|
---|
634 |
|
---|
635 | #ifdef __VMS
|
---|
636 | /* Function to send in segments */
|
---|
637 | static int
|
---|
638 | sendsegmented(int sock_fd, char *buf, int len, int flags)
|
---|
639 | {
|
---|
640 | int n = 0;
|
---|
641 | int remaining = len;
|
---|
642 |
|
---|
643 | while (remaining > 0) {
|
---|
644 | unsigned int segment;
|
---|
645 |
|
---|
646 | segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining);
|
---|
647 | n = send(sock_fd, buf, segment, flags);
|
---|
648 | if (n < 0) {
|
---|
649 | return n;
|
---|
650 | }
|
---|
651 | remaining -= segment;
|
---|
652 | buf += segment;
|
---|
653 | } /* end while */
|
---|
654 |
|
---|
655 | return len;
|
---|
656 | }
|
---|
657 | #endif
|
---|
658 |
|
---|
659 | /* Function to perform the setting of socket blocking mode
|
---|
660 | internally. block = (1 | 0). */
|
---|
661 | static int
|
---|
662 | internal_setblocking(PySocketSockObject *s, int block)
|
---|
663 | {
|
---|
664 | #ifndef RISCOS
|
---|
665 | #ifndef MS_WINDOWS
|
---|
666 | int delay_flag;
|
---|
667 | #endif
|
---|
668 | #endif
|
---|
669 |
|
---|
670 | Py_BEGIN_ALLOW_THREADS
|
---|
671 | #ifdef __BEOS__
|
---|
672 | block = !block;
|
---|
673 | setsockopt(s->sock_fd, SOL_SOCKET, SO_NONBLOCK,
|
---|
674 | (void *)(&block), sizeof(int));
|
---|
675 | #else
|
---|
676 | #ifndef RISCOS
|
---|
677 | #ifndef MS_WINDOWS
|
---|
678 | #if defined(PYOS_OS2) && !defined(PYCC_GCC)
|
---|
679 | block = !block;
|
---|
680 | ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
|
---|
681 | #elif defined(__VMS)
|
---|
682 | block = !block;
|
---|
683 | ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);
|
---|
684 | #else /* !PYOS_OS2 && !__VMS */
|
---|
685 | delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
|
---|
686 | if (block)
|
---|
687 | delay_flag &= (~O_NONBLOCK);
|
---|
688 | else
|
---|
689 | delay_flag |= O_NONBLOCK;
|
---|
690 | fcntl(s->sock_fd, F_SETFL, delay_flag);
|
---|
691 | #endif /* !PYOS_OS2 */
|
---|
692 | #else /* MS_WINDOWS */
|
---|
693 | block = !block;
|
---|
694 | ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
|
---|
695 | #endif /* MS_WINDOWS */
|
---|
696 | #else /* RISCOS */
|
---|
697 | block = !block;
|
---|
698 | socketioctl(s->sock_fd, FIONBIO, (u_long*)&block);
|
---|
699 | #endif /* RISCOS */
|
---|
700 | #endif /* __BEOS__ */
|
---|
701 | Py_END_ALLOW_THREADS
|
---|
702 |
|
---|
703 | /* Since these don't return anything */
|
---|
704 | return 1;
|
---|
705 | }
|
---|
706 |
|
---|
707 | /* Do a select()/poll() on the socket, if necessary (sock_timeout > 0).
|
---|
708 | The argument writing indicates the direction.
|
---|
709 | This does not raise an exception; we'll let our caller do that
|
---|
710 | after they've reacquired the interpreter lock.
|
---|
711 | Returns 1 on timeout, -1 on error, 0 otherwise. */
|
---|
712 | static int
|
---|
713 | internal_select(PySocketSockObject *s, int writing)
|
---|
714 | {
|
---|
715 | int n;
|
---|
716 |
|
---|
717 | /* Nothing to do unless we're in timeout mode (not non-blocking) */
|
---|
718 | if (s->sock_timeout <= 0.0)
|
---|
719 | return 0;
|
---|
720 |
|
---|
721 | /* Guard against closed socket */
|
---|
722 | if (s->sock_fd < 0)
|
---|
723 | return 0;
|
---|
724 |
|
---|
725 | /* Prefer poll, if available, since you can poll() any fd
|
---|
726 | * which can't be done with select(). */
|
---|
727 | #ifdef HAVE_POLL
|
---|
728 | {
|
---|
729 | struct pollfd pollfd;
|
---|
730 | int timeout;
|
---|
731 |
|
---|
732 | pollfd.fd = s->sock_fd;
|
---|
733 | pollfd.events = writing ? POLLOUT : POLLIN;
|
---|
734 |
|
---|
735 | /* s->sock_timeout is in seconds, timeout in ms */
|
---|
736 | timeout = (int)(s->sock_timeout * 1000 + 0.5);
|
---|
737 | n = poll(&pollfd, 1, timeout);
|
---|
738 | }
|
---|
739 | #else
|
---|
740 | {
|
---|
741 | /* Construct the arguments to select */
|
---|
742 | fd_set fds;
|
---|
743 | struct timeval tv;
|
---|
744 | tv.tv_sec = (int)s->sock_timeout;
|
---|
745 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
|
---|
746 | FD_ZERO(&fds);
|
---|
747 | FD_SET(s->sock_fd, &fds);
|
---|
748 |
|
---|
749 | /* See if the socket is ready */
|
---|
750 | if (writing)
|
---|
751 | n = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
|
---|
752 | else
|
---|
753 | n = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
|
---|
754 | }
|
---|
755 | #endif
|
---|
756 |
|
---|
757 | if (n < 0)
|
---|
758 | return -1;
|
---|
759 | if (n == 0)
|
---|
760 | return 1;
|
---|
761 | return 0;
|
---|
762 | }
|
---|
763 |
|
---|
764 | /* Initialize a new socket object. */
|
---|
765 |
|
---|
766 | static double defaulttimeout = -1.0; /* Default timeout for new sockets */
|
---|
767 |
|
---|
768 | PyMODINIT_FUNC
|
---|
769 | init_sockobject(PySocketSockObject *s,
|
---|
770 | SOCKET_T fd, int family, int type, int proto)
|
---|
771 | {
|
---|
772 | #ifdef RISCOS
|
---|
773 | int block = 1;
|
---|
774 | #endif
|
---|
775 | s->sock_fd = fd;
|
---|
776 | s->sock_family = family;
|
---|
777 | s->sock_type = type;
|
---|
778 | s->sock_proto = proto;
|
---|
779 | s->sock_timeout = defaulttimeout;
|
---|
780 |
|
---|
781 | s->errorhandler = &set_error;
|
---|
782 |
|
---|
783 | if (defaulttimeout >= 0.0)
|
---|
784 | internal_setblocking(s, 0);
|
---|
785 |
|
---|
786 | #ifdef RISCOS
|
---|
787 | if (taskwindow)
|
---|
788 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
|
---|
789 | #endif
|
---|
790 | }
|
---|
791 |
|
---|
792 |
|
---|
793 | /* Create a new socket object.
|
---|
794 | This just creates the object and initializes it.
|
---|
795 | If the creation fails, return NULL and set an exception (implicit
|
---|
796 | in NEWOBJ()). */
|
---|
797 |
|
---|
798 | static PySocketSockObject *
|
---|
799 | new_sockobject(SOCKET_T fd, int family, int type, int proto)
|
---|
800 | {
|
---|
801 | PySocketSockObject *s;
|
---|
802 | s = (PySocketSockObject *)
|
---|
803 | PyType_GenericNew(&sock_type, NULL, NULL);
|
---|
804 | if (s != NULL)
|
---|
805 | init_sockobject(s, fd, family, type, proto);
|
---|
806 | return s;
|
---|
807 | }
|
---|
808 |
|
---|
809 |
|
---|
810 | /* Lock to allow python interpreter to continue, but only allow one
|
---|
811 | thread to be in gethostbyname or getaddrinfo */
|
---|
812 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
|
---|
813 | PyThread_type_lock netdb_lock;
|
---|
814 | #endif
|
---|
815 |
|
---|
816 |
|
---|
817 | /* Convert a string specifying a host name or one of a few symbolic
|
---|
818 | names to a numeric IP address. This usually calls gethostbyname()
|
---|
819 | to do the work; the names "" and "<broadcast>" are special.
|
---|
820 | Return the length (IPv4 should be 4 bytes), or negative if
|
---|
821 | an error occurred; then an exception is raised. */
|
---|
822 |
|
---|
823 | static int
|
---|
824 | setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
|
---|
825 | {
|
---|
826 | struct addrinfo hints, *res;
|
---|
827 | int error;
|
---|
828 | int d1, d2, d3, d4;
|
---|
829 | char ch;
|
---|
830 |
|
---|
831 | memset((void *) addr_ret, '\0', sizeof(*addr_ret));
|
---|
832 | if (name[0] == '\0') {
|
---|
833 | int siz;
|
---|
834 | memset(&hints, 0, sizeof(hints));
|
---|
835 | hints.ai_family = af;
|
---|
836 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/
|
---|
837 | hints.ai_flags = AI_PASSIVE;
|
---|
838 | Py_BEGIN_ALLOW_THREADS
|
---|
839 | ACQUIRE_GETADDRINFO_LOCK
|
---|
840 | error = getaddrinfo(NULL, "0", &hints, &res);
|
---|
841 | Py_END_ALLOW_THREADS
|
---|
842 | /* We assume that those thread-unsafe getaddrinfo() versions
|
---|
843 | *are* safe regarding their return value, ie. that a
|
---|
844 | subsequent call to getaddrinfo() does not destroy the
|
---|
845 | outcome of the first call. */
|
---|
846 | RELEASE_GETADDRINFO_LOCK
|
---|
847 | if (error) {
|
---|
848 | set_gaierror(error);
|
---|
849 | return -1;
|
---|
850 | }
|
---|
851 | switch (res->ai_family) {
|
---|
852 | case AF_INET:
|
---|
853 | siz = 4;
|
---|
854 | break;
|
---|
855 | #ifdef ENABLE_IPV6
|
---|
856 | case AF_INET6:
|
---|
857 | siz = 16;
|
---|
858 | break;
|
---|
859 | #endif
|
---|
860 | default:
|
---|
861 | freeaddrinfo(res);
|
---|
862 | PyErr_SetString(socket_error,
|
---|
863 | "unsupported address family");
|
---|
864 | return -1;
|
---|
865 | }
|
---|
866 | if (res->ai_next) {
|
---|
867 | freeaddrinfo(res);
|
---|
868 | PyErr_SetString(socket_error,
|
---|
869 | "wildcard resolved to multiple address");
|
---|
870 | return -1;
|
---|
871 | }
|
---|
872 | if (res->ai_addrlen < addr_ret_size)
|
---|
873 | addr_ret_size = res->ai_addrlen;
|
---|
874 | memcpy(addr_ret, res->ai_addr, addr_ret_size);
|
---|
875 | freeaddrinfo(res);
|
---|
876 | return siz;
|
---|
877 | }
|
---|
878 | if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
|
---|
879 | struct sockaddr_in *sin;
|
---|
880 | if (af != AF_INET && af != AF_UNSPEC) {
|
---|
881 | PyErr_SetString(socket_error,
|
---|
882 | "address family mismatched");
|
---|
883 | return -1;
|
---|
884 | }
|
---|
885 | sin = (struct sockaddr_in *)addr_ret;
|
---|
886 | memset((void *) sin, '\0', sizeof(*sin));
|
---|
887 | sin->sin_family = AF_INET;
|
---|
888 | #ifdef HAVE_SOCKADDR_SA_LEN
|
---|
889 | sin->sin_len = sizeof(*sin);
|
---|
890 | #endif
|
---|
891 | sin->sin_addr.s_addr = INADDR_BROADCAST;
|
---|
892 | return sizeof(sin->sin_addr);
|
---|
893 | }
|
---|
894 | if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
|
---|
895 | 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
|
---|
896 | 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
|
---|
897 | struct sockaddr_in *sin;
|
---|
898 | sin = (struct sockaddr_in *)addr_ret;
|
---|
899 | sin->sin_addr.s_addr = htonl(
|
---|
900 | ((long) d1 << 24) | ((long) d2 << 16) |
|
---|
901 | ((long) d3 << 8) | ((long) d4 << 0));
|
---|
902 | sin->sin_family = AF_INET;
|
---|
903 | #ifdef HAVE_SOCKADDR_SA_LEN
|
---|
904 | sin->sin_len = sizeof(*sin);
|
---|
905 | #endif
|
---|
906 | return 4;
|
---|
907 | }
|
---|
908 | memset(&hints, 0, sizeof(hints));
|
---|
909 | hints.ai_family = af;
|
---|
910 | Py_BEGIN_ALLOW_THREADS
|
---|
911 | ACQUIRE_GETADDRINFO_LOCK
|
---|
912 | error = getaddrinfo(name, NULL, &hints, &res);
|
---|
913 | #if defined(__digital__) && defined(__unix__)
|
---|
914 | if (error == EAI_NONAME && af == AF_UNSPEC) {
|
---|
915 | /* On Tru64 V5.1, numeric-to-addr conversion fails
|
---|
916 | if no address family is given. Assume IPv4 for now.*/
|
---|
917 | hints.ai_family = AF_INET;
|
---|
918 | error = getaddrinfo(name, NULL, &hints, &res);
|
---|
919 | }
|
---|
920 | #endif
|
---|
921 | Py_END_ALLOW_THREADS
|
---|
922 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
|
---|
923 | if (error) {
|
---|
924 | set_gaierror(error);
|
---|
925 | return -1;
|
---|
926 | }
|
---|
927 | if (res->ai_addrlen < addr_ret_size)
|
---|
928 | addr_ret_size = res->ai_addrlen;
|
---|
929 | memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
|
---|
930 | freeaddrinfo(res);
|
---|
931 | switch (addr_ret->sa_family) {
|
---|
932 | case AF_INET:
|
---|
933 | return 4;
|
---|
934 | #ifdef ENABLE_IPV6
|
---|
935 | case AF_INET6:
|
---|
936 | return 16;
|
---|
937 | #endif
|
---|
938 | default:
|
---|
939 | PyErr_SetString(socket_error, "unknown address family");
|
---|
940 | return -1;
|
---|
941 | }
|
---|
942 | }
|
---|
943 |
|
---|
944 |
|
---|
945 | /* Create a string object representing an IP address.
|
---|
946 | This is always a string of the form 'dd.dd.dd.dd' (with variable
|
---|
947 | size numbers). */
|
---|
948 |
|
---|
949 | static PyObject *
|
---|
950 | makeipaddr(struct sockaddr *addr, int addrlen)
|
---|
951 | {
|
---|
952 | char buf[NI_MAXHOST];
|
---|
953 | int error;
|
---|
954 |
|
---|
955 | error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
|
---|
956 | NI_NUMERICHOST);
|
---|
957 | if (error) {
|
---|
958 | set_gaierror(error);
|
---|
959 | return NULL;
|
---|
960 | }
|
---|
961 | return PyString_FromString(buf);
|
---|
962 | }
|
---|
963 |
|
---|
964 |
|
---|
965 | #ifdef USE_BLUETOOTH
|
---|
966 | /* Convert a string representation of a Bluetooth address into a numeric
|
---|
967 | address. Returns the length (6), or raises an exception and returns -1 if
|
---|
968 | an error occurred. */
|
---|
969 |
|
---|
970 | static int
|
---|
971 | setbdaddr(char *name, bdaddr_t *bdaddr)
|
---|
972 | {
|
---|
973 | unsigned int b0, b1, b2, b3, b4, b5;
|
---|
974 | char ch;
|
---|
975 | int n;
|
---|
976 |
|
---|
977 | n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
|
---|
978 | &b5, &b4, &b3, &b2, &b1, &b0, &ch);
|
---|
979 | if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
|
---|
980 | bdaddr->b[0] = b0;
|
---|
981 | bdaddr->b[1] = b1;
|
---|
982 | bdaddr->b[2] = b2;
|
---|
983 | bdaddr->b[3] = b3;
|
---|
984 | bdaddr->b[4] = b4;
|
---|
985 | bdaddr->b[5] = b5;
|
---|
986 | return 6;
|
---|
987 | } else {
|
---|
988 | PyErr_SetString(socket_error, "bad bluetooth address");
|
---|
989 | return -1;
|
---|
990 | }
|
---|
991 | }
|
---|
992 |
|
---|
993 | /* Create a string representation of the Bluetooth address. This is always a
|
---|
994 | string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
|
---|
995 | value (zero padded if necessary). */
|
---|
996 |
|
---|
997 | static PyObject *
|
---|
998 | makebdaddr(bdaddr_t *bdaddr)
|
---|
999 | {
|
---|
1000 | char buf[(6 * 2) + 5 + 1];
|
---|
1001 |
|
---|
1002 | sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
|
---|
1003 | bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
|
---|
1004 | bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
|
---|
1005 | return PyString_FromString(buf);
|
---|
1006 | }
|
---|
1007 | #endif
|
---|
1008 |
|
---|
1009 |
|
---|
1010 | /* Create an object representing the given socket address,
|
---|
1011 | suitable for passing it back to bind(), connect() etc.
|
---|
1012 | The family field of the sockaddr structure is inspected
|
---|
1013 | to determine what kind of address it really is. */
|
---|
1014 |
|
---|
1015 | /*ARGSUSED*/
|
---|
1016 | static PyObject *
|
---|
1017 | makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto)
|
---|
1018 | {
|
---|
1019 | if (addrlen == 0) {
|
---|
1020 | /* No address -- may be recvfrom() from known socket */
|
---|
1021 | Py_INCREF(Py_None);
|
---|
1022 | return Py_None;
|
---|
1023 | }
|
---|
1024 |
|
---|
1025 | #ifdef __BEOS__
|
---|
1026 | /* XXX: BeOS version of accept() doesn't set family correctly */
|
---|
1027 | addr->sa_family = AF_INET;
|
---|
1028 | #endif
|
---|
1029 |
|
---|
1030 | switch (addr->sa_family) {
|
---|
1031 |
|
---|
1032 | case AF_INET:
|
---|
1033 | {
|
---|
1034 | struct sockaddr_in *a;
|
---|
1035 | PyObject *addrobj = makeipaddr(addr, sizeof(*a));
|
---|
1036 | PyObject *ret = NULL;
|
---|
1037 | if (addrobj) {
|
---|
1038 | a = (struct sockaddr_in *)addr;
|
---|
1039 | ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
|
---|
1040 | Py_DECREF(addrobj);
|
---|
1041 | }
|
---|
1042 | return ret;
|
---|
1043 | }
|
---|
1044 |
|
---|
1045 | #if defined(AF_UNIX)
|
---|
1046 | case AF_UNIX:
|
---|
1047 | {
|
---|
1048 | struct sockaddr_un *a = (struct sockaddr_un *) addr;
|
---|
1049 | #ifdef linux
|
---|
1050 | if (a->sun_path[0] == 0) { /* Linux abstract namespace */
|
---|
1051 | addrlen -= (sizeof(*a) - sizeof(a->sun_path));
|
---|
1052 | return PyString_FromStringAndSize(a->sun_path,
|
---|
1053 | addrlen);
|
---|
1054 | }
|
---|
1055 | else
|
---|
1056 | #endif /* linux */
|
---|
1057 | {
|
---|
1058 | /* regular NULL-terminated string */
|
---|
1059 | return PyString_FromString(a->sun_path);
|
---|
1060 | }
|
---|
1061 | }
|
---|
1062 | #endif /* AF_UNIX */
|
---|
1063 |
|
---|
1064 | #if defined(AF_NETLINK)
|
---|
1065 | case AF_NETLINK:
|
---|
1066 | {
|
---|
1067 | struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
|
---|
1068 | return Py_BuildValue("II", a->nl_pid, a->nl_groups);
|
---|
1069 | }
|
---|
1070 | #endif /* AF_NETLINK */
|
---|
1071 |
|
---|
1072 | #ifdef ENABLE_IPV6
|
---|
1073 | case AF_INET6:
|
---|
1074 | {
|
---|
1075 | struct sockaddr_in6 *a;
|
---|
1076 | PyObject *addrobj = makeipaddr(addr, sizeof(*a));
|
---|
1077 | PyObject *ret = NULL;
|
---|
1078 | if (addrobj) {
|
---|
1079 | a = (struct sockaddr_in6 *)addr;
|
---|
1080 | ret = Py_BuildValue("Oiii",
|
---|
1081 | addrobj,
|
---|
1082 | ntohs(a->sin6_port),
|
---|
1083 | a->sin6_flowinfo,
|
---|
1084 | a->sin6_scope_id);
|
---|
1085 | Py_DECREF(addrobj);
|
---|
1086 | }
|
---|
1087 | return ret;
|
---|
1088 | }
|
---|
1089 | #endif
|
---|
1090 |
|
---|
1091 | #ifdef USE_BLUETOOTH
|
---|
1092 | case AF_BLUETOOTH:
|
---|
1093 | switch (proto) {
|
---|
1094 |
|
---|
1095 | case BTPROTO_L2CAP:
|
---|
1096 | {
|
---|
1097 | struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
|
---|
1098 | PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
|
---|
1099 | PyObject *ret = NULL;
|
---|
1100 | if (addrobj) {
|
---|
1101 | ret = Py_BuildValue("Oi",
|
---|
1102 | addrobj,
|
---|
1103 | _BT_L2_MEMB(a, psm));
|
---|
1104 | Py_DECREF(addrobj);
|
---|
1105 | }
|
---|
1106 | return ret;
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | case BTPROTO_RFCOMM:
|
---|
1110 | {
|
---|
1111 | struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
|
---|
1112 | PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
|
---|
1113 | PyObject *ret = NULL;
|
---|
1114 | if (addrobj) {
|
---|
1115 | ret = Py_BuildValue("Oi",
|
---|
1116 | addrobj,
|
---|
1117 | _BT_RC_MEMB(a, channel));
|
---|
1118 | Py_DECREF(addrobj);
|
---|
1119 | }
|
---|
1120 | return ret;
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 | #if !defined(__FreeBSD__)
|
---|
1124 | case BTPROTO_SCO:
|
---|
1125 | {
|
---|
1126 | struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
|
---|
1127 | return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
|
---|
1128 | }
|
---|
1129 | #endif
|
---|
1130 |
|
---|
1131 | }
|
---|
1132 | #endif
|
---|
1133 |
|
---|
1134 | #ifdef HAVE_NETPACKET_PACKET_H
|
---|
1135 | case AF_PACKET:
|
---|
1136 | {
|
---|
1137 | struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
|
---|
1138 | char *ifname = "";
|
---|
1139 | struct ifreq ifr;
|
---|
1140 | /* need to look up interface name give index */
|
---|
1141 | if (a->sll_ifindex) {
|
---|
1142 | ifr.ifr_ifindex = a->sll_ifindex;
|
---|
1143 | if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
|
---|
1144 | ifname = ifr.ifr_name;
|
---|
1145 | }
|
---|
1146 | return Py_BuildValue("shbhs#",
|
---|
1147 | ifname,
|
---|
1148 | ntohs(a->sll_protocol),
|
---|
1149 | a->sll_pkttype,
|
---|
1150 | a->sll_hatype,
|
---|
1151 | a->sll_addr,
|
---|
1152 | a->sll_halen);
|
---|
1153 | }
|
---|
1154 | #endif
|
---|
1155 |
|
---|
1156 | /* More cases here... */
|
---|
1157 |
|
---|
1158 | default:
|
---|
1159 | /* If we don't know the address family, don't raise an
|
---|
1160 | exception -- return it as a tuple. */
|
---|
1161 | return Py_BuildValue("is#",
|
---|
1162 | addr->sa_family,
|
---|
1163 | addr->sa_data,
|
---|
1164 | sizeof(addr->sa_data));
|
---|
1165 |
|
---|
1166 | }
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 |
|
---|
1170 | /* Parse a socket address argument according to the socket object's
|
---|
1171 | address family. Return 1 if the address was in the proper format,
|
---|
1172 | 0 of not. The address is returned through addr_ret, its length
|
---|
1173 | through len_ret. */
|
---|
1174 |
|
---|
1175 | static int
|
---|
1176 | getsockaddrarg(PySocketSockObject *s, PyObject *args,
|
---|
1177 | struct sockaddr **addr_ret, int *len_ret)
|
---|
1178 | {
|
---|
1179 | switch (s->sock_family) {
|
---|
1180 |
|
---|
1181 | #if defined(AF_UNIX)
|
---|
1182 | case AF_UNIX:
|
---|
1183 | {
|
---|
1184 | struct sockaddr_un* addr;
|
---|
1185 | char *path;
|
---|
1186 | int len;
|
---|
1187 | addr = (struct sockaddr_un*)&(s->sock_addr).un;
|
---|
1188 | if (!PyArg_Parse(args, "t#", &path, &len))
|
---|
1189 | return 0;
|
---|
1190 | #ifdef linux
|
---|
1191 | if (len > 0 && path[0] == 0) {
|
---|
1192 | /* Linux abstract namespace extension */
|
---|
1193 | if (len > sizeof addr->sun_path) {
|
---|
1194 | PyErr_SetString(socket_error,
|
---|
1195 | "AF_UNIX path too long");
|
---|
1196 | return 0;
|
---|
1197 | }
|
---|
1198 | }
|
---|
1199 | else
|
---|
1200 | #endif /* linux */
|
---|
1201 | {
|
---|
1202 | /* regular NULL-terminated string */
|
---|
1203 | if (len >= sizeof addr->sun_path) {
|
---|
1204 | PyErr_SetString(socket_error,
|
---|
1205 | "AF_UNIX path too long");
|
---|
1206 | return 0;
|
---|
1207 | }
|
---|
1208 | addr->sun_path[len] = 0;
|
---|
1209 | }
|
---|
1210 | addr->sun_family = s->sock_family;
|
---|
1211 | memcpy(addr->sun_path, path, len);
|
---|
1212 | *addr_ret = (struct sockaddr *) addr;
|
---|
1213 | #if defined(PYOS_OS2)
|
---|
1214 | *len_ret = sizeof(*addr);
|
---|
1215 | #else
|
---|
1216 | *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
|
---|
1217 | #endif
|
---|
1218 | return 1;
|
---|
1219 | }
|
---|
1220 | #endif /* AF_UNIX */
|
---|
1221 |
|
---|
1222 | #if defined(AF_NETLINK)
|
---|
1223 | case AF_NETLINK:
|
---|
1224 | {
|
---|
1225 | struct sockaddr_nl* addr;
|
---|
1226 | int pid, groups;
|
---|
1227 | addr = (struct sockaddr_nl *)&(s->sock_addr).nl;
|
---|
1228 | if (!PyTuple_Check(args)) {
|
---|
1229 | PyErr_Format(
|
---|
1230 | PyExc_TypeError,
|
---|
1231 | "getsockaddrarg: "
|
---|
1232 | "AF_NETLINK address must be tuple, not %.500s",
|
---|
1233 | args->ob_type->tp_name);
|
---|
1234 | return 0;
|
---|
1235 | }
|
---|
1236 | if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
|
---|
1237 | return 0;
|
---|
1238 | addr->nl_family = AF_NETLINK;
|
---|
1239 | addr->nl_pid = pid;
|
---|
1240 | addr->nl_groups = groups;
|
---|
1241 | *addr_ret = (struct sockaddr *) addr;
|
---|
1242 | *len_ret = sizeof(*addr);
|
---|
1243 | return 1;
|
---|
1244 | }
|
---|
1245 | #endif
|
---|
1246 |
|
---|
1247 | case AF_INET:
|
---|
1248 | {
|
---|
1249 | struct sockaddr_in* addr;
|
---|
1250 | char *host;
|
---|
1251 | int port, result;
|
---|
1252 | addr=(struct sockaddr_in*)&(s->sock_addr).in;
|
---|
1253 | if (!PyTuple_Check(args)) {
|
---|
1254 | PyErr_Format(
|
---|
1255 | PyExc_TypeError,
|
---|
1256 | "getsockaddrarg: "
|
---|
1257 | "AF_INET address must be tuple, not %.500s",
|
---|
1258 | args->ob_type->tp_name);
|
---|
1259 | return 0;
|
---|
1260 | }
|
---|
1261 | if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
|
---|
1262 | "idna", &host, &port))
|
---|
1263 | return 0;
|
---|
1264 | result = setipaddr(host, (struct sockaddr *)addr,
|
---|
1265 | sizeof(*addr), AF_INET);
|
---|
1266 | PyMem_Free(host);
|
---|
1267 | if (result < 0)
|
---|
1268 | return 0;
|
---|
1269 | addr->sin_family = AF_INET;
|
---|
1270 | addr->sin_port = htons((short)port);
|
---|
1271 | *addr_ret = (struct sockaddr *) addr;
|
---|
1272 | *len_ret = sizeof *addr;
|
---|
1273 | return 1;
|
---|
1274 | }
|
---|
1275 |
|
---|
1276 | #ifdef ENABLE_IPV6
|
---|
1277 | case AF_INET6:
|
---|
1278 | {
|
---|
1279 | struct sockaddr_in6* addr;
|
---|
1280 | char *host;
|
---|
1281 | int port, flowinfo, scope_id, result;
|
---|
1282 | addr = (struct sockaddr_in6*)&(s->sock_addr).in6;
|
---|
1283 | flowinfo = scope_id = 0;
|
---|
1284 | if (!PyTuple_Check(args)) {
|
---|
1285 | PyErr_Format(
|
---|
1286 | PyExc_TypeError,
|
---|
1287 | "getsockaddrarg: "
|
---|
1288 | "AF_INET6 address must be tuple, not %.500s",
|
---|
1289 | args->ob_type->tp_name);
|
---|
1290 | return 0;
|
---|
1291 | }
|
---|
1292 | if (!PyArg_ParseTuple(args, "eti|ii",
|
---|
1293 | "idna", &host, &port, &flowinfo,
|
---|
1294 | &scope_id)) {
|
---|
1295 | return 0;
|
---|
1296 | }
|
---|
1297 | result = setipaddr(host, (struct sockaddr *)addr,
|
---|
1298 | sizeof(*addr), AF_INET6);
|
---|
1299 | PyMem_Free(host);
|
---|
1300 | if (result < 0)
|
---|
1301 | return 0;
|
---|
1302 | addr->sin6_family = s->sock_family;
|
---|
1303 | addr->sin6_port = htons((short)port);
|
---|
1304 | addr->sin6_flowinfo = flowinfo;
|
---|
1305 | addr->sin6_scope_id = scope_id;
|
---|
1306 | *addr_ret = (struct sockaddr *) addr;
|
---|
1307 | *len_ret = sizeof *addr;
|
---|
1308 | return 1;
|
---|
1309 | }
|
---|
1310 | #endif
|
---|
1311 |
|
---|
1312 | #ifdef USE_BLUETOOTH
|
---|
1313 | case AF_BLUETOOTH:
|
---|
1314 | {
|
---|
1315 | switch (s->sock_proto) {
|
---|
1316 | case BTPROTO_L2CAP:
|
---|
1317 | {
|
---|
1318 | struct sockaddr_l2 *addr = (struct sockaddr_l2 *) _BT_SOCKADDR_MEMB(s, l2);
|
---|
1319 | char *straddr;
|
---|
1320 |
|
---|
1321 | _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
|
---|
1322 | if (!PyArg_ParseTuple(args, "si", &straddr,
|
---|
1323 | &_BT_L2_MEMB(addr, psm))) {
|
---|
1324 | PyErr_SetString(socket_error, "getsockaddrarg: "
|
---|
1325 | "wrong format");
|
---|
1326 | return 0;
|
---|
1327 | }
|
---|
1328 | if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
|
---|
1329 | return 0;
|
---|
1330 |
|
---|
1331 | *addr_ret = (struct sockaddr *) addr;
|
---|
1332 | *len_ret = sizeof *addr;
|
---|
1333 | return 1;
|
---|
1334 | }
|
---|
1335 | case BTPROTO_RFCOMM:
|
---|
1336 | {
|
---|
1337 | struct sockaddr_rc *addr = (struct sockaddr_rc *) _BT_SOCKADDR_MEMB(s, rc);
|
---|
1338 | char *straddr;
|
---|
1339 |
|
---|
1340 | _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
|
---|
1341 | if (!PyArg_ParseTuple(args, "si", &straddr,
|
---|
1342 | &_BT_RC_MEMB(addr, channel))) {
|
---|
1343 | PyErr_SetString(socket_error, "getsockaddrarg: "
|
---|
1344 | "wrong format");
|
---|
1345 | return 0;
|
---|
1346 | }
|
---|
1347 | if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
|
---|
1348 | return 0;
|
---|
1349 |
|
---|
1350 | *addr_ret = (struct sockaddr *) addr;
|
---|
1351 | *len_ret = sizeof *addr;
|
---|
1352 | return 1;
|
---|
1353 | }
|
---|
1354 | #if !defined(__FreeBSD__)
|
---|
1355 | case BTPROTO_SCO:
|
---|
1356 | {
|
---|
1357 | struct sockaddr_sco *addr = (struct sockaddr_sco *) _BT_SOCKADDR_MEMB(s, sco);
|
---|
1358 | char *straddr;
|
---|
1359 |
|
---|
1360 | _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
|
---|
1361 | straddr = PyString_AsString(args);
|
---|
1362 | if (straddr == NULL) {
|
---|
1363 | PyErr_SetString(socket_error, "getsockaddrarg: "
|
---|
1364 | "wrong format");
|
---|
1365 | return 0;
|
---|
1366 | }
|
---|
1367 | if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
|
---|
1368 | return 0;
|
---|
1369 |
|
---|
1370 | *addr_ret = (struct sockaddr *) addr;
|
---|
1371 | *len_ret = sizeof *addr;
|
---|
1372 | return 1;
|
---|
1373 | }
|
---|
1374 | #endif
|
---|
1375 | default:
|
---|
1376 | PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol");
|
---|
1377 | return 0;
|
---|
1378 | }
|
---|
1379 | }
|
---|
1380 | #endif
|
---|
1381 |
|
---|
1382 | #ifdef HAVE_NETPACKET_PACKET_H
|
---|
1383 | case AF_PACKET:
|
---|
1384 | {
|
---|
1385 | struct sockaddr_ll* addr;
|
---|
1386 | struct ifreq ifr;
|
---|
1387 | char *interfaceName;
|
---|
1388 | int protoNumber;
|
---|
1389 | int hatype = 0;
|
---|
1390 | int pkttype = 0;
|
---|
1391 | char *haddr = NULL;
|
---|
1392 | unsigned int halen = 0;
|
---|
1393 |
|
---|
1394 | if (!PyTuple_Check(args)) {
|
---|
1395 | PyErr_Format(
|
---|
1396 | PyExc_TypeError,
|
---|
1397 | "getsockaddrarg: "
|
---|
1398 | "AF_PACKET address must be tuple, not %.500s",
|
---|
1399 | args->ob_type->tp_name);
|
---|
1400 | return 0;
|
---|
1401 | }
|
---|
1402 | if (!PyArg_ParseTuple(args, "si|iis#", &interfaceName,
|
---|
1403 | &protoNumber, &pkttype, &hatype,
|
---|
1404 | &haddr, &halen))
|
---|
1405 | return 0;
|
---|
1406 | strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
|
---|
1407 | ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
|
---|
1408 | if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
|
---|
1409 | s->errorhandler();
|
---|
1410 | return 0;
|
---|
1411 | }
|
---|
1412 | addr = &(s->sock_addr.ll);
|
---|
1413 | addr->sll_family = AF_PACKET;
|
---|
1414 | addr->sll_protocol = htons((short)protoNumber);
|
---|
1415 | addr->sll_ifindex = ifr.ifr_ifindex;
|
---|
1416 | addr->sll_pkttype = pkttype;
|
---|
1417 | addr->sll_hatype = hatype;
|
---|
1418 | if (halen > 8) {
|
---|
1419 | PyErr_SetString(PyExc_ValueError,
|
---|
1420 | "Hardware address must be 8 bytes or less");
|
---|
1421 | return 0;
|
---|
1422 | }
|
---|
1423 | if (halen != 0) {
|
---|
1424 | memcpy(&addr->sll_addr, haddr, halen);
|
---|
1425 | }
|
---|
1426 | addr->sll_halen = halen;
|
---|
1427 | *addr_ret = (struct sockaddr *) addr;
|
---|
1428 | *len_ret = sizeof *addr;
|
---|
1429 | return 1;
|
---|
1430 | }
|
---|
1431 | #endif
|
---|
1432 |
|
---|
1433 | /* More cases here... */
|
---|
1434 |
|
---|
1435 | default:
|
---|
1436 | PyErr_SetString(socket_error, "getsockaddrarg: bad family");
|
---|
1437 | return 0;
|
---|
1438 |
|
---|
1439 | }
|
---|
1440 | }
|
---|
1441 |
|
---|
1442 |
|
---|
1443 | /* Get the address length according to the socket object's address family.
|
---|
1444 | Return 1 if the family is known, 0 otherwise. The length is returned
|
---|
1445 | through len_ret. */
|
---|
1446 |
|
---|
1447 | static int
|
---|
1448 | getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
|
---|
1449 | {
|
---|
1450 | switch (s->sock_family) {
|
---|
1451 |
|
---|
1452 | #if defined(AF_UNIX)
|
---|
1453 | case AF_UNIX:
|
---|
1454 | {
|
---|
1455 | *len_ret = sizeof (struct sockaddr_un);
|
---|
1456 | return 1;
|
---|
1457 | }
|
---|
1458 | #endif /* AF_UNIX */
|
---|
1459 | #if defined(AF_NETLINK)
|
---|
1460 | case AF_NETLINK:
|
---|
1461 | {
|
---|
1462 | *len_ret = sizeof (struct sockaddr_nl);
|
---|
1463 | return 1;
|
---|
1464 | }
|
---|
1465 | #endif
|
---|
1466 |
|
---|
1467 | case AF_INET:
|
---|
1468 | {
|
---|
1469 | *len_ret = sizeof (struct sockaddr_in);
|
---|
1470 | return 1;
|
---|
1471 | }
|
---|
1472 |
|
---|
1473 | #ifdef ENABLE_IPV6
|
---|
1474 | case AF_INET6:
|
---|
1475 | {
|
---|
1476 | *len_ret = sizeof (struct sockaddr_in6);
|
---|
1477 | return 1;
|
---|
1478 | }
|
---|
1479 | #endif
|
---|
1480 |
|
---|
1481 | #ifdef USE_BLUETOOTH
|
---|
1482 | case AF_BLUETOOTH:
|
---|
1483 | {
|
---|
1484 | switch(s->sock_proto)
|
---|
1485 | {
|
---|
1486 |
|
---|
1487 | case BTPROTO_L2CAP:
|
---|
1488 | *len_ret = sizeof (struct sockaddr_l2);
|
---|
1489 | return 1;
|
---|
1490 | case BTPROTO_RFCOMM:
|
---|
1491 | *len_ret = sizeof (struct sockaddr_rc);
|
---|
1492 | return 1;
|
---|
1493 | #if !defined(__FreeBSD__)
|
---|
1494 | case BTPROTO_SCO:
|
---|
1495 | *len_ret = sizeof (struct sockaddr_sco);
|
---|
1496 | return 1;
|
---|
1497 | #endif
|
---|
1498 | default:
|
---|
1499 | PyErr_SetString(socket_error, "getsockaddrlen: "
|
---|
1500 | "unknown BT protocol");
|
---|
1501 | return 0;
|
---|
1502 |
|
---|
1503 | }
|
---|
1504 | }
|
---|
1505 | #endif
|
---|
1506 |
|
---|
1507 | #ifdef HAVE_NETPACKET_PACKET_H
|
---|
1508 | case AF_PACKET:
|
---|
1509 | {
|
---|
1510 | *len_ret = sizeof (struct sockaddr_ll);
|
---|
1511 | return 1;
|
---|
1512 | }
|
---|
1513 | #endif
|
---|
1514 |
|
---|
1515 | /* More cases here... */
|
---|
1516 |
|
---|
1517 | default:
|
---|
1518 | PyErr_SetString(socket_error, "getsockaddrlen: bad family");
|
---|
1519 | return 0;
|
---|
1520 |
|
---|
1521 | }
|
---|
1522 | }
|
---|
1523 |
|
---|
1524 |
|
---|
1525 | /* s.accept() method */
|
---|
1526 |
|
---|
1527 | static PyObject *
|
---|
1528 | sock_accept(PySocketSockObject *s)
|
---|
1529 | {
|
---|
1530 | sock_addr_t addrbuf;
|
---|
1531 | SOCKET_T newfd;
|
---|
1532 | socklen_t addrlen;
|
---|
1533 | PyObject *sock = NULL;
|
---|
1534 | PyObject *addr = NULL;
|
---|
1535 | PyObject *res = NULL;
|
---|
1536 | int timeout;
|
---|
1537 |
|
---|
1538 | if (!getsockaddrlen(s, &addrlen))
|
---|
1539 | return NULL;
|
---|
1540 | memset(&addrbuf, 0, addrlen);
|
---|
1541 |
|
---|
1542 | #ifdef MS_WINDOWS
|
---|
1543 | newfd = INVALID_SOCKET;
|
---|
1544 | #else
|
---|
1545 | newfd = -1;
|
---|
1546 | #endif
|
---|
1547 |
|
---|
1548 | if (!IS_SELECTABLE(s))
|
---|
1549 | return select_error();
|
---|
1550 |
|
---|
1551 | Py_BEGIN_ALLOW_THREADS
|
---|
1552 | timeout = internal_select(s, 0);
|
---|
1553 | if (!timeout)
|
---|
1554 | newfd = accept(s->sock_fd, (struct sockaddr *) &addrbuf,
|
---|
1555 | &addrlen);
|
---|
1556 | Py_END_ALLOW_THREADS
|
---|
1557 |
|
---|
1558 | if (timeout == 1) {
|
---|
1559 | PyErr_SetString(socket_timeout, "timed out");
|
---|
1560 | return NULL;
|
---|
1561 | }
|
---|
1562 |
|
---|
1563 | #ifdef MS_WINDOWS
|
---|
1564 | if (newfd == INVALID_SOCKET)
|
---|
1565 | #else
|
---|
1566 | if (newfd < 0)
|
---|
1567 | #endif
|
---|
1568 | return s->errorhandler();
|
---|
1569 |
|
---|
1570 | /* Create the new object with unspecified family,
|
---|
1571 | to avoid calls to bind() etc. on it. */
|
---|
1572 | sock = (PyObject *) new_sockobject(newfd,
|
---|
1573 | s->sock_family,
|
---|
1574 | s->sock_type,
|
---|
1575 | s->sock_proto);
|
---|
1576 |
|
---|
1577 | if (sock == NULL) {
|
---|
1578 | SOCKETCLOSE(newfd);
|
---|
1579 | goto finally;
|
---|
1580 | }
|
---|
1581 | addr = makesockaddr(s->sock_fd, (struct sockaddr *) &addrbuf,
|
---|
1582 | addrlen, s->sock_proto);
|
---|
1583 | if (addr == NULL)
|
---|
1584 | goto finally;
|
---|
1585 |
|
---|
1586 | res = PyTuple_Pack(2, sock, addr);
|
---|
1587 |
|
---|
1588 | finally:
|
---|
1589 | Py_XDECREF(sock);
|
---|
1590 | Py_XDECREF(addr);
|
---|
1591 | return res;
|
---|
1592 | }
|
---|
1593 |
|
---|
1594 | PyDoc_STRVAR(accept_doc,
|
---|
1595 | "accept() -> (socket object, address info)\n\
|
---|
1596 | \n\
|
---|
1597 | Wait for an incoming connection. Return a new socket representing the\n\
|
---|
1598 | connection, and the address of the client. For IP sockets, the address\n\
|
---|
1599 | info is a pair (hostaddr, port).");
|
---|
1600 |
|
---|
1601 | /* s.setblocking(flag) method. Argument:
|
---|
1602 | False -- non-blocking mode; same as settimeout(0)
|
---|
1603 | True -- blocking mode; same as settimeout(None)
|
---|
1604 | */
|
---|
1605 |
|
---|
1606 | static PyObject *
|
---|
1607 | sock_setblocking(PySocketSockObject *s, PyObject *arg)
|
---|
1608 | {
|
---|
1609 | int block;
|
---|
1610 |
|
---|
1611 | block = PyInt_AsLong(arg);
|
---|
1612 | if (block == -1 && PyErr_Occurred())
|
---|
1613 | return NULL;
|
---|
1614 |
|
---|
1615 | s->sock_timeout = block ? -1.0 : 0.0;
|
---|
1616 | internal_setblocking(s, block);
|
---|
1617 |
|
---|
1618 | Py_INCREF(Py_None);
|
---|
1619 | return Py_None;
|
---|
1620 | }
|
---|
1621 |
|
---|
1622 | PyDoc_STRVAR(setblocking_doc,
|
---|
1623 | "setblocking(flag)\n\
|
---|
1624 | \n\
|
---|
1625 | Set the socket to blocking (flag is true) or non-blocking (false).\n\
|
---|
1626 | setblocking(True) is equivalent to settimeout(None);\n\
|
---|
1627 | setblocking(False) is equivalent to settimeout(0.0).");
|
---|
1628 |
|
---|
1629 | /* s.settimeout(timeout) method. Argument:
|
---|
1630 | None -- no timeout, blocking mode; same as setblocking(True)
|
---|
1631 | 0.0 -- non-blocking mode; same as setblocking(False)
|
---|
1632 | > 0 -- timeout mode; operations time out after timeout seconds
|
---|
1633 | < 0 -- illegal; raises an exception
|
---|
1634 | */
|
---|
1635 | static PyObject *
|
---|
1636 | sock_settimeout(PySocketSockObject *s, PyObject *arg)
|
---|
1637 | {
|
---|
1638 | double timeout;
|
---|
1639 |
|
---|
1640 | if (arg == Py_None)
|
---|
1641 | timeout = -1.0;
|
---|
1642 | else {
|
---|
1643 | timeout = PyFloat_AsDouble(arg);
|
---|
1644 | if (timeout < 0.0) {
|
---|
1645 | if (!PyErr_Occurred())
|
---|
1646 | PyErr_SetString(PyExc_ValueError,
|
---|
1647 | "Timeout value out of range");
|
---|
1648 | return NULL;
|
---|
1649 | }
|
---|
1650 | }
|
---|
1651 |
|
---|
1652 | s->sock_timeout = timeout;
|
---|
1653 | internal_setblocking(s, timeout < 0.0);
|
---|
1654 |
|
---|
1655 | Py_INCREF(Py_None);
|
---|
1656 | return Py_None;
|
---|
1657 | }
|
---|
1658 |
|
---|
1659 | PyDoc_STRVAR(settimeout_doc,
|
---|
1660 | "settimeout(timeout)\n\
|
---|
1661 | \n\
|
---|
1662 | Set a timeout on socket operations. 'timeout' can be a float,\n\
|
---|
1663 | giving in seconds, or None. Setting a timeout of None disables\n\
|
---|
1664 | the timeout feature and is equivalent to setblocking(1).\n\
|
---|
1665 | Setting a timeout of zero is the same as setblocking(0).");
|
---|
1666 |
|
---|
1667 | /* s.gettimeout() method.
|
---|
1668 | Returns the timeout associated with a socket. */
|
---|
1669 | static PyObject *
|
---|
1670 | sock_gettimeout(PySocketSockObject *s)
|
---|
1671 | {
|
---|
1672 | if (s->sock_timeout < 0.0) {
|
---|
1673 | Py_INCREF(Py_None);
|
---|
1674 | return Py_None;
|
---|
1675 | }
|
---|
1676 | else
|
---|
1677 | return PyFloat_FromDouble(s->sock_timeout);
|
---|
1678 | }
|
---|
1679 |
|
---|
1680 | PyDoc_STRVAR(gettimeout_doc,
|
---|
1681 | "gettimeout() -> timeout\n\
|
---|
1682 | \n\
|
---|
1683 | Returns the timeout in floating seconds associated with socket \n\
|
---|
1684 | operations. A timeout of None indicates that timeouts on socket \n\
|
---|
1685 | operations are disabled.");
|
---|
1686 |
|
---|
1687 | #ifdef RISCOS
|
---|
1688 | /* s.sleeptaskw(1 | 0) method */
|
---|
1689 |
|
---|
1690 | static PyObject *
|
---|
1691 | sock_sleeptaskw(PySocketSockObject *s,PyObject *arg)
|
---|
1692 | {
|
---|
1693 | int block;
|
---|
1694 | block = PyInt_AsLong(arg);
|
---|
1695 | if (block == -1 && PyErr_Occurred())
|
---|
1696 | return NULL;
|
---|
1697 | Py_BEGIN_ALLOW_THREADS
|
---|
1698 | socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
|
---|
1699 | Py_END_ALLOW_THREADS
|
---|
1700 |
|
---|
1701 | Py_INCREF(Py_None);
|
---|
1702 | return Py_None;
|
---|
1703 | }
|
---|
1704 | PyDoc_STRVAR(sleeptaskw_doc,
|
---|
1705 | "sleeptaskw(flag)\n\
|
---|
1706 | \n\
|
---|
1707 | Allow sleeps in taskwindows.");
|
---|
1708 | #endif
|
---|
1709 |
|
---|
1710 |
|
---|
1711 | /* s.setsockopt() method.
|
---|
1712 | With an integer third argument, sets an integer option.
|
---|
1713 | With a string third argument, sets an option from a buffer;
|
---|
1714 | use optional built-in module 'struct' to encode the string. */
|
---|
1715 |
|
---|
1716 | static PyObject *
|
---|
1717 | sock_setsockopt(PySocketSockObject *s, PyObject *args)
|
---|
1718 | {
|
---|
1719 | int level;
|
---|
1720 | int optname;
|
---|
1721 | int res;
|
---|
1722 | char *buf;
|
---|
1723 | int buflen;
|
---|
1724 | int flag;
|
---|
1725 |
|
---|
1726 | if (PyArg_ParseTuple(args, "iii:setsockopt",
|
---|
1727 | &level, &optname, &flag)) {
|
---|
1728 | buf = (char *) &flag;
|
---|
1729 | buflen = sizeof flag;
|
---|
1730 | }
|
---|
1731 | else {
|
---|
1732 | PyErr_Clear();
|
---|
1733 | if (!PyArg_ParseTuple(args, "iis#:setsockopt",
|
---|
1734 | &level, &optname, &buf, &buflen))
|
---|
1735 | return NULL;
|
---|
1736 | }
|
---|
1737 | res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
|
---|
1738 | if (res < 0)
|
---|
1739 | return s->errorhandler();
|
---|
1740 | Py_INCREF(Py_None);
|
---|
1741 | return Py_None;
|
---|
1742 | }
|
---|
1743 |
|
---|
1744 | PyDoc_STRVAR(setsockopt_doc,
|
---|
1745 | "setsockopt(level, option, value)\n\
|
---|
1746 | \n\
|
---|
1747 | Set a socket option. See the Unix manual for level and option.\n\
|
---|
1748 | The value argument can either be an integer or a string.");
|
---|
1749 |
|
---|
1750 |
|
---|
1751 | /* s.getsockopt() method.
|
---|
1752 | With two arguments, retrieves an integer option.
|
---|
1753 | With a third integer argument, retrieves a string buffer of that size;
|
---|
1754 | use optional built-in module 'struct' to decode the string. */
|
---|
1755 |
|
---|
1756 | static PyObject *
|
---|
1757 | sock_getsockopt(PySocketSockObject *s, PyObject *args)
|
---|
1758 | {
|
---|
1759 | int level;
|
---|
1760 | int optname;
|
---|
1761 | int res;
|
---|
1762 | PyObject *buf;
|
---|
1763 | socklen_t buflen = 0;
|
---|
1764 |
|
---|
1765 | #ifdef __BEOS__
|
---|
1766 | /* We have incomplete socket support. */
|
---|
1767 | PyErr_SetString(socket_error, "getsockopt not supported");
|
---|
1768 | return NULL;
|
---|
1769 | #else
|
---|
1770 |
|
---|
1771 | if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
|
---|
1772 | &level, &optname, &buflen))
|
---|
1773 | return NULL;
|
---|
1774 |
|
---|
1775 | if (buflen == 0) {
|
---|
1776 | int flag = 0;
|
---|
1777 | socklen_t flagsize = sizeof flag;
|
---|
1778 | res = getsockopt(s->sock_fd, level, optname,
|
---|
1779 | (void *)&flag, &flagsize);
|
---|
1780 | if (res < 0)
|
---|
1781 | return s->errorhandler();
|
---|
1782 | return PyInt_FromLong(flag);
|
---|
1783 | }
|
---|
1784 | #ifdef __VMS
|
---|
1785 | /* socklen_t is unsigned so no negative test is needed,
|
---|
1786 | test buflen == 0 is previously done */
|
---|
1787 | if (buflen > 1024) {
|
---|
1788 | #else
|
---|
1789 | if (buflen <= 0 || buflen > 1024) {
|
---|
1790 | #endif
|
---|
1791 | PyErr_SetString(socket_error,
|
---|
1792 | "getsockopt buflen out of range");
|
---|
1793 | return NULL;
|
---|
1794 | }
|
---|
1795 | buf = PyString_FromStringAndSize((char *)NULL, buflen);
|
---|
1796 | if (buf == NULL)
|
---|
1797 | return NULL;
|
---|
1798 | res = getsockopt(s->sock_fd, level, optname,
|
---|
1799 | (void *)PyString_AS_STRING(buf), &buflen);
|
---|
1800 | if (res < 0) {
|
---|
1801 | Py_DECREF(buf);
|
---|
1802 | return s->errorhandler();
|
---|
1803 | }
|
---|
1804 | _PyString_Resize(&buf, buflen);
|
---|
1805 | return buf;
|
---|
1806 | #endif /* __BEOS__ */
|
---|
1807 | }
|
---|
1808 |
|
---|
1809 | PyDoc_STRVAR(getsockopt_doc,
|
---|
1810 | "getsockopt(level, option[, buffersize]) -> value\n\
|
---|
1811 | \n\
|
---|
1812 | Get a socket option. See the Unix manual for level and option.\n\
|
---|
1813 | If a nonzero buffersize argument is given, the return value is a\n\
|
---|
1814 | string of that length; otherwise it is an integer.");
|
---|
1815 |
|
---|
1816 |
|
---|
1817 | /* s.bind(sockaddr) method */
|
---|
1818 |
|
---|
1819 | static PyObject *
|
---|
1820 | sock_bind(PySocketSockObject *s, PyObject *addro)
|
---|
1821 | {
|
---|
1822 | struct sockaddr *addr;
|
---|
1823 | int addrlen;
|
---|
1824 | int res;
|
---|
1825 |
|
---|
1826 | if (!getsockaddrarg(s, addro, &addr, &addrlen))
|
---|
1827 | return NULL;
|
---|
1828 | Py_BEGIN_ALLOW_THREADS
|
---|
1829 | res = bind(s->sock_fd, addr, addrlen);
|
---|
1830 | Py_END_ALLOW_THREADS
|
---|
1831 | if (res < 0)
|
---|
1832 | return s->errorhandler();
|
---|
1833 | Py_INCREF(Py_None);
|
---|
1834 | return Py_None;
|
---|
1835 | }
|
---|
1836 |
|
---|
1837 | PyDoc_STRVAR(bind_doc,
|
---|
1838 | "bind(address)\n\
|
---|
1839 | \n\
|
---|
1840 | Bind the socket to a local address. For IP sockets, the address is a\n\
|
---|
1841 | pair (host, port); the host must refer to the local host. For raw packet\n\
|
---|
1842 | sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
|
---|
1843 |
|
---|
1844 |
|
---|
1845 | /* s.close() method.
|
---|
1846 | Set the file descriptor to -1 so operations tried subsequently
|
---|
1847 | will surely fail. */
|
---|
1848 |
|
---|
1849 | static PyObject *
|
---|
1850 | sock_close(PySocketSockObject *s)
|
---|
1851 | {
|
---|
1852 | SOCKET_T fd;
|
---|
1853 |
|
---|
1854 | if ((fd = s->sock_fd) != -1) {
|
---|
1855 | s->sock_fd = -1;
|
---|
1856 | Py_BEGIN_ALLOW_THREADS
|
---|
1857 | (void) SOCKETCLOSE(fd);
|
---|
1858 | Py_END_ALLOW_THREADS
|
---|
1859 | }
|
---|
1860 | Py_INCREF(Py_None);
|
---|
1861 | return Py_None;
|
---|
1862 | }
|
---|
1863 |
|
---|
1864 | PyDoc_STRVAR(close_doc,
|
---|
1865 | "close()\n\
|
---|
1866 | \n\
|
---|
1867 | Close the socket. It cannot be used after this call.");
|
---|
1868 |
|
---|
1869 | static int
|
---|
1870 | internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
|
---|
1871 | int *timeoutp)
|
---|
1872 | {
|
---|
1873 | int res, timeout;
|
---|
1874 |
|
---|
1875 | timeout = 0;
|
---|
1876 | res = connect(s->sock_fd, addr, addrlen);
|
---|
1877 |
|
---|
1878 | #ifdef MS_WINDOWS
|
---|
1879 |
|
---|
1880 | if (s->sock_timeout > 0.0) {
|
---|
1881 | if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK &&
|
---|
1882 | IS_SELECTABLE(s)) {
|
---|
1883 | /* This is a mess. Best solution: trust select */
|
---|
1884 | fd_set fds;
|
---|
1885 | fd_set fds_exc;
|
---|
1886 | struct timeval tv;
|
---|
1887 | tv.tv_sec = (int)s->sock_timeout;
|
---|
1888 | tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
|
---|
1889 | FD_ZERO(&fds);
|
---|
1890 | FD_SET(s->sock_fd, &fds);
|
---|
1891 | FD_ZERO(&fds_exc);
|
---|
1892 | FD_SET(s->sock_fd, &fds_exc);
|
---|
1893 | res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv);
|
---|
1894 | if (res == 0) {
|
---|
1895 | res = WSAEWOULDBLOCK;
|
---|
1896 | timeout = 1;
|
---|
1897 | } else if (res > 0) {
|
---|
1898 | if (FD_ISSET(s->sock_fd, &fds))
|
---|
1899 | /* The socket is in the writeable set - this
|
---|
1900 | means connected */
|
---|
1901 | res = 0;
|
---|
1902 | else {
|
---|
1903 | /* As per MS docs, we need to call getsockopt()
|
---|
1904 | to get the underlying error */
|
---|
1905 | int res_size = sizeof res;
|
---|
1906 | /* It must be in the exception set */
|
---|
1907 | assert(FD_ISSET(s->sock_fd, &fds_exc));
|
---|
1908 | if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
|
---|
1909 | (char *)&res, &res_size))
|
---|
1910 | /* getsockopt also clears WSAGetLastError,
|
---|
1911 | so reset it back. */
|
---|
1912 | WSASetLastError(res);
|
---|
1913 | else
|
---|
1914 | res = WSAGetLastError();
|
---|
1915 | }
|
---|
1916 | }
|
---|
1917 | /* else if (res < 0) an error occurred */
|
---|
1918 | }
|
---|
1919 | }
|
---|
1920 |
|
---|
1921 | if (res < 0)
|
---|
1922 | res = WSAGetLastError();
|
---|
1923 |
|
---|
1924 | #else
|
---|
1925 |
|
---|
1926 | if (s->sock_timeout > 0.0) {
|
---|
1927 | if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) {
|
---|
1928 | timeout = internal_select(s, 1);
|
---|
1929 | if (timeout == 0) {
|
---|
1930 | res = connect(s->sock_fd, addr, addrlen);
|
---|
1931 | if (res < 0 && errno == EISCONN)
|
---|
1932 | res = 0;
|
---|
1933 | }
|
---|
1934 | else if (timeout == -1)
|
---|
1935 | res = errno; /* had error */
|
---|
1936 | else
|
---|
1937 | res = EWOULDBLOCK; /* timed out */
|
---|
1938 | }
|
---|
1939 | }
|
---|
1940 |
|
---|
1941 | if (res < 0)
|
---|
1942 | res = errno;
|
---|
1943 |
|
---|
1944 | #endif
|
---|
1945 | *timeoutp = timeout;
|
---|
1946 |
|
---|
1947 | return res;
|
---|
1948 | }
|
---|
1949 |
|
---|
1950 | /* s.connect(sockaddr) method */
|
---|
1951 |
|
---|
1952 | static PyObject *
|
---|
1953 | sock_connect(PySocketSockObject *s, PyObject *addro)
|
---|
1954 | {
|
---|
1955 | struct sockaddr *addr;
|
---|
1956 | int addrlen;
|
---|
1957 | int res;
|
---|
1958 | int timeout;
|
---|
1959 |
|
---|
1960 | if (!getsockaddrarg(s, addro, &addr, &addrlen))
|
---|
1961 | return NULL;
|
---|
1962 |
|
---|
1963 | Py_BEGIN_ALLOW_THREADS
|
---|
1964 | res = internal_connect(s, addr, addrlen, &timeout);
|
---|
1965 | Py_END_ALLOW_THREADS
|
---|
1966 |
|
---|
1967 | if (timeout == 1) {
|
---|
1968 | PyErr_SetString(socket_timeout, "timed out");
|
---|
1969 | return NULL;
|
---|
1970 | }
|
---|
1971 | if (res != 0)
|
---|
1972 | return s->errorhandler();
|
---|
1973 | Py_INCREF(Py_None);
|
---|
1974 | return Py_None;
|
---|
1975 | }
|
---|
1976 |
|
---|
1977 | PyDoc_STRVAR(connect_doc,
|
---|
1978 | "connect(address)\n\
|
---|
1979 | \n\
|
---|
1980 | Connect the socket to a remote address. For IP sockets, the address\n\
|
---|
1981 | is a pair (host, port).");
|
---|
1982 |
|
---|
1983 |
|
---|
1984 | /* s.connect_ex(sockaddr) method */
|
---|
1985 |
|
---|
1986 | static PyObject *
|
---|
1987 | sock_connect_ex(PySocketSockObject *s, PyObject *addro)
|
---|
1988 | {
|
---|
1989 | struct sockaddr *addr;
|
---|
1990 | int addrlen;
|
---|
1991 | int res;
|
---|
1992 | int timeout;
|
---|
1993 |
|
---|
1994 | if (!getsockaddrarg(s, addro, &addr, &addrlen))
|
---|
1995 | return NULL;
|
---|
1996 |
|
---|
1997 | Py_BEGIN_ALLOW_THREADS
|
---|
1998 | res = internal_connect(s, addr, addrlen, &timeout);
|
---|
1999 | Py_END_ALLOW_THREADS
|
---|
2000 |
|
---|
2001 | /* Signals are not errors (though they may raise exceptions). Adapted
|
---|
2002 | from PyErr_SetFromErrnoWithFilenameObject(). */
|
---|
2003 | #ifdef EINTR
|
---|
2004 | if (res == EINTR && PyErr_CheckSignals())
|
---|
2005 | return NULL;
|
---|
2006 | #endif
|
---|
2007 |
|
---|
2008 | return PyInt_FromLong((long) res);
|
---|
2009 | }
|
---|
2010 |
|
---|
2011 | PyDoc_STRVAR(connect_ex_doc,
|
---|
2012 | "connect_ex(address) -> errno\n\
|
---|
2013 | \n\
|
---|
2014 | This is like connect(address), but returns an error code (the errno value)\n\
|
---|
2015 | instead of raising an exception when an error occurs.");
|
---|
2016 |
|
---|
2017 |
|
---|
2018 | /* s.fileno() method */
|
---|
2019 |
|
---|
2020 | static PyObject *
|
---|
2021 | sock_fileno(PySocketSockObject *s)
|
---|
2022 | {
|
---|
2023 | #if SIZEOF_SOCKET_T <= SIZEOF_LONG
|
---|
2024 | return PyInt_FromLong((long) s->sock_fd);
|
---|
2025 | #else
|
---|
2026 | return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd);
|
---|
2027 | #endif
|
---|
2028 | }
|
---|
2029 |
|
---|
2030 | PyDoc_STRVAR(fileno_doc,
|
---|
2031 | "fileno() -> integer\n\
|
---|
2032 | \n\
|
---|
2033 | Return the integer file descriptor of the socket.");
|
---|
2034 |
|
---|
2035 |
|
---|
2036 | #ifndef NO_DUP
|
---|
2037 | /* s.dup() method */
|
---|
2038 |
|
---|
2039 | static PyObject *
|
---|
2040 | sock_dup(PySocketSockObject *s)
|
---|
2041 | {
|
---|
2042 | SOCKET_T newfd;
|
---|
2043 | PyObject *sock;
|
---|
2044 |
|
---|
2045 | newfd = dup(s->sock_fd);
|
---|
2046 | if (newfd < 0)
|
---|
2047 | return s->errorhandler();
|
---|
2048 | sock = (PyObject *) new_sockobject(newfd,
|
---|
2049 | s->sock_family,
|
---|
2050 | s->sock_type,
|
---|
2051 | s->sock_proto);
|
---|
2052 | if (sock == NULL)
|
---|
2053 | SOCKETCLOSE(newfd);
|
---|
2054 | return sock;
|
---|
2055 | }
|
---|
2056 |
|
---|
2057 | PyDoc_STRVAR(dup_doc,
|
---|
2058 | "dup() -> socket object\n\
|
---|
2059 | \n\
|
---|
2060 | Return a new socket object connected to the same system resource.");
|
---|
2061 |
|
---|
2062 | #endif
|
---|
2063 |
|
---|
2064 |
|
---|
2065 | /* s.getsockname() method */
|
---|
2066 |
|
---|
2067 | static PyObject *
|
---|
2068 | sock_getsockname(PySocketSockObject *s)
|
---|
2069 | {
|
---|
2070 | sock_addr_t addrbuf;
|
---|
2071 | int res;
|
---|
2072 | socklen_t addrlen;
|
---|
2073 |
|
---|
2074 | if (!getsockaddrlen(s, &addrlen))
|
---|
2075 | return NULL;
|
---|
2076 | memset(&addrbuf, 0, addrlen);
|
---|
2077 | Py_BEGIN_ALLOW_THREADS
|
---|
2078 | res = getsockname(s->sock_fd, (struct sockaddr *) &addrbuf, &addrlen);
|
---|
2079 | Py_END_ALLOW_THREADS
|
---|
2080 | if (res < 0)
|
---|
2081 | return s->errorhandler();
|
---|
2082 | return makesockaddr(s->sock_fd, (struct sockaddr *) &addrbuf, addrlen,
|
---|
2083 | s->sock_proto);
|
---|
2084 | }
|
---|
2085 |
|
---|
2086 | PyDoc_STRVAR(getsockname_doc,
|
---|
2087 | "getsockname() -> address info\n\
|
---|
2088 | \n\
|
---|
2089 | Return the address of the local endpoint. For IP sockets, the address\n\
|
---|
2090 | info is a pair (hostaddr, port).");
|
---|
2091 |
|
---|
2092 |
|
---|
2093 | #ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
|
---|
2094 | /* s.getpeername() method */
|
---|
2095 |
|
---|
2096 | static PyObject *
|
---|
2097 | sock_getpeername(PySocketSockObject *s)
|
---|
2098 | {
|
---|
2099 | sock_addr_t addrbuf;
|
---|
2100 | int res;
|
---|
2101 | socklen_t addrlen;
|
---|
2102 |
|
---|
2103 | if (!getsockaddrlen(s, &addrlen))
|
---|
2104 | return NULL;
|
---|
2105 | memset(&addrbuf, 0, addrlen);
|
---|
2106 | Py_BEGIN_ALLOW_THREADS
|
---|
2107 | res = getpeername(s->sock_fd, (struct sockaddr *) &addrbuf, &addrlen);
|
---|
2108 | Py_END_ALLOW_THREADS
|
---|
2109 | if (res < 0)
|
---|
2110 | return s->errorhandler();
|
---|
2111 | return makesockaddr(s->sock_fd, (struct sockaddr *) &addrbuf, addrlen,
|
---|
2112 | s->sock_proto);
|
---|
2113 | }
|
---|
2114 |
|
---|
2115 | PyDoc_STRVAR(getpeername_doc,
|
---|
2116 | "getpeername() -> address info\n\
|
---|
2117 | \n\
|
---|
2118 | Return the address of the remote endpoint. For IP sockets, the address\n\
|
---|
2119 | info is a pair (hostaddr, port).");
|
---|
2120 |
|
---|
2121 | #endif /* HAVE_GETPEERNAME */
|
---|
2122 |
|
---|
2123 |
|
---|
2124 | /* s.listen(n) method */
|
---|
2125 |
|
---|
2126 | static PyObject *
|
---|
2127 | sock_listen(PySocketSockObject *s, PyObject *arg)
|
---|
2128 | {
|
---|
2129 | int backlog;
|
---|
2130 | int res;
|
---|
2131 |
|
---|
2132 | backlog = PyInt_AsLong(arg);
|
---|
2133 | if (backlog == -1 && PyErr_Occurred())
|
---|
2134 | return NULL;
|
---|
2135 | Py_BEGIN_ALLOW_THREADS
|
---|
2136 | if (backlog < 1)
|
---|
2137 | backlog = 1;
|
---|
2138 | res = listen(s->sock_fd, backlog);
|
---|
2139 | Py_END_ALLOW_THREADS
|
---|
2140 | if (res < 0)
|
---|
2141 | return s->errorhandler();
|
---|
2142 | Py_INCREF(Py_None);
|
---|
2143 | return Py_None;
|
---|
2144 | }
|
---|
2145 |
|
---|
2146 | PyDoc_STRVAR(listen_doc,
|
---|
2147 | "listen(backlog)\n\
|
---|
2148 | \n\
|
---|
2149 | Enable a server to accept connections. The backlog argument must be at\n\
|
---|
2150 | least 1; it specifies the number of unaccepted connection that the system\n\
|
---|
2151 | will allow before refusing new connections.");
|
---|
2152 |
|
---|
2153 |
|
---|
2154 | #ifndef NO_DUP
|
---|
2155 | /* s.makefile(mode) method.
|
---|
2156 | Create a new open file object referring to a dupped version of
|
---|
2157 | the socket's file descriptor. (The dup() call is necessary so
|
---|
2158 | that the open file and socket objects may be closed independent
|
---|
2159 | of each other.)
|
---|
2160 | The mode argument specifies 'r' or 'w' passed to fdopen(). */
|
---|
2161 |
|
---|
2162 | static PyObject *
|
---|
2163 | sock_makefile(PySocketSockObject *s, PyObject *args)
|
---|
2164 | {
|
---|
2165 | extern int fclose(FILE *);
|
---|
2166 | char *mode = "r";
|
---|
2167 | int bufsize = -1;
|
---|
2168 | #ifdef MS_WIN32
|
---|
2169 | Py_intptr_t fd;
|
---|
2170 | #else
|
---|
2171 | int fd;
|
---|
2172 | #endif
|
---|
2173 | FILE *fp;
|
---|
2174 | PyObject *f;
|
---|
2175 | #ifdef __VMS
|
---|
2176 | char *mode_r = "r";
|
---|
2177 | char *mode_w = "w";
|
---|
2178 | #endif
|
---|
2179 |
|
---|
2180 | if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))
|
---|
2181 | return NULL;
|
---|
2182 | #ifdef __VMS
|
---|
2183 | if (strcmp(mode,"rb") == 0) {
|
---|
2184 | mode = mode_r;
|
---|
2185 | }
|
---|
2186 | else {
|
---|
2187 | if (strcmp(mode,"wb") == 0) {
|
---|
2188 | mode = mode_w;
|
---|
2189 | }
|
---|
2190 | }
|
---|
2191 | #endif
|
---|
2192 | #ifdef MS_WIN32
|
---|
2193 | if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||
|
---|
2194 | ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))
|
---|
2195 | #else
|
---|
2196 | if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)
|
---|
2197 | #endif
|
---|
2198 | {
|
---|
2199 | if (fd >= 0)
|
---|
2200 | SOCKETCLOSE(fd);
|
---|
2201 | return s->errorhandler();
|
---|
2202 | }
|
---|
2203 | f = PyFile_FromFile(fp, "<socket>", mode, fclose);
|
---|
2204 | if (f != NULL)
|
---|
2205 | PyFile_SetBufSize(f, bufsize);
|
---|
2206 | return f;
|
---|
2207 | }
|
---|
2208 |
|
---|
2209 | PyDoc_STRVAR(makefile_doc,
|
---|
2210 | "makefile([mode[, buffersize]]) -> file object\n\
|
---|
2211 | \n\
|
---|
2212 | Return a regular file object corresponding to the socket.\n\
|
---|
2213 | The mode and buffersize arguments are as for the built-in open() function.");
|
---|
2214 |
|
---|
2215 | #endif /* NO_DUP */
|
---|
2216 |
|
---|
2217 | /*
|
---|
2218 | * This is the guts of the recv() and recv_into() methods, which reads into a
|
---|
2219 | * char buffer. If you have any inc/def ref to do to the objects that contain
|
---|
2220 | * the buffer, do it in the caller. This function returns the number of bytes
|
---|
2221 | * succesfully read. If there was an error, it returns -1. Note that it is
|
---|
2222 | * also possible that we return a number of bytes smaller than the request
|
---|
2223 | * bytes.
|
---|
2224 | */
|
---|
2225 | static ssize_t
|
---|
2226 | sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags)
|
---|
2227 | {
|
---|
2228 | ssize_t outlen = -1;
|
---|
2229 | int timeout;
|
---|
2230 | #ifdef __VMS
|
---|
2231 | int remaining;
|
---|
2232 | char *read_buf;
|
---|
2233 | #endif
|
---|
2234 |
|
---|
2235 | if (!IS_SELECTABLE(s)) {
|
---|
2236 | select_error();
|
---|
2237 | return -1;
|
---|
2238 | }
|
---|
2239 |
|
---|
2240 | #ifndef __VMS
|
---|
2241 | Py_BEGIN_ALLOW_THREADS
|
---|
2242 | timeout = internal_select(s, 0);
|
---|
2243 | if (!timeout)
|
---|
2244 | outlen = recv(s->sock_fd, cbuf, len, flags);
|
---|
2245 | Py_END_ALLOW_THREADS
|
---|
2246 |
|
---|
2247 | if (timeout == 1) {
|
---|
2248 | PyErr_SetString(socket_timeout, "timed out");
|
---|
2249 | return -1;
|
---|
2250 | }
|
---|
2251 | if (outlen < 0) {
|
---|
2252 | /* Note: the call to errorhandler() ALWAYS indirectly returned
|
---|
2253 | NULL, so ignore its return value */
|
---|
2254 | s->errorhandler();
|
---|
2255 | return -1;
|
---|
2256 | }
|
---|
2257 | #else
|
---|
2258 | read_buf = cbuf;
|
---|
2259 | remaining = len;
|
---|
2260 | while (remaining != 0) {
|
---|
2261 | unsigned int segment;
|
---|
2262 | int nread = -1;
|
---|
2263 |
|
---|
2264 | segment = remaining /SEGMENT_SIZE;
|
---|
2265 | if (segment != 0) {
|
---|
2266 | segment = SEGMENT_SIZE;
|
---|
2267 | }
|
---|
2268 | else {
|
---|
2269 | segment = remaining;
|
---|
2270 | }
|
---|
2271 |
|
---|
2272 | Py_BEGIN_ALLOW_THREADS
|
---|
2273 | timeout = internal_select(s, 0);
|
---|
2274 | if (!timeout)
|
---|
2275 | nread = recv(s->sock_fd, read_buf, segment, flags);
|
---|
2276 | Py_END_ALLOW_THREADS
|
---|
2277 |
|
---|
2278 | if (timeout == 1) {
|
---|
2279 | PyErr_SetString(socket_timeout, "timed out");
|
---|
2280 | return -1;
|
---|
2281 | }
|
---|
2282 | if (nread < 0) {
|
---|
2283 | s->errorhandler();
|
---|
2284 | return -1;
|
---|
2285 | }
|
---|
2286 | if (nread != remaining) {
|
---|
2287 | read_buf += nread;
|
---|
2288 | break;
|
---|
2289 | }
|
---|
2290 |
|
---|
2291 | remaining -= segment;
|
---|
2292 | read_buf += segment;
|
---|
2293 | }
|
---|
2294 | outlen = read_buf - cbuf;
|
---|
2295 | #endif /* !__VMS */
|
---|
2296 |
|
---|
2297 | return outlen;
|
---|
2298 | }
|
---|
2299 |
|
---|
2300 |
|
---|
2301 | /* s.recv(nbytes [,flags]) method */
|
---|
2302 |
|
---|
2303 | static PyObject *
|
---|
2304 | sock_recv(PySocketSockObject *s, PyObject *args)
|
---|
2305 | {
|
---|
2306 | int recvlen, flags = 0;
|
---|
2307 | ssize_t outlen;
|
---|
2308 | PyObject *buf;
|
---|
2309 |
|
---|
2310 | if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags))
|
---|
2311 | return NULL;
|
---|
2312 |
|
---|
2313 | if (recvlen < 0) {
|
---|
2314 | PyErr_SetString(PyExc_ValueError,
|
---|
2315 | "negative buffersize in recv");
|
---|
2316 | return NULL;
|
---|
2317 | }
|
---|
2318 |
|
---|
2319 | /* Allocate a new string. */
|
---|
2320 | buf = PyString_FromStringAndSize((char *) 0, recvlen);
|
---|
2321 | if (buf == NULL)
|
---|
2322 | return NULL;
|
---|
2323 |
|
---|
2324 | /* Call the guts */
|
---|
2325 | outlen = sock_recv_guts(s, PyString_AS_STRING(buf), recvlen, flags);
|
---|
2326 | if (outlen < 0) {
|
---|
2327 | /* An error occurred, release the string and return an
|
---|
2328 | error. */
|
---|
2329 | Py_DECREF(buf);
|
---|
2330 | return NULL;
|
---|
2331 | }
|
---|
2332 | if (outlen != recvlen) {
|
---|
2333 | /* We did not read as many bytes as we anticipated, resize the
|
---|
2334 | string if possible and be succesful. */
|
---|
2335 | if (_PyString_Resize(&buf, outlen) < 0)
|
---|
2336 | /* Oopsy, not so succesful after all. */
|
---|
2337 | return NULL;
|
---|
2338 | }
|
---|
2339 |
|
---|
2340 | return buf;
|
---|
2341 | }
|
---|
2342 |
|
---|
2343 | PyDoc_STRVAR(recv_doc,
|
---|
2344 | "recv(buffersize[, flags]) -> data\n\
|
---|
2345 | \n\
|
---|
2346 | Receive up to buffersize bytes from the socket. For the optional flags\n\
|
---|
2347 | argument, see the Unix manual. When no data is available, block until\n\
|
---|
2348 | at least one byte is available or until the remote end is closed. When\n\
|
---|
2349 | the remote end is closed and all data is read, return the empty string.");
|
---|
2350 |
|
---|
2351 |
|
---|
2352 | /* s.recv_into(buffer, [nbytes [,flags]]) method */
|
---|
2353 |
|
---|
2354 | static PyObject*
|
---|
2355 | sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)
|
---|
2356 | {
|
---|
2357 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
|
---|
2358 |
|
---|
2359 | int recvlen = 0, flags = 0;
|
---|
2360 | ssize_t readlen;
|
---|
2361 | char *buf;
|
---|
2362 | int buflen;
|
---|
2363 |
|
---|
2364 | /* Get the buffer's memory */
|
---|
2365 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recv", kwlist,
|
---|
2366 | &buf, &buflen, &recvlen, &flags))
|
---|
2367 | return NULL;
|
---|
2368 | assert(buf != 0 && buflen > 0);
|
---|
2369 |
|
---|
2370 | if (recvlen < 0) {
|
---|
2371 | PyErr_SetString(PyExc_ValueError,
|
---|
2372 | "negative buffersize in recv");
|
---|
2373 | return NULL;
|
---|
2374 | }
|
---|
2375 | if (recvlen == 0) {
|
---|
2376 | /* If nbytes was not specified, use the buffer's length */
|
---|
2377 | recvlen = buflen;
|
---|
2378 | }
|
---|
2379 |
|
---|
2380 | /* Check if the buffer is large enough */
|
---|
2381 | if (buflen < recvlen) {
|
---|
2382 | PyErr_SetString(PyExc_ValueError,
|
---|
2383 | "buffer too small for requested bytes");
|
---|
2384 | return NULL;
|
---|
2385 | }
|
---|
2386 |
|
---|
2387 | /* Call the guts */
|
---|
2388 | readlen = sock_recv_guts(s, buf, recvlen, flags);
|
---|
2389 | if (readlen < 0) {
|
---|
2390 | /* Return an error. */
|
---|
2391 | return NULL;
|
---|
2392 | }
|
---|
2393 |
|
---|
2394 | /* Return the number of bytes read. Note that we do not do anything
|
---|
2395 | special here in the case that readlen < recvlen. */
|
---|
2396 | return PyInt_FromSsize_t(readlen);
|
---|
2397 | }
|
---|
2398 |
|
---|
2399 | PyDoc_STRVAR(recv_into_doc,
|
---|
2400 | "recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\
|
---|
2401 | \n\
|
---|
2402 | A version of recv() that stores its data into a buffer rather than creating \n\
|
---|
2403 | a new string. Receive up to buffersize bytes from the socket. If buffersize \n\
|
---|
2404 | is not specified (or 0), receive up to the size available in the given buffer.\n\
|
---|
2405 | \n\
|
---|
2406 | See recv() for documentation about the flags.");
|
---|
2407 |
|
---|
2408 |
|
---|
2409 | /*
|
---|
2410 | * This is the guts of the recv() and recv_into() methods, which reads into a
|
---|
2411 | * char buffer. If you have any inc/def ref to do to the objects that contain
|
---|
2412 | * the buffer, do it in the caller. This function returns the number of bytes
|
---|
2413 | * succesfully read. If there was an error, it returns -1. Note that it is
|
---|
2414 | * also possible that we return a number of bytes smaller than the request
|
---|
2415 | * bytes.
|
---|
2416 | *
|
---|
2417 | * 'addr' is a return value for the address object. Note that you must decref
|
---|
2418 | * it yourself.
|
---|
2419 | */
|
---|
2420 | static ssize_t
|
---|
2421 | sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags,
|
---|
2422 | PyObject** addr)
|
---|
2423 | {
|
---|
2424 | sock_addr_t addrbuf;
|
---|
2425 | int timeout;
|
---|
2426 | ssize_t n = -1;
|
---|
2427 | socklen_t addrlen;
|
---|
2428 |
|
---|
2429 | *addr = NULL;
|
---|
2430 |
|
---|
2431 | if (!getsockaddrlen(s, &addrlen))
|
---|
2432 | return -1;
|
---|
2433 |
|
---|
2434 | if (!IS_SELECTABLE(s)) {
|
---|
2435 | select_error();
|
---|
2436 | return -1;
|
---|
2437 | }
|
---|
2438 |
|
---|
2439 | Py_BEGIN_ALLOW_THREADS
|
---|
2440 | memset(&addrbuf, 0, addrlen);
|
---|
2441 | timeout = internal_select(s, 0);
|
---|
2442 | if (!timeout) {
|
---|
2443 | #ifndef MS_WINDOWS
|
---|
2444 | #if defined(PYOS_OS2) && !defined(PYCC_GCC)
|
---|
2445 | n = recvfrom(s->sock_fd, cbuf, len, flags,
|
---|
2446 | (struct sockaddr *) &addrbuf, &addrlen);
|
---|
2447 | #else
|
---|
2448 | n = recvfrom(s->sock_fd, cbuf, len, flags,
|
---|
2449 | (void *) &addrbuf, &addrlen);
|
---|
2450 | #endif
|
---|
2451 | #else
|
---|
2452 | n = recvfrom(s->sock_fd, cbuf, len, flags,
|
---|
2453 | (struct sockaddr *) &addrbuf, &addrlen);
|
---|
2454 | #endif
|
---|
2455 | }
|
---|
2456 | Py_END_ALLOW_THREADS
|
---|
2457 |
|
---|
2458 | if (timeout == 1) {
|
---|
2459 | PyErr_SetString(socket_timeout, "timed out");
|
---|
2460 | return -1;
|
---|
2461 | }
|
---|
2462 | if (n < 0) {
|
---|
2463 | s->errorhandler();
|
---|
2464 | return -1;
|
---|
2465 | }
|
---|
2466 |
|
---|
2467 | if (!(*addr = makesockaddr(s->sock_fd, (struct sockaddr *) &addrbuf,
|
---|
2468 | addrlen, s->sock_proto)))
|
---|
2469 | return -1;
|
---|
2470 |
|
---|
2471 | return n;
|
---|
2472 | }
|
---|
2473 |
|
---|
2474 | /* s.recvfrom(nbytes [,flags]) method */
|
---|
2475 |
|
---|
2476 | static PyObject *
|
---|
2477 | sock_recvfrom(PySocketSockObject *s, PyObject *args)
|
---|
2478 | {
|
---|
2479 | PyObject *buf = NULL;
|
---|
2480 | PyObject *addr = NULL;
|
---|
2481 | PyObject *ret = NULL;
|
---|
2482 | int recvlen, flags = 0;
|
---|
2483 | ssize_t outlen;
|
---|
2484 |
|
---|
2485 | if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags))
|
---|
2486 | return NULL;
|
---|
2487 |
|
---|
2488 | buf = PyString_FromStringAndSize((char *) 0, recvlen);
|
---|
2489 | if (buf == NULL)
|
---|
2490 | return NULL;
|
---|
2491 |
|
---|
2492 | outlen = sock_recvfrom_guts(s, PyString_AS_STRING(buf),
|
---|
2493 | recvlen, flags, &addr);
|
---|
2494 | if (outlen < 0) {
|
---|
2495 | goto finally;
|
---|
2496 | }
|
---|
2497 |
|
---|
2498 | if (outlen != recvlen) {
|
---|
2499 | /* We did not read as many bytes as we anticipated, resize the
|
---|
2500 | string if possible and be succesful. */
|
---|
2501 | if (_PyString_Resize(&buf, outlen) < 0)
|
---|
2502 | /* Oopsy, not so succesful after all. */
|
---|
2503 | goto finally;
|
---|
2504 | }
|
---|
2505 |
|
---|
2506 | ret = PyTuple_Pack(2, buf, addr);
|
---|
2507 |
|
---|
2508 | finally:
|
---|
2509 | Py_XDECREF(buf);
|
---|
2510 | Py_XDECREF(addr);
|
---|
2511 | return ret;
|
---|
2512 | }
|
---|
2513 |
|
---|
2514 | PyDoc_STRVAR(recvfrom_doc,
|
---|
2515 | "recvfrom(buffersize[, flags]) -> (data, address info)\n\
|
---|
2516 | \n\
|
---|
2517 | Like recv(buffersize, flags) but also return the sender's address info.");
|
---|
2518 |
|
---|
2519 |
|
---|
2520 | /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */
|
---|
2521 |
|
---|
2522 | static PyObject *
|
---|
2523 | sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
|
---|
2524 | {
|
---|
2525 | static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
|
---|
2526 |
|
---|
2527 | int recvlen = 0, flags = 0;
|
---|
2528 | ssize_t readlen;
|
---|
2529 | char *buf;
|
---|
2530 | int buflen;
|
---|
2531 |
|
---|
2532 | PyObject *addr = NULL;
|
---|
2533 |
|
---|
2534 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recvfrom", kwlist,
|
---|
2535 | &buf, &buflen, &recvlen, &flags))
|
---|
2536 | return NULL;
|
---|
2537 | assert(buf != 0 && buflen > 0);
|
---|
2538 |
|
---|
2539 | if (recvlen < 0) {
|
---|
2540 | PyErr_SetString(PyExc_ValueError,
|
---|
2541 | "negative buffersize in recv");
|
---|
2542 | return NULL;
|
---|
2543 | }
|
---|
2544 | if (recvlen == 0) {
|
---|
2545 | /* If nbytes was not specified, use the buffer's length */
|
---|
2546 | recvlen = buflen;
|
---|
2547 | }
|
---|
2548 |
|
---|
2549 | readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
|
---|
2550 | if (readlen < 0) {
|
---|
2551 | /* Return an error */
|
---|
2552 | Py_XDECREF(addr);
|
---|
2553 | return NULL;
|
---|
2554 | }
|
---|
2555 |
|
---|
2556 | /* Return the number of bytes read and the address. Note that we do
|
---|
2557 | not do anything special here in the case that readlen < recvlen. */
|
---|
2558 | return Py_BuildValue("lN", readlen, addr);
|
---|
2559 | }
|
---|
2560 |
|
---|
2561 | PyDoc_STRVAR(recvfrom_into_doc,
|
---|
2562 | "recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
|
---|
2563 | \n\
|
---|
2564 | Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");
|
---|
2565 |
|
---|
2566 |
|
---|
2567 | /* s.send(data [,flags]) method */
|
---|
2568 |
|
---|
2569 | static PyObject *
|
---|
2570 | sock_send(PySocketSockObject *s, PyObject *args)
|
---|
2571 | {
|
---|
2572 | char *buf;
|
---|
2573 | int len, n = -1, flags = 0, timeout;
|
---|
2574 |
|
---|
2575 | if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
|
---|
2576 | return NULL;
|
---|
2577 |
|
---|
2578 | if (!IS_SELECTABLE(s))
|
---|
2579 | return select_error();
|
---|
2580 |
|
---|
2581 | Py_BEGIN_ALLOW_THREADS
|
---|
2582 | timeout = internal_select(s, 1);
|
---|
2583 | if (!timeout)
|
---|
2584 | #ifdef __VMS
|
---|
2585 | n = sendsegmented(s->sock_fd, buf, len, flags);
|
---|
2586 | #else
|
---|
2587 | n = send(s->sock_fd, buf, len, flags);
|
---|
2588 | #endif
|
---|
2589 | Py_END_ALLOW_THREADS
|
---|
2590 |
|
---|
2591 | if (timeout == 1) {
|
---|
2592 | PyErr_SetString(socket_timeout, "timed out");
|
---|
2593 | return NULL;
|
---|
2594 | }
|
---|
2595 | if (n < 0)
|
---|
2596 | return s->errorhandler();
|
---|
2597 | return PyInt_FromLong((long)n);
|
---|
2598 | }
|
---|
2599 |
|
---|
2600 | PyDoc_STRVAR(send_doc,
|
---|
2601 | "send(data[, flags]) -> count\n\
|
---|
2602 | \n\
|
---|
2603 | Send a data string to the socket. For the optional flags\n\
|
---|
2604 | argument, see the Unix manual. Return the number of bytes\n\
|
---|
2605 | sent; this may be less than len(data) if the network is busy.");
|
---|
2606 |
|
---|
2607 |
|
---|
2608 | /* s.sendall(data [,flags]) method */
|
---|
2609 |
|
---|
2610 | static PyObject *
|
---|
2611 | sock_sendall(PySocketSockObject *s, PyObject *args)
|
---|
2612 | {
|
---|
2613 | char *buf;
|
---|
2614 | int len, n = -1, flags = 0, timeout;
|
---|
2615 |
|
---|
2616 | if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags))
|
---|
2617 | return NULL;
|
---|
2618 |
|
---|
2619 | if (!IS_SELECTABLE(s))
|
---|
2620 | return select_error();
|
---|
2621 |
|
---|
2622 | Py_BEGIN_ALLOW_THREADS
|
---|
2623 | do {
|
---|
2624 | timeout = internal_select(s, 1);
|
---|
2625 | n = -1;
|
---|
2626 | if (timeout)
|
---|
2627 | break;
|
---|
2628 | #ifdef __VMS
|
---|
2629 | n = sendsegmented(s->sock_fd, buf, len, flags);
|
---|
2630 | #else
|
---|
2631 | n = send(s->sock_fd, buf, len, flags);
|
---|
2632 | #endif
|
---|
2633 | if (n < 0)
|
---|
2634 | break;
|
---|
2635 | buf += n;
|
---|
2636 | len -= n;
|
---|
2637 | } while (len > 0);
|
---|
2638 | Py_END_ALLOW_THREADS
|
---|
2639 |
|
---|
2640 | if (timeout == 1) {
|
---|
2641 | PyErr_SetString(socket_timeout, "timed out");
|
---|
2642 | return NULL;
|
---|
2643 | }
|
---|
2644 | if (n < 0)
|
---|
2645 | return s->errorhandler();
|
---|
2646 |
|
---|
2647 | Py_INCREF(Py_None);
|
---|
2648 | return Py_None;
|
---|
2649 | }
|
---|
2650 |
|
---|
2651 | PyDoc_STRVAR(sendall_doc,
|
---|
2652 | "sendall(data[, flags])\n\
|
---|
2653 | \n\
|
---|
2654 | Send a data string to the socket. For the optional flags\n\
|
---|
2655 | argument, see the Unix manual. This calls send() repeatedly\n\
|
---|
2656 | until all data is sent. If an error occurs, it's impossible\n\
|
---|
2657 | to tell how much data has been sent.");
|
---|
2658 |
|
---|
2659 |
|
---|
2660 | /* s.sendto(data, [flags,] sockaddr) method */
|
---|
2661 |
|
---|
2662 | static PyObject *
|
---|
2663 | sock_sendto(PySocketSockObject *s, PyObject *args)
|
---|
2664 | {
|
---|
2665 | PyObject *addro;
|
---|
2666 | char *buf;
|
---|
2667 | struct sockaddr *addr;
|
---|
2668 | int addrlen, len, n = -1, flags, timeout;
|
---|
2669 |
|
---|
2670 | flags = 0;
|
---|
2671 | if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
|
---|
2672 | PyErr_Clear();
|
---|
2673 | if (!PyArg_ParseTuple(args, "s#iO:sendto",
|
---|
2674 | &buf, &len, &flags, &addro))
|
---|
2675 | return NULL;
|
---|
2676 | }
|
---|
2677 |
|
---|
2678 | if (!getsockaddrarg(s, addro, &addr, &addrlen))
|
---|
2679 | return NULL;
|
---|
2680 |
|
---|
2681 | if (!IS_SELECTABLE(s))
|
---|
2682 | return select_error();
|
---|
2683 |
|
---|
2684 | Py_BEGIN_ALLOW_THREADS
|
---|
2685 | timeout = internal_select(s, 1);
|
---|
2686 | if (!timeout)
|
---|
2687 | n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
|
---|
2688 | Py_END_ALLOW_THREADS
|
---|
2689 |
|
---|
2690 | if (timeout == 1) {
|
---|
2691 | PyErr_SetString(socket_timeout, "timed out");
|
---|
2692 | return NULL;
|
---|
2693 | }
|
---|
2694 | if (n < 0)
|
---|
2695 | return s->errorhandler();
|
---|
2696 | return PyInt_FromLong((long)n);
|
---|
2697 | }
|
---|
2698 |
|
---|
2699 | PyDoc_STRVAR(sendto_doc,
|
---|
2700 | "sendto(data[, flags], address) -> count\n\
|
---|
2701 | \n\
|
---|
2702 | Like send(data, flags) but allows specifying the destination address.\n\
|
---|
2703 | For IP sockets, the address is a pair (hostaddr, port).");
|
---|
2704 |
|
---|
2705 |
|
---|
2706 | /* s.shutdown(how) method */
|
---|
2707 |
|
---|
2708 | static PyObject *
|
---|
2709 | sock_shutdown(PySocketSockObject *s, PyObject *arg)
|
---|
2710 | {
|
---|
2711 | int how;
|
---|
2712 | int res;
|
---|
2713 |
|
---|
2714 | how = PyInt_AsLong(arg);
|
---|
2715 | if (how == -1 && PyErr_Occurred())
|
---|
2716 | return NULL;
|
---|
2717 | Py_BEGIN_ALLOW_THREADS
|
---|
2718 | res = shutdown(s->sock_fd, how);
|
---|
2719 | Py_END_ALLOW_THREADS
|
---|
2720 | if (res < 0)
|
---|
2721 | return s->errorhandler();
|
---|
2722 | Py_INCREF(Py_None);
|
---|
2723 | return Py_None;
|
---|
2724 | }
|
---|
2725 |
|
---|
2726 | PyDoc_STRVAR(shutdown_doc,
|
---|
2727 | "shutdown(flag)\n\
|
---|
2728 | \n\
|
---|
2729 | Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
|
---|
2730 | of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
|
---|
2731 |
|
---|
2732 |
|
---|
2733 | /* List of methods for socket objects */
|
---|
2734 |
|
---|
2735 | static PyMethodDef sock_methods[] = {
|
---|
2736 | {"accept", (PyCFunction)sock_accept, METH_NOARGS,
|
---|
2737 | accept_doc},
|
---|
2738 | {"bind", (PyCFunction)sock_bind, METH_O,
|
---|
2739 | bind_doc},
|
---|
2740 | {"close", (PyCFunction)sock_close, METH_NOARGS,
|
---|
2741 | close_doc},
|
---|
2742 | {"connect", (PyCFunction)sock_connect, METH_O,
|
---|
2743 | connect_doc},
|
---|
2744 | {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
|
---|
2745 | connect_ex_doc},
|
---|
2746 | #ifndef NO_DUP
|
---|
2747 | {"dup", (PyCFunction)sock_dup, METH_NOARGS,
|
---|
2748 | dup_doc},
|
---|
2749 | #endif
|
---|
2750 | {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
|
---|
2751 | fileno_doc},
|
---|
2752 | #ifdef HAVE_GETPEERNAME
|
---|
2753 | {"getpeername", (PyCFunction)sock_getpeername,
|
---|
2754 | METH_NOARGS, getpeername_doc},
|
---|
2755 | #endif
|
---|
2756 | {"getsockname", (PyCFunction)sock_getsockname,
|
---|
2757 | METH_NOARGS, getsockname_doc},
|
---|
2758 | {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
|
---|
2759 | getsockopt_doc},
|
---|
2760 | {"listen", (PyCFunction)sock_listen, METH_O,
|
---|
2761 | listen_doc},
|
---|
2762 | #ifndef NO_DUP
|
---|
2763 | {"makefile", (PyCFunction)sock_makefile, METH_VARARGS,
|
---|
2764 | makefile_doc},
|
---|
2765 | #endif
|
---|
2766 | {"recv", (PyCFunction)sock_recv, METH_VARARGS,
|
---|
2767 | recv_doc},
|
---|
2768 | {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
|
---|
2769 | recv_into_doc},
|
---|
2770 | {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
|
---|
2771 | recvfrom_doc},
|
---|
2772 | {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
|
---|
2773 | recvfrom_into_doc},
|
---|
2774 | {"send", (PyCFunction)sock_send, METH_VARARGS,
|
---|
2775 | send_doc},
|
---|
2776 | {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
|
---|
2777 | sendall_doc},
|
---|
2778 | {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
|
---|
2779 | sendto_doc},
|
---|
2780 | {"setblocking", (PyCFunction)sock_setblocking, METH_O,
|
---|
2781 | setblocking_doc},
|
---|
2782 | {"settimeout", (PyCFunction)sock_settimeout, METH_O,
|
---|
2783 | settimeout_doc},
|
---|
2784 | {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
|
---|
2785 | gettimeout_doc},
|
---|
2786 | {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
|
---|
2787 | setsockopt_doc},
|
---|
2788 | {"shutdown", (PyCFunction)sock_shutdown, METH_O,
|
---|
2789 | shutdown_doc},
|
---|
2790 | #ifdef RISCOS
|
---|
2791 | {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O,
|
---|
2792 | sleeptaskw_doc},
|
---|
2793 | #endif
|
---|
2794 | {NULL, NULL} /* sentinel */
|
---|
2795 | };
|
---|
2796 |
|
---|
2797 | /* SockObject members */
|
---|
2798 | static PyMemberDef sock_memberlist[] = {
|
---|
2799 | {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},
|
---|
2800 | {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},
|
---|
2801 | {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},
|
---|
2802 | {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"},
|
---|
2803 | {0},
|
---|
2804 | };
|
---|
2805 |
|
---|
2806 | /* Deallocate a socket object in response to the last Py_DECREF().
|
---|
2807 | First close the file description. */
|
---|
2808 |
|
---|
2809 | static void
|
---|
2810 | sock_dealloc(PySocketSockObject *s)
|
---|
2811 | {
|
---|
2812 | if (s->sock_fd != -1)
|
---|
2813 | (void) SOCKETCLOSE(s->sock_fd);
|
---|
2814 | s->ob_type->tp_free((PyObject *)s);
|
---|
2815 | }
|
---|
2816 |
|
---|
2817 |
|
---|
2818 | static PyObject *
|
---|
2819 | sock_repr(PySocketSockObject *s)
|
---|
2820 | {
|
---|
2821 | char buf[512];
|
---|
2822 | #if SIZEOF_SOCKET_T > SIZEOF_LONG
|
---|
2823 | if (s->sock_fd > LONG_MAX) {
|
---|
2824 | /* this can occur on Win64, and actually there is a special
|
---|
2825 | ugly printf formatter for decimal pointer length integer
|
---|
2826 | printing, only bother if necessary*/
|
---|
2827 | PyErr_SetString(PyExc_OverflowError,
|
---|
2828 | "no printf formatter to display "
|
---|
2829 | "the socket descriptor in decimal");
|
---|
2830 | return NULL;
|
---|
2831 | }
|
---|
2832 | #endif
|
---|
2833 | PyOS_snprintf(
|
---|
2834 | buf, sizeof(buf),
|
---|
2835 | "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",
|
---|
2836 | (long)s->sock_fd, s->sock_family,
|
---|
2837 | s->sock_type,
|
---|
2838 | s->sock_proto);
|
---|
2839 | return PyString_FromString(buf);
|
---|
2840 | }
|
---|
2841 |
|
---|
2842 |
|
---|
2843 | /* Create a new, uninitialized socket object. */
|
---|
2844 |
|
---|
2845 | static PyObject *
|
---|
2846 | sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
---|
2847 | {
|
---|
2848 | PyObject *new;
|
---|
2849 |
|
---|
2850 | new = type->tp_alloc(type, 0);
|
---|
2851 | if (new != NULL) {
|
---|
2852 | ((PySocketSockObject *)new)->sock_fd = -1;
|
---|
2853 | ((PySocketSockObject *)new)->sock_timeout = -1.0;
|
---|
2854 | ((PySocketSockObject *)new)->errorhandler = &set_error;
|
---|
2855 | }
|
---|
2856 | return new;
|
---|
2857 | }
|
---|
2858 |
|
---|
2859 |
|
---|
2860 | /* Initialize a new socket object. */
|
---|
2861 |
|
---|
2862 | /*ARGSUSED*/
|
---|
2863 | static int
|
---|
2864 | sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
|
---|
2865 | {
|
---|
2866 | PySocketSockObject *s = (PySocketSockObject *)self;
|
---|
2867 | SOCKET_T fd;
|
---|
2868 | int family = AF_INET, type = SOCK_STREAM, proto = 0;
|
---|
2869 | static char *keywords[] = {"family", "type", "proto", 0};
|
---|
2870 |
|
---|
2871 | if (!PyArg_ParseTupleAndKeywords(args, kwds,
|
---|
2872 | "|iii:socket", keywords,
|
---|
2873 | &family, &type, &proto))
|
---|
2874 | return -1;
|
---|
2875 |
|
---|
2876 | Py_BEGIN_ALLOW_THREADS
|
---|
2877 | fd = socket(family, type, proto);
|
---|
2878 | Py_END_ALLOW_THREADS
|
---|
2879 |
|
---|
2880 | #ifdef MS_WINDOWS
|
---|
2881 | if (fd == INVALID_SOCKET)
|
---|
2882 | #else
|
---|
2883 | if (fd < 0)
|
---|
2884 | #endif
|
---|
2885 | {
|
---|
2886 | set_error();
|
---|
2887 | return -1;
|
---|
2888 | }
|
---|
2889 | init_sockobject(s, fd, family, type, proto);
|
---|
2890 |
|
---|
2891 | return 0;
|
---|
2892 |
|
---|
2893 | }
|
---|
2894 |
|
---|
2895 |
|
---|
2896 | /* Type object for socket objects. */
|
---|
2897 |
|
---|
2898 | static PyTypeObject sock_type = {
|
---|
2899 | PyObject_HEAD_INIT(0) /* Must fill in type value later */
|
---|
2900 | 0, /* ob_size */
|
---|
2901 | "_socket.socket", /* tp_name */
|
---|
2902 | sizeof(PySocketSockObject), /* tp_basicsize */
|
---|
2903 | 0, /* tp_itemsize */
|
---|
2904 | (destructor)sock_dealloc, /* tp_dealloc */
|
---|
2905 | 0, /* tp_print */
|
---|
2906 | 0, /* tp_getattr */
|
---|
2907 | 0, /* tp_setattr */
|
---|
2908 | 0, /* tp_compare */
|
---|
2909 | (reprfunc)sock_repr, /* tp_repr */
|
---|
2910 | 0, /* tp_as_number */
|
---|
2911 | 0, /* tp_as_sequence */
|
---|
2912 | 0, /* tp_as_mapping */
|
---|
2913 | 0, /* tp_hash */
|
---|
2914 | 0, /* tp_call */
|
---|
2915 | 0, /* tp_str */
|
---|
2916 | PyObject_GenericGetAttr, /* tp_getattro */
|
---|
2917 | 0, /* tp_setattro */
|
---|
2918 | 0, /* tp_as_buffer */
|
---|
2919 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
|
---|
2920 | sock_doc, /* tp_doc */
|
---|
2921 | 0, /* tp_traverse */
|
---|
2922 | 0, /* tp_clear */
|
---|
2923 | 0, /* tp_richcompare */
|
---|
2924 | 0, /* tp_weaklistoffset */
|
---|
2925 | 0, /* tp_iter */
|
---|
2926 | 0, /* tp_iternext */
|
---|
2927 | sock_methods, /* tp_methods */
|
---|
2928 | sock_memberlist, /* tp_members */
|
---|
2929 | 0, /* tp_getset */
|
---|
2930 | 0, /* tp_base */
|
---|
2931 | 0, /* tp_dict */
|
---|
2932 | 0, /* tp_descr_get */
|
---|
2933 | 0, /* tp_descr_set */
|
---|
2934 | 0, /* tp_dictoffset */
|
---|
2935 | sock_initobj, /* tp_init */
|
---|
2936 | PyType_GenericAlloc, /* tp_alloc */
|
---|
2937 | sock_new, /* tp_new */
|
---|
2938 | PyObject_Del, /* tp_free */
|
---|
2939 | };
|
---|
2940 |
|
---|
2941 |
|
---|
2942 | /* Python interface to gethostname(). */
|
---|
2943 |
|
---|
2944 | /*ARGSUSED*/
|
---|
2945 | static PyObject *
|
---|
2946 | socket_gethostname(PyObject *self, PyObject *unused)
|
---|
2947 | {
|
---|
2948 | char buf[1024];
|
---|
2949 | int res;
|
---|
2950 | Py_BEGIN_ALLOW_THREADS
|
---|
2951 | res = gethostname(buf, (int) sizeof buf - 1);
|
---|
2952 | Py_END_ALLOW_THREADS
|
---|
2953 | if (res < 0)
|
---|
2954 | return set_error();
|
---|
2955 | buf[sizeof buf - 1] = '\0';
|
---|
2956 | return PyString_FromString(buf);
|
---|
2957 | }
|
---|
2958 |
|
---|
2959 | PyDoc_STRVAR(gethostname_doc,
|
---|
2960 | "gethostname() -> string\n\
|
---|
2961 | \n\
|
---|
2962 | Return the current host name.");
|
---|
2963 |
|
---|
2964 |
|
---|
2965 | /* Python interface to gethostbyname(name). */
|
---|
2966 |
|
---|
2967 | /*ARGSUSED*/
|
---|
2968 | static PyObject *
|
---|
2969 | socket_gethostbyname(PyObject *self, PyObject *args)
|
---|
2970 | {
|
---|
2971 | char *name;
|
---|
2972 | sock_addr_t addrbuf;
|
---|
2973 |
|
---|
2974 | if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
|
---|
2975 | return NULL;
|
---|
2976 | if (setipaddr(name, (struct sockaddr *)&addrbuf, sizeof(addrbuf), AF_INET) < 0)
|
---|
2977 | return NULL;
|
---|
2978 | return makeipaddr((struct sockaddr *)&addrbuf,
|
---|
2979 | sizeof(struct sockaddr_in));
|
---|
2980 | }
|
---|
2981 |
|
---|
2982 | PyDoc_STRVAR(gethostbyname_doc,
|
---|
2983 | "gethostbyname(host) -> address\n\
|
---|
2984 | \n\
|
---|
2985 | Return the IP address (a string of the form '255.255.255.255') for a host.");
|
---|
2986 |
|
---|
2987 |
|
---|
2988 | /* Convenience function common to gethostbyname_ex and gethostbyaddr */
|
---|
2989 |
|
---|
2990 | static PyObject *
|
---|
2991 | gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
|
---|
2992 | {
|
---|
2993 | char **pch;
|
---|
2994 | PyObject *rtn_tuple = (PyObject *)NULL;
|
---|
2995 | PyObject *name_list = (PyObject *)NULL;
|
---|
2996 | PyObject *addr_list = (PyObject *)NULL;
|
---|
2997 | PyObject *tmp;
|
---|
2998 |
|
---|
2999 | if (h == NULL) {
|
---|
3000 | /* Let's get real error message to return */
|
---|
3001 | #ifndef RISCOS
|
---|
3002 | set_herror(h_errno);
|
---|
3003 | #else
|
---|
3004 | PyErr_SetString(socket_error, "host not found");
|
---|
3005 | #endif
|
---|
3006 | return NULL;
|
---|
3007 | }
|
---|
3008 |
|
---|
3009 | if (h->h_addrtype != af) {
|
---|
3010 | #ifdef HAVE_STRERROR
|
---|
3011 | /* Let's get real error message to return */
|
---|
3012 | PyErr_SetString(socket_error,
|
---|
3013 | (char *)strerror(EAFNOSUPPORT));
|
---|
3014 | #else
|
---|
3015 | PyErr_SetString(
|
---|
3016 | socket_error,
|
---|
3017 | "Address family not supported by protocol family");
|
---|
3018 | #endif
|
---|
3019 | return NULL;
|
---|
3020 | }
|
---|
3021 |
|
---|
3022 | switch (af) {
|
---|
3023 |
|
---|
3024 | case AF_INET:
|
---|
3025 | if (alen < sizeof(struct sockaddr_in))
|
---|
3026 | return NULL;
|
---|
3027 | break;
|
---|
3028 |
|
---|
3029 | #ifdef ENABLE_IPV6
|
---|
3030 | case AF_INET6:
|
---|
3031 | if (alen < sizeof(struct sockaddr_in6))
|
---|
3032 | return NULL;
|
---|
3033 | break;
|
---|
3034 | #endif
|
---|
3035 |
|
---|
3036 | }
|
---|
3037 |
|
---|
3038 | if ((name_list = PyList_New(0)) == NULL)
|
---|
3039 | goto err;
|
---|
3040 |
|
---|
3041 | if ((addr_list = PyList_New(0)) == NULL)
|
---|
3042 | goto err;
|
---|
3043 |
|
---|
3044 | /* SF #1511317: h_aliases can be NULL */
|
---|
3045 | if (h->h_aliases) {
|
---|
3046 | for (pch = h->h_aliases; *pch != NULL; pch++) {
|
---|
3047 | int status;
|
---|
3048 | tmp = PyString_FromString(*pch);
|
---|
3049 | if (tmp == NULL)
|
---|
3050 | goto err;
|
---|
3051 |
|
---|
3052 | status = PyList_Append(name_list, tmp);
|
---|
3053 | Py_DECREF(tmp);
|
---|
3054 |
|
---|
3055 | if (status)
|
---|
3056 | goto err;
|
---|
3057 | }
|
---|
3058 | }
|
---|
3059 |
|
---|
3060 | for (pch = h->h_addr_list; *pch != NULL; pch++) {
|
---|
3061 | int status;
|
---|
3062 |
|
---|
3063 | switch (af) {
|
---|
3064 |
|
---|
3065 | case AF_INET:
|
---|
3066 | {
|
---|
3067 | struct sockaddr_in sin;
|
---|
3068 | memset(&sin, 0, sizeof(sin));
|
---|
3069 | sin.sin_family = af;
|
---|
3070 | #ifdef HAVE_SOCKADDR_SA_LEN
|
---|
3071 | sin.sin_len = sizeof(sin);
|
---|
3072 | #endif
|
---|
3073 | memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
|
---|
3074 | tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
|
---|
3075 |
|
---|
3076 | if (pch == h->h_addr_list && alen >= sizeof(sin))
|
---|
3077 | memcpy((char *) addr, &sin, sizeof(sin));
|
---|
3078 | break;
|
---|
3079 | }
|
---|
3080 |
|
---|
3081 | #ifdef ENABLE_IPV6
|
---|
3082 | case AF_INET6:
|
---|
3083 | {
|
---|
3084 | struct sockaddr_in6 sin6;
|
---|
3085 | memset(&sin6, 0, sizeof(sin6));
|
---|
3086 | sin6.sin6_family = af;
|
---|
3087 | #ifdef HAVE_SOCKADDR_SA_LEN
|
---|
3088 | sin6.sin6_len = sizeof(sin6);
|
---|
3089 | #endif
|
---|
3090 | memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
|
---|
3091 | tmp = makeipaddr((struct sockaddr *)&sin6,
|
---|
3092 | sizeof(sin6));
|
---|
3093 |
|
---|
3094 | if (pch == h->h_addr_list && alen >= sizeof(sin6))
|
---|
3095 | memcpy((char *) addr, &sin6, sizeof(sin6));
|
---|
3096 | break;
|
---|
3097 | }
|
---|
3098 | #endif
|
---|
3099 |
|
---|
3100 | default: /* can't happen */
|
---|
3101 | PyErr_SetString(socket_error,
|
---|
3102 | "unsupported address family");
|
---|
3103 | return NULL;
|
---|
3104 | }
|
---|
3105 |
|
---|
3106 | if (tmp == NULL)
|
---|
3107 | goto err;
|
---|
3108 |
|
---|
3109 | status = PyList_Append(addr_list, tmp);
|
---|
3110 | Py_DECREF(tmp);
|
---|
3111 |
|
---|
3112 | if (status)
|
---|
3113 | goto err;
|
---|
3114 | }
|
---|
3115 |
|
---|
3116 | rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
|
---|
3117 |
|
---|
3118 | err:
|
---|
3119 | Py_XDECREF(name_list);
|
---|
3120 | Py_XDECREF(addr_list);
|
---|
3121 | return rtn_tuple;
|
---|
3122 | }
|
---|
3123 |
|
---|
3124 |
|
---|
3125 | /* Python interface to gethostbyname_ex(name). */
|
---|
3126 |
|
---|
3127 | /*ARGSUSED*/
|
---|
3128 | static PyObject *
|
---|
3129 | socket_gethostbyname_ex(PyObject *self, PyObject *args)
|
---|
3130 | {
|
---|
3131 | char *name;
|
---|
3132 | struct hostent *h;
|
---|
3133 | #ifdef ENABLE_IPV6
|
---|
3134 | struct sockaddr_storage addr;
|
---|
3135 | #else
|
---|
3136 | struct sockaddr_in addr;
|
---|
3137 | #endif
|
---|
3138 | struct sockaddr *sa;
|
---|
3139 | PyObject *ret;
|
---|
3140 | #ifdef HAVE_GETHOSTBYNAME_R
|
---|
3141 | struct hostent hp_allocated;
|
---|
3142 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG
|
---|
3143 | struct hostent_data data;
|
---|
3144 | #else
|
---|
3145 | char buf[16384];
|
---|
3146 | int buf_len = (sizeof buf) - 1;
|
---|
3147 | int errnop;
|
---|
3148 | #endif
|
---|
3149 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
|
---|
3150 | int result;
|
---|
3151 | #endif
|
---|
3152 | #endif /* HAVE_GETHOSTBYNAME_R */
|
---|
3153 |
|
---|
3154 | if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
|
---|
3155 | return NULL;
|
---|
3156 | if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
|
---|
3157 | return NULL;
|
---|
3158 | Py_BEGIN_ALLOW_THREADS
|
---|
3159 | #ifdef HAVE_GETHOSTBYNAME_R
|
---|
3160 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
|
---|
3161 | result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
|
---|
3162 | &h, &errnop);
|
---|
3163 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
|
---|
3164 | h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
|
---|
3165 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */
|
---|
3166 | memset((void *) &data, '\0', sizeof(data));
|
---|
3167 | result = gethostbyname_r(name, &hp_allocated, &data);
|
---|
3168 | h = (result != 0) ? NULL : &hp_allocated;
|
---|
3169 | #endif
|
---|
3170 | #else /* not HAVE_GETHOSTBYNAME_R */
|
---|
3171 | #ifdef USE_GETHOSTBYNAME_LOCK
|
---|
3172 | PyThread_acquire_lock(netdb_lock, 1);
|
---|
3173 | #endif
|
---|
3174 | h = gethostbyname(name);
|
---|
3175 | #endif /* HAVE_GETHOSTBYNAME_R */
|
---|
3176 | Py_END_ALLOW_THREADS
|
---|
3177 | /* Some C libraries would require addr.__ss_family instead of
|
---|
3178 | addr.ss_family.
|
---|
3179 | Therefore, we cast the sockaddr_storage into sockaddr to
|
---|
3180 | access sa_family. */
|
---|
3181 | sa = (struct sockaddr*)&addr;
|
---|
3182 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
|
---|
3183 | sa->sa_family);
|
---|
3184 | #ifdef USE_GETHOSTBYNAME_LOCK
|
---|
3185 | PyThread_release_lock(netdb_lock);
|
---|
3186 | #endif
|
---|
3187 | return ret;
|
---|
3188 | }
|
---|
3189 |
|
---|
3190 | PyDoc_STRVAR(ghbn_ex_doc,
|
---|
3191 | "gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
|
---|
3192 | \n\
|
---|
3193 | Return the true host name, a list of aliases, and a list of IP addresses,\n\
|
---|
3194 | for a host. The host argument is a string giving a host name or IP number.");
|
---|
3195 |
|
---|
3196 |
|
---|
3197 | /* Python interface to gethostbyaddr(IP). */
|
---|
3198 |
|
---|
3199 | /*ARGSUSED*/
|
---|
3200 | static PyObject *
|
---|
3201 | socket_gethostbyaddr(PyObject *self, PyObject *args)
|
---|
3202 | {
|
---|
3203 | #ifdef ENABLE_IPV6
|
---|
3204 | struct sockaddr_storage addr;
|
---|
3205 | #else
|
---|
3206 | struct sockaddr_in addr;
|
---|
3207 | #endif
|
---|
3208 | struct sockaddr *sa = (struct sockaddr *)&addr;
|
---|
3209 | char *ip_num;
|
---|
3210 | struct hostent *h;
|
---|
3211 | PyObject *ret;
|
---|
3212 | #ifdef HAVE_GETHOSTBYNAME_R
|
---|
3213 | struct hostent hp_allocated;
|
---|
3214 | #ifdef HAVE_GETHOSTBYNAME_R_3_ARG
|
---|
3215 | struct hostent_data data;
|
---|
3216 | #else
|
---|
3217 | char buf[16384];
|
---|
3218 | int buf_len = (sizeof buf) - 1;
|
---|
3219 | int errnop;
|
---|
3220 | #endif
|
---|
3221 | #if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
|
---|
3222 | int result;
|
---|
3223 | #endif
|
---|
3224 | #endif /* HAVE_GETHOSTBYNAME_R */
|
---|
3225 | char *ap;
|
---|
3226 | int al;
|
---|
3227 | int af;
|
---|
3228 |
|
---|
3229 | if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
|
---|
3230 | return NULL;
|
---|
3231 | af = AF_UNSPEC;
|
---|
3232 | if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
|
---|
3233 | return NULL;
|
---|
3234 | af = sa->sa_family;
|
---|
3235 | ap = NULL;
|
---|
3236 | al = 0;
|
---|
3237 | switch (af) {
|
---|
3238 | case AF_INET:
|
---|
3239 | ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
|
---|
3240 | al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
|
---|
3241 | break;
|
---|
3242 | #ifdef ENABLE_IPV6
|
---|
3243 | case AF_INET6:
|
---|
3244 | ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
|
---|
3245 | al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
|
---|
3246 | break;
|
---|
3247 | #endif
|
---|
3248 | default:
|
---|
3249 | PyErr_SetString(socket_error, "unsupported address family");
|
---|
3250 | return NULL;
|
---|
3251 | }
|
---|
3252 | Py_BEGIN_ALLOW_THREADS
|
---|
3253 | #ifdef HAVE_GETHOSTBYNAME_R
|
---|
3254 | #if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
|
---|
3255 | result = gethostbyaddr_r(ap, al, af,
|
---|
3256 | &hp_allocated, buf, buf_len,
|
---|
3257 | &h, &errnop);
|
---|
3258 | #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
|
---|
3259 | h = gethostbyaddr_r(ap, al, af,
|
---|
3260 | &hp_allocated, buf, buf_len, &errnop);
|
---|
3261 | #else /* HAVE_GETHOSTBYNAME_R_3_ARG */
|
---|
3262 | memset((void *) &data, '\0', sizeof(data));
|
---|
3263 | result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
|
---|
3264 | h = (result != 0) ? NULL : &hp_allocated;
|
---|
3265 | #endif
|
---|
3266 | #else /* not HAVE_GETHOSTBYNAME_R */
|
---|
3267 | #ifdef USE_GETHOSTBYNAME_LOCK
|
---|
3268 | PyThread_acquire_lock(netdb_lock, 1);
|
---|
3269 | #endif
|
---|
3270 | h = gethostbyaddr(ap, al, af);
|
---|
3271 | #endif /* HAVE_GETHOSTBYNAME_R */
|
---|
3272 | Py_END_ALLOW_THREADS
|
---|
3273 | ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
|
---|
3274 | #ifdef USE_GETHOSTBYNAME_LOCK
|
---|
3275 | PyThread_release_lock(netdb_lock);
|
---|
3276 | #endif
|
---|
3277 | return ret;
|
---|
3278 | }
|
---|
3279 |
|
---|
3280 | PyDoc_STRVAR(gethostbyaddr_doc,
|
---|
3281 | "gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
|
---|
3282 | \n\
|
---|
3283 | Return the true host name, a list of aliases, and a list of IP addresses,\n\
|
---|
3284 | for a host. The host argument is a string giving a host name or IP number.");
|
---|
3285 |
|
---|
3286 |
|
---|
3287 | /* Python interface to getservbyname(name).
|
---|
3288 | This only returns the port number, since the other info is already
|
---|
3289 | known or not useful (like the list of aliases). */
|
---|
3290 |
|
---|
3291 | /*ARGSUSED*/
|
---|
3292 | static PyObject *
|
---|
3293 | socket_getservbyname(PyObject *self, PyObject *args)
|
---|
3294 | {
|
---|
3295 | char *name, *proto=NULL;
|
---|
3296 | struct servent *sp;
|
---|
3297 | if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
|
---|
3298 | return NULL;
|
---|
3299 | Py_BEGIN_ALLOW_THREADS
|
---|
3300 | sp = getservbyname(name, proto);
|
---|
3301 | Py_END_ALLOW_THREADS
|
---|
3302 | if (sp == NULL) {
|
---|
3303 | PyErr_SetString(socket_error, "service/proto not found");
|
---|
3304 | return NULL;
|
---|
3305 | }
|
---|
3306 | return PyInt_FromLong((long) ntohs(sp->s_port));
|
---|
3307 | }
|
---|
3308 |
|
---|
3309 | PyDoc_STRVAR(getservbyname_doc,
|
---|
3310 | "getservbyname(servicename[, protocolname]) -> integer\n\
|
---|
3311 | \n\
|
---|
3312 | Return a port number from a service name and protocol name.\n\
|
---|
3313 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\
|
---|
3314 | otherwise any protocol will match.");
|
---|
3315 |
|
---|
3316 |
|
---|
3317 | /* Python interface to getservbyport(port).
|
---|
3318 | This only returns the service name, since the other info is already
|
---|
3319 | known or not useful (like the list of aliases). */
|
---|
3320 |
|
---|
3321 | /*ARGSUSED*/
|
---|
3322 | static PyObject *
|
---|
3323 | socket_getservbyport(PyObject *self, PyObject *args)
|
---|
3324 | {
|
---|
3325 | unsigned short port;
|
---|
3326 | char *proto=NULL;
|
---|
3327 | struct servent *sp;
|
---|
3328 | if (!PyArg_ParseTuple(args, "H|s:getservbyport", &port, &proto))
|
---|
3329 | return NULL;
|
---|
3330 | Py_BEGIN_ALLOW_THREADS
|
---|
3331 | sp = getservbyport(htons(port), proto);
|
---|
3332 | Py_END_ALLOW_THREADS
|
---|
3333 | if (sp == NULL) {
|
---|
3334 | PyErr_SetString(socket_error, "port/proto not found");
|
---|
3335 | return NULL;
|
---|
3336 | }
|
---|
3337 | return PyString_FromString(sp->s_name);
|
---|
3338 | }
|
---|
3339 |
|
---|
3340 | PyDoc_STRVAR(getservbyport_doc,
|
---|
3341 | "getservbyport(port[, protocolname]) -> string\n\
|
---|
3342 | \n\
|
---|
3343 | Return the service name from a port number and protocol name.\n\
|
---|
3344 | The optional protocol name, if given, should be 'tcp' or 'udp',\n\
|
---|
3345 | otherwise any protocol will match.");
|
---|
3346 |
|
---|
3347 | /* Python interface to getprotobyname(name).
|
---|
3348 | This only returns the protocol number, since the other info is
|
---|
3349 | already known or not useful (like the list of aliases). */
|
---|
3350 |
|
---|
3351 | /*ARGSUSED*/
|
---|
3352 | static PyObject *
|
---|
3353 | socket_getprotobyname(PyObject *self, PyObject *args)
|
---|
3354 | {
|
---|
3355 | char *name;
|
---|
3356 | struct protoent *sp;
|
---|
3357 | #ifdef __BEOS__
|
---|
3358 | /* Not available in BeOS yet. - [cjh] */
|
---|
3359 | PyErr_SetString(socket_error, "getprotobyname not supported");
|
---|
3360 | return NULL;
|
---|
3361 | #else
|
---|
3362 | if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
|
---|
3363 | return NULL;
|
---|
3364 | Py_BEGIN_ALLOW_THREADS
|
---|
3365 | sp = getprotobyname(name);
|
---|
3366 | Py_END_ALLOW_THREADS
|
---|
3367 | if (sp == NULL) {
|
---|
3368 | PyErr_SetString(socket_error, "protocol not found");
|
---|
3369 | return NULL;
|
---|
3370 | }
|
---|
3371 | return PyInt_FromLong((long) sp->p_proto);
|
---|
3372 | #endif
|
---|
3373 | }
|
---|
3374 |
|
---|
3375 | PyDoc_STRVAR(getprotobyname_doc,
|
---|
3376 | "getprotobyname(name) -> integer\n\
|
---|
3377 | \n\
|
---|
3378 | Return the protocol number for the named protocol. (Rarely used.)");
|
---|
3379 |
|
---|
3380 |
|
---|
3381 | #ifdef HAVE_SOCKETPAIR
|
---|
3382 | /* Create a pair of sockets using the socketpair() function.
|
---|
3383 | Arguments as for socket() except the default family is AF_UNIX if
|
---|
3384 | defined on the platform; otherwise, the default is AF_INET. */
|
---|
3385 |
|
---|
3386 | /*ARGSUSED*/
|
---|
3387 | static PyObject *
|
---|
3388 | socket_socketpair(PyObject *self, PyObject *args)
|
---|
3389 | {
|
---|
3390 | PySocketSockObject *s0 = NULL, *s1 = NULL;
|
---|
3391 | SOCKET_T sv[2];
|
---|
3392 | int family, type = SOCK_STREAM, proto = 0;
|
---|
3393 | PyObject *res = NULL;
|
---|
3394 |
|
---|
3395 | #if defined(AF_UNIX)
|
---|
3396 | family = AF_UNIX;
|
---|
3397 | #else
|
---|
3398 | family = AF_INET;
|
---|
3399 | #endif
|
---|
3400 | if (!PyArg_ParseTuple(args, "|iii:socketpair",
|
---|
3401 | &family, &type, &proto))
|
---|
3402 | return NULL;
|
---|
3403 | /* Create a pair of socket fds */
|
---|
3404 | if (socketpair(family, type, proto, sv) < 0)
|
---|
3405 | return set_error();
|
---|
3406 | s0 = new_sockobject(sv[0], family, type, proto);
|
---|
3407 | if (s0 == NULL)
|
---|
3408 | goto finally;
|
---|
3409 | s1 = new_sockobject(sv[1], family, type, proto);
|
---|
3410 | if (s1 == NULL)
|
---|
3411 | goto finally;
|
---|
3412 | res = PyTuple_Pack(2, s0, s1);
|
---|
3413 |
|
---|
3414 | finally:
|
---|
3415 | if (res == NULL) {
|
---|
3416 | if (s0 == NULL)
|
---|
3417 | SOCKETCLOSE(sv[0]);
|
---|
3418 | if (s1 == NULL)
|
---|
3419 | SOCKETCLOSE(sv[1]);
|
---|
3420 | }
|
---|
3421 | Py_XDECREF(s0);
|
---|
3422 | Py_XDECREF(s1);
|
---|
3423 | return res;
|
---|
3424 | }
|
---|
3425 |
|
---|
3426 | PyDoc_STRVAR(socketpair_doc,
|
---|
3427 | "socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\
|
---|
3428 | \n\
|
---|
3429 | Create a pair of socket objects from the sockets returned by the platform\n\
|
---|
3430 | socketpair() function.\n\
|
---|
3431 | The arguments are the same as for socket() except the default family is\n\
|
---|
3432 | AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
|
---|
3433 |
|
---|
3434 | #endif /* HAVE_SOCKETPAIR */
|
---|
3435 |
|
---|
3436 |
|
---|
3437 | #ifndef NO_DUP
|
---|
3438 | /* Create a socket object from a numeric file description.
|
---|
3439 | Useful e.g. if stdin is a socket.
|
---|
3440 | Additional arguments as for socket(). */
|
---|
3441 |
|
---|
3442 | /*ARGSUSED*/
|
---|
3443 | static PyObject *
|
---|
3444 | socket_fromfd(PyObject *self, PyObject *args)
|
---|
3445 | {
|
---|
3446 | PySocketSockObject *s;
|
---|
3447 | SOCKET_T fd;
|
---|
3448 | int family, type, proto = 0;
|
---|
3449 | if (!PyArg_ParseTuple(args, "iii|i:fromfd",
|
---|
3450 | &fd, &family, &type, &proto))
|
---|
3451 | return NULL;
|
---|
3452 | /* Dup the fd so it and the socket can be closed independently */
|
---|
3453 | fd = dup(fd);
|
---|
3454 | if (fd < 0)
|
---|
3455 | return set_error();
|
---|
3456 | s = new_sockobject(fd, family, type, proto);
|
---|
3457 | return (PyObject *) s;
|
---|
3458 | }
|
---|
3459 |
|
---|
3460 | PyDoc_STRVAR(fromfd_doc,
|
---|
3461 | "fromfd(fd, family, type[, proto]) -> socket object\n\
|
---|
3462 | \n\
|
---|
3463 | Create a socket object from a duplicate of the given\n\
|
---|
3464 | file descriptor.\n\
|
---|
3465 | The remaining arguments are the same as for socket().");
|
---|
3466 |
|
---|
3467 | #endif /* NO_DUP */
|
---|
3468 |
|
---|
3469 |
|
---|
3470 | static PyObject *
|
---|
3471 | socket_ntohs(PyObject *self, PyObject *args)
|
---|
3472 | {
|
---|
3473 | int x1, x2;
|
---|
3474 |
|
---|
3475 | if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
|
---|
3476 | return NULL;
|
---|
3477 | }
|
---|
3478 | x2 = (int)ntohs((short)x1);
|
---|
3479 | return PyInt_FromLong(x2);
|
---|
3480 | }
|
---|
3481 |
|
---|
3482 | PyDoc_STRVAR(ntohs_doc,
|
---|
3483 | "ntohs(integer) -> integer\n\
|
---|
3484 | \n\
|
---|
3485 | Convert a 16-bit integer from network to host byte order.");
|
---|
3486 |
|
---|
3487 |
|
---|
3488 | static PyObject *
|
---|
3489 | socket_ntohl(PyObject *self, PyObject *arg)
|
---|
3490 | {
|
---|
3491 | unsigned long x;
|
---|
3492 |
|
---|
3493 | if (PyInt_Check(arg)) {
|
---|
3494 | x = PyInt_AS_LONG(arg);
|
---|
3495 | if (x == (unsigned long) -1 && PyErr_Occurred())
|
---|
3496 | return NULL;
|
---|
3497 | }
|
---|
3498 | else if (PyLong_Check(arg)) {
|
---|
3499 | x = PyLong_AsUnsignedLong(arg);
|
---|
3500 | if (x == (unsigned long) -1 && PyErr_Occurred())
|
---|
3501 | return NULL;
|
---|
3502 | #if SIZEOF_LONG > 4
|
---|
3503 | {
|
---|
3504 | unsigned long y;
|
---|
3505 | /* only want the trailing 32 bits */
|
---|
3506 | y = x & 0xFFFFFFFFUL;
|
---|
3507 | if (y ^ x)
|
---|
3508 | return PyErr_Format(PyExc_OverflowError,
|
---|
3509 | "long int larger than 32 bits");
|
---|
3510 | x = y;
|
---|
3511 | }
|
---|
3512 | #endif
|
---|
3513 | }
|
---|
3514 | else
|
---|
3515 | return PyErr_Format(PyExc_TypeError,
|
---|
3516 | "expected int/long, %s found",
|
---|
3517 | arg->ob_type->tp_name);
|
---|
3518 | if (x == (unsigned long) -1 && PyErr_Occurred())
|
---|
3519 | return NULL;
|
---|
3520 | return PyInt_FromLong(ntohl(x));
|
---|
3521 | }
|
---|
3522 |
|
---|
3523 | PyDoc_STRVAR(ntohl_doc,
|
---|
3524 | "ntohl(integer) -> integer\n\
|
---|
3525 | \n\
|
---|
3526 | Convert a 32-bit integer from network to host byte order.");
|
---|
3527 |
|
---|
3528 |
|
---|
3529 | static PyObject *
|
---|
3530 | socket_htons(PyObject *self, PyObject *args)
|
---|
3531 | {
|
---|
3532 | int x1, x2;
|
---|
3533 |
|
---|
3534 | if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
|
---|
3535 | return NULL;
|
---|
3536 | }
|
---|
3537 | x2 = (int)htons((short)x1);
|
---|
3538 | return PyInt_FromLong(x2);
|
---|
3539 | }
|
---|
3540 |
|
---|
3541 | PyDoc_STRVAR(htons_doc,
|
---|
3542 | "htons(integer) -> integer\n\
|
---|
3543 | \n\
|
---|
3544 | Convert a 16-bit integer from host to network byte order.");
|
---|
3545 |
|
---|
3546 |
|
---|
3547 | static PyObject *
|
---|
3548 | socket_htonl(PyObject *self, PyObject *arg)
|
---|
3549 | {
|
---|
3550 | unsigned long x;
|
---|
3551 |
|
---|
3552 | if (PyInt_Check(arg)) {
|
---|
3553 | x = PyInt_AS_LONG(arg);
|
---|
3554 | if (x == (unsigned long) -1 && PyErr_Occurred())
|
---|
3555 | return NULL;
|
---|
3556 | }
|
---|
3557 | else if (PyLong_Check(arg)) {
|
---|
3558 | x = PyLong_AsUnsignedLong(arg);
|
---|
3559 | if (x == (unsigned long) -1 && PyErr_Occurred())
|
---|
3560 | return NULL;
|
---|
3561 | #if SIZEOF_LONG > 4
|
---|
3562 | {
|
---|
3563 | unsigned long y;
|
---|
3564 | /* only want the trailing 32 bits */
|
---|
3565 | y = x & 0xFFFFFFFFUL;
|
---|
3566 | if (y ^ x)
|
---|
3567 | return PyErr_Format(PyExc_OverflowError,
|
---|
3568 | "long int larger than 32 bits");
|
---|
3569 | x = y;
|
---|
3570 | }
|
---|
3571 | #endif
|
---|
3572 | }
|
---|
3573 | else
|
---|
3574 | return PyErr_Format(PyExc_TypeError,
|
---|
3575 | "expected int/long, %s found",
|
---|
3576 | arg->ob_type->tp_name);
|
---|
3577 | return PyInt_FromLong(htonl(x));
|
---|
3578 | }
|
---|
3579 |
|
---|
3580 | PyDoc_STRVAR(htonl_doc,
|
---|
3581 | "htonl(integer) -> integer\n\
|
---|
3582 | \n\
|
---|
3583 | Convert a 32-bit integer from host to network byte order.");
|
---|
3584 |
|
---|
3585 | /* socket.inet_aton() and socket.inet_ntoa() functions. */
|
---|
3586 |
|
---|
3587 | PyDoc_STRVAR(inet_aton_doc,
|
---|
3588 | "inet_aton(string) -> packed 32-bit IP representation\n\
|
---|
3589 | \n\
|
---|
3590 | Convert an IP address in string format (123.45.67.89) to the 32-bit packed\n\
|
---|
3591 | binary format used in low-level network functions.");
|
---|
3592 |
|
---|
3593 | static PyObject*
|
---|
3594 | socket_inet_aton(PyObject *self, PyObject *args)
|
---|
3595 | {
|
---|
3596 | #ifndef INADDR_NONE
|
---|
3597 | #define INADDR_NONE (-1)
|
---|
3598 | #endif
|
---|
3599 | #ifdef HAVE_INET_ATON
|
---|
3600 | struct in_addr buf;
|
---|
3601 | #endif
|
---|
3602 |
|
---|
3603 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
|
---|
3604 | /* Have to use inet_addr() instead */
|
---|
3605 | unsigned long packed_addr;
|
---|
3606 | #endif
|
---|
3607 | char *ip_addr;
|
---|
3608 |
|
---|
3609 | if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
|
---|
3610 | return NULL;
|
---|
3611 |
|
---|
3612 |
|
---|
3613 | #ifdef HAVE_INET_ATON
|
---|
3614 |
|
---|
3615 | #ifdef USE_INET_ATON_WEAKLINK
|
---|
3616 | if (inet_aton != NULL) {
|
---|
3617 | #endif
|
---|
3618 | if (inet_aton(ip_addr, &buf))
|
---|
3619 | return PyString_FromStringAndSize((char *)(&buf),
|
---|
3620 | sizeof(buf));
|
---|
3621 |
|
---|
3622 | PyErr_SetString(socket_error,
|
---|
3623 | "illegal IP address string passed to inet_aton");
|
---|
3624 | return NULL;
|
---|
3625 |
|
---|
3626 | #ifdef USE_INET_ATON_WEAKLINK
|
---|
3627 | } else {
|
---|
3628 | #endif
|
---|
3629 |
|
---|
3630 | #endif
|
---|
3631 |
|
---|
3632 | #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
|
---|
3633 |
|
---|
3634 | /* special-case this address as inet_addr might return INADDR_NONE
|
---|
3635 | * for this */
|
---|
3636 | if (strcmp(ip_addr, "255.255.255.255") == 0) {
|
---|
3637 | packed_addr = 0xFFFFFFFF;
|
---|
3638 | } else {
|
---|
3639 |
|
---|
3640 | packed_addr = inet_addr(ip_addr);
|
---|
3641 |
|
---|
3642 | if (packed_addr == INADDR_NONE) { /* invalid address */
|
---|
3643 | PyErr_SetString(socket_error,
|
---|
3644 | "illegal IP address string passed to inet_aton");
|
---|
3645 | return NULL;
|
---|
3646 | }
|
---|
3647 | }
|
---|
3648 | return PyString_FromStringAndSize((char *) &packed_addr,
|
---|
3649 | sizeof(packed_addr));
|
---|
3650 |
|
---|
3651 | #ifdef USE_INET_ATON_WEAKLINK
|
---|
3652 | }
|
---|
3653 | #endif
|
---|
3654 |
|
---|
3655 | #endif
|
---|
3656 | }
|
---|
3657 |
|
---|
3658 | PyDoc_STRVAR(inet_ntoa_doc,
|
---|
3659 | "inet_ntoa(packed_ip) -> ip_address_string\n\
|
---|
3660 | \n\
|
---|
3661 | Convert an IP address from 32-bit packed binary format to string format");
|
---|
3662 |
|
---|
3663 | static PyObject*
|
---|
3664 | socket_inet_ntoa(PyObject *self, PyObject *args)
|
---|
3665 | {
|
---|
3666 | char *packed_str;
|
---|
3667 | int addr_len;
|
---|
3668 | struct in_addr packed_addr;
|
---|
3669 |
|
---|
3670 | if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {
|
---|
3671 | return NULL;
|
---|
3672 | }
|
---|
3673 |
|
---|
3674 | if (addr_len != sizeof(packed_addr)) {
|
---|
3675 | PyErr_SetString(socket_error,
|
---|
3676 | "packed IP wrong length for inet_ntoa");
|
---|
3677 | return NULL;
|
---|
3678 | }
|
---|
3679 |
|
---|
3680 | memcpy(&packed_addr, packed_str, addr_len);
|
---|
3681 |
|
---|
3682 | return PyString_FromString(inet_ntoa(packed_addr));
|
---|
3683 | }
|
---|
3684 |
|
---|
3685 | #ifdef HAVE_INET_PTON
|
---|
3686 |
|
---|
3687 | PyDoc_STRVAR(inet_pton_doc,
|
---|
3688 | "inet_pton(af, ip) -> packed IP address string\n\
|
---|
3689 | \n\
|
---|
3690 | Convert an IP address from string format to a packed string suitable\n\
|
---|
3691 | for use with low-level network functions.");
|
---|
3692 |
|
---|
3693 | static PyObject *
|
---|
3694 | socket_inet_pton(PyObject *self, PyObject *args)
|
---|
3695 | {
|
---|
3696 | int af;
|
---|
3697 | char* ip;
|
---|
3698 | int retval;
|
---|
3699 | #ifdef ENABLE_IPV6
|
---|
3700 | char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
|
---|
3701 | #else
|
---|
3702 | char packed[sizeof(struct in_addr)];
|
---|
3703 | #endif
|
---|
3704 | if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
|
---|
3705 | return NULL;
|
---|
3706 | }
|
---|
3707 |
|
---|
3708 | #if !defined(ENABLE_IPV6) && defined(AF_INET6)
|
---|
3709 | if(af == AF_INET6) {
|
---|
3710 | PyErr_SetString(socket_error,
|
---|
3711 | "can't use AF_INET6, IPv6 is disabled");
|
---|
3712 | return NULL;
|
---|
3713 | }
|
---|
3714 | #endif
|
---|
3715 |
|
---|
3716 | retval = inet_pton(af, ip, packed);
|
---|
3717 | if (retval < 0) {
|
---|
3718 | PyErr_SetFromErrno(socket_error);
|
---|
3719 | return NULL;
|
---|
3720 | } else if (retval == 0) {
|
---|
3721 | PyErr_SetString(socket_error,
|
---|
3722 | "illegal IP address string passed to inet_pton");
|
---|
3723 | return NULL;
|
---|
3724 | } else if (af == AF_INET) {
|
---|
3725 | return PyString_FromStringAndSize(packed,
|
---|
3726 | sizeof(struct in_addr));
|
---|
3727 | #ifdef ENABLE_IPV6
|
---|
3728 | } else if (af == AF_INET6) {
|
---|
3729 | return PyString_FromStringAndSize(packed,
|
---|
3730 | sizeof(struct in6_addr));
|
---|
3731 | #endif
|
---|
3732 | } else {
|
---|
3733 | PyErr_SetString(socket_error, "unknown address family");
|
---|
3734 | return NULL;
|
---|
3735 | }
|
---|
3736 | }
|
---|
3737 |
|
---|
3738 | PyDoc_STRVAR(inet_ntop_doc,
|
---|
3739 | "inet_ntop(af, packed_ip) -> string formatted IP address\n\
|
---|
3740 | \n\
|
---|
3741 | Convert a packed IP address of the given family to string format.");
|
---|
3742 |
|
---|
3743 | static PyObject *
|
---|
3744 | socket_inet_ntop(PyObject *self, PyObject *args)
|
---|
3745 | {
|
---|
3746 | int af;
|
---|
3747 | char* packed;
|
---|
3748 | int len;
|
---|
3749 | const char* retval;
|
---|
3750 | #ifdef ENABLE_IPV6
|
---|
3751 | char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
|
---|
3752 | #else
|
---|
3753 | char ip[INET_ADDRSTRLEN + 1];
|
---|
3754 | #endif
|
---|
3755 |
|
---|
3756 | /* Guarantee NUL-termination for PyString_FromString() below */
|
---|
3757 | memset((void *) &ip[0], '\0', sizeof(ip));
|
---|
3758 |
|
---|
3759 | if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) {
|
---|
3760 | return NULL;
|
---|
3761 | }
|
---|
3762 |
|
---|
3763 | if (af == AF_INET) {
|
---|
3764 | if (len != sizeof(struct in_addr)) {
|
---|
3765 | PyErr_SetString(PyExc_ValueError,
|
---|
3766 | "invalid length of packed IP address string");
|
---|
3767 | return NULL;
|
---|
3768 | }
|
---|
3769 | #ifdef ENABLE_IPV6
|
---|
3770 | } else if (af == AF_INET6) {
|
---|
3771 | if (len != sizeof(struct in6_addr)) {
|
---|
3772 | PyErr_SetString(PyExc_ValueError,
|
---|
3773 | "invalid length of packed IP address string");
|
---|
3774 | return NULL;
|
---|
3775 | }
|
---|
3776 | #endif
|
---|
3777 | } else {
|
---|
3778 | PyErr_Format(PyExc_ValueError,
|
---|
3779 | "unknown address family %d", af);
|
---|
3780 | return NULL;
|
---|
3781 | }
|
---|
3782 |
|
---|
3783 | retval = inet_ntop(af, packed, ip, sizeof(ip));
|
---|
3784 | if (!retval) {
|
---|
3785 | PyErr_SetFromErrno(socket_error);
|
---|
3786 | return NULL;
|
---|
3787 | } else {
|
---|
3788 | return PyString_FromString(retval);
|
---|
3789 | }
|
---|
3790 |
|
---|
3791 | /* NOTREACHED */
|
---|
3792 | PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
|
---|
3793 | return NULL;
|
---|
3794 | }
|
---|
3795 |
|
---|
3796 | #endif /* HAVE_INET_PTON */
|
---|
3797 |
|
---|
3798 | /* Python interface to getaddrinfo(host, port). */
|
---|
3799 |
|
---|
3800 | /*ARGSUSED*/
|
---|
3801 | static PyObject *
|
---|
3802 | socket_getaddrinfo(PyObject *self, PyObject *args)
|
---|
3803 | {
|
---|
3804 | struct addrinfo hints, *res;
|
---|
3805 | struct addrinfo *res0 = NULL;
|
---|
3806 | PyObject *hobj = NULL;
|
---|
3807 | PyObject *pobj = (PyObject *)NULL;
|
---|
3808 | char pbuf[30];
|
---|
3809 | char *hptr, *pptr;
|
---|
3810 | int family, socktype, protocol, flags;
|
---|
3811 | int error;
|
---|
3812 | PyObject *all = (PyObject *)NULL;
|
---|
3813 | PyObject *single = (PyObject *)NULL;
|
---|
3814 | PyObject *idna = NULL;
|
---|
3815 |
|
---|
3816 | family = socktype = protocol = flags = 0;
|
---|
3817 | family = AF_UNSPEC;
|
---|
3818 | if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
|
---|
3819 | &hobj, &pobj, &family, &socktype,
|
---|
3820 | &protocol, &flags)) {
|
---|
3821 | return NULL;
|
---|
3822 | }
|
---|
3823 | if (hobj == Py_None) {
|
---|
3824 | hptr = NULL;
|
---|
3825 | } else if (PyUnicode_Check(hobj)) {
|
---|
3826 | idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
|
---|
3827 | if (!idna)
|
---|
3828 | return NULL;
|
---|
3829 | hptr = PyString_AsString(idna);
|
---|
3830 | } else if (PyString_Check(hobj)) {
|
---|
3831 | hptr = PyString_AsString(hobj);
|
---|
3832 | } else {
|
---|
3833 | PyErr_SetString(PyExc_TypeError,
|
---|
3834 | "getaddrinfo() argument 1 must be string or None");
|
---|
3835 | return NULL;
|
---|
3836 | }
|
---|
3837 | if (PyInt_Check(pobj)) {
|
---|
3838 | PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj));
|
---|
3839 | pptr = pbuf;
|
---|
3840 | } else if (PyString_Check(pobj)) {
|
---|
3841 | pptr = PyString_AsString(pobj);
|
---|
3842 | } else if (pobj == Py_None) {
|
---|
3843 | pptr = (char *)NULL;
|
---|
3844 | } else {
|
---|
3845 | PyErr_SetString(socket_error, "Int or String expected");
|
---|
3846 | goto err;
|
---|
3847 | }
|
---|
3848 | memset(&hints, 0, sizeof(hints));
|
---|
3849 | hints.ai_family = family;
|
---|
3850 | hints.ai_socktype = socktype;
|
---|
3851 | hints.ai_protocol = protocol;
|
---|
3852 | hints.ai_flags = flags;
|
---|
3853 | Py_BEGIN_ALLOW_THREADS
|
---|
3854 | ACQUIRE_GETADDRINFO_LOCK
|
---|
3855 | error = getaddrinfo(hptr, pptr, &hints, &res0);
|
---|
3856 | Py_END_ALLOW_THREADS
|
---|
3857 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
|
---|
3858 | if (error) {
|
---|
3859 | set_gaierror(error);
|
---|
3860 | goto err;
|
---|
3861 | }
|
---|
3862 |
|
---|
3863 | if ((all = PyList_New(0)) == NULL)
|
---|
3864 | goto err;
|
---|
3865 | for (res = res0; res; res = res->ai_next) {
|
---|
3866 | PyObject *addr =
|
---|
3867 | makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
|
---|
3868 | if (addr == NULL)
|
---|
3869 | goto err;
|
---|
3870 | single = Py_BuildValue("iiisO", res->ai_family,
|
---|
3871 | res->ai_socktype, res->ai_protocol,
|
---|
3872 | res->ai_canonname ? res->ai_canonname : "",
|
---|
3873 | addr);
|
---|
3874 | Py_DECREF(addr);
|
---|
3875 | if (single == NULL)
|
---|
3876 | goto err;
|
---|
3877 |
|
---|
3878 | if (PyList_Append(all, single))
|
---|
3879 | goto err;
|
---|
3880 | Py_XDECREF(single);
|
---|
3881 | }
|
---|
3882 | Py_XDECREF(idna);
|
---|
3883 | if (res0)
|
---|
3884 | freeaddrinfo(res0);
|
---|
3885 | return all;
|
---|
3886 | err:
|
---|
3887 | Py_XDECREF(single);
|
---|
3888 | Py_XDECREF(all);
|
---|
3889 | Py_XDECREF(idna);
|
---|
3890 | if (res0)
|
---|
3891 | freeaddrinfo(res0);
|
---|
3892 | return (PyObject *)NULL;
|
---|
3893 | }
|
---|
3894 |
|
---|
3895 | PyDoc_STRVAR(getaddrinfo_doc,
|
---|
3896 | "getaddrinfo(host, port [, family, socktype, proto, flags])\n\
|
---|
3897 | -> list of (family, socktype, proto, canonname, sockaddr)\n\
|
---|
3898 | \n\
|
---|
3899 | Resolve host and port into addrinfo struct.");
|
---|
3900 |
|
---|
3901 | /* Python interface to getnameinfo(sa, flags). */
|
---|
3902 |
|
---|
3903 | /*ARGSUSED*/
|
---|
3904 | static PyObject *
|
---|
3905 | socket_getnameinfo(PyObject *self, PyObject *args)
|
---|
3906 | {
|
---|
3907 | PyObject *sa = (PyObject *)NULL;
|
---|
3908 | int flags;
|
---|
3909 | char *hostp;
|
---|
3910 | int port, flowinfo, scope_id;
|
---|
3911 | char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
|
---|
3912 | struct addrinfo hints, *res = NULL;
|
---|
3913 | int error;
|
---|
3914 | PyObject *ret = (PyObject *)NULL;
|
---|
3915 |
|
---|
3916 | flags = flowinfo = scope_id = 0;
|
---|
3917 | if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
|
---|
3918 | return NULL;
|
---|
3919 | if (!PyArg_ParseTuple(sa, "si|ii",
|
---|
3920 | &hostp, &port, &flowinfo, &scope_id))
|
---|
3921 | return NULL;
|
---|
3922 | PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
|
---|
3923 | memset(&hints, 0, sizeof(hints));
|
---|
3924 | hints.ai_family = AF_UNSPEC;
|
---|
3925 | hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
|
---|
3926 | Py_BEGIN_ALLOW_THREADS
|
---|
3927 | ACQUIRE_GETADDRINFO_LOCK
|
---|
3928 | error = getaddrinfo(hostp, pbuf, &hints, &res);
|
---|
3929 | Py_END_ALLOW_THREADS
|
---|
3930 | RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
|
---|
3931 | if (error) {
|
---|
3932 | set_gaierror(error);
|
---|
3933 | goto fail;
|
---|
3934 | }
|
---|
3935 | if (res->ai_next) {
|
---|
3936 | PyErr_SetString(socket_error,
|
---|
3937 | "sockaddr resolved to multiple addresses");
|
---|
3938 | goto fail;
|
---|
3939 | }
|
---|
3940 | switch (res->ai_family) {
|
---|
3941 | case AF_INET:
|
---|
3942 | {
|
---|
3943 | char *t1;
|
---|
3944 | int t2;
|
---|
3945 | if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) {
|
---|
3946 | PyErr_SetString(socket_error,
|
---|
3947 | "IPv4 sockaddr must be 2 tuple");
|
---|
3948 | goto fail;
|
---|
3949 | }
|
---|
3950 | break;
|
---|
3951 | }
|
---|
3952 | #ifdef ENABLE_IPV6
|
---|
3953 | case AF_INET6:
|
---|
3954 | {
|
---|
3955 | struct sockaddr_in6 *sin6;
|
---|
3956 | sin6 = (struct sockaddr_in6 *)res->ai_addr;
|
---|
3957 | sin6->sin6_flowinfo = flowinfo;
|
---|
3958 | sin6->sin6_scope_id = scope_id;
|
---|
3959 | break;
|
---|
3960 | }
|
---|
3961 | #endif
|
---|
3962 | }
|
---|
3963 | error = getnameinfo(res->ai_addr, res->ai_addrlen,
|
---|
3964 | hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
|
---|
3965 | if (error) {
|
---|
3966 | set_gaierror(error);
|
---|
3967 | goto fail;
|
---|
3968 | }
|
---|
3969 | ret = Py_BuildValue("ss", hbuf, pbuf);
|
---|
3970 |
|
---|
3971 | fail:
|
---|
3972 | if (res)
|
---|
3973 | freeaddrinfo(res);
|
---|
3974 | return ret;
|
---|
3975 | }
|
---|
3976 |
|
---|
3977 | PyDoc_STRVAR(getnameinfo_doc,
|
---|
3978 | "getnameinfo(sockaddr, flags) --> (host, port)\n\
|
---|
3979 | \n\
|
---|
3980 | Get host and port for a sockaddr.");
|
---|
3981 |
|
---|
3982 |
|
---|
3983 | /* Python API to getting and setting the default timeout value. */
|
---|
3984 |
|
---|
3985 | static PyObject *
|
---|
3986 | socket_getdefaulttimeout(PyObject *self)
|
---|
3987 | {
|
---|
3988 | if (defaulttimeout < 0.0) {
|
---|
3989 | Py_INCREF(Py_None);
|
---|
3990 | return Py_None;
|
---|
3991 | }
|
---|
3992 | else
|
---|
3993 | return PyFloat_FromDouble(defaulttimeout);
|
---|
3994 | }
|
---|
3995 |
|
---|
3996 | PyDoc_STRVAR(getdefaulttimeout_doc,
|
---|
3997 | "getdefaulttimeout() -> timeout\n\
|
---|
3998 | \n\
|
---|
3999 | Returns the default timeout in floating seconds for new socket objects.\n\
|
---|
4000 | A value of None indicates that new socket objects have no timeout.\n\
|
---|
4001 | When the socket module is first imported, the default is None.");
|
---|
4002 |
|
---|
4003 | static PyObject *
|
---|
4004 | socket_setdefaulttimeout(PyObject *self, PyObject *arg)
|
---|
4005 | {
|
---|
4006 | double timeout;
|
---|
4007 |
|
---|
4008 | if (arg == Py_None)
|
---|
4009 | timeout = -1.0;
|
---|
4010 | else {
|
---|
4011 | timeout = PyFloat_AsDouble(arg);
|
---|
4012 | if (timeout < 0.0) {
|
---|
4013 | if (!PyErr_Occurred())
|
---|
4014 | PyErr_SetString(PyExc_ValueError,
|
---|
4015 | "Timeout value out of range");
|
---|
4016 | return NULL;
|
---|
4017 | }
|
---|
4018 | }
|
---|
4019 |
|
---|
4020 | defaulttimeout = timeout;
|
---|
4021 |
|
---|
4022 | Py_INCREF(Py_None);
|
---|
4023 | return Py_None;
|
---|
4024 | }
|
---|
4025 |
|
---|
4026 | PyDoc_STRVAR(setdefaulttimeout_doc,
|
---|
4027 | "setdefaulttimeout(timeout)\n\
|
---|
4028 | \n\
|
---|
4029 | Set the default timeout in floating seconds for new socket objects.\n\
|
---|
4030 | A value of None indicates that new socket objects have no timeout.\n\
|
---|
4031 | When the socket module is first imported, the default is None.");
|
---|
4032 |
|
---|
4033 |
|
---|
4034 | /* List of functions exported by this module. */
|
---|
4035 |
|
---|
4036 | static PyMethodDef socket_methods[] = {
|
---|
4037 | {"gethostbyname", socket_gethostbyname,
|
---|
4038 | METH_VARARGS, gethostbyname_doc},
|
---|
4039 | {"gethostbyname_ex", socket_gethostbyname_ex,
|
---|
4040 | METH_VARARGS, ghbn_ex_doc},
|
---|
4041 | {"gethostbyaddr", socket_gethostbyaddr,
|
---|
4042 | METH_VARARGS, gethostbyaddr_doc},
|
---|
4043 | {"gethostname", socket_gethostname,
|
---|
4044 | METH_NOARGS, gethostname_doc},
|
---|
4045 | {"getservbyname", socket_getservbyname,
|
---|
4046 | METH_VARARGS, getservbyname_doc},
|
---|
4047 | {"getservbyport", socket_getservbyport,
|
---|
4048 | METH_VARARGS, getservbyport_doc},
|
---|
4049 | {"getprotobyname", socket_getprotobyname,
|
---|
4050 | METH_VARARGS, getprotobyname_doc},
|
---|
4051 | #ifndef NO_DUP
|
---|
4052 | {"fromfd", socket_fromfd,
|
---|
4053 | METH_VARARGS, fromfd_doc},
|
---|
4054 | #endif
|
---|
4055 | #ifdef HAVE_SOCKETPAIR
|
---|
4056 | {"socketpair", socket_socketpair,
|
---|
4057 | METH_VARARGS, socketpair_doc},
|
---|
4058 | #endif
|
---|
4059 | {"ntohs", socket_ntohs,
|
---|
4060 | METH_VARARGS, ntohs_doc},
|
---|
4061 | {"ntohl", socket_ntohl,
|
---|
4062 | METH_O, ntohl_doc},
|
---|
4063 | {"htons", socket_htons,
|
---|
4064 | METH_VARARGS, htons_doc},
|
---|
4065 | {"htonl", socket_htonl,
|
---|
4066 | METH_O, htonl_doc},
|
---|
4067 | {"inet_aton", socket_inet_aton,
|
---|
4068 | METH_VARARGS, inet_aton_doc},
|
---|
4069 | {"inet_ntoa", socket_inet_ntoa,
|
---|
4070 | METH_VARARGS, inet_ntoa_doc},
|
---|
4071 | #ifdef HAVE_INET_PTON
|
---|
4072 | {"inet_pton", socket_inet_pton,
|
---|
4073 | METH_VARARGS, inet_pton_doc},
|
---|
4074 | {"inet_ntop", socket_inet_ntop,
|
---|
4075 | METH_VARARGS, inet_ntop_doc},
|
---|
4076 | #endif
|
---|
4077 | {"getaddrinfo", socket_getaddrinfo,
|
---|
4078 | METH_VARARGS, getaddrinfo_doc},
|
---|
4079 | {"getnameinfo", socket_getnameinfo,
|
---|
4080 | METH_VARARGS, getnameinfo_doc},
|
---|
4081 | {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
|
---|
4082 | METH_NOARGS, getdefaulttimeout_doc},
|
---|
4083 | {"setdefaulttimeout", socket_setdefaulttimeout,
|
---|
4084 | METH_O, setdefaulttimeout_doc},
|
---|
4085 | {NULL, NULL} /* Sentinel */
|
---|
4086 | };
|
---|
4087 |
|
---|
4088 |
|
---|
4089 | #ifdef RISCOS
|
---|
4090 | #define OS_INIT_DEFINED
|
---|
4091 |
|
---|
4092 | static int
|
---|
4093 | os_init(void)
|
---|
4094 | {
|
---|
4095 | _kernel_swi_regs r;
|
---|
4096 |
|
---|
4097 | r.r[0] = 0;
|
---|
4098 | _kernel_swi(0x43380, &r, &r);
|
---|
4099 | taskwindow = r.r[0];
|
---|
4100 |
|
---|
4101 | return 1;
|
---|
4102 | }
|
---|
4103 |
|
---|
4104 | #endif /* RISCOS */
|
---|
4105 |
|
---|
4106 |
|
---|
4107 | #ifdef MS_WINDOWS
|
---|
4108 | #define OS_INIT_DEFINED
|
---|
4109 |
|
---|
4110 | /* Additional initialization and cleanup for Windows */
|
---|
4111 |
|
---|
4112 | static void
|
---|
4113 | os_cleanup(void)
|
---|
4114 | {
|
---|
4115 | WSACleanup();
|
---|
4116 | }
|
---|
4117 |
|
---|
4118 | static int
|
---|
4119 | os_init(void)
|
---|
4120 | {
|
---|
4121 | WSADATA WSAData;
|
---|
4122 | int ret;
|
---|
4123 | char buf[100];
|
---|
4124 | ret = WSAStartup(0x0101, &WSAData);
|
---|
4125 | switch (ret) {
|
---|
4126 | case 0: /* No error */
|
---|
4127 | Py_AtExit(os_cleanup);
|
---|
4128 | return 1; /* Success */
|
---|
4129 | case WSASYSNOTREADY:
|
---|
4130 | PyErr_SetString(PyExc_ImportError,
|
---|
4131 | "WSAStartup failed: network not ready");
|
---|
4132 | break;
|
---|
4133 | case WSAVERNOTSUPPORTED:
|
---|
4134 | case WSAEINVAL:
|
---|
4135 | PyErr_SetString(
|
---|
4136 | PyExc_ImportError,
|
---|
4137 | "WSAStartup failed: requested version not supported");
|
---|
4138 | break;
|
---|
4139 | default:
|
---|
4140 | PyOS_snprintf(buf, sizeof(buf),
|
---|
4141 | "WSAStartup failed: error code %d", ret);
|
---|
4142 | PyErr_SetString(PyExc_ImportError, buf);
|
---|
4143 | break;
|
---|
4144 | }
|
---|
4145 | return 0; /* Failure */
|
---|
4146 | }
|
---|
4147 |
|
---|
4148 | #endif /* MS_WINDOWS */
|
---|
4149 |
|
---|
4150 |
|
---|
4151 | #ifdef PYOS_OS2
|
---|
4152 | #define OS_INIT_DEFINED
|
---|
4153 |
|
---|
4154 | /* Additional initialization for OS/2 */
|
---|
4155 |
|
---|
4156 | static int
|
---|
4157 | os_init(void)
|
---|
4158 | {
|
---|
4159 | #ifndef PYCC_GCC
|
---|
4160 | char reason[64];
|
---|
4161 | int rc = sock_init();
|
---|
4162 |
|
---|
4163 | if (rc == 0) {
|
---|
4164 | return 1; /* Success */
|
---|
4165 | }
|
---|
4166 |
|
---|
4167 | PyOS_snprintf(reason, sizeof(reason),
|
---|
4168 | "OS/2 TCP/IP Error# %d", sock_errno());
|
---|
4169 | PyErr_SetString(PyExc_ImportError, reason);
|
---|
4170 |
|
---|
4171 | return 0; /* Failure */
|
---|
4172 | #else
|
---|
4173 | /* No need to initialise sockets with GCC/EMX */
|
---|
4174 | return 1; /* Success */
|
---|
4175 | #endif
|
---|
4176 | }
|
---|
4177 |
|
---|
4178 | #endif /* PYOS_OS2 */
|
---|
4179 |
|
---|
4180 |
|
---|
4181 | #ifndef OS_INIT_DEFINED
|
---|
4182 | static int
|
---|
4183 | os_init(void)
|
---|
4184 | {
|
---|
4185 | return 1; /* Success */
|
---|
4186 | }
|
---|
4187 | #endif
|
---|
4188 |
|
---|
4189 |
|
---|
4190 | /* C API table - always add new things to the end for binary
|
---|
4191 | compatibility. */
|
---|
4192 | static
|
---|
4193 | PySocketModule_APIObject PySocketModuleAPI =
|
---|
4194 | {
|
---|
4195 | &sock_type,
|
---|
4196 | NULL
|
---|
4197 | };
|
---|
4198 |
|
---|
4199 |
|
---|
4200 | /* Initialize the _socket module.
|
---|
4201 |
|
---|
4202 | This module is actually called "_socket", and there's a wrapper
|
---|
4203 | "socket.py" which implements some additional functionality. On some
|
---|
4204 | platforms (e.g. Windows and OS/2), socket.py also implements a
|
---|
4205 | wrapper for the socket type that provides missing functionality such
|
---|
4206 | as makefile(), dup() and fromfd(). The import of "_socket" may fail
|
---|
4207 | with an ImportError exception if os-specific initialization fails.
|
---|
4208 | On Windows, this does WINSOCK initialization. When WINSOCK is
|
---|
4209 | initialized succesfully, a call to WSACleanup() is scheduled to be
|
---|
4210 | made at exit time.
|
---|
4211 | */
|
---|
4212 |
|
---|
4213 | PyDoc_STRVAR(socket_doc,
|
---|
4214 | "Implementation module for socket operations.\n\
|
---|
4215 | \n\
|
---|
4216 | See the socket module for documentation.");
|
---|
4217 |
|
---|
4218 | PyMODINIT_FUNC
|
---|
4219 | init_socket(void)
|
---|
4220 | {
|
---|
4221 | PyObject *m, *has_ipv6;
|
---|
4222 |
|
---|
4223 | if (!os_init())
|
---|
4224 | return;
|
---|
4225 |
|
---|
4226 | sock_type.ob_type = &PyType_Type;
|
---|
4227 | m = Py_InitModule3(PySocket_MODULE_NAME,
|
---|
4228 | socket_methods,
|
---|
4229 | socket_doc);
|
---|
4230 | if (m == NULL)
|
---|
4231 | return;
|
---|
4232 |
|
---|
4233 | socket_error = PyErr_NewException("socket.error", NULL, NULL);
|
---|
4234 | if (socket_error == NULL)
|
---|
4235 | return;
|
---|
4236 | PySocketModuleAPI.error = socket_error;
|
---|
4237 | Py_INCREF(socket_error);
|
---|
4238 | PyModule_AddObject(m, "error", socket_error);
|
---|
4239 | socket_herror = PyErr_NewException("socket.herror",
|
---|
4240 | socket_error, NULL);
|
---|
4241 | if (socket_herror == NULL)
|
---|
4242 | return;
|
---|
4243 | Py_INCREF(socket_herror);
|
---|
4244 | PyModule_AddObject(m, "herror", socket_herror);
|
---|
4245 | socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
|
---|
4246 | NULL);
|
---|
4247 | if (socket_gaierror == NULL)
|
---|
4248 | return;
|
---|
4249 | Py_INCREF(socket_gaierror);
|
---|
4250 | PyModule_AddObject(m, "gaierror", socket_gaierror);
|
---|
4251 | socket_timeout = PyErr_NewException("socket.timeout",
|
---|
4252 | socket_error, NULL);
|
---|
4253 | if (socket_timeout == NULL)
|
---|
4254 | return;
|
---|
4255 | Py_INCREF(socket_timeout);
|
---|
4256 | PyModule_AddObject(m, "timeout", socket_timeout);
|
---|
4257 | Py_INCREF((PyObject *)&sock_type);
|
---|
4258 | if (PyModule_AddObject(m, "SocketType",
|
---|
4259 | (PyObject *)&sock_type) != 0)
|
---|
4260 | return;
|
---|
4261 | Py_INCREF((PyObject *)&sock_type);
|
---|
4262 | if (PyModule_AddObject(m, "socket",
|
---|
4263 | (PyObject *)&sock_type) != 0)
|
---|
4264 | return;
|
---|
4265 |
|
---|
4266 | #ifdef ENABLE_IPV6
|
---|
4267 | has_ipv6 = Py_True;
|
---|
4268 | #else
|
---|
4269 | has_ipv6 = Py_False;
|
---|
4270 | #endif
|
---|
4271 | Py_INCREF(has_ipv6);
|
---|
4272 | PyModule_AddObject(m, "has_ipv6", has_ipv6);
|
---|
4273 |
|
---|
4274 | /* Export C API */
|
---|
4275 | if (PyModule_AddObject(m, PySocket_CAPI_NAME,
|
---|
4276 | PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL)
|
---|
4277 | ) != 0)
|
---|
4278 | return;
|
---|
4279 |
|
---|
4280 | /* Address families (we only support AF_INET and AF_UNIX) */
|
---|
4281 | #ifdef AF_UNSPEC
|
---|
4282 | PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
|
---|
4283 | #endif
|
---|
4284 | PyModule_AddIntConstant(m, "AF_INET", AF_INET);
|
---|
4285 | #ifdef AF_INET6
|
---|
4286 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
|
---|
4287 | #endif /* AF_INET6 */
|
---|
4288 | #if defined(AF_UNIX)
|
---|
4289 | PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
|
---|
4290 | #endif /* AF_UNIX */
|
---|
4291 | #ifdef AF_AX25
|
---|
4292 | /* Amateur Radio AX.25 */
|
---|
4293 | PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
|
---|
4294 | #endif
|
---|
4295 | #ifdef AF_IPX
|
---|
4296 | PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
|
---|
4297 | #endif
|
---|
4298 | #ifdef AF_APPLETALK
|
---|
4299 | /* Appletalk DDP */
|
---|
4300 | PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
|
---|
4301 | #endif
|
---|
4302 | #ifdef AF_NETROM
|
---|
4303 | /* Amateur radio NetROM */
|
---|
4304 | PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
|
---|
4305 | #endif
|
---|
4306 | #ifdef AF_BRIDGE
|
---|
4307 | /* Multiprotocol bridge */
|
---|
4308 | PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
|
---|
4309 | #endif
|
---|
4310 | #ifdef AF_ATMPVC
|
---|
4311 | /* ATM PVCs */
|
---|
4312 | PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC);
|
---|
4313 | #endif
|
---|
4314 | #ifdef AF_AAL5
|
---|
4315 | /* Reserved for Werner's ATM */
|
---|
4316 | PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
|
---|
4317 | #endif
|
---|
4318 | #ifdef AF_X25
|
---|
4319 | /* Reserved for X.25 project */
|
---|
4320 | PyModule_AddIntConstant(m, "AF_X25", AF_X25);
|
---|
4321 | #endif
|
---|
4322 | #ifdef AF_INET6
|
---|
4323 | PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
|
---|
4324 | #endif
|
---|
4325 | #ifdef AF_ROSE
|
---|
4326 | /* Amateur Radio X.25 PLP */
|
---|
4327 | PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
|
---|
4328 | #endif
|
---|
4329 | #ifdef AF_DECnet
|
---|
4330 | /* Reserved for DECnet project */
|
---|
4331 | PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet);
|
---|
4332 | #endif
|
---|
4333 | #ifdef AF_NETBEUI
|
---|
4334 | /* Reserved for 802.2LLC project */
|
---|
4335 | PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI);
|
---|
4336 | #endif
|
---|
4337 | #ifdef AF_SECURITY
|
---|
4338 | /* Security callback pseudo AF */
|
---|
4339 | PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY);
|
---|
4340 | #endif
|
---|
4341 | #ifdef AF_KEY
|
---|
4342 | /* PF_KEY key management API */
|
---|
4343 | PyModule_AddIntConstant(m, "AF_KEY", AF_KEY);
|
---|
4344 | #endif
|
---|
4345 | #ifdef AF_NETLINK
|
---|
4346 | /* */
|
---|
4347 | PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);
|
---|
4348 | PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE);
|
---|
4349 | #ifdef NETLINK_SKIP
|
---|
4350 | PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP);
|
---|
4351 | #endif
|
---|
4352 | #ifdef NETLINK_W1
|
---|
4353 | PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1);
|
---|
4354 | #endif
|
---|
4355 | PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK);
|
---|
4356 | PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL);
|
---|
4357 | #ifdef NETLINK_TCPDIAG
|
---|
4358 | PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG);
|
---|
4359 | #endif
|
---|
4360 | #ifdef NETLINK_NFLOG
|
---|
4361 | PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG);
|
---|
4362 | #endif
|
---|
4363 | #ifdef NETLINK_XFRM
|
---|
4364 | PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM);
|
---|
4365 | #endif
|
---|
4366 | #ifdef NETLINK_ARPD
|
---|
4367 | PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD);
|
---|
4368 | #endif
|
---|
4369 | #ifdef NETLINK_ROUTE6
|
---|
4370 | PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6);
|
---|
4371 | #endif
|
---|
4372 | PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW);
|
---|
4373 | PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG);
|
---|
4374 | #ifdef NETLINK_TAPBASE
|
---|
4375 | PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE);
|
---|
4376 | #endif
|
---|
4377 | #endif /* AF_NETLINK */
|
---|
4378 | #ifdef AF_ROUTE
|
---|
4379 | /* Alias to emulate 4.4BSD */
|
---|
4380 | PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE);
|
---|
4381 | #endif
|
---|
4382 | #ifdef AF_ASH
|
---|
4383 | /* Ash */
|
---|
4384 | PyModule_AddIntConstant(m, "AF_ASH", AF_ASH);
|
---|
4385 | #endif
|
---|
4386 | #ifdef AF_ECONET
|
---|
4387 | /* Acorn Econet */
|
---|
4388 | PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET);
|
---|
4389 | #endif
|
---|
4390 | #ifdef AF_ATMSVC
|
---|
4391 | /* ATM SVCs */
|
---|
4392 | PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC);
|
---|
4393 | #endif
|
---|
4394 | #ifdef AF_SNA
|
---|
4395 | /* Linux SNA Project (nutters!) */
|
---|
4396 | PyModule_AddIntConstant(m, "AF_SNA", AF_SNA);
|
---|
4397 | #endif
|
---|
4398 | #ifdef AF_IRDA
|
---|
4399 | /* IRDA sockets */
|
---|
4400 | PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA);
|
---|
4401 | #endif
|
---|
4402 | #ifdef AF_PPPOX
|
---|
4403 | /* PPPoX sockets */
|
---|
4404 | PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX);
|
---|
4405 | #endif
|
---|
4406 | #ifdef AF_WANPIPE
|
---|
4407 | /* Wanpipe API Sockets */
|
---|
4408 | PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE);
|
---|
4409 | #endif
|
---|
4410 | #ifdef AF_LLC
|
---|
4411 | /* Linux LLC */
|
---|
4412 | PyModule_AddIntConstant(m, "AF_LLC", AF_LLC);
|
---|
4413 | #endif
|
---|
4414 |
|
---|
4415 | #ifdef USE_BLUETOOTH
|
---|
4416 | PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);
|
---|
4417 | PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);
|
---|
4418 | #if !defined(__FreeBSD__)
|
---|
4419 | PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);
|
---|
4420 | #endif
|
---|
4421 | PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);
|
---|
4422 | PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
|
---|
4423 | PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
|
---|
4424 | #endif
|
---|
4425 |
|
---|
4426 | #ifdef HAVE_NETPACKET_PACKET_H
|
---|
4427 | PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
|
---|
4428 | PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
|
---|
4429 | PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
|
---|
4430 | PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
|
---|
4431 | PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
|
---|
4432 | PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
|
---|
4433 | PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
|
---|
4434 | PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
|
---|
4435 | PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
|
---|
4436 | #endif
|
---|
4437 |
|
---|
4438 | /* Socket types */
|
---|
4439 | PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
|
---|
4440 | PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
|
---|
4441 | #ifndef __BEOS__
|
---|
4442 | /* We have incomplete socket support. */
|
---|
4443 | PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
|
---|
4444 | PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
|
---|
4445 | #if defined(SOCK_RDM)
|
---|
4446 | PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
|
---|
4447 | #endif
|
---|
4448 | #endif
|
---|
4449 |
|
---|
4450 | #ifdef SO_DEBUG
|
---|
4451 | PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
|
---|
4452 | #endif
|
---|
4453 | #ifdef SO_ACCEPTCONN
|
---|
4454 | PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
|
---|
4455 | #endif
|
---|
4456 | #ifdef SO_REUSEADDR
|
---|
4457 | PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
|
---|
4458 | #endif
|
---|
4459 | #ifdef SO_EXCLUSIVEADDRUSE
|
---|
4460 | PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE);
|
---|
4461 | #endif
|
---|
4462 |
|
---|
4463 | #ifdef SO_KEEPALIVE
|
---|
4464 | PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
|
---|
4465 | #endif
|
---|
4466 | #ifdef SO_DONTROUTE
|
---|
4467 | PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
|
---|
4468 | #endif
|
---|
4469 | #ifdef SO_BROADCAST
|
---|
4470 | PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
|
---|
4471 | #endif
|
---|
4472 | #ifdef SO_USELOOPBACK
|
---|
4473 | PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
|
---|
4474 | #endif
|
---|
4475 | #ifdef SO_LINGER
|
---|
4476 | PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
|
---|
4477 | #endif
|
---|
4478 | #ifdef SO_OOBINLINE
|
---|
4479 | PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
|
---|
4480 | #endif
|
---|
4481 | #ifdef SO_REUSEPORT
|
---|
4482 | PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
|
---|
4483 | #endif
|
---|
4484 | #ifdef SO_SNDBUF
|
---|
4485 | PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
|
---|
4486 | #endif
|
---|
4487 | #ifdef SO_RCVBUF
|
---|
4488 | PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
|
---|
4489 | #endif
|
---|
4490 | #ifdef SO_SNDLOWAT
|
---|
4491 | PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
|
---|
4492 | #endif
|
---|
4493 | #ifdef SO_RCVLOWAT
|
---|
4494 | PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
|
---|
4495 | #endif
|
---|
4496 | #ifdef SO_SNDTIMEO
|
---|
4497 | PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
|
---|
4498 | #endif
|
---|
4499 | #ifdef SO_RCVTIMEO
|
---|
4500 | PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
|
---|
4501 | #endif
|
---|
4502 | #ifdef SO_ERROR
|
---|
4503 | PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
|
---|
4504 | #endif
|
---|
4505 | #ifdef SO_TYPE
|
---|
4506 | PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
|
---|
4507 | #endif
|
---|
4508 |
|
---|
4509 | /* Maximum number of connections for "listen" */
|
---|
4510 | #ifdef SOMAXCONN
|
---|
4511 | PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
|
---|
4512 | #else
|
---|
4513 | PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
|
---|
4514 | #endif
|
---|
4515 |
|
---|
4516 | /* Flags for send, recv */
|
---|
4517 | #ifdef MSG_OOB
|
---|
4518 | PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
|
---|
4519 | #endif
|
---|
4520 | #ifdef MSG_PEEK
|
---|
4521 | PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
|
---|
4522 | #endif
|
---|
4523 | #ifdef MSG_DONTROUTE
|
---|
4524 | PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
|
---|
4525 | #endif
|
---|
4526 | #ifdef MSG_DONTWAIT
|
---|
4527 | PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
|
---|
4528 | #endif
|
---|
4529 | #ifdef MSG_EOR
|
---|
4530 | PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
|
---|
4531 | #endif
|
---|
4532 | #ifdef MSG_TRUNC
|
---|
4533 | PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
|
---|
4534 | #endif
|
---|
4535 | #ifdef MSG_CTRUNC
|
---|
4536 | PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
|
---|
4537 | #endif
|
---|
4538 | #ifdef MSG_WAITALL
|
---|
4539 | PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
|
---|
4540 | #endif
|
---|
4541 | #ifdef MSG_BTAG
|
---|
4542 | PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
|
---|
4543 | #endif
|
---|
4544 | #ifdef MSG_ETAG
|
---|
4545 | PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
|
---|
4546 | #endif
|
---|
4547 |
|
---|
4548 | /* Protocol level and numbers, usable for [gs]etsockopt */
|
---|
4549 | #ifdef SOL_SOCKET
|
---|
4550 | PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
|
---|
4551 | #endif
|
---|
4552 | #ifdef SOL_IP
|
---|
4553 | PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
|
---|
4554 | #else
|
---|
4555 | PyModule_AddIntConstant(m, "SOL_IP", 0);
|
---|
4556 | #endif
|
---|
4557 | #ifdef SOL_IPX
|
---|
4558 | PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
|
---|
4559 | #endif
|
---|
4560 | #ifdef SOL_AX25
|
---|
4561 | PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
|
---|
4562 | #endif
|
---|
4563 | #ifdef SOL_ATALK
|
---|
4564 | PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
|
---|
4565 | #endif
|
---|
4566 | #ifdef SOL_NETROM
|
---|
4567 | PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
|
---|
4568 | #endif
|
---|
4569 | #ifdef SOL_ROSE
|
---|
4570 | PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
|
---|
4571 | #endif
|
---|
4572 | #ifdef SOL_TCP
|
---|
4573 | PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
|
---|
4574 | #else
|
---|
4575 | PyModule_AddIntConstant(m, "SOL_TCP", 6);
|
---|
4576 | #endif
|
---|
4577 | #ifdef SOL_UDP
|
---|
4578 | PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
|
---|
4579 | #else
|
---|
4580 | PyModule_AddIntConstant(m, "SOL_UDP", 17);
|
---|
4581 | #endif
|
---|
4582 | #ifdef IPPROTO_IP
|
---|
4583 | PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
|
---|
4584 | #else
|
---|
4585 | PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
|
---|
4586 | #endif
|
---|
4587 | #ifdef IPPROTO_HOPOPTS
|
---|
4588 | PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
|
---|
4589 | #endif
|
---|
4590 | #ifdef IPPROTO_ICMP
|
---|
4591 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
|
---|
4592 | #else
|
---|
4593 | PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
|
---|
4594 | #endif
|
---|
4595 | #ifdef IPPROTO_IGMP
|
---|
4596 | PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
|
---|
4597 | #endif
|
---|
4598 | #ifdef IPPROTO_GGP
|
---|
4599 | PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
|
---|
4600 | #endif
|
---|
4601 | #ifdef IPPROTO_IPV4
|
---|
4602 | PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
|
---|
4603 | #endif
|
---|
4604 | #ifdef IPPROTO_IPV6
|
---|
4605 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
|
---|
4606 | #endif
|
---|
4607 | #ifdef IPPROTO_IPIP
|
---|
4608 | PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
|
---|
4609 | #endif
|
---|
4610 | #ifdef IPPROTO_TCP
|
---|
4611 | PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
|
---|
4612 | #else
|
---|
4613 | PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
|
---|
4614 | #endif
|
---|
4615 | #ifdef IPPROTO_EGP
|
---|
4616 | PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
|
---|
4617 | #endif
|
---|
4618 | #ifdef IPPROTO_PUP
|
---|
4619 | PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
|
---|
4620 | #endif
|
---|
4621 | #ifdef IPPROTO_UDP
|
---|
4622 | PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
|
---|
4623 | #else
|
---|
4624 | PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
|
---|
4625 | #endif
|
---|
4626 | #ifdef IPPROTO_IDP
|
---|
4627 | PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
|
---|
4628 | #endif
|
---|
4629 | #ifdef IPPROTO_HELLO
|
---|
4630 | PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
|
---|
4631 | #endif
|
---|
4632 | #ifdef IPPROTO_ND
|
---|
4633 | PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
|
---|
4634 | #endif
|
---|
4635 | #ifdef IPPROTO_TP
|
---|
4636 | PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
|
---|
4637 | #endif
|
---|
4638 | #ifdef IPPROTO_IPV6
|
---|
4639 | PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
|
---|
4640 | #endif
|
---|
4641 | #ifdef IPPROTO_ROUTING
|
---|
4642 | PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
|
---|
4643 | #endif
|
---|
4644 | #ifdef IPPROTO_FRAGMENT
|
---|
4645 | PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
|
---|
4646 | #endif
|
---|
4647 | #ifdef IPPROTO_RSVP
|
---|
4648 | PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
|
---|
4649 | #endif
|
---|
4650 | #ifdef IPPROTO_GRE
|
---|
4651 | PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
|
---|
4652 | #endif
|
---|
4653 | #ifdef IPPROTO_ESP
|
---|
4654 | PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
|
---|
4655 | #endif
|
---|
4656 | #ifdef IPPROTO_AH
|
---|
4657 | PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
|
---|
4658 | #endif
|
---|
4659 | #ifdef IPPROTO_MOBILE
|
---|
4660 | PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
|
---|
4661 | #endif
|
---|
4662 | #ifdef IPPROTO_ICMPV6
|
---|
4663 | PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
|
---|
4664 | #endif
|
---|
4665 | #ifdef IPPROTO_NONE
|
---|
4666 | PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
|
---|
4667 | #endif
|
---|
4668 | #ifdef IPPROTO_DSTOPTS
|
---|
4669 | PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
|
---|
4670 | #endif
|
---|
4671 | #ifdef IPPROTO_XTP
|
---|
4672 | PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
|
---|
4673 | #endif
|
---|
4674 | #ifdef IPPROTO_EON
|
---|
4675 | PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
|
---|
4676 | #endif
|
---|
4677 | #ifdef IPPROTO_PIM
|
---|
4678 | PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
|
---|
4679 | #endif
|
---|
4680 | #ifdef IPPROTO_IPCOMP
|
---|
4681 | PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
|
---|
4682 | #endif
|
---|
4683 | #ifdef IPPROTO_VRRP
|
---|
4684 | PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
|
---|
4685 | #endif
|
---|
4686 | #ifdef IPPROTO_BIP
|
---|
4687 | PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
|
---|
4688 | #endif
|
---|
4689 | /**/
|
---|
4690 | #ifdef IPPROTO_RAW
|
---|
4691 | PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
|
---|
4692 | #else
|
---|
4693 | PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
|
---|
4694 | #endif
|
---|
4695 | #ifdef IPPROTO_MAX
|
---|
4696 | PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
|
---|
4697 | #endif
|
---|
4698 |
|
---|
4699 | /* Some port configuration */
|
---|
4700 | #ifdef IPPORT_RESERVED
|
---|
4701 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
|
---|
4702 | #else
|
---|
4703 | PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
|
---|
4704 | #endif
|
---|
4705 | #ifdef IPPORT_USERRESERVED
|
---|
4706 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
|
---|
4707 | #else
|
---|
4708 | PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
|
---|
4709 | #endif
|
---|
4710 |
|
---|
4711 | /* Some reserved IP v.4 addresses */
|
---|
4712 | #ifdef INADDR_ANY
|
---|
4713 | PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
|
---|
4714 | #else
|
---|
4715 | PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
|
---|
4716 | #endif
|
---|
4717 | #ifdef INADDR_BROADCAST
|
---|
4718 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
|
---|
4719 | #else
|
---|
4720 | PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
|
---|
4721 | #endif
|
---|
4722 | #ifdef INADDR_LOOPBACK
|
---|
4723 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
|
---|
4724 | #else
|
---|
4725 | PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
|
---|
4726 | #endif
|
---|
4727 | #ifdef INADDR_UNSPEC_GROUP
|
---|
4728 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
|
---|
4729 | #else
|
---|
4730 | PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
|
---|
4731 | #endif
|
---|
4732 | #ifdef INADDR_ALLHOSTS_GROUP
|
---|
4733 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
|
---|
4734 | INADDR_ALLHOSTS_GROUP);
|
---|
4735 | #else
|
---|
4736 | PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
|
---|
4737 | #endif
|
---|
4738 | #ifdef INADDR_MAX_LOCAL_GROUP
|
---|
4739 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
|
---|
4740 | INADDR_MAX_LOCAL_GROUP);
|
---|
4741 | #else
|
---|
4742 | PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
|
---|
4743 | #endif
|
---|
4744 | #ifdef INADDR_NONE
|
---|
4745 | PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
|
---|
4746 | #else
|
---|
4747 | PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
|
---|
4748 | #endif
|
---|
4749 |
|
---|
4750 | /* IPv4 [gs]etsockopt options */
|
---|
4751 | #ifdef IP_OPTIONS
|
---|
4752 | PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
|
---|
4753 | #endif
|
---|
4754 | #ifdef IP_HDRINCL
|
---|
4755 | PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
|
---|
4756 | #endif
|
---|
4757 | #ifdef IP_TOS
|
---|
4758 | PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
|
---|
4759 | #endif
|
---|
4760 | #ifdef IP_TTL
|
---|
4761 | PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
|
---|
4762 | #endif
|
---|
4763 | #ifdef IP_RECVOPTS
|
---|
4764 | PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
|
---|
4765 | #endif
|
---|
4766 | #ifdef IP_RECVRETOPTS
|
---|
4767 | PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
|
---|
4768 | #endif
|
---|
4769 | #ifdef IP_RECVDSTADDR
|
---|
4770 | PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
|
---|
4771 | #endif
|
---|
4772 | #ifdef IP_RETOPTS
|
---|
4773 | PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
|
---|
4774 | #endif
|
---|
4775 | #ifdef IP_MULTICAST_IF
|
---|
4776 | PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
|
---|
4777 | #endif
|
---|
4778 | #ifdef IP_MULTICAST_TTL
|
---|
4779 | PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
|
---|
4780 | #endif
|
---|
4781 | #ifdef IP_MULTICAST_LOOP
|
---|
4782 | PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
|
---|
4783 | #endif
|
---|
4784 | #ifdef IP_ADD_MEMBERSHIP
|
---|
4785 | PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
|
---|
4786 | #endif
|
---|
4787 | #ifdef IP_DROP_MEMBERSHIP
|
---|
4788 | PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
|
---|
4789 | #endif
|
---|
4790 | #ifdef IP_DEFAULT_MULTICAST_TTL
|
---|
4791 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
|
---|
4792 | IP_DEFAULT_MULTICAST_TTL);
|
---|
4793 | #endif
|
---|
4794 | #ifdef IP_DEFAULT_MULTICAST_LOOP
|
---|
4795 | PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
|
---|
4796 | IP_DEFAULT_MULTICAST_LOOP);
|
---|
4797 | #endif
|
---|
4798 | #ifdef IP_MAX_MEMBERSHIPS
|
---|
4799 | PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
|
---|
4800 | #endif
|
---|
4801 |
|
---|
4802 | /* IPv6 [gs]etsockopt options, defined in RFC2553 */
|
---|
4803 | #ifdef IPV6_JOIN_GROUP
|
---|
4804 | PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
|
---|
4805 | #endif
|
---|
4806 | #ifdef IPV6_LEAVE_GROUP
|
---|
4807 | PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
|
---|
4808 | #endif
|
---|
4809 | #ifdef IPV6_MULTICAST_HOPS
|
---|
4810 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
|
---|
4811 | #endif
|
---|
4812 | #ifdef IPV6_MULTICAST_IF
|
---|
4813 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
|
---|
4814 | #endif
|
---|
4815 | #ifdef IPV6_MULTICAST_LOOP
|
---|
4816 | PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
|
---|
4817 | #endif
|
---|
4818 | #ifdef IPV6_UNICAST_HOPS
|
---|
4819 | PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
|
---|
4820 | #endif
|
---|
4821 | /* Additional IPV6 socket options, defined in RFC 3493 */
|
---|
4822 | #ifdef IPV6_V6ONLY
|
---|
4823 | PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);
|
---|
4824 | #endif
|
---|
4825 | /* Advanced IPV6 socket options, from RFC 3542 */
|
---|
4826 | #ifdef IPV6_CHECKSUM
|
---|
4827 | PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);
|
---|
4828 | #endif
|
---|
4829 | #ifdef IPV6_DONTFRAG
|
---|
4830 | PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);
|
---|
4831 | #endif
|
---|
4832 | #ifdef IPV6_DSTOPTS
|
---|
4833 | PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);
|
---|
4834 | #endif
|
---|
4835 | #ifdef IPV6_HOPLIMIT
|
---|
4836 | PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);
|
---|
4837 | #endif
|
---|
4838 | #ifdef IPV6_HOPOPTS
|
---|
4839 | PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);
|
---|
4840 | #endif
|
---|
4841 | #ifdef IPV6_NEXTHOP
|
---|
4842 | PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);
|
---|
4843 | #endif
|
---|
4844 | #ifdef IPV6_PATHMTU
|
---|
4845 | PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);
|
---|
4846 | #endif
|
---|
4847 | #ifdef IPV6_PKTINFO
|
---|
4848 | PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);
|
---|
4849 | #endif
|
---|
4850 | #ifdef IPV6_RECVDSTOPTS
|
---|
4851 | PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);
|
---|
4852 | #endif
|
---|
4853 | #ifdef IPV6_RECVHOPLIMIT
|
---|
4854 | PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);
|
---|
4855 | #endif
|
---|
4856 | #ifdef IPV6_RECVHOPOPTS
|
---|
4857 | PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);
|
---|
4858 | #endif
|
---|
4859 | #ifdef IPV6_RECVPKTINFO
|
---|
4860 | PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);
|
---|
4861 | #endif
|
---|
4862 | #ifdef IPV6_RECVRTHDR
|
---|
4863 | PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);
|
---|
4864 | #endif
|
---|
4865 | #ifdef IPV6_RECVTCLASS
|
---|
4866 | PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);
|
---|
4867 | #endif
|
---|
4868 | #ifdef IPV6_RTHDR
|
---|
4869 | PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);
|
---|
4870 | #endif
|
---|
4871 | #ifdef IPV6_RTHDRDSTOPTS
|
---|
4872 | PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);
|
---|
4873 | #endif
|
---|
4874 | #ifdef IPV6_RTHDR_TYPE_0
|
---|
4875 | PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);
|
---|
4876 | #endif
|
---|
4877 | #ifdef IPV6_RECVPATHMTU
|
---|
4878 | PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);
|
---|
4879 | #endif
|
---|
4880 | #ifdef IPV6_TCLASS
|
---|
4881 | PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);
|
---|
4882 | #endif
|
---|
4883 | #ifdef IPV6_USE_MIN_MTU
|
---|
4884 | PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);
|
---|
4885 | #endif
|
---|
4886 |
|
---|
4887 | /* TCP options */
|
---|
4888 | #ifdef TCP_NODELAY
|
---|
4889 | PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
|
---|
4890 | #endif
|
---|
4891 | #ifdef TCP_MAXSEG
|
---|
4892 | PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
|
---|
4893 | #endif
|
---|
4894 | #ifdef TCP_CORK
|
---|
4895 | PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
|
---|
4896 | #endif
|
---|
4897 | #ifdef TCP_KEEPIDLE
|
---|
4898 | PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
|
---|
4899 | #endif
|
---|
4900 | #ifdef TCP_KEEPINTVL
|
---|
4901 | PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
|
---|
4902 | #endif
|
---|
4903 | #ifdef TCP_KEEPCNT
|
---|
4904 | PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
|
---|
4905 | #endif
|
---|
4906 | #ifdef TCP_SYNCNT
|
---|
4907 | PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
|
---|
4908 | #endif
|
---|
4909 | #ifdef TCP_LINGER2
|
---|
4910 | PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
|
---|
4911 | #endif
|
---|
4912 | #ifdef TCP_DEFER_ACCEPT
|
---|
4913 | PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
|
---|
4914 | #endif
|
---|
4915 | #ifdef TCP_WINDOW_CLAMP
|
---|
4916 | PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
|
---|
4917 | #endif
|
---|
4918 | #ifdef TCP_INFO
|
---|
4919 | PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
|
---|
4920 | #endif
|
---|
4921 | #ifdef TCP_QUICKACK
|
---|
4922 | PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
|
---|
4923 | #endif
|
---|
4924 |
|
---|
4925 |
|
---|
4926 | /* IPX options */
|
---|
4927 | #ifdef IPX_TYPE
|
---|
4928 | PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
|
---|
4929 | #endif
|
---|
4930 |
|
---|
4931 | /* get{addr,name}info parameters */
|
---|
4932 | #ifdef EAI_ADDRFAMILY
|
---|
4933 | PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
|
---|
4934 | #endif
|
---|
4935 | #ifdef EAI_AGAIN
|
---|
4936 | PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
|
---|
4937 | #endif
|
---|
4938 | #ifdef EAI_BADFLAGS
|
---|
4939 | PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
|
---|
4940 | #endif
|
---|
4941 | #ifdef EAI_FAIL
|
---|
4942 | PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
|
---|
4943 | #endif
|
---|
4944 | #ifdef EAI_FAMILY
|
---|
4945 | PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
|
---|
4946 | #endif
|
---|
4947 | #ifdef EAI_MEMORY
|
---|
4948 | PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
|
---|
4949 | #endif
|
---|
4950 | #ifdef EAI_NODATA
|
---|
4951 | PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
|
---|
4952 | #endif
|
---|
4953 | #ifdef EAI_NONAME
|
---|
4954 | PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
|
---|
4955 | #endif
|
---|
4956 | #ifdef EAI_OVERFLOW
|
---|
4957 | PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);
|
---|
4958 | #endif
|
---|
4959 | #ifdef EAI_SERVICE
|
---|
4960 | PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
|
---|
4961 | #endif
|
---|
4962 | #ifdef EAI_SOCKTYPE
|
---|
4963 | PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
|
---|
4964 | #endif
|
---|
4965 | #ifdef EAI_SYSTEM
|
---|
4966 | PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
|
---|
4967 | #endif
|
---|
4968 | #ifdef EAI_BADHINTS
|
---|
4969 | PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
|
---|
4970 | #endif
|
---|
4971 | #ifdef EAI_PROTOCOL
|
---|
4972 | PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
|
---|
4973 | #endif
|
---|
4974 | #ifdef EAI_MAX
|
---|
4975 | PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
|
---|
4976 | #endif
|
---|
4977 | #ifdef AI_PASSIVE
|
---|
4978 | PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
|
---|
4979 | #endif
|
---|
4980 | #ifdef AI_CANONNAME
|
---|
4981 | PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
|
---|
4982 | #endif
|
---|
4983 | #ifdef AI_NUMERICHOST
|
---|
4984 | PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
|
---|
4985 | #endif
|
---|
4986 | #ifdef AI_NUMERICSERV
|
---|
4987 | PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);
|
---|
4988 | #endif
|
---|
4989 | #ifdef AI_MASK
|
---|
4990 | PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
|
---|
4991 | #endif
|
---|
4992 | #ifdef AI_ALL
|
---|
4993 | PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
|
---|
4994 | #endif
|
---|
4995 | #ifdef AI_V4MAPPED_CFG
|
---|
4996 | PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
|
---|
4997 | #endif
|
---|
4998 | #ifdef AI_ADDRCONFIG
|
---|
4999 | PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
|
---|
5000 | #endif
|
---|
5001 | #ifdef AI_V4MAPPED
|
---|
5002 | PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
|
---|
5003 | #endif
|
---|
5004 | #ifdef AI_DEFAULT
|
---|
5005 | PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
|
---|
5006 | #endif
|
---|
5007 | #ifdef NI_MAXHOST
|
---|
5008 | PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
|
---|
5009 | #endif
|
---|
5010 | #ifdef NI_MAXSERV
|
---|
5011 | PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
|
---|
5012 | #endif
|
---|
5013 | #ifdef NI_NOFQDN
|
---|
5014 | PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
|
---|
5015 | #endif
|
---|
5016 | #ifdef NI_NUMERICHOST
|
---|
5017 | PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
|
---|
5018 | #endif
|
---|
5019 | #ifdef NI_NAMEREQD
|
---|
5020 | PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
|
---|
5021 | #endif
|
---|
5022 | #ifdef NI_NUMERICSERV
|
---|
5023 | PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
|
---|
5024 | #endif
|
---|
5025 | #ifdef NI_DGRAM
|
---|
5026 | PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
|
---|
5027 | #endif
|
---|
5028 |
|
---|
5029 | /* shutdown() parameters */
|
---|
5030 | #ifdef SHUT_RD
|
---|
5031 | PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);
|
---|
5032 | #elif defined(SD_RECEIVE)
|
---|
5033 | PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
|
---|
5034 | #else
|
---|
5035 | PyModule_AddIntConstant(m, "SHUT_RD", 0);
|
---|
5036 | #endif
|
---|
5037 | #ifdef SHUT_WR
|
---|
5038 | PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);
|
---|
5039 | #elif defined(SD_SEND)
|
---|
5040 | PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
|
---|
5041 | #else
|
---|
5042 | PyModule_AddIntConstant(m, "SHUT_WR", 1);
|
---|
5043 | #endif
|
---|
5044 | #ifdef SHUT_RDWR
|
---|
5045 | PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);
|
---|
5046 | #elif defined(SD_BOTH)
|
---|
5047 | PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
|
---|
5048 | #else
|
---|
5049 | PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
|
---|
5050 | #endif
|
---|
5051 |
|
---|
5052 | /* Initialize gethostbyname lock */
|
---|
5053 | #if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
|
---|
5054 | netdb_lock = PyThread_allocate_lock();
|
---|
5055 | #endif
|
---|
5056 | }
|
---|
5057 |
|
---|
5058 |
|
---|
5059 | #ifndef HAVE_INET_PTON
|
---|
5060 |
|
---|
5061 | /* Simplistic emulation code for inet_pton that only works for IPv4 */
|
---|
5062 | /* These are not exposed because they do not set errno properly */
|
---|
5063 |
|
---|
5064 | int
|
---|
5065 | inet_pton(int af, const char *src, void *dst)
|
---|
5066 | {
|
---|
5067 | if (af == AF_INET) {
|
---|
5068 | long packed_addr;
|
---|
5069 | packed_addr = inet_addr(src);
|
---|
5070 | if (packed_addr == INADDR_NONE)
|
---|
5071 | return 0;
|
---|
5072 | memcpy(dst, &packed_addr, 4);
|
---|
5073 | return 1;
|
---|
5074 | }
|
---|
5075 | /* Should set errno to EAFNOSUPPORT */
|
---|
5076 | return -1;
|
---|
5077 | }
|
---|
5078 |
|
---|
5079 | const char *
|
---|
5080 | inet_ntop(int af, const void *src, char *dst, socklen_t size)
|
---|
5081 | {
|
---|
5082 | if (af == AF_INET) {
|
---|
5083 | struct in_addr packed_addr;
|
---|
5084 | if (size < 16)
|
---|
5085 | /* Should set errno to ENOSPC. */
|
---|
5086 | return NULL;
|
---|
5087 | memcpy(&packed_addr, src, sizeof(packed_addr));
|
---|
5088 | return strncpy(dst, inet_ntoa(packed_addr), size);
|
---|
5089 | }
|
---|
5090 | /* Should set errno to EAFNOSUPPORT */
|
---|
5091 | return NULL;
|
---|
5092 | }
|
---|
5093 |
|
---|
5094 | #endif
|
---|