blob: ce594dc9ddd70d25a15a4e207c4a095685a7c4fc [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
Christian Heimes043d6f62008-01-07 17:19:16 +000010 portable manner, though AF_PACKET, AF_NETLINK and AF_TIPC are supported
11 under Linux.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000012- No read/write operations (use sendall/recv or makefile instead).
13- Additional restrictions apply on some non-Unix platforms (compensated
14 for by socket.py).
Guido van Rossum6574b3e1991-06-25 21:36:08 +000015
Guido van Rossum27e177d1995-03-16 15:43:47 +000016Module interface:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000017
Guido van Rossum27e177d1995-03-16 15:43:47 +000018- socket.error: exception raised for socket specific errors
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000019- socket.gaierror: exception raised for getaddrinfo/getnameinfo errors,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000020 a subclass of socket.error
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000021- socket.herror: exception raised for gethostby* errors,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000022 a subclass of socket.error
Guido van Rossum30a685f1991-06-27 15:51:29 +000023- socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000024- socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])
Guido van Rossum27e177d1995-03-16 15:43:47 +000025- socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')
Guido van Rossum25405c71996-12-19 16:42:52 +000026- socket.getprotobyname(protocolname) --> protocol number
Barry Warsaw11b91a02004-06-28 00:50:43 +000027- socket.getservbyname(servicename[, protocolname]) --> port number
28- socket.getservbyport(portnumber[, protocolname]) --> service name
Guido van Rossum7d0a8262007-05-21 23:13:11 +000029- socket.socket([family[, type [, proto, fileno]]]) --> new socket object
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000030 (fileno specifies a pre-existing socket file descriptor)
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])
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000037 --> List of (family, socktype, proto, canonname, sockaddr)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000038- 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.
Christian Heimes043d6f62008-01-07 17:19:16 +000055- an AF_TIPC socket address is expressed as
56 (addr_type, v1, v2, v3 [, scope]); where addr_type can be one of:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000057 TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID;
Christian Heimes043d6f62008-01-07 17:19:16 +000058 and scope can be one of:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000059 TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE.
Christian Heimes043d6f62008-01-07 17:19:16 +000060 The meaning of v1, v2 and v3 depends on the value of addr_type:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000061 if addr_type is TIPC_ADDR_NAME:
62 v1 is the server type
63 v2 is the port identifier
64 v3 is ignored
65 if addr_type is TIPC_ADDR_NAMESEQ:
66 v1 is the server type
67 v2 is the lower port number
68 v3 is the upper port number
69 if addr_type is TIPC_ADDR_ID:
70 v1 is the node
71 v2 is the ref
72 v3 is ignored
Christian Heimes043d6f62008-01-07 17:19:16 +000073
Guido van Rossum6574b3e1991-06-25 21:36:08 +000074
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000075Local naming conventions:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000076
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000077- names starting with sock_ are socket object methods
78- names starting with socket_ are module-level functions
79- names starting with PySocket are exported through socketmodule.h
Guido van Rossum30a685f1991-06-27 15:51:29 +000080
Guido van Rossum6574b3e1991-06-25 21:36:08 +000081*/
82
Thomas Wouters477c8d52006-05-27 19:21:47 +000083#ifdef __APPLE__
84 /*
85 * inet_aton is not available on OSX 10.3, yet we want to use a binary
86 * that was build on 10.4 or later to work on that release, weak linking
87 * comes to the rescue.
88 */
89# pragma weak inet_aton
90#endif
91
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000092#include "Python.h"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000093#include "structmember.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000094
Guido van Rossum47dfa4a2003-04-25 05:48:32 +000095#undef MAX
96#define MAX(x, y) ((x) < (y) ? (y) : (x))
97
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000098/* Socket object documentation */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000099PyDoc_STRVAR(sock_doc,
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000100"socket([family[, type[, proto]]]) -> socket object\n\
101\n\
102Open a socket of the given type. The family argument specifies the\n\
103address family; it defaults to AF_INET. The type argument specifies\n\
104whether this is a stream (SOCK_STREAM, this is the default)\n\
105or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
106specifying the default protocol. Keyword arguments are accepted.\n\
107\n\
108A socket object represents one endpoint of a network connection.\n\
109\n\
110Methods of socket objects (keyword arguments not allowed):\n\
111\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000112_accept() -- accept connection, returning new socket fd and client address\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000113bind(addr) -- bind the socket to a local address\n\
114close() -- close the socket\n\
115connect(addr) -- connect the socket to a remote address\n\
116connect_ex(addr) -- connect, return an error code instead of an exception\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000117_dup() -- return a new socket fd duplicated from fileno()\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000118fileno() -- return underlying file descriptor\n\
119getpeername() -- return remote address [*]\n\
120getsockname() -- return local address\n\
121getsockopt(level, optname[, buflen]) -- get socket options\n\
122gettimeout() -- return timeout or None\n\
123listen(n) -- start listening for incoming connections\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000124recv(buflen[, flags]) -- receive data\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000125recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000126recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000127recvfrom_into(buffer[, nbytes, [, flags])\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000128 -- receive data and sender\'s address (into a buffer)\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000129sendall(data[, flags]) -- send all data\n\
130send(data[, flags]) -- send data, may not send all of it\n\
131sendto(data[, flags], addr) -- send data to a given address\n\
132setblocking(0 | 1) -- set or clear the blocking I/O flag\n\
133setsockopt(level, optname, value) -- set socket options\n\
134settimeout(None | float) -- set or clear the timeout\n\
135shutdown(how) -- shut down traffic in one or both directions\n\
136\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000137 [*] not available on all platforms!");
Guido van Rossum3baaa131999-03-22 21:44:51 +0000138
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000139/* XXX This is a terrible mess of platform-dependent preprocessor hacks.
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000140 I hope some day someone can clean this up please... */
141
Guido van Rossum9376b741999-09-15 22:01:40 +0000142/* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
143 script doesn't get this right, so we hardcode some platform checks below.
144 On the other hand, not all Linux versions agree, so there the settings
145 computed by the configure script are needed! */
146
147#ifndef linux
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000148# undef HAVE_GETHOSTBYNAME_R_3_ARG
149# undef HAVE_GETHOSTBYNAME_R_5_ARG
150# undef HAVE_GETHOSTBYNAME_R_6_ARG
Guido van Rossum9376b741999-09-15 22:01:40 +0000151#endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000152
Guido van Rossum7a122991999-04-13 04:07:32 +0000153#ifndef WITH_THREAD
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000154# undef HAVE_GETHOSTBYNAME_R
Guido van Rossum7a122991999-04-13 04:07:32 +0000155#endif
156
Guido van Rossume7de2061999-03-24 17:24:33 +0000157#ifdef HAVE_GETHOSTBYNAME_R
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000158# if defined(_AIX) || defined(__osf__)
159# define HAVE_GETHOSTBYNAME_R_3_ARG
160# elif defined(__sun) || defined(__sgi)
161# define HAVE_GETHOSTBYNAME_R_5_ARG
162# elif defined(linux)
Guido van Rossum9376b741999-09-15 22:01:40 +0000163/* Rely on the configure script */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000164# else
165# undef HAVE_GETHOSTBYNAME_R
166# endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000167#endif
168
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000169#if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \
170 !defined(MS_WINDOWS)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000171# define USE_GETHOSTBYNAME_LOCK
Guido van Rossum3baaa131999-03-22 21:44:51 +0000172#endif
173
Hye-Shik Chang9ceebd52005-09-24 14:58:47 +0000174/* To use __FreeBSD_version */
175#ifdef HAVE_SYS_PARAM_H
176#include <sys/param.h>
177#endif
Just van Rossum1040d2c2003-05-09 07:53:18 +0000178/* On systems on which getaddrinfo() is believed to not be thread-safe,
Just van Rossum09aecd72003-05-09 08:03:44 +0000179 (this includes the getaddrinfo emulation) protect access with a lock. */
Hye-Shik Chang9ceebd52005-09-24 14:58:47 +0000180#if defined(WITH_THREAD) && (defined(__APPLE__) || \
181 (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000182 defined(__OpenBSD__) || defined(__NetBSD__) || \
183 defined(__VMS) || !defined(HAVE_GETADDRINFO))
Just van Rossum1040d2c2003-05-09 07:53:18 +0000184#define USE_GETADDRINFO_LOCK
185#endif
186
187#ifdef USE_GETADDRINFO_LOCK
188#define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1);
189#define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock);
190#else
191#define ACQUIRE_GETADDRINFO_LOCK
192#define RELEASE_GETADDRINFO_LOCK
193#endif
194
195#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000196# include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000197#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000198
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000199#if defined(PYCC_VACPP)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000200# include <types.h>
201# include <io.h>
202# include <sys/ioctl.h>
203# include <utils.h>
204# include <ctype.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000205#endif
206
Martin v. Löwis9e437302002-12-06 12:57:26 +0000207#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000208# include <ioctl.h>
209#endif
210
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000211#if defined(PYOS_OS2)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000212# define INCL_DOS
213# define INCL_DOSERRORS
214# define INCL_NOPMAPI
215# include <os2.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000216#endif
217
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000218#if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000219/* make sure that the reentrant (gethostbyaddr_r etc)
220 functions are declared correctly if compiling with
221 MIPSPro 7.x in ANSI C mode (default) */
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000222
Thomas Wouters477c8d52006-05-27 19:21:47 +0000223/* XXX Using _SGIAPI is the wrong thing,
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000224 but I don't know what the right thing is. */
Trent Mick8ea5bdf2004-09-13 17:48:41 +0000225#undef _SGIAPI /* to avoid warning */
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000226#define _SGIAPI 1
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000227
Trent Mick8ea5bdf2004-09-13 17:48:41 +0000228#undef _XOPEN_SOURCE
229#include <sys/socket.h>
230#include <sys/types.h>
231#include <netinet/in.h>
232#ifdef _SS_ALIGNSIZE
233#define HAVE_GETADDRINFO 1
234#define HAVE_GETNAMEINFO 1
235#endif
236
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000237#define HAVE_INET_PTON
238#include <netdb.h>
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000239#endif
240
Thomas Wouters477c8d52006-05-27 19:21:47 +0000241/* Irix 6.5 fails to define this variable at all. This is needed
242 for both GCC and SGI's compiler. I'd say that the SGI headers
Georg Brandldbd83392006-02-20 09:42:33 +0000243 are just busted. Same thing for Solaris. */
244#if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN)
Anthony Baxterbab23cf2003-10-04 08:00:49 +0000245#define INET_ADDRSTRLEN 16
246#endif
247
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000248/* Generic includes */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000249#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000250#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000251#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000252
Marc-André Lemburg976ade62002-02-16 18:47:07 +0000253/* Generic socket object definitions and includes */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000254#define PySocket_BUILDING_SOCKET
Marc-André Lemburgbb8b78b2002-02-16 18:44:52 +0000255#include "socketmodule.h"
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000256
257/* Addressing includes */
258
Guido van Rossum6f489d91996-06-28 20:15:15 +0000259#ifndef MS_WINDOWS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000260
261/* Non-MS WINDOWS includes */
262# include <netdb.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000263
Guido van Rossum9376b741999-09-15 22:01:40 +0000264/* Headers needed for inet_ntoa() and inet_addr() */
Skip Montanaroeb33e5a2007-08-17 12:57:41 +0000265# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000266# include <netdb.h>
Tim Peters603c6832001-11-05 02:45:59 +0000267typedef size_t socklen_t;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000268# else
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000269# include <arpa/inet.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000270# endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000271
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000272# include <fcntl.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000273
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000274#else
Guido van Rossum48a680c2001-03-02 06:34:14 +0000275
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000276/* MS_WINDOWS includes */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277# ifdef HAVE_FCNTL_H
278# include <fcntl.h>
279# endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000280
Jeremy Hylton22308652001-02-02 03:23:09 +0000281#endif
282
Skip Montanaro7befb992004-02-10 16:50:21 +0000283#include <stddef.h>
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000284
285#ifndef offsetof
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000286# define offsetof(type, member) ((size_t)(&((type *)0)->member))
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000287#endif
288
Neal Norwitz39d22e52002-11-02 19:55:21 +0000289#ifndef O_NONBLOCK
290# define O_NONBLOCK O_NDELAY
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000291#endif
292
Trent Micka708d6e2004-09-07 17:48:26 +0000293/* include Python's addrinfo.h unless it causes trouble */
294#if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE)
295 /* Do not include addinfo.h on some newer IRIX versions.
296 * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21,
297 * for example, but not by 6.5.10.
298 */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000299#elif defined(_MSC_VER) && _MSC_VER>1201
Trent Micka708d6e2004-09-07 17:48:26 +0000300 /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and
301 * EAI_* constants are defined in (the already included) ws2tcpip.h.
302 */
303#else
304# include "addrinfo.h"
305#endif
Jason Tishlerc246cb72004-08-09 13:25:59 +0000306
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000307#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +0000308#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000309int inet_pton(int af, const char *src, void *dst);
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000310const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000311#endif
Christian Heimesb6150692007-11-15 23:37:07 +0000312#endif
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000313
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000314#ifdef __APPLE__
315/* On OS X, getaddrinfo returns no error indication of lookup
316 failure, so we must use the emulation instead of the libinfo
317 implementation. Unfortunately, performing an autoconf test
318 for this bug would require DNS access for the machine performing
319 the configuration, which is not acceptable. Therefore, we
320 determine the bug just by checking for __APPLE__. If this bug
321 gets ever fixed, perhaps checking for sys/version.h would be
322 appropriate, which is 10/0 on the system with the bug. */
Jack Jansen84262fb2002-07-02 14:40:42 +0000323#ifndef HAVE_GETNAMEINFO
324/* This bug seems to be fixed in Jaguar. Ths easiest way I could
325 Find to check for Jaguar is that it has getnameinfo(), which
326 older releases don't have */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000327#undef HAVE_GETADDRINFO
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000328#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000329
330#ifdef HAVE_INET_ATON
331#define USE_INET_ATON_WEAKLINK
332#endif
333
Jack Jansen84262fb2002-07-02 14:40:42 +0000334#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000335
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000336/* I know this is a bad practice, but it is the easiest... */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000337#if !defined(HAVE_GETADDRINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000338/* avoid clashes with the C library definition of the symbol. */
339#define getaddrinfo fake_getaddrinfo
340#define gai_strerror fake_gai_strerror
341#define freeaddrinfo fake_freeaddrinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000342#include "getaddrinfo.c"
343#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000344#if !defined(HAVE_GETNAMEINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000345#define getnameinfo fake_getnameinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000346#include "getnameinfo.c"
347#endif
348
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000349#ifdef MS_WINDOWS
350/* On Windows a socket is really a handle not an fd */
351static SOCKET
352dup_socket(SOCKET handle)
353{
Daniel Stutzbachee541e02010-09-03 12:42:06 +0000354 WSAPROTOCOL_INFO info;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000355
Daniel Stutzbachee541e02010-09-03 12:42:06 +0000356 if (WSADuplicateSocket(handle, GetCurrentProcessId(), &info))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000357 return INVALID_SOCKET;
Daniel Stutzbachee541e02010-09-03 12:42:06 +0000358
359 return WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
360 FROM_PROTOCOL_INFO, &info, 0, 0);
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000361}
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000362#define SOCKETCLOSE closesocket
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000363#else
364/* On Unix we can use dup to duplicate the file descriptor of a socket*/
365#define dup_socket(fd) dup(fd)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000366#endif
367
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000368#ifdef MS_WIN32
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000369#define EAFNOSUPPORT WSAEAFNOSUPPORT
370#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000371#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000372
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000373#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000374#define SOCKETCLOSE soclose
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000375#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000376#endif
377
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000378#ifndef SOCKETCLOSE
379#define SOCKETCLOSE close
380#endif
381
Jesse Noller32d68c22009-03-31 18:48:42 +0000382#if defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H) && !defined(__NetBSD__)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000383#define USE_BLUETOOTH 1
384#if defined(__FreeBSD__)
385#define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP
386#define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM
Thomas Wouterscf297e42007-02-23 15:07:44 +0000387#define BTPROTO_HCI BLUETOOTH_PROTO_HCI
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000388#define SOL_HCI SOL_HCI_RAW
389#define HCI_FILTER SO_HCI_RAW_FILTER
Hye-Shik Chang81268602004-02-02 06:05:24 +0000390#define sockaddr_l2 sockaddr_l2cap
391#define sockaddr_rc sockaddr_rfcomm
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000392#define hci_dev hci_node
Hye-Shik Chang81268602004-02-02 06:05:24 +0000393#define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb)
394#define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000395#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000396#elif defined(__NetBSD__)
397#define sockaddr_l2 sockaddr_bt
398#define sockaddr_rc sockaddr_bt
Thomas Wouterscf297e42007-02-23 15:07:44 +0000399#define sockaddr_hci sockaddr_bt
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000400#define sockaddr_sco sockaddr_bt
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000401#define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb)
402#define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000403#define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000404#define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000405#else
Hye-Shik Chang81268602004-02-02 06:05:24 +0000406#define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)
407#define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000408#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000409#define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)
410#endif
411#endif
412
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000413#ifdef __VMS
414/* TCP/IP Services for VMS uses a maximum send/recv buffer length */
415#define SEGMENT_SIZE (32 * 1024 -1)
416#endif
417
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000418#define SAS2SA(x) ((struct sockaddr *)(x))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000419
Martin v. Löwise9416172003-05-03 10:12:45 +0000420/*
421 * Constants for getnameinfo()
422 */
423#if !defined(NI_MAXHOST)
424#define NI_MAXHOST 1025
425#endif
426#if !defined(NI_MAXSERV)
427#define NI_MAXSERV 32
428#endif
429
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000430#ifndef INVALID_SOCKET /* MS defines this */
431#define INVALID_SOCKET (-1)
432#endif
433
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000434/* XXX There's a problem here: *static* functions are not supposed to have
435 a Py prefix (or use CapitalizedWords). Later... */
436
Guido van Rossum30a685f1991-06-27 15:51:29 +0000437/* Global variable holding the exception type for errors detected
438 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000439static PyObject *socket_error;
440static PyObject *socket_herror;
441static PyObject *socket_gaierror;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000442static PyObject *socket_timeout;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000443
Tim Peters643a7fc2002-02-17 04:13:21 +0000444/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000445 The sock_type variable contains pointers to various functions,
446 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000447 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000448static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000449
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000450#if defined(HAVE_POLL_H)
451#include <poll.h>
452#elif defined(HAVE_SYS_POLL_H)
453#include <sys/poll.h>
454#endif
455
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000456#ifdef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE
457/* Platform can select file descriptors beyond FD_SETSIZE */
458#define IS_SELECTABLE(s) 1
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000459#elif defined(HAVE_POLL)
460/* Instead of select(), we'll use poll() since poll() works on any fd. */
461#define IS_SELECTABLE(s) 1
462/* Can we call select() with this socket without a buffer overrun? */
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000463#else
464/* POSIX says selecting file descriptors beyond FD_SETSIZE
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000465 has undefined behaviour. If there's no timeout left, we don't have to
466 call select, so it's a safe, little white lie. */
467#define IS_SELECTABLE(s) ((s)->sock_fd < FD_SETSIZE || s->sock_timeout <= 0.0)
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000468#endif
Neal Norwitz082b2df2006-02-07 07:04:46 +0000469
470static PyObject*
471select_error(void)
472{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000473 PyErr_SetString(socket_error, "unable to select on socket");
474 return NULL;
Neal Norwitz082b2df2006-02-07 07:04:46 +0000475}
476
Guido van Rossum30a685f1991-06-27 15:51:29 +0000477/* Convenience function to raise an error according to errno
478 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000479
Guido van Rossum73624e91994-10-10 17:59:00 +0000480static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000481set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000482{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000483#ifdef MS_WINDOWS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000484 int err_no = WSAGetLastError();
485 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
486 recognizes the error codes used by both GetLastError() and
487 WSAGetLastError */
488 if (err_no)
489 return PyErr_SetExcFromWindowsErr(socket_error, err_no);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000490#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000491
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000492#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000493 if (sock_errno() != NO_ERROR) {
494 APIRET rc;
495 ULONG msglen;
496 char outbuf[100];
497 int myerrorcode = sock_errno();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000498
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000499 /* Retrieve socket-related error message from MPTN.MSG file */
500 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
501 myerrorcode - SOCBASEERR + 26,
502 "mptn.msg",
503 &msglen);
504 if (rc == NO_ERROR) {
505 PyObject *v;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000506
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000507 /* OS/2 doesn't guarantee a terminator */
508 outbuf[msglen] = '\0';
509 if (strlen(outbuf) > 0) {
510 /* If non-empty msg, trim CRLF */
511 char *lastc = &outbuf[ strlen(outbuf)-1 ];
512 while (lastc > outbuf &&
513 isspace(Py_CHARMASK(*lastc))) {
514 /* Trim trailing whitespace (CRLF) */
515 *lastc-- = '\0';
516 }
517 }
518 v = Py_BuildValue("(is)", myerrorcode, outbuf);
519 if (v != NULL) {
520 PyErr_SetObject(socket_error, v);
521 Py_DECREF(v);
522 }
523 return NULL;
524 }
525 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000526#endif
527
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000528 return PyErr_SetFromErrno(socket_error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000529}
530
Guido van Rossum30a685f1991-06-27 15:51:29 +0000531
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000532static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000533set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000534{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000535 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000536
537#ifdef HAVE_HSTRERROR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000538 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000539#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000540 v = Py_BuildValue("(is)", h_error, "host not found");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000541#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000542 if (v != NULL) {
543 PyErr_SetObject(socket_herror, v);
544 Py_DECREF(v);
545 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000546
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000547 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000548}
549
550
551static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000552set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000553{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000554 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000555
Martin v. Löwis272cb402002-03-01 08:31:07 +0000556#ifdef EAI_SYSTEM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000557 /* EAI_SYSTEM is not available on Windows XP. */
558 if (error == EAI_SYSTEM)
559 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000560#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000561
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000562#ifdef HAVE_GAI_STRERROR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000563 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000564#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000565 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000566#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000567 if (v != NULL) {
568 PyErr_SetObject(socket_gaierror, v);
569 Py_DECREF(v);
570 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000571
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000572 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000573}
574
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000575#ifdef __VMS
576/* Function to send in segments */
577static int
578sendsegmented(int sock_fd, char *buf, int len, int flags)
579{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000580 int n = 0;
581 int remaining = len;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000582
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000583 while (remaining > 0) {
584 unsigned int segment;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000585
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000586 segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining);
587 n = send(sock_fd, buf, segment, flags);
588 if (n < 0) {
589 return n;
590 }
591 remaining -= segment;
592 buf += segment;
593 } /* end while */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000594
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000595 return len;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000596}
597#endif
598
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000599/* Function to perform the setting of socket blocking mode
600 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000601static int
602internal_setblocking(PySocketSockObject *s, int block)
603{
Guido van Rossum67f7a382002-06-06 21:08:16 +0000604#ifndef MS_WINDOWS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000605 int delay_flag;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000606#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +0000607
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000608 Py_BEGIN_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000609#ifndef MS_WINDOWS
610#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000611 block = !block;
612 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000613#elif defined(__VMS)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000614 block = !block;
615 ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000616#else /* !PYOS_OS2 && !__VMS */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000617 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
618 if (block)
619 delay_flag &= (~O_NONBLOCK);
620 else
621 delay_flag |= O_NONBLOCK;
622 fcntl(s->sock_fd, F_SETFL, delay_flag);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000623#endif /* !PYOS_OS2 */
624#else /* MS_WINDOWS */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000625 block = !block;
626 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000627#endif /* MS_WINDOWS */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000628 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000629
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000630 /* Since these don't return anything */
631 return 1;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000632}
633
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000634/* Do a select()/poll() on the socket, if necessary (sock_timeout > 0).
Guido van Rossum11ba0942002-06-13 15:07:44 +0000635 The argument writing indicates the direction.
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000636 This does not raise an exception; we'll let our caller do that
637 after they've reacquired the interpreter lock.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000638 Returns 1 on timeout, -1 on error, 0 otherwise. */
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000639static int
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000640internal_select(PySocketSockObject *s, int writing)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000641{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000642 int n;
Guido van Rossum11ba0942002-06-13 15:07:44 +0000643
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000644 /* Nothing to do unless we're in timeout mode (not non-blocking) */
645 if (s->sock_timeout <= 0.0)
646 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000647
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000648 /* Guard against closed socket */
649 if (s->sock_fd < 0)
650 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000651
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000652 /* Prefer poll, if available, since you can poll() any fd
653 * which can't be done with select(). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000654#ifdef HAVE_POLL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000655 {
656 struct pollfd pollfd;
657 int timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000658
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000659 pollfd.fd = s->sock_fd;
660 pollfd.events = writing ? POLLOUT : POLLIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000661
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000662 /* s->sock_timeout is in seconds, timeout in ms */
663 timeout = (int)(s->sock_timeout * 1000 + 0.5);
664 n = poll(&pollfd, 1, timeout);
665 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000666#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000667 {
668 /* Construct the arguments to select */
669 fd_set fds;
670 struct timeval tv;
671 tv.tv_sec = (int)s->sock_timeout;
672 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
673 FD_ZERO(&fds);
674 FD_SET(s->sock_fd, &fds);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000675
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000676 /* See if the socket is ready */
677 if (writing)
Antoine Pitrouf72006f2010-08-17 19:39:39 +0000678 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
679 NULL, &fds, NULL, &tv);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000680 else
Antoine Pitrouf72006f2010-08-17 19:39:39 +0000681 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
682 &fds, NULL, NULL, &tv);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000683 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000684#endif
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000685
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000686 if (n < 0)
687 return -1;
688 if (n == 0)
689 return 1;
690 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000691}
692
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000693/* Initialize a new socket object. */
694
Tim Petersa12b4cf2002-07-18 22:38:44 +0000695static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000696
Martin v. Löwis1a214512008-06-11 05:26:20 +0000697static void
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000698init_sockobject(PySocketSockObject *s,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000699 SOCKET_T fd, int family, int type, int proto)
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000700{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000701 s->sock_fd = fd;
702 s->sock_family = family;
703 s->sock_type = type;
704 s->sock_proto = proto;
705 s->sock_timeout = defaulttimeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000706
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000707 s->errorhandler = &set_error;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000708
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000709 if (defaulttimeout >= 0.0)
710 internal_setblocking(s, 0);
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000711
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000712}
713
714
Guido van Rossum30a685f1991-06-27 15:51:29 +0000715/* Create a new socket object.
716 This just creates the object and initializes it.
717 If the creation fails, return NULL and set an exception (implicit
718 in NEWOBJ()). */
719
Guido van Rossum73624e91994-10-10 17:59:00 +0000720static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000721new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000722{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000723 PySocketSockObject *s;
724 s = (PySocketSockObject *)
725 PyType_GenericNew(&sock_type, NULL, NULL);
726 if (s != NULL)
727 init_sockobject(s, fd, family, type, proto);
728 return s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000729}
730
Guido van Rossum30a685f1991-06-27 15:51:29 +0000731
Guido van Rossum48a680c2001-03-02 06:34:14 +0000732/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000733 thread to be in gethostbyname or getaddrinfo */
734#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
735PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000736#endif
737
738
Guido van Rossum30a685f1991-06-27 15:51:29 +0000739/* Convert a string specifying a host name or one of a few symbolic
740 names to a numeric IP address. This usually calls gethostbyname()
741 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000742 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000743 an error occurred; then an exception is raised. */
744
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000745static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000746setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000747{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000748 struct addrinfo hints, *res;
749 int error;
750 int d1, d2, d3, d4;
751 char ch;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000752
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000753 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
754 if (name[0] == '\0') {
755 int siz;
756 memset(&hints, 0, sizeof(hints));
757 hints.ai_family = af;
758 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
759 hints.ai_flags = AI_PASSIVE;
760 Py_BEGIN_ALLOW_THREADS
761 ACQUIRE_GETADDRINFO_LOCK
762 error = getaddrinfo(NULL, "0", &hints, &res);
763 Py_END_ALLOW_THREADS
764 /* We assume that those thread-unsafe getaddrinfo() versions
765 *are* safe regarding their return value, ie. that a
766 subsequent call to getaddrinfo() does not destroy the
767 outcome of the first call. */
768 RELEASE_GETADDRINFO_LOCK
769 if (error) {
770 set_gaierror(error);
771 return -1;
772 }
773 switch (res->ai_family) {
774 case AF_INET:
775 siz = 4;
776 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000777#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000778 case AF_INET6:
779 siz = 16;
780 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000781#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000782 default:
783 freeaddrinfo(res);
784 PyErr_SetString(socket_error,
785 "unsupported address family");
786 return -1;
787 }
788 if (res->ai_next) {
789 freeaddrinfo(res);
790 PyErr_SetString(socket_error,
791 "wildcard resolved to multiple address");
792 return -1;
793 }
794 if (res->ai_addrlen < addr_ret_size)
795 addr_ret_size = res->ai_addrlen;
796 memcpy(addr_ret, res->ai_addr, addr_ret_size);
797 freeaddrinfo(res);
798 return siz;
799 }
800 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
801 struct sockaddr_in *sin;
802 if (af != AF_INET && af != AF_UNSPEC) {
803 PyErr_SetString(socket_error,
804 "address family mismatched");
805 return -1;
806 }
807 sin = (struct sockaddr_in *)addr_ret;
808 memset((void *) sin, '\0', sizeof(*sin));
809 sin->sin_family = AF_INET;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000810#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000811 sin->sin_len = sizeof(*sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000812#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000813 sin->sin_addr.s_addr = INADDR_BROADCAST;
814 return sizeof(sin->sin_addr);
815 }
816 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
817 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
818 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
819 struct sockaddr_in *sin;
820 sin = (struct sockaddr_in *)addr_ret;
821 sin->sin_addr.s_addr = htonl(
822 ((long) d1 << 24) | ((long) d2 << 16) |
823 ((long) d3 << 8) | ((long) d4 << 0));
824 sin->sin_family = AF_INET;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000825#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000826 sin->sin_len = sizeof(*sin);
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000827#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000828 return 4;
829 }
830 memset(&hints, 0, sizeof(hints));
831 hints.ai_family = af;
832 Py_BEGIN_ALLOW_THREADS
833 ACQUIRE_GETADDRINFO_LOCK
834 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000835#if defined(__digital__) && defined(__unix__)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000836 if (error == EAI_NONAME && af == AF_UNSPEC) {
837 /* On Tru64 V5.1, numeric-to-addr conversion fails
838 if no address family is given. Assume IPv4 for now.*/
839 hints.ai_family = AF_INET;
840 error = getaddrinfo(name, NULL, &hints, &res);
841 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000842#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000843 Py_END_ALLOW_THREADS
844 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
845 if (error) {
846 set_gaierror(error);
847 return -1;
848 }
849 if (res->ai_addrlen < addr_ret_size)
850 addr_ret_size = res->ai_addrlen;
851 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
852 freeaddrinfo(res);
853 switch (addr_ret->sa_family) {
854 case AF_INET:
855 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000856#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000857 case AF_INET6:
858 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000859#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000860 default:
861 PyErr_SetString(socket_error, "unknown address family");
862 return -1;
863 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000864}
865
Guido van Rossum30a685f1991-06-27 15:51:29 +0000866
Guido van Rossum30a685f1991-06-27 15:51:29 +0000867/* Create a string object representing an IP address.
868 This is always a string of the form 'dd.dd.dd.dd' (with variable
869 size numbers). */
870
Guido van Rossum73624e91994-10-10 17:59:00 +0000871static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000872makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000873{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000874 char buf[NI_MAXHOST];
875 int error;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000876
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000877 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
878 NI_NUMERICHOST);
879 if (error) {
880 set_gaierror(error);
881 return NULL;
882 }
883 return PyUnicode_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000884}
885
886
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000887#ifdef USE_BLUETOOTH
888/* Convert a string representation of a Bluetooth address into a numeric
889 address. Returns the length (6), or raises an exception and returns -1 if
890 an error occurred. */
891
892static int
893setbdaddr(char *name, bdaddr_t *bdaddr)
894{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000895 unsigned int b0, b1, b2, b3, b4, b5;
896 char ch;
897 int n;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000898
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000899 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
900 &b5, &b4, &b3, &b2, &b1, &b0, &ch);
901 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
902 bdaddr->b[0] = b0;
903 bdaddr->b[1] = b1;
904 bdaddr->b[2] = b2;
905 bdaddr->b[3] = b3;
906 bdaddr->b[4] = b4;
907 bdaddr->b[5] = b5;
908 return 6;
909 } else {
910 PyErr_SetString(socket_error, "bad bluetooth address");
911 return -1;
912 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000913}
914
915/* Create a string representation of the Bluetooth address. This is always a
916 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
917 value (zero padded if necessary). */
918
919static PyObject *
920makebdaddr(bdaddr_t *bdaddr)
921{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000922 char buf[(6 * 2) + 5 + 1];
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000923
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000924 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
925 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
926 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
927 return PyUnicode_FromString(buf);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000928}
929#endif
930
931
Guido van Rossum30a685f1991-06-27 15:51:29 +0000932/* Create an object representing the given socket address,
933 suitable for passing it back to bind(), connect() etc.
934 The family field of the sockaddr structure is inspected
935 to determine what kind of address it really is. */
936
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000937/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000938static PyObject *
Antoine Pitrouf72006f2010-08-17 19:39:39 +0000939makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000940{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000941 if (addrlen == 0) {
942 /* No address -- may be recvfrom() from known socket */
943 Py_INCREF(Py_None);
944 return Py_None;
945 }
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000946
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000947 switch (addr->sa_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +0000948
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000949 case AF_INET:
950 {
951 struct sockaddr_in *a;
952 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
953 PyObject *ret = NULL;
954 if (addrobj) {
955 a = (struct sockaddr_in *)addr;
956 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
957 Py_DECREF(addrobj);
958 }
959 return ret;
960 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000961
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +0000962#if defined(AF_UNIX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000963 case AF_UNIX:
964 {
965 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000966#ifdef linux
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000967 if (a->sun_path[0] == 0) { /* Linux abstract namespace */
968 addrlen -= offsetof(struct sockaddr_un, sun_path);
969 return PyBytes_FromStringAndSize(a->sun_path, addrlen);
970 }
971 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000972#endif /* linux */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000973 {
974 /* regular NULL-terminated string */
975 return PyUnicode_FromString(a->sun_path);
976 }
977 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000978#endif /* AF_UNIX */
979
Martin v. Löwis11017b12006-01-14 18:12:57 +0000980#if defined(AF_NETLINK)
981 case AF_NETLINK:
982 {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000983 struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
984 return Py_BuildValue("II", a->nl_pid, a->nl_groups);
Martin v. Löwis11017b12006-01-14 18:12:57 +0000985 }
986#endif /* AF_NETLINK */
987
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000988#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000989 case AF_INET6:
990 {
991 struct sockaddr_in6 *a;
992 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
993 PyObject *ret = NULL;
994 if (addrobj) {
995 a = (struct sockaddr_in6 *)addr;
996 ret = Py_BuildValue("Oiii",
997 addrobj,
998 ntohs(a->sin6_port),
999 a->sin6_flowinfo,
1000 a->sin6_scope_id);
1001 Py_DECREF(addrobj);
1002 }
1003 return ret;
1004 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001005#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001006
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001007#ifdef USE_BLUETOOTH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001008 case AF_BLUETOOTH:
1009 switch (proto) {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001010
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001011 case BTPROTO_L2CAP:
1012 {
1013 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
1014 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
1015 PyObject *ret = NULL;
1016 if (addrobj) {
1017 ret = Py_BuildValue("Oi",
1018 addrobj,
1019 _BT_L2_MEMB(a, psm));
1020 Py_DECREF(addrobj);
1021 }
1022 return ret;
1023 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001024
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001025 case BTPROTO_RFCOMM:
1026 {
1027 struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
1028 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
1029 PyObject *ret = NULL;
1030 if (addrobj) {
1031 ret = Py_BuildValue("Oi",
1032 addrobj,
1033 _BT_RC_MEMB(a, channel));
1034 Py_DECREF(addrobj);
1035 }
1036 return ret;
1037 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001038
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001039 case BTPROTO_HCI:
1040 {
1041 struct sockaddr_hci *a = (struct sockaddr_hci *) addr;
1042 PyObject *ret = NULL;
1043 ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
1044 return ret;
1045 }
Thomas Wouterscf297e42007-02-23 15:07:44 +00001046
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001047#if !defined(__FreeBSD__)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001048 case BTPROTO_SCO:
1049 {
1050 struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
1051 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
1052 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001053#endif
1054
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001055 default:
1056 PyErr_SetString(PyExc_ValueError,
1057 "Unknown Bluetooth protocol");
1058 return NULL;
1059 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001060#endif
1061
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001062#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001063 case AF_PACKET:
1064 {
1065 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
1066 char *ifname = "";
1067 struct ifreq ifr;
1068 /* need to look up interface name give index */
1069 if (a->sll_ifindex) {
1070 ifr.ifr_ifindex = a->sll_ifindex;
1071 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
1072 ifname = ifr.ifr_name;
1073 }
1074 return Py_BuildValue("shbhy#",
1075 ifname,
1076 ntohs(a->sll_protocol),
1077 a->sll_pkttype,
1078 a->sll_hatype,
1079 a->sll_addr,
1080 a->sll_halen);
1081 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001082#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001083
Christian Heimes043d6f62008-01-07 17:19:16 +00001084#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001085 case AF_TIPC:
1086 {
1087 struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr;
1088 if (a->addrtype == TIPC_ADDR_NAMESEQ) {
1089 return Py_BuildValue("IIIII",
1090 a->addrtype,
1091 a->addr.nameseq.type,
1092 a->addr.nameseq.lower,
1093 a->addr.nameseq.upper,
1094 a->scope);
1095 } else if (a->addrtype == TIPC_ADDR_NAME) {
1096 return Py_BuildValue("IIIII",
1097 a->addrtype,
1098 a->addr.name.name.type,
1099 a->addr.name.name.instance,
1100 a->addr.name.name.instance,
1101 a->scope);
1102 } else if (a->addrtype == TIPC_ADDR_ID) {
1103 return Py_BuildValue("IIIII",
1104 a->addrtype,
1105 a->addr.id.node,
1106 a->addr.id.ref,
1107 0,
1108 a->scope);
1109 } else {
1110 PyErr_SetString(PyExc_ValueError,
1111 "Invalid address type");
1112 return NULL;
1113 }
1114 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001115#endif
1116
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001117 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001118
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001119 default:
1120 /* If we don't know the address family, don't raise an
1121 exception -- return it as an (int, bytes) tuple. */
1122 return Py_BuildValue("iy#",
1123 addr->sa_family,
1124 addr->sa_data,
1125 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001126
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001127 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001128}
1129
Guido van Rossum30a685f1991-06-27 15:51:29 +00001130
1131/* Parse a socket address argument according to the socket object's
1132 address family. Return 1 if the address was in the proper format,
1133 0 of not. The address is returned through addr_ret, its length
1134 through len_ret. */
1135
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001136static int
Guido van Rossum48a680c2001-03-02 06:34:14 +00001137getsockaddrarg(PySocketSockObject *s, PyObject *args,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001138 struct sockaddr *addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001139{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001140 switch (s->sock_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +00001141
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001142#if defined(AF_UNIX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001143 case AF_UNIX:
1144 {
1145 struct sockaddr_un* addr;
1146 char *path;
1147 int len;
1148 if (!PyArg_Parse(args, "s#", &path, &len))
1149 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001150
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001151 addr = (struct sockaddr_un*)addr_ret;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001152#ifdef linux
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001153 if (len > 0 && path[0] == 0) {
1154 /* Linux abstract namespace extension */
1155 if (len > sizeof addr->sun_path) {
1156 PyErr_SetString(socket_error,
1157 "AF_UNIX path too long");
1158 return 0;
1159 }
1160 }
1161 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001162#endif /* linux */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001163 {
1164 /* regular NULL-terminated string */
1165 if (len >= sizeof addr->sun_path) {
1166 PyErr_SetString(socket_error,
1167 "AF_UNIX path too long");
1168 return 0;
1169 }
1170 addr->sun_path[len] = 0;
1171 }
1172 addr->sun_family = s->sock_family;
1173 memcpy(addr->sun_path, path, len);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001174#if defined(PYOS_OS2)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001175 *len_ret = sizeof(*addr);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001176#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001177 *len_ret = len + offsetof(struct sockaddr_un, sun_path);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001178#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001179 return 1;
1180 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001181#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001182
Martin v. Löwis11017b12006-01-14 18:12:57 +00001183#if defined(AF_NETLINK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001184 case AF_NETLINK:
1185 {
1186 struct sockaddr_nl* addr;
1187 int pid, groups;
1188 addr = (struct sockaddr_nl *)addr_ret;
1189 if (!PyTuple_Check(args)) {
1190 PyErr_Format(
1191 PyExc_TypeError,
1192 "getsockaddrarg: "
1193 "AF_NETLINK address must be tuple, not %.500s",
1194 Py_TYPE(args)->tp_name);
1195 return 0;
1196 }
1197 if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
1198 return 0;
1199 addr->nl_family = AF_NETLINK;
1200 addr->nl_pid = pid;
1201 addr->nl_groups = groups;
1202 *len_ret = sizeof(*addr);
1203 return 1;
1204 }
Martin v. Löwis11017b12006-01-14 18:12:57 +00001205#endif
1206
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001207 case AF_INET:
1208 {
1209 struct sockaddr_in* addr;
1210 char *host;
1211 int port, result;
1212 if (!PyTuple_Check(args)) {
1213 PyErr_Format(
1214 PyExc_TypeError,
1215 "getsockaddrarg: "
1216 "AF_INET address must be tuple, not %.500s",
1217 Py_TYPE(args)->tp_name);
1218 return 0;
1219 }
1220 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
1221 "idna", &host, &port))
1222 return 0;
1223 addr=(struct sockaddr_in*)addr_ret;
1224 result = setipaddr(host, (struct sockaddr *)addr,
1225 sizeof(*addr), AF_INET);
1226 PyMem_Free(host);
1227 if (result < 0)
1228 return 0;
1229 if (port < 0 || port > 0xffff) {
1230 PyErr_SetString(
1231 PyExc_OverflowError,
1232 "getsockaddrarg: port must be 0-65535.");
1233 return 0;
1234 }
1235 addr->sin_family = AF_INET;
1236 addr->sin_port = htons((short)port);
1237 *len_ret = sizeof *addr;
1238 return 1;
1239 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001240
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001241#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001242 case AF_INET6:
1243 {
1244 struct sockaddr_in6* addr;
1245 char *host;
1246 int port, flowinfo, scope_id, result;
1247 flowinfo = scope_id = 0;
1248 if (!PyTuple_Check(args)) {
1249 PyErr_Format(
1250 PyExc_TypeError,
1251 "getsockaddrarg: "
1252 "AF_INET6 address must be tuple, not %.500s",
1253 Py_TYPE(args)->tp_name);
1254 return 0;
1255 }
1256 if (!PyArg_ParseTuple(args, "eti|ii",
1257 "idna", &host, &port, &flowinfo,
1258 &scope_id)) {
1259 return 0;
1260 }
1261 addr = (struct sockaddr_in6*)addr_ret;
1262 result = setipaddr(host, (struct sockaddr *)addr,
1263 sizeof(*addr), AF_INET6);
1264 PyMem_Free(host);
1265 if (result < 0)
1266 return 0;
1267 if (port < 0 || port > 0xffff) {
1268 PyErr_SetString(
1269 PyExc_OverflowError,
1270 "getsockaddrarg: port must be 0-65535.");
1271 return 0;
1272 }
1273 addr->sin6_family = s->sock_family;
1274 addr->sin6_port = htons((short)port);
1275 addr->sin6_flowinfo = flowinfo;
1276 addr->sin6_scope_id = scope_id;
1277 *len_ret = sizeof *addr;
1278 return 1;
1279 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001280#endif
1281
Hye-Shik Chang81268602004-02-02 06:05:24 +00001282#ifdef USE_BLUETOOTH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001283 case AF_BLUETOOTH:
1284 {
1285 switch (s->sock_proto) {
1286 case BTPROTO_L2CAP:
1287 {
1288 struct sockaddr_l2 *addr;
1289 char *straddr;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001290
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001291 addr = (struct sockaddr_l2 *)addr_ret;
1292 memset(addr, 0, sizeof(struct sockaddr_l2));
1293 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
1294 if (!PyArg_ParseTuple(args, "si", &straddr,
1295 &_BT_L2_MEMB(addr, psm))) {
1296 PyErr_SetString(socket_error, "getsockaddrarg: "
1297 "wrong format");
1298 return 0;
1299 }
1300 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
1301 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001302
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001303 *len_ret = sizeof *addr;
1304 return 1;
1305 }
1306 case BTPROTO_RFCOMM:
1307 {
1308 struct sockaddr_rc *addr;
1309 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001310
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001311 addr = (struct sockaddr_rc *)addr_ret;
1312 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
1313 if (!PyArg_ParseTuple(args, "si", &straddr,
1314 &_BT_RC_MEMB(addr, channel))) {
1315 PyErr_SetString(socket_error, "getsockaddrarg: "
1316 "wrong format");
1317 return 0;
1318 }
1319 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
1320 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001321
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001322 *len_ret = sizeof *addr;
1323 return 1;
1324 }
1325 case BTPROTO_HCI:
1326 {
1327 struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret;
1328 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
1329 if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) {
1330 PyErr_SetString(socket_error, "getsockaddrarg: "
1331 "wrong format");
1332 return 0;
1333 }
1334 *len_ret = sizeof *addr;
1335 return 1;
1336 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001337#if !defined(__FreeBSD__)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001338 case BTPROTO_SCO:
1339 {
1340 struct sockaddr_sco *addr;
1341 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001342
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001343 addr = (struct sockaddr_sco *)addr_ret;
1344 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
1345 if (!PyBytes_Check(args)) {
1346 PyErr_SetString(socket_error, "getsockaddrarg: "
1347 "wrong format");
1348 return 0;
1349 }
1350 straddr = PyBytes_AS_STRING(args);
1351 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
1352 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001353
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001354 *len_ret = sizeof *addr;
1355 return 1;
1356 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001357#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001358 default:
1359 PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol");
1360 return 0;
1361 }
1362 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001363#endif
1364
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001365#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001366 case AF_PACKET:
1367 {
1368 struct sockaddr_ll* addr;
1369 struct ifreq ifr;
1370 char *interfaceName;
1371 int protoNumber;
1372 int hatype = 0;
1373 int pkttype = 0;
1374 char *haddr = NULL;
1375 unsigned int halen = 0;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001376
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001377 if (!PyTuple_Check(args)) {
1378 PyErr_Format(
1379 PyExc_TypeError,
1380 "getsockaddrarg: "
1381 "AF_PACKET address must be tuple, not %.500s",
1382 Py_TYPE(args)->tp_name);
1383 return 0;
1384 }
1385 if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName,
1386 &protoNumber, &pkttype, &hatype,
1387 &haddr, &halen))
1388 return 0;
1389 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1390 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
1391 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
1392 s->errorhandler();
1393 return 0;
1394 }
1395 if (halen > 8) {
1396 PyErr_SetString(PyExc_ValueError,
1397 "Hardware address must be 8 bytes or less");
1398 return 0;
1399 }
1400 if (protoNumber < 0 || protoNumber > 0xffff) {
1401 PyErr_SetString(
1402 PyExc_OverflowError,
1403 "getsockaddrarg: protoNumber must be 0-65535.");
1404 return 0;
1405 }
1406 addr = (struct sockaddr_ll*)addr_ret;
1407 addr->sll_family = AF_PACKET;
1408 addr->sll_protocol = htons((short)protoNumber);
1409 addr->sll_ifindex = ifr.ifr_ifindex;
1410 addr->sll_pkttype = pkttype;
1411 addr->sll_hatype = hatype;
1412 if (halen != 0) {
1413 memcpy(&addr->sll_addr, haddr, halen);
1414 }
1415 addr->sll_halen = halen;
1416 *len_ret = sizeof *addr;
1417 return 1;
1418 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001419#endif
1420
Christian Heimes043d6f62008-01-07 17:19:16 +00001421#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001422 case AF_TIPC:
1423 {
1424 unsigned int atype, v1, v2, v3;
1425 unsigned int scope = TIPC_CLUSTER_SCOPE;
1426 struct sockaddr_tipc *addr;
Christian Heimes043d6f62008-01-07 17:19:16 +00001427
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001428 if (!PyTuple_Check(args)) {
1429 PyErr_Format(
1430 PyExc_TypeError,
1431 "getsockaddrarg: "
1432 "AF_TIPC address must be tuple, not %.500s",
1433 Py_TYPE(args)->tp_name);
1434 return 0;
1435 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001436
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001437 if (!PyArg_ParseTuple(args,
1438 "IIII|I;Invalid TIPC address format",
1439 &atype, &v1, &v2, &v3, &scope))
1440 return 0;
Christian Heimes043d6f62008-01-07 17:19:16 +00001441
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001442 addr = (struct sockaddr_tipc *) addr_ret;
1443 memset(addr, 0, sizeof(struct sockaddr_tipc));
Christian Heimes043d6f62008-01-07 17:19:16 +00001444
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001445 addr->family = AF_TIPC;
1446 addr->scope = scope;
1447 addr->addrtype = atype;
Christian Heimes043d6f62008-01-07 17:19:16 +00001448
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001449 if (atype == TIPC_ADDR_NAMESEQ) {
1450 addr->addr.nameseq.type = v1;
1451 addr->addr.nameseq.lower = v2;
1452 addr->addr.nameseq.upper = v3;
1453 } else if (atype == TIPC_ADDR_NAME) {
1454 addr->addr.name.name.type = v1;
1455 addr->addr.name.name.instance = v2;
1456 } else if (atype == TIPC_ADDR_ID) {
1457 addr->addr.id.node = v1;
1458 addr->addr.id.ref = v2;
1459 } else {
1460 /* Shouldn't happen */
1461 PyErr_SetString(PyExc_TypeError, "Invalid address type");
1462 return 0;
1463 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001464
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001465 *len_ret = sizeof(*addr);
Christian Heimes043d6f62008-01-07 17:19:16 +00001466
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001467 return 1;
1468 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001469#endif
1470
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001471 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001472
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001473 default:
1474 PyErr_SetString(socket_error, "getsockaddrarg: bad family");
1475 return 0;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001476
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001477 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001478}
1479
Guido van Rossum30a685f1991-06-27 15:51:29 +00001480
Guido van Rossum48a680c2001-03-02 06:34:14 +00001481/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00001482 Return 1 if the family is known, 0 otherwise. The length is returned
1483 through len_ret. */
1484
1485static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00001486getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001487{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001488 switch (s->sock_family) {
Guido van Rossum710e1df1992-06-12 10:39:36 +00001489
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001490#if defined(AF_UNIX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001491 case AF_UNIX:
1492 {
1493 *len_ret = sizeof (struct sockaddr_un);
1494 return 1;
1495 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001496#endif /* AF_UNIX */
Martin v. Löwis11017b12006-01-14 18:12:57 +00001497#if defined(AF_NETLINK)
1498 case AF_NETLINK:
1499 {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001500 *len_ret = sizeof (struct sockaddr_nl);
1501 return 1;
Martin v. Löwis11017b12006-01-14 18:12:57 +00001502 }
1503#endif
Guido van Rossum710e1df1992-06-12 10:39:36 +00001504
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001505 case AF_INET:
1506 {
1507 *len_ret = sizeof (struct sockaddr_in);
1508 return 1;
1509 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00001510
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001511#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001512 case AF_INET6:
1513 {
1514 *len_ret = sizeof (struct sockaddr_in6);
1515 return 1;
1516 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001517#endif
1518
Hye-Shik Chang81268602004-02-02 06:05:24 +00001519#ifdef USE_BLUETOOTH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001520 case AF_BLUETOOTH:
1521 {
1522 switch(s->sock_proto)
1523 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001524
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001525 case BTPROTO_L2CAP:
1526 *len_ret = sizeof (struct sockaddr_l2);
1527 return 1;
1528 case BTPROTO_RFCOMM:
1529 *len_ret = sizeof (struct sockaddr_rc);
1530 return 1;
1531 case BTPROTO_HCI:
1532 *len_ret = sizeof (struct sockaddr_hci);
1533 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001534#if !defined(__FreeBSD__)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001535 case BTPROTO_SCO:
1536 *len_ret = sizeof (struct sockaddr_sco);
1537 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001538#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001539 default:
1540 PyErr_SetString(socket_error, "getsockaddrlen: "
1541 "unknown BT protocol");
1542 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001543
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001544 }
1545 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001546#endif
1547
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001548#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001549 case AF_PACKET:
1550 {
1551 *len_ret = sizeof (struct sockaddr_ll);
1552 return 1;
1553 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001554#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001555
Christian Heimes043d6f62008-01-07 17:19:16 +00001556#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001557 case AF_TIPC:
1558 {
1559 *len_ret = sizeof (struct sockaddr_tipc);
1560 return 1;
1561 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001562#endif
1563
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001564 /* More cases here... */
Guido van Rossum710e1df1992-06-12 10:39:36 +00001565
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001566 default:
1567 PyErr_SetString(socket_error, "getsockaddrlen: bad family");
1568 return 0;
Guido van Rossum710e1df1992-06-12 10:39:36 +00001569
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001570 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00001571}
1572
1573
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001574/* s._accept() -> (fd, address) */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001575
Guido van Rossum73624e91994-10-10 17:59:00 +00001576static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001577sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001578{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001579 sock_addr_t addrbuf;
1580 SOCKET_T newfd = INVALID_SOCKET;
1581 socklen_t addrlen;
1582 PyObject *sock = NULL;
1583 PyObject *addr = NULL;
1584 PyObject *res = NULL;
1585 int timeout;
Barry Warsaw752300b1997-01-03 17:18:10 +00001586
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001587 if (!getsockaddrlen(s, &addrlen))
1588 return NULL;
1589 memset(&addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001590
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001591 if (!IS_SELECTABLE(s))
1592 return select_error();
Neal Norwitz082b2df2006-02-07 07:04:46 +00001593
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001594 Py_BEGIN_ALLOW_THREADS
1595 timeout = internal_select(s, 0);
1596 if (!timeout)
1597 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
1598 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001599
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001600 if (timeout == 1) {
1601 PyErr_SetString(socket_timeout, "timed out");
1602 return NULL;
1603 }
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001604
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001605 if (newfd == INVALID_SOCKET)
1606 return s->errorhandler();
Barry Warsaw752300b1997-01-03 17:18:10 +00001607
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001608 sock = PyLong_FromSocket_t(newfd);
1609 if (sock == NULL) {
1610 SOCKETCLOSE(newfd);
1611 goto finally;
1612 }
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001613
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001614 addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
1615 addrlen, s->sock_proto);
1616 if (addr == NULL)
1617 goto finally;
Barry Warsaw752300b1997-01-03 17:18:10 +00001618
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001619 res = PyTuple_Pack(2, sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001620
Guido van Rossum67f7a382002-06-06 21:08:16 +00001621finally:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001622 Py_XDECREF(sock);
1623 Py_XDECREF(addr);
1624 return res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001625}
1626
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001627PyDoc_STRVAR(accept_doc,
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001628"_accept() -> (integer, address info)\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001629\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001630Wait for an incoming connection. Return a new socket file descriptor\n\
1631representing the connection, and the address of the client.\n\
1632For IP sockets, the address info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001633
Guido van Rossum11ba0942002-06-13 15:07:44 +00001634/* s.setblocking(flag) method. Argument:
1635 False -- non-blocking mode; same as settimeout(0)
1636 True -- blocking mode; same as settimeout(None)
1637*/
Guido van Rossume4485b01994-09-07 14:32:49 +00001638
Guido van Rossum73624e91994-10-10 17:59:00 +00001639static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001640sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00001641{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001642 int block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001643
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001644 block = PyLong_AsLong(arg);
1645 if (block == -1 && PyErr_Occurred())
1646 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001647
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001648 s->sock_timeout = block ? -1.0 : 0.0;
1649 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00001650
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001651 Py_INCREF(Py_None);
1652 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00001653}
Guido van Rossume4485b01994-09-07 14:32:49 +00001654
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001655PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001656"setblocking(flag)\n\
1657\n\
1658Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001659setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001660setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001661
Guido van Rossum11ba0942002-06-13 15:07:44 +00001662/* s.settimeout(timeout) method. Argument:
1663 None -- no timeout, blocking mode; same as setblocking(True)
1664 0.0 -- non-blocking mode; same as setblocking(False)
1665 > 0 -- timeout mode; operations time out after timeout seconds
1666 < 0 -- illegal; raises an exception
1667*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00001668static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001669sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001670{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001671 double timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001672
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001673 if (arg == Py_None)
1674 timeout = -1.0;
1675 else {
1676 timeout = PyFloat_AsDouble(arg);
1677 if (timeout < 0.0) {
1678 if (!PyErr_Occurred())
1679 PyErr_SetString(PyExc_ValueError,
1680 "Timeout value out of range");
1681 return NULL;
1682 }
1683 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001684
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001685 s->sock_timeout = timeout;
1686 internal_setblocking(s, timeout < 0.0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001687
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001688 Py_INCREF(Py_None);
1689 return Py_None;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001690}
1691
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001692PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001693"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001694\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001695Set a timeout on socket operations. 'timeout' can be a float,\n\
1696giving in seconds, or None. Setting a timeout of None disables\n\
1697the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001698Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001699
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001700/* s.gettimeout() method.
1701 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001702static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001703sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001704{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001705 if (s->sock_timeout < 0.0) {
1706 Py_INCREF(Py_None);
1707 return Py_None;
1708 }
1709 else
1710 return PyFloat_FromDouble(s->sock_timeout);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001711}
1712
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001713PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001714"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001715\n\
1716Returns the timeout in floating seconds associated with socket \n\
1717operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001718operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00001719
Guido van Rossumaee08791992-09-08 09:05:33 +00001720/* s.setsockopt() method.
1721 With an integer third argument, sets an integer option.
1722 With a string third argument, sets an option from a buffer;
1723 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001724
Guido van Rossum73624e91994-10-10 17:59:00 +00001725static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001726sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001727{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001728 int level;
1729 int optname;
1730 int res;
1731 char *buf;
1732 int buflen;
1733 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001734
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001735 if (PyArg_ParseTuple(args, "iii:setsockopt",
1736 &level, &optname, &flag)) {
1737 buf = (char *) &flag;
1738 buflen = sizeof flag;
1739 }
1740 else {
1741 PyErr_Clear();
1742 if (!PyArg_ParseTuple(args, "iiy#:setsockopt",
1743 &level, &optname, &buf, &buflen))
1744 return NULL;
1745 }
1746 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
1747 if (res < 0)
1748 return s->errorhandler();
1749 Py_INCREF(Py_None);
1750 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001751}
1752
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001753PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001754"setsockopt(level, option, value)\n\
1755\n\
1756Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001757The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001758
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001759
Guido van Rossumaee08791992-09-08 09:05:33 +00001760/* s.getsockopt() method.
1761 With two arguments, retrieves an integer option.
1762 With a third integer argument, retrieves a string buffer of that size;
1763 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001764
Guido van Rossum73624e91994-10-10 17:59:00 +00001765static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001766sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001767{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001768 int level;
1769 int optname;
1770 int res;
1771 PyObject *buf;
1772 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001773
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001774 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
1775 &level, &optname, &buflen))
1776 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001777
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001778 if (buflen == 0) {
1779 int flag = 0;
1780 socklen_t flagsize = sizeof flag;
1781 res = getsockopt(s->sock_fd, level, optname,
1782 (void *)&flag, &flagsize);
1783 if (res < 0)
1784 return s->errorhandler();
1785 return PyLong_FromLong(flag);
1786 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001787#ifdef __VMS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001788 /* socklen_t is unsigned so no negative test is needed,
1789 test buflen == 0 is previously done */
1790 if (buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001791#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001792 if (buflen <= 0 || buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001793#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001794 PyErr_SetString(socket_error,
1795 "getsockopt buflen out of range");
1796 return NULL;
1797 }
1798 buf = PyBytes_FromStringAndSize((char *)NULL, buflen);
1799 if (buf == NULL)
1800 return NULL;
1801 res = getsockopt(s->sock_fd, level, optname,
1802 (void *)PyBytes_AS_STRING(buf), &buflen);
1803 if (res < 0) {
1804 Py_DECREF(buf);
1805 return s->errorhandler();
1806 }
1807 _PyBytes_Resize(&buf, buflen);
1808 return buf;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001809}
1810
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001811PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001812"getsockopt(level, option[, buffersize]) -> value\n\
1813\n\
1814Get a socket option. See the Unix manual for level and option.\n\
1815If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001816string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001817
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001818
Fred Drake728819a2000-07-01 03:40:12 +00001819/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001820
Guido van Rossum73624e91994-10-10 17:59:00 +00001821static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001822sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001823{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001824 sock_addr_t addrbuf;
1825 int addrlen;
1826 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001827
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001828 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
1829 return NULL;
1830 Py_BEGIN_ALLOW_THREADS
1831 res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);
1832 Py_END_ALLOW_THREADS
1833 if (res < 0)
1834 return s->errorhandler();
1835 Py_INCREF(Py_None);
1836 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001837}
1838
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001839PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001840"bind(address)\n\
1841\n\
1842Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00001843pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001844sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001845
Guido van Rossum30a685f1991-06-27 15:51:29 +00001846
1847/* s.close() method.
1848 Set the file descriptor to -1 so operations tried subsequently
1849 will surely fail. */
1850
Guido van Rossum73624e91994-10-10 17:59:00 +00001851static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001852sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001853{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001854 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001855
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001856 if ((fd = s->sock_fd) != -1) {
1857 s->sock_fd = -1;
1858 Py_BEGIN_ALLOW_THREADS
1859 (void) SOCKETCLOSE(fd);
1860 Py_END_ALLOW_THREADS
1861 }
1862 Py_INCREF(Py_None);
1863 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001864}
1865
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001866PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001867"close()\n\
1868\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001869Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001870
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001871static int
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001872internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001873 int *timeoutp)
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001874{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001875 int res, timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001876
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001877 timeout = 0;
1878 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001879
1880#ifdef MS_WINDOWS
1881
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001882 if (s->sock_timeout > 0.0) {
1883 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK &&
1884 IS_SELECTABLE(s)) {
1885 /* This is a mess. Best solution: trust select */
1886 fd_set fds;
1887 fd_set fds_exc;
1888 struct timeval tv;
1889 tv.tv_sec = (int)s->sock_timeout;
1890 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
1891 FD_ZERO(&fds);
1892 FD_SET(s->sock_fd, &fds);
1893 FD_ZERO(&fds_exc);
1894 FD_SET(s->sock_fd, &fds_exc);
Antoine Pitrouf72006f2010-08-17 19:39:39 +00001895 res = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
1896 NULL, &fds, &fds_exc, &tv);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001897 if (res == 0) {
1898 res = WSAEWOULDBLOCK;
1899 timeout = 1;
1900 } else if (res > 0) {
1901 if (FD_ISSET(s->sock_fd, &fds))
1902 /* The socket is in the writable set - this
1903 means connected */
1904 res = 0;
1905 else {
1906 /* As per MS docs, we need to call getsockopt()
1907 to get the underlying error */
1908 int res_size = sizeof res;
1909 /* It must be in the exception set */
1910 assert(FD_ISSET(s->sock_fd, &fds_exc));
1911 if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
1912 (char *)&res, &res_size))
1913 /* getsockopt also clears WSAGetLastError,
1914 so reset it back. */
1915 WSASetLastError(res);
1916 else
1917 res = WSAGetLastError();
1918 }
1919 }
1920 /* else if (res < 0) an error occurred */
1921 }
1922 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001923
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001924 if (res < 0)
1925 res = WSAGetLastError();
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001926
1927#else
1928
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001929 if (s->sock_timeout > 0.0) {
1930 if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) {
1931 timeout = internal_select(s, 1);
1932 if (timeout == 0) {
1933 /* Bug #1019808: in case of an EINPROGRESS,
1934 use getsockopt(SO_ERROR) to get the real
1935 error. */
1936 socklen_t res_size = sizeof res;
1937 (void)getsockopt(s->sock_fd, SOL_SOCKET,
1938 SO_ERROR, &res, &res_size);
1939 if (res == EISCONN)
1940 res = 0;
1941 errno = res;
1942 }
1943 else if (timeout == -1) {
1944 res = errno; /* had error */
1945 }
1946 else
1947 res = EWOULDBLOCK; /* timed out */
1948 }
1949 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001950
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001951 if (res < 0)
1952 res = errno;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001953
1954#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001955 *timeoutp = timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001956
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001957 return res;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001958}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001959
Fred Drake728819a2000-07-01 03:40:12 +00001960/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001961
Guido van Rossum73624e91994-10-10 17:59:00 +00001962static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001963sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001964{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001965 sock_addr_t addrbuf;
1966 int addrlen;
1967 int res;
1968 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001969
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001970 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
1971 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001972
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001973 Py_BEGIN_ALLOW_THREADS
1974 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
1975 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001976
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001977 if (timeout == 1) {
1978 PyErr_SetString(socket_timeout, "timed out");
1979 return NULL;
1980 }
1981 if (res != 0)
1982 return s->errorhandler();
1983 Py_INCREF(Py_None);
1984 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001985}
1986
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001987PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001988"connect(address)\n\
1989\n\
1990Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001991is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001992
Guido van Rossum30a685f1991-06-27 15:51:29 +00001993
Fred Drake728819a2000-07-01 03:40:12 +00001994/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001995
1996static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001997sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001998{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001999 sock_addr_t addrbuf;
2000 int addrlen;
2001 int res;
2002 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002003
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002004 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2005 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002006
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002007 Py_BEGIN_ALLOW_THREADS
2008 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
2009 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002010
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002011 /* Signals are not errors (though they may raise exceptions). Adapted
2012 from PyErr_SetFromErrnoWithFilenameObject(). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002013#ifdef EINTR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002014 if (res == EINTR && PyErr_CheckSignals())
2015 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002016#endif
2017
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002018 return PyLong_FromLong((long) res);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002019}
2020
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002021PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002022"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002023\n\
2024This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002025instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002026
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002027
Guido van Rossumed233a51992-06-23 09:07:03 +00002028/* s.fileno() method */
2029
Guido van Rossum73624e91994-10-10 17:59:00 +00002030static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002031sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00002032{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002033 return PyLong_FromSocket_t(s->sock_fd);
Guido van Rossumed233a51992-06-23 09:07:03 +00002034}
2035
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002036PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002037"fileno() -> integer\n\
2038\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002039Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002040
Guido van Rossumed233a51992-06-23 09:07:03 +00002041
Guido van Rossumc89705d1992-11-26 08:54:07 +00002042/* s.getsockname() method */
2043
Guido van Rossum73624e91994-10-10 17:59:00 +00002044static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002045sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002046{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002047 sock_addr_t addrbuf;
2048 int res;
2049 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002050
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002051 if (!getsockaddrlen(s, &addrlen))
2052 return NULL;
2053 memset(&addrbuf, 0, addrlen);
2054 Py_BEGIN_ALLOW_THREADS
2055 res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2056 Py_END_ALLOW_THREADS
2057 if (res < 0)
2058 return s->errorhandler();
2059 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2060 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002061}
2062
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002063PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002064"getsockname() -> address info\n\
2065\n\
2066Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002067info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002068
Guido van Rossumc89705d1992-11-26 08:54:07 +00002069
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002070#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002071/* s.getpeername() method */
2072
Guido van Rossum73624e91994-10-10 17:59:00 +00002073static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002074sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002075{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002076 sock_addr_t addrbuf;
2077 int res;
2078 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002079
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002080 if (!getsockaddrlen(s, &addrlen))
2081 return NULL;
2082 memset(&addrbuf, 0, addrlen);
2083 Py_BEGIN_ALLOW_THREADS
2084 res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2085 Py_END_ALLOW_THREADS
2086 if (res < 0)
2087 return s->errorhandler();
2088 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2089 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002090}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002091
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002092PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002093"getpeername() -> address info\n\
2094\n\
2095Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002096info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002097
Guido van Rossumb6775db1994-08-01 11:34:53 +00002098#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002099
2100
Guido van Rossum30a685f1991-06-27 15:51:29 +00002101/* s.listen(n) method */
2102
Guido van Rossum73624e91994-10-10 17:59:00 +00002103static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002104sock_listen(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002105{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002106 int backlog;
2107 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002108
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002109 backlog = PyLong_AsLong(arg);
2110 if (backlog == -1 && PyErr_Occurred())
2111 return NULL;
2112 Py_BEGIN_ALLOW_THREADS
2113 if (backlog < 1)
2114 backlog = 1;
2115 res = listen(s->sock_fd, backlog);
2116 Py_END_ALLOW_THREADS
2117 if (res < 0)
2118 return s->errorhandler();
2119 Py_INCREF(Py_None);
2120 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002121}
2122
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002123PyDoc_STRVAR(listen_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002124"listen(backlog)\n\
2125\n\
2126Enable a server to accept connections. The backlog argument must be at\n\
2127least 1; it specifies the number of unaccepted connection that the system\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002128will allow before refusing new connections.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002129
2130
Thomas Wouters477c8d52006-05-27 19:21:47 +00002131/*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002132 * This is the guts of the recv() and recv_into() methods, which reads into a
Thomas Wouters902d6eb2007-01-09 23:18:33 +00002133 * char buffer. If you have any inc/dec ref to do to the objects that contain
Thomas Wouters477c8d52006-05-27 19:21:47 +00002134 * the buffer, do it in the caller. This function returns the number of bytes
2135 * succesfully read. If there was an error, it returns -1. Note that it is
2136 * also possible that we return a number of bytes smaller than the request
2137 * bytes.
2138 */
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002139static Py_ssize_t
2140sock_recv_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002141{
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002142 Py_ssize_t outlen = -1;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002143 int timeout;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002144#ifdef __VMS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002145 int remaining;
2146 char *read_buf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002147#endif
2148
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002149 if (!IS_SELECTABLE(s)) {
2150 select_error();
2151 return -1;
2152 }
2153 if (len == 0) {
2154 /* If 0 bytes were requested, do nothing. */
2155 return 0;
2156 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002157
2158#ifndef __VMS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002159 Py_BEGIN_ALLOW_THREADS
2160 timeout = internal_select(s, 0);
2161 if (!timeout)
2162 outlen = recv(s->sock_fd, cbuf, len, flags);
2163 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002164
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002165 if (timeout == 1) {
2166 PyErr_SetString(socket_timeout, "timed out");
2167 return -1;
2168 }
2169 if (outlen < 0) {
2170 /* Note: the call to errorhandler() ALWAYS indirectly returned
2171 NULL, so ignore its return value */
2172 s->errorhandler();
2173 return -1;
2174 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002175#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002176 read_buf = cbuf;
2177 remaining = len;
2178 while (remaining != 0) {
2179 unsigned int segment;
2180 int nread = -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002181
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002182 segment = remaining /SEGMENT_SIZE;
2183 if (segment != 0) {
2184 segment = SEGMENT_SIZE;
2185 }
2186 else {
2187 segment = remaining;
2188 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002189
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002190 Py_BEGIN_ALLOW_THREADS
2191 timeout = internal_select(s, 0);
2192 if (!timeout)
2193 nread = recv(s->sock_fd, read_buf, segment, flags);
2194 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002195
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002196 if (timeout == 1) {
2197 PyErr_SetString(socket_timeout, "timed out");
2198 return -1;
2199 }
2200 if (nread < 0) {
2201 s->errorhandler();
2202 return -1;
2203 }
2204 if (nread != remaining) {
2205 read_buf += nread;
2206 break;
2207 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002208
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002209 remaining -= segment;
2210 read_buf += segment;
2211 }
2212 outlen = read_buf - cbuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002213#endif /* !__VMS */
2214
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002215 return outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002216}
2217
Guido van Rossum48a680c2001-03-02 06:34:14 +00002218
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002219/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002220
Guido van Rossum73624e91994-10-10 17:59:00 +00002221static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002222sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002223{
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002224 Py_ssize_t recvlen, outlen;
2225 int flags = 0;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002226 PyObject *buf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002227
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002228 if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002229 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002230
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002231 if (recvlen < 0) {
2232 PyErr_SetString(PyExc_ValueError,
2233 "negative buffersize in recv");
2234 return NULL;
2235 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002236
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002237 /* Allocate a new string. */
2238 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2239 if (buf == NULL)
2240 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002241
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002242 /* Call the guts */
2243 outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags);
2244 if (outlen < 0) {
2245 /* An error occurred, release the string and return an
2246 error. */
2247 Py_DECREF(buf);
2248 return NULL;
2249 }
2250 if (outlen != recvlen) {
2251 /* We did not read as many bytes as we anticipated, resize the
2252 string if possible and be successful. */
2253 _PyBytes_Resize(&buf, outlen);
2254 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002255
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002256 return buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002257}
2258
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002259PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002260"recv(buffersize[, flags]) -> data\n\
2261\n\
2262Receive up to buffersize bytes from the socket. For the optional flags\n\
2263argument, see the Unix manual. When no data is available, block until\n\
2264at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002265the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002266
Guido van Rossum30a685f1991-06-27 15:51:29 +00002267
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002268/* s.recv_into(buffer, [nbytes [,flags]]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002269
Thomas Wouters477c8d52006-05-27 19:21:47 +00002270static PyObject*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002271sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002272{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002273 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00002274
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002275 int flags = 0;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002276 Py_buffer pbuf;
2277 char *buf;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002278 Py_ssize_t buflen, readlen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002279
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002280 /* Get the buffer's memory */
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002281 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002282 &pbuf, &recvlen, &flags))
2283 return NULL;
2284 buf = pbuf.buf;
2285 buflen = pbuf.len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002286
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002287 if (recvlen < 0) {
2288 PyBuffer_Release(&pbuf);
2289 PyErr_SetString(PyExc_ValueError,
2290 "negative buffersize in recv_into");
2291 return NULL;
2292 }
2293 if (recvlen == 0) {
2294 /* If nbytes was not specified, use the buffer's length */
2295 recvlen = buflen;
2296 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002297
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002298 /* Check if the buffer is large enough */
2299 if (buflen < recvlen) {
2300 PyBuffer_Release(&pbuf);
2301 PyErr_SetString(PyExc_ValueError,
2302 "buffer too small for requested bytes");
2303 return NULL;
2304 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002305
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002306 /* Call the guts */
2307 readlen = sock_recv_guts(s, buf, recvlen, flags);
2308 if (readlen < 0) {
2309 /* Return an error. */
2310 PyBuffer_Release(&pbuf);
2311 return NULL;
2312 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002313
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002314 PyBuffer_Release(&pbuf);
2315 /* Return the number of bytes read. Note that we do not do anything
2316 special here in the case that readlen < recvlen. */
2317 return PyLong_FromSsize_t(readlen);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002318}
2319
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002320PyDoc_STRVAR(recv_into_doc,
2321"recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002322\n\
2323A version of recv() that stores its data into a buffer rather than creating \n\
2324a new string. Receive up to buffersize bytes from the socket. If buffersize \n\
2325is not specified (or 0), receive up to the size available in the given buffer.\n\
2326\n\
2327See recv() for documentation about the flags.");
2328
2329
2330/*
Christian Heimes99170a52007-12-19 02:07:34 +00002331 * This is the guts of the recvfrom() and recvfrom_into() methods, which reads
2332 * into a char buffer. If you have any inc/def ref to do to the objects that
2333 * contain the buffer, do it in the caller. This function returns the number
2334 * of bytes succesfully read. If there was an error, it returns -1. Note
2335 * that it is also possible that we return a number of bytes smaller than the
2336 * request bytes.
Thomas Wouters477c8d52006-05-27 19:21:47 +00002337 *
2338 * 'addr' is a return value for the address object. Note that you must decref
2339 * it yourself.
2340 */
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002341static Py_ssize_t
2342sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002343 PyObject** addr)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002344{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002345 sock_addr_t addrbuf;
2346 int timeout;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002347 Py_ssize_t n = -1;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002348 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002349
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002350 *addr = NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002351
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002352 if (!getsockaddrlen(s, &addrlen))
2353 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002354
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002355 if (!IS_SELECTABLE(s)) {
2356 select_error();
2357 return -1;
2358 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002359
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002360 Py_BEGIN_ALLOW_THREADS
2361 memset(&addrbuf, 0, addrlen);
2362 timeout = internal_select(s, 0);
2363 if (!timeout) {
Guido van Rossum8d665e61996-06-26 18:22:49 +00002364#ifndef MS_WINDOWS
Andrew MacIntyreba43e872002-03-03 03:03:52 +00002365#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002366 n = recvfrom(s->sock_fd, cbuf, len, flags,
2367 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum32c575d1997-12-02 20:37:32 +00002368#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002369 n = recvfrom(s->sock_fd, cbuf, len, flags,
2370 (void *) &addrbuf, &addrlen);
Guido van Rossum32c575d1997-12-02 20:37:32 +00002371#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002372#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002373 n = recvfrom(s->sock_fd, cbuf, len, flags,
2374 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002375#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002376 }
2377 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002378
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002379 if (timeout == 1) {
2380 PyErr_SetString(socket_timeout, "timed out");
2381 return -1;
2382 }
2383 if (n < 0) {
2384 s->errorhandler();
2385 return -1;
2386 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002387
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002388 if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
2389 addrlen, s->sock_proto)))
2390 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002391
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002392 return n;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002393}
2394
2395/* s.recvfrom(nbytes [,flags]) method */
2396
2397static PyObject *
2398sock_recvfrom(PySocketSockObject *s, PyObject *args)
2399{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002400 PyObject *buf = NULL;
2401 PyObject *addr = NULL;
2402 PyObject *ret = NULL;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002403 int flags = 0;
2404 Py_ssize_t recvlen, outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002405
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002406 if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002407 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002408
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002409 if (recvlen < 0) {
2410 PyErr_SetString(PyExc_ValueError,
2411 "negative buffersize in recvfrom");
2412 return NULL;
2413 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002414
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002415 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2416 if (buf == NULL)
2417 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002418
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002419 outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf),
2420 recvlen, flags, &addr);
2421 if (outlen < 0) {
2422 goto finally;
2423 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002424
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002425 if (outlen != recvlen) {
2426 /* We did not read as many bytes as we anticipated, resize the
2427 string if possible and be succesful. */
2428 if (_PyBytes_Resize(&buf, outlen) < 0)
2429 /* Oopsy, not so succesful after all. */
2430 goto finally;
2431 }
Barry Warsaw752300b1997-01-03 17:18:10 +00002432
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002433 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002434
2435finally:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002436 Py_XDECREF(buf);
2437 Py_XDECREF(addr);
2438 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002439}
2440
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002441PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002442"recvfrom(buffersize[, flags]) -> (data, address info)\n\
2443\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002444Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002445
Thomas Wouters477c8d52006-05-27 19:21:47 +00002446
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002447/* s.recvfrom_into(buffer[, nbytes [,flags]]) method */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002448
2449static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002450sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002451{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002452 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00002453
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002454 int flags = 0;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002455 Py_buffer pbuf;
2456 char *buf;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002457 Py_ssize_t readlen, buflen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002458
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002459 PyObject *addr = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002460
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002461 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002462 kwlist, &pbuf,
2463 &recvlen, &flags))
2464 return NULL;
2465 buf = pbuf.buf;
2466 buflen = pbuf.len;
2467 assert(buf != 0 && buflen > 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002468
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002469 if (recvlen < 0) {
2470 PyBuffer_Release(&pbuf);
2471 PyErr_SetString(PyExc_ValueError,
2472 "negative buffersize in recvfrom_into");
2473 return NULL;
2474 }
2475 if (recvlen == 0) {
2476 /* If nbytes was not specified, use the buffer's length */
2477 recvlen = buflen;
2478 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002479
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002480 readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
2481 if (readlen < 0) {
2482 PyBuffer_Release(&pbuf);
2483 /* Return an error */
2484 Py_XDECREF(addr);
2485 return NULL;
2486 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002487
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002488 PyBuffer_Release(&pbuf);
2489 /* Return the number of bytes read and the address. Note that we do
2490 not do anything special here in the case that readlen < recvlen. */
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002491 return Py_BuildValue("nN", readlen, addr);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002492}
2493
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002494PyDoc_STRVAR(recvfrom_into_doc,
2495"recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002496\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002497Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002498
2499
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002500/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002501
Guido van Rossum73624e91994-10-10 17:59:00 +00002502static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002503sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002504{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002505 char *buf;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002506 Py_ssize_t len, n = -1;
2507 int flags = 0, timeout;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002508 Py_buffer pbuf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002509
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002510 if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags))
2511 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002512
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002513 if (!IS_SELECTABLE(s)) {
2514 PyBuffer_Release(&pbuf);
2515 return select_error();
2516 }
2517 buf = pbuf.buf;
2518 len = pbuf.len;
Neal Norwitz082b2df2006-02-07 07:04:46 +00002519
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002520 Py_BEGIN_ALLOW_THREADS
2521 timeout = internal_select(s, 1);
2522 if (!timeout)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002523#ifdef __VMS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002524 n = sendsegmented(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002525#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002526 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002527#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002528 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002529
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002530 PyBuffer_Release(&pbuf);
Martin v. Löwis423be952008-08-13 15:53:07 +00002531
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002532 if (timeout == 1) {
2533 PyErr_SetString(socket_timeout, "timed out");
2534 return NULL;
2535 }
2536 if (n < 0)
2537 return s->errorhandler();
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002538 return PyLong_FromSsize_t(n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00002539}
2540
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002541PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002542"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002543\n\
2544Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002545argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002546sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002547
2548
2549/* s.sendall(data [,flags]) method */
2550
2551static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002552sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002553{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002554 char *buf;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002555 Py_ssize_t len, n = -1;
Antoine Pitrou08ae02f2010-09-27 18:14:43 +00002556 int flags = 0, timeout, saved_errno;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002557 Py_buffer pbuf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002558
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002559 if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags))
2560 return NULL;
2561 buf = pbuf.buf;
2562 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002563
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002564 if (!IS_SELECTABLE(s)) {
2565 PyBuffer_Release(&pbuf);
2566 return select_error();
2567 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002568
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002569 do {
Antoine Pitrou08ae02f2010-09-27 18:14:43 +00002570 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002571 timeout = internal_select(s, 1);
2572 n = -1;
Antoine Pitrou08ae02f2010-09-27 18:14:43 +00002573 if (!timeout) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002574#ifdef __VMS
Antoine Pitrou08ae02f2010-09-27 18:14:43 +00002575 n = sendsegmented(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002576#else
Antoine Pitrou08ae02f2010-09-27 18:14:43 +00002577 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002578#endif
Antoine Pitrou08ae02f2010-09-27 18:14:43 +00002579 }
2580 Py_END_ALLOW_THREADS
2581 if (timeout == 1) {
2582 PyBuffer_Release(&pbuf);
2583 PyErr_SetString(socket_timeout, "timed out");
2584 return NULL;
2585 }
2586 /* PyErr_CheckSignals() might change errno */
2587 saved_errno = errno;
2588 /* We must run our signal handlers before looping again.
2589 send() can return a successful partial write when it is
2590 interrupted, so we can't restrict ourselves to EINTR. */
2591 if (PyErr_CheckSignals()) {
2592 PyBuffer_Release(&pbuf);
2593 return NULL;
2594 }
2595 if (n < 0) {
2596 /* If interrupted, try again */
2597 if (saved_errno == EINTR)
2598 continue;
2599 else
2600 break;
2601 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002602 buf += n;
2603 len -= n;
2604 } while (len > 0);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002605 PyBuffer_Release(&pbuf);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002606
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002607 if (n < 0)
2608 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00002609
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002610 Py_INCREF(Py_None);
2611 return Py_None;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002612}
2613
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002614PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002615"sendall(data[, flags])\n\
2616\n\
2617Send a data string to the socket. For the optional flags\n\
2618argument, see the Unix manual. This calls send() repeatedly\n\
2619until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002620to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002621
Guido van Rossum30a685f1991-06-27 15:51:29 +00002622
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002623/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002624
Guido van Rossum73624e91994-10-10 17:59:00 +00002625static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002626sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002627{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002628 Py_buffer pbuf;
2629 PyObject *addro;
2630 char *buf;
2631 Py_ssize_t len;
2632 sock_addr_t addrbuf;
2633 int addrlen, n = -1, flags, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002634
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002635 flags = 0;
2636 if (!PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro)) {
2637 PyErr_Clear();
2638 if (!PyArg_ParseTuple(args, "y*iO:sendto",
2639 &pbuf, &flags, &addro))
2640 return NULL;
2641 }
2642 buf = pbuf.buf;
2643 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002644
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002645 if (!IS_SELECTABLE(s)) {
2646 PyBuffer_Release(&pbuf);
2647 return select_error();
2648 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002649
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002650 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) {
2651 PyBuffer_Release(&pbuf);
2652 return NULL;
2653 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002654
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002655 Py_BEGIN_ALLOW_THREADS
2656 timeout = internal_select(s, 1);
2657 if (!timeout)
2658 n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen);
2659 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002660
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002661 PyBuffer_Release(&pbuf);
2662 if (timeout == 1) {
2663 PyErr_SetString(socket_timeout, "timed out");
2664 return NULL;
2665 }
2666 if (n < 0)
2667 return s->errorhandler();
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002668 return PyLong_FromSsize_t(n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002669}
2670
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002671PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002672"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002673\n\
2674Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002675For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002676
Guido van Rossum30a685f1991-06-27 15:51:29 +00002677
2678/* s.shutdown(how) method */
2679
Guido van Rossum73624e91994-10-10 17:59:00 +00002680static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002681sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002682{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002683 int how;
2684 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002685
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002686 how = PyLong_AsLong(arg);
2687 if (how == -1 && PyErr_Occurred())
2688 return NULL;
2689 Py_BEGIN_ALLOW_THREADS
2690 res = shutdown(s->sock_fd, how);
2691 Py_END_ALLOW_THREADS
2692 if (res < 0)
2693 return s->errorhandler();
2694 Py_INCREF(Py_None);
2695 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002696}
2697
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002698PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002699"shutdown(flag)\n\
2700\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00002701Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
2702of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002703
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00002704#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Christian Heimesfaf2f632008-01-06 16:59:19 +00002705static PyObject*
2706sock_ioctl(PySocketSockObject *s, PyObject *arg)
2707{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002708 unsigned long cmd = SIO_RCVALL;
2709 unsigned int option = RCVALL_ON;
2710 DWORD recv;
Christian Heimesfaf2f632008-01-06 16:59:19 +00002711
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002712 if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option))
2713 return NULL;
Christian Heimesfaf2f632008-01-06 16:59:19 +00002714
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002715 if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option),
2716 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
2717 return set_error();
2718 }
2719 return PyLong_FromUnsignedLong(recv);
Christian Heimesfaf2f632008-01-06 16:59:19 +00002720}
2721PyDoc_STRVAR(sock_ioctl_doc,
2722"ioctl(cmd, option) -> long\n\
2723\n\
2724Control the socket with WSAIoctl syscall. Currently only socket.SIO_RCVALL\n\
2725is supported as control. Options must be one of the socket.RCVALL_*\n\
2726constants.");
2727
2728#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00002729
2730/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002731
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002732static PyMethodDef sock_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002733 {"_accept", (PyCFunction)sock_accept, METH_NOARGS,
2734 accept_doc},
2735 {"bind", (PyCFunction)sock_bind, METH_O,
2736 bind_doc},
2737 {"close", (PyCFunction)sock_close, METH_NOARGS,
2738 close_doc},
2739 {"connect", (PyCFunction)sock_connect, METH_O,
2740 connect_doc},
2741 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
2742 connect_ex_doc},
2743 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
2744 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002745#ifdef HAVE_GETPEERNAME
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002746 {"getpeername", (PyCFunction)sock_getpeername,
2747 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00002748#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002749 {"getsockname", (PyCFunction)sock_getsockname,
2750 METH_NOARGS, getsockname_doc},
2751 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
2752 getsockopt_doc},
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00002753#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002754 {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS,
2755 sock_ioctl_doc},
Christian Heimesfaf2f632008-01-06 16:59:19 +00002756#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002757 {"listen", (PyCFunction)sock_listen, METH_O,
2758 listen_doc},
2759 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
2760 recv_doc},
2761 {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
2762 recv_into_doc},
2763 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
2764 recvfrom_doc},
2765 {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
2766 recvfrom_into_doc},
2767 {"send", (PyCFunction)sock_send, METH_VARARGS,
2768 send_doc},
2769 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
2770 sendall_doc},
2771 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
2772 sendto_doc},
2773 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
2774 setblocking_doc},
2775 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
2776 settimeout_doc},
2777 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
2778 gettimeout_doc},
2779 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
2780 setsockopt_doc},
2781 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
2782 shutdown_doc},
2783 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002784};
2785
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002786/* SockObject members */
2787static PyMemberDef sock_memberlist[] = {
2788 {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},
2789 {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},
2790 {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},
2791 {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"},
2792 {0},
2793};
Guido van Rossum30a685f1991-06-27 15:51:29 +00002794
Guido van Rossum73624e91994-10-10 17:59:00 +00002795/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00002796 First close the file description. */
2797
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002798static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002799sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002800{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002801 if (s->sock_fd != -1)
2802 (void) SOCKETCLOSE(s->sock_fd);
2803 Py_TYPE(s)->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002804}
2805
Guido van Rossum30a685f1991-06-27 15:51:29 +00002806
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002807static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002808sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002809{
Fred Drakea04eaad2000-06-30 02:46:07 +00002810#if SIZEOF_SOCKET_T > SIZEOF_LONG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002811 if (s->sock_fd > LONG_MAX) {
2812 /* this can occur on Win64, and actually there is a special
2813 ugly printf formatter for decimal pointer length integer
2814 printing, only bother if necessary*/
2815 PyErr_SetString(PyExc_OverflowError,
2816 "no printf formatter to display "
2817 "the socket descriptor in decimal");
2818 return NULL;
2819 }
Fred Drakea04eaad2000-06-30 02:46:07 +00002820#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002821 return PyUnicode_FromFormat(
2822 "<socket object, fd=%ld, family=%d, type=%d, proto=%d>",
2823 (long)s->sock_fd, s->sock_family,
2824 s->sock_type,
2825 s->sock_proto);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002826}
2827
2828
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002829/* Create a new, uninitialized socket object. */
2830
2831static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002832sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002833{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002834 PyObject *new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002835
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002836 new = type->tp_alloc(type, 0);
2837 if (new != NULL) {
2838 ((PySocketSockObject *)new)->sock_fd = -1;
2839 ((PySocketSockObject *)new)->sock_timeout = -1.0;
2840 ((PySocketSockObject *)new)->errorhandler = &set_error;
2841 }
2842 return new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002843}
2844
2845
2846/* Initialize a new socket object. */
2847
2848/*ARGSUSED*/
2849static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002850sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002851{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002852 PySocketSockObject *s = (PySocketSockObject *)self;
2853 PyObject *fdobj = NULL;
2854 SOCKET_T fd = INVALID_SOCKET;
2855 int family = AF_INET, type = SOCK_STREAM, proto = 0;
2856 static char *keywords[] = {"family", "type", "proto", "fileno", 0};
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002857
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002858 if (!PyArg_ParseTupleAndKeywords(args, kwds,
2859 "|iiiO:socket", keywords,
2860 &family, &type, &proto, &fdobj))
2861 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002862
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002863 if (fdobj != NULL && fdobj != Py_None) {
2864 fd = PyLong_AsSocket_t(fdobj);
2865 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
2866 return -1;
2867 if (fd == INVALID_SOCKET) {
2868 PyErr_SetString(PyExc_ValueError,
2869 "can't use invalid socket value");
2870 return -1;
2871 }
2872 }
2873 else {
2874 Py_BEGIN_ALLOW_THREADS
2875 fd = socket(family, type, proto);
2876 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002877
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002878 if (fd == INVALID_SOCKET) {
2879 set_error();
2880 return -1;
2881 }
2882 }
2883 init_sockobject(s, fd, family, type, proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002884
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002885 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002886
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002887}
2888
2889
Guido van Rossumb6775db1994-08-01 11:34:53 +00002890/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002891
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002892static PyTypeObject sock_type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002893 PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
2894 "_socket.socket", /* tp_name */
2895 sizeof(PySocketSockObject), /* tp_basicsize */
2896 0, /* tp_itemsize */
2897 (destructor)sock_dealloc, /* tp_dealloc */
2898 0, /* tp_print */
2899 0, /* tp_getattr */
2900 0, /* tp_setattr */
2901 0, /* tp_reserved */
2902 (reprfunc)sock_repr, /* tp_repr */
2903 0, /* tp_as_number */
2904 0, /* tp_as_sequence */
2905 0, /* tp_as_mapping */
2906 0, /* tp_hash */
2907 0, /* tp_call */
2908 0, /* tp_str */
2909 PyObject_GenericGetAttr, /* tp_getattro */
2910 0, /* tp_setattro */
2911 0, /* tp_as_buffer */
2912 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2913 sock_doc, /* tp_doc */
2914 0, /* tp_traverse */
2915 0, /* tp_clear */
2916 0, /* tp_richcompare */
2917 0, /* tp_weaklistoffset */
2918 0, /* tp_iter */
2919 0, /* tp_iternext */
2920 sock_methods, /* tp_methods */
2921 sock_memberlist, /* tp_members */
2922 0, /* tp_getset */
2923 0, /* tp_base */
2924 0, /* tp_dict */
2925 0, /* tp_descr_get */
2926 0, /* tp_descr_set */
2927 0, /* tp_dictoffset */
2928 sock_initobj, /* tp_init */
2929 PyType_GenericAlloc, /* tp_alloc */
2930 sock_new, /* tp_new */
2931 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002932};
2933
Guido van Rossum30a685f1991-06-27 15:51:29 +00002934
Guido van Rossum81194471991-07-27 21:42:02 +00002935/* Python interface to gethostname(). */
2936
2937/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002938static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002939socket_gethostname(PyObject *self, PyObject *unused)
Guido van Rossum81194471991-07-27 21:42:02 +00002940{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002941 char buf[1024];
2942 int res;
2943 Py_BEGIN_ALLOW_THREADS
2944 res = gethostname(buf, (int) sizeof buf - 1);
2945 Py_END_ALLOW_THREADS
2946 if (res < 0)
2947 return set_error();
2948 buf[sizeof buf - 1] = '\0';
2949 return PyUnicode_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00002950}
Guido van Rossumff4949e1992-08-05 19:58:53 +00002951
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002952PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002953"gethostname() -> string\n\
2954\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002955Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002956
Guido van Rossumff4949e1992-08-05 19:58:53 +00002957
Guido van Rossum30a685f1991-06-27 15:51:29 +00002958/* Python interface to gethostbyname(name). */
2959
2960/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002961static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002962socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002963{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002964 char *name;
2965 sock_addr_t addrbuf;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002966
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002967 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
2968 return NULL;
2969 if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)
2970 return NULL;
2971 return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002972}
2973
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002974PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002975"gethostbyname(host) -> address\n\
2976\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002977Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002978
2979
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002980/* Convenience function common to gethostbyname_ex and gethostbyaddr */
2981
2982static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002983gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002984{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002985 char **pch;
2986 PyObject *rtn_tuple = (PyObject *)NULL;
2987 PyObject *name_list = (PyObject *)NULL;
2988 PyObject *addr_list = (PyObject *)NULL;
2989 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002990
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002991 if (h == NULL) {
2992 /* Let's get real error message to return */
2993 set_herror(h_errno);
2994 return NULL;
2995 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002996
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002997 if (h->h_addrtype != af) {
2998 /* Let's get real error message to return */
2999 PyErr_SetString(socket_error,
3000 (char *)strerror(EAFNOSUPPORT));
Christian Heimesada8c3b2008-03-18 18:26:33 +00003001
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003002 return NULL;
3003 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003004
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003005 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00003006
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003007 case AF_INET:
3008 if (alen < sizeof(struct sockaddr_in))
3009 return NULL;
3010 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003011
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003012#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003013 case AF_INET6:
3014 if (alen < sizeof(struct sockaddr_in6))
3015 return NULL;
3016 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003017#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00003018
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003019 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003020
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003021 if ((name_list = PyList_New(0)) == NULL)
3022 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003023
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003024 if ((addr_list = PyList_New(0)) == NULL)
3025 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003026
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003027 /* SF #1511317: h_aliases can be NULL */
3028 if (h->h_aliases) {
3029 for (pch = h->h_aliases; *pch != NULL; pch++) {
3030 int status;
3031 tmp = PyUnicode_FromString(*pch);
3032 if (tmp == NULL)
3033 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003034
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003035 status = PyList_Append(name_list, tmp);
3036 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003037
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003038 if (status)
3039 goto err;
3040 }
3041 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003042
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003043 for (pch = h->h_addr_list; *pch != NULL; pch++) {
3044 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003045
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003046 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00003047
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003048 case AF_INET:
3049 {
3050 struct sockaddr_in sin;
3051 memset(&sin, 0, sizeof(sin));
3052 sin.sin_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003053#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003054 sin.sin_len = sizeof(sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003055#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003056 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
3057 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00003058
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003059 if (pch == h->h_addr_list && alen >= sizeof(sin))
3060 memcpy((char *) addr, &sin, sizeof(sin));
3061 break;
3062 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003063
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003064#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003065 case AF_INET6:
3066 {
3067 struct sockaddr_in6 sin6;
3068 memset(&sin6, 0, sizeof(sin6));
3069 sin6.sin6_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003070#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003071 sin6.sin6_len = sizeof(sin6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003072#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003073 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
3074 tmp = makeipaddr((struct sockaddr *)&sin6,
3075 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00003076
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003077 if (pch == h->h_addr_list && alen >= sizeof(sin6))
3078 memcpy((char *) addr, &sin6, sizeof(sin6));
3079 break;
3080 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003081#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00003082
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003083 default: /* can't happen */
3084 PyErr_SetString(socket_error,
3085 "unsupported address family");
3086 return NULL;
3087 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003088
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003089 if (tmp == NULL)
3090 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003091
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003092 status = PyList_Append(addr_list, tmp);
3093 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003094
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003095 if (status)
3096 goto err;
3097 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003098
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003099 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003100
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003101 err:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003102 Py_XDECREF(name_list);
3103 Py_XDECREF(addr_list);
3104 return rtn_tuple;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003105}
3106
3107
3108/* Python interface to gethostbyname_ex(name). */
3109
3110/*ARGSUSED*/
3111static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003112socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003113{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003114 char *name;
3115 struct hostent *h;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00003116#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003117 struct sockaddr_storage addr;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00003118#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003119 struct sockaddr_in addr;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00003120#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003121 struct sockaddr *sa;
3122 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003123#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003124 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003125#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003126 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003127#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003128 char buf[16384];
3129 int buf_len = (sizeof buf) - 1;
3130 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003131#endif
3132#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003133 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003134#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003135#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003136
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003137 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
3138 return NULL;
3139 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
3140 return NULL;
3141 Py_BEGIN_ALLOW_THREADS
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003142#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003143#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003144 result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
3145 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003146#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003147 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003148#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003149 memset((void *) &data, '\0', sizeof(data));
3150 result = gethostbyname_r(name, &hp_allocated, &data);
3151 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003152#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003153#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003154#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003155 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003156#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003157 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003158#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003159 Py_END_ALLOW_THREADS
3160 /* Some C libraries would require addr.__ss_family instead of
3161 addr.ss_family.
3162 Therefore, we cast the sockaddr_storage into sockaddr to
3163 access sa_family. */
3164 sa = (struct sockaddr*)&addr;
3165 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
3166 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003167#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003168 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00003169#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003170 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003171}
3172
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003173PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003174"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
3175\n\
3176Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003177for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003178
3179
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003180/* Python interface to gethostbyaddr(IP). */
3181
3182/*ARGSUSED*/
3183static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003184socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003185{
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003186#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003187 struct sockaddr_storage addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003188#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003189 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003190#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003191 struct sockaddr *sa = (struct sockaddr *)&addr;
3192 char *ip_num;
3193 struct hostent *h;
3194 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003195#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003196 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003197#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003198 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003199#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003200 /* glibcs up to 2.10 assume that the buf argument to
3201 gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc
3202 does not ensure. The attribute below instructs the compiler
3203 to maintain this alignment. */
3204 char buf[16384] Py_ALIGNED(8);
3205 int buf_len = (sizeof buf) - 1;
3206 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003207#endif
3208#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003209 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003210#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003211#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003212 char *ap;
3213 int al;
3214 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003215
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003216 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
3217 return NULL;
3218 af = AF_UNSPEC;
3219 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
3220 return NULL;
3221 af = sa->sa_family;
3222 ap = NULL;
3223 al = 0;
3224 switch (af) {
3225 case AF_INET:
3226 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
3227 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3228 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003229#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003230 case AF_INET6:
3231 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
3232 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3233 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003234#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003235 default:
3236 PyErr_SetString(socket_error, "unsupported address family");
3237 return NULL;
3238 }
3239 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003240#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003241#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003242 result = gethostbyaddr_r(ap, al, af,
3243 &hp_allocated, buf, buf_len,
3244 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003245#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003246 h = gethostbyaddr_r(ap, al, af,
3247 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003248#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003249 memset((void *) &data, '\0', sizeof(data));
3250 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
3251 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003252#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003253#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003254#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003255 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003256#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003257 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003258#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003259 Py_END_ALLOW_THREADS
3260 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003261#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003262 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003263#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003264 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003265}
3266
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003267PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003268"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
3269\n\
3270Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003271for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003272
Guido van Rossum30a685f1991-06-27 15:51:29 +00003273
3274/* Python interface to getservbyname(name).
3275 This only returns the port number, since the other info is already
3276 known or not useful (like the list of aliases). */
3277
3278/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00003279static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003280socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00003281{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003282 char *name, *proto=NULL;
3283 struct servent *sp;
3284 if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
3285 return NULL;
3286 Py_BEGIN_ALLOW_THREADS
3287 sp = getservbyname(name, proto);
3288 Py_END_ALLOW_THREADS
3289 if (sp == NULL) {
3290 PyErr_SetString(socket_error, "service/proto not found");
3291 return NULL;
3292 }
3293 return PyLong_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00003294}
3295
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003296PyDoc_STRVAR(getservbyname_doc,
Barry Warsaw11b91a02004-06-28 00:50:43 +00003297"getservbyname(servicename[, protocolname]) -> integer\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003298\n\
3299Return a port number from a service name and protocol name.\n\
Barry Warsaw11b91a02004-06-28 00:50:43 +00003300The optional protocol name, if given, should be 'tcp' or 'udp',\n\
3301otherwise any protocol will match.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003302
Guido van Rossum30a685f1991-06-27 15:51:29 +00003303
Barry Warsaw11b91a02004-06-28 00:50:43 +00003304/* Python interface to getservbyport(port).
3305 This only returns the service name, since the other info is already
3306 known or not useful (like the list of aliases). */
3307
3308/*ARGSUSED*/
3309static PyObject *
3310socket_getservbyport(PyObject *self, PyObject *args)
3311{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003312 int port;
3313 char *proto=NULL;
3314 struct servent *sp;
3315 if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto))
3316 return NULL;
3317 if (port < 0 || port > 0xffff) {
3318 PyErr_SetString(
3319 PyExc_OverflowError,
3320 "getservbyport: port must be 0-65535.");
3321 return NULL;
3322 }
3323 Py_BEGIN_ALLOW_THREADS
3324 sp = getservbyport(htons((short)port), proto);
3325 Py_END_ALLOW_THREADS
3326 if (sp == NULL) {
3327 PyErr_SetString(socket_error, "port/proto not found");
3328 return NULL;
3329 }
3330 return PyUnicode_FromString(sp->s_name);
Barry Warsaw11b91a02004-06-28 00:50:43 +00003331}
3332
3333PyDoc_STRVAR(getservbyport_doc,
3334"getservbyport(port[, protocolname]) -> string\n\
3335\n\
3336Return the service name from a port number and protocol name.\n\
3337The optional protocol name, if given, should be 'tcp' or 'udp',\n\
3338otherwise any protocol will match.");
3339
Guido van Rossum3901d851996-12-19 16:35:04 +00003340/* Python interface to getprotobyname(name).
3341 This only returns the protocol number, since the other info is
3342 already known or not useful (like the list of aliases). */
3343
3344/*ARGSUSED*/
3345static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003346socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00003347{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003348 char *name;
3349 struct protoent *sp;
3350 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
3351 return NULL;
3352 Py_BEGIN_ALLOW_THREADS
3353 sp = getprotobyname(name);
3354 Py_END_ALLOW_THREADS
3355 if (sp == NULL) {
3356 PyErr_SetString(socket_error, "protocol not found");
3357 return NULL;
3358 }
3359 return PyLong_FromLong((long) sp->p_proto);
Guido van Rossum3901d851996-12-19 16:35:04 +00003360}
3361
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003362PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003363"getprotobyname(name) -> integer\n\
3364\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003365Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003366
Guido van Rossum3901d851996-12-19 16:35:04 +00003367
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003368#ifndef NO_DUP
3369/* dup() function for socket fds */
3370
3371static PyObject *
3372socket_dup(PyObject *self, PyObject *fdobj)
3373{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003374 SOCKET_T fd, newfd;
3375 PyObject *newfdobj;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003376
3377
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003378 fd = PyLong_AsSocket_t(fdobj);
3379 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
3380 return NULL;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003381
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003382 newfd = dup_socket(fd);
3383 if (newfd == INVALID_SOCKET)
3384 return set_error();
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003385
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003386 newfdobj = PyLong_FromSocket_t(newfd);
3387 if (newfdobj == NULL)
3388 SOCKETCLOSE(newfd);
3389 return newfdobj;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003390}
3391
3392PyDoc_STRVAR(dup_doc,
3393"dup(integer) -> integer\n\
3394\n\
3395Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\
3396sockets; on some platforms os.dup() won't work for socket file descriptors.");
3397#endif
3398
3399
Dave Cole331708b2004-08-09 04:51:41 +00003400#ifdef HAVE_SOCKETPAIR
3401/* Create a pair of sockets using the socketpair() function.
Dave Cole07fda7e2004-08-23 05:16:23 +00003402 Arguments as for socket() except the default family is AF_UNIX if
Dave Colee8bbfe42004-08-26 00:51:16 +00003403 defined on the platform; otherwise, the default is AF_INET. */
Dave Cole331708b2004-08-09 04:51:41 +00003404
3405/*ARGSUSED*/
3406static PyObject *
3407socket_socketpair(PyObject *self, PyObject *args)
3408{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003409 PySocketSockObject *s0 = NULL, *s1 = NULL;
3410 SOCKET_T sv[2];
3411 int family, type = SOCK_STREAM, proto = 0;
3412 PyObject *res = NULL;
Dave Cole331708b2004-08-09 04:51:41 +00003413
3414#if defined(AF_UNIX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003415 family = AF_UNIX;
Dave Cole331708b2004-08-09 04:51:41 +00003416#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003417 family = AF_INET;
Dave Cole331708b2004-08-09 04:51:41 +00003418#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003419 if (!PyArg_ParseTuple(args, "|iii:socketpair",
3420 &family, &type, &proto))
3421 return NULL;
3422 /* Create a pair of socket fds */
3423 if (socketpair(family, type, proto, sv) < 0)
3424 return set_error();
3425 s0 = new_sockobject(sv[0], family, type, proto);
3426 if (s0 == NULL)
3427 goto finally;
3428 s1 = new_sockobject(sv[1], family, type, proto);
3429 if (s1 == NULL)
3430 goto finally;
3431 res = PyTuple_Pack(2, s0, s1);
Dave Cole331708b2004-08-09 04:51:41 +00003432
3433finally:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003434 if (res == NULL) {
3435 if (s0 == NULL)
3436 SOCKETCLOSE(sv[0]);
3437 if (s1 == NULL)
3438 SOCKETCLOSE(sv[1]);
3439 }
3440 Py_XDECREF(s0);
3441 Py_XDECREF(s1);
3442 return res;
Dave Cole331708b2004-08-09 04:51:41 +00003443}
3444
3445PyDoc_STRVAR(socketpair_doc,
3446"socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\
3447\n\
3448Create a pair of socket objects from the sockets returned by the platform\n\
3449socketpair() function.\n\
Dave Cole07fda7e2004-08-23 05:16:23 +00003450The arguments are the same as for socket() except the default family is\n\
Dave Colee8bbfe42004-08-26 00:51:16 +00003451AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
Dave Cole331708b2004-08-09 04:51:41 +00003452
3453#endif /* HAVE_SOCKETPAIR */
3454
3455
Guido van Rossum006bf911996-06-12 04:04:55 +00003456static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003457socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003458{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003459 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00003460
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003461 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
3462 return NULL;
3463 }
3464 if (x1 < 0) {
3465 PyErr_SetString(PyExc_OverflowError,
3466 "can't convert negative number to unsigned long");
3467 return NULL;
3468 }
3469 x2 = (unsigned int)ntohs((unsigned short)x1);
3470 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00003471}
3472
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003473PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003474"ntohs(integer) -> integer\n\
3475\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003476Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003477
3478
Guido van Rossum006bf911996-06-12 04:04:55 +00003479static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003480socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003481{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003482 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003483
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003484 if (PyLong_Check(arg)) {
3485 x = PyLong_AsUnsignedLong(arg);
3486 if (x == (unsigned long) -1 && PyErr_Occurred())
3487 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003488#if SIZEOF_LONG > 4
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003489 {
3490 unsigned long y;
3491 /* only want the trailing 32 bits */
3492 y = x & 0xFFFFFFFFUL;
3493 if (y ^ x)
3494 return PyErr_Format(PyExc_OverflowError,
3495 "long int larger than 32 bits");
3496 x = y;
3497 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003498#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003499 }
3500 else
3501 return PyErr_Format(PyExc_TypeError,
3502 "expected int/long, %s found",
3503 Py_TYPE(arg)->tp_name);
3504 if (x == (unsigned long) -1 && PyErr_Occurred())
3505 return NULL;
3506 return PyLong_FromUnsignedLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003507}
3508
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003509PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003510"ntohl(integer) -> integer\n\
3511\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003512Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003513
3514
Guido van Rossum006bf911996-06-12 04:04:55 +00003515static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003516socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003517{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003518 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00003519
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003520 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
3521 return NULL;
3522 }
3523 if (x1 < 0) {
3524 PyErr_SetString(PyExc_OverflowError,
3525 "can't convert negative number to unsigned long");
3526 return NULL;
3527 }
3528 x2 = (unsigned int)htons((unsigned short)x1);
3529 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00003530}
3531
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003532PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003533"htons(integer) -> integer\n\
3534\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003535Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003536
3537
Guido van Rossum006bf911996-06-12 04:04:55 +00003538static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003539socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003540{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003541 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003542
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003543 if (PyLong_Check(arg)) {
3544 x = PyLong_AsUnsignedLong(arg);
3545 if (x == (unsigned long) -1 && PyErr_Occurred())
3546 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003547#if SIZEOF_LONG > 4
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003548 {
3549 unsigned long y;
3550 /* only want the trailing 32 bits */
3551 y = x & 0xFFFFFFFFUL;
3552 if (y ^ x)
3553 return PyErr_Format(PyExc_OverflowError,
3554 "long int larger than 32 bits");
3555 x = y;
3556 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003557#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003558 }
3559 else
3560 return PyErr_Format(PyExc_TypeError,
3561 "expected int/long, %s found",
3562 Py_TYPE(arg)->tp_name);
3563 return PyLong_FromUnsignedLong(htonl((unsigned long)x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003564}
3565
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003566PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003567"htonl(integer) -> integer\n\
3568\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003569Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003570
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003571/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003572
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003573PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum7d0a8262007-05-21 23:13:11 +00003574"inet_aton(string) -> bytes giving packed 32-bit IP representation\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003575\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003576Convert 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 +00003577binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003578
3579static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003580socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003581{
Guido van Rossuma2e48551999-09-09 15:42:59 +00003582#ifndef INADDR_NONE
3583#define INADDR_NONE (-1)
3584#endif
Neal Norwitz88f115b2003-02-13 02:15:42 +00003585#ifdef HAVE_INET_ATON
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003586 struct in_addr buf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003587#endif
3588
3589#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
Benjamin Petersonf91df042009-02-13 02:50:59 +00003590#if (SIZEOF_INT != 4)
3591#error "Not sure if in_addr_t exists and int is not 32-bits."
3592#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003593 /* Have to use inet_addr() instead */
3594 unsigned int packed_addr;
Tim Peters1df9fdd2003-02-13 03:13:40 +00003595#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003596 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003597
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003598 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
3599 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003600
Tim Peters1df9fdd2003-02-13 03:13:40 +00003601
3602#ifdef HAVE_INET_ATON
Thomas Wouters477c8d52006-05-27 19:21:47 +00003603
3604#ifdef USE_INET_ATON_WEAKLINK
3605 if (inet_aton != NULL) {
3606#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003607 if (inet_aton(ip_addr, &buf))
3608 return PyBytes_FromStringAndSize((char *)(&buf),
3609 sizeof(buf));
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003610
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003611 PyErr_SetString(socket_error,
3612 "illegal IP address string passed to inet_aton");
3613 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003614
Thomas Wouters477c8d52006-05-27 19:21:47 +00003615#ifdef USE_INET_ATON_WEAKLINK
3616 } else {
3617#endif
3618
3619#endif
3620
3621#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
3622
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003623 /* special-case this address as inet_addr might return INADDR_NONE
3624 * for this */
3625 if (strcmp(ip_addr, "255.255.255.255") == 0) {
3626 packed_addr = 0xFFFFFFFF;
3627 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00003628
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003629 packed_addr = inet_addr(ip_addr);
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003630
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003631 if (packed_addr == INADDR_NONE) { /* invalid address */
3632 PyErr_SetString(socket_error,
3633 "illegal IP address string passed to inet_aton");
3634 return NULL;
3635 }
3636 }
3637 return PyBytes_FromStringAndSize((char *) &packed_addr,
3638 sizeof(packed_addr));
Thomas Wouters477c8d52006-05-27 19:21:47 +00003639
3640#ifdef USE_INET_ATON_WEAKLINK
3641 }
3642#endif
3643
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003644#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003645}
3646
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003647PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00003648"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003649\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003650Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003651
3652static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003653socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003654{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003655 char *packed_str;
3656 int addr_len;
3657 struct in_addr packed_addr;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003658
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003659 if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) {
3660 return NULL;
3661 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00003662
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003663 if (addr_len != sizeof(packed_addr)) {
3664 PyErr_SetString(socket_error,
3665 "packed IP wrong length for inet_ntoa");
3666 return NULL;
3667 }
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003668
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003669 memcpy(&packed_addr, packed_str, addr_len);
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003670
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003671 return PyUnicode_FromString(inet_ntoa(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003672}
Guido van Rossum82a5c661998-07-07 20:45:43 +00003673
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003674#ifdef HAVE_INET_PTON
3675
3676PyDoc_STRVAR(inet_pton_doc,
3677"inet_pton(af, ip) -> packed IP address string\n\
3678\n\
3679Convert an IP address from string format to a packed string suitable\n\
3680for use with low-level network functions.");
3681
3682static PyObject *
3683socket_inet_pton(PyObject *self, PyObject *args)
3684{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003685 int af;
3686 char* ip;
3687 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003688#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003689 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003690#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003691 char packed[sizeof(struct in_addr)];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003692#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003693 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
3694 return NULL;
3695 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003696
Martin v. Löwis04697e82004-06-02 12:35:29 +00003697#if !defined(ENABLE_IPV6) && defined(AF_INET6)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003698 if(af == AF_INET6) {
3699 PyErr_SetString(socket_error,
3700 "can't use AF_INET6, IPv6 is disabled");
3701 return NULL;
3702 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003703#endif
Martin v. Löwis10649092003-08-05 06:25:06 +00003704
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003705 retval = inet_pton(af, ip, packed);
3706 if (retval < 0) {
3707 PyErr_SetFromErrno(socket_error);
3708 return NULL;
3709 } else if (retval == 0) {
3710 PyErr_SetString(socket_error,
3711 "illegal IP address string passed to inet_pton");
3712 return NULL;
3713 } else if (af == AF_INET) {
3714 return PyBytes_FromStringAndSize(packed,
3715 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003716#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003717 } else if (af == AF_INET6) {
3718 return PyBytes_FromStringAndSize(packed,
3719 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003720#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003721 } else {
3722 PyErr_SetString(socket_error, "unknown address family");
3723 return NULL;
3724 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003725}
Thomas Wouters477c8d52006-05-27 19:21:47 +00003726
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003727PyDoc_STRVAR(inet_ntop_doc,
3728"inet_ntop(af, packed_ip) -> string formatted IP address\n\
3729\n\
3730Convert a packed IP address of the given family to string format.");
3731
3732static PyObject *
3733socket_inet_ntop(PyObject *self, PyObject *args)
3734{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003735 int af;
3736 char* packed;
3737 int len;
3738 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003739#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003740 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003741#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003742 char ip[INET_ADDRSTRLEN + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003743#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00003744
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003745 /* Guarantee NUL-termination for PyUnicode_FromString() below */
3746 memset((void *) &ip[0], '\0', sizeof(ip));
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003747
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003748 if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) {
3749 return NULL;
3750 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003751
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003752 if (af == AF_INET) {
3753 if (len != sizeof(struct in_addr)) {
3754 PyErr_SetString(PyExc_ValueError,
3755 "invalid length of packed IP address string");
3756 return NULL;
3757 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003758#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003759 } else if (af == AF_INET6) {
3760 if (len != sizeof(struct in6_addr)) {
3761 PyErr_SetString(PyExc_ValueError,
3762 "invalid length of packed IP address string");
3763 return NULL;
3764 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003765#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003766 } else {
3767 PyErr_Format(PyExc_ValueError,
3768 "unknown address family %d", af);
3769 return NULL;
3770 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003771
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003772 retval = inet_ntop(af, packed, ip, sizeof(ip));
3773 if (!retval) {
3774 PyErr_SetFromErrno(socket_error);
3775 return NULL;
3776 } else {
3777 return PyUnicode_FromString(retval);
3778 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003779
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003780 /* NOTREACHED */
3781 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
3782 return NULL;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003783}
3784
3785#endif /* HAVE_INET_PTON */
3786
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003787/* Python interface to getaddrinfo(host, port). */
3788
3789/*ARGSUSED*/
3790static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003791socket_getaddrinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003792{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003793 struct addrinfo hints, *res;
3794 struct addrinfo *res0 = NULL;
3795 PyObject *hobj = NULL;
3796 PyObject *pobj = (PyObject *)NULL;
3797 char pbuf[30];
3798 char *hptr, *pptr;
3799 int family, socktype, protocol, flags;
3800 int error;
3801 PyObject *all = (PyObject *)NULL;
3802 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003803
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003804 family = socktype = protocol = flags = 0;
3805 family = AF_UNSPEC;
3806 if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
3807 &hobj, &pobj, &family, &socktype,
3808 &protocol, &flags)) {
3809 return NULL;
3810 }
3811 if (hobj == Py_None) {
3812 hptr = NULL;
3813 } else if (PyUnicode_Check(hobj)) {
3814 idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
3815 if (!idna)
3816 return NULL;
3817 assert(PyBytes_Check(idna));
3818 hptr = PyBytes_AS_STRING(idna);
3819 } else if (PyBytes_Check(hobj)) {
3820 hptr = PyBytes_AsString(hobj);
3821 } else {
3822 PyErr_SetString(PyExc_TypeError,
3823 "getaddrinfo() argument 1 must be string or None");
3824 return NULL;
3825 }
3826 if (PyLong_CheckExact(pobj)) {
3827 long value = PyLong_AsLong(pobj);
3828 if (value == -1 && PyErr_Occurred())
3829 goto err;
3830 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value);
3831 pptr = pbuf;
3832 } else if (PyUnicode_Check(pobj)) {
3833 pptr = _PyUnicode_AsString(pobj);
3834 } else if (PyBytes_Check(pobj)) {
3835 pptr = PyBytes_AsString(pobj);
3836 } else if (pobj == Py_None) {
3837 pptr = (char *)NULL;
3838 } else {
3839 PyErr_SetString(socket_error, "Int or String expected");
3840 goto err;
3841 }
3842 memset(&hints, 0, sizeof(hints));
3843 hints.ai_family = family;
3844 hints.ai_socktype = socktype;
3845 hints.ai_protocol = protocol;
3846 hints.ai_flags = flags;
3847 Py_BEGIN_ALLOW_THREADS
3848 ACQUIRE_GETADDRINFO_LOCK
3849 error = getaddrinfo(hptr, pptr, &hints, &res0);
3850 Py_END_ALLOW_THREADS
3851 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
3852 if (error) {
3853 set_gaierror(error);
3854 goto err;
3855 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003856
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003857 if ((all = PyList_New(0)) == NULL)
3858 goto err;
3859 for (res = res0; res; res = res->ai_next) {
3860 PyObject *single;
3861 PyObject *addr =
3862 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
3863 if (addr == NULL)
3864 goto err;
3865 single = Py_BuildValue("iiisO", res->ai_family,
3866 res->ai_socktype, res->ai_protocol,
3867 res->ai_canonname ? res->ai_canonname : "",
3868 addr);
3869 Py_DECREF(addr);
3870 if (single == NULL)
3871 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003872
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003873 if (PyList_Append(all, single))
3874 goto err;
3875 Py_XDECREF(single);
3876 }
3877 Py_XDECREF(idna);
3878 if (res0)
3879 freeaddrinfo(res0);
3880 return all;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003881 err:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003882 Py_XDECREF(all);
3883 Py_XDECREF(idna);
3884 if (res0)
3885 freeaddrinfo(res0);
3886 return (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003887}
3888
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003889PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003890"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
3891 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003892\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003893Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003894
3895/* Python interface to getnameinfo(sa, flags). */
3896
3897/*ARGSUSED*/
3898static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003899socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003900{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003901 PyObject *sa = (PyObject *)NULL;
3902 int flags;
3903 char *hostp;
3904 int port, flowinfo, scope_id;
3905 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
3906 struct addrinfo hints, *res = NULL;
3907 int error;
3908 PyObject *ret = (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003909
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003910 flags = flowinfo = scope_id = 0;
3911 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
3912 return NULL;
3913 if (!PyTuple_Check(sa)) {
3914 PyErr_SetString(PyExc_TypeError,
3915 "getnameinfo() argument 1 must be a tuple");
3916 return NULL;
3917 }
3918 if (!PyArg_ParseTuple(sa, "si|ii",
3919 &hostp, &port, &flowinfo, &scope_id))
3920 return NULL;
3921 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
3922 memset(&hints, 0, sizeof(hints));
3923 hints.ai_family = AF_UNSPEC;
3924 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
3925 Py_BEGIN_ALLOW_THREADS
3926 ACQUIRE_GETADDRINFO_LOCK
3927 error = getaddrinfo(hostp, pbuf, &hints, &res);
3928 Py_END_ALLOW_THREADS
3929 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
3930 if (error) {
3931 set_gaierror(error);
3932 goto fail;
3933 }
3934 if (res->ai_next) {
3935 PyErr_SetString(socket_error,
3936 "sockaddr resolved to multiple addresses");
3937 goto fail;
3938 }
3939 switch (res->ai_family) {
3940 case AF_INET:
3941 {
3942 if (PyTuple_GET_SIZE(sa) != 2) {
3943 PyErr_SetString(socket_error,
3944 "IPv4 sockaddr must be 2 tuple");
3945 goto fail;
3946 }
3947 break;
3948 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003949#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003950 case AF_INET6:
3951 {
3952 struct sockaddr_in6 *sin6;
3953 sin6 = (struct sockaddr_in6 *)res->ai_addr;
3954 sin6->sin6_flowinfo = flowinfo;
3955 sin6->sin6_scope_id = scope_id;
3956 break;
3957 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003958#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003959 }
Antoine Pitrouf72006f2010-08-17 19:39:39 +00003960 error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003961 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
3962 if (error) {
3963 set_gaierror(error);
3964 goto fail;
3965 }
3966 ret = Py_BuildValue("ss", hbuf, pbuf);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003967
3968fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003969 if (res)
3970 freeaddrinfo(res);
3971 return ret;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003972}
3973
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003974PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003975"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003976\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003977Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003978
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003979
3980/* Python API to getting and setting the default timeout value. */
3981
3982static PyObject *
3983socket_getdefaulttimeout(PyObject *self)
3984{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003985 if (defaulttimeout < 0.0) {
3986 Py_INCREF(Py_None);
3987 return Py_None;
3988 }
3989 else
3990 return PyFloat_FromDouble(defaulttimeout);
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003991}
3992
3993PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003994"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003995\n\
3996Returns the default timeout in floating seconds for new socket objects.\n\
3997A value of None indicates that new socket objects have no timeout.\n\
3998When the socket module is first imported, the default is None.");
3999
4000static PyObject *
4001socket_setdefaulttimeout(PyObject *self, PyObject *arg)
4002{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004003 double timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004004
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004005 if (arg == Py_None)
4006 timeout = -1.0;
4007 else {
4008 timeout = PyFloat_AsDouble(arg);
4009 if (timeout < 0.0) {
4010 if (!PyErr_Occurred())
4011 PyErr_SetString(PyExc_ValueError,
4012 "Timeout value out of range");
4013 return NULL;
4014 }
4015 }
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004016
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004017 defaulttimeout = timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004018
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004019 Py_INCREF(Py_None);
4020 return Py_None;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004021}
4022
4023PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004024"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004025\n\
4026Set the default timeout in floating seconds for new socket objects.\n\
4027A value of None indicates that new socket objects have no timeout.\n\
4028When the socket module is first imported, the default is None.");
4029
4030
Guido van Rossum30a685f1991-06-27 15:51:29 +00004031/* List of functions exported by this module. */
4032
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004033static PyMethodDef socket_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004034 {"gethostbyname", socket_gethostbyname,
4035 METH_VARARGS, gethostbyname_doc},
4036 {"gethostbyname_ex", socket_gethostbyname_ex,
4037 METH_VARARGS, ghbn_ex_doc},
4038 {"gethostbyaddr", socket_gethostbyaddr,
4039 METH_VARARGS, gethostbyaddr_doc},
4040 {"gethostname", socket_gethostname,
4041 METH_NOARGS, gethostname_doc},
4042 {"getservbyname", socket_getservbyname,
4043 METH_VARARGS, getservbyname_doc},
4044 {"getservbyport", socket_getservbyport,
4045 METH_VARARGS, getservbyport_doc},
4046 {"getprotobyname", socket_getprotobyname,
4047 METH_VARARGS, getprotobyname_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004048#ifndef NO_DUP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004049 {"dup", socket_dup,
4050 METH_O, dup_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004051#endif
Dave Cole331708b2004-08-09 04:51:41 +00004052#ifdef HAVE_SOCKETPAIR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004053 {"socketpair", socket_socketpair,
4054 METH_VARARGS, socketpair_doc},
Dave Cole331708b2004-08-09 04:51:41 +00004055#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004056 {"ntohs", socket_ntohs,
4057 METH_VARARGS, ntohs_doc},
4058 {"ntohl", socket_ntohl,
4059 METH_O, ntohl_doc},
4060 {"htons", socket_htons,
4061 METH_VARARGS, htons_doc},
4062 {"htonl", socket_htonl,
4063 METH_O, htonl_doc},
4064 {"inet_aton", socket_inet_aton,
4065 METH_VARARGS, inet_aton_doc},
4066 {"inet_ntoa", socket_inet_ntoa,
4067 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004068#ifdef HAVE_INET_PTON
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004069 {"inet_pton", socket_inet_pton,
4070 METH_VARARGS, inet_pton_doc},
4071 {"inet_ntop", socket_inet_ntop,
4072 METH_VARARGS, inet_ntop_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004073#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004074 {"getaddrinfo", socket_getaddrinfo,
4075 METH_VARARGS, getaddrinfo_doc},
4076 {"getnameinfo", socket_getnameinfo,
4077 METH_VARARGS, getnameinfo_doc},
4078 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
4079 METH_NOARGS, getdefaulttimeout_doc},
4080 {"setdefaulttimeout", socket_setdefaulttimeout,
4081 METH_O, setdefaulttimeout_doc},
4082 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004083};
4084
Guido van Rossum30a685f1991-06-27 15:51:29 +00004085
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004086#ifdef MS_WINDOWS
4087#define OS_INIT_DEFINED
4088
4089/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004090
4091static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004092os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00004093{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004094 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00004095}
4096
4097static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004098os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00004099{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004100 WSADATA WSAData;
4101 int ret;
4102 ret = WSAStartup(0x0101, &WSAData);
4103 switch (ret) {
4104 case 0: /* No error */
4105 Py_AtExit(os_cleanup);
4106 return 1; /* Success */
4107 case WSASYSNOTREADY:
4108 PyErr_SetString(PyExc_ImportError,
4109 "WSAStartup failed: network not ready");
4110 break;
4111 case WSAVERNOTSUPPORTED:
4112 case WSAEINVAL:
4113 PyErr_SetString(
4114 PyExc_ImportError,
4115 "WSAStartup failed: requested version not supported");
4116 break;
4117 default:
4118 PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret);
4119 break;
4120 }
4121 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004122}
4123
Guido van Rossum8d665e61996-06-26 18:22:49 +00004124#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004125
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004126
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004127#ifdef PYOS_OS2
4128#define OS_INIT_DEFINED
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004129
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004130/* Additional initialization for OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004131
4132static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004133os_init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004134{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004135#ifndef PYCC_GCC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004136 int rc = sock_init();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004137
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004138 if (rc == 0) {
4139 return 1; /* Success */
4140 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004141
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004142 PyErr_Format(PyExc_ImportError, "OS/2 TCP/IP Error# %d", sock_errno());
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004143
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004144 return 0; /* Failure */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00004145#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004146 /* No need to initialise sockets with GCC/EMX */
4147 return 1; /* Success */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00004148#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004149}
4150
4151#endif /* PYOS_OS2 */
4152
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004153
4154#ifndef OS_INIT_DEFINED
4155static int
4156os_init(void)
4157{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004158 return 1; /* Success */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004159}
4160#endif
4161
4162
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004163/* C API table - always add new things to the end for binary
4164 compatibility. */
4165static
4166PySocketModule_APIObject PySocketModuleAPI =
4167{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004168 &sock_type,
4169 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004170};
4171
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004172
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004173/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004174
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004175 This module is actually called "_socket", and there's a wrapper
Guido van Rossum7d0a8262007-05-21 23:13:11 +00004176 "socket.py" which implements some additional functionality.
4177 The import of "_socket" may fail with an ImportError exception if
4178 os-specific initialization fails. On Windows, this does WINSOCK
4179 initialization. When WINSOCK is initialized succesfully, a call to
4180 WSACleanup() is scheduled to be made at exit time.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004181*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004182
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004183PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004184"Implementation module for socket operations.\n\
4185\n\
4186See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004187
Martin v. Löwis1a214512008-06-11 05:26:20 +00004188static struct PyModuleDef socketmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004189 PyModuleDef_HEAD_INIT,
4190 PySocket_MODULE_NAME,
4191 socket_doc,
4192 -1,
4193 socket_methods,
4194 NULL,
4195 NULL,
4196 NULL,
4197 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00004198};
4199
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004200PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00004201PyInit__socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004202{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004203 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00004204
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004205 if (!os_init())
4206 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004207
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004208 Py_TYPE(&sock_type) = &PyType_Type;
4209 m = PyModule_Create(&socketmodule);
4210 if (m == NULL)
4211 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004212
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004213 socket_error = PyErr_NewException("socket.error",
4214 PyExc_IOError, NULL);
4215 if (socket_error == NULL)
4216 return NULL;
4217 PySocketModuleAPI.error = socket_error;
4218 Py_INCREF(socket_error);
4219 PyModule_AddObject(m, "error", socket_error);
4220 socket_herror = PyErr_NewException("socket.herror",
4221 socket_error, NULL);
4222 if (socket_herror == NULL)
4223 return NULL;
4224 Py_INCREF(socket_herror);
4225 PyModule_AddObject(m, "herror", socket_herror);
4226 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
4227 NULL);
4228 if (socket_gaierror == NULL)
4229 return NULL;
4230 Py_INCREF(socket_gaierror);
4231 PyModule_AddObject(m, "gaierror", socket_gaierror);
4232 socket_timeout = PyErr_NewException("socket.timeout",
4233 socket_error, NULL);
4234 if (socket_timeout == NULL)
4235 return NULL;
4236 Py_INCREF(socket_timeout);
4237 PyModule_AddObject(m, "timeout", socket_timeout);
4238 Py_INCREF((PyObject *)&sock_type);
4239 if (PyModule_AddObject(m, "SocketType",
4240 (PyObject *)&sock_type) != 0)
4241 return NULL;
4242 Py_INCREF((PyObject *)&sock_type);
4243 if (PyModule_AddObject(m, "socket",
4244 (PyObject *)&sock_type) != 0)
4245 return NULL;
Guido van Rossum09be4091999-08-09 14:40:40 +00004246
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004247#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004248 has_ipv6 = Py_True;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004249#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004250 has_ipv6 = Py_False;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004251#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004252 Py_INCREF(has_ipv6);
4253 PyModule_AddObject(m, "has_ipv6", has_ipv6);
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004254
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004255 /* Export C API */
4256 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
4257 PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL)
4258 ) != 0)
4259 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004260
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004261 /* Address families (we only support AF_INET and AF_UNIX) */
Guido van Rossum09be4091999-08-09 14:40:40 +00004262#ifdef AF_UNSPEC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004263 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00004264#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004265 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004266#ifdef AF_INET6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004267 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004268#endif /* AF_INET6 */
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00004269#if defined(AF_UNIX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004270 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004271#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00004272#ifdef AF_AX25
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004273 /* Amateur Radio AX.25 */
4274 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004275#endif
4276#ifdef AF_IPX
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004277 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00004278#endif
4279#ifdef AF_APPLETALK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004280 /* Appletalk DDP */
4281 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004282#endif
4283#ifdef AF_NETROM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004284 /* Amateur radio NetROM */
4285 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004286#endif
4287#ifdef AF_BRIDGE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004288 /* Multiprotocol bridge */
4289 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004290#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004291#ifdef AF_ATMPVC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004292 /* ATM PVCs */
4293 PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004294#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004295#ifdef AF_AAL5
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004296 /* Reserved for Werner's ATM */
4297 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00004298#endif
4299#ifdef AF_X25
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004300 /* Reserved for X.25 project */
4301 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004302#endif
4303#ifdef AF_INET6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004304 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00004305#endif
4306#ifdef AF_ROSE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004307 /* Amateur Radio X.25 PLP */
4308 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004309#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004310#ifdef AF_DECnet
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004311 /* Reserved for DECnet project */
4312 PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004313#endif
4314#ifdef AF_NETBEUI
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004315 /* Reserved for 802.2LLC project */
4316 PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004317#endif
4318#ifdef AF_SECURITY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004319 /* Security callback pseudo AF */
4320 PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004321#endif
4322#ifdef AF_KEY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004323 /* PF_KEY key management API */
4324 PyModule_AddIntConstant(m, "AF_KEY", AF_KEY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004325#endif
4326#ifdef AF_NETLINK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004327 /* */
4328 PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);
4329 PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004330#ifdef NETLINK_SKIP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004331 PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004332#endif
4333#ifdef NETLINK_W1
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004334 PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004335#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004336 PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK);
4337 PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004338#ifdef NETLINK_TCPDIAG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004339 PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004340#endif
4341#ifdef NETLINK_NFLOG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004342 PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004343#endif
Neal Norwitz65851662006-01-16 04:31:40 +00004344#ifdef NETLINK_XFRM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004345 PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM);
Neal Norwitz65851662006-01-16 04:31:40 +00004346#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004347#ifdef NETLINK_ARPD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004348 PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004349#endif
4350#ifdef NETLINK_ROUTE6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004351 PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004352#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004353 PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW);
Thomas Wouterscf297e42007-02-23 15:07:44 +00004354#ifdef NETLINK_DNRTMSG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004355 PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG);
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004356#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004357#ifdef NETLINK_TAPBASE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004358 PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004359#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004360#endif /* AF_NETLINK */
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004361#ifdef AF_ROUTE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004362 /* Alias to emulate 4.4BSD */
4363 PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004364#endif
4365#ifdef AF_ASH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004366 /* Ash */
4367 PyModule_AddIntConstant(m, "AF_ASH", AF_ASH);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004368#endif
4369#ifdef AF_ECONET
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004370 /* Acorn Econet */
4371 PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004372#endif
4373#ifdef AF_ATMSVC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004374 /* ATM SVCs */
4375 PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004376#endif
4377#ifdef AF_SNA
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004378 /* Linux SNA Project (nutters!) */
4379 PyModule_AddIntConstant(m, "AF_SNA", AF_SNA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004380#endif
4381#ifdef AF_IRDA
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004382 /* IRDA sockets */
4383 PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004384#endif
4385#ifdef AF_PPPOX
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004386 /* PPPoX sockets */
4387 PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004388#endif
4389#ifdef AF_WANPIPE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004390 /* Wanpipe API Sockets */
4391 PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004392#endif
4393#ifdef AF_LLC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004394 /* Linux LLC */
4395 PyModule_AddIntConstant(m, "AF_LLC", AF_LLC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004396#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00004397
Hye-Shik Chang81268602004-02-02 06:05:24 +00004398#ifdef USE_BLUETOOTH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004399 PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);
4400 PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);
4401 PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI);
4402 PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI);
4403 PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER);
Hye-Shik Chang81268602004-02-02 06:05:24 +00004404#if !defined(__FreeBSD__)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004405 PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP);
4406 PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR);
4407 PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00004408#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004409 PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);
4410 PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
4411 PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
Martin v. Löwis12af0482004-01-31 12:34:17 +00004412#endif
4413
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00004414#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004415 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
4416 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
4417 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
4418 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
4419 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
4420 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
4421 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
4422 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
4423 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00004424#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004425
Christian Heimes043d6f62008-01-07 17:19:16 +00004426#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004427 PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC);
Christian Heimes043d6f62008-01-07 17:19:16 +00004428
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004429 /* for addresses */
4430 PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ);
4431 PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME);
4432 PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID);
Christian Heimes043d6f62008-01-07 17:19:16 +00004433
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004434 PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE);
4435 PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE);
4436 PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE);
Christian Heimes043d6f62008-01-07 17:19:16 +00004437
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004438 /* for setsockopt() */
4439 PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC);
4440 PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE);
4441 PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE);
4442 PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE",
4443 TIPC_DEST_DROPPABLE);
4444 PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT);
Christian Heimes043d6f62008-01-07 17:19:16 +00004445
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004446 PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE",
4447 TIPC_LOW_IMPORTANCE);
4448 PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE",
4449 TIPC_MEDIUM_IMPORTANCE);
4450 PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE",
4451 TIPC_HIGH_IMPORTANCE);
4452 PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE",
4453 TIPC_CRITICAL_IMPORTANCE);
Christian Heimes043d6f62008-01-07 17:19:16 +00004454
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004455 /* for subscriptions */
4456 PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS);
4457 PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE);
Christian Heimes25bb7832008-01-11 16:17:00 +00004458#ifdef TIPC_SUB_CANCEL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004459 /* doesn't seem to be available everywhere */
4460 PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL);
Christian Heimes25bb7832008-01-11 16:17:00 +00004461#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004462 PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER);
4463 PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED);
4464 PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN);
4465 PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT);
4466 PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV);
4467 PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV);
Christian Heimes043d6f62008-01-07 17:19:16 +00004468#endif
4469
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004470 /* Socket types */
4471 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
4472 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004473/* We have incomplete socket support. */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004474 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
4475 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00004476#if defined(SOCK_RDM)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004477 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004478#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004479
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004480#ifdef SO_DEBUG
4481 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004482#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004483#ifdef SO_ACCEPTCONN
4484 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004485#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004486#ifdef SO_REUSEADDR
4487 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004488#endif
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00004489#ifdef SO_EXCLUSIVEADDRUSE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004490 PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE);
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00004491#endif
4492
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004493#ifdef SO_KEEPALIVE
4494 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004495#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004496#ifdef SO_DONTROUTE
4497 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004498#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004499#ifdef SO_BROADCAST
4500 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004501#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004502#ifdef SO_USELOOPBACK
4503 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004504#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004505#ifdef SO_LINGER
4506 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004507#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004508#ifdef SO_OOBINLINE
4509 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004510#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004511#ifdef SO_REUSEPORT
4512 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004513#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004514#ifdef SO_SNDBUF
4515 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004516#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004517#ifdef SO_RCVBUF
4518 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004519#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004520#ifdef SO_SNDLOWAT
4521 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004522#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004523#ifdef SO_RCVLOWAT
4524 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004525#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004526#ifdef SO_SNDTIMEO
4527 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004528#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004529#ifdef SO_RCVTIMEO
4530 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004531#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004532#ifdef SO_ERROR
4533 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004534#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004535#ifdef SO_TYPE
4536 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004537#endif
4538
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004539 /* Maximum number of connections for "listen" */
4540#ifdef SOMAXCONN
4541 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004542#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004543 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004544#endif
4545
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004546 /* Flags for send, recv */
4547#ifdef MSG_OOB
4548 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004549#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004550#ifdef MSG_PEEK
4551 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004552#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004553#ifdef MSG_DONTROUTE
4554 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004555#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004556#ifdef MSG_DONTWAIT
4557 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00004558#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004559#ifdef MSG_EOR
4560 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004561#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004562#ifdef MSG_TRUNC
4563 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004564#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004565#ifdef MSG_CTRUNC
4566 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004567#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004568#ifdef MSG_WAITALL
4569 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004570#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004571#ifdef MSG_BTAG
4572 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004573#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004574#ifdef MSG_ETAG
4575 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004576#endif
4577
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004578 /* Protocol level and numbers, usable for [gs]etsockopt */
4579#ifdef SOL_SOCKET
4580 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004581#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004582#ifdef SOL_IP
4583 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004584#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004585 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00004586#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004587#ifdef SOL_IPX
4588 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00004589#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004590#ifdef SOL_AX25
4591 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004592#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004593#ifdef SOL_ATALK
4594 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004595#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004596#ifdef SOL_NETROM
4597 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004598#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004599#ifdef SOL_ROSE
4600 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004601#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004602#ifdef SOL_TCP
4603 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004604#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004605 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00004606#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004607#ifdef SOL_UDP
4608 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004609#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004610 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00004611#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004612#ifdef IPPROTO_IP
4613 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00004614#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004615 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004616#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004617#ifdef IPPROTO_HOPOPTS
4618 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004619#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004620#ifdef IPPROTO_ICMP
4621 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00004622#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004623 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004624#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004625#ifdef IPPROTO_IGMP
4626 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004627#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004628#ifdef IPPROTO_GGP
4629 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004630#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004631#ifdef IPPROTO_IPV4
4632 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004633#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004634#ifdef IPPROTO_IPV6
4635 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisa0f17342003-10-03 13:56:20 +00004636#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004637#ifdef IPPROTO_IPIP
4638 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004639#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004640#ifdef IPPROTO_TCP
4641 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00004642#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004643 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004644#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004645#ifdef IPPROTO_EGP
4646 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004647#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004648#ifdef IPPROTO_PUP
4649 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004650#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004651#ifdef IPPROTO_UDP
4652 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00004653#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004654 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004655#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004656#ifdef IPPROTO_IDP
4657 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004658#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004659#ifdef IPPROTO_HELLO
4660 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004661#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004662#ifdef IPPROTO_ND
4663 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004664#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004665#ifdef IPPROTO_TP
4666 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004667#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004668#ifdef IPPROTO_IPV6
4669 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004670#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004671#ifdef IPPROTO_ROUTING
4672 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004673#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004674#ifdef IPPROTO_FRAGMENT
4675 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004676#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004677#ifdef IPPROTO_RSVP
4678 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004679#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004680#ifdef IPPROTO_GRE
4681 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004682#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004683#ifdef IPPROTO_ESP
4684 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004685#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004686#ifdef IPPROTO_AH
4687 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004688#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004689#ifdef IPPROTO_MOBILE
4690 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004691#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004692#ifdef IPPROTO_ICMPV6
4693 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004694#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004695#ifdef IPPROTO_NONE
4696 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004697#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004698#ifdef IPPROTO_DSTOPTS
4699 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004700#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004701#ifdef IPPROTO_XTP
4702 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004703#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004704#ifdef IPPROTO_EON
4705 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004706#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004707#ifdef IPPROTO_PIM
4708 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004709#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004710#ifdef IPPROTO_IPCOMP
4711 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004712#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004713#ifdef IPPROTO_VRRP
4714 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004715#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004716#ifdef IPPROTO_BIP
4717 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004718#endif
4719/**/
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004720#ifdef IPPROTO_RAW
4721 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00004722#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004723 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004724#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004725#ifdef IPPROTO_MAX
4726 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004727#endif
4728
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004729 /* Some port configuration */
4730#ifdef IPPORT_RESERVED
4731 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004732#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004733 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004734#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004735#ifdef IPPORT_USERRESERVED
4736 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004737#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004738 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004739#endif
4740
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004741 /* Some reserved IP v.4 addresses */
4742#ifdef INADDR_ANY
4743 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004744#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004745 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004746#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004747#ifdef INADDR_BROADCAST
4748 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004749#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004750 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004751#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004752#ifdef INADDR_LOOPBACK
4753 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004754#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004755 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004756#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004757#ifdef INADDR_UNSPEC_GROUP
4758 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004759#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004760 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004761#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004762#ifdef INADDR_ALLHOSTS_GROUP
4763 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
4764 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004765#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004766 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004767#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004768#ifdef INADDR_MAX_LOCAL_GROUP
4769 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
4770 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004771#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004772 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004773#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004774#ifdef INADDR_NONE
4775 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004776#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004777 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004778#endif
4779
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004780 /* IPv4 [gs]etsockopt options */
4781#ifdef IP_OPTIONS
4782 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004783#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004784#ifdef IP_HDRINCL
4785 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004786#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004787#ifdef IP_TOS
4788 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004789#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004790#ifdef IP_TTL
4791 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004792#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004793#ifdef IP_RECVOPTS
4794 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004795#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004796#ifdef IP_RECVRETOPTS
4797 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004798#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004799#ifdef IP_RECVDSTADDR
4800 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004801#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004802#ifdef IP_RETOPTS
4803 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004804#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004805#ifdef IP_MULTICAST_IF
4806 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004807#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004808#ifdef IP_MULTICAST_TTL
4809 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004810#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004811#ifdef IP_MULTICAST_LOOP
4812 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004813#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004814#ifdef IP_ADD_MEMBERSHIP
4815 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004816#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004817#ifdef IP_DROP_MEMBERSHIP
4818 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004819#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004820#ifdef IP_DEFAULT_MULTICAST_TTL
4821 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
4822 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00004823#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004824#ifdef IP_DEFAULT_MULTICAST_LOOP
4825 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
4826 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004827#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004828#ifdef IP_MAX_MEMBERSHIPS
4829 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00004830#endif
4831
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004832 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
4833#ifdef IPV6_JOIN_GROUP
4834 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004835#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004836#ifdef IPV6_LEAVE_GROUP
4837 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004838#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004839#ifdef IPV6_MULTICAST_HOPS
4840 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004841#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004842#ifdef IPV6_MULTICAST_IF
4843 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004844#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004845#ifdef IPV6_MULTICAST_LOOP
4846 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004847#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004848#ifdef IPV6_UNICAST_HOPS
4849 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004850#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004851 /* Additional IPV6 socket options, defined in RFC 3493 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00004852#ifdef IPV6_V6ONLY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004853 PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004854#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004855 /* Advanced IPV6 socket options, from RFC 3542 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00004856#ifdef IPV6_CHECKSUM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004857 PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004858#endif
4859#ifdef IPV6_DONTFRAG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004860 PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004861#endif
4862#ifdef IPV6_DSTOPTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004863 PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004864#endif
4865#ifdef IPV6_HOPLIMIT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004866 PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004867#endif
4868#ifdef IPV6_HOPOPTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004869 PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004870#endif
4871#ifdef IPV6_NEXTHOP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004872 PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004873#endif
4874#ifdef IPV6_PATHMTU
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004875 PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004876#endif
4877#ifdef IPV6_PKTINFO
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004878 PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004879#endif
4880#ifdef IPV6_RECVDSTOPTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004881 PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004882#endif
4883#ifdef IPV6_RECVHOPLIMIT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004884 PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004885#endif
4886#ifdef IPV6_RECVHOPOPTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004887 PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004888#endif
4889#ifdef IPV6_RECVPKTINFO
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004890 PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004891#endif
4892#ifdef IPV6_RECVRTHDR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004893 PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004894#endif
4895#ifdef IPV6_RECVTCLASS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004896 PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004897#endif
4898#ifdef IPV6_RTHDR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004899 PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004900#endif
4901#ifdef IPV6_RTHDRDSTOPTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004902 PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004903#endif
4904#ifdef IPV6_RTHDR_TYPE_0
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004905 PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004906#endif
4907#ifdef IPV6_RECVPATHMTU
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004908 PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004909#endif
4910#ifdef IPV6_TCLASS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004911 PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004912#endif
4913#ifdef IPV6_USE_MIN_MTU
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004914 PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004915#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004916
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004917 /* TCP options */
4918#ifdef TCP_NODELAY
4919 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00004920#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004921#ifdef TCP_MAXSEG
4922 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00004923#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004924#ifdef TCP_CORK
4925 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004926#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004927#ifdef TCP_KEEPIDLE
4928 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004929#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004930#ifdef TCP_KEEPINTVL
4931 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004932#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004933#ifdef TCP_KEEPCNT
4934 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004935#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004936#ifdef TCP_SYNCNT
4937 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004938#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004939#ifdef TCP_LINGER2
4940 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004941#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004942#ifdef TCP_DEFER_ACCEPT
4943 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004944#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004945#ifdef TCP_WINDOW_CLAMP
4946 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004947#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004948#ifdef TCP_INFO
4949 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004950#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004951#ifdef TCP_QUICKACK
4952 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004953#endif
4954
Guido van Rossum09be4091999-08-09 14:40:40 +00004955
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004956 /* IPX options */
4957#ifdef IPX_TYPE
4958 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004959#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004960
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004961 /* get{addr,name}info parameters */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004962#ifdef EAI_ADDRFAMILY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004963 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004964#endif
4965#ifdef EAI_AGAIN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004966 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004967#endif
4968#ifdef EAI_BADFLAGS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004969 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004970#endif
4971#ifdef EAI_FAIL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004972 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004973#endif
4974#ifdef EAI_FAMILY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004975 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004976#endif
4977#ifdef EAI_MEMORY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004978 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004979#endif
4980#ifdef EAI_NODATA
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004981 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004982#endif
4983#ifdef EAI_NONAME
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004984 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004985#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004986#ifdef EAI_OVERFLOW
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004987 PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004988#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004989#ifdef EAI_SERVICE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004990 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004991#endif
4992#ifdef EAI_SOCKTYPE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004993 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004994#endif
4995#ifdef EAI_SYSTEM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004996 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004997#endif
4998#ifdef EAI_BADHINTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004999 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005000#endif
5001#ifdef EAI_PROTOCOL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005002 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005003#endif
5004#ifdef EAI_MAX
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005005 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005006#endif
5007#ifdef AI_PASSIVE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005008 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005009#endif
5010#ifdef AI_CANONNAME
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005011 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005012#endif
5013#ifdef AI_NUMERICHOST
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005014 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005015#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00005016#ifdef AI_NUMERICSERV
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005017 PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005018#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005019#ifdef AI_MASK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005020 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005021#endif
5022#ifdef AI_ALL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005023 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005024#endif
5025#ifdef AI_V4MAPPED_CFG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005026 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005027#endif
5028#ifdef AI_ADDRCONFIG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005029 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005030#endif
5031#ifdef AI_V4MAPPED
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005032 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005033#endif
5034#ifdef AI_DEFAULT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005035 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005036#endif
5037#ifdef NI_MAXHOST
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005038 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005039#endif
5040#ifdef NI_MAXSERV
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005041 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005042#endif
5043#ifdef NI_NOFQDN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005044 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005045#endif
5046#ifdef NI_NUMERICHOST
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005047 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005048#endif
5049#ifdef NI_NAMEREQD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005050 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005051#endif
5052#ifdef NI_NUMERICSERV
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005053 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005054#endif
5055#ifdef NI_DGRAM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005056 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005057#endif
5058
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005059 /* shutdown() parameters */
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005060#ifdef SHUT_RD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005061 PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005062#elif defined(SD_RECEIVE)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005063 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005064#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005065 PyModule_AddIntConstant(m, "SHUT_RD", 0);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005066#endif
5067#ifdef SHUT_WR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005068 PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005069#elif defined(SD_SEND)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005070 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005071#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005072 PyModule_AddIntConstant(m, "SHUT_WR", 1);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005073#endif
5074#ifdef SHUT_RDWR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005075 PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005076#elif defined(SD_BOTH)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005077 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005078#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005079 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005080#endif
5081
Christian Heimesfaf2f632008-01-06 16:59:19 +00005082#ifdef SIO_RCVALL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005083 {
5084 PyObject *tmp;
5085 tmp = PyLong_FromUnsignedLong(SIO_RCVALL);
5086 if (tmp == NULL)
5087 return NULL;
5088 PyModule_AddObject(m, "SIO_RCVALL", tmp);
5089 }
5090 PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF);
5091 PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON);
5092 PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00005093#ifdef RCVALL_IPLEVEL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005094 PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00005095#endif
5096#ifdef RCVALL_MAX
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005097 PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00005098#endif
Christian Heimesfaf2f632008-01-06 16:59:19 +00005099#endif /* _MSTCPIP_ */
5100
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005101 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00005102#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005103 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00005104#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005105 return m;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005106}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005107
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005108
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005109#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +00005110#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005111
5112/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005113/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005114
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005115int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005116inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005117{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005118 if (af == AF_INET) {
Benjamin Petersonf91df042009-02-13 02:50:59 +00005119#if (SIZEOF_INT != 4)
5120#error "Not sure if in_addr_t exists and int is not 32-bits."
5121#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005122 unsigned int packed_addr;
5123 packed_addr = inet_addr(src);
5124 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;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005131}
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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005136 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;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005146}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005147
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005148#endif
Christian Heimesb6150692007-11-15 23:37:07 +00005149#endif