blob: b04d246f1d05859e21ec6ce6a772ecc62fe96864 [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
Gregory P. Smith397cd8a2010-10-17 04:23:21 +0000382#if (defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)) && !defined(__NetBSD__) && !defined(__DragonFly__)
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)
Gregory P. Smith397cd8a2010-10-17 04:23:21 +0000396#elif defined(__NetBSD__) || defined(__DragonFly__)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000397#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
Gregory P. Smith397cd8a2010-10-17 04:23:21 +0000401#define SOL_HCI BTPROTO_HCI
402#define HCI_DATA_DIR SO_HCI_DIRECTION
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000403#define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb)
404#define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000405#define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000406#define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000407#else
Hye-Shik Chang81268602004-02-02 06:05:24 +0000408#define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)
409#define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000410#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000411#define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)
412#endif
413#endif
414
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000415#ifdef __VMS
416/* TCP/IP Services for VMS uses a maximum send/recv buffer length */
417#define SEGMENT_SIZE (32 * 1024 -1)
418#endif
419
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000420#define SAS2SA(x) ((struct sockaddr *)(x))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000421
Martin v. Löwise9416172003-05-03 10:12:45 +0000422/*
423 * Constants for getnameinfo()
424 */
425#if !defined(NI_MAXHOST)
426#define NI_MAXHOST 1025
427#endif
428#if !defined(NI_MAXSERV)
429#define NI_MAXSERV 32
430#endif
431
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000432#ifndef INVALID_SOCKET /* MS defines this */
433#define INVALID_SOCKET (-1)
434#endif
435
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000436/* XXX There's a problem here: *static* functions are not supposed to have
437 a Py prefix (or use CapitalizedWords). Later... */
438
Guido van Rossum30a685f1991-06-27 15:51:29 +0000439/* Global variable holding the exception type for errors detected
440 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000441static PyObject *socket_error;
442static PyObject *socket_herror;
443static PyObject *socket_gaierror;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000444static PyObject *socket_timeout;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000445
Tim Peters643a7fc2002-02-17 04:13:21 +0000446/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000447 The sock_type variable contains pointers to various functions,
448 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000449 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000450static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000451
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000452#if defined(HAVE_POLL_H)
453#include <poll.h>
454#elif defined(HAVE_SYS_POLL_H)
455#include <sys/poll.h>
456#endif
457
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000458#ifdef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE
459/* Platform can select file descriptors beyond FD_SETSIZE */
460#define IS_SELECTABLE(s) 1
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000461#elif defined(HAVE_POLL)
462/* Instead of select(), we'll use poll() since poll() works on any fd. */
463#define IS_SELECTABLE(s) 1
464/* Can we call select() with this socket without a buffer overrun? */
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000465#else
466/* POSIX says selecting file descriptors beyond FD_SETSIZE
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000467 has undefined behaviour. If there's no timeout left, we don't have to
468 call select, so it's a safe, little white lie. */
469#define IS_SELECTABLE(s) ((s)->sock_fd < FD_SETSIZE || s->sock_timeout <= 0.0)
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000470#endif
Neal Norwitz082b2df2006-02-07 07:04:46 +0000471
472static PyObject*
473select_error(void)
474{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000475 PyErr_SetString(socket_error, "unable to select on socket");
476 return NULL;
Neal Norwitz082b2df2006-02-07 07:04:46 +0000477}
478
Guido van Rossum30a685f1991-06-27 15:51:29 +0000479/* Convenience function to raise an error according to errno
480 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000481
Guido van Rossum73624e91994-10-10 17:59:00 +0000482static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000483set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000484{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000485#ifdef MS_WINDOWS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000486 int err_no = WSAGetLastError();
487 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
488 recognizes the error codes used by both GetLastError() and
489 WSAGetLastError */
490 if (err_no)
491 return PyErr_SetExcFromWindowsErr(socket_error, err_no);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000492#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000493
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000494#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000495 if (sock_errno() != NO_ERROR) {
496 APIRET rc;
497 ULONG msglen;
498 char outbuf[100];
499 int myerrorcode = sock_errno();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000500
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000501 /* Retrieve socket-related error message from MPTN.MSG file */
502 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
503 myerrorcode - SOCBASEERR + 26,
504 "mptn.msg",
505 &msglen);
506 if (rc == NO_ERROR) {
507 PyObject *v;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000508
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000509 /* OS/2 doesn't guarantee a terminator */
510 outbuf[msglen] = '\0';
511 if (strlen(outbuf) > 0) {
512 /* If non-empty msg, trim CRLF */
513 char *lastc = &outbuf[ strlen(outbuf)-1 ];
514 while (lastc > outbuf &&
515 isspace(Py_CHARMASK(*lastc))) {
516 /* Trim trailing whitespace (CRLF) */
517 *lastc-- = '\0';
518 }
519 }
520 v = Py_BuildValue("(is)", myerrorcode, outbuf);
521 if (v != NULL) {
522 PyErr_SetObject(socket_error, v);
523 Py_DECREF(v);
524 }
525 return NULL;
526 }
527 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000528#endif
529
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000530 return PyErr_SetFromErrno(socket_error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000531}
532
Guido van Rossum30a685f1991-06-27 15:51:29 +0000533
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000534static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000535set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000536{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000537 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000538
539#ifdef HAVE_HSTRERROR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000540 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000541#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000542 v = Py_BuildValue("(is)", h_error, "host not found");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000543#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000544 if (v != NULL) {
545 PyErr_SetObject(socket_herror, v);
546 Py_DECREF(v);
547 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000548
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000549 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000550}
551
552
553static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000554set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000555{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000556 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000557
Martin v. Löwis272cb402002-03-01 08:31:07 +0000558#ifdef EAI_SYSTEM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000559 /* EAI_SYSTEM is not available on Windows XP. */
560 if (error == EAI_SYSTEM)
561 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000562#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000563
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000564#ifdef HAVE_GAI_STRERROR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000565 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000566#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000567 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000568#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000569 if (v != NULL) {
570 PyErr_SetObject(socket_gaierror, v);
571 Py_DECREF(v);
572 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000573
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000574 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000575}
576
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000577#ifdef __VMS
578/* Function to send in segments */
579static int
580sendsegmented(int sock_fd, char *buf, int len, int flags)
581{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000582 int n = 0;
583 int remaining = len;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000584
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000585 while (remaining > 0) {
586 unsigned int segment;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000587
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000588 segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining);
589 n = send(sock_fd, buf, segment, flags);
590 if (n < 0) {
591 return n;
592 }
593 remaining -= segment;
594 buf += segment;
595 } /* end while */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000596
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000597 return len;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000598}
599#endif
600
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000601/* Function to perform the setting of socket blocking mode
602 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000603static int
604internal_setblocking(PySocketSockObject *s, int block)
605{
Guido van Rossum67f7a382002-06-06 21:08:16 +0000606#ifndef MS_WINDOWS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000607 int delay_flag;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000608#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +0000609
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000610 Py_BEGIN_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000611#ifndef MS_WINDOWS
612#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000613 block = !block;
614 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000615#elif defined(__VMS)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000616 block = !block;
617 ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000618#else /* !PYOS_OS2 && !__VMS */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000619 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
620 if (block)
621 delay_flag &= (~O_NONBLOCK);
622 else
623 delay_flag |= O_NONBLOCK;
624 fcntl(s->sock_fd, F_SETFL, delay_flag);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000625#endif /* !PYOS_OS2 */
626#else /* MS_WINDOWS */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000627 block = !block;
628 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000629#endif /* MS_WINDOWS */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000630 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000631
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000632 /* Since these don't return anything */
633 return 1;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000634}
635
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000636/* Do a select()/poll() on the socket, if necessary (sock_timeout > 0).
Guido van Rossum11ba0942002-06-13 15:07:44 +0000637 The argument writing indicates the direction.
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000638 This does not raise an exception; we'll let our caller do that
639 after they've reacquired the interpreter lock.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000640 Returns 1 on timeout, -1 on error, 0 otherwise. */
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000641static int
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000642internal_select(PySocketSockObject *s, int writing)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000643{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000644 int n;
Guido van Rossum11ba0942002-06-13 15:07:44 +0000645
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000646 /* Nothing to do unless we're in timeout mode (not non-blocking) */
647 if (s->sock_timeout <= 0.0)
648 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000649
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000650 /* Guard against closed socket */
651 if (s->sock_fd < 0)
652 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000653
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000654 /* Prefer poll, if available, since you can poll() any fd
655 * which can't be done with select(). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000656#ifdef HAVE_POLL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000657 {
658 struct pollfd pollfd;
659 int timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000660
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000661 pollfd.fd = s->sock_fd;
662 pollfd.events = writing ? POLLOUT : POLLIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000663
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000664 /* s->sock_timeout is in seconds, timeout in ms */
665 timeout = (int)(s->sock_timeout * 1000 + 0.5);
666 n = poll(&pollfd, 1, timeout);
667 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000668#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000669 {
670 /* Construct the arguments to select */
671 fd_set fds;
672 struct timeval tv;
673 tv.tv_sec = (int)s->sock_timeout;
674 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
675 FD_ZERO(&fds);
676 FD_SET(s->sock_fd, &fds);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000677
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000678 /* See if the socket is ready */
679 if (writing)
Antoine Pitrouf72006f2010-08-17 19:39:39 +0000680 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
681 NULL, &fds, NULL, &tv);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000682 else
Antoine Pitrouf72006f2010-08-17 19:39:39 +0000683 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
684 &fds, NULL, NULL, &tv);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000685 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000686#endif
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000687
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000688 if (n < 0)
689 return -1;
690 if (n == 0)
691 return 1;
692 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000693}
694
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000695/* Initialize a new socket object. */
696
Tim Petersa12b4cf2002-07-18 22:38:44 +0000697static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000698
Martin v. Löwis1a214512008-06-11 05:26:20 +0000699static void
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000700init_sockobject(PySocketSockObject *s,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000701 SOCKET_T fd, int family, int type, int proto)
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000702{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000703 s->sock_fd = fd;
704 s->sock_family = family;
705 s->sock_type = type;
706 s->sock_proto = proto;
707 s->sock_timeout = defaulttimeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000708
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000709 s->errorhandler = &set_error;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000710
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000711 if (defaulttimeout >= 0.0)
712 internal_setblocking(s, 0);
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000713
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000714}
715
716
Guido van Rossum30a685f1991-06-27 15:51:29 +0000717/* Create a new socket object.
718 This just creates the object and initializes it.
719 If the creation fails, return NULL and set an exception (implicit
720 in NEWOBJ()). */
721
Guido van Rossum73624e91994-10-10 17:59:00 +0000722static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000723new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000724{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000725 PySocketSockObject *s;
726 s = (PySocketSockObject *)
727 PyType_GenericNew(&sock_type, NULL, NULL);
728 if (s != NULL)
729 init_sockobject(s, fd, family, type, proto);
730 return s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000731}
732
Guido van Rossum30a685f1991-06-27 15:51:29 +0000733
Guido van Rossum48a680c2001-03-02 06:34:14 +0000734/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000735 thread to be in gethostbyname or getaddrinfo */
736#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
737PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000738#endif
739
740
Guido van Rossum30a685f1991-06-27 15:51:29 +0000741/* Convert a string specifying a host name or one of a few symbolic
742 names to a numeric IP address. This usually calls gethostbyname()
743 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000744 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000745 an error occurred; then an exception is raised. */
746
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000747static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000748setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000749{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000750 struct addrinfo hints, *res;
751 int error;
752 int d1, d2, d3, d4;
753 char ch;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000754
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000755 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
756 if (name[0] == '\0') {
757 int siz;
758 memset(&hints, 0, sizeof(hints));
759 hints.ai_family = af;
760 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
761 hints.ai_flags = AI_PASSIVE;
762 Py_BEGIN_ALLOW_THREADS
763 ACQUIRE_GETADDRINFO_LOCK
764 error = getaddrinfo(NULL, "0", &hints, &res);
765 Py_END_ALLOW_THREADS
766 /* We assume that those thread-unsafe getaddrinfo() versions
767 *are* safe regarding their return value, ie. that a
768 subsequent call to getaddrinfo() does not destroy the
769 outcome of the first call. */
770 RELEASE_GETADDRINFO_LOCK
771 if (error) {
772 set_gaierror(error);
773 return -1;
774 }
775 switch (res->ai_family) {
776 case AF_INET:
777 siz = 4;
778 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000779#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000780 case AF_INET6:
781 siz = 16;
782 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000783#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000784 default:
785 freeaddrinfo(res);
786 PyErr_SetString(socket_error,
787 "unsupported address family");
788 return -1;
789 }
790 if (res->ai_next) {
791 freeaddrinfo(res);
792 PyErr_SetString(socket_error,
793 "wildcard resolved to multiple address");
794 return -1;
795 }
796 if (res->ai_addrlen < addr_ret_size)
797 addr_ret_size = res->ai_addrlen;
798 memcpy(addr_ret, res->ai_addr, addr_ret_size);
799 freeaddrinfo(res);
800 return siz;
801 }
802 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
803 struct sockaddr_in *sin;
804 if (af != AF_INET && af != AF_UNSPEC) {
805 PyErr_SetString(socket_error,
806 "address family mismatched");
807 return -1;
808 }
809 sin = (struct sockaddr_in *)addr_ret;
810 memset((void *) sin, '\0', sizeof(*sin));
811 sin->sin_family = AF_INET;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000812#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000813 sin->sin_len = sizeof(*sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000814#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000815 sin->sin_addr.s_addr = INADDR_BROADCAST;
816 return sizeof(sin->sin_addr);
817 }
818 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
819 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
820 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
821 struct sockaddr_in *sin;
822 sin = (struct sockaddr_in *)addr_ret;
823 sin->sin_addr.s_addr = htonl(
824 ((long) d1 << 24) | ((long) d2 << 16) |
825 ((long) d3 << 8) | ((long) d4 << 0));
826 sin->sin_family = AF_INET;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000827#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000828 sin->sin_len = sizeof(*sin);
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000829#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000830 return 4;
831 }
832 memset(&hints, 0, sizeof(hints));
833 hints.ai_family = af;
834 Py_BEGIN_ALLOW_THREADS
835 ACQUIRE_GETADDRINFO_LOCK
836 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000837#if defined(__digital__) && defined(__unix__)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000838 if (error == EAI_NONAME && af == AF_UNSPEC) {
839 /* On Tru64 V5.1, numeric-to-addr conversion fails
840 if no address family is given. Assume IPv4 for now.*/
841 hints.ai_family = AF_INET;
842 error = getaddrinfo(name, NULL, &hints, &res);
843 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000844#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000845 Py_END_ALLOW_THREADS
846 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
847 if (error) {
848 set_gaierror(error);
849 return -1;
850 }
851 if (res->ai_addrlen < addr_ret_size)
852 addr_ret_size = res->ai_addrlen;
853 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
854 freeaddrinfo(res);
855 switch (addr_ret->sa_family) {
856 case AF_INET:
857 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000858#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000859 case AF_INET6:
860 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000861#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000862 default:
863 PyErr_SetString(socket_error, "unknown address family");
864 return -1;
865 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000866}
867
Guido van Rossum30a685f1991-06-27 15:51:29 +0000868
Guido van Rossum30a685f1991-06-27 15:51:29 +0000869/* Create a string object representing an IP address.
870 This is always a string of the form 'dd.dd.dd.dd' (with variable
871 size numbers). */
872
Guido van Rossum73624e91994-10-10 17:59:00 +0000873static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000874makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000875{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000876 char buf[NI_MAXHOST];
877 int error;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000878
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000879 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
880 NI_NUMERICHOST);
881 if (error) {
882 set_gaierror(error);
883 return NULL;
884 }
885 return PyUnicode_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000886}
887
888
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000889#ifdef USE_BLUETOOTH
890/* Convert a string representation of a Bluetooth address into a numeric
891 address. Returns the length (6), or raises an exception and returns -1 if
892 an error occurred. */
893
894static int
895setbdaddr(char *name, bdaddr_t *bdaddr)
896{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000897 unsigned int b0, b1, b2, b3, b4, b5;
898 char ch;
899 int n;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000900
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000901 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
902 &b5, &b4, &b3, &b2, &b1, &b0, &ch);
903 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
904 bdaddr->b[0] = b0;
905 bdaddr->b[1] = b1;
906 bdaddr->b[2] = b2;
907 bdaddr->b[3] = b3;
908 bdaddr->b[4] = b4;
909 bdaddr->b[5] = b5;
910 return 6;
911 } else {
912 PyErr_SetString(socket_error, "bad bluetooth address");
913 return -1;
914 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000915}
916
917/* Create a string representation of the Bluetooth address. This is always a
918 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
919 value (zero padded if necessary). */
920
921static PyObject *
922makebdaddr(bdaddr_t *bdaddr)
923{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000924 char buf[(6 * 2) + 5 + 1];
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000925
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000926 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
927 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
928 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
929 return PyUnicode_FromString(buf);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000930}
931#endif
932
933
Guido van Rossum30a685f1991-06-27 15:51:29 +0000934/* Create an object representing the given socket address,
935 suitable for passing it back to bind(), connect() etc.
936 The family field of the sockaddr structure is inspected
937 to determine what kind of address it really is. */
938
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000939/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000940static PyObject *
Antoine Pitrouf72006f2010-08-17 19:39:39 +0000941makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000942{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000943 if (addrlen == 0) {
944 /* No address -- may be recvfrom() from known socket */
945 Py_INCREF(Py_None);
946 return Py_None;
947 }
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000948
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000949 switch (addr->sa_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +0000950
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000951 case AF_INET:
952 {
953 struct sockaddr_in *a;
954 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
955 PyObject *ret = NULL;
956 if (addrobj) {
957 a = (struct sockaddr_in *)addr;
958 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
959 Py_DECREF(addrobj);
960 }
961 return ret;
962 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000963
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +0000964#if defined(AF_UNIX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000965 case AF_UNIX:
966 {
967 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000968#ifdef linux
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000969 if (a->sun_path[0] == 0) { /* Linux abstract namespace */
970 addrlen -= offsetof(struct sockaddr_un, sun_path);
971 return PyBytes_FromStringAndSize(a->sun_path, addrlen);
972 }
973 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000974#endif /* linux */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000975 {
976 /* regular NULL-terminated string */
977 return PyUnicode_FromString(a->sun_path);
978 }
979 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000980#endif /* AF_UNIX */
981
Martin v. Löwis11017b12006-01-14 18:12:57 +0000982#if defined(AF_NETLINK)
983 case AF_NETLINK:
984 {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000985 struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
986 return Py_BuildValue("II", a->nl_pid, a->nl_groups);
Martin v. Löwis11017b12006-01-14 18:12:57 +0000987 }
988#endif /* AF_NETLINK */
989
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000990#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000991 case AF_INET6:
992 {
993 struct sockaddr_in6 *a;
994 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
995 PyObject *ret = NULL;
996 if (addrobj) {
997 a = (struct sockaddr_in6 *)addr;
998 ret = Py_BuildValue("Oiii",
999 addrobj,
1000 ntohs(a->sin6_port),
1001 a->sin6_flowinfo,
1002 a->sin6_scope_id);
1003 Py_DECREF(addrobj);
1004 }
1005 return ret;
1006 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001007#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001008
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001009#ifdef USE_BLUETOOTH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001010 case AF_BLUETOOTH:
1011 switch (proto) {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001012
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001013 case BTPROTO_L2CAP:
1014 {
1015 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
1016 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
1017 PyObject *ret = NULL;
1018 if (addrobj) {
1019 ret = Py_BuildValue("Oi",
1020 addrobj,
1021 _BT_L2_MEMB(a, psm));
1022 Py_DECREF(addrobj);
1023 }
1024 return ret;
1025 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001026
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001027 case BTPROTO_RFCOMM:
1028 {
1029 struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
1030 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
1031 PyObject *ret = NULL;
1032 if (addrobj) {
1033 ret = Py_BuildValue("Oi",
1034 addrobj,
1035 _BT_RC_MEMB(a, channel));
1036 Py_DECREF(addrobj);
1037 }
1038 return ret;
1039 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001040
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001041 case BTPROTO_HCI:
1042 {
1043 struct sockaddr_hci *a = (struct sockaddr_hci *) addr;
Gregory P. Smith397cd8a2010-10-17 04:23:21 +00001044#if defined(__NetBSD__) || defined(__DragonFly__)
1045 return makebdaddr(&_BT_HCI_MEMB(a, bdaddr));
1046#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001047 PyObject *ret = NULL;
1048 ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
1049 return ret;
Gregory P. Smith397cd8a2010-10-17 04:23:21 +00001050#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001051 }
Thomas Wouterscf297e42007-02-23 15:07:44 +00001052
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001053#if !defined(__FreeBSD__)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001054 case BTPROTO_SCO:
1055 {
1056 struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
1057 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
1058 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001059#endif
1060
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001061 default:
1062 PyErr_SetString(PyExc_ValueError,
1063 "Unknown Bluetooth protocol");
1064 return NULL;
1065 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001066#endif
1067
Antoine Pitrou323dd702010-10-27 20:27:14 +00001068#if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001069 case AF_PACKET:
1070 {
1071 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
1072 char *ifname = "";
1073 struct ifreq ifr;
1074 /* need to look up interface name give index */
1075 if (a->sll_ifindex) {
1076 ifr.ifr_ifindex = a->sll_ifindex;
1077 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
1078 ifname = ifr.ifr_name;
1079 }
1080 return Py_BuildValue("shbhy#",
1081 ifname,
1082 ntohs(a->sll_protocol),
1083 a->sll_pkttype,
1084 a->sll_hatype,
1085 a->sll_addr,
1086 a->sll_halen);
1087 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001088#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001089
Christian Heimes043d6f62008-01-07 17:19:16 +00001090#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001091 case AF_TIPC:
1092 {
1093 struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr;
1094 if (a->addrtype == TIPC_ADDR_NAMESEQ) {
1095 return Py_BuildValue("IIIII",
1096 a->addrtype,
1097 a->addr.nameseq.type,
1098 a->addr.nameseq.lower,
1099 a->addr.nameseq.upper,
1100 a->scope);
1101 } else if (a->addrtype == TIPC_ADDR_NAME) {
1102 return Py_BuildValue("IIIII",
1103 a->addrtype,
1104 a->addr.name.name.type,
1105 a->addr.name.name.instance,
1106 a->addr.name.name.instance,
1107 a->scope);
1108 } else if (a->addrtype == TIPC_ADDR_ID) {
1109 return Py_BuildValue("IIIII",
1110 a->addrtype,
1111 a->addr.id.node,
1112 a->addr.id.ref,
1113 0,
1114 a->scope);
1115 } else {
1116 PyErr_SetString(PyExc_ValueError,
1117 "Invalid address type");
1118 return NULL;
1119 }
1120 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001121#endif
1122
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001123 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001124
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001125 default:
1126 /* If we don't know the address family, don't raise an
1127 exception -- return it as an (int, bytes) tuple. */
1128 return Py_BuildValue("iy#",
1129 addr->sa_family,
1130 addr->sa_data,
1131 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001132
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001133 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001134}
1135
Guido van Rossum30a685f1991-06-27 15:51:29 +00001136
1137/* Parse a socket address argument according to the socket object's
1138 address family. Return 1 if the address was in the proper format,
1139 0 of not. The address is returned through addr_ret, its length
1140 through len_ret. */
1141
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001142static int
Guido van Rossum48a680c2001-03-02 06:34:14 +00001143getsockaddrarg(PySocketSockObject *s, PyObject *args,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001144 struct sockaddr *addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001145{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001146 switch (s->sock_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +00001147
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001148#if defined(AF_UNIX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001149 case AF_UNIX:
1150 {
1151 struct sockaddr_un* addr;
1152 char *path;
1153 int len;
1154 if (!PyArg_Parse(args, "s#", &path, &len))
1155 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001156
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001157 addr = (struct sockaddr_un*)addr_ret;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001158#ifdef linux
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001159 if (len > 0 && path[0] == 0) {
1160 /* Linux abstract namespace extension */
1161 if (len > sizeof addr->sun_path) {
1162 PyErr_SetString(socket_error,
1163 "AF_UNIX path too long");
1164 return 0;
1165 }
1166 }
1167 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001168#endif /* linux */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001169 {
1170 /* regular NULL-terminated string */
1171 if (len >= sizeof addr->sun_path) {
1172 PyErr_SetString(socket_error,
1173 "AF_UNIX path too long");
1174 return 0;
1175 }
1176 addr->sun_path[len] = 0;
1177 }
1178 addr->sun_family = s->sock_family;
1179 memcpy(addr->sun_path, path, len);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001180#if defined(PYOS_OS2)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001181 *len_ret = sizeof(*addr);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001182#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001183 *len_ret = len + offsetof(struct sockaddr_un, sun_path);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001184#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001185 return 1;
1186 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001187#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001188
Martin v. Löwis11017b12006-01-14 18:12:57 +00001189#if defined(AF_NETLINK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001190 case AF_NETLINK:
1191 {
1192 struct sockaddr_nl* addr;
1193 int pid, groups;
1194 addr = (struct sockaddr_nl *)addr_ret;
1195 if (!PyTuple_Check(args)) {
1196 PyErr_Format(
1197 PyExc_TypeError,
1198 "getsockaddrarg: "
1199 "AF_NETLINK address must be tuple, not %.500s",
1200 Py_TYPE(args)->tp_name);
1201 return 0;
1202 }
1203 if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
1204 return 0;
1205 addr->nl_family = AF_NETLINK;
1206 addr->nl_pid = pid;
1207 addr->nl_groups = groups;
1208 *len_ret = sizeof(*addr);
1209 return 1;
1210 }
Martin v. Löwis11017b12006-01-14 18:12:57 +00001211#endif
1212
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001213 case AF_INET:
1214 {
1215 struct sockaddr_in* addr;
1216 char *host;
1217 int port, result;
1218 if (!PyTuple_Check(args)) {
1219 PyErr_Format(
1220 PyExc_TypeError,
1221 "getsockaddrarg: "
1222 "AF_INET address must be tuple, not %.500s",
1223 Py_TYPE(args)->tp_name);
1224 return 0;
1225 }
1226 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
1227 "idna", &host, &port))
1228 return 0;
1229 addr=(struct sockaddr_in*)addr_ret;
1230 result = setipaddr(host, (struct sockaddr *)addr,
1231 sizeof(*addr), AF_INET);
1232 PyMem_Free(host);
1233 if (result < 0)
1234 return 0;
1235 if (port < 0 || port > 0xffff) {
1236 PyErr_SetString(
1237 PyExc_OverflowError,
1238 "getsockaddrarg: port must be 0-65535.");
1239 return 0;
1240 }
1241 addr->sin_family = AF_INET;
1242 addr->sin_port = htons((short)port);
1243 *len_ret = sizeof *addr;
1244 return 1;
1245 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001246
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001247#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001248 case AF_INET6:
1249 {
1250 struct sockaddr_in6* addr;
1251 char *host;
1252 int port, flowinfo, scope_id, result;
1253 flowinfo = scope_id = 0;
1254 if (!PyTuple_Check(args)) {
1255 PyErr_Format(
1256 PyExc_TypeError,
1257 "getsockaddrarg: "
1258 "AF_INET6 address must be tuple, not %.500s",
1259 Py_TYPE(args)->tp_name);
1260 return 0;
1261 }
1262 if (!PyArg_ParseTuple(args, "eti|ii",
1263 "idna", &host, &port, &flowinfo,
1264 &scope_id)) {
1265 return 0;
1266 }
1267 addr = (struct sockaddr_in6*)addr_ret;
1268 result = setipaddr(host, (struct sockaddr *)addr,
1269 sizeof(*addr), AF_INET6);
1270 PyMem_Free(host);
1271 if (result < 0)
1272 return 0;
1273 if (port < 0 || port > 0xffff) {
1274 PyErr_SetString(
1275 PyExc_OverflowError,
1276 "getsockaddrarg: port must be 0-65535.");
1277 return 0;
1278 }
1279 addr->sin6_family = s->sock_family;
1280 addr->sin6_port = htons((short)port);
1281 addr->sin6_flowinfo = flowinfo;
1282 addr->sin6_scope_id = scope_id;
1283 *len_ret = sizeof *addr;
1284 return 1;
1285 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001286#endif
1287
Hye-Shik Chang81268602004-02-02 06:05:24 +00001288#ifdef USE_BLUETOOTH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001289 case AF_BLUETOOTH:
1290 {
1291 switch (s->sock_proto) {
1292 case BTPROTO_L2CAP:
1293 {
1294 struct sockaddr_l2 *addr;
1295 char *straddr;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001296
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001297 addr = (struct sockaddr_l2 *)addr_ret;
1298 memset(addr, 0, sizeof(struct sockaddr_l2));
1299 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
1300 if (!PyArg_ParseTuple(args, "si", &straddr,
1301 &_BT_L2_MEMB(addr, psm))) {
1302 PyErr_SetString(socket_error, "getsockaddrarg: "
1303 "wrong format");
1304 return 0;
1305 }
1306 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
1307 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001308
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001309 *len_ret = sizeof *addr;
1310 return 1;
1311 }
1312 case BTPROTO_RFCOMM:
1313 {
1314 struct sockaddr_rc *addr;
1315 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001316
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001317 addr = (struct sockaddr_rc *)addr_ret;
1318 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
1319 if (!PyArg_ParseTuple(args, "si", &straddr,
1320 &_BT_RC_MEMB(addr, channel))) {
1321 PyErr_SetString(socket_error, "getsockaddrarg: "
1322 "wrong format");
1323 return 0;
1324 }
1325 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
1326 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001327
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001328 *len_ret = sizeof *addr;
1329 return 1;
1330 }
1331 case BTPROTO_HCI:
1332 {
1333 struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret;
Gregory P. Smith397cd8a2010-10-17 04:23:21 +00001334#if defined(__NetBSD__) || defined(__DragonFly__)
1335 char *straddr = PyBytes_AS_STRING(args);
1336
1337 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
1338 if (straddr == NULL) {
1339 PyErr_SetString(socket_error, "getsockaddrarg: "
1340 "wrong format");
1341 return 0;
1342 }
1343 if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0)
1344 return 0;
1345#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001346 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
1347 if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) {
1348 PyErr_SetString(socket_error, "getsockaddrarg: "
1349 "wrong format");
1350 return 0;
1351 }
Gregory P. Smith397cd8a2010-10-17 04:23:21 +00001352#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001353 *len_ret = sizeof *addr;
1354 return 1;
1355 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001356#if !defined(__FreeBSD__)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001357 case BTPROTO_SCO:
1358 {
1359 struct sockaddr_sco *addr;
1360 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001361
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001362 addr = (struct sockaddr_sco *)addr_ret;
1363 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
1364 if (!PyBytes_Check(args)) {
1365 PyErr_SetString(socket_error, "getsockaddrarg: "
1366 "wrong format");
1367 return 0;
1368 }
1369 straddr = PyBytes_AS_STRING(args);
1370 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
1371 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001372
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001373 *len_ret = sizeof *addr;
1374 return 1;
1375 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001376#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001377 default:
1378 PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol");
1379 return 0;
1380 }
1381 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001382#endif
1383
Antoine Pitrou323dd702010-10-27 20:27:14 +00001384#if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001385 case AF_PACKET:
1386 {
1387 struct sockaddr_ll* addr;
1388 struct ifreq ifr;
1389 char *interfaceName;
1390 int protoNumber;
1391 int hatype = 0;
1392 int pkttype = 0;
1393 char *haddr = NULL;
1394 unsigned int halen = 0;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001395
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001396 if (!PyTuple_Check(args)) {
1397 PyErr_Format(
1398 PyExc_TypeError,
1399 "getsockaddrarg: "
1400 "AF_PACKET address must be tuple, not %.500s",
1401 Py_TYPE(args)->tp_name);
1402 return 0;
1403 }
1404 if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName,
1405 &protoNumber, &pkttype, &hatype,
1406 &haddr, &halen))
1407 return 0;
1408 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1409 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
1410 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
1411 s->errorhandler();
1412 return 0;
1413 }
1414 if (halen > 8) {
1415 PyErr_SetString(PyExc_ValueError,
1416 "Hardware address must be 8 bytes or less");
1417 return 0;
1418 }
1419 if (protoNumber < 0 || protoNumber > 0xffff) {
1420 PyErr_SetString(
1421 PyExc_OverflowError,
1422 "getsockaddrarg: protoNumber must be 0-65535.");
1423 return 0;
1424 }
1425 addr = (struct sockaddr_ll*)addr_ret;
1426 addr->sll_family = AF_PACKET;
1427 addr->sll_protocol = htons((short)protoNumber);
1428 addr->sll_ifindex = ifr.ifr_ifindex;
1429 addr->sll_pkttype = pkttype;
1430 addr->sll_hatype = hatype;
1431 if (halen != 0) {
1432 memcpy(&addr->sll_addr, haddr, halen);
1433 }
1434 addr->sll_halen = halen;
1435 *len_ret = sizeof *addr;
1436 return 1;
1437 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001438#endif
1439
Christian Heimes043d6f62008-01-07 17:19:16 +00001440#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001441 case AF_TIPC:
1442 {
1443 unsigned int atype, v1, v2, v3;
1444 unsigned int scope = TIPC_CLUSTER_SCOPE;
1445 struct sockaddr_tipc *addr;
Christian Heimes043d6f62008-01-07 17:19:16 +00001446
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001447 if (!PyTuple_Check(args)) {
1448 PyErr_Format(
1449 PyExc_TypeError,
1450 "getsockaddrarg: "
1451 "AF_TIPC address must be tuple, not %.500s",
1452 Py_TYPE(args)->tp_name);
1453 return 0;
1454 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001455
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001456 if (!PyArg_ParseTuple(args,
1457 "IIII|I;Invalid TIPC address format",
1458 &atype, &v1, &v2, &v3, &scope))
1459 return 0;
Christian Heimes043d6f62008-01-07 17:19:16 +00001460
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001461 addr = (struct sockaddr_tipc *) addr_ret;
1462 memset(addr, 0, sizeof(struct sockaddr_tipc));
Christian Heimes043d6f62008-01-07 17:19:16 +00001463
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001464 addr->family = AF_TIPC;
1465 addr->scope = scope;
1466 addr->addrtype = atype;
Christian Heimes043d6f62008-01-07 17:19:16 +00001467
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001468 if (atype == TIPC_ADDR_NAMESEQ) {
1469 addr->addr.nameseq.type = v1;
1470 addr->addr.nameseq.lower = v2;
1471 addr->addr.nameseq.upper = v3;
1472 } else if (atype == TIPC_ADDR_NAME) {
1473 addr->addr.name.name.type = v1;
1474 addr->addr.name.name.instance = v2;
1475 } else if (atype == TIPC_ADDR_ID) {
1476 addr->addr.id.node = v1;
1477 addr->addr.id.ref = v2;
1478 } else {
1479 /* Shouldn't happen */
1480 PyErr_SetString(PyExc_TypeError, "Invalid address type");
1481 return 0;
1482 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001483
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001484 *len_ret = sizeof(*addr);
Christian Heimes043d6f62008-01-07 17:19:16 +00001485
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001486 return 1;
1487 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001488#endif
1489
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001490 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001491
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001492 default:
1493 PyErr_SetString(socket_error, "getsockaddrarg: bad family");
1494 return 0;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001495
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001496 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001497}
1498
Guido van Rossum30a685f1991-06-27 15:51:29 +00001499
Guido van Rossum48a680c2001-03-02 06:34:14 +00001500/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00001501 Return 1 if the family is known, 0 otherwise. The length is returned
1502 through len_ret. */
1503
1504static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00001505getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001506{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001507 switch (s->sock_family) {
Guido van Rossum710e1df1992-06-12 10:39:36 +00001508
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001509#if defined(AF_UNIX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001510 case AF_UNIX:
1511 {
1512 *len_ret = sizeof (struct sockaddr_un);
1513 return 1;
1514 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001515#endif /* AF_UNIX */
Martin v. Löwis11017b12006-01-14 18:12:57 +00001516#if defined(AF_NETLINK)
1517 case AF_NETLINK:
1518 {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001519 *len_ret = sizeof (struct sockaddr_nl);
1520 return 1;
Martin v. Löwis11017b12006-01-14 18:12:57 +00001521 }
1522#endif
Guido van Rossum710e1df1992-06-12 10:39:36 +00001523
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001524 case AF_INET:
1525 {
1526 *len_ret = sizeof (struct sockaddr_in);
1527 return 1;
1528 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00001529
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001530#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001531 case AF_INET6:
1532 {
1533 *len_ret = sizeof (struct sockaddr_in6);
1534 return 1;
1535 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001536#endif
1537
Hye-Shik Chang81268602004-02-02 06:05:24 +00001538#ifdef USE_BLUETOOTH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001539 case AF_BLUETOOTH:
1540 {
1541 switch(s->sock_proto)
1542 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001543
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001544 case BTPROTO_L2CAP:
1545 *len_ret = sizeof (struct sockaddr_l2);
1546 return 1;
1547 case BTPROTO_RFCOMM:
1548 *len_ret = sizeof (struct sockaddr_rc);
1549 return 1;
1550 case BTPROTO_HCI:
1551 *len_ret = sizeof (struct sockaddr_hci);
1552 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001553#if !defined(__FreeBSD__)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001554 case BTPROTO_SCO:
1555 *len_ret = sizeof (struct sockaddr_sco);
1556 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001557#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001558 default:
1559 PyErr_SetString(socket_error, "getsockaddrlen: "
1560 "unknown BT protocol");
1561 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001562
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001563 }
1564 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001565#endif
1566
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001567#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001568 case AF_PACKET:
1569 {
1570 *len_ret = sizeof (struct sockaddr_ll);
1571 return 1;
1572 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001573#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001574
Christian Heimes043d6f62008-01-07 17:19:16 +00001575#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001576 case AF_TIPC:
1577 {
1578 *len_ret = sizeof (struct sockaddr_tipc);
1579 return 1;
1580 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001581#endif
1582
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001583 /* More cases here... */
Guido van Rossum710e1df1992-06-12 10:39:36 +00001584
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001585 default:
1586 PyErr_SetString(socket_error, "getsockaddrlen: bad family");
1587 return 0;
Guido van Rossum710e1df1992-06-12 10:39:36 +00001588
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001589 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00001590}
1591
1592
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001593/* s._accept() -> (fd, address) */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001594
Guido van Rossum73624e91994-10-10 17:59:00 +00001595static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001596sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001597{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001598 sock_addr_t addrbuf;
1599 SOCKET_T newfd = INVALID_SOCKET;
1600 socklen_t addrlen;
1601 PyObject *sock = NULL;
1602 PyObject *addr = NULL;
1603 PyObject *res = NULL;
1604 int timeout;
Barry Warsaw752300b1997-01-03 17:18:10 +00001605
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001606 if (!getsockaddrlen(s, &addrlen))
1607 return NULL;
1608 memset(&addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001609
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001610 if (!IS_SELECTABLE(s))
1611 return select_error();
Neal Norwitz082b2df2006-02-07 07:04:46 +00001612
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001613 Py_BEGIN_ALLOW_THREADS
1614 timeout = internal_select(s, 0);
1615 if (!timeout)
1616 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
1617 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001618
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001619 if (timeout == 1) {
1620 PyErr_SetString(socket_timeout, "timed out");
1621 return NULL;
1622 }
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001623
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001624 if (newfd == INVALID_SOCKET)
1625 return s->errorhandler();
Barry Warsaw752300b1997-01-03 17:18:10 +00001626
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001627 sock = PyLong_FromSocket_t(newfd);
1628 if (sock == NULL) {
1629 SOCKETCLOSE(newfd);
1630 goto finally;
1631 }
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001632
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001633 addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
1634 addrlen, s->sock_proto);
1635 if (addr == NULL)
1636 goto finally;
Barry Warsaw752300b1997-01-03 17:18:10 +00001637
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001638 res = PyTuple_Pack(2, sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001639
Guido van Rossum67f7a382002-06-06 21:08:16 +00001640finally:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001641 Py_XDECREF(sock);
1642 Py_XDECREF(addr);
1643 return res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001644}
1645
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001646PyDoc_STRVAR(accept_doc,
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001647"_accept() -> (integer, address info)\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001648\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001649Wait for an incoming connection. Return a new socket file descriptor\n\
1650representing the connection, and the address of the client.\n\
1651For IP sockets, the address info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001652
Guido van Rossum11ba0942002-06-13 15:07:44 +00001653/* s.setblocking(flag) method. Argument:
1654 False -- non-blocking mode; same as settimeout(0)
1655 True -- blocking mode; same as settimeout(None)
1656*/
Guido van Rossume4485b01994-09-07 14:32:49 +00001657
Guido van Rossum73624e91994-10-10 17:59:00 +00001658static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001659sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00001660{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001661 int block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001662
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001663 block = PyLong_AsLong(arg);
1664 if (block == -1 && PyErr_Occurred())
1665 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001666
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001667 s->sock_timeout = block ? -1.0 : 0.0;
1668 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00001669
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001670 Py_INCREF(Py_None);
1671 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00001672}
Guido van Rossume4485b01994-09-07 14:32:49 +00001673
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001674PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001675"setblocking(flag)\n\
1676\n\
1677Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001678setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001679setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001680
Guido van Rossum11ba0942002-06-13 15:07:44 +00001681/* s.settimeout(timeout) method. Argument:
1682 None -- no timeout, blocking mode; same as setblocking(True)
1683 0.0 -- non-blocking mode; same as setblocking(False)
1684 > 0 -- timeout mode; operations time out after timeout seconds
1685 < 0 -- illegal; raises an exception
1686*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00001687static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001688sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001689{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001690 double timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001691
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001692 if (arg == Py_None)
1693 timeout = -1.0;
1694 else {
1695 timeout = PyFloat_AsDouble(arg);
1696 if (timeout < 0.0) {
1697 if (!PyErr_Occurred())
1698 PyErr_SetString(PyExc_ValueError,
1699 "Timeout value out of range");
1700 return NULL;
1701 }
1702 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001703
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001704 s->sock_timeout = timeout;
1705 internal_setblocking(s, timeout < 0.0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001706
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001707 Py_INCREF(Py_None);
1708 return Py_None;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001709}
1710
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001711PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001712"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001713\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001714Set a timeout on socket operations. 'timeout' can be a float,\n\
1715giving in seconds, or None. Setting a timeout of None disables\n\
1716the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001717Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001718
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001719/* s.gettimeout() method.
1720 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001721static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001722sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001723{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001724 if (s->sock_timeout < 0.0) {
1725 Py_INCREF(Py_None);
1726 return Py_None;
1727 }
1728 else
1729 return PyFloat_FromDouble(s->sock_timeout);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001730}
1731
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001732PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001733"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001734\n\
1735Returns the timeout in floating seconds associated with socket \n\
1736operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001737operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00001738
Guido van Rossumaee08791992-09-08 09:05:33 +00001739/* s.setsockopt() method.
1740 With an integer third argument, sets an integer option.
1741 With a string third argument, sets an option from a buffer;
1742 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001743
Guido van Rossum73624e91994-10-10 17:59:00 +00001744static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001745sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001746{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001747 int level;
1748 int optname;
1749 int res;
1750 char *buf;
1751 int buflen;
1752 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001753
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001754 if (PyArg_ParseTuple(args, "iii:setsockopt",
1755 &level, &optname, &flag)) {
1756 buf = (char *) &flag;
1757 buflen = sizeof flag;
1758 }
1759 else {
1760 PyErr_Clear();
1761 if (!PyArg_ParseTuple(args, "iiy#:setsockopt",
1762 &level, &optname, &buf, &buflen))
1763 return NULL;
1764 }
1765 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
1766 if (res < 0)
1767 return s->errorhandler();
1768 Py_INCREF(Py_None);
1769 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001770}
1771
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001772PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001773"setsockopt(level, option, value)\n\
1774\n\
1775Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001776The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001777
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001778
Guido van Rossumaee08791992-09-08 09:05:33 +00001779/* s.getsockopt() method.
1780 With two arguments, retrieves an integer option.
1781 With a third integer argument, retrieves a string buffer of that size;
1782 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001783
Guido van Rossum73624e91994-10-10 17:59:00 +00001784static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001785sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001786{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001787 int level;
1788 int optname;
1789 int res;
1790 PyObject *buf;
1791 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001792
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001793 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
1794 &level, &optname, &buflen))
1795 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001796
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001797 if (buflen == 0) {
1798 int flag = 0;
1799 socklen_t flagsize = sizeof flag;
1800 res = getsockopt(s->sock_fd, level, optname,
1801 (void *)&flag, &flagsize);
1802 if (res < 0)
1803 return s->errorhandler();
1804 return PyLong_FromLong(flag);
1805 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001806#ifdef __VMS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001807 /* socklen_t is unsigned so no negative test is needed,
1808 test buflen == 0 is previously done */
1809 if (buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001810#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001811 if (buflen <= 0 || buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001812#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001813 PyErr_SetString(socket_error,
1814 "getsockopt buflen out of range");
1815 return NULL;
1816 }
1817 buf = PyBytes_FromStringAndSize((char *)NULL, buflen);
1818 if (buf == NULL)
1819 return NULL;
1820 res = getsockopt(s->sock_fd, level, optname,
1821 (void *)PyBytes_AS_STRING(buf), &buflen);
1822 if (res < 0) {
1823 Py_DECREF(buf);
1824 return s->errorhandler();
1825 }
1826 _PyBytes_Resize(&buf, buflen);
1827 return buf;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001828}
1829
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001830PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001831"getsockopt(level, option[, buffersize]) -> value\n\
1832\n\
1833Get a socket option. See the Unix manual for level and option.\n\
1834If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001835string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001836
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001837
Fred Drake728819a2000-07-01 03:40:12 +00001838/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001839
Guido van Rossum73624e91994-10-10 17:59:00 +00001840static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001841sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001842{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001843 sock_addr_t addrbuf;
1844 int addrlen;
1845 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001846
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001847 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
1848 return NULL;
1849 Py_BEGIN_ALLOW_THREADS
1850 res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);
1851 Py_END_ALLOW_THREADS
1852 if (res < 0)
1853 return s->errorhandler();
1854 Py_INCREF(Py_None);
1855 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001856}
1857
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001858PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001859"bind(address)\n\
1860\n\
1861Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00001862pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001863sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001864
Guido van Rossum30a685f1991-06-27 15:51:29 +00001865
1866/* s.close() method.
1867 Set the file descriptor to -1 so operations tried subsequently
1868 will surely fail. */
1869
Guido van Rossum73624e91994-10-10 17:59:00 +00001870static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001871sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001872{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001873 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001874
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001875 if ((fd = s->sock_fd) != -1) {
1876 s->sock_fd = -1;
1877 Py_BEGIN_ALLOW_THREADS
1878 (void) SOCKETCLOSE(fd);
1879 Py_END_ALLOW_THREADS
1880 }
1881 Py_INCREF(Py_None);
1882 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001883}
1884
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001885PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001886"close()\n\
1887\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001888Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001889
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001890static int
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001891internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001892 int *timeoutp)
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001893{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001894 int res, timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001895
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001896 timeout = 0;
1897 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001898
1899#ifdef MS_WINDOWS
1900
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001901 if (s->sock_timeout > 0.0) {
1902 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK &&
1903 IS_SELECTABLE(s)) {
1904 /* This is a mess. Best solution: trust select */
1905 fd_set fds;
1906 fd_set fds_exc;
1907 struct timeval tv;
1908 tv.tv_sec = (int)s->sock_timeout;
1909 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
1910 FD_ZERO(&fds);
1911 FD_SET(s->sock_fd, &fds);
1912 FD_ZERO(&fds_exc);
1913 FD_SET(s->sock_fd, &fds_exc);
Antoine Pitrouf72006f2010-08-17 19:39:39 +00001914 res = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
1915 NULL, &fds, &fds_exc, &tv);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001916 if (res == 0) {
1917 res = WSAEWOULDBLOCK;
1918 timeout = 1;
1919 } else if (res > 0) {
1920 if (FD_ISSET(s->sock_fd, &fds))
1921 /* The socket is in the writable set - this
1922 means connected */
1923 res = 0;
1924 else {
1925 /* As per MS docs, we need to call getsockopt()
1926 to get the underlying error */
1927 int res_size = sizeof res;
1928 /* It must be in the exception set */
1929 assert(FD_ISSET(s->sock_fd, &fds_exc));
1930 if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
1931 (char *)&res, &res_size))
1932 /* getsockopt also clears WSAGetLastError,
1933 so reset it back. */
1934 WSASetLastError(res);
1935 else
1936 res = WSAGetLastError();
1937 }
1938 }
1939 /* else if (res < 0) an error occurred */
1940 }
1941 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001942
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001943 if (res < 0)
1944 res = WSAGetLastError();
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001945
1946#else
1947
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001948 if (s->sock_timeout > 0.0) {
1949 if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) {
1950 timeout = internal_select(s, 1);
1951 if (timeout == 0) {
1952 /* Bug #1019808: in case of an EINPROGRESS,
1953 use getsockopt(SO_ERROR) to get the real
1954 error. */
1955 socklen_t res_size = sizeof res;
1956 (void)getsockopt(s->sock_fd, SOL_SOCKET,
1957 SO_ERROR, &res, &res_size);
1958 if (res == EISCONN)
1959 res = 0;
1960 errno = res;
1961 }
1962 else if (timeout == -1) {
1963 res = errno; /* had error */
1964 }
1965 else
1966 res = EWOULDBLOCK; /* timed out */
1967 }
1968 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001969
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001970 if (res < 0)
1971 res = errno;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001972
1973#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001974 *timeoutp = timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001975
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001976 return res;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001977}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001978
Fred Drake728819a2000-07-01 03:40:12 +00001979/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001980
Guido van Rossum73624e91994-10-10 17:59:00 +00001981static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001982sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001983{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001984 sock_addr_t addrbuf;
1985 int addrlen;
1986 int res;
1987 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001988
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001989 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
1990 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001991
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001992 Py_BEGIN_ALLOW_THREADS
1993 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
1994 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001995
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001996 if (timeout == 1) {
1997 PyErr_SetString(socket_timeout, "timed out");
1998 return NULL;
1999 }
2000 if (res != 0)
2001 return s->errorhandler();
2002 Py_INCREF(Py_None);
2003 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002004}
2005
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002006PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002007"connect(address)\n\
2008\n\
2009Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002010is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002011
Guido van Rossum30a685f1991-06-27 15:51:29 +00002012
Fred Drake728819a2000-07-01 03:40:12 +00002013/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002014
2015static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002016sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002017{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002018 sock_addr_t addrbuf;
2019 int addrlen;
2020 int res;
2021 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002022
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002023 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2024 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002025
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002026 Py_BEGIN_ALLOW_THREADS
2027 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
2028 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002029
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002030 /* Signals are not errors (though they may raise exceptions). Adapted
2031 from PyErr_SetFromErrnoWithFilenameObject(). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002032#ifdef EINTR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002033 if (res == EINTR && PyErr_CheckSignals())
2034 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002035#endif
2036
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002037 return PyLong_FromLong((long) res);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002038}
2039
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002040PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002041"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002042\n\
2043This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002044instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002045
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002046
Guido van Rossumed233a51992-06-23 09:07:03 +00002047/* s.fileno() method */
2048
Guido van Rossum73624e91994-10-10 17:59:00 +00002049static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002050sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00002051{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002052 return PyLong_FromSocket_t(s->sock_fd);
Guido van Rossumed233a51992-06-23 09:07:03 +00002053}
2054
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002055PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002056"fileno() -> integer\n\
2057\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002058Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002059
Guido van Rossumed233a51992-06-23 09:07:03 +00002060
Guido van Rossumc89705d1992-11-26 08:54:07 +00002061/* s.getsockname() method */
2062
Guido van Rossum73624e91994-10-10 17:59:00 +00002063static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002064sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002065{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002066 sock_addr_t addrbuf;
2067 int res;
2068 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002069
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002070 if (!getsockaddrlen(s, &addrlen))
2071 return NULL;
2072 memset(&addrbuf, 0, addrlen);
2073 Py_BEGIN_ALLOW_THREADS
2074 res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2075 Py_END_ALLOW_THREADS
2076 if (res < 0)
2077 return s->errorhandler();
2078 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2079 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002080}
2081
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002082PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002083"getsockname() -> address info\n\
2084\n\
2085Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002086info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002087
Guido van Rossumc89705d1992-11-26 08:54:07 +00002088
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002089#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002090/* s.getpeername() method */
2091
Guido van Rossum73624e91994-10-10 17:59:00 +00002092static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002093sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002094{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002095 sock_addr_t addrbuf;
2096 int res;
2097 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002098
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002099 if (!getsockaddrlen(s, &addrlen))
2100 return NULL;
2101 memset(&addrbuf, 0, addrlen);
2102 Py_BEGIN_ALLOW_THREADS
2103 res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2104 Py_END_ALLOW_THREADS
2105 if (res < 0)
2106 return s->errorhandler();
2107 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2108 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002109}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002110
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002111PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002112"getpeername() -> address info\n\
2113\n\
2114Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002115info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002116
Guido van Rossumb6775db1994-08-01 11:34:53 +00002117#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002118
2119
Guido van Rossum30a685f1991-06-27 15:51:29 +00002120/* s.listen(n) method */
2121
Guido van Rossum73624e91994-10-10 17:59:00 +00002122static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002123sock_listen(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002124{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002125 int backlog;
2126 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002127
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002128 backlog = PyLong_AsLong(arg);
2129 if (backlog == -1 && PyErr_Occurred())
2130 return NULL;
2131 Py_BEGIN_ALLOW_THREADS
2132 if (backlog < 1)
2133 backlog = 1;
2134 res = listen(s->sock_fd, backlog);
2135 Py_END_ALLOW_THREADS
2136 if (res < 0)
2137 return s->errorhandler();
2138 Py_INCREF(Py_None);
2139 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002140}
2141
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002142PyDoc_STRVAR(listen_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002143"listen(backlog)\n\
2144\n\
2145Enable a server to accept connections. The backlog argument must be at\n\
2146least 1; it specifies the number of unaccepted connection that the system\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002147will allow before refusing new connections.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002148
2149
Thomas Wouters477c8d52006-05-27 19:21:47 +00002150/*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002151 * This is the guts of the recv() and recv_into() methods, which reads into a
Thomas Wouters902d6eb2007-01-09 23:18:33 +00002152 * char buffer. If you have any inc/dec ref to do to the objects that contain
Thomas Wouters477c8d52006-05-27 19:21:47 +00002153 * the buffer, do it in the caller. This function returns the number of bytes
Ezio Melotti13925002011-03-16 11:05:33 +02002154 * successfully read. If there was an error, it returns -1. Note that it is
Thomas Wouters477c8d52006-05-27 19:21:47 +00002155 * also possible that we return a number of bytes smaller than the request
2156 * bytes.
2157 */
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002158static Py_ssize_t
2159sock_recv_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002160{
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002161 Py_ssize_t outlen = -1;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002162 int timeout;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002163#ifdef __VMS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002164 int remaining;
2165 char *read_buf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002166#endif
2167
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002168 if (!IS_SELECTABLE(s)) {
2169 select_error();
2170 return -1;
2171 }
2172 if (len == 0) {
2173 /* If 0 bytes were requested, do nothing. */
2174 return 0;
2175 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002176
2177#ifndef __VMS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002178 Py_BEGIN_ALLOW_THREADS
2179 timeout = internal_select(s, 0);
2180 if (!timeout)
2181 outlen = recv(s->sock_fd, cbuf, len, flags);
2182 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002183
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002184 if (timeout == 1) {
2185 PyErr_SetString(socket_timeout, "timed out");
2186 return -1;
2187 }
2188 if (outlen < 0) {
2189 /* Note: the call to errorhandler() ALWAYS indirectly returned
2190 NULL, so ignore its return value */
2191 s->errorhandler();
2192 return -1;
2193 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002194#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002195 read_buf = cbuf;
2196 remaining = len;
2197 while (remaining != 0) {
2198 unsigned int segment;
2199 int nread = -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002200
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002201 segment = remaining /SEGMENT_SIZE;
2202 if (segment != 0) {
2203 segment = SEGMENT_SIZE;
2204 }
2205 else {
2206 segment = remaining;
2207 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002208
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002209 Py_BEGIN_ALLOW_THREADS
2210 timeout = internal_select(s, 0);
2211 if (!timeout)
2212 nread = recv(s->sock_fd, read_buf, segment, flags);
2213 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002214
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002215 if (timeout == 1) {
2216 PyErr_SetString(socket_timeout, "timed out");
2217 return -1;
2218 }
2219 if (nread < 0) {
2220 s->errorhandler();
2221 return -1;
2222 }
2223 if (nread != remaining) {
2224 read_buf += nread;
2225 break;
2226 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002227
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002228 remaining -= segment;
2229 read_buf += segment;
2230 }
2231 outlen = read_buf - cbuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002232#endif /* !__VMS */
2233
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002234 return outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002235}
2236
Guido van Rossum48a680c2001-03-02 06:34:14 +00002237
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002238/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002239
Guido van Rossum73624e91994-10-10 17:59:00 +00002240static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002241sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002242{
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002243 Py_ssize_t recvlen, outlen;
2244 int flags = 0;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002245 PyObject *buf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002246
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002247 if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002248 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002249
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002250 if (recvlen < 0) {
2251 PyErr_SetString(PyExc_ValueError,
2252 "negative buffersize in recv");
2253 return NULL;
2254 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002255
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002256 /* Allocate a new string. */
2257 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2258 if (buf == NULL)
2259 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002260
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002261 /* Call the guts */
2262 outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags);
2263 if (outlen < 0) {
2264 /* An error occurred, release the string and return an
2265 error. */
2266 Py_DECREF(buf);
2267 return NULL;
2268 }
2269 if (outlen != recvlen) {
2270 /* We did not read as many bytes as we anticipated, resize the
2271 string if possible and be successful. */
2272 _PyBytes_Resize(&buf, outlen);
2273 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002274
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002275 return buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002276}
2277
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002278PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002279"recv(buffersize[, flags]) -> data\n\
2280\n\
2281Receive up to buffersize bytes from the socket. For the optional flags\n\
2282argument, see the Unix manual. When no data is available, block until\n\
2283at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002284the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002285
Guido van Rossum30a685f1991-06-27 15:51:29 +00002286
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002287/* s.recv_into(buffer, [nbytes [,flags]]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002288
Thomas Wouters477c8d52006-05-27 19:21:47 +00002289static PyObject*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002290sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002291{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002292 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00002293
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002294 int flags = 0;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002295 Py_buffer pbuf;
2296 char *buf;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002297 Py_ssize_t buflen, readlen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002298
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002299 /* Get the buffer's memory */
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002300 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002301 &pbuf, &recvlen, &flags))
2302 return NULL;
2303 buf = pbuf.buf;
2304 buflen = pbuf.len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002305
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002306 if (recvlen < 0) {
2307 PyBuffer_Release(&pbuf);
2308 PyErr_SetString(PyExc_ValueError,
2309 "negative buffersize in recv_into");
2310 return NULL;
2311 }
2312 if (recvlen == 0) {
2313 /* If nbytes was not specified, use the buffer's length */
2314 recvlen = buflen;
2315 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002316
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002317 /* Check if the buffer is large enough */
2318 if (buflen < recvlen) {
2319 PyBuffer_Release(&pbuf);
2320 PyErr_SetString(PyExc_ValueError,
2321 "buffer too small for requested bytes");
2322 return NULL;
2323 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002324
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002325 /* Call the guts */
2326 readlen = sock_recv_guts(s, buf, recvlen, flags);
2327 if (readlen < 0) {
2328 /* Return an error. */
2329 PyBuffer_Release(&pbuf);
2330 return NULL;
2331 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002332
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002333 PyBuffer_Release(&pbuf);
2334 /* Return the number of bytes read. Note that we do not do anything
2335 special here in the case that readlen < recvlen. */
2336 return PyLong_FromSsize_t(readlen);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002337}
2338
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002339PyDoc_STRVAR(recv_into_doc,
2340"recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002341\n\
2342A version of recv() that stores its data into a buffer rather than creating \n\
2343a new string. Receive up to buffersize bytes from the socket. If buffersize \n\
2344is not specified (or 0), receive up to the size available in the given buffer.\n\
2345\n\
2346See recv() for documentation about the flags.");
2347
2348
2349/*
Christian Heimes99170a52007-12-19 02:07:34 +00002350 * This is the guts of the recvfrom() and recvfrom_into() methods, which reads
2351 * into a char buffer. If you have any inc/def ref to do to the objects that
2352 * contain the buffer, do it in the caller. This function returns the number
Ezio Melotti13925002011-03-16 11:05:33 +02002353 * of bytes successfully read. If there was an error, it returns -1. Note
Christian Heimes99170a52007-12-19 02:07:34 +00002354 * that it is also possible that we return a number of bytes smaller than the
2355 * request bytes.
Thomas Wouters477c8d52006-05-27 19:21:47 +00002356 *
2357 * 'addr' is a return value for the address object. Note that you must decref
2358 * it yourself.
2359 */
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002360static Py_ssize_t
2361sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002362 PyObject** addr)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002363{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002364 sock_addr_t addrbuf;
2365 int timeout;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002366 Py_ssize_t n = -1;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002367 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002368
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002369 *addr = NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002370
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002371 if (!getsockaddrlen(s, &addrlen))
2372 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002373
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002374 if (!IS_SELECTABLE(s)) {
2375 select_error();
2376 return -1;
2377 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002378
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002379 Py_BEGIN_ALLOW_THREADS
2380 memset(&addrbuf, 0, addrlen);
2381 timeout = internal_select(s, 0);
2382 if (!timeout) {
Guido van Rossum8d665e61996-06-26 18:22:49 +00002383#ifndef MS_WINDOWS
Andrew MacIntyreba43e872002-03-03 03:03:52 +00002384#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002385 n = recvfrom(s->sock_fd, cbuf, len, flags,
2386 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum32c575d1997-12-02 20:37:32 +00002387#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002388 n = recvfrom(s->sock_fd, cbuf, len, flags,
2389 (void *) &addrbuf, &addrlen);
Guido van Rossum32c575d1997-12-02 20:37:32 +00002390#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002391#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002392 n = recvfrom(s->sock_fd, cbuf, len, flags,
2393 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002394#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002395 }
2396 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002397
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002398 if (timeout == 1) {
2399 PyErr_SetString(socket_timeout, "timed out");
2400 return -1;
2401 }
2402 if (n < 0) {
2403 s->errorhandler();
2404 return -1;
2405 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002406
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002407 if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
2408 addrlen, s->sock_proto)))
2409 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002410
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002411 return n;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002412}
2413
2414/* s.recvfrom(nbytes [,flags]) method */
2415
2416static PyObject *
2417sock_recvfrom(PySocketSockObject *s, PyObject *args)
2418{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002419 PyObject *buf = NULL;
2420 PyObject *addr = NULL;
2421 PyObject *ret = NULL;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002422 int flags = 0;
2423 Py_ssize_t recvlen, outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002424
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002425 if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002426 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002427
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002428 if (recvlen < 0) {
2429 PyErr_SetString(PyExc_ValueError,
2430 "negative buffersize in recvfrom");
2431 return NULL;
2432 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002433
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002434 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2435 if (buf == NULL)
2436 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002437
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002438 outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf),
2439 recvlen, flags, &addr);
2440 if (outlen < 0) {
2441 goto finally;
2442 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002443
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002444 if (outlen != recvlen) {
2445 /* We did not read as many bytes as we anticipated, resize the
Ezio Melotti13925002011-03-16 11:05:33 +02002446 string if possible and be successful. */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002447 if (_PyBytes_Resize(&buf, outlen) < 0)
Ezio Melotti13925002011-03-16 11:05:33 +02002448 /* Oopsy, not so successful after all. */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002449 goto finally;
2450 }
Barry Warsaw752300b1997-01-03 17:18:10 +00002451
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002452 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002453
2454finally:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002455 Py_XDECREF(buf);
2456 Py_XDECREF(addr);
2457 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002458}
2459
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002460PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002461"recvfrom(buffersize[, flags]) -> (data, address info)\n\
2462\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002463Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002464
Thomas Wouters477c8d52006-05-27 19:21:47 +00002465
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002466/* s.recvfrom_into(buffer[, nbytes [,flags]]) method */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002467
2468static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002469sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002470{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002471 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00002472
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002473 int flags = 0;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002474 Py_buffer pbuf;
2475 char *buf;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002476 Py_ssize_t readlen, buflen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002477
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002478 PyObject *addr = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002479
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002480 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002481 kwlist, &pbuf,
2482 &recvlen, &flags))
2483 return NULL;
2484 buf = pbuf.buf;
2485 buflen = pbuf.len;
2486 assert(buf != 0 && buflen > 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002487
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002488 if (recvlen < 0) {
2489 PyBuffer_Release(&pbuf);
2490 PyErr_SetString(PyExc_ValueError,
2491 "negative buffersize in recvfrom_into");
2492 return NULL;
2493 }
2494 if (recvlen == 0) {
2495 /* If nbytes was not specified, use the buffer's length */
2496 recvlen = buflen;
2497 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002498
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002499 readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
2500 if (readlen < 0) {
2501 PyBuffer_Release(&pbuf);
2502 /* Return an error */
2503 Py_XDECREF(addr);
2504 return NULL;
2505 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002506
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002507 PyBuffer_Release(&pbuf);
2508 /* Return the number of bytes read and the address. Note that we do
2509 not do anything special here in the case that readlen < recvlen. */
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002510 return Py_BuildValue("nN", readlen, addr);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002511}
2512
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002513PyDoc_STRVAR(recvfrom_into_doc,
2514"recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002515\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002516Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002517
2518
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002519/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002520
Guido van Rossum73624e91994-10-10 17:59:00 +00002521static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002522sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002523{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002524 char *buf;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002525 Py_ssize_t len, n = -1;
2526 int flags = 0, timeout;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002527 Py_buffer pbuf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002528
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002529 if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags))
2530 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002531
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002532 if (!IS_SELECTABLE(s)) {
2533 PyBuffer_Release(&pbuf);
2534 return select_error();
2535 }
2536 buf = pbuf.buf;
2537 len = pbuf.len;
Neal Norwitz082b2df2006-02-07 07:04:46 +00002538
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002539 Py_BEGIN_ALLOW_THREADS
2540 timeout = internal_select(s, 1);
2541 if (!timeout)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002542#ifdef __VMS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002543 n = sendsegmented(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002544#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002545 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002546#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002547 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002548
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002549 PyBuffer_Release(&pbuf);
Martin v. Löwis423be952008-08-13 15:53:07 +00002550
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002551 if (timeout == 1) {
2552 PyErr_SetString(socket_timeout, "timed out");
2553 return NULL;
2554 }
2555 if (n < 0)
2556 return s->errorhandler();
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002557 return PyLong_FromSsize_t(n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00002558}
2559
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002560PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002561"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002562\n\
2563Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002564argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002565sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002566
2567
2568/* s.sendall(data [,flags]) method */
2569
2570static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002571sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002572{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002573 char *buf;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002574 Py_ssize_t len, n = -1;
Antoine Pitrou08ae02f2010-09-27 18:14:43 +00002575 int flags = 0, timeout, saved_errno;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002576 Py_buffer pbuf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002577
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002578 if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags))
2579 return NULL;
2580 buf = pbuf.buf;
2581 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002582
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002583 if (!IS_SELECTABLE(s)) {
2584 PyBuffer_Release(&pbuf);
2585 return select_error();
2586 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002587
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002588 do {
Antoine Pitrou08ae02f2010-09-27 18:14:43 +00002589 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002590 timeout = internal_select(s, 1);
2591 n = -1;
Antoine Pitrou08ae02f2010-09-27 18:14:43 +00002592 if (!timeout) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002593#ifdef __VMS
Antoine Pitrou08ae02f2010-09-27 18:14:43 +00002594 n = sendsegmented(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002595#else
Antoine Pitrou08ae02f2010-09-27 18:14:43 +00002596 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002597#endif
Antoine Pitrou08ae02f2010-09-27 18:14:43 +00002598 }
2599 Py_END_ALLOW_THREADS
2600 if (timeout == 1) {
2601 PyBuffer_Release(&pbuf);
2602 PyErr_SetString(socket_timeout, "timed out");
2603 return NULL;
2604 }
2605 /* PyErr_CheckSignals() might change errno */
2606 saved_errno = errno;
2607 /* We must run our signal handlers before looping again.
2608 send() can return a successful partial write when it is
2609 interrupted, so we can't restrict ourselves to EINTR. */
2610 if (PyErr_CheckSignals()) {
2611 PyBuffer_Release(&pbuf);
2612 return NULL;
2613 }
2614 if (n < 0) {
2615 /* If interrupted, try again */
2616 if (saved_errno == EINTR)
2617 continue;
2618 else
2619 break;
2620 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002621 buf += n;
2622 len -= n;
2623 } while (len > 0);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002624 PyBuffer_Release(&pbuf);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002625
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002626 if (n < 0)
2627 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00002628
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002629 Py_INCREF(Py_None);
2630 return Py_None;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002631}
2632
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002633PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002634"sendall(data[, flags])\n\
2635\n\
2636Send a data string to the socket. For the optional flags\n\
2637argument, see the Unix manual. This calls send() repeatedly\n\
2638until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002639to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002640
Guido van Rossum30a685f1991-06-27 15:51:29 +00002641
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002642/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002643
Guido van Rossum73624e91994-10-10 17:59:00 +00002644static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002645sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002646{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002647 Py_buffer pbuf;
2648 PyObject *addro;
2649 char *buf;
2650 Py_ssize_t len;
2651 sock_addr_t addrbuf;
2652 int addrlen, n = -1, flags, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002653
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002654 flags = 0;
2655 if (!PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro)) {
2656 PyErr_Clear();
2657 if (!PyArg_ParseTuple(args, "y*iO:sendto",
2658 &pbuf, &flags, &addro))
2659 return NULL;
2660 }
2661 buf = pbuf.buf;
2662 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002663
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002664 if (!IS_SELECTABLE(s)) {
2665 PyBuffer_Release(&pbuf);
2666 return select_error();
2667 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002668
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002669 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) {
2670 PyBuffer_Release(&pbuf);
2671 return NULL;
2672 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002673
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002674 Py_BEGIN_ALLOW_THREADS
2675 timeout = internal_select(s, 1);
2676 if (!timeout)
2677 n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen);
2678 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002679
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002680 PyBuffer_Release(&pbuf);
2681 if (timeout == 1) {
2682 PyErr_SetString(socket_timeout, "timed out");
2683 return NULL;
2684 }
2685 if (n < 0)
2686 return s->errorhandler();
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002687 return PyLong_FromSsize_t(n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002688}
2689
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002690PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002691"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002692\n\
2693Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002694For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002695
Guido van Rossum30a685f1991-06-27 15:51:29 +00002696
2697/* s.shutdown(how) method */
2698
Guido van Rossum73624e91994-10-10 17:59:00 +00002699static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002700sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002701{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002702 int how;
2703 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002704
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002705 how = PyLong_AsLong(arg);
2706 if (how == -1 && PyErr_Occurred())
2707 return NULL;
2708 Py_BEGIN_ALLOW_THREADS
2709 res = shutdown(s->sock_fd, how);
2710 Py_END_ALLOW_THREADS
2711 if (res < 0)
2712 return s->errorhandler();
2713 Py_INCREF(Py_None);
2714 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002715}
2716
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002717PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002718"shutdown(flag)\n\
2719\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00002720Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
2721of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002722
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00002723#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Christian Heimesfaf2f632008-01-06 16:59:19 +00002724static PyObject*
2725sock_ioctl(PySocketSockObject *s, PyObject *arg)
2726{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002727 unsigned long cmd = SIO_RCVALL;
2728 unsigned int option = RCVALL_ON;
2729 DWORD recv;
Christian Heimesfaf2f632008-01-06 16:59:19 +00002730
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002731 if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option))
2732 return NULL;
Christian Heimesfaf2f632008-01-06 16:59:19 +00002733
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002734 if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option),
2735 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
2736 return set_error();
2737 }
2738 return PyLong_FromUnsignedLong(recv);
Christian Heimesfaf2f632008-01-06 16:59:19 +00002739}
2740PyDoc_STRVAR(sock_ioctl_doc,
2741"ioctl(cmd, option) -> long\n\
2742\n\
2743Control the socket with WSAIoctl syscall. Currently only socket.SIO_RCVALL\n\
2744is supported as control. Options must be one of the socket.RCVALL_*\n\
2745constants.");
2746
2747#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00002748
2749/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002750
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002751static PyMethodDef sock_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002752 {"_accept", (PyCFunction)sock_accept, METH_NOARGS,
2753 accept_doc},
2754 {"bind", (PyCFunction)sock_bind, METH_O,
2755 bind_doc},
2756 {"close", (PyCFunction)sock_close, METH_NOARGS,
2757 close_doc},
2758 {"connect", (PyCFunction)sock_connect, METH_O,
2759 connect_doc},
2760 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
2761 connect_ex_doc},
2762 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
2763 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002764#ifdef HAVE_GETPEERNAME
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002765 {"getpeername", (PyCFunction)sock_getpeername,
2766 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00002767#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002768 {"getsockname", (PyCFunction)sock_getsockname,
2769 METH_NOARGS, getsockname_doc},
2770 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
2771 getsockopt_doc},
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00002772#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002773 {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS,
2774 sock_ioctl_doc},
Christian Heimesfaf2f632008-01-06 16:59:19 +00002775#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002776 {"listen", (PyCFunction)sock_listen, METH_O,
2777 listen_doc},
2778 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
2779 recv_doc},
2780 {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
2781 recv_into_doc},
2782 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
2783 recvfrom_doc},
2784 {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
2785 recvfrom_into_doc},
2786 {"send", (PyCFunction)sock_send, METH_VARARGS,
2787 send_doc},
2788 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
2789 sendall_doc},
2790 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
2791 sendto_doc},
2792 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
2793 setblocking_doc},
2794 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
2795 settimeout_doc},
2796 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
2797 gettimeout_doc},
2798 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
2799 setsockopt_doc},
2800 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
2801 shutdown_doc},
2802 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002803};
2804
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002805/* SockObject members */
2806static PyMemberDef sock_memberlist[] = {
2807 {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},
2808 {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},
2809 {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},
2810 {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"},
2811 {0},
2812};
Guido van Rossum30a685f1991-06-27 15:51:29 +00002813
Guido van Rossum73624e91994-10-10 17:59:00 +00002814/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00002815 First close the file description. */
2816
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002817static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002818sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002819{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002820 if (s->sock_fd != -1)
2821 (void) SOCKETCLOSE(s->sock_fd);
2822 Py_TYPE(s)->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002823}
2824
Guido van Rossum30a685f1991-06-27 15:51:29 +00002825
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002826static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002827sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002828{
Fred Drakea04eaad2000-06-30 02:46:07 +00002829#if SIZEOF_SOCKET_T > SIZEOF_LONG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002830 if (s->sock_fd > LONG_MAX) {
2831 /* this can occur on Win64, and actually there is a special
2832 ugly printf formatter for decimal pointer length integer
2833 printing, only bother if necessary*/
2834 PyErr_SetString(PyExc_OverflowError,
2835 "no printf formatter to display "
2836 "the socket descriptor in decimal");
2837 return NULL;
2838 }
Fred Drakea04eaad2000-06-30 02:46:07 +00002839#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002840 return PyUnicode_FromFormat(
2841 "<socket object, fd=%ld, family=%d, type=%d, proto=%d>",
2842 (long)s->sock_fd, s->sock_family,
2843 s->sock_type,
2844 s->sock_proto);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002845}
2846
2847
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002848/* Create a new, uninitialized socket object. */
2849
2850static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002851sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002852{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002853 PyObject *new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002854
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002855 new = type->tp_alloc(type, 0);
2856 if (new != NULL) {
2857 ((PySocketSockObject *)new)->sock_fd = -1;
2858 ((PySocketSockObject *)new)->sock_timeout = -1.0;
2859 ((PySocketSockObject *)new)->errorhandler = &set_error;
2860 }
2861 return new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002862}
2863
2864
2865/* Initialize a new socket object. */
2866
2867/*ARGSUSED*/
2868static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002869sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002870{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002871 PySocketSockObject *s = (PySocketSockObject *)self;
2872 PyObject *fdobj = NULL;
2873 SOCKET_T fd = INVALID_SOCKET;
2874 int family = AF_INET, type = SOCK_STREAM, proto = 0;
2875 static char *keywords[] = {"family", "type", "proto", "fileno", 0};
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002876
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002877 if (!PyArg_ParseTupleAndKeywords(args, kwds,
2878 "|iiiO:socket", keywords,
2879 &family, &type, &proto, &fdobj))
2880 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002881
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002882 if (fdobj != NULL && fdobj != Py_None) {
2883 fd = PyLong_AsSocket_t(fdobj);
2884 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
2885 return -1;
2886 if (fd == INVALID_SOCKET) {
2887 PyErr_SetString(PyExc_ValueError,
2888 "can't use invalid socket value");
2889 return -1;
2890 }
2891 }
2892 else {
2893 Py_BEGIN_ALLOW_THREADS
2894 fd = socket(family, type, proto);
2895 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002896
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002897 if (fd == INVALID_SOCKET) {
2898 set_error();
2899 return -1;
2900 }
2901 }
2902 init_sockobject(s, fd, family, type, proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002903
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002904 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002905
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002906}
2907
2908
Guido van Rossumb6775db1994-08-01 11:34:53 +00002909/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002910
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002911static PyTypeObject sock_type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002912 PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
2913 "_socket.socket", /* tp_name */
2914 sizeof(PySocketSockObject), /* tp_basicsize */
2915 0, /* tp_itemsize */
2916 (destructor)sock_dealloc, /* tp_dealloc */
2917 0, /* tp_print */
2918 0, /* tp_getattr */
2919 0, /* tp_setattr */
2920 0, /* tp_reserved */
2921 (reprfunc)sock_repr, /* tp_repr */
2922 0, /* tp_as_number */
2923 0, /* tp_as_sequence */
2924 0, /* tp_as_mapping */
2925 0, /* tp_hash */
2926 0, /* tp_call */
2927 0, /* tp_str */
2928 PyObject_GenericGetAttr, /* tp_getattro */
2929 0, /* tp_setattro */
2930 0, /* tp_as_buffer */
2931 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2932 sock_doc, /* tp_doc */
2933 0, /* tp_traverse */
2934 0, /* tp_clear */
2935 0, /* tp_richcompare */
2936 0, /* tp_weaklistoffset */
2937 0, /* tp_iter */
2938 0, /* tp_iternext */
2939 sock_methods, /* tp_methods */
2940 sock_memberlist, /* tp_members */
2941 0, /* tp_getset */
2942 0, /* tp_base */
2943 0, /* tp_dict */
2944 0, /* tp_descr_get */
2945 0, /* tp_descr_set */
2946 0, /* tp_dictoffset */
2947 sock_initobj, /* tp_init */
2948 PyType_GenericAlloc, /* tp_alloc */
2949 sock_new, /* tp_new */
2950 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002951};
2952
Guido van Rossum30a685f1991-06-27 15:51:29 +00002953
Guido van Rossum81194471991-07-27 21:42:02 +00002954/* Python interface to gethostname(). */
2955
2956/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002957static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002958socket_gethostname(PyObject *self, PyObject *unused)
Guido van Rossum81194471991-07-27 21:42:02 +00002959{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002960 char buf[1024];
2961 int res;
2962 Py_BEGIN_ALLOW_THREADS
2963 res = gethostname(buf, (int) sizeof buf - 1);
2964 Py_END_ALLOW_THREADS
2965 if (res < 0)
2966 return set_error();
2967 buf[sizeof buf - 1] = '\0';
2968 return PyUnicode_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00002969}
Guido van Rossumff4949e1992-08-05 19:58:53 +00002970
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002971PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002972"gethostname() -> string\n\
2973\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002974Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002975
Guido van Rossumff4949e1992-08-05 19:58:53 +00002976
Guido van Rossum30a685f1991-06-27 15:51:29 +00002977/* Python interface to gethostbyname(name). */
2978
2979/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002980static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002981socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002982{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002983 char *name;
2984 sock_addr_t addrbuf;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002985
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002986 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
2987 return NULL;
2988 if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)
2989 return NULL;
2990 return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002991}
2992
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002993PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002994"gethostbyname(host) -> address\n\
2995\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002996Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002997
2998
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002999/* Convenience function common to gethostbyname_ex and gethostbyaddr */
3000
3001static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003002gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003003{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003004 char **pch;
3005 PyObject *rtn_tuple = (PyObject *)NULL;
3006 PyObject *name_list = (PyObject *)NULL;
3007 PyObject *addr_list = (PyObject *)NULL;
3008 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003009
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003010 if (h == NULL) {
3011 /* Let's get real error message to return */
3012 set_herror(h_errno);
3013 return NULL;
3014 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003015
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003016 if (h->h_addrtype != af) {
3017 /* Let's get real error message to return */
3018 PyErr_SetString(socket_error,
3019 (char *)strerror(EAFNOSUPPORT));
Christian Heimesada8c3b2008-03-18 18:26:33 +00003020
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003021 return NULL;
3022 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003023
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003024 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00003025
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003026 case AF_INET:
3027 if (alen < sizeof(struct sockaddr_in))
3028 return NULL;
3029 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003030
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003031#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003032 case AF_INET6:
3033 if (alen < sizeof(struct sockaddr_in6))
3034 return NULL;
3035 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003036#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00003037
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003038 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003039
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003040 if ((name_list = PyList_New(0)) == NULL)
3041 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003042
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003043 if ((addr_list = PyList_New(0)) == NULL)
3044 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003045
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003046 /* SF #1511317: h_aliases can be NULL */
3047 if (h->h_aliases) {
3048 for (pch = h->h_aliases; *pch != NULL; pch++) {
3049 int status;
3050 tmp = PyUnicode_FromString(*pch);
3051 if (tmp == NULL)
3052 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003053
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003054 status = PyList_Append(name_list, tmp);
3055 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003056
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003057 if (status)
3058 goto err;
3059 }
3060 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003061
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003062 for (pch = h->h_addr_list; *pch != NULL; pch++) {
3063 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003064
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003065 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00003066
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003067 case AF_INET:
3068 {
3069 struct sockaddr_in sin;
3070 memset(&sin, 0, sizeof(sin));
3071 sin.sin_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003072#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003073 sin.sin_len = sizeof(sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003074#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003075 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
3076 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00003077
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003078 if (pch == h->h_addr_list && alen >= sizeof(sin))
3079 memcpy((char *) addr, &sin, sizeof(sin));
3080 break;
3081 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003082
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003083#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003084 case AF_INET6:
3085 {
3086 struct sockaddr_in6 sin6;
3087 memset(&sin6, 0, sizeof(sin6));
3088 sin6.sin6_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003089#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003090 sin6.sin6_len = sizeof(sin6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003091#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003092 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
3093 tmp = makeipaddr((struct sockaddr *)&sin6,
3094 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00003095
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003096 if (pch == h->h_addr_list && alen >= sizeof(sin6))
3097 memcpy((char *) addr, &sin6, sizeof(sin6));
3098 break;
3099 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003100#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00003101
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003102 default: /* can't happen */
3103 PyErr_SetString(socket_error,
3104 "unsupported address family");
3105 return NULL;
3106 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003107
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003108 if (tmp == NULL)
3109 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003110
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003111 status = PyList_Append(addr_list, tmp);
3112 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003113
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003114 if (status)
3115 goto err;
3116 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003117
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003118 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003119
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003120 err:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003121 Py_XDECREF(name_list);
3122 Py_XDECREF(addr_list);
3123 return rtn_tuple;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003124}
3125
3126
3127/* Python interface to gethostbyname_ex(name). */
3128
3129/*ARGSUSED*/
3130static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003131socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003132{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003133 char *name;
3134 struct hostent *h;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00003135#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003136 struct sockaddr_storage addr;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00003137#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003138 struct sockaddr_in addr;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00003139#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003140 struct sockaddr *sa;
3141 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003142#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003143 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003144#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003145 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003146#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003147 char buf[16384];
3148 int buf_len = (sizeof buf) - 1;
3149 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003150#endif
3151#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003152 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003153#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003154#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003155
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003156 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
3157 return NULL;
3158 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
3159 return NULL;
3160 Py_BEGIN_ALLOW_THREADS
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003161#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003162#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003163 result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
3164 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003165#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003166 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003167#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003168 memset((void *) &data, '\0', sizeof(data));
3169 result = gethostbyname_r(name, &hp_allocated, &data);
3170 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003171#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003172#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003173#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003174 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003175#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003176 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003177#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003178 Py_END_ALLOW_THREADS
3179 /* Some C libraries would require addr.__ss_family instead of
3180 addr.ss_family.
3181 Therefore, we cast the sockaddr_storage into sockaddr to
3182 access sa_family. */
3183 sa = (struct sockaddr*)&addr;
3184 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
3185 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003186#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003187 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00003188#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003189 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003190}
3191
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003192PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003193"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
3194\n\
3195Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003196for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003197
3198
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003199/* Python interface to gethostbyaddr(IP). */
3200
3201/*ARGSUSED*/
3202static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003203socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003204{
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003205#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003206 struct sockaddr_storage addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003207#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003208 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003209#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003210 struct sockaddr *sa = (struct sockaddr *)&addr;
3211 char *ip_num;
3212 struct hostent *h;
3213 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003214#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003215 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003216#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003217 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003218#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003219 /* glibcs up to 2.10 assume that the buf argument to
3220 gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc
3221 does not ensure. The attribute below instructs the compiler
3222 to maintain this alignment. */
3223 char buf[16384] Py_ALIGNED(8);
3224 int buf_len = (sizeof buf) - 1;
3225 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003226#endif
3227#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003228 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003229#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003230#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003231 char *ap;
3232 int al;
3233 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003234
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003235 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
3236 return NULL;
3237 af = AF_UNSPEC;
3238 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
3239 return NULL;
3240 af = sa->sa_family;
3241 ap = NULL;
3242 al = 0;
3243 switch (af) {
3244 case AF_INET:
3245 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
3246 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3247 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003248#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003249 case AF_INET6:
3250 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
3251 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3252 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003253#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003254 default:
3255 PyErr_SetString(socket_error, "unsupported address family");
3256 return NULL;
3257 }
3258 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003259#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003260#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003261 result = gethostbyaddr_r(ap, al, af,
3262 &hp_allocated, buf, buf_len,
3263 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003264#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003265 h = gethostbyaddr_r(ap, al, af,
3266 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003267#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003268 memset((void *) &data, '\0', sizeof(data));
3269 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
3270 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003271#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003272#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003273#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003274 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003275#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003276 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003277#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003278 Py_END_ALLOW_THREADS
3279 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003280#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003281 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003282#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003283 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003284}
3285
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003286PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003287"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
3288\n\
3289Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003290for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003291
Guido van Rossum30a685f1991-06-27 15:51:29 +00003292
3293/* Python interface to getservbyname(name).
3294 This only returns the port number, since the other info is already
3295 known or not useful (like the list of aliases). */
3296
3297/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00003298static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003299socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00003300{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003301 char *name, *proto=NULL;
3302 struct servent *sp;
3303 if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
3304 return NULL;
3305 Py_BEGIN_ALLOW_THREADS
3306 sp = getservbyname(name, proto);
3307 Py_END_ALLOW_THREADS
3308 if (sp == NULL) {
3309 PyErr_SetString(socket_error, "service/proto not found");
3310 return NULL;
3311 }
3312 return PyLong_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00003313}
3314
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003315PyDoc_STRVAR(getservbyname_doc,
Barry Warsaw11b91a02004-06-28 00:50:43 +00003316"getservbyname(servicename[, protocolname]) -> integer\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003317\n\
3318Return a port number from a service name and protocol name.\n\
Barry Warsaw11b91a02004-06-28 00:50:43 +00003319The optional protocol name, if given, should be 'tcp' or 'udp',\n\
3320otherwise any protocol will match.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003321
Guido van Rossum30a685f1991-06-27 15:51:29 +00003322
Barry Warsaw11b91a02004-06-28 00:50:43 +00003323/* Python interface to getservbyport(port).
3324 This only returns the service name, since the other info is already
3325 known or not useful (like the list of aliases). */
3326
3327/*ARGSUSED*/
3328static PyObject *
3329socket_getservbyport(PyObject *self, PyObject *args)
3330{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003331 int port;
3332 char *proto=NULL;
3333 struct servent *sp;
3334 if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto))
3335 return NULL;
3336 if (port < 0 || port > 0xffff) {
3337 PyErr_SetString(
3338 PyExc_OverflowError,
3339 "getservbyport: port must be 0-65535.");
3340 return NULL;
3341 }
3342 Py_BEGIN_ALLOW_THREADS
3343 sp = getservbyport(htons((short)port), proto);
3344 Py_END_ALLOW_THREADS
3345 if (sp == NULL) {
3346 PyErr_SetString(socket_error, "port/proto not found");
3347 return NULL;
3348 }
3349 return PyUnicode_FromString(sp->s_name);
Barry Warsaw11b91a02004-06-28 00:50:43 +00003350}
3351
3352PyDoc_STRVAR(getservbyport_doc,
3353"getservbyport(port[, protocolname]) -> string\n\
3354\n\
3355Return the service name from a port number and protocol name.\n\
3356The optional protocol name, if given, should be 'tcp' or 'udp',\n\
3357otherwise any protocol will match.");
3358
Guido van Rossum3901d851996-12-19 16:35:04 +00003359/* Python interface to getprotobyname(name).
3360 This only returns the protocol number, since the other info is
3361 already known or not useful (like the list of aliases). */
3362
3363/*ARGSUSED*/
3364static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003365socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00003366{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003367 char *name;
3368 struct protoent *sp;
3369 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
3370 return NULL;
3371 Py_BEGIN_ALLOW_THREADS
3372 sp = getprotobyname(name);
3373 Py_END_ALLOW_THREADS
3374 if (sp == NULL) {
3375 PyErr_SetString(socket_error, "protocol not found");
3376 return NULL;
3377 }
3378 return PyLong_FromLong((long) sp->p_proto);
Guido van Rossum3901d851996-12-19 16:35:04 +00003379}
3380
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003381PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003382"getprotobyname(name) -> integer\n\
3383\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003384Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003385
Guido van Rossum3901d851996-12-19 16:35:04 +00003386
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003387#ifndef NO_DUP
3388/* dup() function for socket fds */
3389
3390static PyObject *
3391socket_dup(PyObject *self, PyObject *fdobj)
3392{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003393 SOCKET_T fd, newfd;
3394 PyObject *newfdobj;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003395
3396
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003397 fd = PyLong_AsSocket_t(fdobj);
3398 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
3399 return NULL;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003400
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003401 newfd = dup_socket(fd);
3402 if (newfd == INVALID_SOCKET)
3403 return set_error();
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003404
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003405 newfdobj = PyLong_FromSocket_t(newfd);
3406 if (newfdobj == NULL)
3407 SOCKETCLOSE(newfd);
3408 return newfdobj;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003409}
3410
3411PyDoc_STRVAR(dup_doc,
3412"dup(integer) -> integer\n\
3413\n\
3414Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\
3415sockets; on some platforms os.dup() won't work for socket file descriptors.");
3416#endif
3417
3418
Dave Cole331708b2004-08-09 04:51:41 +00003419#ifdef HAVE_SOCKETPAIR
3420/* Create a pair of sockets using the socketpair() function.
Dave Cole07fda7e2004-08-23 05:16:23 +00003421 Arguments as for socket() except the default family is AF_UNIX if
Dave Colee8bbfe42004-08-26 00:51:16 +00003422 defined on the platform; otherwise, the default is AF_INET. */
Dave Cole331708b2004-08-09 04:51:41 +00003423
3424/*ARGSUSED*/
3425static PyObject *
3426socket_socketpair(PyObject *self, PyObject *args)
3427{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003428 PySocketSockObject *s0 = NULL, *s1 = NULL;
3429 SOCKET_T sv[2];
3430 int family, type = SOCK_STREAM, proto = 0;
3431 PyObject *res = NULL;
Dave Cole331708b2004-08-09 04:51:41 +00003432
3433#if defined(AF_UNIX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003434 family = AF_UNIX;
Dave Cole331708b2004-08-09 04:51:41 +00003435#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003436 family = AF_INET;
Dave Cole331708b2004-08-09 04:51:41 +00003437#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003438 if (!PyArg_ParseTuple(args, "|iii:socketpair",
3439 &family, &type, &proto))
3440 return NULL;
3441 /* Create a pair of socket fds */
3442 if (socketpair(family, type, proto, sv) < 0)
3443 return set_error();
3444 s0 = new_sockobject(sv[0], family, type, proto);
3445 if (s0 == NULL)
3446 goto finally;
3447 s1 = new_sockobject(sv[1], family, type, proto);
3448 if (s1 == NULL)
3449 goto finally;
3450 res = PyTuple_Pack(2, s0, s1);
Dave Cole331708b2004-08-09 04:51:41 +00003451
3452finally:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003453 if (res == NULL) {
3454 if (s0 == NULL)
3455 SOCKETCLOSE(sv[0]);
3456 if (s1 == NULL)
3457 SOCKETCLOSE(sv[1]);
3458 }
3459 Py_XDECREF(s0);
3460 Py_XDECREF(s1);
3461 return res;
Dave Cole331708b2004-08-09 04:51:41 +00003462}
3463
3464PyDoc_STRVAR(socketpair_doc,
3465"socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\
3466\n\
3467Create a pair of socket objects from the sockets returned by the platform\n\
3468socketpair() function.\n\
Dave Cole07fda7e2004-08-23 05:16:23 +00003469The arguments are the same as for socket() except the default family is\n\
Dave Colee8bbfe42004-08-26 00:51:16 +00003470AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
Dave Cole331708b2004-08-09 04:51:41 +00003471
3472#endif /* HAVE_SOCKETPAIR */
3473
3474
Guido van Rossum006bf911996-06-12 04:04:55 +00003475static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003476socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003477{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003478 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00003479
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003480 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
3481 return NULL;
3482 }
3483 if (x1 < 0) {
3484 PyErr_SetString(PyExc_OverflowError,
3485 "can't convert negative number to unsigned long");
3486 return NULL;
3487 }
3488 x2 = (unsigned int)ntohs((unsigned short)x1);
3489 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00003490}
3491
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003492PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003493"ntohs(integer) -> integer\n\
3494\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003495Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003496
3497
Guido van Rossum006bf911996-06-12 04:04:55 +00003498static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003499socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003500{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003501 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003502
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003503 if (PyLong_Check(arg)) {
3504 x = PyLong_AsUnsignedLong(arg);
3505 if (x == (unsigned long) -1 && PyErr_Occurred())
3506 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003507#if SIZEOF_LONG > 4
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003508 {
3509 unsigned long y;
3510 /* only want the trailing 32 bits */
3511 y = x & 0xFFFFFFFFUL;
3512 if (y ^ x)
3513 return PyErr_Format(PyExc_OverflowError,
3514 "long int larger than 32 bits");
3515 x = y;
3516 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003517#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003518 }
3519 else
3520 return PyErr_Format(PyExc_TypeError,
3521 "expected int/long, %s found",
3522 Py_TYPE(arg)->tp_name);
3523 if (x == (unsigned long) -1 && PyErr_Occurred())
3524 return NULL;
3525 return PyLong_FromUnsignedLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003526}
3527
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003528PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003529"ntohl(integer) -> integer\n\
3530\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003531Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003532
3533
Guido van Rossum006bf911996-06-12 04:04:55 +00003534static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003535socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003536{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003537 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00003538
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003539 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
3540 return NULL;
3541 }
3542 if (x1 < 0) {
3543 PyErr_SetString(PyExc_OverflowError,
3544 "can't convert negative number to unsigned long");
3545 return NULL;
3546 }
3547 x2 = (unsigned int)htons((unsigned short)x1);
3548 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00003549}
3550
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003551PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003552"htons(integer) -> integer\n\
3553\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003554Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003555
3556
Guido van Rossum006bf911996-06-12 04:04:55 +00003557static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003558socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003559{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003560 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003561
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003562 if (PyLong_Check(arg)) {
3563 x = PyLong_AsUnsignedLong(arg);
3564 if (x == (unsigned long) -1 && PyErr_Occurred())
3565 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003566#if SIZEOF_LONG > 4
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003567 {
3568 unsigned long y;
3569 /* only want the trailing 32 bits */
3570 y = x & 0xFFFFFFFFUL;
3571 if (y ^ x)
3572 return PyErr_Format(PyExc_OverflowError,
3573 "long int larger than 32 bits");
3574 x = y;
3575 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003576#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003577 }
3578 else
3579 return PyErr_Format(PyExc_TypeError,
3580 "expected int/long, %s found",
3581 Py_TYPE(arg)->tp_name);
3582 return PyLong_FromUnsignedLong(htonl((unsigned long)x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003583}
3584
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003585PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003586"htonl(integer) -> integer\n\
3587\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003588Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003589
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003590/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003591
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003592PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum7d0a8262007-05-21 23:13:11 +00003593"inet_aton(string) -> bytes giving packed 32-bit IP representation\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003594\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003595Convert 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 +00003596binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003597
3598static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003599socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003600{
Guido van Rossuma2e48551999-09-09 15:42:59 +00003601#ifndef INADDR_NONE
3602#define INADDR_NONE (-1)
3603#endif
Neal Norwitz88f115b2003-02-13 02:15:42 +00003604#ifdef HAVE_INET_ATON
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003605 struct in_addr buf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003606#endif
3607
3608#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
Benjamin Petersonf91df042009-02-13 02:50:59 +00003609#if (SIZEOF_INT != 4)
3610#error "Not sure if in_addr_t exists and int is not 32-bits."
3611#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003612 /* Have to use inet_addr() instead */
3613 unsigned int packed_addr;
Tim Peters1df9fdd2003-02-13 03:13:40 +00003614#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003615 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003616
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003617 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
3618 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003619
Tim Peters1df9fdd2003-02-13 03:13:40 +00003620
3621#ifdef HAVE_INET_ATON
Thomas Wouters477c8d52006-05-27 19:21:47 +00003622
3623#ifdef USE_INET_ATON_WEAKLINK
3624 if (inet_aton != NULL) {
3625#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003626 if (inet_aton(ip_addr, &buf))
3627 return PyBytes_FromStringAndSize((char *)(&buf),
3628 sizeof(buf));
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003629
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003630 PyErr_SetString(socket_error,
3631 "illegal IP address string passed to inet_aton");
3632 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003633
Thomas Wouters477c8d52006-05-27 19:21:47 +00003634#ifdef USE_INET_ATON_WEAKLINK
3635 } else {
3636#endif
3637
3638#endif
3639
3640#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
3641
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003642 /* special-case this address as inet_addr might return INADDR_NONE
3643 * for this */
3644 if (strcmp(ip_addr, "255.255.255.255") == 0) {
3645 packed_addr = 0xFFFFFFFF;
3646 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00003647
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003648 packed_addr = inet_addr(ip_addr);
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003649
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003650 if (packed_addr == INADDR_NONE) { /* invalid address */
3651 PyErr_SetString(socket_error,
3652 "illegal IP address string passed to inet_aton");
3653 return NULL;
3654 }
3655 }
3656 return PyBytes_FromStringAndSize((char *) &packed_addr,
3657 sizeof(packed_addr));
Thomas Wouters477c8d52006-05-27 19:21:47 +00003658
3659#ifdef USE_INET_ATON_WEAKLINK
3660 }
3661#endif
3662
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003663#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003664}
3665
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003666PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00003667"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003668\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003669Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003670
3671static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003672socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003673{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003674 char *packed_str;
3675 int addr_len;
3676 struct in_addr packed_addr;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003677
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003678 if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) {
3679 return NULL;
3680 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00003681
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003682 if (addr_len != sizeof(packed_addr)) {
3683 PyErr_SetString(socket_error,
3684 "packed IP wrong length for inet_ntoa");
3685 return NULL;
3686 }
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003687
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003688 memcpy(&packed_addr, packed_str, addr_len);
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003689
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003690 return PyUnicode_FromString(inet_ntoa(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003691}
Guido van Rossum82a5c661998-07-07 20:45:43 +00003692
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003693#ifdef HAVE_INET_PTON
3694
3695PyDoc_STRVAR(inet_pton_doc,
3696"inet_pton(af, ip) -> packed IP address string\n\
3697\n\
3698Convert an IP address from string format to a packed string suitable\n\
3699for use with low-level network functions.");
3700
3701static PyObject *
3702socket_inet_pton(PyObject *self, PyObject *args)
3703{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003704 int af;
3705 char* ip;
3706 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003707#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003708 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003709#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003710 char packed[sizeof(struct in_addr)];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003711#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003712 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
3713 return NULL;
3714 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003715
Martin v. Löwis04697e82004-06-02 12:35:29 +00003716#if !defined(ENABLE_IPV6) && defined(AF_INET6)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003717 if(af == AF_INET6) {
3718 PyErr_SetString(socket_error,
3719 "can't use AF_INET6, IPv6 is disabled");
3720 return NULL;
3721 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003722#endif
Martin v. Löwis10649092003-08-05 06:25:06 +00003723
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003724 retval = inet_pton(af, ip, packed);
3725 if (retval < 0) {
3726 PyErr_SetFromErrno(socket_error);
3727 return NULL;
3728 } else if (retval == 0) {
3729 PyErr_SetString(socket_error,
3730 "illegal IP address string passed to inet_pton");
3731 return NULL;
3732 } else if (af == AF_INET) {
3733 return PyBytes_FromStringAndSize(packed,
3734 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003735#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003736 } else if (af == AF_INET6) {
3737 return PyBytes_FromStringAndSize(packed,
3738 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003739#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003740 } else {
3741 PyErr_SetString(socket_error, "unknown address family");
3742 return NULL;
3743 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003744}
Thomas Wouters477c8d52006-05-27 19:21:47 +00003745
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003746PyDoc_STRVAR(inet_ntop_doc,
3747"inet_ntop(af, packed_ip) -> string formatted IP address\n\
3748\n\
3749Convert a packed IP address of the given family to string format.");
3750
3751static PyObject *
3752socket_inet_ntop(PyObject *self, PyObject *args)
3753{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003754 int af;
3755 char* packed;
3756 int len;
3757 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003758#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003759 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003760#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003761 char ip[INET_ADDRSTRLEN + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003762#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00003763
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003764 /* Guarantee NUL-termination for PyUnicode_FromString() below */
3765 memset((void *) &ip[0], '\0', sizeof(ip));
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003766
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003767 if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) {
3768 return NULL;
3769 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003770
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003771 if (af == AF_INET) {
3772 if (len != sizeof(struct in_addr)) {
3773 PyErr_SetString(PyExc_ValueError,
3774 "invalid length of packed IP address string");
3775 return NULL;
3776 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003777#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003778 } else if (af == AF_INET6) {
3779 if (len != sizeof(struct in6_addr)) {
3780 PyErr_SetString(PyExc_ValueError,
3781 "invalid length of packed IP address string");
3782 return NULL;
3783 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003784#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003785 } else {
3786 PyErr_Format(PyExc_ValueError,
3787 "unknown address family %d", af);
3788 return NULL;
3789 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003790
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003791 retval = inet_ntop(af, packed, ip, sizeof(ip));
3792 if (!retval) {
3793 PyErr_SetFromErrno(socket_error);
3794 return NULL;
3795 } else {
3796 return PyUnicode_FromString(retval);
3797 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003798
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003799 /* NOTREACHED */
3800 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
3801 return NULL;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003802}
3803
3804#endif /* HAVE_INET_PTON */
3805
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003806/* Python interface to getaddrinfo(host, port). */
3807
3808/*ARGSUSED*/
3809static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003810socket_getaddrinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003811{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003812 struct addrinfo hints, *res;
3813 struct addrinfo *res0 = NULL;
3814 PyObject *hobj = NULL;
3815 PyObject *pobj = (PyObject *)NULL;
3816 char pbuf[30];
3817 char *hptr, *pptr;
3818 int family, socktype, protocol, flags;
3819 int error;
3820 PyObject *all = (PyObject *)NULL;
3821 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003822
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003823 family = socktype = protocol = flags = 0;
3824 family = AF_UNSPEC;
3825 if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
3826 &hobj, &pobj, &family, &socktype,
3827 &protocol, &flags)) {
3828 return NULL;
3829 }
3830 if (hobj == Py_None) {
3831 hptr = NULL;
3832 } else if (PyUnicode_Check(hobj)) {
3833 idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
3834 if (!idna)
3835 return NULL;
3836 assert(PyBytes_Check(idna));
3837 hptr = PyBytes_AS_STRING(idna);
3838 } else if (PyBytes_Check(hobj)) {
3839 hptr = PyBytes_AsString(hobj);
3840 } else {
3841 PyErr_SetString(PyExc_TypeError,
3842 "getaddrinfo() argument 1 must be string or None");
3843 return NULL;
3844 }
3845 if (PyLong_CheckExact(pobj)) {
3846 long value = PyLong_AsLong(pobj);
3847 if (value == -1 && PyErr_Occurred())
3848 goto err;
3849 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value);
3850 pptr = pbuf;
3851 } else if (PyUnicode_Check(pobj)) {
3852 pptr = _PyUnicode_AsString(pobj);
3853 } else if (PyBytes_Check(pobj)) {
3854 pptr = PyBytes_AsString(pobj);
3855 } else if (pobj == Py_None) {
3856 pptr = (char *)NULL;
3857 } else {
3858 PyErr_SetString(socket_error, "Int or String expected");
3859 goto err;
3860 }
3861 memset(&hints, 0, sizeof(hints));
3862 hints.ai_family = family;
3863 hints.ai_socktype = socktype;
3864 hints.ai_protocol = protocol;
3865 hints.ai_flags = flags;
3866 Py_BEGIN_ALLOW_THREADS
3867 ACQUIRE_GETADDRINFO_LOCK
3868 error = getaddrinfo(hptr, pptr, &hints, &res0);
3869 Py_END_ALLOW_THREADS
3870 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
3871 if (error) {
3872 set_gaierror(error);
3873 goto err;
3874 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003875
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003876 if ((all = PyList_New(0)) == NULL)
3877 goto err;
3878 for (res = res0; res; res = res->ai_next) {
3879 PyObject *single;
3880 PyObject *addr =
3881 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
3882 if (addr == NULL)
3883 goto err;
3884 single = Py_BuildValue("iiisO", res->ai_family,
3885 res->ai_socktype, res->ai_protocol,
3886 res->ai_canonname ? res->ai_canonname : "",
3887 addr);
3888 Py_DECREF(addr);
3889 if (single == NULL)
3890 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003891
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003892 if (PyList_Append(all, single))
3893 goto err;
3894 Py_XDECREF(single);
3895 }
3896 Py_XDECREF(idna);
3897 if (res0)
3898 freeaddrinfo(res0);
3899 return all;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003900 err:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003901 Py_XDECREF(all);
3902 Py_XDECREF(idna);
3903 if (res0)
3904 freeaddrinfo(res0);
3905 return (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003906}
3907
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003908PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003909"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
3910 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003911\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003912Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003913
3914/* Python interface to getnameinfo(sa, flags). */
3915
3916/*ARGSUSED*/
3917static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003918socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003919{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003920 PyObject *sa = (PyObject *)NULL;
3921 int flags;
3922 char *hostp;
3923 int port, flowinfo, scope_id;
3924 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
3925 struct addrinfo hints, *res = NULL;
3926 int error;
3927 PyObject *ret = (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003928
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003929 flags = flowinfo = scope_id = 0;
3930 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
3931 return NULL;
3932 if (!PyTuple_Check(sa)) {
3933 PyErr_SetString(PyExc_TypeError,
3934 "getnameinfo() argument 1 must be a tuple");
3935 return NULL;
3936 }
3937 if (!PyArg_ParseTuple(sa, "si|ii",
3938 &hostp, &port, &flowinfo, &scope_id))
3939 return NULL;
3940 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
3941 memset(&hints, 0, sizeof(hints));
3942 hints.ai_family = AF_UNSPEC;
3943 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
3944 Py_BEGIN_ALLOW_THREADS
3945 ACQUIRE_GETADDRINFO_LOCK
3946 error = getaddrinfo(hostp, pbuf, &hints, &res);
3947 Py_END_ALLOW_THREADS
3948 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
3949 if (error) {
3950 set_gaierror(error);
3951 goto fail;
3952 }
3953 if (res->ai_next) {
3954 PyErr_SetString(socket_error,
3955 "sockaddr resolved to multiple addresses");
3956 goto fail;
3957 }
3958 switch (res->ai_family) {
3959 case AF_INET:
3960 {
3961 if (PyTuple_GET_SIZE(sa) != 2) {
3962 PyErr_SetString(socket_error,
3963 "IPv4 sockaddr must be 2 tuple");
3964 goto fail;
3965 }
3966 break;
3967 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003968#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003969 case AF_INET6:
3970 {
3971 struct sockaddr_in6 *sin6;
3972 sin6 = (struct sockaddr_in6 *)res->ai_addr;
3973 sin6->sin6_flowinfo = flowinfo;
3974 sin6->sin6_scope_id = scope_id;
3975 break;
3976 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003977#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003978 }
Antoine Pitrouf72006f2010-08-17 19:39:39 +00003979 error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003980 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
3981 if (error) {
3982 set_gaierror(error);
3983 goto fail;
3984 }
3985 ret = Py_BuildValue("ss", hbuf, pbuf);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003986
3987fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003988 if (res)
3989 freeaddrinfo(res);
3990 return ret;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003991}
3992
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003993PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003994"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003995\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003996Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003997
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003998
3999/* Python API to getting and setting the default timeout value. */
4000
4001static PyObject *
4002socket_getdefaulttimeout(PyObject *self)
4003{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004004 if (defaulttimeout < 0.0) {
4005 Py_INCREF(Py_None);
4006 return Py_None;
4007 }
4008 else
4009 return PyFloat_FromDouble(defaulttimeout);
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004010}
4011
4012PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004013"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004014\n\
4015Returns the default timeout in floating seconds for new socket objects.\n\
4016A value of None indicates that new socket objects have no timeout.\n\
4017When the socket module is first imported, the default is None.");
4018
4019static PyObject *
4020socket_setdefaulttimeout(PyObject *self, PyObject *arg)
4021{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004022 double timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004023
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004024 if (arg == Py_None)
4025 timeout = -1.0;
4026 else {
4027 timeout = PyFloat_AsDouble(arg);
4028 if (timeout < 0.0) {
4029 if (!PyErr_Occurred())
4030 PyErr_SetString(PyExc_ValueError,
4031 "Timeout value out of range");
4032 return NULL;
4033 }
4034 }
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004035
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004036 defaulttimeout = timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004037
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004038 Py_INCREF(Py_None);
4039 return Py_None;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004040}
4041
4042PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004043"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004044\n\
4045Set the default timeout in floating seconds for new socket objects.\n\
4046A value of None indicates that new socket objects have no timeout.\n\
4047When the socket module is first imported, the default is None.");
4048
4049
Guido van Rossum30a685f1991-06-27 15:51:29 +00004050/* List of functions exported by this module. */
4051
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004052static PyMethodDef socket_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004053 {"gethostbyname", socket_gethostbyname,
4054 METH_VARARGS, gethostbyname_doc},
4055 {"gethostbyname_ex", socket_gethostbyname_ex,
4056 METH_VARARGS, ghbn_ex_doc},
4057 {"gethostbyaddr", socket_gethostbyaddr,
4058 METH_VARARGS, gethostbyaddr_doc},
4059 {"gethostname", socket_gethostname,
4060 METH_NOARGS, gethostname_doc},
4061 {"getservbyname", socket_getservbyname,
4062 METH_VARARGS, getservbyname_doc},
4063 {"getservbyport", socket_getservbyport,
4064 METH_VARARGS, getservbyport_doc},
4065 {"getprotobyname", socket_getprotobyname,
4066 METH_VARARGS, getprotobyname_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004067#ifndef NO_DUP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004068 {"dup", socket_dup,
4069 METH_O, dup_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004070#endif
Dave Cole331708b2004-08-09 04:51:41 +00004071#ifdef HAVE_SOCKETPAIR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004072 {"socketpair", socket_socketpair,
4073 METH_VARARGS, socketpair_doc},
Dave Cole331708b2004-08-09 04:51:41 +00004074#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004075 {"ntohs", socket_ntohs,
4076 METH_VARARGS, ntohs_doc},
4077 {"ntohl", socket_ntohl,
4078 METH_O, ntohl_doc},
4079 {"htons", socket_htons,
4080 METH_VARARGS, htons_doc},
4081 {"htonl", socket_htonl,
4082 METH_O, htonl_doc},
4083 {"inet_aton", socket_inet_aton,
4084 METH_VARARGS, inet_aton_doc},
4085 {"inet_ntoa", socket_inet_ntoa,
4086 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004087#ifdef HAVE_INET_PTON
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004088 {"inet_pton", socket_inet_pton,
4089 METH_VARARGS, inet_pton_doc},
4090 {"inet_ntop", socket_inet_ntop,
4091 METH_VARARGS, inet_ntop_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004092#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004093 {"getaddrinfo", socket_getaddrinfo,
4094 METH_VARARGS, getaddrinfo_doc},
4095 {"getnameinfo", socket_getnameinfo,
4096 METH_VARARGS, getnameinfo_doc},
4097 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
4098 METH_NOARGS, getdefaulttimeout_doc},
4099 {"setdefaulttimeout", socket_setdefaulttimeout,
4100 METH_O, setdefaulttimeout_doc},
4101 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004102};
4103
Guido van Rossum30a685f1991-06-27 15:51:29 +00004104
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004105#ifdef MS_WINDOWS
4106#define OS_INIT_DEFINED
4107
4108/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004109
4110static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004111os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00004112{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004113 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00004114}
4115
4116static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004117os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00004118{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004119 WSADATA WSAData;
4120 int ret;
4121 ret = WSAStartup(0x0101, &WSAData);
4122 switch (ret) {
4123 case 0: /* No error */
4124 Py_AtExit(os_cleanup);
4125 return 1; /* Success */
4126 case WSASYSNOTREADY:
4127 PyErr_SetString(PyExc_ImportError,
4128 "WSAStartup failed: network not ready");
4129 break;
4130 case WSAVERNOTSUPPORTED:
4131 case WSAEINVAL:
4132 PyErr_SetString(
4133 PyExc_ImportError,
4134 "WSAStartup failed: requested version not supported");
4135 break;
4136 default:
4137 PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret);
4138 break;
4139 }
4140 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004141}
4142
Guido van Rossum8d665e61996-06-26 18:22:49 +00004143#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004144
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004145
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004146#ifdef PYOS_OS2
4147#define OS_INIT_DEFINED
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004148
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004149/* Additional initialization for OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004150
4151static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004152os_init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004153{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004154#ifndef PYCC_GCC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004155 int rc = sock_init();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004156
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004157 if (rc == 0) {
4158 return 1; /* Success */
4159 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004160
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004161 PyErr_Format(PyExc_ImportError, "OS/2 TCP/IP Error# %d", sock_errno());
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004162
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004163 return 0; /* Failure */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00004164#else
Ezio Melotti13925002011-03-16 11:05:33 +02004165 /* No need to initialize sockets with GCC/EMX */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004166 return 1; /* Success */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00004167#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004168}
4169
4170#endif /* PYOS_OS2 */
4171
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004172
4173#ifndef OS_INIT_DEFINED
4174static int
4175os_init(void)
4176{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004177 return 1; /* Success */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004178}
4179#endif
4180
4181
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004182/* C API table - always add new things to the end for binary
4183 compatibility. */
4184static
4185PySocketModule_APIObject PySocketModuleAPI =
4186{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004187 &sock_type,
4188 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004189};
4190
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004191
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004192/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004193
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004194 This module is actually called "_socket", and there's a wrapper
Guido van Rossum7d0a8262007-05-21 23:13:11 +00004195 "socket.py" which implements some additional functionality.
4196 The import of "_socket" may fail with an ImportError exception if
4197 os-specific initialization fails. On Windows, this does WINSOCK
Ezio Melotti13925002011-03-16 11:05:33 +02004198 initialization. When WINSOCK is initialized successfully, a call to
Guido van Rossum7d0a8262007-05-21 23:13:11 +00004199 WSACleanup() is scheduled to be made at exit time.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004200*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004201
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004202PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004203"Implementation module for socket operations.\n\
4204\n\
4205See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004206
Martin v. Löwis1a214512008-06-11 05:26:20 +00004207static struct PyModuleDef socketmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004208 PyModuleDef_HEAD_INIT,
4209 PySocket_MODULE_NAME,
4210 socket_doc,
4211 -1,
4212 socket_methods,
4213 NULL,
4214 NULL,
4215 NULL,
4216 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00004217};
4218
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004219PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00004220PyInit__socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004221{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004222 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00004223
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004224 if (!os_init())
4225 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004226
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004227 Py_TYPE(&sock_type) = &PyType_Type;
4228 m = PyModule_Create(&socketmodule);
4229 if (m == NULL)
4230 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004231
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004232 socket_error = PyErr_NewException("socket.error",
4233 PyExc_IOError, NULL);
4234 if (socket_error == NULL)
4235 return NULL;
4236 PySocketModuleAPI.error = socket_error;
4237 Py_INCREF(socket_error);
4238 PyModule_AddObject(m, "error", socket_error);
4239 socket_herror = PyErr_NewException("socket.herror",
4240 socket_error, NULL);
4241 if (socket_herror == NULL)
4242 return NULL;
4243 Py_INCREF(socket_herror);
4244 PyModule_AddObject(m, "herror", socket_herror);
4245 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
4246 NULL);
4247 if (socket_gaierror == NULL)
4248 return NULL;
4249 Py_INCREF(socket_gaierror);
4250 PyModule_AddObject(m, "gaierror", socket_gaierror);
4251 socket_timeout = PyErr_NewException("socket.timeout",
4252 socket_error, NULL);
4253 if (socket_timeout == NULL)
4254 return NULL;
4255 Py_INCREF(socket_timeout);
4256 PyModule_AddObject(m, "timeout", socket_timeout);
4257 Py_INCREF((PyObject *)&sock_type);
4258 if (PyModule_AddObject(m, "SocketType",
4259 (PyObject *)&sock_type) != 0)
4260 return NULL;
4261 Py_INCREF((PyObject *)&sock_type);
4262 if (PyModule_AddObject(m, "socket",
4263 (PyObject *)&sock_type) != 0)
4264 return NULL;
Guido van Rossum09be4091999-08-09 14:40:40 +00004265
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004266#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004267 has_ipv6 = Py_True;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004268#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004269 has_ipv6 = Py_False;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004270#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004271 Py_INCREF(has_ipv6);
4272 PyModule_AddObject(m, "has_ipv6", has_ipv6);
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004273
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004274 /* Export C API */
4275 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
4276 PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL)
4277 ) != 0)
4278 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004279
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004280 /* Address families (we only support AF_INET and AF_UNIX) */
Guido van Rossum09be4091999-08-09 14:40:40 +00004281#ifdef AF_UNSPEC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004282 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00004283#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004284 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004285#ifdef AF_INET6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004286 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004287#endif /* AF_INET6 */
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00004288#if defined(AF_UNIX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004289 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004290#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00004291#ifdef AF_AX25
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004292 /* Amateur Radio AX.25 */
4293 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004294#endif
4295#ifdef AF_IPX
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004296 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00004297#endif
4298#ifdef AF_APPLETALK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004299 /* Appletalk DDP */
4300 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004301#endif
4302#ifdef AF_NETROM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004303 /* Amateur radio NetROM */
4304 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004305#endif
4306#ifdef AF_BRIDGE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004307 /* Multiprotocol bridge */
4308 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004309#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004310#ifdef AF_ATMPVC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004311 /* ATM PVCs */
4312 PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004313#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004314#ifdef AF_AAL5
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004315 /* Reserved for Werner's ATM */
4316 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00004317#endif
4318#ifdef AF_X25
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004319 /* Reserved for X.25 project */
4320 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004321#endif
4322#ifdef AF_INET6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004323 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00004324#endif
4325#ifdef AF_ROSE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004326 /* Amateur Radio X.25 PLP */
4327 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004328#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004329#ifdef AF_DECnet
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004330 /* Reserved for DECnet project */
4331 PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004332#endif
4333#ifdef AF_NETBEUI
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004334 /* Reserved for 802.2LLC project */
4335 PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004336#endif
4337#ifdef AF_SECURITY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004338 /* Security callback pseudo AF */
4339 PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004340#endif
4341#ifdef AF_KEY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004342 /* PF_KEY key management API */
4343 PyModule_AddIntConstant(m, "AF_KEY", AF_KEY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004344#endif
4345#ifdef AF_NETLINK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004346 /* */
4347 PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);
4348 PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004349#ifdef NETLINK_SKIP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004350 PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004351#endif
4352#ifdef NETLINK_W1
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004353 PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004354#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004355 PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK);
4356 PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004357#ifdef NETLINK_TCPDIAG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004358 PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004359#endif
4360#ifdef NETLINK_NFLOG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004361 PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004362#endif
Neal Norwitz65851662006-01-16 04:31:40 +00004363#ifdef NETLINK_XFRM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004364 PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM);
Neal Norwitz65851662006-01-16 04:31:40 +00004365#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004366#ifdef NETLINK_ARPD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004367 PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004368#endif
4369#ifdef NETLINK_ROUTE6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004370 PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004371#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004372 PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW);
Thomas Wouterscf297e42007-02-23 15:07:44 +00004373#ifdef NETLINK_DNRTMSG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004374 PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG);
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004375#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004376#ifdef NETLINK_TAPBASE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004377 PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004378#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004379#endif /* AF_NETLINK */
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004380#ifdef AF_ROUTE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004381 /* Alias to emulate 4.4BSD */
4382 PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004383#endif
4384#ifdef AF_ASH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004385 /* Ash */
4386 PyModule_AddIntConstant(m, "AF_ASH", AF_ASH);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004387#endif
4388#ifdef AF_ECONET
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004389 /* Acorn Econet */
4390 PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004391#endif
4392#ifdef AF_ATMSVC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004393 /* ATM SVCs */
4394 PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004395#endif
4396#ifdef AF_SNA
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004397 /* Linux SNA Project (nutters!) */
4398 PyModule_AddIntConstant(m, "AF_SNA", AF_SNA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004399#endif
4400#ifdef AF_IRDA
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004401 /* IRDA sockets */
4402 PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004403#endif
4404#ifdef AF_PPPOX
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004405 /* PPPoX sockets */
4406 PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004407#endif
4408#ifdef AF_WANPIPE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004409 /* Wanpipe API Sockets */
4410 PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004411#endif
4412#ifdef AF_LLC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004413 /* Linux LLC */
4414 PyModule_AddIntConstant(m, "AF_LLC", AF_LLC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004415#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00004416
Hye-Shik Chang81268602004-02-02 06:05:24 +00004417#ifdef USE_BLUETOOTH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004418 PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);
4419 PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);
4420 PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI);
4421 PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI);
Gregory P. Smith397cd8a2010-10-17 04:23:21 +00004422#if !defined(__NetBSD__) && !defined(__DragonFly__)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004423 PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER);
Gregory P. Smith397cd8a2010-10-17 04:23:21 +00004424#endif
Hye-Shik Chang81268602004-02-02 06:05:24 +00004425#if !defined(__FreeBSD__)
Gregory P. Smith397cd8a2010-10-17 04:23:21 +00004426#if !defined(__NetBSD__) && !defined(__DragonFly__)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004427 PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP);
Gregory P. Smith397cd8a2010-10-17 04:23:21 +00004428#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004429 PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR);
4430 PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00004431#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004432 PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);
4433 PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
4434 PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
Martin v. Löwis12af0482004-01-31 12:34:17 +00004435#endif
4436
Antoine Pitrou323dd702010-10-27 20:27:14 +00004437#ifdef AF_PACKET
4438 PyModule_AddIntMacro(m, AF_PACKET);
4439#endif
4440#ifdef PF_PACKET
4441 PyModule_AddIntMacro(m, PF_PACKET);
4442#endif
4443#ifdef PACKET_HOST
4444 PyModule_AddIntMacro(m, PACKET_HOST);
4445#endif
4446#ifdef PACKET_BROADCAST
4447 PyModule_AddIntMacro(m, PACKET_BROADCAST);
4448#endif
4449#ifdef PACKET_MULTICAST
4450 PyModule_AddIntMacro(m, PACKET_MULTICAST);
4451#endif
4452#ifdef PACKET_OTHERHOST
4453 PyModule_AddIntMacro(m, PACKET_OTHERHOST);
4454#endif
4455#ifdef PACKET_OUTGOING
4456 PyModule_AddIntMacro(m, PACKET_OUTGOING);
4457#endif
4458#ifdef PACKET_LOOPBACK
4459 PyModule_AddIntMacro(m, PACKET_LOOPBACK);
4460#endif
4461#ifdef PACKET_FASTROUTE
4462 PyModule_AddIntMacro(m, PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00004463#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004464
Christian Heimes043d6f62008-01-07 17:19:16 +00004465#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004466 PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC);
Christian Heimes043d6f62008-01-07 17:19:16 +00004467
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004468 /* for addresses */
4469 PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ);
4470 PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME);
4471 PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID);
Christian Heimes043d6f62008-01-07 17:19:16 +00004472
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004473 PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE);
4474 PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE);
4475 PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE);
Christian Heimes043d6f62008-01-07 17:19:16 +00004476
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004477 /* for setsockopt() */
4478 PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC);
4479 PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE);
4480 PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE);
4481 PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE",
4482 TIPC_DEST_DROPPABLE);
4483 PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT);
Christian Heimes043d6f62008-01-07 17:19:16 +00004484
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004485 PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE",
4486 TIPC_LOW_IMPORTANCE);
4487 PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE",
4488 TIPC_MEDIUM_IMPORTANCE);
4489 PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE",
4490 TIPC_HIGH_IMPORTANCE);
4491 PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE",
4492 TIPC_CRITICAL_IMPORTANCE);
Christian Heimes043d6f62008-01-07 17:19:16 +00004493
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004494 /* for subscriptions */
4495 PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS);
4496 PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE);
Christian Heimes25bb7832008-01-11 16:17:00 +00004497#ifdef TIPC_SUB_CANCEL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004498 /* doesn't seem to be available everywhere */
4499 PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL);
Christian Heimes25bb7832008-01-11 16:17:00 +00004500#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004501 PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER);
4502 PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED);
4503 PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN);
4504 PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT);
4505 PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV);
4506 PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV);
Christian Heimes043d6f62008-01-07 17:19:16 +00004507#endif
4508
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004509 /* Socket types */
4510 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
4511 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004512/* We have incomplete socket support. */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004513 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
4514 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00004515#if defined(SOCK_RDM)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004516 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004517#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004518
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004519#ifdef SO_DEBUG
4520 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004521#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004522#ifdef SO_ACCEPTCONN
4523 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004524#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004525#ifdef SO_REUSEADDR
4526 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004527#endif
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00004528#ifdef SO_EXCLUSIVEADDRUSE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004529 PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE);
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00004530#endif
4531
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004532#ifdef SO_KEEPALIVE
4533 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004534#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004535#ifdef SO_DONTROUTE
4536 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004537#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004538#ifdef SO_BROADCAST
4539 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004540#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004541#ifdef SO_USELOOPBACK
4542 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004543#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004544#ifdef SO_LINGER
4545 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004546#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004547#ifdef SO_OOBINLINE
4548 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004549#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004550#ifdef SO_REUSEPORT
4551 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004552#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004553#ifdef SO_SNDBUF
4554 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004555#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004556#ifdef SO_RCVBUF
4557 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004558#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004559#ifdef SO_SNDLOWAT
4560 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004561#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004562#ifdef SO_RCVLOWAT
4563 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004564#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004565#ifdef SO_SNDTIMEO
4566 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004567#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004568#ifdef SO_RCVTIMEO
4569 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004570#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004571#ifdef SO_ERROR
4572 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004573#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004574#ifdef SO_TYPE
4575 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004576#endif
4577
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004578 /* Maximum number of connections for "listen" */
4579#ifdef SOMAXCONN
4580 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004581#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004582 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004583#endif
4584
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004585 /* Flags for send, recv */
4586#ifdef MSG_OOB
4587 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004588#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004589#ifdef MSG_PEEK
4590 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004591#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004592#ifdef MSG_DONTROUTE
4593 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004594#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004595#ifdef MSG_DONTWAIT
4596 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00004597#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004598#ifdef MSG_EOR
4599 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004600#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004601#ifdef MSG_TRUNC
4602 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004603#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004604#ifdef MSG_CTRUNC
4605 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004606#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004607#ifdef MSG_WAITALL
4608 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004609#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004610#ifdef MSG_BTAG
4611 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004612#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004613#ifdef MSG_ETAG
4614 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004615#endif
4616
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004617 /* Protocol level and numbers, usable for [gs]etsockopt */
4618#ifdef SOL_SOCKET
4619 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004620#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004621#ifdef SOL_IP
4622 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004623#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004624 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00004625#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004626#ifdef SOL_IPX
4627 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00004628#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004629#ifdef SOL_AX25
4630 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004631#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004632#ifdef SOL_ATALK
4633 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004634#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004635#ifdef SOL_NETROM
4636 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004637#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004638#ifdef SOL_ROSE
4639 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004640#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004641#ifdef SOL_TCP
4642 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004643#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004644 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00004645#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004646#ifdef SOL_UDP
4647 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004648#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004649 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00004650#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004651#ifdef IPPROTO_IP
4652 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00004653#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004654 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004655#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004656#ifdef IPPROTO_HOPOPTS
4657 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004658#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004659#ifdef IPPROTO_ICMP
4660 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00004661#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004662 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004663#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004664#ifdef IPPROTO_IGMP
4665 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004666#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004667#ifdef IPPROTO_GGP
4668 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004669#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004670#ifdef IPPROTO_IPV4
4671 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004672#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004673#ifdef IPPROTO_IPV6
4674 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisa0f17342003-10-03 13:56:20 +00004675#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004676#ifdef IPPROTO_IPIP
4677 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004678#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004679#ifdef IPPROTO_TCP
4680 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00004681#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004682 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004683#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004684#ifdef IPPROTO_EGP
4685 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004686#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004687#ifdef IPPROTO_PUP
4688 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004689#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004690#ifdef IPPROTO_UDP
4691 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00004692#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004693 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004694#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004695#ifdef IPPROTO_IDP
4696 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004697#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004698#ifdef IPPROTO_HELLO
4699 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004700#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004701#ifdef IPPROTO_ND
4702 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004703#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004704#ifdef IPPROTO_TP
4705 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004706#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004707#ifdef IPPROTO_IPV6
4708 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004709#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004710#ifdef IPPROTO_ROUTING
4711 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004712#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004713#ifdef IPPROTO_FRAGMENT
4714 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004715#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004716#ifdef IPPROTO_RSVP
4717 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004718#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004719#ifdef IPPROTO_GRE
4720 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004721#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004722#ifdef IPPROTO_ESP
4723 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004724#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004725#ifdef IPPROTO_AH
4726 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004727#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004728#ifdef IPPROTO_MOBILE
4729 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004730#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004731#ifdef IPPROTO_ICMPV6
4732 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004733#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004734#ifdef IPPROTO_NONE
4735 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004736#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004737#ifdef IPPROTO_DSTOPTS
4738 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004739#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004740#ifdef IPPROTO_XTP
4741 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004742#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004743#ifdef IPPROTO_EON
4744 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004745#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004746#ifdef IPPROTO_PIM
4747 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004748#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004749#ifdef IPPROTO_IPCOMP
4750 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004751#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004752#ifdef IPPROTO_VRRP
4753 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004754#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004755#ifdef IPPROTO_BIP
4756 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004757#endif
4758/**/
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004759#ifdef IPPROTO_RAW
4760 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00004761#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004762 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004763#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004764#ifdef IPPROTO_MAX
4765 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004766#endif
4767
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004768 /* Some port configuration */
4769#ifdef IPPORT_RESERVED
4770 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004771#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004772 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004773#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004774#ifdef IPPORT_USERRESERVED
4775 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004776#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004777 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004778#endif
4779
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004780 /* Some reserved IP v.4 addresses */
4781#ifdef INADDR_ANY
4782 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004783#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004784 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004785#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004786#ifdef INADDR_BROADCAST
4787 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004788#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004789 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004790#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004791#ifdef INADDR_LOOPBACK
4792 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004793#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004794 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004795#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004796#ifdef INADDR_UNSPEC_GROUP
4797 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004798#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004799 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004800#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004801#ifdef INADDR_ALLHOSTS_GROUP
4802 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
4803 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004804#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004805 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004806#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004807#ifdef INADDR_MAX_LOCAL_GROUP
4808 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
4809 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004810#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004811 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004812#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004813#ifdef INADDR_NONE
4814 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004815#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004816 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004817#endif
4818
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004819 /* IPv4 [gs]etsockopt options */
4820#ifdef IP_OPTIONS
4821 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004822#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004823#ifdef IP_HDRINCL
4824 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004825#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004826#ifdef IP_TOS
4827 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004828#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004829#ifdef IP_TTL
4830 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004831#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004832#ifdef IP_RECVOPTS
4833 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004834#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004835#ifdef IP_RECVRETOPTS
4836 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004837#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004838#ifdef IP_RECVDSTADDR
4839 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004840#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004841#ifdef IP_RETOPTS
4842 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004843#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004844#ifdef IP_MULTICAST_IF
4845 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004846#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004847#ifdef IP_MULTICAST_TTL
4848 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004849#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004850#ifdef IP_MULTICAST_LOOP
4851 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004852#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004853#ifdef IP_ADD_MEMBERSHIP
4854 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004855#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004856#ifdef IP_DROP_MEMBERSHIP
4857 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004858#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004859#ifdef IP_DEFAULT_MULTICAST_TTL
4860 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
4861 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00004862#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004863#ifdef IP_DEFAULT_MULTICAST_LOOP
4864 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
4865 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004866#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004867#ifdef IP_MAX_MEMBERSHIPS
4868 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00004869#endif
4870
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004871 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
4872#ifdef IPV6_JOIN_GROUP
4873 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004874#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004875#ifdef IPV6_LEAVE_GROUP
4876 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004877#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004878#ifdef IPV6_MULTICAST_HOPS
4879 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004880#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004881#ifdef IPV6_MULTICAST_IF
4882 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004883#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004884#ifdef IPV6_MULTICAST_LOOP
4885 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004886#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004887#ifdef IPV6_UNICAST_HOPS
4888 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004889#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004890 /* Additional IPV6 socket options, defined in RFC 3493 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00004891#ifdef IPV6_V6ONLY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004892 PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004893#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004894 /* Advanced IPV6 socket options, from RFC 3542 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00004895#ifdef IPV6_CHECKSUM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004896 PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004897#endif
4898#ifdef IPV6_DONTFRAG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004899 PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004900#endif
4901#ifdef IPV6_DSTOPTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004902 PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004903#endif
4904#ifdef IPV6_HOPLIMIT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004905 PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004906#endif
4907#ifdef IPV6_HOPOPTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004908 PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004909#endif
4910#ifdef IPV6_NEXTHOP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004911 PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004912#endif
4913#ifdef IPV6_PATHMTU
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004914 PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004915#endif
4916#ifdef IPV6_PKTINFO
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004917 PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004918#endif
4919#ifdef IPV6_RECVDSTOPTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004920 PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004921#endif
4922#ifdef IPV6_RECVHOPLIMIT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004923 PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004924#endif
4925#ifdef IPV6_RECVHOPOPTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004926 PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004927#endif
4928#ifdef IPV6_RECVPKTINFO
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004929 PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004930#endif
4931#ifdef IPV6_RECVRTHDR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004932 PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004933#endif
4934#ifdef IPV6_RECVTCLASS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004935 PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004936#endif
4937#ifdef IPV6_RTHDR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004938 PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004939#endif
4940#ifdef IPV6_RTHDRDSTOPTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004941 PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004942#endif
4943#ifdef IPV6_RTHDR_TYPE_0
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004944 PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004945#endif
4946#ifdef IPV6_RECVPATHMTU
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004947 PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004948#endif
4949#ifdef IPV6_TCLASS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004950 PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004951#endif
4952#ifdef IPV6_USE_MIN_MTU
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004953 PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004954#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004955
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004956 /* TCP options */
4957#ifdef TCP_NODELAY
4958 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00004959#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004960#ifdef TCP_MAXSEG
4961 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00004962#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004963#ifdef TCP_CORK
4964 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004965#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004966#ifdef TCP_KEEPIDLE
4967 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004968#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004969#ifdef TCP_KEEPINTVL
4970 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004971#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004972#ifdef TCP_KEEPCNT
4973 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004974#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004975#ifdef TCP_SYNCNT
4976 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004977#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004978#ifdef TCP_LINGER2
4979 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004980#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004981#ifdef TCP_DEFER_ACCEPT
4982 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004983#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004984#ifdef TCP_WINDOW_CLAMP
4985 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004986#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004987#ifdef TCP_INFO
4988 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004989#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004990#ifdef TCP_QUICKACK
4991 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004992#endif
4993
Guido van Rossum09be4091999-08-09 14:40:40 +00004994
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004995 /* IPX options */
4996#ifdef IPX_TYPE
4997 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004998#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004999
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005000 /* get{addr,name}info parameters */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005001#ifdef EAI_ADDRFAMILY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005002 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005003#endif
5004#ifdef EAI_AGAIN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005005 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005006#endif
5007#ifdef EAI_BADFLAGS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005008 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005009#endif
5010#ifdef EAI_FAIL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005011 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005012#endif
5013#ifdef EAI_FAMILY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005014 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005015#endif
5016#ifdef EAI_MEMORY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005017 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005018#endif
5019#ifdef EAI_NODATA
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005020 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005021#endif
5022#ifdef EAI_NONAME
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005023 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005024#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00005025#ifdef EAI_OVERFLOW
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005026 PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005027#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005028#ifdef EAI_SERVICE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005029 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005030#endif
5031#ifdef EAI_SOCKTYPE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005032 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005033#endif
5034#ifdef EAI_SYSTEM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005035 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005036#endif
5037#ifdef EAI_BADHINTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005038 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005039#endif
5040#ifdef EAI_PROTOCOL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005041 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005042#endif
5043#ifdef EAI_MAX
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005044 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005045#endif
5046#ifdef AI_PASSIVE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005047 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005048#endif
5049#ifdef AI_CANONNAME
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005050 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005051#endif
5052#ifdef AI_NUMERICHOST
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005053 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005054#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00005055#ifdef AI_NUMERICSERV
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005056 PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005057#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005058#ifdef AI_MASK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005059 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005060#endif
5061#ifdef AI_ALL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005062 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005063#endif
5064#ifdef AI_V4MAPPED_CFG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005065 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005066#endif
5067#ifdef AI_ADDRCONFIG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005068 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005069#endif
5070#ifdef AI_V4MAPPED
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005071 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005072#endif
5073#ifdef AI_DEFAULT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005074 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005075#endif
5076#ifdef NI_MAXHOST
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005077 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005078#endif
5079#ifdef NI_MAXSERV
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005080 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005081#endif
5082#ifdef NI_NOFQDN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005083 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005084#endif
5085#ifdef NI_NUMERICHOST
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005086 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005087#endif
5088#ifdef NI_NAMEREQD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005089 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005090#endif
5091#ifdef NI_NUMERICSERV
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005092 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005093#endif
5094#ifdef NI_DGRAM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005095 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005096#endif
5097
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005098 /* shutdown() parameters */
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005099#ifdef SHUT_RD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005100 PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005101#elif defined(SD_RECEIVE)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005102 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005103#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005104 PyModule_AddIntConstant(m, "SHUT_RD", 0);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005105#endif
5106#ifdef SHUT_WR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005107 PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005108#elif defined(SD_SEND)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005109 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005110#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005111 PyModule_AddIntConstant(m, "SHUT_WR", 1);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005112#endif
5113#ifdef SHUT_RDWR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005114 PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005115#elif defined(SD_BOTH)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005116 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005117#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005118 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005119#endif
5120
Christian Heimesfaf2f632008-01-06 16:59:19 +00005121#ifdef SIO_RCVALL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005122 {
5123 PyObject *tmp;
5124 tmp = PyLong_FromUnsignedLong(SIO_RCVALL);
5125 if (tmp == NULL)
5126 return NULL;
5127 PyModule_AddObject(m, "SIO_RCVALL", tmp);
5128 }
5129 PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF);
5130 PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON);
5131 PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00005132#ifdef RCVALL_IPLEVEL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005133 PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00005134#endif
5135#ifdef RCVALL_MAX
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005136 PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00005137#endif
Christian Heimesfaf2f632008-01-06 16:59:19 +00005138#endif /* _MSTCPIP_ */
5139
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005140 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00005141#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005142 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00005143#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005144 return m;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005145}
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#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +00005149#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005150
5151/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005152/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005153
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005154int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005155inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005156{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005157 if (af == AF_INET) {
Benjamin Petersonf91df042009-02-13 02:50:59 +00005158#if (SIZEOF_INT != 4)
5159#error "Not sure if in_addr_t exists and int is not 32-bits."
5160#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005161 unsigned int packed_addr;
5162 packed_addr = inet_addr(src);
5163 if (packed_addr == INADDR_NONE)
5164 return 0;
5165 memcpy(dst, &packed_addr, 4);
5166 return 1;
5167 }
5168 /* Should set errno to EAFNOSUPPORT */
5169 return -1;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005170}
5171
Martin v. Löwisc925b1532001-07-21 09:42:15 +00005172const char *
5173inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005174{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005175 if (af == AF_INET) {
5176 struct in_addr packed_addr;
5177 if (size < 16)
5178 /* Should set errno to ENOSPC. */
5179 return NULL;
5180 memcpy(&packed_addr, src, sizeof(packed_addr));
5181 return strncpy(dst, inet_ntoa(packed_addr), size);
5182 }
5183 /* Should set errno to EAFNOSUPPORT */
5184 return NULL;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005185}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005186
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005187#endif
Christian Heimesb6150692007-11-15 23:37:07 +00005188#endif