blob: 21270a3d9f47b3200b0f2c53828a163c93270ac2 [file] [log] [blame]
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001/* Socket module */
2
3/*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005This module provides an interface to Berkeley socket IPC.
6
7Limitations:
8
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00009- Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a
Martin v. Löwis11017b12006-01-14 18:12:57 +000010 portable manner, though AF_PACKET and AF_NETLINK are supported under Linux.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000011- 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).
Guido van Rossum6574b3e1991-06-25 21:36:08 +000014
Guido van Rossum27e177d1995-03-16 15:43:47 +000015Module interface:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000016
Guido van Rossum27e177d1995-03-16 15:43:47 +000017- socket.error: exception raised for socket specific errors
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000018- 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
Guido van Rossum83a072d2002-09-03 19:10:18 +000022- socket.fromfd(fd, family, type[, proto]) --> new socket object (created
23 from an existing file descriptor)
Guido van Rossum30a685f1991-06-27 15:51:29 +000024- socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000025- socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])
Guido van Rossum27e177d1995-03-16 15:43:47 +000026- socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')
Guido van Rossum25405c71996-12-19 16:42:52 +000027- socket.getprotobyname(protocolname) --> protocol number
Barry Warsaw11b91a02004-06-28 00:50:43 +000028- socket.getservbyname(servicename[, protocolname]) --> port number
29- socket.getservbyport(portnumber[, protocolname]) --> service name
Guido van Rossum83a072d2002-09-03 19:10:18 +000030- socket.socket([family[, type [, proto]]]) --> new socket object
Dave Cole331708b2004-08-09 04:51:41 +000031- socket.socketpair([family[, type [, proto]]]) --> (socket, socket)
Guido van Rossum006bf911996-06-12 04:04:55 +000032- 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
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000036- socket.getaddrinfo(host, port [, family, socktype, proto, flags])
37 --> List of (family, socktype, proto, canonname, sockaddr)
38- socket.getnameinfo(sockaddr, flags) --> (host, port)
Guido van Rossum27e177d1995-03-16 15:43:47 +000039- socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
Guido van Rossum47dfa4a2003-04-25 05:48:32 +000040- socket.has_ipv6: boolean value indicating if IPv6 is supported
Guido van Rossum5c9eb211999-08-20 18:21:51 +000041- socket.inet_aton(IP address) -> 32-bit packed IP representation
42- socket.inet_ntoa(packed IP) -> IP address string
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +000043- socket.getdefaulttimeout() -> None | float
44- socket.setdefaulttimeout(None | float)
Guido van Rossum6574b3e1991-06-25 21:36:08 +000045- 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
Guido van Rossum27e177d1995-03-16 15:43:47 +000049- a UNIX domain socket address is a string specifying the pathname
Jeremy Hylton22308652001-02-02 03:23:09 +000050- 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:
Jeremy Hyltondbfb6622001-02-02 19:55:17 +000053 ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +000054 specify packet-type and ha-type/addr.
Guido van Rossum6574b3e1991-06-25 21:36:08 +000055
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000056Local naming conventions:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000057
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000058- 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
Guido van Rossum30a685f1991-06-27 15:51:29 +000061
Guido van Rossum6574b3e1991-06-25 21:36:08 +000062*/
63
Ronald Oussorend06b6f22006-04-23 11:59:25 +000064#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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000073#include "Python.h"
Georg Brandlbc45a3f2006-03-17 19:17:34 +000074#include "structmember.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000075
Guido van Rossum47dfa4a2003-04-25 05:48:32 +000076#undef MAX
77#define MAX(x, y) ((x) < (y) ? (y) : (x))
78
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000079/* Socket object documentation */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000080PyDoc_STRVAR(sock_doc,
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000081"socket([family[, type[, proto]]]) -> socket object\n\
82\n\
83Open a socket of the given type. The family argument specifies the\n\
84address family; it defaults to AF_INET. The type argument specifies\n\
85whether this is a stream (SOCK_STREAM, this is the default)\n\
86or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
87specifying the default protocol. Keyword arguments are accepted.\n\
88\n\
89A socket object represents one endpoint of a network connection.\n\
90\n\
91Methods of socket objects (keyword arguments not allowed):\n\
92\n\
93accept() -- accept a connection, returning new socket and client address\n\
94bind(addr) -- bind the socket to a local address\n\
95close() -- close the socket\n\
96connect(addr) -- connect the socket to a remote address\n\
97connect_ex(addr) -- connect, return an error code instead of an exception\n\
98dup() -- return a new socket object identical to the current one [*]\n\
99fileno() -- return underlying file descriptor\n\
100getpeername() -- return remote address [*]\n\
101getsockname() -- return local address\n\
102getsockopt(level, optname[, buflen]) -- get socket options\n\
103gettimeout() -- return timeout or None\n\
104listen(n) -- start listening for incoming connections\n\
105makefile([mode, [bufsize]]) -- return a file object for the socket [*]\n\
106recv(buflen[, flags]) -- receive data\n\
Martin Blaisaf2ae722006-06-04 13:49:49 +0000107recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
Martin Blais2856e5f2006-05-26 12:03:27 +0000108recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\
Martin Blaisaf2ae722006-06-04 13:49:49 +0000109recvfrom_into(buffer[, nbytes, [, flags])\n\
Martin Blais2856e5f2006-05-26 12:03:27 +0000110 -- receive data and sender\'s address (into a buffer)\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000111sendall(data[, flags]) -- send all data\n\
112send(data[, flags]) -- send data, may not send all of it\n\
113sendto(data[, flags], addr) -- send data to a given address\n\
114setblocking(0 | 1) -- set or clear the blocking I/O flag\n\
115setsockopt(level, optname, value) -- set socket options\n\
116settimeout(None | float) -- set or clear the timeout\n\
117shutdown(how) -- shut down traffic in one or both directions\n\
118\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000119 [*] not available on all platforms!");
Guido van Rossum3baaa131999-03-22 21:44:51 +0000120
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000121/* XXX This is a terrible mess of platform-dependent preprocessor hacks.
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000122 I hope some day someone can clean this up please... */
123
Guido van Rossum9376b741999-09-15 22:01:40 +0000124/* 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
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000130# undef HAVE_GETHOSTBYNAME_R_3_ARG
131# undef HAVE_GETHOSTBYNAME_R_5_ARG
132# undef HAVE_GETHOSTBYNAME_R_6_ARG
Guido van Rossum9376b741999-09-15 22:01:40 +0000133#endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000134
Guido van Rossum7a122991999-04-13 04:07:32 +0000135#ifndef WITH_THREAD
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000136# undef HAVE_GETHOSTBYNAME_R
Guido van Rossum7a122991999-04-13 04:07:32 +0000137#endif
138
Guido van Rossume7de2061999-03-24 17:24:33 +0000139#ifdef HAVE_GETHOSTBYNAME_R
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000140# 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)
Guido van Rossum9376b741999-09-15 22:01:40 +0000145/* Rely on the configure script */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000146# else
147# undef HAVE_GETHOSTBYNAME_R
148# endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000149#endif
150
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000151#if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \
152 !defined(MS_WINDOWS)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000153# define USE_GETHOSTBYNAME_LOCK
Guido van Rossum3baaa131999-03-22 21:44:51 +0000154#endif
155
Hye-Shik Chang9ceebd52005-09-24 14:58:47 +0000156/* To use __FreeBSD_version */
157#ifdef HAVE_SYS_PARAM_H
158#include <sys/param.h>
159#endif
Just van Rossum1040d2c2003-05-09 07:53:18 +0000160/* On systems on which getaddrinfo() is believed to not be thread-safe,
Just van Rossum09aecd72003-05-09 08:03:44 +0000161 (this includes the getaddrinfo emulation) protect access with a lock. */
Hye-Shik Chang9ceebd52005-09-24 14:58:47 +0000162#if defined(WITH_THREAD) && (defined(__APPLE__) || \
163 (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \
Neal Norwitz2a30cd02006-07-10 01:18:57 +0000164 defined(__OpenBSD__) || defined(__NetBSD__) || \
165 defined(__VMS) || !defined(HAVE_GETADDRINFO))
Just van Rossum1040d2c2003-05-09 07:53:18 +0000166#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)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000178# include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000179#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000180
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000181#if defined(PYCC_VACPP)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000182# include <types.h>
183# include <io.h>
184# include <sys/ioctl.h>
185# include <utils.h>
186# include <ctype.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000187#endif
188
Martin v. Löwis9e437302002-12-06 12:57:26 +0000189#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000190# include <ioctl.h>
191#endif
192
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000193#if defined(PYOS_OS2)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000194# define INCL_DOS
195# define INCL_DOSERRORS
196# define INCL_NOPMAPI
197# include <os2.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000198#endif
199
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000200#if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000201/* 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) */
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000204
Martin Blais2856e5f2006-05-26 12:03:27 +0000205/* XXX Using _SGIAPI is the wrong thing,
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000206 but I don't know what the right thing is. */
Trent Mick8ea5bdf2004-09-13 17:48:41 +0000207#undef _SGIAPI /* to avoid warning */
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000208#define _SGIAPI 1
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000209
Trent Mick8ea5bdf2004-09-13 17:48:41 +0000210#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
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000219#define HAVE_INET_PTON
220#include <netdb.h>
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000221#endif
222
Martin Blais2856e5f2006-05-26 12:03:27 +0000223/* 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
Georg Brandldbd83392006-02-20 09:42:33 +0000225 are just busted. Same thing for Solaris. */
226#if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN)
Anthony Baxterbab23cf2003-10-04 08:00:49 +0000227#define INET_ADDRSTRLEN 16
228#endif
229
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000230/* Generic includes */
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000231#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232#include <sys/types.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000233#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000234
Marc-André Lemburg976ade62002-02-16 18:47:07 +0000235/* Generic socket object definitions and includes */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000236#define PySocket_BUILDING_SOCKET
Marc-André Lemburgbb8b78b2002-02-16 18:44:52 +0000237#include "socketmodule.h"
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000238
239/* Addressing includes */
240
Guido van Rossum6f489d91996-06-28 20:15:15 +0000241#ifndef MS_WINDOWS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000242
243/* Non-MS WINDOWS includes */
244# include <netdb.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000245
Guido van Rossum9376b741999-09-15 22:01:40 +0000246/* Headers needed for inet_ntoa() and inet_addr() */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000247# ifdef __BEOS__
248# include <net/netdb.h>
249# elif defined(PYOS_OS2) && defined(PYCC_VACPP)
250# include <netdb.h>
Tim Peters603c6832001-11-05 02:45:59 +0000251typedef size_t socklen_t;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000252# else
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000253# include <arpa/inet.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000254# endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000255
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000256# ifndef RISCOS
257# include <fcntl.h>
258# else
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000259# include <sys/ioctl.h>
260# include <socklib.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000261# define NO_DUP
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000262int h_errno; /* not used */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000263# define INET_ADDRSTRLEN 16
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000264# endif
265
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000266#else
Guido van Rossum48a680c2001-03-02 06:34:14 +0000267
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000268/* MS_WINDOWS includes */
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000269# ifdef HAVE_FCNTL_H
270# include <fcntl.h>
271# endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000272
Jeremy Hylton22308652001-02-02 03:23:09 +0000273#endif
274
Skip Montanaro7befb992004-02-10 16:50:21 +0000275#include <stddef.h>
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000276
277#ifndef offsetof
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000278# define offsetof(type, member) ((size_t)(&((type *)0)->member))
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000279#endif
280
Neal Norwitz39d22e52002-11-02 19:55:21 +0000281#ifndef O_NONBLOCK
282# define O_NONBLOCK O_NDELAY
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000283#endif
284
Trent Micka708d6e2004-09-07 17:48:26 +0000285/* 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 */
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000291#elif defined(_MSC_VER) && _MSC_VER>1201
Trent Micka708d6e2004-09-07 17:48:26 +0000292 /* 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
Jason Tishlerc246cb72004-08-09 13:25:59 +0000298
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000299#ifndef HAVE_INET_PTON
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000300int inet_pton(int af, const char *src, void *dst);
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000301const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000302#endif
303
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000304#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. */
Jack Jansen84262fb2002-07-02 14:40:42 +0000313#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 */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000317#undef HAVE_GETADDRINFO
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000318#endif
Ronald Oussorend06b6f22006-04-23 11:59:25 +0000319
320#ifdef HAVE_INET_ATON
321#define USE_INET_ATON_WEAKLINK
322#endif
323
Jack Jansen84262fb2002-07-02 14:40:42 +0000324#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000325
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000326/* I know this is a bad practice, but it is the easiest... */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000327#if !defined(HAVE_GETADDRINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000328/* 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
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000332#include "getaddrinfo.c"
333#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000334#if !defined(HAVE_GETNAMEINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000335#define getnameinfo fake_getnameinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000336#include "getnameinfo.c"
337#endif
338
Guido van Rossumbcc20741998-08-04 22:53:56 +0000339#if defined(MS_WINDOWS) || defined(__BEOS__)
340/* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000341/* seem to be a few differences in the API */
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000342#define SOCKETCLOSE closesocket
Guido van Rossumbe32c891996-06-20 16:25:29 +0000343#define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000344#endif
345
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000346#ifdef MS_WIN32
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000347#define EAFNOSUPPORT WSAEAFNOSUPPORT
348#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000349#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000350
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000351#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000352#define SOCKETCLOSE soclose
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000353#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000354#endif
355
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000356#ifndef SOCKETCLOSE
357#define SOCKETCLOSE close
358#endif
359
Hye-Shik Chang81268602004-02-02 06:05:24 +0000360#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
Martin v. Löwis45423a72007-02-14 10:07:37 +0000365#define BTPROTO_HCI BLUETOOTH_PROTO_HCI
Hye-Shik Chang81268602004-02-02 06:05:24 +0000366#define sockaddr_l2 sockaddr_l2cap
367#define sockaddr_rc sockaddr_rfcomm
Hye-Shik Chang81268602004-02-02 06:05:24 +0000368#define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb)
369#define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb)
Martin v. Löwis45423a72007-02-14 10:07:37 +0000370#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Matt Flemingec926502006-07-28 11:27:27 +0000371#elif defined(__NetBSD__)
372#define sockaddr_l2 sockaddr_bt
373#define sockaddr_rc sockaddr_bt
Martin v. Löwis45423a72007-02-14 10:07:37 +0000374#define sockaddr_hci sockaddr_bt
Matt Flemingec926502006-07-28 11:27:27 +0000375#define sockaddr_sco sockaddr_bt
Matt Flemingec926502006-07-28 11:27:27 +0000376#define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb)
377#define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb)
Martin v. Löwis45423a72007-02-14 10:07:37 +0000378#define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb)
Matt Flemingec926502006-07-28 11:27:27 +0000379#define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000380#else
Hye-Shik Chang81268602004-02-02 06:05:24 +0000381#define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)
382#define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)
Martin v. Löwis45423a72007-02-14 10:07:37 +0000383#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000384#define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)
385#endif
386#endif
387
Neal Norwitz2a30cd02006-07-10 01:18:57 +0000388#ifdef __VMS
389/* TCP/IP Services for VMS uses a maximum send/recv buffer length */
390#define SEGMENT_SIZE (32 * 1024 -1)
391#endif
392
Martin v. Löwis046c4d12006-12-03 11:23:45 +0000393#define SAS2SA(x) ((struct sockaddr *)(x))
394
Martin v. Löwise9416172003-05-03 10:12:45 +0000395/*
396 * Constants for getnameinfo()
397 */
398#if !defined(NI_MAXHOST)
399#define NI_MAXHOST 1025
400#endif
401#if !defined(NI_MAXSERV)
402#define NI_MAXSERV 32
403#endif
404
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000405/* XXX There's a problem here: *static* functions are not supposed to have
406 a Py prefix (or use CapitalizedWords). Later... */
407
Guido van Rossum30a685f1991-06-27 15:51:29 +0000408/* Global variable holding the exception type for errors detected
409 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000410static PyObject *socket_error;
411static PyObject *socket_herror;
412static PyObject *socket_gaierror;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000413static PyObject *socket_timeout;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000414
Guido van Rossum48a680c2001-03-02 06:34:14 +0000415#ifdef RISCOS
416/* Global variable which is !=0 if Python is running in a RISC OS taskwindow */
417static int taskwindow;
418#endif
419
Tim Peters643a7fc2002-02-17 04:13:21 +0000420/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000421 The sock_type variable contains pointers to various functions,
422 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000423 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000424static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000425
Anthony Baxter93ab5fa2006-07-11 02:04:09 +0000426#if defined(HAVE_POLL_H)
427#include <poll.h>
428#elif defined(HAVE_SYS_POLL_H)
429#include <sys/poll.h>
430#endif
431
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000432#ifdef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE
433/* Platform can select file descriptors beyond FD_SETSIZE */
434#define IS_SELECTABLE(s) 1
Anthony Baxter93ab5fa2006-07-11 02:04:09 +0000435#elif defined(HAVE_POLL)
436/* Instead of select(), we'll use poll() since poll() works on any fd. */
437#define IS_SELECTABLE(s) 1
438/* Can we call select() with this socket without a buffer overrun? */
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000439#else
440/* POSIX says selecting file descriptors beyond FD_SETSIZE
Anthony Baxter93ab5fa2006-07-11 02:04:09 +0000441 has undefined behaviour. If there's no timeout left, we don't have to
442 call select, so it's a safe, little white lie. */
443#define IS_SELECTABLE(s) ((s)->sock_fd < FD_SETSIZE || s->sock_timeout <= 0.0)
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000444#endif
Neal Norwitz082b2df2006-02-07 07:04:46 +0000445
446static PyObject*
447select_error(void)
448{
449 PyErr_SetString(socket_error, "unable to select on socket");
450 return NULL;
451}
452
Guido van Rossum30a685f1991-06-27 15:51:29 +0000453/* Convenience function to raise an error according to errno
454 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000455
Guido van Rossum73624e91994-10-10 17:59:00 +0000456static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000457set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000458{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000459#ifdef MS_WINDOWS
Mark Hammond46a733d2000-07-24 01:45:11 +0000460 int err_no = WSAGetLastError();
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000461 static struct {
462 int no;
463 const char *msg;
464 } *msgp, msgs[] = {
465 {WSAEINTR, "Interrupted system call"},
466 {WSAEBADF, "Bad file descriptor"},
467 {WSAEACCES, "Permission denied"},
468 {WSAEFAULT, "Bad address"},
469 {WSAEINVAL, "Invalid argument"},
470 {WSAEMFILE, "Too many open files"},
471 {WSAEWOULDBLOCK,
472 "The socket operation could not complete "
473 "without blocking"},
474 {WSAEINPROGRESS, "Operation now in progress"},
475 {WSAEALREADY, "Operation already in progress"},
476 {WSAENOTSOCK, "Socket operation on non-socket"},
477 {WSAEDESTADDRREQ, "Destination address required"},
478 {WSAEMSGSIZE, "Message too long"},
479 {WSAEPROTOTYPE, "Protocol wrong type for socket"},
480 {WSAENOPROTOOPT, "Protocol not available"},
481 {WSAEPROTONOSUPPORT, "Protocol not supported"},
482 {WSAESOCKTNOSUPPORT, "Socket type not supported"},
483 {WSAEOPNOTSUPP, "Operation not supported"},
484 {WSAEPFNOSUPPORT, "Protocol family not supported"},
485 {WSAEAFNOSUPPORT, "Address family not supported"},
486 {WSAEADDRINUSE, "Address already in use"},
487 {WSAEADDRNOTAVAIL, "Can't assign requested address"},
488 {WSAENETDOWN, "Network is down"},
489 {WSAENETUNREACH, "Network is unreachable"},
490 {WSAENETRESET, "Network dropped connection on reset"},
491 {WSAECONNABORTED, "Software caused connection abort"},
492 {WSAECONNRESET, "Connection reset by peer"},
493 {WSAENOBUFS, "No buffer space available"},
494 {WSAEISCONN, "Socket is already connected"},
495 {WSAENOTCONN, "Socket is not connected"},
496 {WSAESHUTDOWN, "Can't send after socket shutdown"},
497 {WSAETOOMANYREFS, "Too many references: can't splice"},
498 {WSAETIMEDOUT, "Operation timed out"},
499 {WSAECONNREFUSED, "Connection refused"},
500 {WSAELOOP, "Too many levels of symbolic links"},
501 {WSAENAMETOOLONG, "File name too long"},
502 {WSAEHOSTDOWN, "Host is down"},
503 {WSAEHOSTUNREACH, "No route to host"},
504 {WSAENOTEMPTY, "Directory not empty"},
505 {WSAEPROCLIM, "Too many processes"},
506 {WSAEUSERS, "Too many users"},
507 {WSAEDQUOT, "Disc quota exceeded"},
508 {WSAESTALE, "Stale NFS file handle"},
509 {WSAEREMOTE, "Too many levels of remote in path"},
510 {WSASYSNOTREADY, "Network subsystem is unvailable"},
511 {WSAVERNOTSUPPORTED, "WinSock version is not supported"},
512 {WSANOTINITIALISED,
513 "Successful WSAStartup() not yet performed"},
514 {WSAEDISCON, "Graceful shutdown in progress"},
515 /* Resolver errors */
516 {WSAHOST_NOT_FOUND, "No such host is known"},
517 {WSATRY_AGAIN, "Host not found, or server failed"},
518 {WSANO_RECOVERY, "Unexpected server error encountered"},
519 {WSANO_DATA, "Valid name without requested data"},
520 {WSANO_ADDRESS, "No address, look for MX record"},
521 {0, NULL}
522 };
Mark Hammond46a733d2000-07-24 01:45:11 +0000523 if (err_no) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000524 PyObject *v;
Mark Hammond46a733d2000-07-24 01:45:11 +0000525 const char *msg = "winsock error";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000526
Mark Hammond46a733d2000-07-24 01:45:11 +0000527 for (msgp = msgs; msgp->msg; msgp++) {
528 if (err_no == msgp->no) {
529 msg = msgp->msg;
530 break;
531 }
532 }
533
534 v = Py_BuildValue("(is)", err_no, msg);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000535 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000536 PyErr_SetObject(socket_error, v);
Guido van Rossum73624e91994-10-10 17:59:00 +0000537 Py_DECREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000538 }
539 return NULL;
540 }
541 else
542#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000543
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000544#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000545 if (sock_errno() != NO_ERROR) {
546 APIRET rc;
547 ULONG msglen;
548 char outbuf[100];
549 int myerrorcode = sock_errno();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000550
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000551 /* Retrieve socket-related error message from MPTN.MSG file */
552 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
553 myerrorcode - SOCBASEERR + 26,
554 "mptn.msg",
555 &msglen);
556 if (rc == NO_ERROR) {
557 PyObject *v;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000558
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000559 /* OS/2 doesn't guarantee a terminator */
560 outbuf[msglen] = '\0';
561 if (strlen(outbuf) > 0) {
562 /* If non-empty msg, trim CRLF */
563 char *lastc = &outbuf[ strlen(outbuf)-1 ];
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000564 while (lastc > outbuf &&
565 isspace(Py_CHARMASK(*lastc))) {
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000566 /* Trim trailing whitespace (CRLF) */
567 *lastc-- = '\0';
568 }
569 }
570 v = Py_BuildValue("(is)", myerrorcode, outbuf);
571 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000572 PyErr_SetObject(socket_error, v);
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000573 Py_DECREF(v);
574 }
575 return NULL;
576 }
577 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000578#endif
579
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000580#if defined(RISCOS)
581 if (_inet_error.errnum != NULL) {
582 PyObject *v;
583 v = Py_BuildValue("(is)", errno, _inet_err());
584 if (v != NULL) {
585 PyErr_SetObject(socket_error, v);
586 Py_DECREF(v);
587 }
588 return NULL;
589 }
590#endif
591
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000592 return PyErr_SetFromErrno(socket_error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000593}
594
Guido van Rossum30a685f1991-06-27 15:51:29 +0000595
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000596static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000597set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000598{
599 PyObject *v;
600
601#ifdef HAVE_HSTRERROR
602 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
603#else
604 v = Py_BuildValue("(is)", h_error, "host not found");
605#endif
606 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000607 PyErr_SetObject(socket_herror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000608 Py_DECREF(v);
609 }
610
611 return NULL;
612}
613
614
615static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000616set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000617{
618 PyObject *v;
619
Martin v. Löwis272cb402002-03-01 08:31:07 +0000620#ifdef EAI_SYSTEM
621 /* EAI_SYSTEM is not available on Windows XP. */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000622 if (error == EAI_SYSTEM)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000623 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000624#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000625
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000626#ifdef HAVE_GAI_STRERROR
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000627 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000628#else
629 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
630#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000631 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000632 PyErr_SetObject(socket_gaierror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000633 Py_DECREF(v);
634 }
635
636 return NULL;
637}
638
Neal Norwitz2a30cd02006-07-10 01:18:57 +0000639#ifdef __VMS
640/* Function to send in segments */
641static int
642sendsegmented(int sock_fd, char *buf, int len, int flags)
643{
644 int n = 0;
645 int remaining = len;
646
647 while (remaining > 0) {
648 unsigned int segment;
649
650 segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining);
651 n = send(sock_fd, buf, segment, flags);
652 if (n < 0) {
653 return n;
654 }
655 remaining -= segment;
656 buf += segment;
657 } /* end while */
658
659 return len;
660}
661#endif
662
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000663/* Function to perform the setting of socket blocking mode
664 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000665static int
666internal_setblocking(PySocketSockObject *s, int block)
667{
668#ifndef RISCOS
669#ifndef MS_WINDOWS
670 int delay_flag;
671#endif
672#endif
673
674 Py_BEGIN_ALLOW_THREADS
675#ifdef __BEOS__
676 block = !block;
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000677 setsockopt(s->sock_fd, SOL_SOCKET, SO_NONBLOCK,
678 (void *)(&block), sizeof(int));
Guido van Rossum67f7a382002-06-06 21:08:16 +0000679#else
680#ifndef RISCOS
681#ifndef MS_WINDOWS
682#if defined(PYOS_OS2) && !defined(PYCC_GCC)
683 block = !block;
684 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000685#elif defined(__VMS)
686 block = !block;
Neal Norwitz2a30cd02006-07-10 01:18:57 +0000687 ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);
688#else /* !PYOS_OS2 && !__VMS */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000689 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
690 if (block)
Neal Norwitz39d22e52002-11-02 19:55:21 +0000691 delay_flag &= (~O_NONBLOCK);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000692 else
Neal Norwitz39d22e52002-11-02 19:55:21 +0000693 delay_flag |= O_NONBLOCK;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000694 fcntl(s->sock_fd, F_SETFL, delay_flag);
695#endif /* !PYOS_OS2 */
696#else /* MS_WINDOWS */
697 block = !block;
698 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
699#endif /* MS_WINDOWS */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000700#else /* RISCOS */
701 block = !block;
702 socketioctl(s->sock_fd, FIONBIO, (u_long*)&block);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000703#endif /* RISCOS */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000704#endif /* __BEOS__ */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000705 Py_END_ALLOW_THREADS
706
707 /* Since these don't return anything */
708 return 1;
709}
710
Anthony Baxter93ab5fa2006-07-11 02:04:09 +0000711/* Do a select()/poll() on the socket, if necessary (sock_timeout > 0).
Guido van Rossum11ba0942002-06-13 15:07:44 +0000712 The argument writing indicates the direction.
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000713 This does not raise an exception; we'll let our caller do that
714 after they've reacquired the interpreter lock.
Neal Norwitz9b0ca792006-08-02 06:46:21 +0000715 Returns 1 on timeout, -1 on error, 0 otherwise. */
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000716static int
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000717internal_select(PySocketSockObject *s, int writing)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000718{
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000719 int n;
Guido van Rossum11ba0942002-06-13 15:07:44 +0000720
Guido van Rossumad654902002-07-19 12:44:59 +0000721 /* Nothing to do unless we're in timeout mode (not non-blocking) */
Guido van Rossum11ba0942002-06-13 15:07:44 +0000722 if (s->sock_timeout <= 0.0)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000723 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000724
Guido van Rossumad654902002-07-19 12:44:59 +0000725 /* Guard against closed socket */
726 if (s->sock_fd < 0)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000727 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000728
Anthony Baxter93ab5fa2006-07-11 02:04:09 +0000729 /* Prefer poll, if available, since you can poll() any fd
730 * which can't be done with select(). */
731#ifdef HAVE_POLL
732 {
733 struct pollfd pollfd;
734 int timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000735
Anthony Baxter93ab5fa2006-07-11 02:04:09 +0000736 pollfd.fd = s->sock_fd;
737 pollfd.events = writing ? POLLOUT : POLLIN;
738
739 /* s->sock_timeout is in seconds, timeout in ms */
740 timeout = (int)(s->sock_timeout * 1000 + 0.5);
741 n = poll(&pollfd, 1, timeout);
742 }
743#else
744 {
745 /* Construct the arguments to select */
746 fd_set fds;
747 struct timeval tv;
748 tv.tv_sec = (int)s->sock_timeout;
749 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
750 FD_ZERO(&fds);
751 FD_SET(s->sock_fd, &fds);
752
753 /* See if the socket is ready */
754 if (writing)
755 n = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
756 else
757 n = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
758 }
759#endif
Neal Norwitz9b0ca792006-08-02 06:46:21 +0000760
761 if (n < 0)
762 return -1;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000763 if (n == 0)
764 return 1;
765 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000766}
767
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000768/* Initialize a new socket object. */
769
Tim Petersa12b4cf2002-07-18 22:38:44 +0000770static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000771
Mark Hammond62b1ab12002-07-23 06:31:15 +0000772PyMODINIT_FUNC
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000773init_sockobject(PySocketSockObject *s,
774 SOCKET_T fd, int family, int type, int proto)
775{
776#ifdef RISCOS
777 int block = 1;
778#endif
779 s->sock_fd = fd;
780 s->sock_family = family;
781 s->sock_type = type;
782 s->sock_proto = proto;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000783 s->sock_timeout = defaulttimeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000784
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000785 s->errorhandler = &set_error;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000786
787 if (defaulttimeout >= 0.0)
788 internal_setblocking(s, 0);
789
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000790#ifdef RISCOS
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000791 if (taskwindow)
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000792 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000793#endif
794}
795
796
Guido van Rossum30a685f1991-06-27 15:51:29 +0000797/* Create a new socket object.
798 This just creates the object and initializes it.
799 If the creation fails, return NULL and set an exception (implicit
800 in NEWOBJ()). */
801
Guido van Rossum73624e91994-10-10 17:59:00 +0000802static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000803new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000804{
Guido van Rossum73624e91994-10-10 17:59:00 +0000805 PySocketSockObject *s;
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000806 s = (PySocketSockObject *)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000807 PyType_GenericNew(&sock_type, NULL, NULL);
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000808 if (s != NULL)
809 init_sockobject(s, fd, family, type, proto);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000810 return s;
811}
812
Guido van Rossum30a685f1991-06-27 15:51:29 +0000813
Guido van Rossum48a680c2001-03-02 06:34:14 +0000814/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000815 thread to be in gethostbyname or getaddrinfo */
816#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
817PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000818#endif
819
820
Guido van Rossum30a685f1991-06-27 15:51:29 +0000821/* Convert a string specifying a host name or one of a few symbolic
822 names to a numeric IP address. This usually calls gethostbyname()
823 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000824 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000825 an error occurred; then an exception is raised. */
826
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000827static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000828setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000829{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000830 struct addrinfo hints, *res;
831 int error;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000832 int d1, d2, d3, d4;
833 char ch;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000834
Guido van Rossuma376cc51996-12-05 23:43:35 +0000835 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
Guido van Rossum30a685f1991-06-27 15:51:29 +0000836 if (name[0] == '\0') {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000837 int siz;
838 memset(&hints, 0, sizeof(hints));
839 hints.ai_family = af;
840 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
841 hints.ai_flags = AI_PASSIVE;
Just van Rossum1040d2c2003-05-09 07:53:18 +0000842 Py_BEGIN_ALLOW_THREADS
843 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000844 error = getaddrinfo(NULL, "0", &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +0000845 Py_END_ALLOW_THREADS
846 /* We assume that those thread-unsafe getaddrinfo() versions
847 *are* safe regarding their return value, ie. that a
848 subsequent call to getaddrinfo() does not destroy the
849 outcome of the first call. */
850 RELEASE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000851 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000852 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000853 return -1;
854 }
855 switch (res->ai_family) {
856 case AF_INET:
857 siz = 4;
858 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000859#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000860 case AF_INET6:
861 siz = 16;
862 break;
863#endif
864 default:
865 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000866 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000867 "unsupported address family");
868 return -1;
869 }
870 if (res->ai_next) {
Martin v. Löwisf0b11d22001-11-07 08:31:03 +0000871 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000872 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000873 "wildcard resolved to multiple address");
874 return -1;
875 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000876 if (res->ai_addrlen < addr_ret_size)
877 addr_ret_size = res->ai_addrlen;
878 memcpy(addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000879 freeaddrinfo(res);
880 return siz;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000881 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000882 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000883 struct sockaddr_in *sin;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +0000884 if (af != AF_INET && af != AF_UNSPEC) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000885 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000886 "address family mismatched");
887 return -1;
888 }
889 sin = (struct sockaddr_in *)addr_ret;
890 memset((void *) sin, '\0', sizeof(*sin));
891 sin->sin_family = AF_INET;
892#ifdef HAVE_SOCKADDR_SA_LEN
893 sin->sin_len = sizeof(*sin);
894#endif
895 sin->sin_addr.s_addr = INADDR_BROADCAST;
896 return sizeof(sin->sin_addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000897 }
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000898 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
899 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
900 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
901 struct sockaddr_in *sin;
902 sin = (struct sockaddr_in *)addr_ret;
903 sin->sin_addr.s_addr = htonl(
904 ((long) d1 << 24) | ((long) d2 << 16) |
905 ((long) d3 << 8) | ((long) d4 << 0));
906 sin->sin_family = AF_INET;
907#ifdef HAVE_SOCKADDR_SA_LEN
908 sin->sin_len = sizeof(*sin);
909#endif
910 return 4;
911 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000912 memset(&hints, 0, sizeof(hints));
913 hints.ai_family = af;
Just van Rossum1040d2c2003-05-09 07:53:18 +0000914 Py_BEGIN_ALLOW_THREADS
915 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000916 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000917#if defined(__digital__) && defined(__unix__)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000918 if (error == EAI_NONAME && af == AF_UNSPEC) {
919 /* On Tru64 V5.1, numeric-to-addr conversion fails
920 if no address family is given. Assume IPv4 for now.*/
921 hints.ai_family = AF_INET;
922 error = getaddrinfo(name, NULL, &hints, &res);
923 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000924#endif
Just van Rossum1040d2c2003-05-09 07:53:18 +0000925 Py_END_ALLOW_THREADS
926 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000927 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000928 set_gaierror(error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000929 return -1;
930 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000931 if (res->ai_addrlen < addr_ret_size)
932 addr_ret_size = res->ai_addrlen;
933 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000934 freeaddrinfo(res);
935 switch (addr_ret->sa_family) {
936 case AF_INET:
937 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000938#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000939 case AF_INET6:
940 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000941#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000942 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000943 PyErr_SetString(socket_error, "unknown address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000944 return -1;
945 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000946}
947
Guido van Rossum30a685f1991-06-27 15:51:29 +0000948
Guido van Rossum30a685f1991-06-27 15:51:29 +0000949/* Create a string object representing an IP address.
950 This is always a string of the form 'dd.dd.dd.dd' (with variable
951 size numbers). */
952
Guido van Rossum73624e91994-10-10 17:59:00 +0000953static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000954makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000955{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000956 char buf[NI_MAXHOST];
957 int error;
958
959 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
960 NI_NUMERICHOST);
961 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000962 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000963 return NULL;
964 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000965 return PyString_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000966}
967
968
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000969#ifdef USE_BLUETOOTH
970/* Convert a string representation of a Bluetooth address into a numeric
971 address. Returns the length (6), or raises an exception and returns -1 if
972 an error occurred. */
973
974static int
975setbdaddr(char *name, bdaddr_t *bdaddr)
976{
977 unsigned int b0, b1, b2, b3, b4, b5;
978 char ch;
979 int n;
980
981 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
982 &b5, &b4, &b3, &b2, &b1, &b0, &ch);
983 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
984 bdaddr->b[0] = b0;
985 bdaddr->b[1] = b1;
986 bdaddr->b[2] = b2;
987 bdaddr->b[3] = b3;
988 bdaddr->b[4] = b4;
989 bdaddr->b[5] = b5;
990 return 6;
991 } else {
992 PyErr_SetString(socket_error, "bad bluetooth address");
993 return -1;
994 }
995}
996
997/* Create a string representation of the Bluetooth address. This is always a
998 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
999 value (zero padded if necessary). */
1000
1001static PyObject *
1002makebdaddr(bdaddr_t *bdaddr)
1003{
1004 char buf[(6 * 2) + 5 + 1];
1005
1006 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
1007 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
1008 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
1009 return PyString_FromString(buf);
1010}
1011#endif
1012
1013
Guido van Rossum30a685f1991-06-27 15:51:29 +00001014/* Create an object representing the given socket address,
1015 suitable for passing it back to bind(), connect() etc.
1016 The family field of the sockaddr structure is inspected
1017 to determine what kind of address it really is. */
1018
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001019/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001020static PyObject *
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001021makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001022{
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001023 if (addrlen == 0) {
1024 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +00001025 Py_INCREF(Py_None);
1026 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001027 }
1028
Guido van Rossumbcc20741998-08-04 22:53:56 +00001029#ifdef __BEOS__
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00001030 /* XXX: BeOS version of accept() doesn't set family correctly */
Guido van Rossumbcc20741998-08-04 22:53:56 +00001031 addr->sa_family = AF_INET;
1032#endif
1033
Guido van Rossum30a685f1991-06-27 15:51:29 +00001034 switch (addr->sa_family) {
1035
1036 case AF_INET:
1037 {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001038 struct sockaddr_in *a;
1039 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
Barry Warsaw752300b1997-01-03 17:18:10 +00001040 PyObject *ret = NULL;
1041 if (addrobj) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001042 a = (struct sockaddr_in *)addr;
Barry Warsaw752300b1997-01-03 17:18:10 +00001043 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
1044 Py_DECREF(addrobj);
1045 }
Guido van Rossum6f5afc91993-02-05 09:46:15 +00001046 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001047 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00001048
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001049#if defined(AF_UNIX)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001050 case AF_UNIX:
1051 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001052 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Armin Rigoa9017c32006-04-19 11:50:27 +00001053#ifdef linux
1054 if (a->sun_path[0] == 0) { /* Linux abstract namespace */
1055 addrlen -= (sizeof(*a) - sizeof(a->sun_path));
1056 return PyString_FromStringAndSize(a->sun_path,
1057 addrlen);
1058 }
1059 else
1060#endif /* linux */
1061 {
1062 /* regular NULL-terminated string */
1063 return PyString_FromString(a->sun_path);
1064 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001065 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001066#endif /* AF_UNIX */
1067
Martin v. Löwis11017b12006-01-14 18:12:57 +00001068#if defined(AF_NETLINK)
1069 case AF_NETLINK:
1070 {
1071 struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
Martin v. Löwisd0560052006-01-15 07:49:20 +00001072 return Py_BuildValue("II", a->nl_pid, a->nl_groups);
Martin v. Löwis11017b12006-01-14 18:12:57 +00001073 }
1074#endif /* AF_NETLINK */
1075
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001076#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001077 case AF_INET6:
1078 {
1079 struct sockaddr_in6 *a;
1080 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
1081 PyObject *ret = NULL;
1082 if (addrobj) {
1083 a = (struct sockaddr_in6 *)addr;
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001084 ret = Py_BuildValue("Oiii",
1085 addrobj,
1086 ntohs(a->sin6_port),
1087 a->sin6_flowinfo,
1088 a->sin6_scope_id);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001089 Py_DECREF(addrobj);
1090 }
1091 return ret;
1092 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001093#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001094
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001095#ifdef USE_BLUETOOTH
1096 case AF_BLUETOOTH:
1097 switch (proto) {
1098
1099 case BTPROTO_L2CAP:
1100 {
1101 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
1102 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
1103 PyObject *ret = NULL;
1104 if (addrobj) {
1105 ret = Py_BuildValue("Oi",
1106 addrobj,
1107 _BT_L2_MEMB(a, psm));
1108 Py_DECREF(addrobj);
1109 }
1110 return ret;
1111 }
1112
1113 case BTPROTO_RFCOMM:
1114 {
1115 struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
1116 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
1117 PyObject *ret = NULL;
1118 if (addrobj) {
1119 ret = Py_BuildValue("Oi",
1120 addrobj,
1121 _BT_RC_MEMB(a, channel));
1122 Py_DECREF(addrobj);
1123 }
1124 return ret;
1125 }
1126
Martin v. Löwis45423a72007-02-14 10:07:37 +00001127 case BTPROTO_HCI:
1128 {
1129 struct sockaddr_hci *a = (struct sockaddr_hci *) addr;
1130 PyObject *ret = NULL;
1131 ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
1132 return ret;
1133 }
1134
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001135#if !defined(__FreeBSD__)
1136 case BTPROTO_SCO:
1137 {
1138 struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
1139 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
1140 }
1141#endif
1142
1143 }
1144#endif
1145
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001146#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001147 case AF_PACKET:
1148 {
1149 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
1150 char *ifname = "";
1151 struct ifreq ifr;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001152 /* need to look up interface name give index */
1153 if (a->sll_ifindex) {
Jeremy Hylton22308652001-02-02 03:23:09 +00001154 ifr.ifr_ifindex = a->sll_ifindex;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001155 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
Jeremy Hylton22308652001-02-02 03:23:09 +00001156 ifname = ifr.ifr_name;
Jeremy Hylton22308652001-02-02 03:23:09 +00001157 }
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001158 return Py_BuildValue("shbhs#",
1159 ifname,
1160 ntohs(a->sll_protocol),
1161 a->sll_pkttype,
1162 a->sll_hatype,
1163 a->sll_addr,
1164 a->sll_halen);
Jeremy Hylton22308652001-02-02 03:23:09 +00001165 }
1166#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001167
Guido van Rossum30a685f1991-06-27 15:51:29 +00001168 /* More cases here... */
1169
1170 default:
Guido van Rossumaa948df1997-05-07 17:41:48 +00001171 /* If we don't know the address family, don't raise an
1172 exception -- return it as a tuple. */
1173 return Py_BuildValue("is#",
1174 addr->sa_family,
1175 addr->sa_data,
1176 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001177
Guido van Rossum30a685f1991-06-27 15:51:29 +00001178 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001179}
1180
Guido van Rossum30a685f1991-06-27 15:51:29 +00001181
1182/* Parse a socket address argument according to the socket object's
1183 address family. Return 1 if the address was in the proper format,
1184 0 of not. The address is returned through addr_ret, its length
1185 through len_ret. */
1186
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001187static int
Guido van Rossum48a680c2001-03-02 06:34:14 +00001188getsockaddrarg(PySocketSockObject *s, PyObject *args,
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001189 struct sockaddr *addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001190{
Guido van Rossum30a685f1991-06-27 15:51:29 +00001191 switch (s->sock_family) {
1192
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001193#if defined(AF_UNIX)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001194 case AF_UNIX:
1195 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001196 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001197 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001198 int len;
Guido van Rossum7e488981998-10-08 02:25:24 +00001199 if (!PyArg_Parse(args, "t#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001200 return 0;
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001201
1202 addr = (struct sockaddr_un*)addr_ret;
Armin Rigoa9017c32006-04-19 11:50:27 +00001203#ifdef linux
1204 if (len > 0 && path[0] == 0) {
1205 /* Linux abstract namespace extension */
1206 if (len > sizeof addr->sun_path) {
1207 PyErr_SetString(socket_error,
1208 "AF_UNIX path too long");
1209 return 0;
1210 }
1211 }
1212 else
1213#endif /* linux */
1214 {
1215 /* regular NULL-terminated string */
1216 if (len >= sizeof addr->sun_path) {
1217 PyErr_SetString(socket_error,
1218 "AF_UNIX path too long");
1219 return 0;
1220 }
1221 addr->sun_path[len] = 0;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001222 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001223 addr->sun_family = s->sock_family;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001224 memcpy(addr->sun_path, path, len);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001225#if defined(PYOS_OS2)
1226 *len_ret = sizeof(*addr);
1227#else
Guido van Rossum65af28a1996-06-11 18:36:33 +00001228 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001229#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001230 return 1;
1231 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001232#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001233
Martin v. Löwis11017b12006-01-14 18:12:57 +00001234#if defined(AF_NETLINK)
1235 case AF_NETLINK:
1236 {
1237 struct sockaddr_nl* addr;
1238 int pid, groups;
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001239 addr = (struct sockaddr_nl *)addr_ret;
Martin v. Löwis11017b12006-01-14 18:12:57 +00001240 if (!PyTuple_Check(args)) {
1241 PyErr_Format(
1242 PyExc_TypeError,
1243 "getsockaddrarg: "
1244 "AF_NETLINK address must be tuple, not %.500s",
1245 args->ob_type->tp_name);
1246 return 0;
1247 }
1248 if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
1249 return 0;
1250 addr->nl_family = AF_NETLINK;
1251 addr->nl_pid = pid;
1252 addr->nl_groups = groups;
Martin v. Löwis11017b12006-01-14 18:12:57 +00001253 *len_ret = sizeof(*addr);
1254 return 1;
1255 }
1256#endif
1257
Guido van Rossum30a685f1991-06-27 15:51:29 +00001258 case AF_INET:
1259 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001260 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001261 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001262 int port, result;
Guido van Rossume4dad902000-12-01 13:13:11 +00001263 if (!PyTuple_Check(args)) {
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001264 PyErr_Format(
1265 PyExc_TypeError,
1266 "getsockaddrarg: "
1267 "AF_INET address must be tuple, not %.500s",
1268 args->ob_type->tp_name);
Guido van Rossume4dad902000-12-01 13:13:11 +00001269 return 0;
1270 }
Martin Blais2856e5f2006-05-26 12:03:27 +00001271 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
Martin v. Löwis2548c732003-04-18 10:39:54 +00001272 "idna", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001273 return 0;
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001274 addr=(struct sockaddr_in*)addr_ret;
Martin Blais2856e5f2006-05-26 12:03:27 +00001275 result = setipaddr(host, (struct sockaddr *)addr,
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001276 sizeof(*addr), AF_INET);
1277 PyMem_Free(host);
1278 if (result < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001279 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001280 addr->sin_family = AF_INET;
Guido van Rossum644a12b1997-04-09 19:24:53 +00001281 addr->sin_port = htons((short)port);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001282 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001283 return 1;
1284 }
1285
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001286#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001287 case AF_INET6:
1288 {
1289 struct sockaddr_in6* addr;
1290 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001291 int port, flowinfo, scope_id, result;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001292 flowinfo = scope_id = 0;
Thomas Wouters04520492006-04-25 15:08:10 +00001293 if (!PyTuple_Check(args)) {
1294 PyErr_Format(
1295 PyExc_TypeError,
1296 "getsockaddrarg: "
1297 "AF_INET6 address must be tuple, not %.500s",
1298 args->ob_type->tp_name);
1299 return 0;
1300 }
Martin Blais2856e5f2006-05-26 12:03:27 +00001301 if (!PyArg_ParseTuple(args, "eti|ii",
Martin v. Löwis2548c732003-04-18 10:39:54 +00001302 "idna", &host, &port, &flowinfo,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001303 &scope_id)) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001304 return 0;
1305 }
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001306 addr = (struct sockaddr_in6*)addr_ret;
Martin Blais2856e5f2006-05-26 12:03:27 +00001307 result = setipaddr(host, (struct sockaddr *)addr,
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001308 sizeof(*addr), AF_INET6);
1309 PyMem_Free(host);
1310 if (result < 0)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001311 return 0;
1312 addr->sin6_family = s->sock_family;
1313 addr->sin6_port = htons((short)port);
1314 addr->sin6_flowinfo = flowinfo;
1315 addr->sin6_scope_id = scope_id;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001316 *len_ret = sizeof *addr;
1317 return 1;
1318 }
1319#endif
1320
Hye-Shik Chang81268602004-02-02 06:05:24 +00001321#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00001322 case AF_BLUETOOTH:
1323 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001324 switch (s->sock_proto) {
1325 case BTPROTO_L2CAP:
Martin v. Löwis12af0482004-01-31 12:34:17 +00001326 {
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001327 struct sockaddr_l2 *addr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001328 char *straddr;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001329
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001330 addr = (struct sockaddr_l2 *)addr_ret;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001331 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
1332 if (!PyArg_ParseTuple(args, "si", &straddr,
1333 &_BT_L2_MEMB(addr, psm))) {
1334 PyErr_SetString(socket_error, "getsockaddrarg: "
1335 "wrong format");
Martin v. Löwis12af0482004-01-31 12:34:17 +00001336 return 0;
1337 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001338 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
1339 return 0;
1340
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001341 *len_ret = sizeof *addr;
1342 return 1;
1343 }
1344 case BTPROTO_RFCOMM:
1345 {
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001346 struct sockaddr_rc *addr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001347 char *straddr;
1348
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001349 addr = (struct sockaddr_rc *)addr_ret;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001350 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
1351 if (!PyArg_ParseTuple(args, "si", &straddr,
1352 &_BT_RC_MEMB(addr, channel))) {
1353 PyErr_SetString(socket_error, "getsockaddrarg: "
1354 "wrong format");
1355 return 0;
1356 }
1357 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
1358 return 0;
1359
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001360 *len_ret = sizeof *addr;
1361 return 1;
1362 }
Martin v. Löwis45423a72007-02-14 10:07:37 +00001363 case BTPROTO_HCI:
1364 {
Martin v. Löwisf2284822007-02-14 11:30:07 +00001365 struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret;
Martin v. Löwis45423a72007-02-14 10:07:37 +00001366 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
1367 if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) {
1368 PyErr_SetString(socket_error, "getsockaddrarg: "
1369 "wrong format");
1370 return 0;
1371 }
Martin v. Löwis45423a72007-02-14 10:07:37 +00001372 *len_ret = sizeof *addr;
1373 return 1;
1374 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001375#if !defined(__FreeBSD__)
1376 case BTPROTO_SCO:
1377 {
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001378 struct sockaddr_sco *addr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001379 char *straddr;
1380
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001381 addr = (struct sockaddr_sco *)addr_ret;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001382 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
1383 straddr = PyString_AsString(args);
1384 if (straddr == NULL) {
1385 PyErr_SetString(socket_error, "getsockaddrarg: "
1386 "wrong format");
1387 return 0;
1388 }
1389 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
1390 return 0;
1391
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001392 *len_ret = sizeof *addr;
1393 return 1;
1394 }
1395#endif
1396 default:
1397 PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol");
1398 return 0;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001399 }
1400 }
1401#endif
1402
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001403#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001404 case AF_PACKET:
1405 {
1406 struct sockaddr_ll* addr;
1407 struct ifreq ifr;
1408 char *interfaceName;
1409 int protoNumber;
1410 int hatype = 0;
1411 int pkttype = 0;
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001412 char *haddr = NULL;
1413 unsigned int halen = 0;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001414
Thomas Wouters04520492006-04-25 15:08:10 +00001415 if (!PyTuple_Check(args)) {
1416 PyErr_Format(
1417 PyExc_TypeError,
1418 "getsockaddrarg: "
1419 "AF_PACKET address must be tuple, not %.500s",
1420 args->ob_type->tp_name);
1421 return 0;
1422 }
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001423 if (!PyArg_ParseTuple(args, "si|iis#", &interfaceName,
1424 &protoNumber, &pkttype, &hatype,
1425 &haddr, &halen))
Jeremy Hylton22308652001-02-02 03:23:09 +00001426 return 0;
1427 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1428 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001429 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001430 s->errorhandler();
Jeremy Hylton22308652001-02-02 03:23:09 +00001431 return 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001432 }
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001433 if (halen > 8) {
1434 PyErr_SetString(PyExc_ValueError,
1435 "Hardware address must be 8 bytes or less");
1436 return 0;
1437 }
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001438 addr = (struct sockaddr_ll*)addr_ret;
1439 addr->sll_family = AF_PACKET;
1440 addr->sll_protocol = htons((short)protoNumber);
1441 addr->sll_ifindex = ifr.ifr_ifindex;
1442 addr->sll_pkttype = pkttype;
1443 addr->sll_hatype = hatype;
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001444 if (halen != 0) {
1445 memcpy(&addr->sll_addr, haddr, halen);
1446 }
1447 addr->sll_halen = halen;
Jeremy Hylton22308652001-02-02 03:23:09 +00001448 *len_ret = sizeof *addr;
1449 return 1;
1450 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001451#endif
1452
Guido van Rossum30a685f1991-06-27 15:51:29 +00001453 /* More cases here... */
1454
1455 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001456 PyErr_SetString(socket_error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001457 return 0;
1458
1459 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001460}
1461
Guido van Rossum30a685f1991-06-27 15:51:29 +00001462
Guido van Rossum48a680c2001-03-02 06:34:14 +00001463/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00001464 Return 1 if the family is known, 0 otherwise. The length is returned
1465 through len_ret. */
1466
1467static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00001468getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001469{
1470 switch (s->sock_family) {
1471
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001472#if defined(AF_UNIX)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001473 case AF_UNIX:
1474 {
1475 *len_ret = sizeof (struct sockaddr_un);
1476 return 1;
1477 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001478#endif /* AF_UNIX */
Martin v. Löwis11017b12006-01-14 18:12:57 +00001479#if defined(AF_NETLINK)
1480 case AF_NETLINK:
1481 {
1482 *len_ret = sizeof (struct sockaddr_nl);
1483 return 1;
1484 }
1485#endif
Guido van Rossum710e1df1992-06-12 10:39:36 +00001486
1487 case AF_INET:
1488 {
1489 *len_ret = sizeof (struct sockaddr_in);
1490 return 1;
1491 }
1492
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001493#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001494 case AF_INET6:
1495 {
1496 *len_ret = sizeof (struct sockaddr_in6);
1497 return 1;
1498 }
1499#endif
1500
Hye-Shik Chang81268602004-02-02 06:05:24 +00001501#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00001502 case AF_BLUETOOTH:
1503 {
1504 switch(s->sock_proto)
1505 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001506
1507 case BTPROTO_L2CAP:
1508 *len_ret = sizeof (struct sockaddr_l2);
1509 return 1;
1510 case BTPROTO_RFCOMM:
1511 *len_ret = sizeof (struct sockaddr_rc);
1512 return 1;
Martin v. Löwis45423a72007-02-14 10:07:37 +00001513 case BTPROTO_HCI:
1514 *len_ret = sizeof (struct sockaddr_hci);
1515 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001516#if !defined(__FreeBSD__)
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001517 case BTPROTO_SCO:
1518 *len_ret = sizeof (struct sockaddr_sco);
1519 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001520#endif
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001521 default:
1522 PyErr_SetString(socket_error, "getsockaddrlen: "
1523 "unknown BT protocol");
1524 return 0;
1525
Martin v. Löwis12af0482004-01-31 12:34:17 +00001526 }
1527 }
1528#endif
1529
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001530#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001531 case AF_PACKET:
1532 {
1533 *len_ret = sizeof (struct sockaddr_ll);
1534 return 1;
1535 }
1536#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001537
Guido van Rossum710e1df1992-06-12 10:39:36 +00001538 /* More cases here... */
1539
1540 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001541 PyErr_SetString(socket_error, "getsockaddrlen: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +00001542 return 0;
1543
1544 }
1545}
1546
1547
Guido van Rossum30a685f1991-06-27 15:51:29 +00001548/* s.accept() method */
1549
Guido van Rossum73624e91994-10-10 17:59:00 +00001550static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001551sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001552{
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001553 sock_addr_t addrbuf;
Fred Drakea04eaad2000-06-30 02:46:07 +00001554 SOCKET_T newfd;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001555 socklen_t addrlen;
Barry Warsaw752300b1997-01-03 17:18:10 +00001556 PyObject *sock = NULL;
1557 PyObject *addr = NULL;
1558 PyObject *res = NULL;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001559 int timeout;
Barry Warsaw752300b1997-01-03 17:18:10 +00001560
Guido van Rossum710e1df1992-06-12 10:39:36 +00001561 if (!getsockaddrlen(s, &addrlen))
1562 return NULL;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001563 memset(&addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001564
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001565#ifdef MS_WINDOWS
1566 newfd = INVALID_SOCKET;
1567#else
1568 newfd = -1;
1569#endif
1570
Neal Norwitz082b2df2006-02-07 07:04:46 +00001571 if (!IS_SELECTABLE(s))
1572 return select_error();
1573
Guido van Rossum73624e91994-10-10 17:59:00 +00001574 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001575 timeout = internal_select(s, 0);
1576 if (!timeout)
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001577 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001578 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001579
Neal Norwitz9b0ca792006-08-02 06:46:21 +00001580 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001581 PyErr_SetString(socket_timeout, "timed out");
1582 return NULL;
1583 }
1584
Fred Drakea04eaad2000-06-30 02:46:07 +00001585#ifdef MS_WINDOWS
1586 if (newfd == INVALID_SOCKET)
1587#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001588 if (newfd < 0)
Fred Drakea04eaad2000-06-30 02:46:07 +00001589#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001590 return s->errorhandler();
Barry Warsaw752300b1997-01-03 17:18:10 +00001591
Guido van Rossum30a685f1991-06-27 15:51:29 +00001592 /* Create the new object with unspecified family,
1593 to avoid calls to bind() etc. on it. */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001594 sock = (PyObject *) new_sockobject(newfd,
1595 s->sock_family,
1596 s->sock_type,
1597 s->sock_proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001598
Barry Warsaw752300b1997-01-03 17:18:10 +00001599 if (sock == NULL) {
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001600 SOCKETCLOSE(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +00001601 goto finally;
1602 }
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001603 addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001604 addrlen, s->sock_proto);
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001605 if (addr == NULL)
Barry Warsaw752300b1997-01-03 17:18:10 +00001606 goto finally;
1607
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001608 res = PyTuple_Pack(2, sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001609
Guido van Rossum67f7a382002-06-06 21:08:16 +00001610finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001611 Py_XDECREF(sock);
1612 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001613 return res;
1614}
1615
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001616PyDoc_STRVAR(accept_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001617"accept() -> (socket object, address info)\n\
1618\n\
1619Wait for an incoming connection. Return a new socket representing the\n\
1620connection, and the address of the client. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001621info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001622
Guido van Rossum11ba0942002-06-13 15:07:44 +00001623/* s.setblocking(flag) method. Argument:
1624 False -- non-blocking mode; same as settimeout(0)
1625 True -- blocking mode; same as settimeout(None)
1626*/
Guido van Rossume4485b01994-09-07 14:32:49 +00001627
Guido van Rossum73624e91994-10-10 17:59:00 +00001628static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001629sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00001630{
1631 int block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001632
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001633 block = PyInt_AsLong(arg);
1634 if (block == -1 && PyErr_Occurred())
Guido van Rossume4485b01994-09-07 14:32:49 +00001635 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001636
Guido van Rossum11ba0942002-06-13 15:07:44 +00001637 s->sock_timeout = block ? -1.0 : 0.0;
Guido van Rossume8008f02002-06-07 03:36:20 +00001638 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00001639
Guido van Rossum73624e91994-10-10 17:59:00 +00001640 Py_INCREF(Py_None);
1641 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00001642}
Guido van Rossume4485b01994-09-07 14:32:49 +00001643
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001644PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001645"setblocking(flag)\n\
1646\n\
1647Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001648setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001649setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001650
Guido van Rossum11ba0942002-06-13 15:07:44 +00001651/* s.settimeout(timeout) method. Argument:
1652 None -- no timeout, blocking mode; same as setblocking(True)
1653 0.0 -- non-blocking mode; same as setblocking(False)
1654 > 0 -- timeout mode; operations time out after timeout seconds
1655 < 0 -- illegal; raises an exception
1656*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00001657static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001658sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001659{
Guido van Rossum11ba0942002-06-13 15:07:44 +00001660 double timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001661
1662 if (arg == Py_None)
Guido van Rossum11ba0942002-06-13 15:07:44 +00001663 timeout = -1.0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001664 else {
Guido van Rossum11ba0942002-06-13 15:07:44 +00001665 timeout = PyFloat_AsDouble(arg);
1666 if (timeout < 0.0) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00001667 if (!PyErr_Occurred())
1668 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001669 "Timeout value out of range");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001670 return NULL;
1671 }
1672 }
1673
Guido van Rossum11ba0942002-06-13 15:07:44 +00001674 s->sock_timeout = timeout;
1675 internal_setblocking(s, timeout < 0.0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001676
1677 Py_INCREF(Py_None);
1678 return Py_None;
1679}
1680
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001681PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001682"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001683\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001684Set a timeout on socket operations. 'timeout' can be a float,\n\
1685giving in seconds, or None. Setting a timeout of None disables\n\
1686the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001687Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001688
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001689/* s.gettimeout() method.
1690 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001691static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001692sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001693{
1694 if (s->sock_timeout < 0.0) {
1695 Py_INCREF(Py_None);
1696 return Py_None;
1697 }
1698 else
1699 return PyFloat_FromDouble(s->sock_timeout);
1700}
1701
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001702PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001703"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001704\n\
1705Returns the timeout in floating seconds associated with socket \n\
1706operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001707operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00001708
Guido van Rossum48a680c2001-03-02 06:34:14 +00001709#ifdef RISCOS
1710/* s.sleeptaskw(1 | 0) method */
1711
1712static PyObject *
Martin v. Löwisa94568a2003-05-10 07:36:56 +00001713sock_sleeptaskw(PySocketSockObject *s,PyObject *arg)
Guido van Rossum48a680c2001-03-02 06:34:14 +00001714{
Guido van Rossum67f7a382002-06-06 21:08:16 +00001715 int block;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00001716 block = PyInt_AsLong(arg);
1717 if (block == -1 && PyErr_Occurred())
Guido van Rossum67f7a382002-06-06 21:08:16 +00001718 return NULL;
1719 Py_BEGIN_ALLOW_THREADS
1720 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
1721 Py_END_ALLOW_THREADS
Guido van Rossum48a680c2001-03-02 06:34:14 +00001722
Guido van Rossum67f7a382002-06-06 21:08:16 +00001723 Py_INCREF(Py_None);
1724 return Py_None;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001725}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001726PyDoc_STRVAR(sleeptaskw_doc,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001727"sleeptaskw(flag)\n\
1728\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001729Allow sleeps in taskwindows.");
Guido van Rossum48a680c2001-03-02 06:34:14 +00001730#endif
1731
1732
Guido van Rossumaee08791992-09-08 09:05:33 +00001733/* s.setsockopt() method.
1734 With an integer third argument, sets an integer option.
1735 With a string third argument, sets an option from a buffer;
1736 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001737
Guido van Rossum73624e91994-10-10 17:59:00 +00001738static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001739sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001740{
1741 int level;
1742 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001743 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +00001744 char *buf;
1745 int buflen;
1746 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001747
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001748 if (PyArg_ParseTuple(args, "iii:setsockopt",
1749 &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001750 buf = (char *) &flag;
1751 buflen = sizeof flag;
1752 }
1753 else {
Guido van Rossum73624e91994-10-10 17:59:00 +00001754 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001755 if (!PyArg_ParseTuple(args, "iis#:setsockopt",
1756 &level, &optname, &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +00001757 return NULL;
1758 }
Thomas Wouters334fb892000-07-25 12:56:38 +00001759 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001760 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001761 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001762 Py_INCREF(Py_None);
1763 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001764}
1765
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001766PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001767"setsockopt(level, option, value)\n\
1768\n\
1769Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001770The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001771
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001772
Guido van Rossumaee08791992-09-08 09:05:33 +00001773/* s.getsockopt() method.
1774 With two arguments, retrieves an integer option.
1775 With a third integer argument, retrieves a string buffer of that size;
1776 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001777
Guido van Rossum73624e91994-10-10 17:59:00 +00001778static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001779sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001780{
1781 int level;
1782 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001783 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00001784 PyObject *buf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001785 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001786
Guido van Rossumbcc20741998-08-04 22:53:56 +00001787#ifdef __BEOS__
Fred Drake564a6cc2001-05-11 20:12:26 +00001788 /* We have incomplete socket support. */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001789 PyErr_SetString(socket_error, "getsockopt not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00001790 return NULL;
1791#else
1792
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001793 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
1794 &level, &optname, &buflen))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001795 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001796
Guido van Rossumbe32c891996-06-20 16:25:29 +00001797 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001798 int flag = 0;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001799 socklen_t flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001800 res = getsockopt(s->sock_fd, level, optname,
Thomas Wouters334fb892000-07-25 12:56:38 +00001801 (void *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +00001802 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001803 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001804 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +00001805 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001806#ifdef __VMS
Neal Norwitz2a30cd02006-07-10 01:18:57 +00001807 /* socklen_t is unsigned so no negative test is needed,
1808 test buflen == 0 is previously done */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001809 if (buflen > 1024) {
1810#else
Guido van Rossumaee08791992-09-08 09:05:33 +00001811 if (buflen <= 0 || buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001812#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001813 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +00001814 "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +00001815 return NULL;
1816 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001817 buf = PyString_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001818 if (buf == NULL)
1819 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001820 res = getsockopt(s->sock_fd, level, optname,
Fred Drake564a6cc2001-05-11 20:12:26 +00001821 (void *)PyString_AS_STRING(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001822 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001823 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001824 return s->errorhandler();
Guido van Rossumaee08791992-09-08 09:05:33 +00001825 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001826 _PyString_Resize(&buf, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001827 return buf;
Guido van Rossumbcc20741998-08-04 22:53:56 +00001828#endif /* __BEOS__ */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001829}
1830
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001831PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001832"getsockopt(level, option[, buffersize]) -> value\n\
1833\n\
1834Get a socket option. See the Unix manual for level and option.\n\
1835If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001836string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001837
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001838
Fred Drake728819a2000-07-01 03:40:12 +00001839/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001840
Guido van Rossum73624e91994-10-10 17:59:00 +00001841static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001842sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001843{
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001844 sock_addr_t addrbuf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001845 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001846 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001847
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001848 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001849 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001850 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001851 res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001852 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001853 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001854 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001855 Py_INCREF(Py_None);
1856 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001857}
1858
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001859PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001860"bind(address)\n\
1861\n\
1862Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00001863pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001864sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001865
Guido van Rossum30a685f1991-06-27 15:51:29 +00001866
1867/* s.close() method.
1868 Set the file descriptor to -1 so operations tried subsequently
1869 will surely fail. */
1870
Guido van Rossum73624e91994-10-10 17:59:00 +00001871static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001872sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001873{
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001874 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001875
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001876 if ((fd = s->sock_fd) != -1) {
1877 s->sock_fd = -1;
Guido van Rossum08481461996-10-12 14:07:22 +00001878 Py_BEGIN_ALLOW_THREADS
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001879 (void) SOCKETCLOSE(fd);
Guido van Rossum08481461996-10-12 14:07:22 +00001880 Py_END_ALLOW_THREADS
1881 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001882 Py_INCREF(Py_None);
1883 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001884}
1885
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001886PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001887"close()\n\
1888\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001889Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001890
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001891static int
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001892internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
1893 int *timeoutp)
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001894{
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001895 int res, timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001896
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001897 timeout = 0;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001898 res = connect(s->sock_fd, addr, addrlen);
1899
1900#ifdef MS_WINDOWS
1901
1902 if (s->sock_timeout > 0.0) {
Neal Norwitz082b2df2006-02-07 07:04:46 +00001903 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK &&
1904 IS_SELECTABLE(s)) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001905 /* This is a mess. Best solution: trust select */
1906 fd_set fds;
Mark Hammonda57ec932004-08-03 05:06:26 +00001907 fd_set fds_exc;
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001908 struct timeval tv;
1909 tv.tv_sec = (int)s->sock_timeout;
1910 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
1911 FD_ZERO(&fds);
1912 FD_SET(s->sock_fd, &fds);
Mark Hammonda57ec932004-08-03 05:06:26 +00001913 FD_ZERO(&fds_exc);
1914 FD_SET(s->sock_fd, &fds_exc);
1915 res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001916 if (res == 0) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001917 res = WSAEWOULDBLOCK;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001918 timeout = 1;
Mark Hammonda57ec932004-08-03 05:06:26 +00001919 } else if (res > 0) {
1920 if (FD_ISSET(s->sock_fd, &fds))
1921 /* The socket is in the writeable set - this
1922 means connected */
1923 res = 0;
1924 else {
1925 /* As per MS docs, we need to call getsockopt()
1926 to get the underlying error */
1927 int res_size = sizeof res;
1928 /* It must be in the exception set */
1929 assert(FD_ISSET(s->sock_fd, &fds_exc));
Martin Blais2856e5f2006-05-26 12:03:27 +00001930 if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
Mark Hammonda57ec932004-08-03 05:06:26 +00001931 (char *)&res, &res_size))
1932 /* getsockopt also clears WSAGetLastError,
1933 so reset it back. */
1934 WSASetLastError(res);
1935 else
1936 res = WSAGetLastError();
1937 }
1938 }
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001939 /* else if (res < 0) an error occurred */
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001940 }
1941 }
1942
1943 if (res < 0)
1944 res = WSAGetLastError();
1945
1946#else
1947
1948 if (s->sock_timeout > 0.0) {
Neal Norwitz082b2df2006-02-07 07:04:46 +00001949 if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001950 timeout = internal_select(s, 1);
Neal Norwitz9b0ca792006-08-02 06:46:21 +00001951 if (timeout == 0) {
1952 res = connect(s->sock_fd, addr, addrlen);
1953 if (res < 0 && errno == EISCONN)
1954 res = 0;
1955 }
1956 else if (timeout == -1)
1957 res = errno; /* had error */
1958 else
1959 res = EWOULDBLOCK; /* timed out */
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001960 }
1961 }
1962
1963 if (res < 0)
1964 res = errno;
1965
1966#endif
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001967 *timeoutp = timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001968
1969 return res;
1970}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001971
Fred Drake728819a2000-07-01 03:40:12 +00001972/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001973
Guido van Rossum73624e91994-10-10 17:59:00 +00001974static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001975sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001976{
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001977 sock_addr_t addrbuf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001978 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001979 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001980 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001981
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001982 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001983 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001984
Guido van Rossum73624e91994-10-10 17:59:00 +00001985 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis046c4d12006-12-03 11:23:45 +00001986 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
Guido van Rossum11ba0942002-06-13 15:07:44 +00001987 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001988
Neal Norwitz9b0ca792006-08-02 06:46:21 +00001989 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001990 PyErr_SetString(socket_timeout, "timed out");
1991 return NULL;
1992 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001993 if (res != 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001994 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001995 Py_INCREF(Py_None);
1996 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001997}
1998
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001999PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002000"connect(address)\n\
2001\n\
2002Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002003is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002004
Guido van Rossum30a685f1991-06-27 15:51:29 +00002005
Fred Drake728819a2000-07-01 03:40:12 +00002006/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002007
2008static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002009sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002010{
Martin v. Löwis046c4d12006-12-03 11:23:45 +00002011 sock_addr_t addrbuf;
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002012 int addrlen;
2013 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002014 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002015
Martin v. Löwis046c4d12006-12-03 11:23:45 +00002016 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002017 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002018
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002019 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis046c4d12006-12-03 11:23:45 +00002020 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002021 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002022
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002023 /* Signals are not errors (though they may raise exceptions). Adapted
2024 from PyErr_SetFromErrnoWithFilenameObject(). */
2025#ifdef EINTR
2026 if (res == EINTR && PyErr_CheckSignals())
2027 return NULL;
2028#endif
2029
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002030 return PyInt_FromLong((long) res);
2031}
2032
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002033PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002034"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002035\n\
2036This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002037instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002038
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002039
Guido van Rossumed233a51992-06-23 09:07:03 +00002040/* s.fileno() method */
2041
Guido van Rossum73624e91994-10-10 17:59:00 +00002042static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002043sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00002044{
Fred Drakea04eaad2000-06-30 02:46:07 +00002045#if SIZEOF_SOCKET_T <= SIZEOF_LONG
Guido van Rossum73624e91994-10-10 17:59:00 +00002046 return PyInt_FromLong((long) s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00002047#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00002048 return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00002049#endif
Guido van Rossumed233a51992-06-23 09:07:03 +00002050}
2051
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002052PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002053"fileno() -> integer\n\
2054\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002055Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002056
Guido van Rossumed233a51992-06-23 09:07:03 +00002057
Guido van Rossumbe32c891996-06-20 16:25:29 +00002058#ifndef NO_DUP
2059/* s.dup() method */
2060
2061static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002062sock_dup(PySocketSockObject *s)
Guido van Rossumbe32c891996-06-20 16:25:29 +00002063{
Fred Drakea04eaad2000-06-30 02:46:07 +00002064 SOCKET_T newfd;
Guido van Rossumbe32c891996-06-20 16:25:29 +00002065 PyObject *sock;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002066
Guido van Rossumbe32c891996-06-20 16:25:29 +00002067 newfd = dup(s->sock_fd);
2068 if (newfd < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002069 return s->errorhandler();
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002070 sock = (PyObject *) new_sockobject(newfd,
2071 s->sock_family,
2072 s->sock_type,
2073 s->sock_proto);
Guido van Rossumbe32c891996-06-20 16:25:29 +00002074 if (sock == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00002075 SOCKETCLOSE(newfd);
Guido van Rossumbe32c891996-06-20 16:25:29 +00002076 return sock;
2077}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002078
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002079PyDoc_STRVAR(dup_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002080"dup() -> socket object\n\
2081\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002082Return a new socket object connected to the same system resource.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002083
Guido van Rossumbe32c891996-06-20 16:25:29 +00002084#endif
2085
2086
Guido van Rossumc89705d1992-11-26 08:54:07 +00002087/* s.getsockname() method */
2088
Guido van Rossum73624e91994-10-10 17:59:00 +00002089static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002090sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002091{
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002092 sock_addr_t addrbuf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002093 int res;
2094 socklen_t addrlen;
2095
Guido van Rossumc89705d1992-11-26 08:54:07 +00002096 if (!getsockaddrlen(s, &addrlen))
2097 return NULL;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002098 memset(&addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002099 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis046c4d12006-12-03 11:23:45 +00002100 res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002101 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00002102 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002103 return s->errorhandler();
Martin v. Löwis046c4d12006-12-03 11:23:45 +00002104 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00002105 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002106}
2107
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002108PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002109"getsockname() -> address info\n\
2110\n\
2111Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002112info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002113
Guido van Rossumc89705d1992-11-26 08:54:07 +00002114
Guido van Rossumb6775db1994-08-01 11:34:53 +00002115#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002116/* s.getpeername() method */
2117
Guido van Rossum73624e91994-10-10 17:59:00 +00002118static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002119sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002120{
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002121 sock_addr_t addrbuf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002122 int res;
2123 socklen_t addrlen;
2124
Guido van Rossumc89705d1992-11-26 08:54:07 +00002125 if (!getsockaddrlen(s, &addrlen))
2126 return NULL;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002127 memset(&addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002128 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis046c4d12006-12-03 11:23:45 +00002129 res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002130 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00002131 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002132 return s->errorhandler();
Martin v. Löwis046c4d12006-12-03 11:23:45 +00002133 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00002134 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002135}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002136
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002137PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002138"getpeername() -> address info\n\
2139\n\
2140Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002141info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002142
Guido van Rossumb6775db1994-08-01 11:34:53 +00002143#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002144
2145
Guido van Rossum30a685f1991-06-27 15:51:29 +00002146/* s.listen(n) method */
2147
Guido van Rossum73624e91994-10-10 17:59:00 +00002148static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002149sock_listen(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002150{
2151 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002152 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002153
2154 backlog = PyInt_AsLong(arg);
2155 if (backlog == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002156 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002157 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +00002158 if (backlog < 1)
2159 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002160 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +00002161 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002162 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002163 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002164 Py_INCREF(Py_None);
2165 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002166}
2167
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002168PyDoc_STRVAR(listen_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002169"listen(backlog)\n\
2170\n\
2171Enable a server to accept connections. The backlog argument must be at\n\
2172least 1; it specifies the number of unaccepted connection that the system\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002173will allow before refusing new connections.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002174
2175
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002176#ifndef NO_DUP
Guido van Rossum30a685f1991-06-27 15:51:29 +00002177/* s.makefile(mode) method.
2178 Create a new open file object referring to a dupped version of
2179 the socket's file descriptor. (The dup() call is necessary so
2180 that the open file and socket objects may be closed independent
2181 of each other.)
2182 The mode argument specifies 'r' or 'w' passed to fdopen(). */
2183
Guido van Rossum73624e91994-10-10 17:59:00 +00002184static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002185sock_makefile(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002186{
Tim Petersdbd9ba62000-07-09 03:09:57 +00002187 extern int fclose(FILE *);
Guido van Rossum6b144911995-03-14 15:05:13 +00002188 char *mode = "r";
2189 int bufsize = -1;
Fred Drakea04eaad2000-06-30 02:46:07 +00002190#ifdef MS_WIN32
Tim Peters79248aa2001-08-29 21:37:10 +00002191 Py_intptr_t fd;
Fred Drakea04eaad2000-06-30 02:46:07 +00002192#else
Guido van Rossum30a685f1991-06-27 15:51:29 +00002193 int fd;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002194#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00002195 FILE *fp;
Guido van Rossum6b144911995-03-14 15:05:13 +00002196 PyObject *f;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002197#ifdef __VMS
2198 char *mode_r = "r";
2199 char *mode_w = "w";
2200#endif
Guido van Rossum6b144911995-03-14 15:05:13 +00002201
Guido van Rossum43713e52000-02-29 13:59:29 +00002202 if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002203 return NULL;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002204#ifdef __VMS
2205 if (strcmp(mode,"rb") == 0) {
2206 mode = mode_r;
2207 }
2208 else {
2209 if (strcmp(mode,"wb") == 0) {
2210 mode = mode_w;
2211 }
2212 }
2213#endif
Guido van Rossum8d665e61996-06-26 18:22:49 +00002214#ifdef MS_WIN32
Guido van Rossum82a5c661998-07-07 20:45:43 +00002215 if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||
2216 ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00002217#else
Guido van Rossum82a5c661998-07-07 20:45:43 +00002218 if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00002219#endif
Guido van Rossum82a5c661998-07-07 20:45:43 +00002220 {
Guido van Rossum6b144911995-03-14 15:05:13 +00002221 if (fd >= 0)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00002222 SOCKETCLOSE(fd);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002223 return s->errorhandler();
Guido van Rossum6b144911995-03-14 15:05:13 +00002224 }
2225 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
2226 if (f != NULL)
2227 PyFile_SetBufSize(f, bufsize);
2228 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +00002229}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002230
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002231PyDoc_STRVAR(makefile_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002232"makefile([mode[, buffersize]]) -> file object\n\
2233\n\
2234Return a regular file object corresponding to the socket.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002235The mode and buffersize arguments are as for the built-in open() function.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002236
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002237#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002238
Martin Blais2856e5f2006-05-26 12:03:27 +00002239/*
Martin Blaisaf2ae722006-06-04 13:49:49 +00002240 * This is the guts of the recv() and recv_into() methods, which reads into a
Andrew M. Kuchling8d0baae2006-12-19 14:29:04 +00002241 * char buffer. If you have any inc/dec ref to do to the objects that contain
Martin Blais2856e5f2006-05-26 12:03:27 +00002242 * the buffer, do it in the caller. This function returns the number of bytes
2243 * succesfully read. If there was an error, it returns -1. Note that it is
2244 * also possible that we return a number of bytes smaller than the request
2245 * bytes.
2246 */
Martin Blaisaf2ae722006-06-04 13:49:49 +00002247static ssize_t
Martin Blais2856e5f2006-05-26 12:03:27 +00002248sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags)
2249{
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002250 ssize_t outlen = -1;
Martin Blaisaf2ae722006-06-04 13:49:49 +00002251 int timeout;
Martin Blais2856e5f2006-05-26 12:03:27 +00002252#ifdef __VMS
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002253 int remaining;
Martin Blais2856e5f2006-05-26 12:03:27 +00002254 char *read_buf;
2255#endif
2256
2257 if (!IS_SELECTABLE(s)) {
2258 select_error();
2259 return -1;
2260 }
2261
2262#ifndef __VMS
2263 Py_BEGIN_ALLOW_THREADS
2264 timeout = internal_select(s, 0);
2265 if (!timeout)
2266 outlen = recv(s->sock_fd, cbuf, len, flags);
2267 Py_END_ALLOW_THREADS
2268
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002269 if (timeout == 1) {
Martin Blais2856e5f2006-05-26 12:03:27 +00002270 PyErr_SetString(socket_timeout, "timed out");
2271 return -1;
2272 }
2273 if (outlen < 0) {
2274 /* Note: the call to errorhandler() ALWAYS indirectly returned
2275 NULL, so ignore its return value */
2276 s->errorhandler();
2277 return -1;
2278 }
2279#else
2280 read_buf = cbuf;
2281 remaining = len;
2282 while (remaining != 0) {
2283 unsigned int segment;
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002284 int nread = -1;
Martin Blais2856e5f2006-05-26 12:03:27 +00002285
2286 segment = remaining /SEGMENT_SIZE;
2287 if (segment != 0) {
2288 segment = SEGMENT_SIZE;
2289 }
2290 else {
2291 segment = remaining;
2292 }
2293
2294 Py_BEGIN_ALLOW_THREADS
2295 timeout = internal_select(s, 0);
2296 if (!timeout)
2297 nread = recv(s->sock_fd, read_buf, segment, flags);
2298 Py_END_ALLOW_THREADS
2299
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002300 if (timeout == 1) {
Martin Blais2856e5f2006-05-26 12:03:27 +00002301 PyErr_SetString(socket_timeout, "timed out");
2302 return -1;
2303 }
2304 if (nread < 0) {
2305 s->errorhandler();
2306 return -1;
2307 }
2308 if (nread != remaining) {
2309 read_buf += nread;
2310 break;
2311 }
2312
2313 remaining -= segment;
2314 read_buf += segment;
2315 }
2316 outlen = read_buf - cbuf;
2317#endif /* !__VMS */
2318
2319 return outlen;
2320}
2321
Guido van Rossum48a680c2001-03-02 06:34:14 +00002322
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002323/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002324
Guido van Rossum73624e91994-10-10 17:59:00 +00002325static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002326sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002327{
Martin Blaisaf2ae722006-06-04 13:49:49 +00002328 int recvlen, flags = 0;
2329 ssize_t outlen;
Guido van Rossum73624e91994-10-10 17:59:00 +00002330 PyObject *buf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002331
Martin Blais2856e5f2006-05-26 12:03:27 +00002332 if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002333 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002334
Martin Blais2856e5f2006-05-26 12:03:27 +00002335 if (recvlen < 0) {
Martin v. Löwisfba64e12001-11-19 10:41:26 +00002336 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00002337 "negative buffersize in recv");
Martin v. Löwisfba64e12001-11-19 10:41:26 +00002338 return NULL;
2339 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002340
Martin Blais2856e5f2006-05-26 12:03:27 +00002341 /* Allocate a new string. */
2342 buf = PyString_FromStringAndSize((char *) 0, recvlen);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002343 if (buf == NULL)
2344 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002345
Martin Blais2856e5f2006-05-26 12:03:27 +00002346 /* Call the guts */
Martin Blaisaf2ae722006-06-04 13:49:49 +00002347 outlen = sock_recv_guts(s, PyString_AS_STRING(buf), recvlen, flags);
Martin Blais2856e5f2006-05-26 12:03:27 +00002348 if (outlen < 0) {
Georg Brandlcddabbf2006-07-28 18:36:01 +00002349 /* An error occurred, release the string and return an
Martin Blais2856e5f2006-05-26 12:03:27 +00002350 error. */
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002351 Py_DECREF(buf);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002352 return NULL;
2353 }
Martin Blais2856e5f2006-05-26 12:03:27 +00002354 if (outlen != recvlen) {
2355 /* We did not read as many bytes as we anticipated, resize the
2356 string if possible and be succesful. */
2357 if (_PyString_Resize(&buf, outlen) < 0)
2358 /* Oopsy, not so succesful after all. */
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002359 return NULL;
Martin Blais2856e5f2006-05-26 12:03:27 +00002360 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002361
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002362 return buf;
2363}
2364
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002365PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002366"recv(buffersize[, flags]) -> data\n\
2367\n\
2368Receive up to buffersize bytes from the socket. For the optional flags\n\
2369argument, see the Unix manual. When no data is available, block until\n\
2370at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002371the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002372
Guido van Rossum30a685f1991-06-27 15:51:29 +00002373
Martin Blaisaf2ae722006-06-04 13:49:49 +00002374/* s.recv_into(buffer, [nbytes [,flags]]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002375
Martin Blais2856e5f2006-05-26 12:03:27 +00002376static PyObject*
Martin Blaisaf2ae722006-06-04 13:49:49 +00002377sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)
Martin Blais2856e5f2006-05-26 12:03:27 +00002378{
2379 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
2380
Martin Blaisaf2ae722006-06-04 13:49:49 +00002381 int recvlen = 0, flags = 0;
2382 ssize_t readlen;
Martin Blais2856e5f2006-05-26 12:03:27 +00002383 char *buf;
2384 int buflen;
2385
2386 /* Get the buffer's memory */
Neal Norwitzea3307b2007-03-31 18:54:18 +00002387 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recv_into", kwlist,
Martin Blais2856e5f2006-05-26 12:03:27 +00002388 &buf, &buflen, &recvlen, &flags))
2389 return NULL;
2390 assert(buf != 0 && buflen > 0);
2391
2392 if (recvlen < 0) {
2393 PyErr_SetString(PyExc_ValueError,
Facundo Batista1fe9f962007-03-28 03:45:20 +00002394 "negative buffersize in recv_into");
Martin Blais2856e5f2006-05-26 12:03:27 +00002395 return NULL;
2396 }
2397 if (recvlen == 0) {
2398 /* If nbytes was not specified, use the buffer's length */
2399 recvlen = buflen;
2400 }
2401
2402 /* Check if the buffer is large enough */
2403 if (buflen < recvlen) {
2404 PyErr_SetString(PyExc_ValueError,
2405 "buffer too small for requested bytes");
2406 return NULL;
2407 }
2408
2409 /* Call the guts */
2410 readlen = sock_recv_guts(s, buf, recvlen, flags);
2411 if (readlen < 0) {
2412 /* Return an error. */
2413 return NULL;
2414 }
2415
2416 /* Return the number of bytes read. Note that we do not do anything
2417 special here in the case that readlen < recvlen. */
Martin Blaisaf2ae722006-06-04 13:49:49 +00002418 return PyInt_FromSsize_t(readlen);
Martin Blais2856e5f2006-05-26 12:03:27 +00002419}
2420
Martin Blaisaf2ae722006-06-04 13:49:49 +00002421PyDoc_STRVAR(recv_into_doc,
2422"recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\
Martin Blais2856e5f2006-05-26 12:03:27 +00002423\n\
2424A version of recv() that stores its data into a buffer rather than creating \n\
2425a new string. Receive up to buffersize bytes from the socket. If buffersize \n\
2426is not specified (or 0), receive up to the size available in the given buffer.\n\
2427\n\
2428See recv() for documentation about the flags.");
2429
2430
2431/*
Martin Blaisaf2ae722006-06-04 13:49:49 +00002432 * This is the guts of the recv() and recv_into() methods, which reads into a
Martin Blais2856e5f2006-05-26 12:03:27 +00002433 * char buffer. If you have any inc/def ref to do to the objects that contain
2434 * the buffer, do it in the caller. This function returns the number of bytes
2435 * succesfully read. If there was an error, it returns -1. Note that it is
2436 * also possible that we return a number of bytes smaller than the request
2437 * bytes.
2438 *
2439 * 'addr' is a return value for the address object. Note that you must decref
2440 * it yourself.
2441 */
Martin Blaisaf2ae722006-06-04 13:49:49 +00002442static ssize_t
Martin Blais2856e5f2006-05-26 12:03:27 +00002443sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags,
2444 PyObject** addr)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002445{
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002446 sock_addr_t addrbuf;
Martin Blaisaf2ae722006-06-04 13:49:49 +00002447 int timeout;
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002448 ssize_t n = -1;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002449 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002450
Martin Blais2856e5f2006-05-26 12:03:27 +00002451 *addr = NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002452
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00002453 if (!getsockaddrlen(s, &addrlen))
Martin Blais2856e5f2006-05-26 12:03:27 +00002454 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002455
Martin Blais2856e5f2006-05-26 12:03:27 +00002456 if (!IS_SELECTABLE(s)) {
2457 select_error();
2458 return -1;
2459 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002460
Guido van Rossum73624e91994-10-10 17:59:00 +00002461 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002462 memset(&addrbuf, 0, addrlen);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002463 timeout = internal_select(s, 0);
Martin v. Löwis7e75f1a2006-04-15 08:35:59 +00002464 if (!timeout) {
Guido van Rossum8d665e61996-06-26 18:22:49 +00002465#ifndef MS_WINDOWS
Andrew MacIntyreba43e872002-03-03 03:03:52 +00002466#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Martin Blais2856e5f2006-05-26 12:03:27 +00002467 n = recvfrom(s->sock_fd, cbuf, len, flags,
Martin v. Löwis046c4d12006-12-03 11:23:45 +00002468 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum32c575d1997-12-02 20:37:32 +00002469#else
Martin Blais2856e5f2006-05-26 12:03:27 +00002470 n = recvfrom(s->sock_fd, cbuf, len, flags,
Martin v. Löwis7e75f1a2006-04-15 08:35:59 +00002471 (void *) &addrbuf, &addrlen);
Guido van Rossum32c575d1997-12-02 20:37:32 +00002472#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002473#else
Martin Blais2856e5f2006-05-26 12:03:27 +00002474 n = recvfrom(s->sock_fd, cbuf, len, flags,
Martin v. Löwis046c4d12006-12-03 11:23:45 +00002475 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002476#endif
Martin v. Löwis7e75f1a2006-04-15 08:35:59 +00002477 }
Guido van Rossum73624e91994-10-10 17:59:00 +00002478 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002479
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002480 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002481 PyErr_SetString(socket_timeout, "timed out");
Martin Blais2856e5f2006-05-26 12:03:27 +00002482 return -1;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002483 }
Guido van Rossum7c53b771995-09-13 18:39:47 +00002484 if (n < 0) {
Martin Blais2856e5f2006-05-26 12:03:27 +00002485 s->errorhandler();
2486 return -1;
Guido van Rossum7c53b771995-09-13 18:39:47 +00002487 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002488
Martin v. Löwis046c4d12006-12-03 11:23:45 +00002489 if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
Martin Blais2856e5f2006-05-26 12:03:27 +00002490 addrlen, s->sock_proto)))
2491 return -1;
2492
2493 return n;
2494}
2495
2496/* s.recvfrom(nbytes [,flags]) method */
2497
2498static PyObject *
2499sock_recvfrom(PySocketSockObject *s, PyObject *args)
2500{
2501 PyObject *buf = NULL;
2502 PyObject *addr = NULL;
2503 PyObject *ret = NULL;
Martin Blaisaf2ae722006-06-04 13:49:49 +00002504 int recvlen, flags = 0;
2505 ssize_t outlen;
Martin Blais2856e5f2006-05-26 12:03:27 +00002506
2507 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002508 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002509
Facundo Batista1fe9f962007-03-28 03:45:20 +00002510 if (recvlen < 0) {
2511 PyErr_SetString(PyExc_ValueError,
2512 "negative buffersize in recvfrom");
2513 return NULL;
2514 }
2515
Martin Blais2856e5f2006-05-26 12:03:27 +00002516 buf = PyString_FromStringAndSize((char *) 0, recvlen);
2517 if (buf == NULL)
2518 return NULL;
2519
2520 outlen = sock_recvfrom_guts(s, PyString_AS_STRING(buf),
2521 recvlen, flags, &addr);
2522 if (outlen < 0) {
Barry Warsaw752300b1997-01-03 17:18:10 +00002523 goto finally;
Martin Blais2856e5f2006-05-26 12:03:27 +00002524 }
2525
2526 if (outlen != recvlen) {
2527 /* We did not read as many bytes as we anticipated, resize the
2528 string if possible and be succesful. */
2529 if (_PyString_Resize(&buf, outlen) < 0)
2530 /* Oopsy, not so succesful after all. */
2531 goto finally;
2532 }
Barry Warsaw752300b1997-01-03 17:18:10 +00002533
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002534 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002535
2536finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00002537 Py_XDECREF(buf);
Martin Blais2856e5f2006-05-26 12:03:27 +00002538 Py_XDECREF(addr);
Guido van Rossum6f5afc91993-02-05 09:46:15 +00002539 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002540}
2541
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002542PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002543"recvfrom(buffersize[, flags]) -> (data, address info)\n\
2544\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002545Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002546
Martin Blais2856e5f2006-05-26 12:03:27 +00002547
Martin Blaisaf2ae722006-06-04 13:49:49 +00002548/* s.recvfrom_into(buffer[, nbytes [,flags]]) method */
Martin Blais2856e5f2006-05-26 12:03:27 +00002549
2550static PyObject *
Martin Blaisaf2ae722006-06-04 13:49:49 +00002551sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
Martin Blais2856e5f2006-05-26 12:03:27 +00002552{
2553 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
2554
Martin Blaisaf2ae722006-06-04 13:49:49 +00002555 int recvlen = 0, flags = 0;
2556 ssize_t readlen;
Martin Blais2856e5f2006-05-26 12:03:27 +00002557 char *buf;
2558 int buflen;
2559
2560 PyObject *addr = NULL;
Martin Blais2856e5f2006-05-26 12:03:27 +00002561
Neal Norwitzea3307b2007-03-31 18:54:18 +00002562 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recvfrom_into",
2563 kwlist, &buf, &buflen,
2564 &recvlen, &flags))
Martin Blais2856e5f2006-05-26 12:03:27 +00002565 return NULL;
2566 assert(buf != 0 && buflen > 0);
2567
2568 if (recvlen < 0) {
2569 PyErr_SetString(PyExc_ValueError,
Facundo Batista1fe9f962007-03-28 03:45:20 +00002570 "negative buffersize in recvfrom_into");
Martin Blais2856e5f2006-05-26 12:03:27 +00002571 return NULL;
2572 }
2573 if (recvlen == 0) {
2574 /* If nbytes was not specified, use the buffer's length */
2575 recvlen = buflen;
2576 }
2577
2578 readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
2579 if (readlen < 0) {
2580 /* Return an error */
Martin Blaisaf2ae722006-06-04 13:49:49 +00002581 Py_XDECREF(addr);
2582 return NULL;
Martin Blais2856e5f2006-05-26 12:03:27 +00002583 }
2584
2585 /* Return the number of bytes read and the address. Note that we do
2586 not do anything special here in the case that readlen < recvlen. */
Neal Norwitzf0cab1f2006-06-08 05:12:45 +00002587 return Py_BuildValue("lN", readlen, addr);
Martin Blais2856e5f2006-05-26 12:03:27 +00002588}
2589
Martin Blaisaf2ae722006-06-04 13:49:49 +00002590PyDoc_STRVAR(recvfrom_into_doc,
2591"recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
Martin Blais2856e5f2006-05-26 12:03:27 +00002592\n\
Martin Blaisaf2ae722006-06-04 13:49:49 +00002593Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");
Martin Blais2856e5f2006-05-26 12:03:27 +00002594
2595
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002596/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002597
Guido van Rossum73624e91994-10-10 17:59:00 +00002598static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002599sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002600{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002601 char *buf;
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002602 int len, n = -1, flags = 0, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002603
Guido van Rossum43713e52000-02-29 13:59:29 +00002604 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002605 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002606
Neal Norwitz082b2df2006-02-07 07:04:46 +00002607 if (!IS_SELECTABLE(s))
2608 return select_error();
2609
Guido van Rossum73624e91994-10-10 17:59:00 +00002610 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002611 timeout = internal_select(s, 1);
2612 if (!timeout)
Neal Norwitz2a30cd02006-07-10 01:18:57 +00002613#ifdef __VMS
2614 n = sendsegmented(s->sock_fd, buf, len, flags);
2615#else
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002616 n = send(s->sock_fd, buf, len, flags);
Neal Norwitz2a30cd02006-07-10 01:18:57 +00002617#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00002618 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002619
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002620 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002621 PyErr_SetString(socket_timeout, "timed out");
2622 return NULL;
2623 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00002624 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002625 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002626 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00002627}
2628
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002629PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002630"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002631\n\
2632Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002633argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002634sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002635
2636
2637/* s.sendall(data [,flags]) method */
2638
2639static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002640sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002641{
2642 char *buf;
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002643 int len, n = -1, flags = 0, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002644
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002645 if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags))
2646 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002647
Neal Norwitz082b2df2006-02-07 07:04:46 +00002648 if (!IS_SELECTABLE(s))
2649 return select_error();
2650
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002651 Py_BEGIN_ALLOW_THREADS
2652 do {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002653 timeout = internal_select(s, 1);
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002654 n = -1;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002655 if (timeout)
2656 break;
Neal Norwitz2a30cd02006-07-10 01:18:57 +00002657#ifdef __VMS
2658 n = sendsegmented(s->sock_fd, buf, len, flags);
2659#else
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002660 n = send(s->sock_fd, buf, len, flags);
Neal Norwitz2a30cd02006-07-10 01:18:57 +00002661#endif
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002662 if (n < 0)
2663 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002664 buf += n;
2665 len -= n;
2666 } while (len > 0);
2667 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002668
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002669 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002670 PyErr_SetString(socket_timeout, "timed out");
2671 return NULL;
2672 }
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002673 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002674 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00002675
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002676 Py_INCREF(Py_None);
2677 return Py_None;
2678}
2679
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002680PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002681"sendall(data[, flags])\n\
2682\n\
2683Send a data string to the socket. For the optional flags\n\
2684argument, see the Unix manual. This calls send() repeatedly\n\
2685until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002686to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002687
Guido van Rossum30a685f1991-06-27 15:51:29 +00002688
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002689/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002690
Guido van Rossum73624e91994-10-10 17:59:00 +00002691static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002692sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002693{
Guido van Rossum73624e91994-10-10 17:59:00 +00002694 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002695 char *buf;
Martin v. Löwis046c4d12006-12-03 11:23:45 +00002696 sock_addr_t addrbuf;
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002697 int addrlen, len, n = -1, flags, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002698
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002699 flags = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002700 if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00002701 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002702 if (!PyArg_ParseTuple(args, "s#iO:sendto",
2703 &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002704 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002705 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002706
Neal Norwitz082b2df2006-02-07 07:04:46 +00002707 if (!IS_SELECTABLE(s))
2708 return select_error();
2709
Martin v. Löwis046c4d12006-12-03 11:23:45 +00002710 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2711 return NULL;
2712
Guido van Rossum73624e91994-10-10 17:59:00 +00002713 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002714 timeout = internal_select(s, 1);
2715 if (!timeout)
Martin v. Löwis046c4d12006-12-03 11:23:45 +00002716 n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002717 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002718
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002719 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002720 PyErr_SetString(socket_timeout, "timed out");
2721 return NULL;
2722 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002723 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002724 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002725 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002726}
2727
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002728PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002729"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002730\n\
2731Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002732For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002733
Guido van Rossum30a685f1991-06-27 15:51:29 +00002734
2735/* s.shutdown(how) method */
2736
Guido van Rossum73624e91994-10-10 17:59:00 +00002737static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002738sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002739{
2740 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002741 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002742
2743 how = PyInt_AsLong(arg);
2744 if (how == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002745 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002746 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002747 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00002748 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002749 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002750 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002751 Py_INCREF(Py_None);
2752 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002753}
2754
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002755PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002756"shutdown(flag)\n\
2757\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00002758Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
2759of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002760
Guido van Rossum30a685f1991-06-27 15:51:29 +00002761
2762/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002763
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002764static PyMethodDef sock_methods[] = {
Martin Blais2856e5f2006-05-26 12:03:27 +00002765 {"accept", (PyCFunction)sock_accept, METH_NOARGS,
2766 accept_doc},
2767 {"bind", (PyCFunction)sock_bind, METH_O,
2768 bind_doc},
2769 {"close", (PyCFunction)sock_close, METH_NOARGS,
2770 close_doc},
2771 {"connect", (PyCFunction)sock_connect, METH_O,
2772 connect_doc},
2773 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
2774 connect_ex_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002775#ifndef NO_DUP
Martin Blais2856e5f2006-05-26 12:03:27 +00002776 {"dup", (PyCFunction)sock_dup, METH_NOARGS,
2777 dup_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002778#endif
Martin Blais2856e5f2006-05-26 12:03:27 +00002779 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
2780 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002781#ifdef HAVE_GETPEERNAME
Martin Blais2856e5f2006-05-26 12:03:27 +00002782 {"getpeername", (PyCFunction)sock_getpeername,
2783 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00002784#endif
Martin Blais2856e5f2006-05-26 12:03:27 +00002785 {"getsockname", (PyCFunction)sock_getsockname,
2786 METH_NOARGS, getsockname_doc},
2787 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
2788 getsockopt_doc},
2789 {"listen", (PyCFunction)sock_listen, METH_O,
2790 listen_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002791#ifndef NO_DUP
Martin Blais2856e5f2006-05-26 12:03:27 +00002792 {"makefile", (PyCFunction)sock_makefile, METH_VARARGS,
2793 makefile_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002794#endif
Martin Blais2856e5f2006-05-26 12:03:27 +00002795 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
2796 recv_doc},
Martin Blaisaf2ae722006-06-04 13:49:49 +00002797 {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
2798 recv_into_doc},
Martin Blais2856e5f2006-05-26 12:03:27 +00002799 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
2800 recvfrom_doc},
Martin Blaisaf2ae722006-06-04 13:49:49 +00002801 {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
2802 recvfrom_into_doc},
Martin Blais2856e5f2006-05-26 12:03:27 +00002803 {"send", (PyCFunction)sock_send, METH_VARARGS,
2804 send_doc},
2805 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
2806 sendall_doc},
2807 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
2808 sendto_doc},
2809 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
2810 setblocking_doc},
2811 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
2812 settimeout_doc},
2813 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
2814 gettimeout_doc},
2815 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
2816 setsockopt_doc},
2817 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
2818 shutdown_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002819#ifdef RISCOS
Martin Blais2856e5f2006-05-26 12:03:27 +00002820 {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O,
2821 sleeptaskw_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002822#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00002823 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002824};
2825
Georg Brandlbc45a3f2006-03-17 19:17:34 +00002826/* SockObject members */
2827static PyMemberDef sock_memberlist[] = {
2828 {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},
2829 {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},
2830 {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},
2831 {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"},
2832 {0},
2833};
Guido van Rossum30a685f1991-06-27 15:51:29 +00002834
Guido van Rossum73624e91994-10-10 17:59:00 +00002835/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00002836 First close the file description. */
2837
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002838static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002839sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002840{
Guido van Rossumfa972c92000-04-10 12:45:45 +00002841 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00002842 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002843 s->ob_type->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002844}
2845
Guido van Rossum30a685f1991-06-27 15:51:29 +00002846
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002847static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002848sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002849{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002850 char buf[512];
Fred Drakea04eaad2000-06-30 02:46:07 +00002851#if SIZEOF_SOCKET_T > SIZEOF_LONG
2852 if (s->sock_fd > LONG_MAX) {
2853 /* this can occur on Win64, and actually there is a special
2854 ugly printf formatter for decimal pointer length integer
2855 printing, only bother if necessary*/
2856 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002857 "no printf formatter to display "
2858 "the socket descriptor in decimal");
Fred Drakea04eaad2000-06-30 02:46:07 +00002859 return NULL;
2860 }
2861#endif
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002862 PyOS_snprintf(
2863 buf, sizeof(buf),
2864 "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",
2865 (long)s->sock_fd, s->sock_family,
2866 s->sock_type,
2867 s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00002868 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002869}
2870
2871
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002872/* Create a new, uninitialized socket object. */
2873
2874static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002875sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002876{
2877 PyObject *new;
2878
2879 new = type->tp_alloc(type, 0);
Guido van Rossume1c478f2002-06-06 20:08:25 +00002880 if (new != NULL) {
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002881 ((PySocketSockObject *)new)->sock_fd = -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002882 ((PySocketSockObject *)new)->sock_timeout = -1.0;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002883 ((PySocketSockObject *)new)->errorhandler = &set_error;
Guido van Rossume1c478f2002-06-06 20:08:25 +00002884 }
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002885 return new;
2886}
2887
2888
2889/* Initialize a new socket object. */
2890
2891/*ARGSUSED*/
2892static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002893sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002894{
2895 PySocketSockObject *s = (PySocketSockObject *)self;
2896 SOCKET_T fd;
2897 int family = AF_INET, type = SOCK_STREAM, proto = 0;
Martin v. Löwis02cbf4a2006-02-27 17:20:04 +00002898 static char *keywords[] = {"family", "type", "proto", 0};
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002899
2900 if (!PyArg_ParseTupleAndKeywords(args, kwds,
2901 "|iii:socket", keywords,
2902 &family, &type, &proto))
2903 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002904
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002905 Py_BEGIN_ALLOW_THREADS
2906 fd = socket(family, type, proto);
2907 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002908
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002909#ifdef MS_WINDOWS
2910 if (fd == INVALID_SOCKET)
2911#else
2912 if (fd < 0)
2913#endif
2914 {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002915 set_error();
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002916 return -1;
2917 }
2918 init_sockobject(s, fd, family, type, proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002919
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002920 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002921
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002922}
2923
2924
Guido van Rossumb6775db1994-08-01 11:34:53 +00002925/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002926
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002927static PyTypeObject sock_type = {
Guido van Rossum55558541996-05-23 22:54:50 +00002928 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002929 0, /* ob_size */
Guido van Rossumca6dfa52001-10-28 12:31:33 +00002930 "_socket.socket", /* tp_name */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002931 sizeof(PySocketSockObject), /* tp_basicsize */
2932 0, /* tp_itemsize */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002933 (destructor)sock_dealloc, /* tp_dealloc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002934 0, /* tp_print */
2935 0, /* tp_getattr */
2936 0, /* tp_setattr */
2937 0, /* tp_compare */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002938 (reprfunc)sock_repr, /* tp_repr */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002939 0, /* tp_as_number */
2940 0, /* tp_as_sequence */
2941 0, /* tp_as_mapping */
2942 0, /* tp_hash */
2943 0, /* tp_call */
2944 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002945 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002946 0, /* tp_setattro */
2947 0, /* tp_as_buffer */
2948 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002949 sock_doc, /* tp_doc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002950 0, /* tp_traverse */
2951 0, /* tp_clear */
2952 0, /* tp_richcompare */
2953 0, /* tp_weaklistoffset */
2954 0, /* tp_iter */
2955 0, /* tp_iternext */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002956 sock_methods, /* tp_methods */
Georg Brandlbc45a3f2006-03-17 19:17:34 +00002957 sock_memberlist, /* tp_members */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002958 0, /* tp_getset */
2959 0, /* tp_base */
2960 0, /* tp_dict */
2961 0, /* tp_descr_get */
2962 0, /* tp_descr_set */
2963 0, /* tp_dictoffset */
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002964 sock_initobj, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002965 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002966 sock_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002967 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002968};
2969
Guido van Rossum30a685f1991-06-27 15:51:29 +00002970
Guido van Rossum81194471991-07-27 21:42:02 +00002971/* Python interface to gethostname(). */
2972
2973/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002974static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00002975socket_gethostname(PyObject *self, PyObject *unused)
Guido van Rossum81194471991-07-27 21:42:02 +00002976{
2977 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00002978 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00002979 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002980 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00002981 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002982 if (res < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002983 return set_error();
Guido van Rossum81194471991-07-27 21:42:02 +00002984 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00002985 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00002986}
Guido van Rossumff4949e1992-08-05 19:58:53 +00002987
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002988PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002989"gethostname() -> string\n\
2990\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002991Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002992
Guido van Rossumff4949e1992-08-05 19:58:53 +00002993
Guido van Rossum30a685f1991-06-27 15:51:29 +00002994/* Python interface to gethostbyname(name). */
2995
2996/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002997static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002998socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002999{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003000 char *name;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00003001 sock_addr_t addrbuf;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003002
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003003 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00003004 return NULL;
Martin v. Löwis046c4d12006-12-03 11:23:45 +00003005 if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00003006 return NULL;
Martin v. Löwis046c4d12006-12-03 11:23:45 +00003007 return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
Guido van Rossum30a685f1991-06-27 15:51:29 +00003008}
3009
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003010PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003011"gethostbyname(host) -> address\n\
3012\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003013Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003014
3015
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003016/* Convenience function common to gethostbyname_ex and gethostbyaddr */
3017
3018static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003019gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003020{
3021 char **pch;
3022 PyObject *rtn_tuple = (PyObject *)NULL;
3023 PyObject *name_list = (PyObject *)NULL;
3024 PyObject *addr_list = (PyObject *)NULL;
3025 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003026
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003027 if (h == NULL) {
Guido van Rossum48a680c2001-03-02 06:34:14 +00003028 /* Let's get real error message to return */
Guido van Rossume2ae77b2001-10-24 20:42:55 +00003029#ifndef RISCOS
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003030 set_herror(h_errno);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00003031#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003032 PyErr_SetString(socket_error, "host not found");
Guido van Rossume2ae77b2001-10-24 20:42:55 +00003033#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003034 return NULL;
3035 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003036
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003037 if (h->h_addrtype != af) {
3038#ifdef HAVE_STRERROR
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003039 /* Let's get real error message to return */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003040 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003041 (char *)strerror(EAFNOSUPPORT));
3042#else
3043 PyErr_SetString(
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003044 socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003045 "Address family not supported by protocol family");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003046#endif
3047 return NULL;
3048 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003049
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003050 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00003051
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003052 case AF_INET:
3053 if (alen < sizeof(struct sockaddr_in))
3054 return NULL;
3055 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003056
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003057#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003058 case AF_INET6:
3059 if (alen < sizeof(struct sockaddr_in6))
3060 return NULL;
3061 break;
3062#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00003063
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003064 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003065
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003066 if ((name_list = PyList_New(0)) == NULL)
3067 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003068
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003069 if ((addr_list = PyList_New(0)) == NULL)
3070 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003071
Georg Brandl9dc7b7c2006-08-14 22:10:24 +00003072 /* SF #1511317: h_aliases can be NULL */
3073 if (h->h_aliases) {
3074 for (pch = h->h_aliases; *pch != NULL; pch++) {
3075 int status;
3076 tmp = PyString_FromString(*pch);
3077 if (tmp == NULL)
3078 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003079
Georg Brandl9dc7b7c2006-08-14 22:10:24 +00003080 status = PyList_Append(name_list, tmp);
3081 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003082
Georg Brandl9dc7b7c2006-08-14 22:10:24 +00003083 if (status)
3084 goto err;
3085 }
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003086 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003087
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003088 for (pch = h->h_addr_list; *pch != NULL; pch++) {
3089 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003090
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003091 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00003092
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003093 case AF_INET:
3094 {
3095 struct sockaddr_in sin;
3096 memset(&sin, 0, sizeof(sin));
3097 sin.sin_family = af;
3098#ifdef HAVE_SOCKADDR_SA_LEN
3099 sin.sin_len = sizeof(sin);
3100#endif
3101 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
3102 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00003103
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003104 if (pch == h->h_addr_list && alen >= sizeof(sin))
3105 memcpy((char *) addr, &sin, sizeof(sin));
3106 break;
3107 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003108
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003109#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003110 case AF_INET6:
3111 {
3112 struct sockaddr_in6 sin6;
3113 memset(&sin6, 0, sizeof(sin6));
3114 sin6.sin6_family = af;
3115#ifdef HAVE_SOCKADDR_SA_LEN
3116 sin6.sin6_len = sizeof(sin6);
3117#endif
3118 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
3119 tmp = makeipaddr((struct sockaddr *)&sin6,
3120 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00003121
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003122 if (pch == h->h_addr_list && alen >= sizeof(sin6))
3123 memcpy((char *) addr, &sin6, sizeof(sin6));
3124 break;
3125 }
3126#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00003127
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003128 default: /* can't happen */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003129 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003130 "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003131 return NULL;
3132 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003133
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003134 if (tmp == NULL)
3135 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003136
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003137 status = PyList_Append(addr_list, tmp);
3138 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003139
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003140 if (status)
3141 goto err;
3142 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003143
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003144 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003145
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003146 err:
3147 Py_XDECREF(name_list);
3148 Py_XDECREF(addr_list);
3149 return rtn_tuple;
3150}
3151
3152
3153/* Python interface to gethostbyname_ex(name). */
3154
3155/*ARGSUSED*/
3156static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003157socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003158{
3159 char *name;
3160 struct hostent *h;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00003161#ifdef ENABLE_IPV6
3162 struct sockaddr_storage addr;
3163#else
3164 struct sockaddr_in addr;
3165#endif
Martin v. Löwis9db2f572001-07-23 01:30:10 +00003166 struct sockaddr *sa;
Guido van Rossum955becc1999-03-22 20:14:53 +00003167 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003168#ifdef HAVE_GETHOSTBYNAME_R
3169 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003170#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
3171 struct hostent_data data;
3172#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003173 char buf[16384];
3174 int buf_len = (sizeof buf) - 1;
3175 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003176#endif
3177#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003178 int result;
3179#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003180#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003181
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003182 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003183 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003184 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003185 return NULL;
3186 Py_BEGIN_ALLOW_THREADS
3187#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003188#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003189 result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
3190 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003191#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003192 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003193#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00003194 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003195 result = gethostbyname_r(name, &hp_allocated, &data);
3196 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003197#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003198#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003199#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00003200 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003201#endif
3202 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003203#endif /* HAVE_GETHOSTBYNAME_R */
3204 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00003205 /* Some C libraries would require addr.__ss_family instead of
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003206 addr.ss_family.
3207 Therefore, we cast the sockaddr_storage into sockaddr to
3208 access sa_family. */
Martin v. Löwis9db2f572001-07-23 01:30:10 +00003209 sa = (struct sockaddr*)&addr;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003210 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003211 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003212#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00003213 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00003214#endif
3215 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003216}
3217
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003218PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003219"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
3220\n\
3221Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003222for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003223
3224
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003225/* Python interface to gethostbyaddr(IP). */
3226
3227/*ARGSUSED*/
3228static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003229socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003230{
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003231#ifdef ENABLE_IPV6
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003232 struct sockaddr_storage addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003233#else
Guido van Rossum48a680c2001-03-02 06:34:14 +00003234 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003235#endif
3236 struct sockaddr *sa = (struct sockaddr *)&addr;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003237 char *ip_num;
3238 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00003239 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003240#ifdef HAVE_GETHOSTBYNAME_R
3241 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003242#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
3243 struct hostent_data data;
3244#else
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003245 char buf[16384];
3246 int buf_len = (sizeof buf) - 1;
3247 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003248#endif
3249#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003250 int result;
3251#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003252#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003253 char *ap;
3254 int al;
3255 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003256
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003257 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003258 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003259 af = AF_UNSPEC;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00003260 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003261 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003262 af = sa->sa_family;
3263 ap = NULL;
3264 al = 0;
3265 switch (af) {
3266 case AF_INET:
3267 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
3268 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3269 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003270#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003271 case AF_INET6:
3272 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
3273 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3274 break;
3275#endif
3276 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003277 PyErr_SetString(socket_error, "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003278 return NULL;
3279 }
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003280 Py_BEGIN_ALLOW_THREADS
3281#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003282#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003283 result = gethostbyaddr_r(ap, al, af,
3284 &hp_allocated, buf, buf_len,
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003285 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003286#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003287 h = gethostbyaddr_r(ap, al, af,
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003288 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003289#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00003290 memset((void *) &data, '\0', sizeof(data));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003291 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003292 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003293#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003294#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003295#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00003296 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003297#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003298 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003299#endif /* HAVE_GETHOSTBYNAME_R */
3300 Py_END_ALLOW_THREADS
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003301 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003302#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00003303 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003304#endif
3305 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003306}
3307
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003308PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003309"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
3310\n\
3311Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003312for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003313
Guido van Rossum30a685f1991-06-27 15:51:29 +00003314
3315/* Python interface to getservbyname(name).
3316 This only returns the port number, since the other info is already
3317 known or not useful (like the list of aliases). */
3318
3319/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00003320static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003321socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00003322{
Barry Warsaw11b91a02004-06-28 00:50:43 +00003323 char *name, *proto=NULL;
Guido van Rossum30a685f1991-06-27 15:51:29 +00003324 struct servent *sp;
Barry Warsaw11b91a02004-06-28 00:50:43 +00003325 if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00003326 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00003327 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00003328 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00003329 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00003330 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003331 PyErr_SetString(socket_error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00003332 return NULL;
3333 }
Guido van Rossum73624e91994-10-10 17:59:00 +00003334 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00003335}
3336
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003337PyDoc_STRVAR(getservbyname_doc,
Barry Warsaw11b91a02004-06-28 00:50:43 +00003338"getservbyname(servicename[, protocolname]) -> integer\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003339\n\
3340Return a port number from a service name and protocol name.\n\
Barry Warsaw11b91a02004-06-28 00:50:43 +00003341The optional protocol name, if given, should be 'tcp' or 'udp',\n\
3342otherwise any protocol will match.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003343
Guido van Rossum30a685f1991-06-27 15:51:29 +00003344
Barry Warsaw11b91a02004-06-28 00:50:43 +00003345/* Python interface to getservbyport(port).
3346 This only returns the service name, since the other info is already
3347 known or not useful (like the list of aliases). */
3348
3349/*ARGSUSED*/
3350static PyObject *
3351socket_getservbyport(PyObject *self, PyObject *args)
3352{
Raymond Hettinger87de0ca2004-09-28 02:19:40 +00003353 unsigned short port;
Barry Warsaw11b91a02004-06-28 00:50:43 +00003354 char *proto=NULL;
3355 struct servent *sp;
Raymond Hettinger87de0ca2004-09-28 02:19:40 +00003356 if (!PyArg_ParseTuple(args, "H|s:getservbyport", &port, &proto))
Barry Warsaw11b91a02004-06-28 00:50:43 +00003357 return NULL;
3358 Py_BEGIN_ALLOW_THREADS
3359 sp = getservbyport(htons(port), proto);
3360 Py_END_ALLOW_THREADS
3361 if (sp == NULL) {
3362 PyErr_SetString(socket_error, "port/proto not found");
3363 return NULL;
3364 }
3365 return PyString_FromString(sp->s_name);
3366}
3367
3368PyDoc_STRVAR(getservbyport_doc,
3369"getservbyport(port[, protocolname]) -> string\n\
3370\n\
3371Return the service name from a port number and protocol name.\n\
3372The optional protocol name, if given, should be 'tcp' or 'udp',\n\
3373otherwise any protocol will match.");
3374
Guido van Rossum3901d851996-12-19 16:35:04 +00003375/* Python interface to getprotobyname(name).
3376 This only returns the protocol number, since the other info is
3377 already known or not useful (like the list of aliases). */
3378
3379/*ARGSUSED*/
3380static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003381socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00003382{
3383 char *name;
3384 struct protoent *sp;
Guido van Rossumbcc20741998-08-04 22:53:56 +00003385#ifdef __BEOS__
3386/* Not available in BeOS yet. - [cjh] */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003387 PyErr_SetString(socket_error, "getprotobyname not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00003388 return NULL;
3389#else
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003390 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00003391 return NULL;
3392 Py_BEGIN_ALLOW_THREADS
3393 sp = getprotobyname(name);
3394 Py_END_ALLOW_THREADS
3395 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003396 PyErr_SetString(socket_error, "protocol not found");
Guido van Rossum3901d851996-12-19 16:35:04 +00003397 return NULL;
3398 }
3399 return PyInt_FromLong((long) sp->p_proto);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003400#endif
Guido van Rossum3901d851996-12-19 16:35:04 +00003401}
3402
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003403PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003404"getprotobyname(name) -> integer\n\
3405\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003406Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003407
Guido van Rossum3901d851996-12-19 16:35:04 +00003408
Dave Cole331708b2004-08-09 04:51:41 +00003409#ifdef HAVE_SOCKETPAIR
3410/* Create a pair of sockets using the socketpair() function.
Dave Cole07fda7e2004-08-23 05:16:23 +00003411 Arguments as for socket() except the default family is AF_UNIX if
Dave Colee8bbfe42004-08-26 00:51:16 +00003412 defined on the platform; otherwise, the default is AF_INET. */
Dave Cole331708b2004-08-09 04:51:41 +00003413
3414/*ARGSUSED*/
3415static PyObject *
3416socket_socketpair(PyObject *self, PyObject *args)
3417{
3418 PySocketSockObject *s0 = NULL, *s1 = NULL;
3419 SOCKET_T sv[2];
3420 int family, type = SOCK_STREAM, proto = 0;
3421 PyObject *res = NULL;
3422
3423#if defined(AF_UNIX)
3424 family = AF_UNIX;
3425#else
3426 family = AF_INET;
3427#endif
3428 if (!PyArg_ParseTuple(args, "|iii:socketpair",
3429 &family, &type, &proto))
3430 return NULL;
3431 /* Create a pair of socket fds */
3432 if (socketpair(family, type, proto, sv) < 0)
3433 return set_error();
Dave Cole331708b2004-08-09 04:51:41 +00003434 s0 = new_sockobject(sv[0], family, type, proto);
3435 if (s0 == NULL)
3436 goto finally;
3437 s1 = new_sockobject(sv[1], family, type, proto);
3438 if (s1 == NULL)
3439 goto finally;
3440 res = PyTuple_Pack(2, s0, s1);
3441
3442finally:
3443 if (res == NULL) {
3444 if (s0 == NULL)
3445 SOCKETCLOSE(sv[0]);
3446 if (s1 == NULL)
3447 SOCKETCLOSE(sv[1]);
3448 }
3449 Py_XDECREF(s0);
3450 Py_XDECREF(s1);
3451 return res;
3452}
3453
3454PyDoc_STRVAR(socketpair_doc,
3455"socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\
3456\n\
3457Create a pair of socket objects from the sockets returned by the platform\n\
3458socketpair() function.\n\
Dave Cole07fda7e2004-08-23 05:16:23 +00003459The arguments are the same as for socket() except the default family is\n\
Dave Colee8bbfe42004-08-26 00:51:16 +00003460AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
Dave Cole331708b2004-08-09 04:51:41 +00003461
3462#endif /* HAVE_SOCKETPAIR */
3463
3464
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003465#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00003466/* Create a socket object from a numeric file description.
3467 Useful e.g. if stdin is a socket.
3468 Additional arguments as for socket(). */
3469
3470/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00003471static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003472socket_fromfd(PyObject *self, PyObject *args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00003473{
Guido van Rossum73624e91994-10-10 17:59:00 +00003474 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00003475 SOCKET_T fd;
3476 int family, type, proto = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003477 if (!PyArg_ParseTuple(args, "iii|i:fromfd",
3478 &fd, &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00003479 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00003480 /* Dup the fd so it and the socket can be closed independently */
3481 fd = dup(fd);
3482 if (fd < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003483 return set_error();
3484 s = new_sockobject(fd, family, type, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00003485 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00003486}
Guido van Rossum82a5c661998-07-07 20:45:43 +00003487
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003488PyDoc_STRVAR(fromfd_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003489"fromfd(fd, family, type[, proto]) -> socket object\n\
3490\n\
Georg Brandldcfdae72006-04-01 07:33:08 +00003491Create a socket object from a duplicate of the given\n\
3492file descriptor.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003493The remaining arguments are the same as for socket().");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003494
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003495#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00003496
Guido van Rossum82a5c661998-07-07 20:45:43 +00003497
Guido van Rossum006bf911996-06-12 04:04:55 +00003498static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003499socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003500{
3501 int x1, x2;
3502
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003503 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00003504 return NULL;
3505 }
Guido van Rossumbb2cc692007-01-14 17:03:32 +00003506 if (x1 < 0) {
3507 PyErr_SetString(PyExc_OverflowError,
3508 "can't convert negative number to unsigned long");
3509 return NULL;
3510 }
3511 x2 = (unsigned int)ntohs((unsigned short)x1);
Guido van Rossum006bf911996-06-12 04:04:55 +00003512 return PyInt_FromLong(x2);
3513}
3514
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003515PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003516"ntohs(integer) -> integer\n\
3517\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003518Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003519
3520
Guido van Rossum006bf911996-06-12 04:04:55 +00003521static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003522socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003523{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003524 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003525
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003526 if (PyInt_Check(arg)) {
3527 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003528 if (x == (unsigned long) -1 && PyErr_Occurred())
3529 return NULL;
Guido van Rossumbb2cc692007-01-14 17:03:32 +00003530 if ((long)x < 0) {
3531 PyErr_SetString(PyExc_OverflowError,
3532 "can't convert negative number to unsigned long");
3533 return NULL;
3534 }
Guido van Rossum006bf911996-06-12 04:04:55 +00003535 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003536 else if (PyLong_Check(arg)) {
3537 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003538 if (x == (unsigned long) -1 && PyErr_Occurred())
3539 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003540#if SIZEOF_LONG > 4
3541 {
3542 unsigned long y;
3543 /* only want the trailing 32 bits */
3544 y = x & 0xFFFFFFFFUL;
3545 if (y ^ x)
3546 return PyErr_Format(PyExc_OverflowError,
3547 "long int larger than 32 bits");
3548 x = y;
Tim Peters58141872002-08-06 22:25:02 +00003549 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003550#endif
3551 }
3552 else
Tim Peters58141872002-08-06 22:25:02 +00003553 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003554 "expected int/long, %s found",
3555 arg->ob_type->tp_name);
3556 if (x == (unsigned long) -1 && PyErr_Occurred())
3557 return NULL;
Guido van Rossumbb2cc692007-01-14 17:03:32 +00003558 return PyLong_FromUnsignedLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003559}
3560
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003561PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003562"ntohl(integer) -> integer\n\
3563\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003564Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003565
3566
Guido van Rossum006bf911996-06-12 04:04:55 +00003567static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003568socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003569{
Hye-Shik Changa7b673f2003-12-17 09:40:23 +00003570 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00003571
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003572 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00003573 return NULL;
3574 }
Guido van Rossumbb2cc692007-01-14 17:03:32 +00003575 if (x1 < 0) {
3576 PyErr_SetString(PyExc_OverflowError,
3577 "can't convert negative number to unsigned long");
3578 return NULL;
3579 }
3580 x2 = (unsigned int)htons((unsigned short)x1);
Guido van Rossum006bf911996-06-12 04:04:55 +00003581 return PyInt_FromLong(x2);
3582}
3583
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003584PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003585"htons(integer) -> integer\n\
3586\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003587Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003588
3589
Guido van Rossum006bf911996-06-12 04:04:55 +00003590static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003591socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003592{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003593 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003594
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003595 if (PyInt_Check(arg)) {
3596 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003597 if (x == (unsigned long) -1 && PyErr_Occurred())
3598 return NULL;
Guido van Rossumbb2cc692007-01-14 17:03:32 +00003599 if ((long)x < 0) {
3600 PyErr_SetString(PyExc_OverflowError,
3601 "can't convert negative number to unsigned long");
3602 return NULL;
3603 }
Guido van Rossum006bf911996-06-12 04:04:55 +00003604 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003605 else if (PyLong_Check(arg)) {
3606 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003607 if (x == (unsigned long) -1 && PyErr_Occurred())
3608 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003609#if SIZEOF_LONG > 4
3610 {
3611 unsigned long y;
3612 /* only want the trailing 32 bits */
3613 y = x & 0xFFFFFFFFUL;
3614 if (y ^ x)
3615 return PyErr_Format(PyExc_OverflowError,
3616 "long int larger than 32 bits");
3617 x = y;
Tim Peters58141872002-08-06 22:25:02 +00003618 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003619#endif
3620 }
3621 else
Tim Peters58141872002-08-06 22:25:02 +00003622 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003623 "expected int/long, %s found",
3624 arg->ob_type->tp_name);
Guido van Rossumbb2cc692007-01-14 17:03:32 +00003625 return PyLong_FromUnsignedLong(htonl((unsigned long)x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003626}
3627
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003628PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003629"htonl(integer) -> integer\n\
3630\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003631Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003632
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003633/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003634
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003635PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003636"inet_aton(string) -> packed 32-bit IP representation\n\
3637\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003638Convert an IP address in string format (123.45.67.89) to the 32-bit packed\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003639binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003640
3641static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003642socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003643{
Guido van Rossuma2e48551999-09-09 15:42:59 +00003644#ifndef INADDR_NONE
3645#define INADDR_NONE (-1)
3646#endif
Neal Norwitz88f115b2003-02-13 02:15:42 +00003647#ifdef HAVE_INET_ATON
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003648 struct in_addr buf;
Ronald Oussorend06b6f22006-04-23 11:59:25 +00003649#endif
3650
3651#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
Tim Peters1df9fdd2003-02-13 03:13:40 +00003652 /* Have to use inet_addr() instead */
3653 unsigned long packed_addr;
3654#endif
3655 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003656
Tim Peters1df9fdd2003-02-13 03:13:40 +00003657 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003658 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003659
Tim Peters1df9fdd2003-02-13 03:13:40 +00003660
3661#ifdef HAVE_INET_ATON
Ronald Oussorend06b6f22006-04-23 11:59:25 +00003662
3663#ifdef USE_INET_ATON_WEAKLINK
3664 if (inet_aton != NULL) {
3665#endif
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003666 if (inet_aton(ip_addr, &buf))
3667 return PyString_FromStringAndSize((char *)(&buf),
3668 sizeof(buf));
3669
3670 PyErr_SetString(socket_error,
3671 "illegal IP address string passed to inet_aton");
3672 return NULL;
3673
Ronald Oussorend06b6f22006-04-23 11:59:25 +00003674#ifdef USE_INET_ATON_WEAKLINK
3675 } else {
3676#endif
3677
3678#endif
3679
3680#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
3681
Georg Brandld2e3ba72005-08-26 08:34:00 +00003682 /* special-case this address as inet_addr might return INADDR_NONE
3683 * for this */
3684 if (strcmp(ip_addr, "255.255.255.255") == 0) {
3685 packed_addr = 0xFFFFFFFF;
3686 } else {
Martin Blais2856e5f2006-05-26 12:03:27 +00003687
Georg Brandld2e3ba72005-08-26 08:34:00 +00003688 packed_addr = inet_addr(ip_addr);
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003689
Georg Brandld2e3ba72005-08-26 08:34:00 +00003690 if (packed_addr == INADDR_NONE) { /* invalid address */
3691 PyErr_SetString(socket_error,
3692 "illegal IP address string passed to inet_aton");
3693 return NULL;
3694 }
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003695 }
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003696 return PyString_FromStringAndSize((char *) &packed_addr,
3697 sizeof(packed_addr));
Ronald Oussorend06b6f22006-04-23 11:59:25 +00003698
3699#ifdef USE_INET_ATON_WEAKLINK
3700 }
3701#endif
3702
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003703#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003704}
3705
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003706PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00003707"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003708\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003709Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003710
3711static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003712socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003713{
3714 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003715 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003716 struct in_addr packed_addr;
3717
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003718 if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003719 return NULL;
3720 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00003721
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003722 if (addr_len != sizeof(packed_addr)) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003723 PyErr_SetString(socket_error,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003724 "packed IP wrong length for inet_ntoa");
3725 return NULL;
3726 }
3727
3728 memcpy(&packed_addr, packed_str, addr_len);
3729
3730 return PyString_FromString(inet_ntoa(packed_addr));
3731}
Guido van Rossum82a5c661998-07-07 20:45:43 +00003732
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003733#ifdef HAVE_INET_PTON
3734
3735PyDoc_STRVAR(inet_pton_doc,
3736"inet_pton(af, ip) -> packed IP address string\n\
3737\n\
3738Convert an IP address from string format to a packed string suitable\n\
3739for use with low-level network functions.");
3740
3741static PyObject *
3742socket_inet_pton(PyObject *self, PyObject *args)
3743{
3744 int af;
3745 char* ip;
3746 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003747#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003748 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003749#else
3750 char packed[sizeof(struct in_addr)];
3751#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003752 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
3753 return NULL;
3754 }
3755
Martin v. Löwis04697e82004-06-02 12:35:29 +00003756#if !defined(ENABLE_IPV6) && defined(AF_INET6)
Martin v. Löwis10649092003-08-05 06:25:06 +00003757 if(af == AF_INET6) {
3758 PyErr_SetString(socket_error,
3759 "can't use AF_INET6, IPv6 is disabled");
3760 return NULL;
3761 }
Martin Blais2856e5f2006-05-26 12:03:27 +00003762#endif
Martin v. Löwis10649092003-08-05 06:25:06 +00003763
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003764 retval = inet_pton(af, ip, packed);
3765 if (retval < 0) {
3766 PyErr_SetFromErrno(socket_error);
3767 return NULL;
3768 } else if (retval == 0) {
3769 PyErr_SetString(socket_error,
3770 "illegal IP address string passed to inet_pton");
3771 return NULL;
3772 } else if (af == AF_INET) {
3773 return PyString_FromStringAndSize(packed,
3774 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003775#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003776 } else if (af == AF_INET6) {
3777 return PyString_FromStringAndSize(packed,
3778 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003779#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003780 } else {
3781 PyErr_SetString(socket_error, "unknown address family");
3782 return NULL;
3783 }
3784}
Martin Blais2856e5f2006-05-26 12:03:27 +00003785
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003786PyDoc_STRVAR(inet_ntop_doc,
3787"inet_ntop(af, packed_ip) -> string formatted IP address\n\
3788\n\
3789Convert a packed IP address of the given family to string format.");
3790
3791static PyObject *
3792socket_inet_ntop(PyObject *self, PyObject *args)
3793{
3794 int af;
3795 char* packed;
3796 int len;
3797 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003798#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003799 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003800#else
3801 char ip[INET_ADDRSTRLEN + 1];
3802#endif
Martin Blais2856e5f2006-05-26 12:03:27 +00003803
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003804 /* Guarantee NUL-termination for PyString_FromString() below */
Jeremy Hylton80961f32004-11-07 14:24:25 +00003805 memset((void *) &ip[0], '\0', sizeof(ip));
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003806
3807 if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) {
3808 return NULL;
3809 }
3810
3811 if (af == AF_INET) {
3812 if (len != sizeof(struct in_addr)) {
3813 PyErr_SetString(PyExc_ValueError,
3814 "invalid length of packed IP address string");
3815 return NULL;
3816 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003817#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003818 } else if (af == AF_INET6) {
3819 if (len != sizeof(struct in6_addr)) {
3820 PyErr_SetString(PyExc_ValueError,
3821 "invalid length of packed IP address string");
3822 return NULL;
3823 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003824#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003825 } else {
3826 PyErr_Format(PyExc_ValueError,
3827 "unknown address family %d", af);
3828 return NULL;
3829 }
3830
3831 retval = inet_ntop(af, packed, ip, sizeof(ip));
3832 if (!retval) {
3833 PyErr_SetFromErrno(socket_error);
3834 return NULL;
3835 } else {
3836 return PyString_FromString(retval);
3837 }
3838
3839 /* NOTREACHED */
3840 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
3841 return NULL;
3842}
3843
3844#endif /* HAVE_INET_PTON */
3845
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003846/* Python interface to getaddrinfo(host, port). */
3847
3848/*ARGSUSED*/
3849static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003850socket_getaddrinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003851{
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003852 struct addrinfo hints, *res;
3853 struct addrinfo *res0 = NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003854 PyObject *hobj = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003855 PyObject *pobj = (PyObject *)NULL;
Martin v. Löwisf65b1a12001-08-12 09:28:40 +00003856 char pbuf[30];
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003857 char *hptr, *pptr;
3858 int family, socktype, protocol, flags;
3859 int error;
3860 PyObject *all = (PyObject *)NULL;
3861 PyObject *single = (PyObject *)NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003862 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003863
3864 family = socktype = protocol = flags = 0;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003865 family = AF_UNSPEC;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003866 if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
3867 &hobj, &pobj, &family, &socktype,
3868 &protocol, &flags)) {
3869 return NULL;
3870 }
3871 if (hobj == Py_None) {
3872 hptr = NULL;
3873 } else if (PyUnicode_Check(hobj)) {
3874 idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
3875 if (!idna)
3876 return NULL;
3877 hptr = PyString_AsString(idna);
3878 } else if (PyString_Check(hobj)) {
3879 hptr = PyString_AsString(hobj);
3880 } else {
Martin Blais2856e5f2006-05-26 12:03:27 +00003881 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2548c732003-04-18 10:39:54 +00003882 "getaddrinfo() argument 1 must be string or None");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003883 return NULL;
3884 }
3885 if (PyInt_Check(pobj)) {
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003886 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003887 pptr = pbuf;
3888 } else if (PyString_Check(pobj)) {
3889 pptr = PyString_AsString(pobj);
3890 } else if (pobj == Py_None) {
3891 pptr = (char *)NULL;
3892 } else {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003893 PyErr_SetString(socket_error, "Int or String expected");
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003894 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003895 }
3896 memset(&hints, 0, sizeof(hints));
3897 hints.ai_family = family;
3898 hints.ai_socktype = socktype;
3899 hints.ai_protocol = protocol;
3900 hints.ai_flags = flags;
Just van Rossum1040d2c2003-05-09 07:53:18 +00003901 Py_BEGIN_ALLOW_THREADS
3902 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003903 error = getaddrinfo(hptr, pptr, &hints, &res0);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003904 Py_END_ALLOW_THREADS
3905 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003906 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003907 set_gaierror(error);
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003908 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003909 }
3910
3911 if ((all = PyList_New(0)) == NULL)
3912 goto err;
3913 for (res = res0; res; res = res->ai_next) {
Guido van Rossum716aac02001-10-12 18:59:27 +00003914 PyObject *addr =
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00003915 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
Guido van Rossum716aac02001-10-12 18:59:27 +00003916 if (addr == NULL)
3917 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003918 single = Py_BuildValue("iiisO", res->ai_family,
3919 res->ai_socktype, res->ai_protocol,
3920 res->ai_canonname ? res->ai_canonname : "",
Guido van Rossum716aac02001-10-12 18:59:27 +00003921 addr);
3922 Py_DECREF(addr);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003923 if (single == NULL)
3924 goto err;
3925
3926 if (PyList_Append(all, single))
3927 goto err;
3928 Py_XDECREF(single);
3929 }
Martin v. Löwis2548c732003-04-18 10:39:54 +00003930 Py_XDECREF(idna);
Neal Norwitz90128ba2002-08-09 03:37:42 +00003931 if (res0)
3932 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003933 return all;
3934 err:
3935 Py_XDECREF(single);
3936 Py_XDECREF(all);
Martin v. Löwis2548c732003-04-18 10:39:54 +00003937 Py_XDECREF(idna);
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003938 if (res0)
3939 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003940 return (PyObject *)NULL;
3941}
3942
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003943PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003944"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
3945 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003946\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003947Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003948
3949/* Python interface to getnameinfo(sa, flags). */
3950
3951/*ARGSUSED*/
3952static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003953socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003954{
3955 PyObject *sa = (PyObject *)NULL;
3956 int flags;
3957 char *hostp;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003958 int port, flowinfo, scope_id;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003959 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
3960 struct addrinfo hints, *res = NULL;
3961 int error;
3962 PyObject *ret = (PyObject *)NULL;
3963
3964 flags = flowinfo = scope_id = 0;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003965 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003966 return NULL;
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003967 if (!PyArg_ParseTuple(sa, "si|ii",
3968 &hostp, &port, &flowinfo, &scope_id))
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003969 return NULL;
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003970 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003971 memset(&hints, 0, sizeof(hints));
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003972 hints.ai_family = AF_UNSPEC;
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003973 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Just van Rossum1040d2c2003-05-09 07:53:18 +00003974 Py_BEGIN_ALLOW_THREADS
3975 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003976 error = getaddrinfo(hostp, pbuf, &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003977 Py_END_ALLOW_THREADS
3978 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003979 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003980 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003981 goto fail;
3982 }
3983 if (res->ai_next) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003984 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003985 "sockaddr resolved to multiple addresses");
3986 goto fail;
3987 }
3988 switch (res->ai_family) {
3989 case AF_INET:
3990 {
3991 char *t1;
3992 int t2;
3993 if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003994 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003995 "IPv4 sockaddr must be 2 tuple");
3996 goto fail;
3997 }
3998 break;
3999 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00004000#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004001 case AF_INET6:
4002 {
4003 struct sockaddr_in6 *sin6;
4004 sin6 = (struct sockaddr_in6 *)res->ai_addr;
4005 sin6->sin6_flowinfo = flowinfo;
4006 sin6->sin6_scope_id = scope_id;
4007 break;
4008 }
4009#endif
4010 }
4011 error = getnameinfo(res->ai_addr, res->ai_addrlen,
4012 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
4013 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004014 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004015 goto fail;
4016 }
4017 ret = Py_BuildValue("ss", hbuf, pbuf);
4018
4019fail:
4020 if (res)
4021 freeaddrinfo(res);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004022 return ret;
4023}
4024
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004025PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004026"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004027\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004028Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004029
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004030
4031/* Python API to getting and setting the default timeout value. */
4032
4033static PyObject *
4034socket_getdefaulttimeout(PyObject *self)
4035{
4036 if (defaulttimeout < 0.0) {
4037 Py_INCREF(Py_None);
4038 return Py_None;
4039 }
4040 else
4041 return PyFloat_FromDouble(defaulttimeout);
4042}
4043
4044PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004045"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004046\n\
4047Returns the default timeout in floating seconds for new socket objects.\n\
4048A value of None indicates that new socket objects have no timeout.\n\
4049When the socket module is first imported, the default is None.");
4050
4051static PyObject *
4052socket_setdefaulttimeout(PyObject *self, PyObject *arg)
4053{
4054 double timeout;
4055
4056 if (arg == Py_None)
4057 timeout = -1.0;
4058 else {
4059 timeout = PyFloat_AsDouble(arg);
4060 if (timeout < 0.0) {
4061 if (!PyErr_Occurred())
4062 PyErr_SetString(PyExc_ValueError,
4063 "Timeout value out of range");
4064 return NULL;
4065 }
4066 }
4067
4068 defaulttimeout = timeout;
4069
4070 Py_INCREF(Py_None);
4071 return Py_None;
4072}
4073
4074PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004075"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004076\n\
4077Set the default timeout in floating seconds for new socket objects.\n\
4078A value of None indicates that new socket objects have no timeout.\n\
4079When the socket module is first imported, the default is None.");
4080
4081
Guido van Rossum30a685f1991-06-27 15:51:29 +00004082/* List of functions exported by this module. */
4083
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004084static PyMethodDef socket_methods[] = {
4085 {"gethostbyname", socket_gethostbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004086 METH_VARARGS, gethostbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004087 {"gethostbyname_ex", socket_gethostbyname_ex,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004088 METH_VARARGS, ghbn_ex_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004089 {"gethostbyaddr", socket_gethostbyaddr,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004090 METH_VARARGS, gethostbyaddr_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004091 {"gethostname", socket_gethostname,
Georg Brandl96a8c392006-05-29 21:04:52 +00004092 METH_NOARGS, gethostname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004093 {"getservbyname", socket_getservbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004094 METH_VARARGS, getservbyname_doc},
Barry Warsaw11b91a02004-06-28 00:50:43 +00004095 {"getservbyport", socket_getservbyport,
4096 METH_VARARGS, getservbyport_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004097 {"getprotobyname", socket_getprotobyname,
Georg Brandl96a8c392006-05-29 21:04:52 +00004098 METH_VARARGS, getprotobyname_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004099#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004100 {"fromfd", socket_fromfd,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004101 METH_VARARGS, fromfd_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004102#endif
Dave Cole331708b2004-08-09 04:51:41 +00004103#ifdef HAVE_SOCKETPAIR
4104 {"socketpair", socket_socketpair,
4105 METH_VARARGS, socketpair_doc},
4106#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004107 {"ntohs", socket_ntohs,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004108 METH_VARARGS, ntohs_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004109 {"ntohl", socket_ntohl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004110 METH_O, ntohl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004111 {"htons", socket_htons,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004112 METH_VARARGS, htons_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004113 {"htonl", socket_htonl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004114 METH_O, htonl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004115 {"inet_aton", socket_inet_aton,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004116 METH_VARARGS, inet_aton_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004117 {"inet_ntoa", socket_inet_ntoa,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004118 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004119#ifdef HAVE_INET_PTON
4120 {"inet_pton", socket_inet_pton,
4121 METH_VARARGS, inet_pton_doc},
4122 {"inet_ntop", socket_inet_ntop,
4123 METH_VARARGS, inet_ntop_doc},
4124#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004125 {"getaddrinfo", socket_getaddrinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004126 METH_VARARGS, getaddrinfo_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004127 {"getnameinfo", socket_getnameinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004128 METH_VARARGS, getnameinfo_doc},
Guido van Rossum1693ba82002-07-18 21:11:26 +00004129 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004130 METH_NOARGS, getdefaulttimeout_doc},
4131 {"setdefaulttimeout", socket_setdefaulttimeout,
4132 METH_O, setdefaulttimeout_doc},
Guido van Rossum30a685f1991-06-27 15:51:29 +00004133 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004134};
4135
Guido van Rossum30a685f1991-06-27 15:51:29 +00004136
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004137#ifdef RISCOS
4138#define OS_INIT_DEFINED
Guido van Rossumbe32c891996-06-20 16:25:29 +00004139
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004140static int
4141os_init(void)
4142{
4143 _kernel_swi_regs r;
4144
4145 r.r[0] = 0;
4146 _kernel_swi(0x43380, &r, &r);
4147 taskwindow = r.r[0];
4148
Martin v. Löwisa94568a2003-05-10 07:36:56 +00004149 return 1;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004150}
4151
4152#endif /* RISCOS */
4153
4154
4155#ifdef MS_WINDOWS
4156#define OS_INIT_DEFINED
4157
4158/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004159
4160static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004161os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00004162{
4163 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00004164}
4165
4166static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004167os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00004168{
4169 WSADATA WSAData;
4170 int ret;
4171 char buf[100];
4172 ret = WSAStartup(0x0101, &WSAData);
4173 switch (ret) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004174 case 0: /* No error */
Mark Hammond14350ab2003-07-09 04:57:46 +00004175 Py_AtExit(os_cleanup);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004176 return 1; /* Success */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004177 case WSASYSNOTREADY:
4178 PyErr_SetString(PyExc_ImportError,
4179 "WSAStartup failed: network not ready");
4180 break;
4181 case WSAVERNOTSUPPORTED:
4182 case WSAEINVAL:
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004183 PyErr_SetString(
4184 PyExc_ImportError,
4185 "WSAStartup failed: requested version not supported");
Guido van Rossumbe32c891996-06-20 16:25:29 +00004186 break;
4187 default:
Tim Peters885d4572001-11-28 20:27:42 +00004188 PyOS_snprintf(buf, sizeof(buf),
4189 "WSAStartup failed: error code %d", ret);
Guido van Rossumbe32c891996-06-20 16:25:29 +00004190 PyErr_SetString(PyExc_ImportError, buf);
4191 break;
4192 }
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004193 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004194}
4195
Guido van Rossum8d665e61996-06-26 18:22:49 +00004196#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004197
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004198
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004199#ifdef PYOS_OS2
4200#define OS_INIT_DEFINED
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004201
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004202/* Additional initialization for OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004203
4204static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004205os_init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004206{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004207#ifndef PYCC_GCC
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004208 char reason[64];
4209 int rc = sock_init();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004210
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004211 if (rc == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004212 return 1; /* Success */
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004213 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004214
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004215 PyOS_snprintf(reason, sizeof(reason),
4216 "OS/2 TCP/IP Error# %d", sock_errno());
4217 PyErr_SetString(PyExc_ImportError, reason);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004218
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004219 return 0; /* Failure */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00004220#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004221 /* No need to initialise sockets with GCC/EMX */
4222 return 1; /* Success */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00004223#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004224}
4225
4226#endif /* PYOS_OS2 */
4227
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004228
4229#ifndef OS_INIT_DEFINED
4230static int
4231os_init(void)
4232{
4233 return 1; /* Success */
4234}
4235#endif
4236
4237
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004238/* C API table - always add new things to the end for binary
4239 compatibility. */
4240static
4241PySocketModule_APIObject PySocketModuleAPI =
4242{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004243 &sock_type,
Brett Cannon06c34792004-03-23 23:16:54 +00004244 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004245};
4246
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004247
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004248/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004249
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004250 This module is actually called "_socket", and there's a wrapper
4251 "socket.py" which implements some additional functionality. On some
4252 platforms (e.g. Windows and OS/2), socket.py also implements a
4253 wrapper for the socket type that provides missing functionality such
4254 as makefile(), dup() and fromfd(). The import of "_socket" may fail
4255 with an ImportError exception if os-specific initialization fails.
4256 On Windows, this does WINSOCK initialization. When WINSOCK is
4257 initialized succesfully, a call to WSACleanup() is scheduled to be
4258 made at exit time.
4259*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004260
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004261PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004262"Implementation module for socket operations.\n\
4263\n\
4264See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004265
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004266PyMODINIT_FUNC
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004267init_socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004268{
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004269 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00004270
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004271 if (!os_init())
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004272 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004273
4274 sock_type.ob_type = &PyType_Type;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004275 m = Py_InitModule3(PySocket_MODULE_NAME,
4276 socket_methods,
4277 socket_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00004278 if (m == NULL)
4279 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004280
4281 socket_error = PyErr_NewException("socket.error", NULL, NULL);
4282 if (socket_error == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004283 return;
Brett Cannon06c34792004-03-23 23:16:54 +00004284 PySocketModuleAPI.error = socket_error;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004285 Py_INCREF(socket_error);
4286 PyModule_AddObject(m, "error", socket_error);
4287 socket_herror = PyErr_NewException("socket.herror",
4288 socket_error, NULL);
4289 if (socket_herror == NULL)
4290 return;
4291 Py_INCREF(socket_herror);
4292 PyModule_AddObject(m, "herror", socket_herror);
4293 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004294 NULL);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004295 if (socket_gaierror == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004296 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004297 Py_INCREF(socket_gaierror);
4298 PyModule_AddObject(m, "gaierror", socket_gaierror);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00004299 socket_timeout = PyErr_NewException("socket.timeout",
4300 socket_error, NULL);
4301 if (socket_timeout == NULL)
4302 return;
4303 Py_INCREF(socket_timeout);
4304 PyModule_AddObject(m, "timeout", socket_timeout);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004305 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00004306 if (PyModule_AddObject(m, "SocketType",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004307 (PyObject *)&sock_type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004308 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004309 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00004310 if (PyModule_AddObject(m, "socket",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004311 (PyObject *)&sock_type) != 0)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004312 return;
Guido van Rossum09be4091999-08-09 14:40:40 +00004313
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004314#ifdef ENABLE_IPV6
4315 has_ipv6 = Py_True;
4316#else
4317 has_ipv6 = Py_False;
4318#endif
4319 Py_INCREF(has_ipv6);
4320 PyModule_AddObject(m, "has_ipv6", has_ipv6);
4321
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004322 /* Export C API */
Fred Drake4baedc12002-04-01 14:53:37 +00004323 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004324 PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL)
4325 ) != 0)
4326 return;
4327
Guido van Rossum09be4091999-08-09 14:40:40 +00004328 /* Address families (we only support AF_INET and AF_UNIX) */
4329#ifdef AF_UNSPEC
Fred Drake4baedc12002-04-01 14:53:37 +00004330 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00004331#endif
Fred Drake4baedc12002-04-01 14:53:37 +00004332 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004333#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00004334 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004335#endif /* AF_INET6 */
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00004336#if defined(AF_UNIX)
Fred Drake4baedc12002-04-01 14:53:37 +00004337 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004338#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00004339#ifdef AF_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00004340 /* Amateur Radio AX.25 */
4341 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004342#endif
4343#ifdef AF_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00004344 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00004345#endif
4346#ifdef AF_APPLETALK
Fred Drake4baedc12002-04-01 14:53:37 +00004347 /* Appletalk DDP */
4348 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004349#endif
4350#ifdef AF_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00004351 /* Amateur radio NetROM */
4352 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004353#endif
4354#ifdef AF_BRIDGE
Fred Drake4baedc12002-04-01 14:53:37 +00004355 /* Multiprotocol bridge */
4356 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004357#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004358#ifdef AF_ATMPVC
4359 /* ATM PVCs */
4360 PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC);
4361#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004362#ifdef AF_AAL5
Fred Drake4baedc12002-04-01 14:53:37 +00004363 /* Reserved for Werner's ATM */
4364 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00004365#endif
4366#ifdef AF_X25
Fred Drake4baedc12002-04-01 14:53:37 +00004367 /* Reserved for X.25 project */
4368 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004369#endif
4370#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00004371 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00004372#endif
4373#ifdef AF_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00004374 /* Amateur Radio X.25 PLP */
4375 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004376#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004377#ifdef AF_DECnet
4378 /* Reserved for DECnet project */
4379 PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet);
4380#endif
4381#ifdef AF_NETBEUI
4382 /* Reserved for 802.2LLC project */
4383 PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI);
4384#endif
4385#ifdef AF_SECURITY
4386 /* Security callback pseudo AF */
4387 PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY);
4388#endif
4389#ifdef AF_KEY
4390 /* PF_KEY key management API */
4391 PyModule_AddIntConstant(m, "AF_KEY", AF_KEY);
4392#endif
4393#ifdef AF_NETLINK
4394 /* */
4395 PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);
Martin v. Löwis11017b12006-01-14 18:12:57 +00004396 PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE);
Martin v. Löwis5fe60e72006-04-06 22:29:33 +00004397#ifdef NETLINK_SKIP
Martin v. Löwis11017b12006-01-14 18:12:57 +00004398 PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP);
Martin v. Löwis5fe60e72006-04-06 22:29:33 +00004399#endif
4400#ifdef NETLINK_W1
4401 PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1);
4402#endif
Martin v. Löwis11017b12006-01-14 18:12:57 +00004403 PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK);
4404 PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004405#ifdef NETLINK_TCPDIAG
Martin v. Löwis11017b12006-01-14 18:12:57 +00004406 PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004407#endif
4408#ifdef NETLINK_NFLOG
Martin v. Löwis11017b12006-01-14 18:12:57 +00004409 PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004410#endif
Neal Norwitz65851662006-01-16 04:31:40 +00004411#ifdef NETLINK_XFRM
Martin v. Löwis11017b12006-01-14 18:12:57 +00004412 PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM);
Neal Norwitz65851662006-01-16 04:31:40 +00004413#endif
Martin v. Löwis5fe60e72006-04-06 22:29:33 +00004414#ifdef NETLINK_ARPD
Martin v. Löwis11017b12006-01-14 18:12:57 +00004415 PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD);
Martin v. Löwis5fe60e72006-04-06 22:29:33 +00004416#endif
4417#ifdef NETLINK_ROUTE6
Martin v. Löwis11017b12006-01-14 18:12:57 +00004418 PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6);
Martin v. Löwis5fe60e72006-04-06 22:29:33 +00004419#endif
Martin v. Löwis11017b12006-01-14 18:12:57 +00004420 PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW);
Martin v. Löwisb1cc1d42007-02-13 12:14:19 +00004421#ifdef NETLINK_DNRTMSG
Martin v. Löwis11017b12006-01-14 18:12:57 +00004422 PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG);
Martin v. Löwisb1cc1d42007-02-13 12:14:19 +00004423#endif
Martin v. Löwis5fe60e72006-04-06 22:29:33 +00004424#ifdef NETLINK_TAPBASE
Martin v. Löwis11017b12006-01-14 18:12:57 +00004425 PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE);
Martin v. Löwis5fe60e72006-04-06 22:29:33 +00004426#endif
Georg Brandldcfdae72006-04-01 07:33:08 +00004427#endif /* AF_NETLINK */
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004428#ifdef AF_ROUTE
4429 /* Alias to emulate 4.4BSD */
4430 PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE);
4431#endif
4432#ifdef AF_ASH
4433 /* Ash */
4434 PyModule_AddIntConstant(m, "AF_ASH", AF_ASH);
4435#endif
4436#ifdef AF_ECONET
4437 /* Acorn Econet */
4438 PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET);
4439#endif
4440#ifdef AF_ATMSVC
4441 /* ATM SVCs */
4442 PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC);
4443#endif
4444#ifdef AF_SNA
4445 /* Linux SNA Project (nutters!) */
4446 PyModule_AddIntConstant(m, "AF_SNA", AF_SNA);
4447#endif
4448#ifdef AF_IRDA
4449 /* IRDA sockets */
4450 PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA);
4451#endif
4452#ifdef AF_PPPOX
4453 /* PPPoX sockets */
4454 PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX);
4455#endif
4456#ifdef AF_WANPIPE
4457 /* Wanpipe API Sockets */
4458 PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE);
4459#endif
4460#ifdef AF_LLC
4461 /* Linux LLC */
4462 PyModule_AddIntConstant(m, "AF_LLC", AF_LLC);
4463#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00004464
Hye-Shik Chang81268602004-02-02 06:05:24 +00004465#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00004466 PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);
4467 PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);
Martin v. Löwis45423a72007-02-14 10:07:37 +00004468 PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI);
4469 PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI);
4470 PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP);
4471 PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR);
4472 PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER);
Hye-Shik Chang81268602004-02-02 06:05:24 +00004473#if !defined(__FreeBSD__)
Martin v. Löwis12af0482004-01-31 12:34:17 +00004474 PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00004475#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00004476 PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);
Neal Norwitz1e44ca92006-05-30 03:18:50 +00004477 PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
4478 PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
Martin v. Löwis12af0482004-01-31 12:34:17 +00004479#endif
4480
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00004481#ifdef HAVE_NETPACKET_PACKET_H
Fred Drake4baedc12002-04-01 14:53:37 +00004482 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
4483 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
4484 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
4485 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
4486 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
4487 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
4488 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
4489 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
4490 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00004491#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004492
4493 /* Socket types */
Fred Drake4baedc12002-04-01 14:53:37 +00004494 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
4495 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004496#ifndef __BEOS__
4497/* We have incomplete socket support. */
Fred Drake4baedc12002-04-01 14:53:37 +00004498 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
4499 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00004500#if defined(SOCK_RDM)
Fred Drake4baedc12002-04-01 14:53:37 +00004501 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004502#endif
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00004503#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004504
4505#ifdef SO_DEBUG
Fred Drake4baedc12002-04-01 14:53:37 +00004506 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004507#endif
4508#ifdef SO_ACCEPTCONN
Fred Drake4baedc12002-04-01 14:53:37 +00004509 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004510#endif
4511#ifdef SO_REUSEADDR
Fred Drake4baedc12002-04-01 14:53:37 +00004512 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004513#endif
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00004514#ifdef SO_EXCLUSIVEADDRUSE
4515 PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE);
4516#endif
4517
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004518#ifdef SO_KEEPALIVE
Fred Drake4baedc12002-04-01 14:53:37 +00004519 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004520#endif
4521#ifdef SO_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00004522 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004523#endif
4524#ifdef SO_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00004525 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004526#endif
4527#ifdef SO_USELOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00004528 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004529#endif
4530#ifdef SO_LINGER
Fred Drake4baedc12002-04-01 14:53:37 +00004531 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004532#endif
4533#ifdef SO_OOBINLINE
Fred Drake4baedc12002-04-01 14:53:37 +00004534 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004535#endif
4536#ifdef SO_REUSEPORT
Fred Drake4baedc12002-04-01 14:53:37 +00004537 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004538#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004539#ifdef SO_SNDBUF
Fred Drake4baedc12002-04-01 14:53:37 +00004540 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004541#endif
4542#ifdef SO_RCVBUF
Fred Drake4baedc12002-04-01 14:53:37 +00004543 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004544#endif
4545#ifdef SO_SNDLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00004546 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004547#endif
4548#ifdef SO_RCVLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00004549 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004550#endif
4551#ifdef SO_SNDTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00004552 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004553#endif
4554#ifdef SO_RCVTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00004555 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004556#endif
4557#ifdef SO_ERROR
Fred Drake4baedc12002-04-01 14:53:37 +00004558 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004559#endif
4560#ifdef SO_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004561 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004562#endif
4563
4564 /* Maximum number of connections for "listen" */
4565#ifdef SOMAXCONN
Fred Drake4baedc12002-04-01 14:53:37 +00004566 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004567#else
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004568 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004569#endif
4570
4571 /* Flags for send, recv */
4572#ifdef MSG_OOB
Fred Drake4baedc12002-04-01 14:53:37 +00004573 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004574#endif
4575#ifdef MSG_PEEK
Fred Drake4baedc12002-04-01 14:53:37 +00004576 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004577#endif
4578#ifdef MSG_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00004579 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004580#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00004581#ifdef MSG_DONTWAIT
Fred Drake4baedc12002-04-01 14:53:37 +00004582 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00004583#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004584#ifdef MSG_EOR
Fred Drake4baedc12002-04-01 14:53:37 +00004585 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004586#endif
4587#ifdef MSG_TRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00004588 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004589#endif
4590#ifdef MSG_CTRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00004591 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004592#endif
4593#ifdef MSG_WAITALL
Fred Drake4baedc12002-04-01 14:53:37 +00004594 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004595#endif
4596#ifdef MSG_BTAG
Fred Drake4baedc12002-04-01 14:53:37 +00004597 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004598#endif
4599#ifdef MSG_ETAG
Fred Drake4baedc12002-04-01 14:53:37 +00004600 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004601#endif
4602
4603 /* Protocol level and numbers, usable for [gs]etsockopt */
4604#ifdef SOL_SOCKET
Fred Drake4baedc12002-04-01 14:53:37 +00004605 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004606#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004607#ifdef SOL_IP
Fred Drake4baedc12002-04-01 14:53:37 +00004608 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004609#else
Fred Drake4baedc12002-04-01 14:53:37 +00004610 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00004611#endif
4612#ifdef SOL_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00004613 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00004614#endif
4615#ifdef SOL_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00004616 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004617#endif
4618#ifdef SOL_ATALK
Fred Drake4baedc12002-04-01 14:53:37 +00004619 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004620#endif
4621#ifdef SOL_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00004622 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004623#endif
4624#ifdef SOL_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00004625 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004626#endif
4627#ifdef SOL_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00004628 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004629#else
Fred Drake4baedc12002-04-01 14:53:37 +00004630 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00004631#endif
4632#ifdef SOL_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00004633 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004634#else
Fred Drake4baedc12002-04-01 14:53:37 +00004635 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00004636#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004637#ifdef IPPROTO_IP
Fred Drake4baedc12002-04-01 14:53:37 +00004638 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00004639#else
Fred Drake4baedc12002-04-01 14:53:37 +00004640 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004641#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004642#ifdef IPPROTO_HOPOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004643 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004644#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004645#ifdef IPPROTO_ICMP
Fred Drake4baedc12002-04-01 14:53:37 +00004646 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00004647#else
Fred Drake4baedc12002-04-01 14:53:37 +00004648 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004649#endif
4650#ifdef IPPROTO_IGMP
Fred Drake4baedc12002-04-01 14:53:37 +00004651 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004652#endif
4653#ifdef IPPROTO_GGP
Fred Drake4baedc12002-04-01 14:53:37 +00004654 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004655#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004656#ifdef IPPROTO_IPV4
Fred Drake4baedc12002-04-01 14:53:37 +00004657 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004658#endif
Martin v. Löwisa0f17342003-10-03 13:56:20 +00004659#ifdef IPPROTO_IPV6
4660 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
4661#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004662#ifdef IPPROTO_IPIP
Fred Drake4baedc12002-04-01 14:53:37 +00004663 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004664#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004665#ifdef IPPROTO_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00004666 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00004667#else
Fred Drake4baedc12002-04-01 14:53:37 +00004668 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004669#endif
4670#ifdef IPPROTO_EGP
Fred Drake4baedc12002-04-01 14:53:37 +00004671 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004672#endif
4673#ifdef IPPROTO_PUP
Fred Drake4baedc12002-04-01 14:53:37 +00004674 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004675#endif
4676#ifdef IPPROTO_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00004677 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00004678#else
Fred Drake4baedc12002-04-01 14:53:37 +00004679 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004680#endif
4681#ifdef IPPROTO_IDP
Fred Drake4baedc12002-04-01 14:53:37 +00004682 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004683#endif
4684#ifdef IPPROTO_HELLO
Fred Drake4baedc12002-04-01 14:53:37 +00004685 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004686#endif
4687#ifdef IPPROTO_ND
Fred Drake4baedc12002-04-01 14:53:37 +00004688 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004689#endif
4690#ifdef IPPROTO_TP
Fred Drake4baedc12002-04-01 14:53:37 +00004691 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004692#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004693#ifdef IPPROTO_IPV6
Fred Drake4baedc12002-04-01 14:53:37 +00004694 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004695#endif
4696#ifdef IPPROTO_ROUTING
Fred Drake4baedc12002-04-01 14:53:37 +00004697 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004698#endif
4699#ifdef IPPROTO_FRAGMENT
Fred Drake4baedc12002-04-01 14:53:37 +00004700 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004701#endif
4702#ifdef IPPROTO_RSVP
Fred Drake4baedc12002-04-01 14:53:37 +00004703 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004704#endif
4705#ifdef IPPROTO_GRE
Fred Drake4baedc12002-04-01 14:53:37 +00004706 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004707#endif
4708#ifdef IPPROTO_ESP
Fred Drake4baedc12002-04-01 14:53:37 +00004709 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004710#endif
4711#ifdef IPPROTO_AH
Fred Drake4baedc12002-04-01 14:53:37 +00004712 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004713#endif
4714#ifdef IPPROTO_MOBILE
Fred Drake4baedc12002-04-01 14:53:37 +00004715 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004716#endif
4717#ifdef IPPROTO_ICMPV6
Fred Drake4baedc12002-04-01 14:53:37 +00004718 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004719#endif
4720#ifdef IPPROTO_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00004721 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004722#endif
4723#ifdef IPPROTO_DSTOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004724 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004725#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004726#ifdef IPPROTO_XTP
Fred Drake4baedc12002-04-01 14:53:37 +00004727 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004728#endif
4729#ifdef IPPROTO_EON
Fred Drake4baedc12002-04-01 14:53:37 +00004730 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004731#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004732#ifdef IPPROTO_PIM
Fred Drake4baedc12002-04-01 14:53:37 +00004733 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004734#endif
4735#ifdef IPPROTO_IPCOMP
Fred Drake4baedc12002-04-01 14:53:37 +00004736 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004737#endif
4738#ifdef IPPROTO_VRRP
Fred Drake4baedc12002-04-01 14:53:37 +00004739 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004740#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004741#ifdef IPPROTO_BIP
Fred Drake4baedc12002-04-01 14:53:37 +00004742 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004743#endif
4744/**/
4745#ifdef IPPROTO_RAW
Fred Drake4baedc12002-04-01 14:53:37 +00004746 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00004747#else
Fred Drake4baedc12002-04-01 14:53:37 +00004748 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004749#endif
4750#ifdef IPPROTO_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00004751 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004752#endif
4753
4754 /* Some port configuration */
4755#ifdef IPPORT_RESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00004756 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004757#else
Fred Drake4baedc12002-04-01 14:53:37 +00004758 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004759#endif
4760#ifdef IPPORT_USERRESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00004761 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004762#else
Fred Drake4baedc12002-04-01 14:53:37 +00004763 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004764#endif
4765
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004766 /* Some reserved IP v.4 addresses */
4767#ifdef INADDR_ANY
Fred Drake4baedc12002-04-01 14:53:37 +00004768 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004769#else
Fred Drake4baedc12002-04-01 14:53:37 +00004770 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004771#endif
4772#ifdef INADDR_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00004773 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004774#else
Fred Drake4baedc12002-04-01 14:53:37 +00004775 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004776#endif
4777#ifdef INADDR_LOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00004778 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004779#else
Fred Drake4baedc12002-04-01 14:53:37 +00004780 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004781#endif
4782#ifdef INADDR_UNSPEC_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004783 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004784#else
Fred Drake4baedc12002-04-01 14:53:37 +00004785 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004786#endif
4787#ifdef INADDR_ALLHOSTS_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004788 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
4789 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004790#else
Fred Drake4baedc12002-04-01 14:53:37 +00004791 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004792#endif
4793#ifdef INADDR_MAX_LOCAL_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004794 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
4795 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004796#else
Fred Drake4baedc12002-04-01 14:53:37 +00004797 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004798#endif
4799#ifdef INADDR_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00004800 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004801#else
Fred Drake4baedc12002-04-01 14:53:37 +00004802 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004803#endif
4804
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004805 /* IPv4 [gs]etsockopt options */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004806#ifdef IP_OPTIONS
Fred Drake4baedc12002-04-01 14:53:37 +00004807 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004808#endif
4809#ifdef IP_HDRINCL
Fred Drake4baedc12002-04-01 14:53:37 +00004810 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004811#endif
4812#ifdef IP_TOS
Fred Drake4baedc12002-04-01 14:53:37 +00004813 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004814#endif
4815#ifdef IP_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004816 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004817#endif
4818#ifdef IP_RECVOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004819 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004820#endif
4821#ifdef IP_RECVRETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004822 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004823#endif
4824#ifdef IP_RECVDSTADDR
Fred Drake4baedc12002-04-01 14:53:37 +00004825 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004826#endif
4827#ifdef IP_RETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004828 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004829#endif
4830#ifdef IP_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00004831 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004832#endif
4833#ifdef IP_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004834 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004835#endif
4836#ifdef IP_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004837 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004838#endif
4839#ifdef IP_ADD_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00004840 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004841#endif
4842#ifdef IP_DROP_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00004843 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004844#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004845#ifdef IP_DEFAULT_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004846 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
4847 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00004848#endif
4849#ifdef IP_DEFAULT_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004850 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
4851 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004852#endif
4853#ifdef IP_MAX_MEMBERSHIPS
Fred Drake4baedc12002-04-01 14:53:37 +00004854 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00004855#endif
4856
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004857 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
4858#ifdef IPV6_JOIN_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004859 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004860#endif
4861#ifdef IPV6_LEAVE_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004862 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004863#endif
4864#ifdef IPV6_MULTICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00004865 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004866#endif
4867#ifdef IPV6_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00004868 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004869#endif
4870#ifdef IPV6_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004871 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004872#endif
4873#ifdef IPV6_UNICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00004874 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004875#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004876 /* Additional IPV6 socket options, defined in RFC 3493 */
4877#ifdef IPV6_V6ONLY
4878 PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);
4879#endif
4880 /* Advanced IPV6 socket options, from RFC 3542 */
4881#ifdef IPV6_CHECKSUM
4882 PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);
4883#endif
4884#ifdef IPV6_DONTFRAG
4885 PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);
4886#endif
4887#ifdef IPV6_DSTOPTS
4888 PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);
4889#endif
4890#ifdef IPV6_HOPLIMIT
4891 PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);
4892#endif
4893#ifdef IPV6_HOPOPTS
4894 PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);
4895#endif
4896#ifdef IPV6_NEXTHOP
4897 PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);
4898#endif
4899#ifdef IPV6_PATHMTU
4900 PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);
4901#endif
4902#ifdef IPV6_PKTINFO
4903 PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);
4904#endif
4905#ifdef IPV6_RECVDSTOPTS
4906 PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);
4907#endif
4908#ifdef IPV6_RECVHOPLIMIT
4909 PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);
4910#endif
4911#ifdef IPV6_RECVHOPOPTS
4912 PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);
4913#endif
4914#ifdef IPV6_RECVPKTINFO
4915 PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);
4916#endif
4917#ifdef IPV6_RECVRTHDR
4918 PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);
4919#endif
4920#ifdef IPV6_RECVTCLASS
4921 PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);
4922#endif
4923#ifdef IPV6_RTHDR
4924 PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);
4925#endif
4926#ifdef IPV6_RTHDRDSTOPTS
4927 PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);
4928#endif
4929#ifdef IPV6_RTHDR_TYPE_0
4930 PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);
4931#endif
4932#ifdef IPV6_RECVPATHMTU
4933 PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);
4934#endif
4935#ifdef IPV6_TCLASS
4936 PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);
4937#endif
4938#ifdef IPV6_USE_MIN_MTU
4939 PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);
4940#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004941
Guido van Rossum09be4091999-08-09 14:40:40 +00004942 /* TCP options */
4943#ifdef TCP_NODELAY
Fred Drake4baedc12002-04-01 14:53:37 +00004944 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00004945#endif
4946#ifdef TCP_MAXSEG
Fred Drake4baedc12002-04-01 14:53:37 +00004947 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00004948#endif
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004949#ifdef TCP_CORK
Fred Drake4baedc12002-04-01 14:53:37 +00004950 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004951#endif
4952#ifdef TCP_KEEPIDLE
Fred Drake4baedc12002-04-01 14:53:37 +00004953 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004954#endif
4955#ifdef TCP_KEEPINTVL
Fred Drake4baedc12002-04-01 14:53:37 +00004956 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004957#endif
4958#ifdef TCP_KEEPCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004959 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004960#endif
4961#ifdef TCP_SYNCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004962 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004963#endif
4964#ifdef TCP_LINGER2
Fred Drake4baedc12002-04-01 14:53:37 +00004965 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004966#endif
4967#ifdef TCP_DEFER_ACCEPT
Fred Drake4baedc12002-04-01 14:53:37 +00004968 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004969#endif
4970#ifdef TCP_WINDOW_CLAMP
Fred Drake4baedc12002-04-01 14:53:37 +00004971 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004972#endif
4973#ifdef TCP_INFO
Fred Drake4baedc12002-04-01 14:53:37 +00004974 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004975#endif
4976#ifdef TCP_QUICKACK
Fred Drake4baedc12002-04-01 14:53:37 +00004977 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004978#endif
4979
Guido van Rossum09be4091999-08-09 14:40:40 +00004980
4981 /* IPX options */
4982#ifdef IPX_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004983 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004984#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004985
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004986 /* get{addr,name}info parameters */
4987#ifdef EAI_ADDRFAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004988 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004989#endif
4990#ifdef EAI_AGAIN
Fred Drake4baedc12002-04-01 14:53:37 +00004991 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004992#endif
4993#ifdef EAI_BADFLAGS
Fred Drake4baedc12002-04-01 14:53:37 +00004994 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004995#endif
4996#ifdef EAI_FAIL
Fred Drake4baedc12002-04-01 14:53:37 +00004997 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004998#endif
4999#ifdef EAI_FAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00005000 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005001#endif
5002#ifdef EAI_MEMORY
Fred Drake4baedc12002-04-01 14:53:37 +00005003 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005004#endif
5005#ifdef EAI_NODATA
Fred Drake4baedc12002-04-01 14:53:37 +00005006 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005007#endif
5008#ifdef EAI_NONAME
Fred Drake4baedc12002-04-01 14:53:37 +00005009 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005010#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00005011#ifdef EAI_OVERFLOW
5012 PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);
5013#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005014#ifdef EAI_SERVICE
Fred Drake4baedc12002-04-01 14:53:37 +00005015 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005016#endif
5017#ifdef EAI_SOCKTYPE
Fred Drake4baedc12002-04-01 14:53:37 +00005018 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005019#endif
5020#ifdef EAI_SYSTEM
Fred Drake4baedc12002-04-01 14:53:37 +00005021 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005022#endif
5023#ifdef EAI_BADHINTS
Fred Drake4baedc12002-04-01 14:53:37 +00005024 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005025#endif
5026#ifdef EAI_PROTOCOL
Fred Drake4baedc12002-04-01 14:53:37 +00005027 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005028#endif
5029#ifdef EAI_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00005030 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005031#endif
5032#ifdef AI_PASSIVE
Fred Drake4baedc12002-04-01 14:53:37 +00005033 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005034#endif
5035#ifdef AI_CANONNAME
Fred Drake4baedc12002-04-01 14:53:37 +00005036 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005037#endif
5038#ifdef AI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00005039 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005040#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00005041#ifdef AI_NUMERICSERV
5042 PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);
5043#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005044#ifdef AI_MASK
Fred Drake4baedc12002-04-01 14:53:37 +00005045 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005046#endif
5047#ifdef AI_ALL
Fred Drake4baedc12002-04-01 14:53:37 +00005048 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005049#endif
5050#ifdef AI_V4MAPPED_CFG
Fred Drake4baedc12002-04-01 14:53:37 +00005051 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005052#endif
5053#ifdef AI_ADDRCONFIG
Fred Drake4baedc12002-04-01 14:53:37 +00005054 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005055#endif
5056#ifdef AI_V4MAPPED
Fred Drake4baedc12002-04-01 14:53:37 +00005057 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005058#endif
5059#ifdef AI_DEFAULT
Fred Drake4baedc12002-04-01 14:53:37 +00005060 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005061#endif
5062#ifdef NI_MAXHOST
Fred Drake4baedc12002-04-01 14:53:37 +00005063 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005064#endif
5065#ifdef NI_MAXSERV
Fred Drake4baedc12002-04-01 14:53:37 +00005066 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005067#endif
5068#ifdef NI_NOFQDN
Fred Drake4baedc12002-04-01 14:53:37 +00005069 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005070#endif
5071#ifdef NI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00005072 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005073#endif
5074#ifdef NI_NAMEREQD
Fred Drake4baedc12002-04-01 14:53:37 +00005075 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005076#endif
5077#ifdef NI_NUMERICSERV
Fred Drake4baedc12002-04-01 14:53:37 +00005078 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005079#endif
5080#ifdef NI_DGRAM
Fred Drake4baedc12002-04-01 14:53:37 +00005081 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005082#endif
5083
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005084 /* shutdown() parameters */
5085#ifdef SHUT_RD
5086 PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);
5087#elif defined(SD_RECEIVE)
5088 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
5089#else
5090 PyModule_AddIntConstant(m, "SHUT_RD", 0);
5091#endif
5092#ifdef SHUT_WR
5093 PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);
5094#elif defined(SD_SEND)
5095 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
5096#else
5097 PyModule_AddIntConstant(m, "SHUT_WR", 1);
5098#endif
5099#ifdef SHUT_RDWR
5100 PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);
5101#elif defined(SD_BOTH)
5102 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
5103#else
5104 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
5105#endif
5106
Guido van Rossum4f199ea1998-04-09 20:56:35 +00005107 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00005108#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
5109 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00005110#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005111}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005112
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005113
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005114#ifndef HAVE_INET_PTON
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005115
5116/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005117/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005118
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005119int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005120inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005121{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005122 if (af == AF_INET) {
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005123 long packed_addr;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005124 packed_addr = inet_addr(src);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005125 if (packed_addr == INADDR_NONE)
5126 return 0;
5127 memcpy(dst, &packed_addr, 4);
5128 return 1;
5129 }
5130 /* Should set errno to EAFNOSUPPORT */
5131 return -1;
5132}
5133
Martin v. Löwisc925b1532001-07-21 09:42:15 +00005134const char *
5135inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005136{
5137 if (af == AF_INET) {
5138 struct in_addr packed_addr;
5139 if (size < 16)
5140 /* Should set errno to ENOSPC. */
5141 return NULL;
5142 memcpy(&packed_addr, src, sizeof(packed_addr));
5143 return strncpy(dst, inet_ntoa(packed_addr), size);
5144 }
5145 /* Should set errno to EAFNOSUPPORT */
5146 return NULL;
5147}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005148
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005149#endif