blob: 96682ca86d3bbb1f418500f44527bf1c24b927fa [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 */
Martin Blaisaf2ae722006-06-04 13:49:49 +00002387 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recv", 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
Martin Blaisaf2ae722006-06-04 13:49:49 +00002562 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recvfrom", kwlist,
Martin Blais2856e5f2006-05-26 12:03:27 +00002563 &buf, &buflen, &recvlen, &flags))
2564 return NULL;
2565 assert(buf != 0 && buflen > 0);
2566
2567 if (recvlen < 0) {
2568 PyErr_SetString(PyExc_ValueError,
Facundo Batista1fe9f962007-03-28 03:45:20 +00002569 "negative buffersize in recvfrom_into");
Martin Blais2856e5f2006-05-26 12:03:27 +00002570 return NULL;
2571 }
2572 if (recvlen == 0) {
2573 /* If nbytes was not specified, use the buffer's length */
2574 recvlen = buflen;
2575 }
2576
2577 readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
2578 if (readlen < 0) {
2579 /* Return an error */
Martin Blaisaf2ae722006-06-04 13:49:49 +00002580 Py_XDECREF(addr);
2581 return NULL;
Martin Blais2856e5f2006-05-26 12:03:27 +00002582 }
2583
2584 /* Return the number of bytes read and the address. Note that we do
2585 not do anything special here in the case that readlen < recvlen. */
Neal Norwitzf0cab1f2006-06-08 05:12:45 +00002586 return Py_BuildValue("lN", readlen, addr);
Martin Blais2856e5f2006-05-26 12:03:27 +00002587}
2588
Martin Blaisaf2ae722006-06-04 13:49:49 +00002589PyDoc_STRVAR(recvfrom_into_doc,
2590"recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
Martin Blais2856e5f2006-05-26 12:03:27 +00002591\n\
Martin Blaisaf2ae722006-06-04 13:49:49 +00002592Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");
Martin Blais2856e5f2006-05-26 12:03:27 +00002593
2594
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002595/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002596
Guido van Rossum73624e91994-10-10 17:59:00 +00002597static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002598sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002599{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002600 char *buf;
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002601 int len, n = -1, flags = 0, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002602
Guido van Rossum43713e52000-02-29 13:59:29 +00002603 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002604 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002605
Neal Norwitz082b2df2006-02-07 07:04:46 +00002606 if (!IS_SELECTABLE(s))
2607 return select_error();
2608
Guido van Rossum73624e91994-10-10 17:59:00 +00002609 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002610 timeout = internal_select(s, 1);
2611 if (!timeout)
Neal Norwitz2a30cd02006-07-10 01:18:57 +00002612#ifdef __VMS
2613 n = sendsegmented(s->sock_fd, buf, len, flags);
2614#else
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002615 n = send(s->sock_fd, buf, len, flags);
Neal Norwitz2a30cd02006-07-10 01:18:57 +00002616#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00002617 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002618
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002619 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002620 PyErr_SetString(socket_timeout, "timed out");
2621 return NULL;
2622 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00002623 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002624 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002625 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00002626}
2627
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002628PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002629"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002630\n\
2631Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002632argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002633sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002634
2635
2636/* s.sendall(data [,flags]) method */
2637
2638static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002639sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002640{
2641 char *buf;
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002642 int len, n = -1, flags = 0, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002643
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002644 if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags))
2645 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002646
Neal Norwitz082b2df2006-02-07 07:04:46 +00002647 if (!IS_SELECTABLE(s))
2648 return select_error();
2649
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002650 Py_BEGIN_ALLOW_THREADS
2651 do {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002652 timeout = internal_select(s, 1);
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002653 n = -1;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002654 if (timeout)
2655 break;
Neal Norwitz2a30cd02006-07-10 01:18:57 +00002656#ifdef __VMS
2657 n = sendsegmented(s->sock_fd, buf, len, flags);
2658#else
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002659 n = send(s->sock_fd, buf, len, flags);
Neal Norwitz2a30cd02006-07-10 01:18:57 +00002660#endif
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002661 if (n < 0)
2662 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002663 buf += n;
2664 len -= n;
2665 } while (len > 0);
2666 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002667
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002668 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002669 PyErr_SetString(socket_timeout, "timed out");
2670 return NULL;
2671 }
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002672 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002673 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00002674
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002675 Py_INCREF(Py_None);
2676 return Py_None;
2677}
2678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002679PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002680"sendall(data[, flags])\n\
2681\n\
2682Send a data string to the socket. For the optional flags\n\
2683argument, see the Unix manual. This calls send() repeatedly\n\
2684until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002685to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002686
Guido van Rossum30a685f1991-06-27 15:51:29 +00002687
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002688/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002689
Guido van Rossum73624e91994-10-10 17:59:00 +00002690static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002691sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002692{
Guido van Rossum73624e91994-10-10 17:59:00 +00002693 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002694 char *buf;
Martin v. Löwis046c4d12006-12-03 11:23:45 +00002695 sock_addr_t addrbuf;
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002696 int addrlen, len, n = -1, flags, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002697
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002698 flags = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002699 if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00002700 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002701 if (!PyArg_ParseTuple(args, "s#iO:sendto",
2702 &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002703 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002704 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002705
Neal Norwitz082b2df2006-02-07 07:04:46 +00002706 if (!IS_SELECTABLE(s))
2707 return select_error();
2708
Martin v. Löwis046c4d12006-12-03 11:23:45 +00002709 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2710 return NULL;
2711
Guido van Rossum73624e91994-10-10 17:59:00 +00002712 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002713 timeout = internal_select(s, 1);
2714 if (!timeout)
Martin v. Löwis046c4d12006-12-03 11:23:45 +00002715 n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002716 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002717
Neal Norwitz9b0ca792006-08-02 06:46:21 +00002718 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002719 PyErr_SetString(socket_timeout, "timed out");
2720 return NULL;
2721 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002722 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002723 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002724 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002725}
2726
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002727PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002728"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002729\n\
2730Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002731For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002732
Guido van Rossum30a685f1991-06-27 15:51:29 +00002733
2734/* s.shutdown(how) method */
2735
Guido van Rossum73624e91994-10-10 17:59:00 +00002736static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002737sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002738{
2739 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002740 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002741
2742 how = PyInt_AsLong(arg);
2743 if (how == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002744 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002745 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002746 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00002747 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002748 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002749 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002750 Py_INCREF(Py_None);
2751 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002752}
2753
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002754PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002755"shutdown(flag)\n\
2756\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00002757Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
2758of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002759
Guido van Rossum30a685f1991-06-27 15:51:29 +00002760
2761/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002762
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002763static PyMethodDef sock_methods[] = {
Martin Blais2856e5f2006-05-26 12:03:27 +00002764 {"accept", (PyCFunction)sock_accept, METH_NOARGS,
2765 accept_doc},
2766 {"bind", (PyCFunction)sock_bind, METH_O,
2767 bind_doc},
2768 {"close", (PyCFunction)sock_close, METH_NOARGS,
2769 close_doc},
2770 {"connect", (PyCFunction)sock_connect, METH_O,
2771 connect_doc},
2772 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
2773 connect_ex_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002774#ifndef NO_DUP
Martin Blais2856e5f2006-05-26 12:03:27 +00002775 {"dup", (PyCFunction)sock_dup, METH_NOARGS,
2776 dup_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002777#endif
Martin Blais2856e5f2006-05-26 12:03:27 +00002778 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
2779 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002780#ifdef HAVE_GETPEERNAME
Martin Blais2856e5f2006-05-26 12:03:27 +00002781 {"getpeername", (PyCFunction)sock_getpeername,
2782 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00002783#endif
Martin Blais2856e5f2006-05-26 12:03:27 +00002784 {"getsockname", (PyCFunction)sock_getsockname,
2785 METH_NOARGS, getsockname_doc},
2786 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
2787 getsockopt_doc},
2788 {"listen", (PyCFunction)sock_listen, METH_O,
2789 listen_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002790#ifndef NO_DUP
Martin Blais2856e5f2006-05-26 12:03:27 +00002791 {"makefile", (PyCFunction)sock_makefile, METH_VARARGS,
2792 makefile_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002793#endif
Martin Blais2856e5f2006-05-26 12:03:27 +00002794 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
2795 recv_doc},
Martin Blaisaf2ae722006-06-04 13:49:49 +00002796 {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
2797 recv_into_doc},
Martin Blais2856e5f2006-05-26 12:03:27 +00002798 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
2799 recvfrom_doc},
Martin Blaisaf2ae722006-06-04 13:49:49 +00002800 {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
2801 recvfrom_into_doc},
Martin Blais2856e5f2006-05-26 12:03:27 +00002802 {"send", (PyCFunction)sock_send, METH_VARARGS,
2803 send_doc},
2804 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
2805 sendall_doc},
2806 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
2807 sendto_doc},
2808 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
2809 setblocking_doc},
2810 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
2811 settimeout_doc},
2812 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
2813 gettimeout_doc},
2814 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
2815 setsockopt_doc},
2816 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
2817 shutdown_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002818#ifdef RISCOS
Martin Blais2856e5f2006-05-26 12:03:27 +00002819 {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O,
2820 sleeptaskw_doc},
Guido van Rossum48a680c2001-03-02 06:34:14 +00002821#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00002822 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002823};
2824
Georg Brandlbc45a3f2006-03-17 19:17:34 +00002825/* SockObject members */
2826static PyMemberDef sock_memberlist[] = {
2827 {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},
2828 {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},
2829 {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},
2830 {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"},
2831 {0},
2832};
Guido van Rossum30a685f1991-06-27 15:51:29 +00002833
Guido van Rossum73624e91994-10-10 17:59:00 +00002834/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00002835 First close the file description. */
2836
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002837static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002838sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002839{
Guido van Rossumfa972c92000-04-10 12:45:45 +00002840 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00002841 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002842 s->ob_type->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002843}
2844
Guido van Rossum30a685f1991-06-27 15:51:29 +00002845
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002846static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002847sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002848{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002849 char buf[512];
Fred Drakea04eaad2000-06-30 02:46:07 +00002850#if SIZEOF_SOCKET_T > SIZEOF_LONG
2851 if (s->sock_fd > LONG_MAX) {
2852 /* this can occur on Win64, and actually there is a special
2853 ugly printf formatter for decimal pointer length integer
2854 printing, only bother if necessary*/
2855 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002856 "no printf formatter to display "
2857 "the socket descriptor in decimal");
Fred Drakea04eaad2000-06-30 02:46:07 +00002858 return NULL;
2859 }
2860#endif
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002861 PyOS_snprintf(
2862 buf, sizeof(buf),
2863 "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",
2864 (long)s->sock_fd, s->sock_family,
2865 s->sock_type,
2866 s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00002867 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002868}
2869
2870
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002871/* Create a new, uninitialized socket object. */
2872
2873static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002874sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002875{
2876 PyObject *new;
2877
2878 new = type->tp_alloc(type, 0);
Guido van Rossume1c478f2002-06-06 20:08:25 +00002879 if (new != NULL) {
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002880 ((PySocketSockObject *)new)->sock_fd = -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002881 ((PySocketSockObject *)new)->sock_timeout = -1.0;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002882 ((PySocketSockObject *)new)->errorhandler = &set_error;
Guido van Rossume1c478f2002-06-06 20:08:25 +00002883 }
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002884 return new;
2885}
2886
2887
2888/* Initialize a new socket object. */
2889
2890/*ARGSUSED*/
2891static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002892sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002893{
2894 PySocketSockObject *s = (PySocketSockObject *)self;
2895 SOCKET_T fd;
2896 int family = AF_INET, type = SOCK_STREAM, proto = 0;
Martin v. Löwis02cbf4a2006-02-27 17:20:04 +00002897 static char *keywords[] = {"family", "type", "proto", 0};
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002898
2899 if (!PyArg_ParseTupleAndKeywords(args, kwds,
2900 "|iii:socket", keywords,
2901 &family, &type, &proto))
2902 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002903
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002904 Py_BEGIN_ALLOW_THREADS
2905 fd = socket(family, type, proto);
2906 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002907
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002908#ifdef MS_WINDOWS
2909 if (fd == INVALID_SOCKET)
2910#else
2911 if (fd < 0)
2912#endif
2913 {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002914 set_error();
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002915 return -1;
2916 }
2917 init_sockobject(s, fd, family, type, proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002918
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002919 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002920
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002921}
2922
2923
Guido van Rossumb6775db1994-08-01 11:34:53 +00002924/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002925
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002926static PyTypeObject sock_type = {
Guido van Rossum55558541996-05-23 22:54:50 +00002927 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002928 0, /* ob_size */
Guido van Rossumca6dfa52001-10-28 12:31:33 +00002929 "_socket.socket", /* tp_name */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002930 sizeof(PySocketSockObject), /* tp_basicsize */
2931 0, /* tp_itemsize */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002932 (destructor)sock_dealloc, /* tp_dealloc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002933 0, /* tp_print */
2934 0, /* tp_getattr */
2935 0, /* tp_setattr */
2936 0, /* tp_compare */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002937 (reprfunc)sock_repr, /* tp_repr */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002938 0, /* tp_as_number */
2939 0, /* tp_as_sequence */
2940 0, /* tp_as_mapping */
2941 0, /* tp_hash */
2942 0, /* tp_call */
2943 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002944 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002945 0, /* tp_setattro */
2946 0, /* tp_as_buffer */
2947 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002948 sock_doc, /* tp_doc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002949 0, /* tp_traverse */
2950 0, /* tp_clear */
2951 0, /* tp_richcompare */
2952 0, /* tp_weaklistoffset */
2953 0, /* tp_iter */
2954 0, /* tp_iternext */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002955 sock_methods, /* tp_methods */
Georg Brandlbc45a3f2006-03-17 19:17:34 +00002956 sock_memberlist, /* tp_members */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002957 0, /* tp_getset */
2958 0, /* tp_base */
2959 0, /* tp_dict */
2960 0, /* tp_descr_get */
2961 0, /* tp_descr_set */
2962 0, /* tp_dictoffset */
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002963 sock_initobj, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002964 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002965 sock_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002966 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002967};
2968
Guido van Rossum30a685f1991-06-27 15:51:29 +00002969
Guido van Rossum81194471991-07-27 21:42:02 +00002970/* Python interface to gethostname(). */
2971
2972/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002973static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00002974socket_gethostname(PyObject *self, PyObject *unused)
Guido van Rossum81194471991-07-27 21:42:02 +00002975{
2976 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00002977 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00002978 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002979 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00002980 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002981 if (res < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002982 return set_error();
Guido van Rossum81194471991-07-27 21:42:02 +00002983 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00002984 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00002985}
Guido van Rossumff4949e1992-08-05 19:58:53 +00002986
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002987PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002988"gethostname() -> string\n\
2989\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002990Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002991
Guido van Rossumff4949e1992-08-05 19:58:53 +00002992
Guido van Rossum30a685f1991-06-27 15:51:29 +00002993/* Python interface to gethostbyname(name). */
2994
2995/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002996static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002997socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002998{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002999 char *name;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00003000 sock_addr_t addrbuf;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003001
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003002 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00003003 return NULL;
Martin v. Löwis046c4d12006-12-03 11:23:45 +00003004 if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00003005 return NULL;
Martin v. Löwis046c4d12006-12-03 11:23:45 +00003006 return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
Guido van Rossum30a685f1991-06-27 15:51:29 +00003007}
3008
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003009PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003010"gethostbyname(host) -> address\n\
3011\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003012Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003013
3014
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003015/* Convenience function common to gethostbyname_ex and gethostbyaddr */
3016
3017static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003018gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003019{
3020 char **pch;
3021 PyObject *rtn_tuple = (PyObject *)NULL;
3022 PyObject *name_list = (PyObject *)NULL;
3023 PyObject *addr_list = (PyObject *)NULL;
3024 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003025
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003026 if (h == NULL) {
Guido van Rossum48a680c2001-03-02 06:34:14 +00003027 /* Let's get real error message to return */
Guido van Rossume2ae77b2001-10-24 20:42:55 +00003028#ifndef RISCOS
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003029 set_herror(h_errno);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00003030#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003031 PyErr_SetString(socket_error, "host not found");
Guido van Rossume2ae77b2001-10-24 20:42:55 +00003032#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003033 return NULL;
3034 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003035
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003036 if (h->h_addrtype != af) {
3037#ifdef HAVE_STRERROR
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003038 /* Let's get real error message to return */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003039 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003040 (char *)strerror(EAFNOSUPPORT));
3041#else
3042 PyErr_SetString(
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003043 socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003044 "Address family not supported by protocol family");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003045#endif
3046 return NULL;
3047 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003048
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003049 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00003050
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003051 case AF_INET:
3052 if (alen < sizeof(struct sockaddr_in))
3053 return NULL;
3054 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003055
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003056#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003057 case AF_INET6:
3058 if (alen < sizeof(struct sockaddr_in6))
3059 return NULL;
3060 break;
3061#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00003062
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003063 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003064
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003065 if ((name_list = PyList_New(0)) == NULL)
3066 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003067
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003068 if ((addr_list = PyList_New(0)) == NULL)
3069 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003070
Georg Brandl9dc7b7c2006-08-14 22:10:24 +00003071 /* SF #1511317: h_aliases can be NULL */
3072 if (h->h_aliases) {
3073 for (pch = h->h_aliases; *pch != NULL; pch++) {
3074 int status;
3075 tmp = PyString_FromString(*pch);
3076 if (tmp == NULL)
3077 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003078
Georg Brandl9dc7b7c2006-08-14 22:10:24 +00003079 status = PyList_Append(name_list, tmp);
3080 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003081
Georg Brandl9dc7b7c2006-08-14 22:10:24 +00003082 if (status)
3083 goto err;
3084 }
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003085 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003086
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003087 for (pch = h->h_addr_list; *pch != NULL; pch++) {
3088 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003089
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003090 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00003091
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003092 case AF_INET:
3093 {
3094 struct sockaddr_in sin;
3095 memset(&sin, 0, sizeof(sin));
3096 sin.sin_family = af;
3097#ifdef HAVE_SOCKADDR_SA_LEN
3098 sin.sin_len = sizeof(sin);
3099#endif
3100 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
3101 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00003102
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003103 if (pch == h->h_addr_list && alen >= sizeof(sin))
3104 memcpy((char *) addr, &sin, sizeof(sin));
3105 break;
3106 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003107
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003108#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003109 case AF_INET6:
3110 {
3111 struct sockaddr_in6 sin6;
3112 memset(&sin6, 0, sizeof(sin6));
3113 sin6.sin6_family = af;
3114#ifdef HAVE_SOCKADDR_SA_LEN
3115 sin6.sin6_len = sizeof(sin6);
3116#endif
3117 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
3118 tmp = makeipaddr((struct sockaddr *)&sin6,
3119 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00003120
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003121 if (pch == h->h_addr_list && alen >= sizeof(sin6))
3122 memcpy((char *) addr, &sin6, sizeof(sin6));
3123 break;
3124 }
3125#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00003126
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003127 default: /* can't happen */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003128 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003129 "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003130 return NULL;
3131 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003132
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003133 if (tmp == NULL)
3134 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003135
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003136 status = PyList_Append(addr_list, tmp);
3137 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003138
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003139 if (status)
3140 goto err;
3141 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003142
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003143 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003144
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003145 err:
3146 Py_XDECREF(name_list);
3147 Py_XDECREF(addr_list);
3148 return rtn_tuple;
3149}
3150
3151
3152/* Python interface to gethostbyname_ex(name). */
3153
3154/*ARGSUSED*/
3155static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003156socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003157{
3158 char *name;
3159 struct hostent *h;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00003160#ifdef ENABLE_IPV6
3161 struct sockaddr_storage addr;
3162#else
3163 struct sockaddr_in addr;
3164#endif
Martin v. Löwis9db2f572001-07-23 01:30:10 +00003165 struct sockaddr *sa;
Guido van Rossum955becc1999-03-22 20:14:53 +00003166 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003167#ifdef HAVE_GETHOSTBYNAME_R
3168 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003169#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
3170 struct hostent_data data;
3171#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003172 char buf[16384];
3173 int buf_len = (sizeof buf) - 1;
3174 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003175#endif
3176#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003177 int result;
3178#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003179#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003180
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003181 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003182 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003183 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003184 return NULL;
3185 Py_BEGIN_ALLOW_THREADS
3186#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003187#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003188 result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
3189 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003190#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003191 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003192#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00003193 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003194 result = gethostbyname_r(name, &hp_allocated, &data);
3195 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003196#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003197#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003198#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00003199 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003200#endif
3201 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003202#endif /* HAVE_GETHOSTBYNAME_R */
3203 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00003204 /* Some C libraries would require addr.__ss_family instead of
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003205 addr.ss_family.
3206 Therefore, we cast the sockaddr_storage into sockaddr to
3207 access sa_family. */
Martin v. Löwis9db2f572001-07-23 01:30:10 +00003208 sa = (struct sockaddr*)&addr;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003209 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003210 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003211#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00003212 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00003213#endif
3214 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003215}
3216
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003217PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003218"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
3219\n\
3220Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003221for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003222
3223
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003224/* Python interface to gethostbyaddr(IP). */
3225
3226/*ARGSUSED*/
3227static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003228socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003229{
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003230#ifdef ENABLE_IPV6
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003231 struct sockaddr_storage addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003232#else
Guido van Rossum48a680c2001-03-02 06:34:14 +00003233 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003234#endif
3235 struct sockaddr *sa = (struct sockaddr *)&addr;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003236 char *ip_num;
3237 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00003238 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003239#ifdef HAVE_GETHOSTBYNAME_R
3240 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003241#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
3242 struct hostent_data data;
3243#else
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003244 char buf[16384];
3245 int buf_len = (sizeof buf) - 1;
3246 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003247#endif
3248#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003249 int result;
3250#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003251#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003252 char *ap;
3253 int al;
3254 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003255
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003256 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003257 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003258 af = AF_UNSPEC;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00003259 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003260 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003261 af = sa->sa_family;
3262 ap = NULL;
3263 al = 0;
3264 switch (af) {
3265 case AF_INET:
3266 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
3267 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3268 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003269#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003270 case AF_INET6:
3271 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
3272 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3273 break;
3274#endif
3275 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003276 PyErr_SetString(socket_error, "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003277 return NULL;
3278 }
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003279 Py_BEGIN_ALLOW_THREADS
3280#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003281#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003282 result = gethostbyaddr_r(ap, al, af,
3283 &hp_allocated, buf, buf_len,
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003284 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003285#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003286 h = gethostbyaddr_r(ap, al, af,
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003287 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003288#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00003289 memset((void *) &data, '\0', sizeof(data));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003290 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003291 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003292#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003293#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003294#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00003295 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003296#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003297 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003298#endif /* HAVE_GETHOSTBYNAME_R */
3299 Py_END_ALLOW_THREADS
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003300 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003301#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00003302 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003303#endif
3304 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003305}
3306
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003307PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003308"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
3309\n\
3310Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003311for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003312
Guido van Rossum30a685f1991-06-27 15:51:29 +00003313
3314/* Python interface to getservbyname(name).
3315 This only returns the port number, since the other info is already
3316 known or not useful (like the list of aliases). */
3317
3318/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00003319static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003320socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00003321{
Barry Warsaw11b91a02004-06-28 00:50:43 +00003322 char *name, *proto=NULL;
Guido van Rossum30a685f1991-06-27 15:51:29 +00003323 struct servent *sp;
Barry Warsaw11b91a02004-06-28 00:50:43 +00003324 if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00003325 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00003326 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00003327 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00003328 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00003329 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003330 PyErr_SetString(socket_error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00003331 return NULL;
3332 }
Guido van Rossum73624e91994-10-10 17:59:00 +00003333 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00003334}
3335
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003336PyDoc_STRVAR(getservbyname_doc,
Barry Warsaw11b91a02004-06-28 00:50:43 +00003337"getservbyname(servicename[, protocolname]) -> integer\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003338\n\
3339Return a port number from a service name and protocol name.\n\
Barry Warsaw11b91a02004-06-28 00:50:43 +00003340The optional protocol name, if given, should be 'tcp' or 'udp',\n\
3341otherwise any protocol will match.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003342
Guido van Rossum30a685f1991-06-27 15:51:29 +00003343
Barry Warsaw11b91a02004-06-28 00:50:43 +00003344/* Python interface to getservbyport(port).
3345 This only returns the service name, since the other info is already
3346 known or not useful (like the list of aliases). */
3347
3348/*ARGSUSED*/
3349static PyObject *
3350socket_getservbyport(PyObject *self, PyObject *args)
3351{
Raymond Hettinger87de0ca2004-09-28 02:19:40 +00003352 unsigned short port;
Barry Warsaw11b91a02004-06-28 00:50:43 +00003353 char *proto=NULL;
3354 struct servent *sp;
Raymond Hettinger87de0ca2004-09-28 02:19:40 +00003355 if (!PyArg_ParseTuple(args, "H|s:getservbyport", &port, &proto))
Barry Warsaw11b91a02004-06-28 00:50:43 +00003356 return NULL;
3357 Py_BEGIN_ALLOW_THREADS
3358 sp = getservbyport(htons(port), proto);
3359 Py_END_ALLOW_THREADS
3360 if (sp == NULL) {
3361 PyErr_SetString(socket_error, "port/proto not found");
3362 return NULL;
3363 }
3364 return PyString_FromString(sp->s_name);
3365}
3366
3367PyDoc_STRVAR(getservbyport_doc,
3368"getservbyport(port[, protocolname]) -> string\n\
3369\n\
3370Return the service name from a port number and protocol name.\n\
3371The optional protocol name, if given, should be 'tcp' or 'udp',\n\
3372otherwise any protocol will match.");
3373
Guido van Rossum3901d851996-12-19 16:35:04 +00003374/* Python interface to getprotobyname(name).
3375 This only returns the protocol number, since the other info is
3376 already known or not useful (like the list of aliases). */
3377
3378/*ARGSUSED*/
3379static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003380socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00003381{
3382 char *name;
3383 struct protoent *sp;
Guido van Rossumbcc20741998-08-04 22:53:56 +00003384#ifdef __BEOS__
3385/* Not available in BeOS yet. - [cjh] */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003386 PyErr_SetString(socket_error, "getprotobyname not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00003387 return NULL;
3388#else
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003389 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00003390 return NULL;
3391 Py_BEGIN_ALLOW_THREADS
3392 sp = getprotobyname(name);
3393 Py_END_ALLOW_THREADS
3394 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003395 PyErr_SetString(socket_error, "protocol not found");
Guido van Rossum3901d851996-12-19 16:35:04 +00003396 return NULL;
3397 }
3398 return PyInt_FromLong((long) sp->p_proto);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003399#endif
Guido van Rossum3901d851996-12-19 16:35:04 +00003400}
3401
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003402PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003403"getprotobyname(name) -> integer\n\
3404\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003405Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003406
Guido van Rossum3901d851996-12-19 16:35:04 +00003407
Dave Cole331708b2004-08-09 04:51:41 +00003408#ifdef HAVE_SOCKETPAIR
3409/* Create a pair of sockets using the socketpair() function.
Dave Cole07fda7e2004-08-23 05:16:23 +00003410 Arguments as for socket() except the default family is AF_UNIX if
Dave Colee8bbfe42004-08-26 00:51:16 +00003411 defined on the platform; otherwise, the default is AF_INET. */
Dave Cole331708b2004-08-09 04:51:41 +00003412
3413/*ARGSUSED*/
3414static PyObject *
3415socket_socketpair(PyObject *self, PyObject *args)
3416{
3417 PySocketSockObject *s0 = NULL, *s1 = NULL;
3418 SOCKET_T sv[2];
3419 int family, type = SOCK_STREAM, proto = 0;
3420 PyObject *res = NULL;
3421
3422#if defined(AF_UNIX)
3423 family = AF_UNIX;
3424#else
3425 family = AF_INET;
3426#endif
3427 if (!PyArg_ParseTuple(args, "|iii:socketpair",
3428 &family, &type, &proto))
3429 return NULL;
3430 /* Create a pair of socket fds */
3431 if (socketpair(family, type, proto, sv) < 0)
3432 return set_error();
Dave Cole331708b2004-08-09 04:51:41 +00003433 s0 = new_sockobject(sv[0], family, type, proto);
3434 if (s0 == NULL)
3435 goto finally;
3436 s1 = new_sockobject(sv[1], family, type, proto);
3437 if (s1 == NULL)
3438 goto finally;
3439 res = PyTuple_Pack(2, s0, s1);
3440
3441finally:
3442 if (res == NULL) {
3443 if (s0 == NULL)
3444 SOCKETCLOSE(sv[0]);
3445 if (s1 == NULL)
3446 SOCKETCLOSE(sv[1]);
3447 }
3448 Py_XDECREF(s0);
3449 Py_XDECREF(s1);
3450 return res;
3451}
3452
3453PyDoc_STRVAR(socketpair_doc,
3454"socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\
3455\n\
3456Create a pair of socket objects from the sockets returned by the platform\n\
3457socketpair() function.\n\
Dave Cole07fda7e2004-08-23 05:16:23 +00003458The arguments are the same as for socket() except the default family is\n\
Dave Colee8bbfe42004-08-26 00:51:16 +00003459AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
Dave Cole331708b2004-08-09 04:51:41 +00003460
3461#endif /* HAVE_SOCKETPAIR */
3462
3463
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003464#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00003465/* Create a socket object from a numeric file description.
3466 Useful e.g. if stdin is a socket.
3467 Additional arguments as for socket(). */
3468
3469/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00003470static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003471socket_fromfd(PyObject *self, PyObject *args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00003472{
Guido van Rossum73624e91994-10-10 17:59:00 +00003473 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00003474 SOCKET_T fd;
3475 int family, type, proto = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003476 if (!PyArg_ParseTuple(args, "iii|i:fromfd",
3477 &fd, &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00003478 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00003479 /* Dup the fd so it and the socket can be closed independently */
3480 fd = dup(fd);
3481 if (fd < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003482 return set_error();
3483 s = new_sockobject(fd, family, type, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00003484 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00003485}
Guido van Rossum82a5c661998-07-07 20:45:43 +00003486
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003487PyDoc_STRVAR(fromfd_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003488"fromfd(fd, family, type[, proto]) -> socket object\n\
3489\n\
Georg Brandldcfdae72006-04-01 07:33:08 +00003490Create a socket object from a duplicate of the given\n\
3491file descriptor.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003492The remaining arguments are the same as for socket().");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003493
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003494#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00003495
Guido van Rossum82a5c661998-07-07 20:45:43 +00003496
Guido van Rossum006bf911996-06-12 04:04:55 +00003497static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003498socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003499{
3500 int x1, x2;
3501
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003502 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00003503 return NULL;
3504 }
Guido van Rossumbb2cc692007-01-14 17:03:32 +00003505 if (x1 < 0) {
3506 PyErr_SetString(PyExc_OverflowError,
3507 "can't convert negative number to unsigned long");
3508 return NULL;
3509 }
3510 x2 = (unsigned int)ntohs((unsigned short)x1);
Guido van Rossum006bf911996-06-12 04:04:55 +00003511 return PyInt_FromLong(x2);
3512}
3513
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003514PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003515"ntohs(integer) -> integer\n\
3516\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003517Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003518
3519
Guido van Rossum006bf911996-06-12 04:04:55 +00003520static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003521socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003522{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003523 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003524
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003525 if (PyInt_Check(arg)) {
3526 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003527 if (x == (unsigned long) -1 && PyErr_Occurred())
3528 return NULL;
Guido van Rossumbb2cc692007-01-14 17:03:32 +00003529 if ((long)x < 0) {
3530 PyErr_SetString(PyExc_OverflowError,
3531 "can't convert negative number to unsigned long");
3532 return NULL;
3533 }
Guido van Rossum006bf911996-06-12 04:04:55 +00003534 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003535 else if (PyLong_Check(arg)) {
3536 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003537 if (x == (unsigned long) -1 && PyErr_Occurred())
3538 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003539#if SIZEOF_LONG > 4
3540 {
3541 unsigned long y;
3542 /* only want the trailing 32 bits */
3543 y = x & 0xFFFFFFFFUL;
3544 if (y ^ x)
3545 return PyErr_Format(PyExc_OverflowError,
3546 "long int larger than 32 bits");
3547 x = y;
Tim Peters58141872002-08-06 22:25:02 +00003548 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003549#endif
3550 }
3551 else
Tim Peters58141872002-08-06 22:25:02 +00003552 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003553 "expected int/long, %s found",
3554 arg->ob_type->tp_name);
3555 if (x == (unsigned long) -1 && PyErr_Occurred())
3556 return NULL;
Guido van Rossumbb2cc692007-01-14 17:03:32 +00003557 return PyLong_FromUnsignedLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003558}
3559
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003560PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003561"ntohl(integer) -> integer\n\
3562\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003563Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003564
3565
Guido van Rossum006bf911996-06-12 04:04:55 +00003566static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003567socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003568{
Hye-Shik Changa7b673f2003-12-17 09:40:23 +00003569 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00003570
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003571 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00003572 return NULL;
3573 }
Guido van Rossumbb2cc692007-01-14 17:03:32 +00003574 if (x1 < 0) {
3575 PyErr_SetString(PyExc_OverflowError,
3576 "can't convert negative number to unsigned long");
3577 return NULL;
3578 }
3579 x2 = (unsigned int)htons((unsigned short)x1);
Guido van Rossum006bf911996-06-12 04:04:55 +00003580 return PyInt_FromLong(x2);
3581}
3582
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003583PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003584"htons(integer) -> integer\n\
3585\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003586Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003587
3588
Guido van Rossum006bf911996-06-12 04:04:55 +00003589static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003590socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003591{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003592 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003593
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003594 if (PyInt_Check(arg)) {
3595 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003596 if (x == (unsigned long) -1 && PyErr_Occurred())
3597 return NULL;
Guido van Rossumbb2cc692007-01-14 17:03:32 +00003598 if ((long)x < 0) {
3599 PyErr_SetString(PyExc_OverflowError,
3600 "can't convert negative number to unsigned long");
3601 return NULL;
3602 }
Guido van Rossum006bf911996-06-12 04:04:55 +00003603 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003604 else if (PyLong_Check(arg)) {
3605 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003606 if (x == (unsigned long) -1 && PyErr_Occurred())
3607 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003608#if SIZEOF_LONG > 4
3609 {
3610 unsigned long y;
3611 /* only want the trailing 32 bits */
3612 y = x & 0xFFFFFFFFUL;
3613 if (y ^ x)
3614 return PyErr_Format(PyExc_OverflowError,
3615 "long int larger than 32 bits");
3616 x = y;
Tim Peters58141872002-08-06 22:25:02 +00003617 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003618#endif
3619 }
3620 else
Tim Peters58141872002-08-06 22:25:02 +00003621 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003622 "expected int/long, %s found",
3623 arg->ob_type->tp_name);
Guido van Rossumbb2cc692007-01-14 17:03:32 +00003624 return PyLong_FromUnsignedLong(htonl((unsigned long)x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003625}
3626
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003627PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003628"htonl(integer) -> integer\n\
3629\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003630Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003631
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003632/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003633
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003634PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003635"inet_aton(string) -> packed 32-bit IP representation\n\
3636\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003637Convert 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 +00003638binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003639
3640static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003641socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003642{
Guido van Rossuma2e48551999-09-09 15:42:59 +00003643#ifndef INADDR_NONE
3644#define INADDR_NONE (-1)
3645#endif
Neal Norwitz88f115b2003-02-13 02:15:42 +00003646#ifdef HAVE_INET_ATON
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003647 struct in_addr buf;
Ronald Oussorend06b6f22006-04-23 11:59:25 +00003648#endif
3649
3650#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
Tim Peters1df9fdd2003-02-13 03:13:40 +00003651 /* Have to use inet_addr() instead */
3652 unsigned long packed_addr;
3653#endif
3654 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003655
Tim Peters1df9fdd2003-02-13 03:13:40 +00003656 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003657 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003658
Tim Peters1df9fdd2003-02-13 03:13:40 +00003659
3660#ifdef HAVE_INET_ATON
Ronald Oussorend06b6f22006-04-23 11:59:25 +00003661
3662#ifdef USE_INET_ATON_WEAKLINK
3663 if (inet_aton != NULL) {
3664#endif
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003665 if (inet_aton(ip_addr, &buf))
3666 return PyString_FromStringAndSize((char *)(&buf),
3667 sizeof(buf));
3668
3669 PyErr_SetString(socket_error,
3670 "illegal IP address string passed to inet_aton");
3671 return NULL;
3672
Ronald Oussorend06b6f22006-04-23 11:59:25 +00003673#ifdef USE_INET_ATON_WEAKLINK
3674 } else {
3675#endif
3676
3677#endif
3678
3679#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
3680
Georg Brandld2e3ba72005-08-26 08:34:00 +00003681 /* special-case this address as inet_addr might return INADDR_NONE
3682 * for this */
3683 if (strcmp(ip_addr, "255.255.255.255") == 0) {
3684 packed_addr = 0xFFFFFFFF;
3685 } else {
Martin Blais2856e5f2006-05-26 12:03:27 +00003686
Georg Brandld2e3ba72005-08-26 08:34:00 +00003687 packed_addr = inet_addr(ip_addr);
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003688
Georg Brandld2e3ba72005-08-26 08:34:00 +00003689 if (packed_addr == INADDR_NONE) { /* invalid address */
3690 PyErr_SetString(socket_error,
3691 "illegal IP address string passed to inet_aton");
3692 return NULL;
3693 }
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003694 }
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003695 return PyString_FromStringAndSize((char *) &packed_addr,
3696 sizeof(packed_addr));
Ronald Oussorend06b6f22006-04-23 11:59:25 +00003697
3698#ifdef USE_INET_ATON_WEAKLINK
3699 }
3700#endif
3701
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003702#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003703}
3704
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003705PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00003706"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003707\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003708Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003709
3710static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003711socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003712{
3713 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003714 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003715 struct in_addr packed_addr;
3716
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003717 if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003718 return NULL;
3719 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00003720
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003721 if (addr_len != sizeof(packed_addr)) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003722 PyErr_SetString(socket_error,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003723 "packed IP wrong length for inet_ntoa");
3724 return NULL;
3725 }
3726
3727 memcpy(&packed_addr, packed_str, addr_len);
3728
3729 return PyString_FromString(inet_ntoa(packed_addr));
3730}
Guido van Rossum82a5c661998-07-07 20:45:43 +00003731
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003732#ifdef HAVE_INET_PTON
3733
3734PyDoc_STRVAR(inet_pton_doc,
3735"inet_pton(af, ip) -> packed IP address string\n\
3736\n\
3737Convert an IP address from string format to a packed string suitable\n\
3738for use with low-level network functions.");
3739
3740static PyObject *
3741socket_inet_pton(PyObject *self, PyObject *args)
3742{
3743 int af;
3744 char* ip;
3745 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003746#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003747 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003748#else
3749 char packed[sizeof(struct in_addr)];
3750#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003751 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
3752 return NULL;
3753 }
3754
Martin v. Löwis04697e82004-06-02 12:35:29 +00003755#if !defined(ENABLE_IPV6) && defined(AF_INET6)
Martin v. Löwis10649092003-08-05 06:25:06 +00003756 if(af == AF_INET6) {
3757 PyErr_SetString(socket_error,
3758 "can't use AF_INET6, IPv6 is disabled");
3759 return NULL;
3760 }
Martin Blais2856e5f2006-05-26 12:03:27 +00003761#endif
Martin v. Löwis10649092003-08-05 06:25:06 +00003762
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003763 retval = inet_pton(af, ip, packed);
3764 if (retval < 0) {
3765 PyErr_SetFromErrno(socket_error);
3766 return NULL;
3767 } else if (retval == 0) {
3768 PyErr_SetString(socket_error,
3769 "illegal IP address string passed to inet_pton");
3770 return NULL;
3771 } else if (af == AF_INET) {
3772 return PyString_FromStringAndSize(packed,
3773 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003774#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003775 } else if (af == AF_INET6) {
3776 return PyString_FromStringAndSize(packed,
3777 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003778#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003779 } else {
3780 PyErr_SetString(socket_error, "unknown address family");
3781 return NULL;
3782 }
3783}
Martin Blais2856e5f2006-05-26 12:03:27 +00003784
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003785PyDoc_STRVAR(inet_ntop_doc,
3786"inet_ntop(af, packed_ip) -> string formatted IP address\n\
3787\n\
3788Convert a packed IP address of the given family to string format.");
3789
3790static PyObject *
3791socket_inet_ntop(PyObject *self, PyObject *args)
3792{
3793 int af;
3794 char* packed;
3795 int len;
3796 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003797#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003798 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003799#else
3800 char ip[INET_ADDRSTRLEN + 1];
3801#endif
Martin Blais2856e5f2006-05-26 12:03:27 +00003802
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003803 /* Guarantee NUL-termination for PyString_FromString() below */
Jeremy Hylton80961f32004-11-07 14:24:25 +00003804 memset((void *) &ip[0], '\0', sizeof(ip));
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003805
3806 if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) {
3807 return NULL;
3808 }
3809
3810 if (af == AF_INET) {
3811 if (len != sizeof(struct in_addr)) {
3812 PyErr_SetString(PyExc_ValueError,
3813 "invalid length of packed IP address string");
3814 return NULL;
3815 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003816#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003817 } else if (af == AF_INET6) {
3818 if (len != sizeof(struct in6_addr)) {
3819 PyErr_SetString(PyExc_ValueError,
3820 "invalid length of packed IP address string");
3821 return NULL;
3822 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003823#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003824 } else {
3825 PyErr_Format(PyExc_ValueError,
3826 "unknown address family %d", af);
3827 return NULL;
3828 }
3829
3830 retval = inet_ntop(af, packed, ip, sizeof(ip));
3831 if (!retval) {
3832 PyErr_SetFromErrno(socket_error);
3833 return NULL;
3834 } else {
3835 return PyString_FromString(retval);
3836 }
3837
3838 /* NOTREACHED */
3839 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
3840 return NULL;
3841}
3842
3843#endif /* HAVE_INET_PTON */
3844
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003845/* Python interface to getaddrinfo(host, port). */
3846
3847/*ARGSUSED*/
3848static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003849socket_getaddrinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003850{
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003851 struct addrinfo hints, *res;
3852 struct addrinfo *res0 = NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003853 PyObject *hobj = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003854 PyObject *pobj = (PyObject *)NULL;
Martin v. Löwisf65b1a12001-08-12 09:28:40 +00003855 char pbuf[30];
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003856 char *hptr, *pptr;
3857 int family, socktype, protocol, flags;
3858 int error;
3859 PyObject *all = (PyObject *)NULL;
3860 PyObject *single = (PyObject *)NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003861 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003862
3863 family = socktype = protocol = flags = 0;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003864 family = AF_UNSPEC;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003865 if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
3866 &hobj, &pobj, &family, &socktype,
3867 &protocol, &flags)) {
3868 return NULL;
3869 }
3870 if (hobj == Py_None) {
3871 hptr = NULL;
3872 } else if (PyUnicode_Check(hobj)) {
3873 idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
3874 if (!idna)
3875 return NULL;
3876 hptr = PyString_AsString(idna);
3877 } else if (PyString_Check(hobj)) {
3878 hptr = PyString_AsString(hobj);
3879 } else {
Martin Blais2856e5f2006-05-26 12:03:27 +00003880 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2548c732003-04-18 10:39:54 +00003881 "getaddrinfo() argument 1 must be string or None");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003882 return NULL;
3883 }
3884 if (PyInt_Check(pobj)) {
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003885 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003886 pptr = pbuf;
3887 } else if (PyString_Check(pobj)) {
3888 pptr = PyString_AsString(pobj);
3889 } else if (pobj == Py_None) {
3890 pptr = (char *)NULL;
3891 } else {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003892 PyErr_SetString(socket_error, "Int or String expected");
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003893 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003894 }
3895 memset(&hints, 0, sizeof(hints));
3896 hints.ai_family = family;
3897 hints.ai_socktype = socktype;
3898 hints.ai_protocol = protocol;
3899 hints.ai_flags = flags;
Just van Rossum1040d2c2003-05-09 07:53:18 +00003900 Py_BEGIN_ALLOW_THREADS
3901 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003902 error = getaddrinfo(hptr, pptr, &hints, &res0);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003903 Py_END_ALLOW_THREADS
3904 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003905 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003906 set_gaierror(error);
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003907 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003908 }
3909
3910 if ((all = PyList_New(0)) == NULL)
3911 goto err;
3912 for (res = res0; res; res = res->ai_next) {
Guido van Rossum716aac02001-10-12 18:59:27 +00003913 PyObject *addr =
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00003914 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
Guido van Rossum716aac02001-10-12 18:59:27 +00003915 if (addr == NULL)
3916 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003917 single = Py_BuildValue("iiisO", res->ai_family,
3918 res->ai_socktype, res->ai_protocol,
3919 res->ai_canonname ? res->ai_canonname : "",
Guido van Rossum716aac02001-10-12 18:59:27 +00003920 addr);
3921 Py_DECREF(addr);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003922 if (single == NULL)
3923 goto err;
3924
3925 if (PyList_Append(all, single))
3926 goto err;
3927 Py_XDECREF(single);
3928 }
Martin v. Löwis2548c732003-04-18 10:39:54 +00003929 Py_XDECREF(idna);
Neal Norwitz90128ba2002-08-09 03:37:42 +00003930 if (res0)
3931 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003932 return all;
3933 err:
3934 Py_XDECREF(single);
3935 Py_XDECREF(all);
Martin v. Löwis2548c732003-04-18 10:39:54 +00003936 Py_XDECREF(idna);
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003937 if (res0)
3938 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003939 return (PyObject *)NULL;
3940}
3941
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003942PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003943"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
3944 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003945\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003946Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003947
3948/* Python interface to getnameinfo(sa, flags). */
3949
3950/*ARGSUSED*/
3951static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003952socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003953{
3954 PyObject *sa = (PyObject *)NULL;
3955 int flags;
3956 char *hostp;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003957 int port, flowinfo, scope_id;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003958 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
3959 struct addrinfo hints, *res = NULL;
3960 int error;
3961 PyObject *ret = (PyObject *)NULL;
3962
3963 flags = flowinfo = scope_id = 0;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003964 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003965 return NULL;
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003966 if (!PyArg_ParseTuple(sa, "si|ii",
3967 &hostp, &port, &flowinfo, &scope_id))
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003968 return NULL;
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003969 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003970 memset(&hints, 0, sizeof(hints));
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003971 hints.ai_family = AF_UNSPEC;
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003972 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Just van Rossum1040d2c2003-05-09 07:53:18 +00003973 Py_BEGIN_ALLOW_THREADS
3974 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003975 error = getaddrinfo(hostp, pbuf, &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003976 Py_END_ALLOW_THREADS
3977 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003978 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003979 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003980 goto fail;
3981 }
3982 if (res->ai_next) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003983 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003984 "sockaddr resolved to multiple addresses");
3985 goto fail;
3986 }
3987 switch (res->ai_family) {
3988 case AF_INET:
3989 {
3990 char *t1;
3991 int t2;
3992 if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003993 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003994 "IPv4 sockaddr must be 2 tuple");
3995 goto fail;
3996 }
3997 break;
3998 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003999#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004000 case AF_INET6:
4001 {
4002 struct sockaddr_in6 *sin6;
4003 sin6 = (struct sockaddr_in6 *)res->ai_addr;
4004 sin6->sin6_flowinfo = flowinfo;
4005 sin6->sin6_scope_id = scope_id;
4006 break;
4007 }
4008#endif
4009 }
4010 error = getnameinfo(res->ai_addr, res->ai_addrlen,
4011 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
4012 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004013 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004014 goto fail;
4015 }
4016 ret = Py_BuildValue("ss", hbuf, pbuf);
4017
4018fail:
4019 if (res)
4020 freeaddrinfo(res);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004021 return ret;
4022}
4023
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004024PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004025"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004026\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004027Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004028
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004029
4030/* Python API to getting and setting the default timeout value. */
4031
4032static PyObject *
4033socket_getdefaulttimeout(PyObject *self)
4034{
4035 if (defaulttimeout < 0.0) {
4036 Py_INCREF(Py_None);
4037 return Py_None;
4038 }
4039 else
4040 return PyFloat_FromDouble(defaulttimeout);
4041}
4042
4043PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004044"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004045\n\
4046Returns the default timeout in floating seconds for new socket objects.\n\
4047A value of None indicates that new socket objects have no timeout.\n\
4048When the socket module is first imported, the default is None.");
4049
4050static PyObject *
4051socket_setdefaulttimeout(PyObject *self, PyObject *arg)
4052{
4053 double timeout;
4054
4055 if (arg == Py_None)
4056 timeout = -1.0;
4057 else {
4058 timeout = PyFloat_AsDouble(arg);
4059 if (timeout < 0.0) {
4060 if (!PyErr_Occurred())
4061 PyErr_SetString(PyExc_ValueError,
4062 "Timeout value out of range");
4063 return NULL;
4064 }
4065 }
4066
4067 defaulttimeout = timeout;
4068
4069 Py_INCREF(Py_None);
4070 return Py_None;
4071}
4072
4073PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004074"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004075\n\
4076Set the default timeout in floating seconds for new socket objects.\n\
4077A value of None indicates that new socket objects have no timeout.\n\
4078When the socket module is first imported, the default is None.");
4079
4080
Guido van Rossum30a685f1991-06-27 15:51:29 +00004081/* List of functions exported by this module. */
4082
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004083static PyMethodDef socket_methods[] = {
4084 {"gethostbyname", socket_gethostbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004085 METH_VARARGS, gethostbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004086 {"gethostbyname_ex", socket_gethostbyname_ex,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004087 METH_VARARGS, ghbn_ex_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004088 {"gethostbyaddr", socket_gethostbyaddr,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004089 METH_VARARGS, gethostbyaddr_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004090 {"gethostname", socket_gethostname,
Georg Brandl96a8c392006-05-29 21:04:52 +00004091 METH_NOARGS, gethostname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004092 {"getservbyname", socket_getservbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004093 METH_VARARGS, getservbyname_doc},
Barry Warsaw11b91a02004-06-28 00:50:43 +00004094 {"getservbyport", socket_getservbyport,
4095 METH_VARARGS, getservbyport_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004096 {"getprotobyname", socket_getprotobyname,
Georg Brandl96a8c392006-05-29 21:04:52 +00004097 METH_VARARGS, getprotobyname_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004098#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004099 {"fromfd", socket_fromfd,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004100 METH_VARARGS, fromfd_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004101#endif
Dave Cole331708b2004-08-09 04:51:41 +00004102#ifdef HAVE_SOCKETPAIR
4103 {"socketpair", socket_socketpair,
4104 METH_VARARGS, socketpair_doc},
4105#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004106 {"ntohs", socket_ntohs,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004107 METH_VARARGS, ntohs_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004108 {"ntohl", socket_ntohl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004109 METH_O, ntohl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004110 {"htons", socket_htons,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004111 METH_VARARGS, htons_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004112 {"htonl", socket_htonl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004113 METH_O, htonl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004114 {"inet_aton", socket_inet_aton,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004115 METH_VARARGS, inet_aton_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004116 {"inet_ntoa", socket_inet_ntoa,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004117 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004118#ifdef HAVE_INET_PTON
4119 {"inet_pton", socket_inet_pton,
4120 METH_VARARGS, inet_pton_doc},
4121 {"inet_ntop", socket_inet_ntop,
4122 METH_VARARGS, inet_ntop_doc},
4123#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004124 {"getaddrinfo", socket_getaddrinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004125 METH_VARARGS, getaddrinfo_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004126 {"getnameinfo", socket_getnameinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004127 METH_VARARGS, getnameinfo_doc},
Guido van Rossum1693ba82002-07-18 21:11:26 +00004128 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004129 METH_NOARGS, getdefaulttimeout_doc},
4130 {"setdefaulttimeout", socket_setdefaulttimeout,
4131 METH_O, setdefaulttimeout_doc},
Guido van Rossum30a685f1991-06-27 15:51:29 +00004132 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004133};
4134
Guido van Rossum30a685f1991-06-27 15:51:29 +00004135
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004136#ifdef RISCOS
4137#define OS_INIT_DEFINED
Guido van Rossumbe32c891996-06-20 16:25:29 +00004138
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004139static int
4140os_init(void)
4141{
4142 _kernel_swi_regs r;
4143
4144 r.r[0] = 0;
4145 _kernel_swi(0x43380, &r, &r);
4146 taskwindow = r.r[0];
4147
Martin v. Löwisa94568a2003-05-10 07:36:56 +00004148 return 1;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004149}
4150
4151#endif /* RISCOS */
4152
4153
4154#ifdef MS_WINDOWS
4155#define OS_INIT_DEFINED
4156
4157/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004158
4159static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004160os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00004161{
4162 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00004163}
4164
4165static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004166os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00004167{
4168 WSADATA WSAData;
4169 int ret;
4170 char buf[100];
4171 ret = WSAStartup(0x0101, &WSAData);
4172 switch (ret) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004173 case 0: /* No error */
Mark Hammond14350ab2003-07-09 04:57:46 +00004174 Py_AtExit(os_cleanup);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004175 return 1; /* Success */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004176 case WSASYSNOTREADY:
4177 PyErr_SetString(PyExc_ImportError,
4178 "WSAStartup failed: network not ready");
4179 break;
4180 case WSAVERNOTSUPPORTED:
4181 case WSAEINVAL:
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004182 PyErr_SetString(
4183 PyExc_ImportError,
4184 "WSAStartup failed: requested version not supported");
Guido van Rossumbe32c891996-06-20 16:25:29 +00004185 break;
4186 default:
Tim Peters885d4572001-11-28 20:27:42 +00004187 PyOS_snprintf(buf, sizeof(buf),
4188 "WSAStartup failed: error code %d", ret);
Guido van Rossumbe32c891996-06-20 16:25:29 +00004189 PyErr_SetString(PyExc_ImportError, buf);
4190 break;
4191 }
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004192 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004193}
4194
Guido van Rossum8d665e61996-06-26 18:22:49 +00004195#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004196
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004197
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004198#ifdef PYOS_OS2
4199#define OS_INIT_DEFINED
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004200
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004201/* Additional initialization for OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004202
4203static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004204os_init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004205{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004206#ifndef PYCC_GCC
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004207 char reason[64];
4208 int rc = sock_init();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004209
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004210 if (rc == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004211 return 1; /* Success */
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004212 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004213
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004214 PyOS_snprintf(reason, sizeof(reason),
4215 "OS/2 TCP/IP Error# %d", sock_errno());
4216 PyErr_SetString(PyExc_ImportError, reason);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004217
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004218 return 0; /* Failure */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00004219#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004220 /* No need to initialise sockets with GCC/EMX */
4221 return 1; /* Success */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00004222#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004223}
4224
4225#endif /* PYOS_OS2 */
4226
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004227
4228#ifndef OS_INIT_DEFINED
4229static int
4230os_init(void)
4231{
4232 return 1; /* Success */
4233}
4234#endif
4235
4236
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004237/* C API table - always add new things to the end for binary
4238 compatibility. */
4239static
4240PySocketModule_APIObject PySocketModuleAPI =
4241{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004242 &sock_type,
Brett Cannon06c34792004-03-23 23:16:54 +00004243 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004244};
4245
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004246
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004247/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004248
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004249 This module is actually called "_socket", and there's a wrapper
4250 "socket.py" which implements some additional functionality. On some
4251 platforms (e.g. Windows and OS/2), socket.py also implements a
4252 wrapper for the socket type that provides missing functionality such
4253 as makefile(), dup() and fromfd(). The import of "_socket" may fail
4254 with an ImportError exception if os-specific initialization fails.
4255 On Windows, this does WINSOCK initialization. When WINSOCK is
4256 initialized succesfully, a call to WSACleanup() is scheduled to be
4257 made at exit time.
4258*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004259
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004260PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004261"Implementation module for socket operations.\n\
4262\n\
4263See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004264
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004265PyMODINIT_FUNC
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004266init_socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004267{
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004268 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00004269
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004270 if (!os_init())
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004271 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004272
4273 sock_type.ob_type = &PyType_Type;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004274 m = Py_InitModule3(PySocket_MODULE_NAME,
4275 socket_methods,
4276 socket_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00004277 if (m == NULL)
4278 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004279
4280 socket_error = PyErr_NewException("socket.error", NULL, NULL);
4281 if (socket_error == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004282 return;
Brett Cannon06c34792004-03-23 23:16:54 +00004283 PySocketModuleAPI.error = socket_error;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004284 Py_INCREF(socket_error);
4285 PyModule_AddObject(m, "error", socket_error);
4286 socket_herror = PyErr_NewException("socket.herror",
4287 socket_error, NULL);
4288 if (socket_herror == NULL)
4289 return;
4290 Py_INCREF(socket_herror);
4291 PyModule_AddObject(m, "herror", socket_herror);
4292 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004293 NULL);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004294 if (socket_gaierror == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004295 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004296 Py_INCREF(socket_gaierror);
4297 PyModule_AddObject(m, "gaierror", socket_gaierror);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00004298 socket_timeout = PyErr_NewException("socket.timeout",
4299 socket_error, NULL);
4300 if (socket_timeout == NULL)
4301 return;
4302 Py_INCREF(socket_timeout);
4303 PyModule_AddObject(m, "timeout", socket_timeout);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004304 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00004305 if (PyModule_AddObject(m, "SocketType",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004306 (PyObject *)&sock_type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004307 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004308 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00004309 if (PyModule_AddObject(m, "socket",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004310 (PyObject *)&sock_type) != 0)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004311 return;
Guido van Rossum09be4091999-08-09 14:40:40 +00004312
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004313#ifdef ENABLE_IPV6
4314 has_ipv6 = Py_True;
4315#else
4316 has_ipv6 = Py_False;
4317#endif
4318 Py_INCREF(has_ipv6);
4319 PyModule_AddObject(m, "has_ipv6", has_ipv6);
4320
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004321 /* Export C API */
Fred Drake4baedc12002-04-01 14:53:37 +00004322 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004323 PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL)
4324 ) != 0)
4325 return;
4326
Guido van Rossum09be4091999-08-09 14:40:40 +00004327 /* Address families (we only support AF_INET and AF_UNIX) */
4328#ifdef AF_UNSPEC
Fred Drake4baedc12002-04-01 14:53:37 +00004329 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00004330#endif
Fred Drake4baedc12002-04-01 14:53:37 +00004331 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004332#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00004333 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004334#endif /* AF_INET6 */
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00004335#if defined(AF_UNIX)
Fred Drake4baedc12002-04-01 14:53:37 +00004336 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004337#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00004338#ifdef AF_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00004339 /* Amateur Radio AX.25 */
4340 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004341#endif
4342#ifdef AF_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00004343 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00004344#endif
4345#ifdef AF_APPLETALK
Fred Drake4baedc12002-04-01 14:53:37 +00004346 /* Appletalk DDP */
4347 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004348#endif
4349#ifdef AF_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00004350 /* Amateur radio NetROM */
4351 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004352#endif
4353#ifdef AF_BRIDGE
Fred Drake4baedc12002-04-01 14:53:37 +00004354 /* Multiprotocol bridge */
4355 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004356#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004357#ifdef AF_ATMPVC
4358 /* ATM PVCs */
4359 PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC);
4360#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004361#ifdef AF_AAL5
Fred Drake4baedc12002-04-01 14:53:37 +00004362 /* Reserved for Werner's ATM */
4363 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00004364#endif
4365#ifdef AF_X25
Fred Drake4baedc12002-04-01 14:53:37 +00004366 /* Reserved for X.25 project */
4367 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004368#endif
4369#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00004370 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00004371#endif
4372#ifdef AF_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00004373 /* Amateur Radio X.25 PLP */
4374 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004375#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004376#ifdef AF_DECnet
4377 /* Reserved for DECnet project */
4378 PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet);
4379#endif
4380#ifdef AF_NETBEUI
4381 /* Reserved for 802.2LLC project */
4382 PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI);
4383#endif
4384#ifdef AF_SECURITY
4385 /* Security callback pseudo AF */
4386 PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY);
4387#endif
4388#ifdef AF_KEY
4389 /* PF_KEY key management API */
4390 PyModule_AddIntConstant(m, "AF_KEY", AF_KEY);
4391#endif
4392#ifdef AF_NETLINK
4393 /* */
4394 PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);
Martin v. Löwis11017b12006-01-14 18:12:57 +00004395 PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE);
Martin v. Löwis5fe60e72006-04-06 22:29:33 +00004396#ifdef NETLINK_SKIP
Martin v. Löwis11017b12006-01-14 18:12:57 +00004397 PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP);
Martin v. Löwis5fe60e72006-04-06 22:29:33 +00004398#endif
4399#ifdef NETLINK_W1
4400 PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1);
4401#endif
Martin v. Löwis11017b12006-01-14 18:12:57 +00004402 PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK);
4403 PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004404#ifdef NETLINK_TCPDIAG
Martin v. Löwis11017b12006-01-14 18:12:57 +00004405 PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004406#endif
4407#ifdef NETLINK_NFLOG
Martin v. Löwis11017b12006-01-14 18:12:57 +00004408 PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004409#endif
Neal Norwitz65851662006-01-16 04:31:40 +00004410#ifdef NETLINK_XFRM
Martin v. Löwis11017b12006-01-14 18:12:57 +00004411 PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM);
Neal Norwitz65851662006-01-16 04:31:40 +00004412#endif
Martin v. Löwis5fe60e72006-04-06 22:29:33 +00004413#ifdef NETLINK_ARPD
Martin v. Löwis11017b12006-01-14 18:12:57 +00004414 PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD);
Martin v. Löwis5fe60e72006-04-06 22:29:33 +00004415#endif
4416#ifdef NETLINK_ROUTE6
Martin v. Löwis11017b12006-01-14 18:12:57 +00004417 PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6);
Martin v. Löwis5fe60e72006-04-06 22:29:33 +00004418#endif
Martin v. Löwis11017b12006-01-14 18:12:57 +00004419 PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW);
Martin v. Löwisb1cc1d42007-02-13 12:14:19 +00004420#ifdef NETLINK_DNRTMSG
Martin v. Löwis11017b12006-01-14 18:12:57 +00004421 PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG);
Martin v. Löwisb1cc1d42007-02-13 12:14:19 +00004422#endif
Martin v. Löwis5fe60e72006-04-06 22:29:33 +00004423#ifdef NETLINK_TAPBASE
Martin v. Löwis11017b12006-01-14 18:12:57 +00004424 PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE);
Martin v. Löwis5fe60e72006-04-06 22:29:33 +00004425#endif
Georg Brandldcfdae72006-04-01 07:33:08 +00004426#endif /* AF_NETLINK */
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004427#ifdef AF_ROUTE
4428 /* Alias to emulate 4.4BSD */
4429 PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE);
4430#endif
4431#ifdef AF_ASH
4432 /* Ash */
4433 PyModule_AddIntConstant(m, "AF_ASH", AF_ASH);
4434#endif
4435#ifdef AF_ECONET
4436 /* Acorn Econet */
4437 PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET);
4438#endif
4439#ifdef AF_ATMSVC
4440 /* ATM SVCs */
4441 PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC);
4442#endif
4443#ifdef AF_SNA
4444 /* Linux SNA Project (nutters!) */
4445 PyModule_AddIntConstant(m, "AF_SNA", AF_SNA);
4446#endif
4447#ifdef AF_IRDA
4448 /* IRDA sockets */
4449 PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA);
4450#endif
4451#ifdef AF_PPPOX
4452 /* PPPoX sockets */
4453 PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX);
4454#endif
4455#ifdef AF_WANPIPE
4456 /* Wanpipe API Sockets */
4457 PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE);
4458#endif
4459#ifdef AF_LLC
4460 /* Linux LLC */
4461 PyModule_AddIntConstant(m, "AF_LLC", AF_LLC);
4462#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00004463
Hye-Shik Chang81268602004-02-02 06:05:24 +00004464#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00004465 PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);
4466 PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);
Martin v. Löwis45423a72007-02-14 10:07:37 +00004467 PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI);
4468 PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI);
4469 PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP);
4470 PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR);
4471 PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER);
Hye-Shik Chang81268602004-02-02 06:05:24 +00004472#if !defined(__FreeBSD__)
Martin v. Löwis12af0482004-01-31 12:34:17 +00004473 PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00004474#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00004475 PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);
Neal Norwitz1e44ca92006-05-30 03:18:50 +00004476 PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
4477 PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
Martin v. Löwis12af0482004-01-31 12:34:17 +00004478#endif
4479
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00004480#ifdef HAVE_NETPACKET_PACKET_H
Fred Drake4baedc12002-04-01 14:53:37 +00004481 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
4482 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
4483 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
4484 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
4485 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
4486 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
4487 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
4488 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
4489 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00004490#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004491
4492 /* Socket types */
Fred Drake4baedc12002-04-01 14:53:37 +00004493 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
4494 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004495#ifndef __BEOS__
4496/* We have incomplete socket support. */
Fred Drake4baedc12002-04-01 14:53:37 +00004497 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
4498 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00004499#if defined(SOCK_RDM)
Fred Drake4baedc12002-04-01 14:53:37 +00004500 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004501#endif
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00004502#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004503
4504#ifdef SO_DEBUG
Fred Drake4baedc12002-04-01 14:53:37 +00004505 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004506#endif
4507#ifdef SO_ACCEPTCONN
Fred Drake4baedc12002-04-01 14:53:37 +00004508 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004509#endif
4510#ifdef SO_REUSEADDR
Fred Drake4baedc12002-04-01 14:53:37 +00004511 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004512#endif
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00004513#ifdef SO_EXCLUSIVEADDRUSE
4514 PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE);
4515#endif
4516
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004517#ifdef SO_KEEPALIVE
Fred Drake4baedc12002-04-01 14:53:37 +00004518 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004519#endif
4520#ifdef SO_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00004521 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004522#endif
4523#ifdef SO_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00004524 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004525#endif
4526#ifdef SO_USELOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00004527 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004528#endif
4529#ifdef SO_LINGER
Fred Drake4baedc12002-04-01 14:53:37 +00004530 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004531#endif
4532#ifdef SO_OOBINLINE
Fred Drake4baedc12002-04-01 14:53:37 +00004533 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004534#endif
4535#ifdef SO_REUSEPORT
Fred Drake4baedc12002-04-01 14:53:37 +00004536 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004537#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004538#ifdef SO_SNDBUF
Fred Drake4baedc12002-04-01 14:53:37 +00004539 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004540#endif
4541#ifdef SO_RCVBUF
Fred Drake4baedc12002-04-01 14:53:37 +00004542 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004543#endif
4544#ifdef SO_SNDLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00004545 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004546#endif
4547#ifdef SO_RCVLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00004548 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004549#endif
4550#ifdef SO_SNDTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00004551 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004552#endif
4553#ifdef SO_RCVTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00004554 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004555#endif
4556#ifdef SO_ERROR
Fred Drake4baedc12002-04-01 14:53:37 +00004557 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004558#endif
4559#ifdef SO_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004560 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004561#endif
4562
4563 /* Maximum number of connections for "listen" */
4564#ifdef SOMAXCONN
Fred Drake4baedc12002-04-01 14:53:37 +00004565 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004566#else
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004567 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004568#endif
4569
4570 /* Flags for send, recv */
4571#ifdef MSG_OOB
Fred Drake4baedc12002-04-01 14:53:37 +00004572 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004573#endif
4574#ifdef MSG_PEEK
Fred Drake4baedc12002-04-01 14:53:37 +00004575 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004576#endif
4577#ifdef MSG_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00004578 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004579#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00004580#ifdef MSG_DONTWAIT
Fred Drake4baedc12002-04-01 14:53:37 +00004581 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00004582#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004583#ifdef MSG_EOR
Fred Drake4baedc12002-04-01 14:53:37 +00004584 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004585#endif
4586#ifdef MSG_TRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00004587 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004588#endif
4589#ifdef MSG_CTRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00004590 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004591#endif
4592#ifdef MSG_WAITALL
Fred Drake4baedc12002-04-01 14:53:37 +00004593 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004594#endif
4595#ifdef MSG_BTAG
Fred Drake4baedc12002-04-01 14:53:37 +00004596 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004597#endif
4598#ifdef MSG_ETAG
Fred Drake4baedc12002-04-01 14:53:37 +00004599 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004600#endif
4601
4602 /* Protocol level and numbers, usable for [gs]etsockopt */
4603#ifdef SOL_SOCKET
Fred Drake4baedc12002-04-01 14:53:37 +00004604 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004605#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004606#ifdef SOL_IP
Fred Drake4baedc12002-04-01 14:53:37 +00004607 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004608#else
Fred Drake4baedc12002-04-01 14:53:37 +00004609 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00004610#endif
4611#ifdef SOL_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00004612 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00004613#endif
4614#ifdef SOL_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00004615 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004616#endif
4617#ifdef SOL_ATALK
Fred Drake4baedc12002-04-01 14:53:37 +00004618 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004619#endif
4620#ifdef SOL_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00004621 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004622#endif
4623#ifdef SOL_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00004624 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004625#endif
4626#ifdef SOL_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00004627 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004628#else
Fred Drake4baedc12002-04-01 14:53:37 +00004629 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00004630#endif
4631#ifdef SOL_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00004632 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004633#else
Fred Drake4baedc12002-04-01 14:53:37 +00004634 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00004635#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004636#ifdef IPPROTO_IP
Fred Drake4baedc12002-04-01 14:53:37 +00004637 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00004638#else
Fred Drake4baedc12002-04-01 14:53:37 +00004639 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004640#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004641#ifdef IPPROTO_HOPOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004642 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004643#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004644#ifdef IPPROTO_ICMP
Fred Drake4baedc12002-04-01 14:53:37 +00004645 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00004646#else
Fred Drake4baedc12002-04-01 14:53:37 +00004647 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004648#endif
4649#ifdef IPPROTO_IGMP
Fred Drake4baedc12002-04-01 14:53:37 +00004650 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004651#endif
4652#ifdef IPPROTO_GGP
Fred Drake4baedc12002-04-01 14:53:37 +00004653 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004654#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004655#ifdef IPPROTO_IPV4
Fred Drake4baedc12002-04-01 14:53:37 +00004656 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004657#endif
Martin v. Löwisa0f17342003-10-03 13:56:20 +00004658#ifdef IPPROTO_IPV6
4659 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
4660#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004661#ifdef IPPROTO_IPIP
Fred Drake4baedc12002-04-01 14:53:37 +00004662 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004663#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004664#ifdef IPPROTO_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00004665 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00004666#else
Fred Drake4baedc12002-04-01 14:53:37 +00004667 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004668#endif
4669#ifdef IPPROTO_EGP
Fred Drake4baedc12002-04-01 14:53:37 +00004670 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004671#endif
4672#ifdef IPPROTO_PUP
Fred Drake4baedc12002-04-01 14:53:37 +00004673 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004674#endif
4675#ifdef IPPROTO_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00004676 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00004677#else
Fred Drake4baedc12002-04-01 14:53:37 +00004678 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004679#endif
4680#ifdef IPPROTO_IDP
Fred Drake4baedc12002-04-01 14:53:37 +00004681 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004682#endif
4683#ifdef IPPROTO_HELLO
Fred Drake4baedc12002-04-01 14:53:37 +00004684 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004685#endif
4686#ifdef IPPROTO_ND
Fred Drake4baedc12002-04-01 14:53:37 +00004687 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004688#endif
4689#ifdef IPPROTO_TP
Fred Drake4baedc12002-04-01 14:53:37 +00004690 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004691#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004692#ifdef IPPROTO_IPV6
Fred Drake4baedc12002-04-01 14:53:37 +00004693 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004694#endif
4695#ifdef IPPROTO_ROUTING
Fred Drake4baedc12002-04-01 14:53:37 +00004696 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004697#endif
4698#ifdef IPPROTO_FRAGMENT
Fred Drake4baedc12002-04-01 14:53:37 +00004699 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004700#endif
4701#ifdef IPPROTO_RSVP
Fred Drake4baedc12002-04-01 14:53:37 +00004702 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004703#endif
4704#ifdef IPPROTO_GRE
Fred Drake4baedc12002-04-01 14:53:37 +00004705 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004706#endif
4707#ifdef IPPROTO_ESP
Fred Drake4baedc12002-04-01 14:53:37 +00004708 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004709#endif
4710#ifdef IPPROTO_AH
Fred Drake4baedc12002-04-01 14:53:37 +00004711 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004712#endif
4713#ifdef IPPROTO_MOBILE
Fred Drake4baedc12002-04-01 14:53:37 +00004714 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004715#endif
4716#ifdef IPPROTO_ICMPV6
Fred Drake4baedc12002-04-01 14:53:37 +00004717 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004718#endif
4719#ifdef IPPROTO_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00004720 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004721#endif
4722#ifdef IPPROTO_DSTOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004723 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004724#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004725#ifdef IPPROTO_XTP
Fred Drake4baedc12002-04-01 14:53:37 +00004726 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004727#endif
4728#ifdef IPPROTO_EON
Fred Drake4baedc12002-04-01 14:53:37 +00004729 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004730#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004731#ifdef IPPROTO_PIM
Fred Drake4baedc12002-04-01 14:53:37 +00004732 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004733#endif
4734#ifdef IPPROTO_IPCOMP
Fred Drake4baedc12002-04-01 14:53:37 +00004735 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004736#endif
4737#ifdef IPPROTO_VRRP
Fred Drake4baedc12002-04-01 14:53:37 +00004738 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004739#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004740#ifdef IPPROTO_BIP
Fred Drake4baedc12002-04-01 14:53:37 +00004741 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004742#endif
4743/**/
4744#ifdef IPPROTO_RAW
Fred Drake4baedc12002-04-01 14:53:37 +00004745 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00004746#else
Fred Drake4baedc12002-04-01 14:53:37 +00004747 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004748#endif
4749#ifdef IPPROTO_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00004750 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004751#endif
4752
4753 /* Some port configuration */
4754#ifdef IPPORT_RESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00004755 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004756#else
Fred Drake4baedc12002-04-01 14:53:37 +00004757 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004758#endif
4759#ifdef IPPORT_USERRESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00004760 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004761#else
Fred Drake4baedc12002-04-01 14:53:37 +00004762 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004763#endif
4764
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004765 /* Some reserved IP v.4 addresses */
4766#ifdef INADDR_ANY
Fred Drake4baedc12002-04-01 14:53:37 +00004767 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004768#else
Fred Drake4baedc12002-04-01 14:53:37 +00004769 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004770#endif
4771#ifdef INADDR_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00004772 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004773#else
Fred Drake4baedc12002-04-01 14:53:37 +00004774 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004775#endif
4776#ifdef INADDR_LOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00004777 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004778#else
Fred Drake4baedc12002-04-01 14:53:37 +00004779 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004780#endif
4781#ifdef INADDR_UNSPEC_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004782 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004783#else
Fred Drake4baedc12002-04-01 14:53:37 +00004784 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004785#endif
4786#ifdef INADDR_ALLHOSTS_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004787 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
4788 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004789#else
Fred Drake4baedc12002-04-01 14:53:37 +00004790 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004791#endif
4792#ifdef INADDR_MAX_LOCAL_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004793 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
4794 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004795#else
Fred Drake4baedc12002-04-01 14:53:37 +00004796 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004797#endif
4798#ifdef INADDR_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00004799 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004800#else
Fred Drake4baedc12002-04-01 14:53:37 +00004801 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004802#endif
4803
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004804 /* IPv4 [gs]etsockopt options */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004805#ifdef IP_OPTIONS
Fred Drake4baedc12002-04-01 14:53:37 +00004806 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004807#endif
4808#ifdef IP_HDRINCL
Fred Drake4baedc12002-04-01 14:53:37 +00004809 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004810#endif
4811#ifdef IP_TOS
Fred Drake4baedc12002-04-01 14:53:37 +00004812 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004813#endif
4814#ifdef IP_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004815 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004816#endif
4817#ifdef IP_RECVOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004818 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004819#endif
4820#ifdef IP_RECVRETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004821 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004822#endif
4823#ifdef IP_RECVDSTADDR
Fred Drake4baedc12002-04-01 14:53:37 +00004824 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004825#endif
4826#ifdef IP_RETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004827 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004828#endif
4829#ifdef IP_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00004830 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004831#endif
4832#ifdef IP_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004833 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004834#endif
4835#ifdef IP_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004836 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004837#endif
4838#ifdef IP_ADD_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00004839 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004840#endif
4841#ifdef IP_DROP_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00004842 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004843#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004844#ifdef IP_DEFAULT_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004845 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
4846 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00004847#endif
4848#ifdef IP_DEFAULT_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004849 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
4850 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004851#endif
4852#ifdef IP_MAX_MEMBERSHIPS
Fred Drake4baedc12002-04-01 14:53:37 +00004853 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00004854#endif
4855
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004856 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
4857#ifdef IPV6_JOIN_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004858 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004859#endif
4860#ifdef IPV6_LEAVE_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004861 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004862#endif
4863#ifdef IPV6_MULTICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00004864 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004865#endif
4866#ifdef IPV6_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00004867 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004868#endif
4869#ifdef IPV6_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004870 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004871#endif
4872#ifdef IPV6_UNICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00004873 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004874#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004875 /* Additional IPV6 socket options, defined in RFC 3493 */
4876#ifdef IPV6_V6ONLY
4877 PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);
4878#endif
4879 /* Advanced IPV6 socket options, from RFC 3542 */
4880#ifdef IPV6_CHECKSUM
4881 PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);
4882#endif
4883#ifdef IPV6_DONTFRAG
4884 PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);
4885#endif
4886#ifdef IPV6_DSTOPTS
4887 PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);
4888#endif
4889#ifdef IPV6_HOPLIMIT
4890 PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);
4891#endif
4892#ifdef IPV6_HOPOPTS
4893 PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);
4894#endif
4895#ifdef IPV6_NEXTHOP
4896 PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);
4897#endif
4898#ifdef IPV6_PATHMTU
4899 PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);
4900#endif
4901#ifdef IPV6_PKTINFO
4902 PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);
4903#endif
4904#ifdef IPV6_RECVDSTOPTS
4905 PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);
4906#endif
4907#ifdef IPV6_RECVHOPLIMIT
4908 PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);
4909#endif
4910#ifdef IPV6_RECVHOPOPTS
4911 PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);
4912#endif
4913#ifdef IPV6_RECVPKTINFO
4914 PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);
4915#endif
4916#ifdef IPV6_RECVRTHDR
4917 PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);
4918#endif
4919#ifdef IPV6_RECVTCLASS
4920 PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);
4921#endif
4922#ifdef IPV6_RTHDR
4923 PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);
4924#endif
4925#ifdef IPV6_RTHDRDSTOPTS
4926 PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);
4927#endif
4928#ifdef IPV6_RTHDR_TYPE_0
4929 PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);
4930#endif
4931#ifdef IPV6_RECVPATHMTU
4932 PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);
4933#endif
4934#ifdef IPV6_TCLASS
4935 PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);
4936#endif
4937#ifdef IPV6_USE_MIN_MTU
4938 PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);
4939#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004940
Guido van Rossum09be4091999-08-09 14:40:40 +00004941 /* TCP options */
4942#ifdef TCP_NODELAY
Fred Drake4baedc12002-04-01 14:53:37 +00004943 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00004944#endif
4945#ifdef TCP_MAXSEG
Fred Drake4baedc12002-04-01 14:53:37 +00004946 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00004947#endif
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004948#ifdef TCP_CORK
Fred Drake4baedc12002-04-01 14:53:37 +00004949 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004950#endif
4951#ifdef TCP_KEEPIDLE
Fred Drake4baedc12002-04-01 14:53:37 +00004952 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004953#endif
4954#ifdef TCP_KEEPINTVL
Fred Drake4baedc12002-04-01 14:53:37 +00004955 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004956#endif
4957#ifdef TCP_KEEPCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004958 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004959#endif
4960#ifdef TCP_SYNCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004961 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004962#endif
4963#ifdef TCP_LINGER2
Fred Drake4baedc12002-04-01 14:53:37 +00004964 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004965#endif
4966#ifdef TCP_DEFER_ACCEPT
Fred Drake4baedc12002-04-01 14:53:37 +00004967 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004968#endif
4969#ifdef TCP_WINDOW_CLAMP
Fred Drake4baedc12002-04-01 14:53:37 +00004970 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004971#endif
4972#ifdef TCP_INFO
Fred Drake4baedc12002-04-01 14:53:37 +00004973 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004974#endif
4975#ifdef TCP_QUICKACK
Fred Drake4baedc12002-04-01 14:53:37 +00004976 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004977#endif
4978
Guido van Rossum09be4091999-08-09 14:40:40 +00004979
4980 /* IPX options */
4981#ifdef IPX_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004982 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004983#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004984
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004985 /* get{addr,name}info parameters */
4986#ifdef EAI_ADDRFAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004987 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004988#endif
4989#ifdef EAI_AGAIN
Fred Drake4baedc12002-04-01 14:53:37 +00004990 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004991#endif
4992#ifdef EAI_BADFLAGS
Fred Drake4baedc12002-04-01 14:53:37 +00004993 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004994#endif
4995#ifdef EAI_FAIL
Fred Drake4baedc12002-04-01 14:53:37 +00004996 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004997#endif
4998#ifdef EAI_FAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004999 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005000#endif
5001#ifdef EAI_MEMORY
Fred Drake4baedc12002-04-01 14:53:37 +00005002 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005003#endif
5004#ifdef EAI_NODATA
Fred Drake4baedc12002-04-01 14:53:37 +00005005 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005006#endif
5007#ifdef EAI_NONAME
Fred Drake4baedc12002-04-01 14:53:37 +00005008 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005009#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00005010#ifdef EAI_OVERFLOW
5011 PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);
5012#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005013#ifdef EAI_SERVICE
Fred Drake4baedc12002-04-01 14:53:37 +00005014 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005015#endif
5016#ifdef EAI_SOCKTYPE
Fred Drake4baedc12002-04-01 14:53:37 +00005017 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005018#endif
5019#ifdef EAI_SYSTEM
Fred Drake4baedc12002-04-01 14:53:37 +00005020 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005021#endif
5022#ifdef EAI_BADHINTS
Fred Drake4baedc12002-04-01 14:53:37 +00005023 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005024#endif
5025#ifdef EAI_PROTOCOL
Fred Drake4baedc12002-04-01 14:53:37 +00005026 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005027#endif
5028#ifdef EAI_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00005029 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005030#endif
5031#ifdef AI_PASSIVE
Fred Drake4baedc12002-04-01 14:53:37 +00005032 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005033#endif
5034#ifdef AI_CANONNAME
Fred Drake4baedc12002-04-01 14:53:37 +00005035 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005036#endif
5037#ifdef AI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00005038 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005039#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00005040#ifdef AI_NUMERICSERV
5041 PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);
5042#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005043#ifdef AI_MASK
Fred Drake4baedc12002-04-01 14:53:37 +00005044 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005045#endif
5046#ifdef AI_ALL
Fred Drake4baedc12002-04-01 14:53:37 +00005047 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005048#endif
5049#ifdef AI_V4MAPPED_CFG
Fred Drake4baedc12002-04-01 14:53:37 +00005050 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005051#endif
5052#ifdef AI_ADDRCONFIG
Fred Drake4baedc12002-04-01 14:53:37 +00005053 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005054#endif
5055#ifdef AI_V4MAPPED
Fred Drake4baedc12002-04-01 14:53:37 +00005056 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005057#endif
5058#ifdef AI_DEFAULT
Fred Drake4baedc12002-04-01 14:53:37 +00005059 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005060#endif
5061#ifdef NI_MAXHOST
Fred Drake4baedc12002-04-01 14:53:37 +00005062 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005063#endif
5064#ifdef NI_MAXSERV
Fred Drake4baedc12002-04-01 14:53:37 +00005065 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005066#endif
5067#ifdef NI_NOFQDN
Fred Drake4baedc12002-04-01 14:53:37 +00005068 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005069#endif
5070#ifdef NI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00005071 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005072#endif
5073#ifdef NI_NAMEREQD
Fred Drake4baedc12002-04-01 14:53:37 +00005074 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005075#endif
5076#ifdef NI_NUMERICSERV
Fred Drake4baedc12002-04-01 14:53:37 +00005077 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005078#endif
5079#ifdef NI_DGRAM
Fred Drake4baedc12002-04-01 14:53:37 +00005080 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005081#endif
5082
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005083 /* shutdown() parameters */
5084#ifdef SHUT_RD
5085 PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);
5086#elif defined(SD_RECEIVE)
5087 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
5088#else
5089 PyModule_AddIntConstant(m, "SHUT_RD", 0);
5090#endif
5091#ifdef SHUT_WR
5092 PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);
5093#elif defined(SD_SEND)
5094 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
5095#else
5096 PyModule_AddIntConstant(m, "SHUT_WR", 1);
5097#endif
5098#ifdef SHUT_RDWR
5099 PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);
5100#elif defined(SD_BOTH)
5101 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
5102#else
5103 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
5104#endif
5105
Guido van Rossum4f199ea1998-04-09 20:56:35 +00005106 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00005107#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
5108 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00005109#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005110}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005111
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005112
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005113#ifndef HAVE_INET_PTON
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005114
5115/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005116/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005117
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005118int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005119inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005120{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005121 if (af == AF_INET) {
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005122 long packed_addr;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005123 packed_addr = inet_addr(src);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005124 if (packed_addr == INADDR_NONE)
5125 return 0;
5126 memcpy(dst, &packed_addr, 4);
5127 return 1;
5128 }
5129 /* Should set errno to EAFNOSUPPORT */
5130 return -1;
5131}
5132
Martin v. Löwisc925b1532001-07-21 09:42:15 +00005133const char *
5134inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005135{
5136 if (af == AF_INET) {
5137 struct in_addr packed_addr;
5138 if (size < 16)
5139 /* Should set errno to ENOSPC. */
5140 return NULL;
5141 memcpy(&packed_addr, src, sizeof(packed_addr));
5142 return strncpy(dst, inet_ntoa(packed_addr), size);
5143 }
5144 /* Should set errno to EAFNOSUPPORT */
5145 return NULL;
5146}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005147
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005148#endif