blob: f4edc062fd65397cfdf9582494f0fe18373794ec [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
Andrew Svetlov0832af62012-12-18 23:10:48 +020018- socket.error: exception raised for socket specific errors, alias for OSError
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000019- socket.gaierror: exception raised for getaddrinfo/getnameinfo errors,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 a subclass of socket.error
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000021- socket.herror: exception raised for gethostby* errors,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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
Berker Peksaga6ec5ee2014-06-30 11:28:40 +030036- socket.getaddrinfo(host, port [, family, type, proto, flags])
37 --> List of (family, type, 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)
Gregory P. Smith5ed2e772011-05-15 00:26:45 -070045- socket.if_nameindex() -> list of tuples (if_index, if_name)
46- socket.if_nametoindex(name) -> corresponding interface index
47- socket.if_indextoname(index) -> corresponding interface name
Guido van Rossum6574b3e1991-06-25 21:36:08 +000048- an Internet socket address is a pair (hostname, port)
49 where hostname can be anything recognized by gethostbyname()
50 (including the dd.dd.dd.dd notation) and port is in host byte order
51- where a hostname is returned, the dd.dd.dd.dd notation is used
Guido van Rossum27e177d1995-03-16 15:43:47 +000052- a UNIX domain socket address is a string specifying the pathname
Jeremy Hylton22308652001-02-02 03:23:09 +000053- an AF_PACKET socket address is a tuple containing a string
54 specifying the ethernet interface and an integer specifying
55 the Ethernet protocol number to be received. For example:
Jeremy Hyltondbfb6622001-02-02 19:55:17 +000056 ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +000057 specify packet-type and ha-type/addr.
Christian Heimes043d6f62008-01-07 17:19:16 +000058- an AF_TIPC socket address is expressed as
59 (addr_type, v1, v2, v3 [, scope]); where addr_type can be one of:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID;
Christian Heimes043d6f62008-01-07 17:19:16 +000061 and scope can be one of:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE.
Christian Heimes043d6f62008-01-07 17:19:16 +000063 The meaning of v1, v2 and v3 depends on the value of addr_type:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 if addr_type is TIPC_ADDR_NAME:
65 v1 is the server type
66 v2 is the port identifier
67 v3 is ignored
68 if addr_type is TIPC_ADDR_NAMESEQ:
69 v1 is the server type
70 v2 is the lower port number
71 v3 is the upper port number
72 if addr_type is TIPC_ADDR_ID:
73 v1 is the node
74 v2 is the ref
75 v3 is ignored
Christian Heimes043d6f62008-01-07 17:19:16 +000076
Guido van Rossum6574b3e1991-06-25 21:36:08 +000077
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000078Local naming conventions:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000079
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000080- names starting with sock_ are socket object methods
81- names starting with socket_ are module-level functions
82- names starting with PySocket are exported through socketmodule.h
Guido van Rossum30a685f1991-06-27 15:51:29 +000083
Guido van Rossum6574b3e1991-06-25 21:36:08 +000084*/
85
Thomas Wouters477c8d52006-05-27 19:21:47 +000086#ifdef __APPLE__
Ned Deily47299fd2016-02-15 16:54:08 +110087#include <AvailabilityMacros.h>
88/* for getaddrinfo thread safety test on old versions of OS X */
89#ifndef MAC_OS_X_VERSION_10_5
90#define MAC_OS_X_VERSION_10_5 1050
91#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000092 /*
93 * inet_aton is not available on OSX 10.3, yet we want to use a binary
94 * that was build on 10.4 or later to work on that release, weak linking
95 * comes to the rescue.
96 */
97# pragma weak inet_aton
98#endif
99
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000100#include "Python.h"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000101#include "structmember.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000102
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000103/* Socket object documentation */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000104PyDoc_STRVAR(sock_doc,
Victor Stinnerdaf45552013-08-28 00:53:59 +0200105"socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None) -> socket object\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000106\n\
107Open a socket of the given type. The family argument specifies the\n\
108address family; it defaults to AF_INET. The type argument specifies\n\
109whether this is a stream (SOCK_STREAM, this is the default)\n\
110or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
111specifying the default protocol. Keyword arguments are accepted.\n\
Victor Stinnerdaf45552013-08-28 00:53:59 +0200112The socket is created as non-inheritable.\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000113\n\
114A socket object represents one endpoint of a network connection.\n\
115\n\
116Methods of socket objects (keyword arguments not allowed):\n\
117\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000118_accept() -- accept connection, returning new socket fd and client address\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000119bind(addr) -- bind the socket to a local address\n\
120close() -- close the socket\n\
121connect(addr) -- connect the socket to a remote address\n\
122connect_ex(addr) -- connect, return an error code instead of an exception\n\
Victor Stinnerdaf45552013-08-28 00:53:59 +0200123dup() -- return a new socket fd duplicated from fileno()\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000124fileno() -- return underlying file descriptor\n\
125getpeername() -- return remote address [*]\n\
126getsockname() -- return local address\n\
127getsockopt(level, optname[, buflen]) -- get socket options\n\
128gettimeout() -- return timeout or None\n\
Charles-François Natali644b8f52014-05-22 19:45:39 +0100129listen([n]) -- start listening for incoming connections\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000130recv(buflen[, flags]) -- receive data\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000131recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000132recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000133recvfrom_into(buffer[, nbytes, [, flags])\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000134 -- receive data and sender\'s address (into a buffer)\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000135sendall(data[, flags]) -- send all data\n\
136send(data[, flags]) -- send data, may not send all of it\n\
137sendto(data[, flags], addr) -- send data to a given address\n\
138setblocking(0 | 1) -- set or clear the blocking I/O flag\n\
Christian Heimesdffa3942016-09-05 23:54:41 +0200139setsockopt(level, optname, value[, optlen]) -- set socket options\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000140settimeout(None | float) -- set or clear the timeout\n\
141shutdown(how) -- shut down traffic in one or both directions\n\
Gregory P. Smith5ed2e772011-05-15 00:26:45 -0700142if_nameindex() -- return all network interface indices and names\n\
Gregory P. Smithb6471db2011-05-22 22:47:55 -0700143if_nametoindex(name) -- return the corresponding interface index\n\
144if_indextoname(index) -- return the corresponding interface name\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000145\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000146 [*] not available on all platforms!");
Guido van Rossum3baaa131999-03-22 21:44:51 +0000147
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000148/* XXX This is a terrible mess of platform-dependent preprocessor hacks.
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000149 I hope some day someone can clean this up please... */
150
Guido van Rossum9376b741999-09-15 22:01:40 +0000151/* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
152 script doesn't get this right, so we hardcode some platform checks below.
153 On the other hand, not all Linux versions agree, so there the settings
154 computed by the configure script are needed! */
155
Benjamin Petersonb6c34582016-09-07 14:08:34 -0700156#ifndef __linux__
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000157# undef HAVE_GETHOSTBYNAME_R_3_ARG
158# undef HAVE_GETHOSTBYNAME_R_5_ARG
159# undef HAVE_GETHOSTBYNAME_R_6_ARG
Guido van Rossum9376b741999-09-15 22:01:40 +0000160#endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000161
Victor Stinner710d27e2011-08-23 10:57:32 +0200162#if defined(__OpenBSD__)
163# include <sys/uio.h>
164#endif
165
Stefan Krah1f9eb872016-05-22 17:35:34 +0200166#if !defined(WITH_THREAD)
167# undef HAVE_GETHOSTBYNAME_R
168#endif
169
170#if defined(__ANDROID__) && __ANDROID_API__ < 23
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000171# undef HAVE_GETHOSTBYNAME_R
Guido van Rossum7a122991999-04-13 04:07:32 +0000172#endif
173
Guido van Rossume7de2061999-03-24 17:24:33 +0000174#ifdef HAVE_GETHOSTBYNAME_R
Martin Panterc9e08d82016-11-14 04:26:36 +0000175# if defined(_AIX) && !defined(_LINUX_SOURCE_COMPAT)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000176# define HAVE_GETHOSTBYNAME_R_3_ARG
177# elif defined(__sun) || defined(__sgi)
178# define HAVE_GETHOSTBYNAME_R_5_ARG
Benjamin Petersonb6c34582016-09-07 14:08:34 -0700179# elif defined(__linux__)
Guido van Rossum9376b741999-09-15 22:01:40 +0000180/* Rely on the configure script */
Martin Panterc9e08d82016-11-14 04:26:36 +0000181# elif defined(_LINUX_SOURCE_COMPAT) /* Linux compatibility on AIX */
182# define HAVE_GETHOSTBYNAME_R_6_ARG
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000183# else
184# undef HAVE_GETHOSTBYNAME_R
185# endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000186#endif
187
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000188#if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \
189 !defined(MS_WINDOWS)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000190# define USE_GETHOSTBYNAME_LOCK
Guido van Rossum3baaa131999-03-22 21:44:51 +0000191#endif
192
Ned Deilye1d4e582016-02-23 22:05:29 +1100193/* To use __FreeBSD_version, __OpenBSD__, and __NetBSD_Version__ */
Hye-Shik Chang9ceebd52005-09-24 14:58:47 +0000194#ifdef HAVE_SYS_PARAM_H
195#include <sys/param.h>
196#endif
Just van Rossum1040d2c2003-05-09 07:53:18 +0000197/* On systems on which getaddrinfo() is believed to not be thread-safe,
Ned Deily47299fd2016-02-15 16:54:08 +1100198 (this includes the getaddrinfo emulation) protect access with a lock.
199
200 getaddrinfo is thread-safe on Mac OS X 10.5 and later. Originally it was
201 a mix of code including an unsafe implementation from an old BSD's
202 libresolv. In 10.5 Apple reimplemented it as a safe IPC call to the
203 mDNSResponder process. 10.5 is the first be UNIX '03 certified, which
Ned Deilye1d4e582016-02-23 22:05:29 +1100204 includes the requirement that getaddrinfo be thread-safe. See issue #25924.
Ned Deily47299fd2016-02-15 16:54:08 +1100205
Ned Deilye1d4e582016-02-23 22:05:29 +1100206 It's thread-safe in OpenBSD starting with 5.4, released Nov 2013:
207 http://www.openbsd.org/plus54.html
208
209 It's thread-safe in NetBSD starting with 4.0, released Dec 2007:
210
211http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/net/getaddrinfo.c.diff?r1=1.82&r2=1.83
Ned Deily47299fd2016-02-15 16:54:08 +1100212 */
213#if defined(WITH_THREAD) && ( \
214 (defined(__APPLE__) && \
215 MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5) || \
Hye-Shik Chang9ceebd52005-09-24 14:58:47 +0000216 (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \
Ned Deilye1d4e582016-02-23 22:05:29 +1100217 (defined(__OpenBSD__) && OpenBSD+0 < 201311) || \
218 (defined(__NetBSD__) && __NetBSD_Version__+0 < 400000000) || \
Christian Heimesaf01f662013-12-21 16:19:10 +0100219 !defined(HAVE_GETADDRINFO))
Just van Rossum1040d2c2003-05-09 07:53:18 +0000220#define USE_GETADDRINFO_LOCK
221#endif
222
223#ifdef USE_GETADDRINFO_LOCK
224#define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1);
225#define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock);
226#else
227#define ACQUIRE_GETADDRINFO_LOCK
228#define RELEASE_GETADDRINFO_LOCK
229#endif
230
231#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000232# include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000233#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000234
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000235#if defined(PYCC_VACPP)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000236# include <types.h>
237# include <io.h>
238# include <sys/ioctl.h>
239# include <utils.h>
240# include <ctype.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000241#endif
242
Martin v. Löwis9d6c6692012-02-03 17:44:58 +0100243#ifdef __APPLE__
244# include <sys/ioctl.h>
245#endif
246
247
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000248#if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000249/* make sure that the reentrant (gethostbyaddr_r etc)
250 functions are declared correctly if compiling with
251 MIPSPro 7.x in ANSI C mode (default) */
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000252
Thomas Wouters477c8d52006-05-27 19:21:47 +0000253/* XXX Using _SGIAPI is the wrong thing,
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000254 but I don't know what the right thing is. */
Trent Mick8ea5bdf2004-09-13 17:48:41 +0000255#undef _SGIAPI /* to avoid warning */
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000256#define _SGIAPI 1
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000257
Trent Mick8ea5bdf2004-09-13 17:48:41 +0000258#undef _XOPEN_SOURCE
259#include <sys/socket.h>
260#include <sys/types.h>
261#include <netinet/in.h>
262#ifdef _SS_ALIGNSIZE
263#define HAVE_GETADDRINFO 1
264#define HAVE_GETNAMEINFO 1
265#endif
266
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000267#define HAVE_INET_PTON
268#include <netdb.h>
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000269#endif
270
Thomas Wouters477c8d52006-05-27 19:21:47 +0000271/* Irix 6.5 fails to define this variable at all. This is needed
272 for both GCC and SGI's compiler. I'd say that the SGI headers
Georg Brandldbd83392006-02-20 09:42:33 +0000273 are just busted. Same thing for Solaris. */
274#if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN)
Anthony Baxterbab23cf2003-10-04 08:00:49 +0000275#define INET_ADDRSTRLEN 16
276#endif
277
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000278/* Generic includes */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000279#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000280#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000281#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000282
Gregory P. Smith3b1f2c32011-05-15 12:18:23 -0700283#ifdef HAVE_SYS_SOCKET_H
284#include <sys/socket.h>
285#endif
286
287#ifdef HAVE_NET_IF_H
288#include <net/if.h>
289#endif
290
Christian Heimesdffa3942016-09-05 23:54:41 +0200291#ifdef HAVE_SOCKADDR_ALG
292#include <linux/if_alg.h>
293#ifndef AF_ALG
294#define AF_ALG 38
295#endif
296#ifndef SOL_ALG
297#define SOL_ALG 279
298#endif
299
300/* Linux 3.19 */
301#ifndef ALG_SET_AEAD_ASSOCLEN
302#define ALG_SET_AEAD_ASSOCLEN 4
303#endif
304#ifndef ALG_SET_AEAD_AUTHSIZE
305#define ALG_SET_AEAD_AUTHSIZE 5
306#endif
307/* Linux 4.8 */
308#ifndef ALG_SET_PUBKEY
309#define ALG_SET_PUBKEY 6
310#endif
311
312#ifndef ALG_OP_SIGN
313#define ALG_OP_SIGN 2
314#endif
315#ifndef ALG_OP_VERIFY
316#define ALG_OP_VERIFY 3
317#endif
318
319#endif /* HAVE_SOCKADDR_ALG */
320
Marc-André Lemburg976ade62002-02-16 18:47:07 +0000321/* Generic socket object definitions and includes */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000322#define PySocket_BUILDING_SOCKET
Marc-André Lemburgbb8b78b2002-02-16 18:44:52 +0000323#include "socketmodule.h"
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000324
325/* Addressing includes */
326
Guido van Rossum6f489d91996-06-28 20:15:15 +0000327#ifndef MS_WINDOWS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000328
329/* Non-MS WINDOWS includes */
330# include <netdb.h>
Nick Coghlan4ecf6cf2011-08-22 15:45:19 +1000331# include <unistd.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000332
Guido van Rossum9376b741999-09-15 22:01:40 +0000333/* Headers needed for inet_ntoa() and inet_addr() */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000334# include <arpa/inet.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000335
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000336# include <fcntl.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000337
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000338#else
Guido van Rossum48a680c2001-03-02 06:34:14 +0000339
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000340/* MS_WINDOWS includes */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000341# ifdef HAVE_FCNTL_H
342# include <fcntl.h>
343# endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000344
Steve Dower65e4cb12014-11-22 12:54:57 -0800345#if defined(_MSC_VER) && _MSC_VER >= 1800
346/* Provides the IsWindows7SP1OrGreater() function */
347#include <VersionHelpers.h>
348#endif
349
Jeremy Hylton22308652001-02-02 03:23:09 +0000350#endif
351
Skip Montanaro7befb992004-02-10 16:50:21 +0000352#include <stddef.h>
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000353
Neal Norwitz39d22e52002-11-02 19:55:21 +0000354#ifndef O_NONBLOCK
355# define O_NONBLOCK O_NDELAY
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000356#endif
357
Trent Micka708d6e2004-09-07 17:48:26 +0000358/* include Python's addrinfo.h unless it causes trouble */
359#if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE)
360 /* Do not include addinfo.h on some newer IRIX versions.
361 * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21,
362 * for example, but not by 6.5.10.
363 */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000364#elif defined(_MSC_VER) && _MSC_VER>1201
Trent Micka708d6e2004-09-07 17:48:26 +0000365 /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and
366 * EAI_* constants are defined in (the already included) ws2tcpip.h.
367 */
368#else
369# include "addrinfo.h"
370#endif
Jason Tishlerc246cb72004-08-09 13:25:59 +0000371
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000372#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +0000373#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000374int inet_pton(int af, const char *src, void *dst);
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000375const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000376#endif
Christian Heimesb6150692007-11-15 23:37:07 +0000377#endif
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000378
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000379#ifdef __APPLE__
380/* On OS X, getaddrinfo returns no error indication of lookup
381 failure, so we must use the emulation instead of the libinfo
382 implementation. Unfortunately, performing an autoconf test
383 for this bug would require DNS access for the machine performing
384 the configuration, which is not acceptable. Therefore, we
385 determine the bug just by checking for __APPLE__. If this bug
386 gets ever fixed, perhaps checking for sys/version.h would be
387 appropriate, which is 10/0 on the system with the bug. */
Jack Jansen84262fb2002-07-02 14:40:42 +0000388#ifndef HAVE_GETNAMEINFO
389/* This bug seems to be fixed in Jaguar. Ths easiest way I could
390 Find to check for Jaguar is that it has getnameinfo(), which
391 older releases don't have */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000392#undef HAVE_GETADDRINFO
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000393#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000394
395#ifdef HAVE_INET_ATON
396#define USE_INET_ATON_WEAKLINK
397#endif
398
Jack Jansen84262fb2002-07-02 14:40:42 +0000399#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000400
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000401/* I know this is a bad practice, but it is the easiest... */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000402#if !defined(HAVE_GETADDRINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000403/* avoid clashes with the C library definition of the symbol. */
404#define getaddrinfo fake_getaddrinfo
405#define gai_strerror fake_gai_strerror
406#define freeaddrinfo fake_freeaddrinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000407#include "getaddrinfo.c"
408#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000409#if !defined(HAVE_GETNAMEINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000410#define getnameinfo fake_getnameinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000411#include "getnameinfo.c"
412#endif
413
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000414#ifdef MS_WINDOWS
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000415#define SOCKETCLOSE closesocket
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000416#endif
417
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000418#ifdef MS_WIN32
Kristján Valur Jónsson310052c2012-03-31 13:35:00 +0000419#undef EAFNOSUPPORT
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000420#define EAFNOSUPPORT WSAEAFNOSUPPORT
421#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000422#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000423
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000424#ifndef SOCKETCLOSE
425#define SOCKETCLOSE close
426#endif
427
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +0000428#if (defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)) && !defined(__NetBSD__) && !defined(__DragonFly__)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000429#define USE_BLUETOOTH 1
430#if defined(__FreeBSD__)
431#define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP
432#define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM
Thomas Wouterscf297e42007-02-23 15:07:44 +0000433#define BTPROTO_HCI BLUETOOTH_PROTO_HCI
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000434#define SOL_HCI SOL_HCI_RAW
435#define HCI_FILTER SO_HCI_RAW_FILTER
Hye-Shik Chang81268602004-02-02 06:05:24 +0000436#define sockaddr_l2 sockaddr_l2cap
437#define sockaddr_rc sockaddr_rfcomm
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000438#define hci_dev hci_node
Hye-Shik Chang81268602004-02-02 06:05:24 +0000439#define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb)
440#define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000441#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +0000442#elif defined(__NetBSD__) || defined(__DragonFly__)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000443#define sockaddr_l2 sockaddr_bt
444#define sockaddr_rc sockaddr_bt
Thomas Wouterscf297e42007-02-23 15:07:44 +0000445#define sockaddr_hci sockaddr_bt
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000446#define sockaddr_sco sockaddr_bt
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +0000447#define SOL_HCI BTPROTO_HCI
448#define HCI_DATA_DIR SO_HCI_DIRECTION
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000449#define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb)
450#define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000451#define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000452#define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000453#else
Hye-Shik Chang81268602004-02-02 06:05:24 +0000454#define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)
455#define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000456#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000457#define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)
458#endif
459#endif
460
Charles-François Natali8b759652011-12-23 16:44:51 +0100461/* Convert "sock_addr_t *" to "struct sockaddr *". */
462#define SAS2SA(x) (&((x)->sa))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000463
Martin v. Löwise9416172003-05-03 10:12:45 +0000464/*
465 * Constants for getnameinfo()
466 */
467#if !defined(NI_MAXHOST)
468#define NI_MAXHOST 1025
469#endif
470#if !defined(NI_MAXSERV)
471#define NI_MAXSERV 32
472#endif
473
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000474#ifndef INVALID_SOCKET /* MS defines this */
475#define INVALID_SOCKET (-1)
476#endif
477
Charles-François Natali0cc86852013-09-13 19:53:08 +0200478#ifndef INADDR_NONE
479#define INADDR_NONE (-1)
480#endif
481
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000482/* XXX There's a problem here: *static* functions are not supposed to have
483 a Py prefix (or use CapitalizedWords). Later... */
484
Guido van Rossum30a685f1991-06-27 15:51:29 +0000485/* Global variable holding the exception type for errors detected
486 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000487static PyObject *socket_herror;
488static PyObject *socket_gaierror;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000489static PyObject *socket_timeout;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000490
Tim Peters643a7fc2002-02-17 04:13:21 +0000491/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000492 The sock_type variable contains pointers to various functions,
493 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000494 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000495static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000496
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000497#if defined(HAVE_POLL_H)
498#include <poll.h>
499#elif defined(HAVE_SYS_POLL_H)
500#include <sys/poll.h>
501#endif
502
Nick Coghlan96fe56a2011-08-22 11:55:57 +1000503/* Largest value to try to store in a socklen_t (used when handling
504 ancillary data). POSIX requires socklen_t to hold at least
505 (2**31)-1 and recommends against storing larger values, but
506 socklen_t was originally int in the BSD interface, so to be on the
507 safe side we use the smaller of (2**31)-1 and INT_MAX. */
508#if INT_MAX > 0x7fffffff
509#define SOCKLEN_T_LIMIT 0x7fffffff
510#else
511#define SOCKLEN_T_LIMIT INT_MAX
512#endif
513
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200514#ifdef HAVE_POLL
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000515/* Instead of select(), we'll use poll() since poll() works on any fd. */
516#define IS_SELECTABLE(s) 1
517/* Can we call select() with this socket without a buffer overrun? */
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000518#else
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200519/* If there's no timeout left, we don't have to call select, so it's a safe,
520 * little white lie. */
Victor Stinner71694d52015-03-28 01:18:54 +0100521#define IS_SELECTABLE(s) (_PyIsSelectable_fd((s)->sock_fd) || (s)->sock_timeout <= 0)
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000522#endif
Neal Norwitz082b2df2006-02-07 07:04:46 +0000523
524static PyObject*
525select_error(void)
526{
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200527 PyErr_SetString(PyExc_OSError, "unable to select on socket");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 return NULL;
Neal Norwitz082b2df2006-02-07 07:04:46 +0000529}
530
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000531#ifdef MS_WINDOWS
Antoine Pitrouc8f01962010-09-28 22:03:27 +0000532#ifndef WSAEAGAIN
533#define WSAEAGAIN WSAEWOULDBLOCK
534#endif
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000535#define CHECK_ERRNO(expected) \
536 (WSAGetLastError() == WSA ## expected)
537#else
538#define CHECK_ERRNO(expected) \
539 (errno == expected)
540#endif
541
Victor Stinnerdaf45552013-08-28 00:53:59 +0200542#ifdef MS_WINDOWS
Victor Stinner81c41db2015-04-02 11:50:57 +0200543# define GET_SOCK_ERROR WSAGetLastError()
544# define SET_SOCK_ERROR(err) WSASetLastError(err)
545# define SOCK_TIMEOUT_ERR WSAEWOULDBLOCK
546# define SOCK_INPROGRESS_ERR WSAEWOULDBLOCK
547#else
548# define GET_SOCK_ERROR errno
549# define SET_SOCK_ERROR(err) do { errno = err; } while (0)
550# define SOCK_TIMEOUT_ERR EWOULDBLOCK
551# define SOCK_INPROGRESS_ERR EINPROGRESS
552#endif
553
554
555#ifdef MS_WINDOWS
Victor Stinnerdaf45552013-08-28 00:53:59 +0200556/* Does WSASocket() support the WSA_FLAG_NO_HANDLE_INHERIT flag? */
557static int support_wsa_no_inherit = -1;
558#endif
559
Guido van Rossum30a685f1991-06-27 15:51:29 +0000560/* Convenience function to raise an error according to errno
561 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000562
Guido van Rossum73624e91994-10-10 17:59:00 +0000563static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000564set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000565{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000566#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 int err_no = WSAGetLastError();
568 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
569 recognizes the error codes used by both GetLastError() and
570 WSAGetLastError */
571 if (err_no)
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200572 return PyErr_SetExcFromWindowsErr(PyExc_OSError, err_no);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000573#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000574
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200575 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000576}
577
Guido van Rossum30a685f1991-06-27 15:51:29 +0000578
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000579static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000580set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000581{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000583
584#ifdef HAVE_HSTRERROR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000586#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 v = Py_BuildValue("(is)", h_error, "host not found");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000588#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 if (v != NULL) {
590 PyErr_SetObject(socket_herror, v);
591 Py_DECREF(v);
592 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000595}
596
597
598static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000599set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000602
Martin v. Löwis272cb402002-03-01 08:31:07 +0000603#ifdef EAI_SYSTEM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 /* EAI_SYSTEM is not available on Windows XP. */
605 if (error == EAI_SYSTEM)
606 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000607#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000608
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000609#ifdef HAVE_GAI_STRERROR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000611#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000613#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 if (v != NULL) {
615 PyErr_SetObject(socket_gaierror, v);
616 Py_DECREF(v);
617 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000620}
621
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000622/* Function to perform the setting of socket blocking mode
623 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000624static int
625internal_setblocking(PySocketSockObject *s, int block)
626{
Yury Selivanovfa22b292016-10-18 16:03:52 -0400627 int result = -1;
Victor Stinnerb6dab6b2014-07-23 22:56:55 +0200628#ifdef MS_WINDOWS
629 u_long arg;
630#endif
Victor Stinner9a954832013-12-04 00:41:24 +0100631#if !defined(MS_WINDOWS) \
Christian Heimesaf01f662013-12-21 16:19:10 +0100632 && !((defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)))
Victor Stinner9a954832013-12-04 00:41:24 +0100633 int delay_flag, new_delay_flag;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000634#endif
Antoine Pitroub1c54962010-10-14 15:05:38 +0000635#ifdef SOCK_NONBLOCK
636 if (block)
637 s->sock_type &= (~SOCK_NONBLOCK);
638 else
639 s->sock_type |= SOCK_NONBLOCK;
640#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +0000641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 Py_BEGIN_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000643#ifndef MS_WINDOWS
Christian Heimesaf01f662013-12-21 16:19:10 +0100644#if (defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 block = !block;
Christian Heimes0b9d64e2016-09-09 00:28:57 +0200646 if (ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block) == -1)
Yury Selivanovfa22b292016-10-18 16:03:52 -0400647 goto done;
Victor Stinner9a954832013-12-04 00:41:24 +0100648#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
Christian Heimes0b9d64e2016-09-09 00:28:57 +0200650 if (delay_flag == -1)
Yury Selivanovfa22b292016-10-18 16:03:52 -0400651 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 if (block)
Victor Stinner9a954832013-12-04 00:41:24 +0100653 new_delay_flag = delay_flag & (~O_NONBLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 else
Victor Stinner9a954832013-12-04 00:41:24 +0100655 new_delay_flag = delay_flag | O_NONBLOCK;
656 if (new_delay_flag != delay_flag)
Christian Heimes0b9d64e2016-09-09 00:28:57 +0200657 if (fcntl(s->sock_fd, F_SETFL, new_delay_flag) == -1)
Yury Selivanovfa22b292016-10-18 16:03:52 -0400658 goto done;
Victor Stinner9a954832013-12-04 00:41:24 +0100659#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +0000660#else /* MS_WINDOWS */
Victor Stinnerb6dab6b2014-07-23 22:56:55 +0200661 arg = !block;
Christian Heimes0b9d64e2016-09-09 00:28:57 +0200662 if (ioctlsocket(s->sock_fd, FIONBIO, &arg) != 0)
Yury Selivanovfa22b292016-10-18 16:03:52 -0400663 goto done;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000664#endif /* MS_WINDOWS */
Yury Selivanovfa22b292016-10-18 16:03:52 -0400665
666 result = 0;
667
668 done:
Martin Panterfa27d5f2016-10-20 00:48:23 +0000669 ; /* necessary for --without-threads flag */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000671
Yury Selivanovfa22b292016-10-18 16:03:52 -0400672 if (result) {
Christian Heimes0b9d64e2016-09-09 00:28:57 +0200673#ifndef MS_WINDOWS
Yury Selivanovfa22b292016-10-18 16:03:52 -0400674 PyErr_SetFromErrno(PyExc_OSError);
Christian Heimes0b9d64e2016-09-09 00:28:57 +0200675#else
Yury Selivanovfa22b292016-10-18 16:03:52 -0400676 PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
Christian Heimes0b9d64e2016-09-09 00:28:57 +0200677#endif
Yury Selivanovfa22b292016-10-18 16:03:52 -0400678 }
679
680 return result;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000681}
682
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000683static int
Victor Stinner81c41db2015-04-02 11:50:57 +0200684internal_select(PySocketSockObject *s, int writing, _PyTime_t interval,
685 int connect)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 int n;
Victor Stinner71694d52015-03-28 01:18:54 +0100688#ifdef HAVE_POLL
689 struct pollfd pollfd;
Victor Stinnere6951c62015-03-31 13:50:44 +0200690 _PyTime_t ms;
Victor Stinner71694d52015-03-28 01:18:54 +0100691#else
Victor Stinner416f2e62015-03-31 13:56:29 +0200692 fd_set fds, efds;
Victor Stinnerced11742015-04-09 10:27:25 +0200693 struct timeval tv, *tvp;
Victor Stinner71694d52015-03-28 01:18:54 +0100694#endif
Guido van Rossum11ba0942002-06-13 15:07:44 +0000695
Victor Stinnerb7df3142015-03-27 22:59:32 +0100696#ifdef WITH_THREAD
697 /* must be called with the GIL held */
698 assert(PyGILState_Check());
699#endif
700
Victor Stinner416f2e62015-03-31 13:56:29 +0200701 /* Error condition is for output only */
Victor Stinner81c41db2015-04-02 11:50:57 +0200702 assert(!(connect && !writing));
Guido van Rossum67f7a382002-06-06 21:08:16 +0000703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 /* Guard against closed socket */
Victor Stinner0cec8772016-07-22 17:26:53 +0200705 if (s->sock_fd == INVALID_SOCKET)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 /* Prefer poll, if available, since you can poll() any fd
709 * which can't be done with select(). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000710#ifdef HAVE_POLL
Victor Stinner71694d52015-03-28 01:18:54 +0100711 pollfd.fd = s->sock_fd;
712 pollfd.events = writing ? POLLOUT : POLLIN;
Victor Stinner81c41db2015-04-02 11:50:57 +0200713 if (connect) {
714 /* On Windows, the socket becomes writable on connection success,
715 but a connection failure is notified as an error. On POSIX, the
716 socket becomes writable on connection success or on connection
717 failure. */
Victor Stinner416f2e62015-03-31 13:56:29 +0200718 pollfd.events |= POLLERR;
Victor Stinner81c41db2015-04-02 11:50:57 +0200719 }
Guido van Rossum67f7a382002-06-06 21:08:16 +0000720
Victor Stinner71694d52015-03-28 01:18:54 +0100721 /* s->sock_timeout is in seconds, timeout in ms */
Victor Stinner10550cd2015-04-03 13:22:27 +0200722 ms = _PyTime_AsMilliseconds(interval, _PyTime_ROUND_CEILING);
Victor Stinnere6951c62015-03-31 13:50:44 +0200723 assert(ms <= INT_MAX);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000724
Victor Stinner71694d52015-03-28 01:18:54 +0100725 Py_BEGIN_ALLOW_THREADS;
Victor Stinnere6951c62015-03-31 13:50:44 +0200726 n = poll(&pollfd, 1, (int)ms);
Victor Stinner71694d52015-03-28 01:18:54 +0100727 Py_END_ALLOW_THREADS;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000728#else
Victor Stinnerced11742015-04-09 10:27:25 +0200729 if (interval >= 0) {
730 _PyTime_AsTimeval_noraise(interval, &tv, _PyTime_ROUND_CEILING);
731 tvp = &tv;
732 }
733 else
734 tvp = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000735
Victor Stinner71694d52015-03-28 01:18:54 +0100736 FD_ZERO(&fds);
737 FD_SET(s->sock_fd, &fds);
Victor Stinner416f2e62015-03-31 13:56:29 +0200738 FD_ZERO(&efds);
Victor Stinner81c41db2015-04-02 11:50:57 +0200739 if (connect) {
740 /* On Windows, the socket becomes writable on connection success,
741 but a connection failure is notified as an error. On POSIX, the
742 socket becomes writable on connection success or on connection
743 failure. */
Victor Stinner416f2e62015-03-31 13:56:29 +0200744 FD_SET(s->sock_fd, &efds);
Victor Stinner81c41db2015-04-02 11:50:57 +0200745 }
Victor Stinner71694d52015-03-28 01:18:54 +0100746
747 /* See if the socket is ready */
748 Py_BEGIN_ALLOW_THREADS;
749 if (writing)
750 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
Victor Stinnerced11742015-04-09 10:27:25 +0200751 NULL, &fds, &efds, tvp);
Victor Stinner71694d52015-03-28 01:18:54 +0100752 else
753 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
Victor Stinnerced11742015-04-09 10:27:25 +0200754 &fds, NULL, &efds, tvp);
Victor Stinner71694d52015-03-28 01:18:54 +0100755 Py_END_ALLOW_THREADS;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000756#endif
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 if (n < 0)
759 return -1;
760 if (n == 0)
761 return 1;
762 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000763}
764
Victor Stinner31bf2d52015-04-01 21:57:09 +0200765/* Call a socket function.
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000766
Victor Stinner81c41db2015-04-02 11:50:57 +0200767 On error, raise an exception and return -1 if err is set, or fill err and
768 return -1 otherwise. If a signal was received and the signal handler raised
769 an exception, return -1, and set err to -1 if err is set.
770
771 On success, return 0, and set err to 0 if err is set.
Victor Stinnerb7df3142015-03-27 22:59:32 +0100772
Victor Stinner31bf2d52015-04-01 21:57:09 +0200773 If the socket has a timeout, wait until the socket is ready before calling
774 the function: wait until the socket is writable if writing is nonzero, wait
775 until the socket received data otherwise.
Victor Stinnerb7df3142015-03-27 22:59:32 +0100776
Victor Stinner81c41db2015-04-02 11:50:57 +0200777 If the socket function is interrupted by a signal (failed with EINTR): retry
Victor Stinner31bf2d52015-04-01 21:57:09 +0200778 the function, except if the signal handler raised an exception (PEP 475).
779
780 When the function is retried, recompute the timeout using a monotonic clock.
781
Victor Stinner81c41db2015-04-02 11:50:57 +0200782 sock_call_ex() must be called with the GIL held. The socket function is
783 called with the GIL released. */
Victor Stinner31bf2d52015-04-01 21:57:09 +0200784static int
Victor Stinner81c41db2015-04-02 11:50:57 +0200785sock_call_ex(PySocketSockObject *s,
786 int writing,
787 int (*sock_func) (PySocketSockObject *s, void *data),
788 void *data,
789 int connect,
Victor Stinner8912d142015-04-06 23:16:34 +0200790 int *err,
791 _PyTime_t timeout)
Victor Stinner31bf2d52015-04-01 21:57:09 +0200792{
Victor Stinner8912d142015-04-06 23:16:34 +0200793 int has_timeout = (timeout > 0);
Victor Stinner31bf2d52015-04-01 21:57:09 +0200794 _PyTime_t deadline = 0;
795 int deadline_initialized = 0;
796 int res;
797
Victor Stinner92f01132015-10-11 09:54:42 +0200798#ifdef WITH_THREAD
Victor Stinner31bf2d52015-04-01 21:57:09 +0200799 /* sock_call() must be called with the GIL held. */
800 assert(PyGILState_Check());
Victor Stinner92f01132015-10-11 09:54:42 +0200801#endif
Victor Stinner31bf2d52015-04-01 21:57:09 +0200802
803 /* outer loop to retry select() when select() is interrupted by a signal
Victor Stinner81c41db2015-04-02 11:50:57 +0200804 or to retry select()+sock_func() on false positive (see above) */
Victor Stinner31bf2d52015-04-01 21:57:09 +0200805 while (1) {
Victor Stinner81c41db2015-04-02 11:50:57 +0200806 /* For connect(), poll even for blocking socket. The connection
807 runs asynchronously. */
808 if (has_timeout || connect) {
Victor Stinner81c41db2015-04-02 11:50:57 +0200809 if (has_timeout) {
Victor Stinner10550cd2015-04-03 13:22:27 +0200810 _PyTime_t interval;
811
Victor Stinner81c41db2015-04-02 11:50:57 +0200812 if (deadline_initialized) {
813 /* recompute the timeout */
814 interval = deadline - _PyTime_GetMonotonicClock();
815 }
816 else {
817 deadline_initialized = 1;
Victor Stinner8912d142015-04-06 23:16:34 +0200818 deadline = _PyTime_GetMonotonicClock() + timeout;
819 interval = timeout;
Victor Stinner81c41db2015-04-02 11:50:57 +0200820 }
Victor Stinner31bf2d52015-04-01 21:57:09 +0200821
Victor Stinner10550cd2015-04-03 13:22:27 +0200822 if (interval >= 0)
823 res = internal_select(s, writing, interval, connect);
824 else
825 res = 1;
826 }
827 else {
Victor Stinner222dfc72015-04-06 22:30:41 +0200828 res = internal_select(s, writing, timeout, connect);
Victor Stinner10550cd2015-04-03 13:22:27 +0200829 }
830
Victor Stinner31bf2d52015-04-01 21:57:09 +0200831 if (res == -1) {
Victor Stinner81c41db2015-04-02 11:50:57 +0200832 if (err)
833 *err = GET_SOCK_ERROR;
834
Victor Stinner31bf2d52015-04-01 21:57:09 +0200835 if (CHECK_ERRNO(EINTR)) {
836 /* select() was interrupted by a signal */
Victor Stinner81c41db2015-04-02 11:50:57 +0200837 if (PyErr_CheckSignals()) {
838 if (err)
839 *err = -1;
Victor Stinner31bf2d52015-04-01 21:57:09 +0200840 return -1;
Victor Stinner81c41db2015-04-02 11:50:57 +0200841 }
Victor Stinner31bf2d52015-04-01 21:57:09 +0200842
843 /* retry select() */
844 continue;
845 }
846
847 /* select() failed */
848 s->errorhandler();
849 return -1;
850 }
851
852 if (res == 1) {
Victor Stinner81c41db2015-04-02 11:50:57 +0200853 if (err)
854 *err = SOCK_TIMEOUT_ERR;
855 else
856 PyErr_SetString(socket_timeout, "timed out");
Victor Stinner31bf2d52015-04-01 21:57:09 +0200857 return -1;
858 }
859
860 /* the socket is ready */
861 }
862
Victor Stinner81c41db2015-04-02 11:50:57 +0200863 /* inner loop to retry sock_func() when sock_func() is interrupted
864 by a signal */
Victor Stinner31bf2d52015-04-01 21:57:09 +0200865 while (1) {
866 Py_BEGIN_ALLOW_THREADS
Victor Stinner81c41db2015-04-02 11:50:57 +0200867 res = sock_func(s, data);
Victor Stinner31bf2d52015-04-01 21:57:09 +0200868 Py_END_ALLOW_THREADS
869
870 if (res) {
Victor Stinner81c41db2015-04-02 11:50:57 +0200871 /* sock_func() succeeded */
872 if (err)
873 *err = 0;
Victor Stinner31bf2d52015-04-01 21:57:09 +0200874 return 0;
875 }
876
Victor Stinner81c41db2015-04-02 11:50:57 +0200877 if (err)
878 *err = GET_SOCK_ERROR;
879
Victor Stinner31bf2d52015-04-01 21:57:09 +0200880 if (!CHECK_ERRNO(EINTR))
881 break;
882
Victor Stinner81c41db2015-04-02 11:50:57 +0200883 /* sock_func() was interrupted by a signal */
884 if (PyErr_CheckSignals()) {
885 if (err)
886 *err = -1;
Victor Stinner31bf2d52015-04-01 21:57:09 +0200887 return -1;
Victor Stinner81c41db2015-04-02 11:50:57 +0200888 }
Victor Stinner31bf2d52015-04-01 21:57:09 +0200889
Victor Stinner81c41db2015-04-02 11:50:57 +0200890 /* retry sock_func() */
Victor Stinner31bf2d52015-04-01 21:57:09 +0200891 }
892
893 if (s->sock_timeout > 0
894 && (CHECK_ERRNO(EWOULDBLOCK) || CHECK_ERRNO(EAGAIN))) {
Victor Stinner81c41db2015-04-02 11:50:57 +0200895 /* False positive: sock_func() failed with EWOULDBLOCK or EAGAIN.
Victor Stinner31bf2d52015-04-01 21:57:09 +0200896
897 For example, select() could indicate a socket is ready for
898 reading, but the data then discarded by the OS because of a
899 wrong checksum.
900
901 Loop on select() to recheck for socket readyness. */
902 continue;
903 }
904
Victor Stinner81c41db2015-04-02 11:50:57 +0200905 /* sock_func() failed */
906 if (!err)
907 s->errorhandler();
908 /* else: err was already set before */
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000909 return -1;
910 }
Victor Stinner31bf2d52015-04-01 21:57:09 +0200911}
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000912
Victor Stinner81c41db2015-04-02 11:50:57 +0200913static int
914sock_call(PySocketSockObject *s,
915 int writing,
916 int (*func) (PySocketSockObject *s, void *data),
917 void *data)
918{
Victor Stinner8912d142015-04-06 23:16:34 +0200919 return sock_call_ex(s, writing, func, data, 0, NULL, s->sock_timeout);
Victor Stinner81c41db2015-04-02 11:50:57 +0200920}
921
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000922
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000923/* Initialize a new socket object. */
924
Victor Stinner88ed6402015-04-09 10:23:12 +0200925/* Default timeout for new sockets */
926static _PyTime_t defaulttimeout = _PYTIME_FROMSECONDS(-1);
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000927
Christian Heimes0b9d64e2016-09-09 00:28:57 +0200928static int
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000929init_sockobject(PySocketSockObject *s,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 SOCKET_T fd, int family, int type, int proto)
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000931{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 s->sock_fd = fd;
933 s->sock_family = family;
934 s->sock_type = type;
935 s->sock_proto = proto;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 s->errorhandler = &set_error;
Antoine Pitroub1c54962010-10-14 15:05:38 +0000938#ifdef SOCK_NONBLOCK
939 if (type & SOCK_NONBLOCK)
Victor Stinner71694d52015-03-28 01:18:54 +0100940 s->sock_timeout = 0;
Antoine Pitroub1c54962010-10-14 15:05:38 +0000941 else
942#endif
943 {
944 s->sock_timeout = defaulttimeout;
Christian Heimes0b9d64e2016-09-09 00:28:57 +0200945 if (defaulttimeout >= 0) {
946 if (internal_setblocking(s, 0) == -1) {
947 return -1;
948 }
949 }
Antoine Pitroub1c54962010-10-14 15:05:38 +0000950 }
Christian Heimes0b9d64e2016-09-09 00:28:57 +0200951 return 0;
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000952}
953
954
Guido van Rossum30a685f1991-06-27 15:51:29 +0000955/* Create a new socket object.
956 This just creates the object and initializes it.
957 If the creation fails, return NULL and set an exception (implicit
958 in NEWOBJ()). */
959
Guido van Rossum73624e91994-10-10 17:59:00 +0000960static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000961new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 PySocketSockObject *s;
964 s = (PySocketSockObject *)
965 PyType_GenericNew(&sock_type, NULL, NULL);
Christian Heimes0b9d64e2016-09-09 00:28:57 +0200966 if (s == NULL)
967 return NULL;
968 if (init_sockobject(s, fd, family, type, proto) == -1) {
969 Py_DECREF(s);
970 return NULL;
971 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 return s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000973}
974
Guido van Rossum30a685f1991-06-27 15:51:29 +0000975
Guido van Rossum48a680c2001-03-02 06:34:14 +0000976/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000977 thread to be in gethostbyname or getaddrinfo */
978#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Matthias Klosed182a6c2012-08-14 17:29:04 +0200979static PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000980#endif
981
982
Guido van Rossum30a685f1991-06-27 15:51:29 +0000983/* Convert a string specifying a host name or one of a few symbolic
984 names to a numeric IP address. This usually calls gethostbyname()
985 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000986 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000987 an error occurred; then an exception is raised. */
988
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000989static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200990setipaddr(const char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000991{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 struct addrinfo hints, *res;
993 int error;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
996 if (name[0] == '\0') {
997 int siz;
998 memset(&hints, 0, sizeof(hints));
999 hints.ai_family = af;
1000 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
1001 hints.ai_flags = AI_PASSIVE;
1002 Py_BEGIN_ALLOW_THREADS
1003 ACQUIRE_GETADDRINFO_LOCK
1004 error = getaddrinfo(NULL, "0", &hints, &res);
1005 Py_END_ALLOW_THREADS
1006 /* We assume that those thread-unsafe getaddrinfo() versions
1007 *are* safe regarding their return value, ie. that a
1008 subsequent call to getaddrinfo() does not destroy the
1009 outcome of the first call. */
1010 RELEASE_GETADDRINFO_LOCK
1011 if (error) {
1012 set_gaierror(error);
1013 return -1;
1014 }
1015 switch (res->ai_family) {
1016 case AF_INET:
1017 siz = 4;
1018 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001019#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 case AF_INET6:
1021 siz = 16;
1022 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001023#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 default:
1025 freeaddrinfo(res);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001026 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 "unsupported address family");
1028 return -1;
1029 }
1030 if (res->ai_next) {
1031 freeaddrinfo(res);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001032 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 "wildcard resolved to multiple address");
1034 return -1;
1035 }
1036 if (res->ai_addrlen < addr_ret_size)
1037 addr_ret_size = res->ai_addrlen;
1038 memcpy(addr_ret, res->ai_addr, addr_ret_size);
1039 freeaddrinfo(res);
1040 return siz;
1041 }
Charles-François Natali0cc86852013-09-13 19:53:08 +02001042 /* special-case broadcast - inet_addr() below can return INADDR_NONE for
Victor Stinnere990c6e2013-11-16 00:18:58 +01001043 * this */
Charles-François Natali0cc86852013-09-13 19:53:08 +02001044 if (strcmp(name, "255.255.255.255") == 0 ||
1045 strcmp(name, "<broadcast>") == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 struct sockaddr_in *sin;
1047 if (af != AF_INET && af != AF_UNSPEC) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001048 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 "address family mismatched");
1050 return -1;
1051 }
1052 sin = (struct sockaddr_in *)addr_ret;
1053 memset((void *) sin, '\0', sizeof(*sin));
1054 sin->sin_family = AF_INET;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001055#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 sin->sin_len = sizeof(*sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001057#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 sin->sin_addr.s_addr = INADDR_BROADCAST;
1059 return sizeof(sin->sin_addr);
1060 }
Charles-François Natali0cc86852013-09-13 19:53:08 +02001061
1062 /* avoid a name resolution in case of numeric address */
1063#ifdef HAVE_INET_PTON
1064 /* check for an IPv4 address */
1065 if (af == AF_UNSPEC || af == AF_INET) {
1066 struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret;
1067 memset(sin, 0, sizeof(*sin));
1068 if (inet_pton(AF_INET, name, &sin->sin_addr) > 0) {
1069 sin->sin_family = AF_INET;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +00001070#ifdef HAVE_SOCKADDR_SA_LEN
Charles-François Natali0cc86852013-09-13 19:53:08 +02001071 sin->sin_len = sizeof(*sin);
Anthony Baxter0e85f9d2003-05-02 15:40:46 +00001072#endif
Charles-François Natali0cc86852013-09-13 19:53:08 +02001073 return 4;
1074 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 }
Charles-François Natali0cc86852013-09-13 19:53:08 +02001076#ifdef ENABLE_IPV6
1077 /* check for an IPv6 address - if the address contains a scope ID, we
1078 * fallback to getaddrinfo(), which can handle translation from interface
1079 * name to interface index */
1080 if ((af == AF_UNSPEC || af == AF_INET6) && !strchr(name, '%')) {
1081 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)addr_ret;
1082 memset(sin, 0, sizeof(*sin));
1083 if (inet_pton(AF_INET6, name, &sin->sin6_addr) > 0) {
1084 sin->sin6_family = AF_INET6;
1085#ifdef HAVE_SOCKADDR_SA_LEN
1086 sin->sin6_len = sizeof(*sin);
1087#endif
1088 return 16;
1089 }
1090 }
1091#endif /* ENABLE_IPV6 */
1092#else /* HAVE_INET_PTON */
1093 /* check for an IPv4 address */
1094 if (af == AF_INET || af == AF_UNSPEC) {
1095 struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret;
1096 memset(sin, 0, sizeof(*sin));
1097 if ((sin->sin_addr.s_addr = inet_addr(name)) != INADDR_NONE) {
1098 sin->sin_family = AF_INET;
1099#ifdef HAVE_SOCKADDR_SA_LEN
1100 sin->sin_len = sizeof(*sin);
1101#endif
1102 return 4;
1103 }
Victor Stinnere990c6e2013-11-16 00:18:58 +01001104 }
Charles-François Natali0cc86852013-09-13 19:53:08 +02001105#endif /* HAVE_INET_PTON */
1106
1107 /* perform a name resolution */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 memset(&hints, 0, sizeof(hints));
1109 hints.ai_family = af;
1110 Py_BEGIN_ALLOW_THREADS
1111 ACQUIRE_GETADDRINFO_LOCK
1112 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +00001113#if defined(__digital__) && defined(__unix__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 if (error == EAI_NONAME && af == AF_UNSPEC) {
1115 /* On Tru64 V5.1, numeric-to-addr conversion fails
1116 if no address family is given. Assume IPv4 for now.*/
1117 hints.ai_family = AF_INET;
1118 error = getaddrinfo(name, NULL, &hints, &res);
1119 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +00001120#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 Py_END_ALLOW_THREADS
1122 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
1123 if (error) {
1124 set_gaierror(error);
1125 return -1;
1126 }
1127 if (res->ai_addrlen < addr_ret_size)
1128 addr_ret_size = res->ai_addrlen;
1129 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
1130 freeaddrinfo(res);
1131 switch (addr_ret->sa_family) {
1132 case AF_INET:
1133 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001134#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 case AF_INET6:
1136 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +00001137#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001139 PyErr_SetString(PyExc_OSError, "unknown address family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 return -1;
1141 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001142}
1143
Guido van Rossum30a685f1991-06-27 15:51:29 +00001144
Guido van Rossum30a685f1991-06-27 15:51:29 +00001145/* Create a string object representing an IP address.
1146 This is always a string of the form 'dd.dd.dd.dd' (with variable
1147 size numbers). */
1148
Guido van Rossum73624e91994-10-10 17:59:00 +00001149static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001150makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 char buf[NI_MAXHOST];
1153 int error;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
1156 NI_NUMERICHOST);
1157 if (error) {
1158 set_gaierror(error);
1159 return NULL;
1160 }
1161 return PyUnicode_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +00001162}
1163
1164
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001165#ifdef USE_BLUETOOTH
1166/* Convert a string representation of a Bluetooth address into a numeric
1167 address. Returns the length (6), or raises an exception and returns -1 if
1168 an error occurred. */
1169
1170static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001171setbdaddr(const char *name, bdaddr_t *bdaddr)
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 unsigned int b0, b1, b2, b3, b4, b5;
1174 char ch;
1175 int n;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
1178 &b5, &b4, &b3, &b2, &b1, &b0, &ch);
1179 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
1180 bdaddr->b[0] = b0;
1181 bdaddr->b[1] = b1;
1182 bdaddr->b[2] = b2;
1183 bdaddr->b[3] = b3;
1184 bdaddr->b[4] = b4;
1185 bdaddr->b[5] = b5;
1186 return 6;
1187 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001188 PyErr_SetString(PyExc_OSError, "bad bluetooth address");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 return -1;
1190 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001191}
1192
1193/* Create a string representation of the Bluetooth address. This is always a
1194 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
1195 value (zero padded if necessary). */
1196
1197static PyObject *
1198makebdaddr(bdaddr_t *bdaddr)
1199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 char buf[(6 * 2) + 5 + 1];
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
1203 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
1204 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
1205 return PyUnicode_FromString(buf);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001206}
1207#endif
1208
1209
Guido van Rossum30a685f1991-06-27 15:51:29 +00001210/* Create an object representing the given socket address,
1211 suitable for passing it back to bind(), connect() etc.
1212 The family field of the sockaddr structure is inspected
1213 to determine what kind of address it really is. */
1214
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001215/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001216static PyObject *
Antoine Pitrou19467d22010-08-17 19:33:30 +00001217makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 if (addrlen == 0) {
1220 /* No address -- may be recvfrom() from known socket */
1221 Py_INCREF(Py_None);
1222 return Py_None;
1223 }
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 switch (addr->sa_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +00001226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 case AF_INET:
1228 {
1229 struct sockaddr_in *a;
1230 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
1231 PyObject *ret = NULL;
1232 if (addrobj) {
1233 a = (struct sockaddr_in *)addr;
1234 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
1235 Py_DECREF(addrobj);
1236 }
1237 return ret;
1238 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00001239
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001240#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 case AF_UNIX:
1242 {
1243 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Benjamin Petersonb6c34582016-09-07 14:08:34 -07001244#ifdef __linux__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 if (a->sun_path[0] == 0) { /* Linux abstract namespace */
1246 addrlen -= offsetof(struct sockaddr_un, sun_path);
Charles-François Natalib10c71d2013-05-21 10:45:46 +02001247 return PyBytes_FromStringAndSize(a->sun_path, addrlen);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 }
1249 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001250#endif /* linux */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 {
1252 /* regular NULL-terminated string */
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001253 return PyUnicode_DecodeFSDefault(a->sun_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 }
1255 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001256#endif /* AF_UNIX */
1257
Martin v. Löwis11017b12006-01-14 18:12:57 +00001258#if defined(AF_NETLINK)
1259 case AF_NETLINK:
1260 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
1262 return Py_BuildValue("II", a->nl_pid, a->nl_groups);
Martin v. Löwis11017b12006-01-14 18:12:57 +00001263 }
1264#endif /* AF_NETLINK */
1265
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001266#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 case AF_INET6:
1268 {
1269 struct sockaddr_in6 *a;
1270 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
1271 PyObject *ret = NULL;
1272 if (addrobj) {
1273 a = (struct sockaddr_in6 *)addr;
Charles-François Natali366999a2012-01-02 15:47:29 +01001274 ret = Py_BuildValue("OiII",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 addrobj,
1276 ntohs(a->sin6_port),
Charles-François Natali366999a2012-01-02 15:47:29 +01001277 ntohl(a->sin6_flowinfo),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 a->sin6_scope_id);
1279 Py_DECREF(addrobj);
1280 }
1281 return ret;
1282 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001283#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001284
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001285#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 case AF_BLUETOOTH:
1287 switch (proto) {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 case BTPROTO_L2CAP:
1290 {
1291 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
1292 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
1293 PyObject *ret = NULL;
1294 if (addrobj) {
1295 ret = Py_BuildValue("Oi",
1296 addrobj,
1297 _BT_L2_MEMB(a, psm));
1298 Py_DECREF(addrobj);
1299 }
1300 return ret;
1301 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 case BTPROTO_RFCOMM:
1304 {
1305 struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
1306 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
1307 PyObject *ret = NULL;
1308 if (addrobj) {
1309 ret = Py_BuildValue("Oi",
1310 addrobj,
1311 _BT_RC_MEMB(a, channel));
1312 Py_DECREF(addrobj);
1313 }
1314 return ret;
1315 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 case BTPROTO_HCI:
1318 {
1319 struct sockaddr_hci *a = (struct sockaddr_hci *) addr;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001320#if defined(__NetBSD__) || defined(__DragonFly__)
1321 return makebdaddr(&_BT_HCI_MEMB(a, bdaddr));
1322#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 PyObject *ret = NULL;
1324 ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
1325 return ret;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001326#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 }
Thomas Wouterscf297e42007-02-23 15:07:44 +00001328
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001329#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 case BTPROTO_SCO:
1331 {
1332 struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
1333 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
1334 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001335#endif
1336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 default:
1338 PyErr_SetString(PyExc_ValueError,
1339 "Unknown Bluetooth protocol");
1340 return NULL;
1341 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001342#endif
1343
Antoine Pitroub156a462010-10-27 20:13:57 +00001344#if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 case AF_PACKET:
1346 {
1347 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
1348 char *ifname = "";
1349 struct ifreq ifr;
1350 /* need to look up interface name give index */
1351 if (a->sll_ifindex) {
1352 ifr.ifr_ifindex = a->sll_ifindex;
1353 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
1354 ifname = ifr.ifr_name;
1355 }
1356 return Py_BuildValue("shbhy#",
1357 ifname,
1358 ntohs(a->sll_protocol),
1359 a->sll_pkttype,
1360 a->sll_hatype,
1361 a->sll_addr,
1362 a->sll_halen);
1363 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001364#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001365
Christian Heimes043d6f62008-01-07 17:19:16 +00001366#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 case AF_TIPC:
1368 {
1369 struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr;
1370 if (a->addrtype == TIPC_ADDR_NAMESEQ) {
1371 return Py_BuildValue("IIIII",
1372 a->addrtype,
1373 a->addr.nameseq.type,
1374 a->addr.nameseq.lower,
1375 a->addr.nameseq.upper,
1376 a->scope);
1377 } else if (a->addrtype == TIPC_ADDR_NAME) {
1378 return Py_BuildValue("IIIII",
1379 a->addrtype,
1380 a->addr.name.name.type,
1381 a->addr.name.name.instance,
1382 a->addr.name.name.instance,
1383 a->scope);
1384 } else if (a->addrtype == TIPC_ADDR_ID) {
1385 return Py_BuildValue("IIIII",
1386 a->addrtype,
1387 a->addr.id.node,
1388 a->addr.id.ref,
1389 0,
1390 a->scope);
1391 } else {
1392 PyErr_SetString(PyExc_ValueError,
1393 "Invalid address type");
1394 return NULL;
1395 }
1396 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001397#endif
1398
Vinay Sajipecfc98c2014-03-20 12:42:42 +00001399#ifdef AF_CAN
Charles-François Natali47413c12011-10-06 19:47:44 +02001400 case AF_CAN:
1401 {
1402 struct sockaddr_can *a = (struct sockaddr_can *)addr;
1403 char *ifname = "";
1404 struct ifreq ifr;
1405 /* need to look up interface name given index */
1406 if (a->can_ifindex) {
1407 ifr.ifr_ifindex = a->can_ifindex;
1408 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
1409 ifname = ifr.ifr_name;
1410 }
1411
1412 return Py_BuildValue("O&h", PyUnicode_DecodeFSDefault,
1413 ifname,
1414 a->can_family);
1415 }
1416#endif
1417
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001418#ifdef PF_SYSTEM
1419 case PF_SYSTEM:
1420 switch(proto) {
1421#ifdef SYSPROTO_CONTROL
1422 case SYSPROTO_CONTROL:
1423 {
1424 struct sockaddr_ctl *a = (struct sockaddr_ctl *)addr;
1425 return Py_BuildValue("(II)", a->sc_id, a->sc_unit);
1426 }
1427#endif
1428 default:
1429 PyErr_SetString(PyExc_ValueError,
1430 "Invalid address type");
1431 return 0;
1432 }
1433#endif
1434
Christian Heimesdffa3942016-09-05 23:54:41 +02001435#ifdef HAVE_SOCKADDR_ALG
1436 case AF_ALG:
1437 {
1438 struct sockaddr_alg *a = (struct sockaddr_alg *)addr;
1439 return Py_BuildValue("s#s#HH",
1440 a->salg_type,
1441 strnlen((const char*)a->salg_type,
1442 sizeof(a->salg_type)),
1443 a->salg_name,
1444 strnlen((const char*)a->salg_name,
1445 sizeof(a->salg_name)),
1446 a->salg_feat,
1447 a->salg_mask);
1448 }
1449#endif
1450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 default:
1454 /* If we don't know the address family, don't raise an
1455 exception -- return it as an (int, bytes) tuple. */
1456 return Py_BuildValue("iy#",
1457 addr->sa_family,
1458 addr->sa_data,
1459 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001462}
1463
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001464/* Helper for getsockaddrarg: bypass IDNA for ASCII-only host names
1465 (in particular, numeric IP addresses). */
1466struct maybe_idna {
1467 PyObject *obj;
1468 char *buf;
1469};
1470
1471static void
1472idna_cleanup(struct maybe_idna *data)
1473{
1474 Py_CLEAR(data->obj);
1475}
1476
1477static int
1478idna_converter(PyObject *obj, struct maybe_idna *data)
1479{
1480 size_t len;
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001481 PyObject *obj2;
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001482 if (obj == NULL) {
1483 idna_cleanup(data);
1484 return 1;
1485 }
1486 data->obj = NULL;
1487 len = -1;
1488 if (PyBytes_Check(obj)) {
1489 data->buf = PyBytes_AsString(obj);
1490 len = PyBytes_Size(obj);
1491 }
1492 else if (PyByteArray_Check(obj)) {
1493 data->buf = PyByteArray_AsString(obj);
1494 len = PyByteArray_Size(obj);
1495 }
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001496 else if (PyUnicode_Check(obj)) {
1497 if (PyUnicode_READY(obj) == 0 && PyUnicode_IS_COMPACT_ASCII(obj)) {
1498 data->buf = PyUnicode_DATA(obj);
1499 len = PyUnicode_GET_LENGTH(obj);
1500 }
1501 else {
1502 obj2 = PyUnicode_AsEncodedString(obj, "idna", NULL);
1503 if (!obj2) {
1504 PyErr_SetString(PyExc_TypeError, "encoding of hostname failed");
1505 return 0;
1506 }
1507 assert(PyBytes_Check(obj2));
1508 data->obj = obj2;
1509 data->buf = PyBytes_AS_STRING(obj2);
1510 len = PyBytes_GET_SIZE(obj2);
1511 }
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001512 }
1513 else {
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001514 PyErr_Format(PyExc_TypeError, "str, bytes or bytearray expected, not %s",
1515 obj->ob_type->tp_name);
1516 return 0;
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001517 }
1518 if (strlen(data->buf) != len) {
1519 Py_CLEAR(data->obj);
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001520 PyErr_SetString(PyExc_TypeError, "host name must not contain null character");
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001521 return 0;
1522 }
1523 return Py_CLEANUP_SUPPORTED;
Victor Stinner1a62a682014-08-17 19:33:28 +02001524}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001525
1526/* Parse a socket address argument according to the socket object's
1527 address family. Return 1 if the address was in the proper format,
1528 0 of not. The address is returned through addr_ret, its length
1529 through len_ret. */
1530
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001531static int
Guido van Rossum48a680c2001-03-02 06:34:14 +00001532getsockaddrarg(PySocketSockObject *s, PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 struct sockaddr *addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 switch (s->sock_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +00001536
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001537#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 case AF_UNIX:
1539 {
1540 struct sockaddr_un* addr;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001541 Py_buffer path;
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001542 int retval = 0;
1543
1544 /* PEP 383. Not using PyUnicode_FSConverter since we need to
1545 allow embedded nulls on Linux. */
1546 if (PyUnicode_Check(args)) {
1547 if ((args = PyUnicode_EncodeFSDefault(args)) == NULL)
1548 return 0;
1549 }
1550 else
1551 Py_INCREF(args);
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001552 if (!PyArg_Parse(args, "y*", &path)) {
1553 Py_DECREF(args);
1554 return retval;
1555 }
1556 assert(path.len >= 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 addr = (struct sockaddr_un*)addr_ret;
Benjamin Petersonb6c34582016-09-07 14:08:34 -07001559#ifdef __linux__
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001560 if (path.len > 0 && *(const char *)path.buf == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 /* Linux abstract namespace extension */
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001562 if ((size_t)path.len > sizeof addr->sun_path) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001563 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 "AF_UNIX path too long");
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001565 goto unix_out;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 }
1567 }
1568 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001569#endif /* linux */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 {
1571 /* regular NULL-terminated string */
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001572 if ((size_t)path.len >= sizeof addr->sun_path) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001573 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 "AF_UNIX path too long");
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001575 goto unix_out;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 }
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001577 addr->sun_path[path.len] = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 }
1579 addr->sun_family = s->sock_family;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001580 memcpy(addr->sun_path, path.buf, path.len);
1581 *len_ret = path.len + offsetof(struct sockaddr_un, sun_path);
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001582 retval = 1;
1583 unix_out:
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001584 PyBuffer_Release(&path);
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001585 Py_DECREF(args);
1586 return retval;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001588#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001589
Martin v. Löwis11017b12006-01-14 18:12:57 +00001590#if defined(AF_NETLINK)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 case AF_NETLINK:
1592 {
1593 struct sockaddr_nl* addr;
1594 int pid, groups;
1595 addr = (struct sockaddr_nl *)addr_ret;
1596 if (!PyTuple_Check(args)) {
1597 PyErr_Format(
1598 PyExc_TypeError,
1599 "getsockaddrarg: "
1600 "AF_NETLINK address must be tuple, not %.500s",
1601 Py_TYPE(args)->tp_name);
1602 return 0;
1603 }
1604 if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
1605 return 0;
1606 addr->nl_family = AF_NETLINK;
1607 addr->nl_pid = pid;
1608 addr->nl_groups = groups;
1609 *len_ret = sizeof(*addr);
1610 return 1;
1611 }
Martin v. Löwis11017b12006-01-14 18:12:57 +00001612#endif
1613
Charles-François Natali10b8cf42011-11-10 19:21:37 +01001614#ifdef AF_RDS
1615 case AF_RDS:
1616 /* RDS sockets use sockaddr_in: fall-through */
1617#endif
1618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 case AF_INET:
1620 {
1621 struct sockaddr_in* addr;
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001622 struct maybe_idna host = {NULL, NULL};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 int port, result;
1624 if (!PyTuple_Check(args)) {
1625 PyErr_Format(
1626 PyExc_TypeError,
1627 "getsockaddrarg: "
1628 "AF_INET address must be tuple, not %.500s",
1629 Py_TYPE(args)->tp_name);
1630 return 0;
1631 }
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001632 if (!PyArg_ParseTuple(args, "O&i:getsockaddrarg",
1633 idna_converter, &host, &port))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 return 0;
1635 addr=(struct sockaddr_in*)addr_ret;
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001636 result = setipaddr(host.buf, (struct sockaddr *)addr,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 sizeof(*addr), AF_INET);
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001638 idna_cleanup(&host);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 if (result < 0)
1640 return 0;
1641 if (port < 0 || port > 0xffff) {
1642 PyErr_SetString(
1643 PyExc_OverflowError,
1644 "getsockaddrarg: port must be 0-65535.");
1645 return 0;
1646 }
1647 addr->sin_family = AF_INET;
1648 addr->sin_port = htons((short)port);
1649 *len_ret = sizeof *addr;
1650 return 1;
1651 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001652
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001653#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 case AF_INET6:
1655 {
1656 struct sockaddr_in6* addr;
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001657 struct maybe_idna host = {NULL, NULL};
Charles-François Natali366999a2012-01-02 15:47:29 +01001658 int port, result;
1659 unsigned int flowinfo, scope_id;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 flowinfo = scope_id = 0;
1661 if (!PyTuple_Check(args)) {
1662 PyErr_Format(
1663 PyExc_TypeError,
1664 "getsockaddrarg: "
1665 "AF_INET6 address must be tuple, not %.500s",
1666 Py_TYPE(args)->tp_name);
1667 return 0;
1668 }
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001669 if (!PyArg_ParseTuple(args, "O&i|II",
1670 idna_converter, &host, &port, &flowinfo,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 &scope_id)) {
1672 return 0;
1673 }
1674 addr = (struct sockaddr_in6*)addr_ret;
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001675 result = setipaddr(host.buf, (struct sockaddr *)addr,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 sizeof(*addr), AF_INET6);
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001677 idna_cleanup(&host);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 if (result < 0)
1679 return 0;
1680 if (port < 0 || port > 0xffff) {
1681 PyErr_SetString(
1682 PyExc_OverflowError,
1683 "getsockaddrarg: port must be 0-65535.");
1684 return 0;
1685 }
Charles-François Natali9b0c0062012-06-23 10:17:05 +02001686 if (flowinfo > 0xfffff) {
Charles-François Natali366999a2012-01-02 15:47:29 +01001687 PyErr_SetString(
1688 PyExc_OverflowError,
1689 "getsockaddrarg: flowinfo must be 0-1048575.");
1690 return 0;
1691 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 addr->sin6_family = s->sock_family;
1693 addr->sin6_port = htons((short)port);
Charles-François Natali366999a2012-01-02 15:47:29 +01001694 addr->sin6_flowinfo = htonl(flowinfo);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 addr->sin6_scope_id = scope_id;
1696 *len_ret = sizeof *addr;
1697 return 1;
1698 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001699#endif
1700
Hye-Shik Chang81268602004-02-02 06:05:24 +00001701#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 case AF_BLUETOOTH:
1703 {
1704 switch (s->sock_proto) {
1705 case BTPROTO_L2CAP:
1706 {
1707 struct sockaddr_l2 *addr;
1708 char *straddr;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 addr = (struct sockaddr_l2 *)addr_ret;
1711 memset(addr, 0, sizeof(struct sockaddr_l2));
1712 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
1713 if (!PyArg_ParseTuple(args, "si", &straddr,
1714 &_BT_L2_MEMB(addr, psm))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001715 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 "wrong format");
1717 return 0;
1718 }
1719 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
1720 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 *len_ret = sizeof *addr;
1723 return 1;
1724 }
1725 case BTPROTO_RFCOMM:
1726 {
1727 struct sockaddr_rc *addr;
1728 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 addr = (struct sockaddr_rc *)addr_ret;
1731 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
1732 if (!PyArg_ParseTuple(args, "si", &straddr,
1733 &_BT_RC_MEMB(addr, channel))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001734 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 "wrong format");
1736 return 0;
1737 }
1738 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
1739 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 *len_ret = sizeof *addr;
1742 return 1;
1743 }
1744 case BTPROTO_HCI:
1745 {
1746 struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001747#if defined(__NetBSD__) || defined(__DragonFly__)
Alexander Belopolskye239d232010-12-08 23:31:48 +00001748 char *straddr = PyBytes_AS_STRING(args);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001749
Alexander Belopolskye239d232010-12-08 23:31:48 +00001750 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001751 if (straddr == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001752 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001753 "wrong format");
1754 return 0;
1755 }
1756 if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0)
1757 return 0;
1758#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
1760 if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001761 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 "wrong format");
1763 return 0;
1764 }
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001765#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 *len_ret = sizeof *addr;
1767 return 1;
1768 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001769#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 case BTPROTO_SCO:
1771 {
1772 struct sockaddr_sco *addr;
1773 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 addr = (struct sockaddr_sco *)addr_ret;
1776 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
1777 if (!PyBytes_Check(args)) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001778 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 "wrong format");
1780 return 0;
1781 }
1782 straddr = PyBytes_AS_STRING(args);
1783 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
1784 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 *len_ret = sizeof *addr;
1787 return 1;
1788 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001789#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001791 PyErr_SetString(PyExc_OSError, "getsockaddrarg: unknown Bluetooth protocol");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 return 0;
1793 }
1794 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001795#endif
1796
Antoine Pitroub156a462010-10-27 20:13:57 +00001797#if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 case AF_PACKET:
1799 {
1800 struct sockaddr_ll* addr;
1801 struct ifreq ifr;
1802 char *interfaceName;
1803 int protoNumber;
1804 int hatype = 0;
1805 int pkttype = 0;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001806 Py_buffer haddr = {NULL, NULL};
Guido van Rossum48a680c2001-03-02 06:34:14 +00001807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 if (!PyTuple_Check(args)) {
1809 PyErr_Format(
1810 PyExc_TypeError,
1811 "getsockaddrarg: "
1812 "AF_PACKET address must be tuple, not %.500s",
1813 Py_TYPE(args)->tp_name);
1814 return 0;
1815 }
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001816 if (!PyArg_ParseTuple(args, "si|iiy*", &interfaceName,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 &protoNumber, &pkttype, &hatype,
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001818 &haddr))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 return 0;
1820 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1821 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
1822 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
1823 s->errorhandler();
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001824 PyBuffer_Release(&haddr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 return 0;
1826 }
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001827 if (haddr.buf && haddr.len > 8) {
1828 PyErr_SetString(PyExc_ValueError,
1829 "Hardware address must be 8 bytes or less");
1830 PyBuffer_Release(&haddr);
1831 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 }
1833 if (protoNumber < 0 || protoNumber > 0xffff) {
1834 PyErr_SetString(
1835 PyExc_OverflowError,
1836 "getsockaddrarg: protoNumber must be 0-65535.");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001837 PyBuffer_Release(&haddr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 return 0;
1839 }
1840 addr = (struct sockaddr_ll*)addr_ret;
1841 addr->sll_family = AF_PACKET;
1842 addr->sll_protocol = htons((short)protoNumber);
1843 addr->sll_ifindex = ifr.ifr_ifindex;
1844 addr->sll_pkttype = pkttype;
1845 addr->sll_hatype = hatype;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001846 if (haddr.buf) {
1847 memcpy(&addr->sll_addr, haddr.buf, haddr.len);
1848 addr->sll_halen = haddr.len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 }
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001850 else
1851 addr->sll_halen = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 *len_ret = sizeof *addr;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001853 PyBuffer_Release(&haddr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 return 1;
1855 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001856#endif
1857
Christian Heimes043d6f62008-01-07 17:19:16 +00001858#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 case AF_TIPC:
1860 {
1861 unsigned int atype, v1, v2, v3;
1862 unsigned int scope = TIPC_CLUSTER_SCOPE;
1863 struct sockaddr_tipc *addr;
Christian Heimes043d6f62008-01-07 17:19:16 +00001864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 if (!PyTuple_Check(args)) {
1866 PyErr_Format(
1867 PyExc_TypeError,
1868 "getsockaddrarg: "
1869 "AF_TIPC address must be tuple, not %.500s",
1870 Py_TYPE(args)->tp_name);
1871 return 0;
1872 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 if (!PyArg_ParseTuple(args,
1875 "IIII|I;Invalid TIPC address format",
1876 &atype, &v1, &v2, &v3, &scope))
1877 return 0;
Christian Heimes043d6f62008-01-07 17:19:16 +00001878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 addr = (struct sockaddr_tipc *) addr_ret;
1880 memset(addr, 0, sizeof(struct sockaddr_tipc));
Christian Heimes043d6f62008-01-07 17:19:16 +00001881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 addr->family = AF_TIPC;
1883 addr->scope = scope;
1884 addr->addrtype = atype;
Christian Heimes043d6f62008-01-07 17:19:16 +00001885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 if (atype == TIPC_ADDR_NAMESEQ) {
1887 addr->addr.nameseq.type = v1;
1888 addr->addr.nameseq.lower = v2;
1889 addr->addr.nameseq.upper = v3;
1890 } else if (atype == TIPC_ADDR_NAME) {
1891 addr->addr.name.name.type = v1;
1892 addr->addr.name.name.instance = v2;
1893 } else if (atype == TIPC_ADDR_ID) {
1894 addr->addr.id.node = v1;
1895 addr->addr.id.ref = v2;
1896 } else {
1897 /* Shouldn't happen */
1898 PyErr_SetString(PyExc_TypeError, "Invalid address type");
1899 return 0;
1900 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 *len_ret = sizeof(*addr);
Christian Heimes043d6f62008-01-07 17:19:16 +00001903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 return 1;
1905 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001906#endif
1907
Vinay Sajiped6783f2014-03-21 11:44:32 +00001908#if defined(AF_CAN) && defined(CAN_RAW) && defined(CAN_BCM)
Charles-François Natali47413c12011-10-06 19:47:44 +02001909 case AF_CAN:
1910 switch (s->sock_proto) {
1911 case CAN_RAW:
Charles-François Natali773e42d2013-02-05 19:42:01 +01001912 /* fall-through */
1913 case CAN_BCM:
Charles-François Natali47413c12011-10-06 19:47:44 +02001914 {
1915 struct sockaddr_can *addr;
1916 PyObject *interfaceName;
1917 struct ifreq ifr;
Charles-François Natali47413c12011-10-06 19:47:44 +02001918 Py_ssize_t len;
1919
Benjamin Peterson18b71912013-05-16 15:29:44 -05001920 addr = (struct sockaddr_can *)addr_ret;
1921
Charles-François Natali47413c12011-10-06 19:47:44 +02001922 if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter,
1923 &interfaceName))
1924 return 0;
1925
1926 len = PyBytes_GET_SIZE(interfaceName);
1927
1928 if (len == 0) {
1929 ifr.ifr_ifindex = 0;
Victor Stinner1a62a682014-08-17 19:33:28 +02001930 } else if ((size_t)len < sizeof(ifr.ifr_name)) {
Christian Heimes15b68852012-09-10 01:25:50 +02001931 strncpy(ifr.ifr_name, PyBytes_AS_STRING(interfaceName), sizeof(ifr.ifr_name));
1932 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Charles-François Natali47413c12011-10-06 19:47:44 +02001933 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
1934 s->errorhandler();
1935 Py_DECREF(interfaceName);
1936 return 0;
1937 }
1938 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001939 PyErr_SetString(PyExc_OSError,
Charles-François Natali47413c12011-10-06 19:47:44 +02001940 "AF_CAN interface name too long");
1941 Py_DECREF(interfaceName);
1942 return 0;
1943 }
1944
1945 addr->can_family = AF_CAN;
1946 addr->can_ifindex = ifr.ifr_ifindex;
1947
1948 *len_ret = sizeof(*addr);
1949 Py_DECREF(interfaceName);
1950 return 1;
1951 }
1952 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001953 PyErr_SetString(PyExc_OSError,
Charles-François Natali47413c12011-10-06 19:47:44 +02001954 "getsockaddrarg: unsupported CAN protocol");
1955 return 0;
1956 }
1957#endif
Victor Stinnera534fc42013-06-03 22:07:27 +02001958
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001959#ifdef PF_SYSTEM
1960 case PF_SYSTEM:
1961 switch (s->sock_proto) {
1962#ifdef SYSPROTO_CONTROL
1963 case SYSPROTO_CONTROL:
1964 {
1965 struct sockaddr_ctl *addr;
Victor Stinnera534fc42013-06-03 22:07:27 +02001966
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001967 addr = (struct sockaddr_ctl *)addr_ret;
1968 addr->sc_family = AF_SYSTEM;
Victor Stinnera534fc42013-06-03 22:07:27 +02001969 addr->ss_sysaddr = AF_SYS_CONTROL;
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001970
1971 if (PyUnicode_Check(args)) {
1972 struct ctl_info info;
1973 PyObject *ctl_name;
1974
1975 if (!PyArg_Parse(args, "O&",
1976 PyUnicode_FSConverter, &ctl_name)) {
1977 return 0;
1978 }
1979
Victor Stinnerf50e1872015-03-20 11:32:24 +01001980 if (PyBytes_GET_SIZE(ctl_name) > (Py_ssize_t)sizeof(info.ctl_name)) {
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001981 PyErr_SetString(PyExc_ValueError,
1982 "provided string is too long");
1983 Py_DECREF(ctl_name);
1984 return 0;
1985 }
1986 strncpy(info.ctl_name, PyBytes_AS_STRING(ctl_name),
1987 sizeof(info.ctl_name));
1988 Py_DECREF(ctl_name);
1989
1990 if (ioctl(s->sock_fd, CTLIOCGINFO, &info)) {
1991 PyErr_SetString(PyExc_OSError,
1992 "cannot find kernel control with provided name");
1993 return 0;
1994 }
Victor Stinnera534fc42013-06-03 22:07:27 +02001995
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001996 addr->sc_id = info.ctl_id;
1997 addr->sc_unit = 0;
1998 } else if (!PyArg_ParseTuple(args, "II",
1999 &(addr->sc_id), &(addr->sc_unit))) {
2000 PyErr_SetString(PyExc_TypeError, "getsockaddrarg: "
2001 "expected str or tuple of two ints");
Victor Stinnera534fc42013-06-03 22:07:27 +02002002
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01002003 return 0;
2004 }
Victor Stinnera534fc42013-06-03 22:07:27 +02002005
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01002006 *len_ret = sizeof(*addr);
2007 return 1;
2008 }
2009#endif
2010 default:
2011 PyErr_SetString(PyExc_OSError,
2012 "getsockaddrarg: unsupported PF_SYSTEM protocol");
2013 return 0;
2014 }
2015#endif
Christian Heimesdffa3942016-09-05 23:54:41 +02002016#ifdef HAVE_SOCKADDR_ALG
2017 case AF_ALG:
2018 {
2019 struct sockaddr_alg *sa;
2020 char *type;
2021 char *name;
2022 sa = (struct sockaddr_alg *)addr_ret;
2023
2024 memset(sa, 0, sizeof(*sa));
2025 sa->salg_family = AF_ALG;
2026
2027 if (!PyArg_ParseTuple(args, "ss|HH:getsockaddrarg",
2028 &type, &name, &sa->salg_feat, &sa->salg_mask))
2029 return 0;
2030 /* sockaddr_alg has fixed-sized char arrays for type and name */
2031 if (strlen(type) > sizeof(sa->salg_type)) {
2032 PyErr_SetString(PyExc_ValueError, "AF_ALG type too long.");
2033 return 0;
2034 }
2035 strncpy((char *)sa->salg_type, type, sizeof(sa->salg_type));
2036 if (strlen(name) > sizeof(sa->salg_name)) {
2037 PyErr_SetString(PyExc_ValueError, "AF_ALG name too long.");
2038 return 0;
2039 }
2040 strncpy((char *)sa->salg_name, name, sizeof(sa->salg_name));
2041
2042 *len_ret = sizeof(*sa);
2043 return 1;
2044 }
2045#endif
Charles-François Natali47413c12011-10-06 19:47:44 +02002046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002050 PyErr_SetString(PyExc_OSError, "getsockaddrarg: bad family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 return 0;
Guido van Rossum30a685f1991-06-27 15:51:29 +00002052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002054}
2055
Guido van Rossum30a685f1991-06-27 15:51:29 +00002056
Guido van Rossum48a680c2001-03-02 06:34:14 +00002057/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00002058 Return 1 if the family is known, 0 otherwise. The length is returned
2059 through len_ret. */
2060
2061static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00002062getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00002063{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 switch (s->sock_family) {
Guido van Rossum710e1df1992-06-12 10:39:36 +00002065
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00002066#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 case AF_UNIX:
2068 {
2069 *len_ret = sizeof (struct sockaddr_un);
2070 return 1;
2071 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002072#endif /* AF_UNIX */
Martin Panter6d57fe12016-09-17 03:26:16 +00002073
Martin v. Löwis11017b12006-01-14 18:12:57 +00002074#if defined(AF_NETLINK)
Martin Panter6d57fe12016-09-17 03:26:16 +00002075 case AF_NETLINK:
2076 {
2077 *len_ret = sizeof (struct sockaddr_nl);
2078 return 1;
2079 }
Martin v. Löwis11017b12006-01-14 18:12:57 +00002080#endif
Guido van Rossum710e1df1992-06-12 10:39:36 +00002081
Charles-François Natali10b8cf42011-11-10 19:21:37 +01002082#ifdef AF_RDS
2083 case AF_RDS:
2084 /* RDS sockets use sockaddr_in: fall-through */
2085#endif
2086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 case AF_INET:
2088 {
2089 *len_ret = sizeof (struct sockaddr_in);
2090 return 1;
2091 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00002092
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002093#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 case AF_INET6:
2095 {
2096 *len_ret = sizeof (struct sockaddr_in6);
2097 return 1;
2098 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002099#endif
2100
Hye-Shik Chang81268602004-02-02 06:05:24 +00002101#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 case AF_BLUETOOTH:
2103 {
2104 switch(s->sock_proto)
2105 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00002106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 case BTPROTO_L2CAP:
2108 *len_ret = sizeof (struct sockaddr_l2);
2109 return 1;
2110 case BTPROTO_RFCOMM:
2111 *len_ret = sizeof (struct sockaddr_rc);
2112 return 1;
2113 case BTPROTO_HCI:
2114 *len_ret = sizeof (struct sockaddr_hci);
2115 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00002116#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 case BTPROTO_SCO:
2118 *len_ret = sizeof (struct sockaddr_sco);
2119 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00002120#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002122 PyErr_SetString(PyExc_OSError, "getsockaddrlen: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 "unknown BT protocol");
2124 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00002125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 }
2127 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00002128#endif
2129
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00002130#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 case AF_PACKET:
2132 {
2133 *len_ret = sizeof (struct sockaddr_ll);
2134 return 1;
2135 }
Jeremy Hylton22308652001-02-02 03:23:09 +00002136#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00002137
Christian Heimes043d6f62008-01-07 17:19:16 +00002138#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 case AF_TIPC:
2140 {
2141 *len_ret = sizeof (struct sockaddr_tipc);
2142 return 1;
2143 }
Christian Heimes043d6f62008-01-07 17:19:16 +00002144#endif
2145
Vinay Sajipecfc98c2014-03-20 12:42:42 +00002146#ifdef AF_CAN
Charles-François Natali47413c12011-10-06 19:47:44 +02002147 case AF_CAN:
2148 {
2149 *len_ret = sizeof (struct sockaddr_can);
2150 return 1;
2151 }
2152#endif
Victor Stinnera534fc42013-06-03 22:07:27 +02002153
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01002154#ifdef PF_SYSTEM
2155 case PF_SYSTEM:
2156 switch(s->sock_proto) {
2157#ifdef SYSPROTO_CONTROL
2158 case SYSPROTO_CONTROL:
2159 *len_ret = sizeof (struct sockaddr_ctl);
2160 return 1;
2161#endif
2162 default:
2163 PyErr_SetString(PyExc_OSError, "getsockaddrlen: "
2164 "unknown PF_SYSTEM protocol");
2165 return 0;
2166 }
2167#endif
Christian Heimesdffa3942016-09-05 23:54:41 +02002168#ifdef HAVE_SOCKADDR_ALG
2169 case AF_ALG:
2170 {
2171 *len_ret = sizeof (struct sockaddr_alg);
2172 return 1;
2173 }
2174#endif
Charles-François Natali47413c12011-10-06 19:47:44 +02002175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176 /* More cases here... */
Guido van Rossum710e1df1992-06-12 10:39:36 +00002177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002179 PyErr_SetString(PyExc_OSError, "getsockaddrlen: bad family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 return 0;
Guido van Rossum710e1df1992-06-12 10:39:36 +00002181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00002183}
2184
2185
Nick Coghlan96fe56a2011-08-22 11:55:57 +10002186/* Support functions for the sendmsg() and recvmsg[_into]() methods.
2187 Currently, these methods are only compiled if the RFC 2292/3542
2188 CMSG_LEN() macro is available. Older systems seem to have used
2189 sizeof(struct cmsghdr) + (length) where CMSG_LEN() is used now, so
2190 it may be possible to define CMSG_LEN() that way if it's not
2191 provided. Some architectures might need extra padding after the
2192 cmsghdr, however, and CMSG_LEN() would have to take account of
2193 this. */
2194#ifdef CMSG_LEN
2195/* If length is in range, set *result to CMSG_LEN(length) and return
2196 true; otherwise, return false. */
2197static int
2198get_CMSG_LEN(size_t length, size_t *result)
2199{
2200 size_t tmp;
2201
2202 if (length > (SOCKLEN_T_LIMIT - CMSG_LEN(0)))
2203 return 0;
2204 tmp = CMSG_LEN(length);
2205 if (tmp > SOCKLEN_T_LIMIT || tmp < length)
2206 return 0;
2207 *result = tmp;
2208 return 1;
2209}
2210
2211#ifdef CMSG_SPACE
2212/* If length is in range, set *result to CMSG_SPACE(length) and return
2213 true; otherwise, return false. */
2214static int
2215get_CMSG_SPACE(size_t length, size_t *result)
2216{
2217 size_t tmp;
2218
2219 /* Use CMSG_SPACE(1) here in order to take account of the padding
2220 necessary before *and* after the data. */
2221 if (length > (SOCKLEN_T_LIMIT - CMSG_SPACE(1)))
2222 return 0;
2223 tmp = CMSG_SPACE(length);
2224 if (tmp > SOCKLEN_T_LIMIT || tmp < length)
2225 return 0;
2226 *result = tmp;
2227 return 1;
2228}
2229#endif
2230
2231/* Return true iff msg->msg_controllen is valid, cmsgh is a valid
2232 pointer in msg->msg_control with at least "space" bytes after it,
2233 and its cmsg_len member inside the buffer. */
2234static int
2235cmsg_min_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t space)
2236{
2237 size_t cmsg_offset;
2238 static const size_t cmsg_len_end = (offsetof(struct cmsghdr, cmsg_len) +
2239 sizeof(cmsgh->cmsg_len));
2240
Charles-François Natali466517d2011-08-28 18:23:43 +02002241 /* Note that POSIX allows msg_controllen to be of signed type. */
Brett Cannonb05cbe62014-01-07 17:01:01 -05002242 if (cmsgh == NULL || msg->msg_control == NULL)
Nick Coghlan96fe56a2011-08-22 11:55:57 +10002243 return 0;
Brett Cannonb05cbe62014-01-07 17:01:01 -05002244 /* Note that POSIX allows msg_controllen to be of a signed type. This is
2245 annoying under OS X as it's unsigned there and so it triggers a
2246 tautological comparison warning under Clang when compared against 0.
2247 Since the check is valid on other platforms, silence the warning under
2248 Clang. */
2249 #ifdef __clang__
2250 #pragma clang diagnostic push
2251 #pragma clang diagnostic ignored "-Wtautological-compare"
2252 #endif
Serhiy Storchakacaa01f82015-03-05 23:42:24 +02002253 #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))
Serhiy Storchakab48af342015-02-26 15:27:57 +02002254 #pragma GCC diagnostic push
2255 #pragma GCC diagnostic ignored "-Wtype-limits"
2256 #endif
Brett Cannonb05cbe62014-01-07 17:01:01 -05002257 if (msg->msg_controllen < 0)
2258 return 0;
Serhiy Storchakacaa01f82015-03-05 23:42:24 +02002259 #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))
Serhiy Storchakab48af342015-02-26 15:27:57 +02002260 #pragma GCC diagnostic pop
2261 #endif
Brett Cannonb05cbe62014-01-07 17:01:01 -05002262 #ifdef __clang__
2263 #pragma clang diagnostic pop
2264 #endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10002265 if (space < cmsg_len_end)
2266 space = cmsg_len_end;
2267 cmsg_offset = (char *)cmsgh - (char *)msg->msg_control;
2268 return (cmsg_offset <= (size_t)-1 - space &&
2269 cmsg_offset + space <= msg->msg_controllen);
2270}
2271
2272/* If pointer CMSG_DATA(cmsgh) is in buffer msg->msg_control, set
2273 *space to number of bytes following it in the buffer and return
2274 true; otherwise, return false. Assumes cmsgh, msg->msg_control and
2275 msg->msg_controllen are valid. */
2276static int
2277get_cmsg_data_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *space)
2278{
2279 size_t data_offset;
2280 char *data_ptr;
2281
2282 if ((data_ptr = (char *)CMSG_DATA(cmsgh)) == NULL)
2283 return 0;
2284 data_offset = data_ptr - (char *)msg->msg_control;
2285 if (data_offset > msg->msg_controllen)
2286 return 0;
2287 *space = msg->msg_controllen - data_offset;
2288 return 1;
2289}
2290
2291/* If cmsgh is invalid or not contained in the buffer pointed to by
2292 msg->msg_control, return -1. If cmsgh is valid and its associated
2293 data is entirely contained in the buffer, set *data_len to the
2294 length of the associated data and return 0. If only part of the
2295 associated data is contained in the buffer but cmsgh is otherwise
2296 valid, set *data_len to the length contained in the buffer and
2297 return 1. */
2298static int
2299get_cmsg_data_len(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *data_len)
2300{
2301 size_t space, cmsg_data_len;
2302
2303 if (!cmsg_min_space(msg, cmsgh, CMSG_LEN(0)) ||
2304 cmsgh->cmsg_len < CMSG_LEN(0))
2305 return -1;
2306 cmsg_data_len = cmsgh->cmsg_len - CMSG_LEN(0);
2307 if (!get_cmsg_data_space(msg, cmsgh, &space))
2308 return -1;
2309 if (space >= cmsg_data_len) {
2310 *data_len = cmsg_data_len;
2311 return 0;
2312 }
2313 *data_len = space;
2314 return 1;
2315}
2316#endif /* CMSG_LEN */
2317
2318
Victor Stinner31bf2d52015-04-01 21:57:09 +02002319struct sock_accept {
2320 socklen_t *addrlen;
2321 sock_addr_t *addrbuf;
2322 SOCKET_T result;
2323};
2324
2325#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
2326/* accept4() is available on Linux 2.6.28+ and glibc 2.10 */
2327static int accept4_works = -1;
2328#endif
2329
2330static int
2331sock_accept_impl(PySocketSockObject *s, void *data)
2332{
2333 struct sock_accept *ctx = data;
Christian Heimesdffa3942016-09-05 23:54:41 +02002334 struct sockaddr *addr = SAS2SA(ctx->addrbuf);
2335 socklen_t *paddrlen = ctx->addrlen;
2336#ifdef HAVE_SOCKADDR_ALG
2337 /* AF_ALG does not support accept() with addr and raises
2338 * ECONNABORTED instead. */
2339 if (s->sock_family == AF_ALG) {
2340 addr = NULL;
2341 paddrlen = NULL;
2342 *ctx->addrlen = 0;
2343 }
2344#endif
Victor Stinner31bf2d52015-04-01 21:57:09 +02002345
2346#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
2347 if (accept4_works != 0) {
Christian Heimesdffa3942016-09-05 23:54:41 +02002348 ctx->result = accept4(s->sock_fd, addr, paddrlen,
Victor Stinner31bf2d52015-04-01 21:57:09 +02002349 SOCK_CLOEXEC);
2350 if (ctx->result == INVALID_SOCKET && accept4_works == -1) {
2351 /* On Linux older than 2.6.28, accept4() fails with ENOSYS */
2352 accept4_works = (errno != ENOSYS);
2353 }
2354 }
2355 if (accept4_works == 0)
Christian Heimesdffa3942016-09-05 23:54:41 +02002356 ctx->result = accept(s->sock_fd, addr, paddrlen);
Victor Stinner31bf2d52015-04-01 21:57:09 +02002357#else
Christian Heimesdffa3942016-09-05 23:54:41 +02002358 ctx->result = accept(s->sock_fd, addr, paddrlen);
Victor Stinner31bf2d52015-04-01 21:57:09 +02002359#endif
Victor Stinnerbea232a2015-07-27 23:37:11 +02002360
2361#ifdef MS_WINDOWS
2362 return (ctx->result != INVALID_SOCKET);
2363#else
Victor Stinner31bf2d52015-04-01 21:57:09 +02002364 return (ctx->result >= 0);
Victor Stinnerbea232a2015-07-27 23:37:11 +02002365#endif
Victor Stinner31bf2d52015-04-01 21:57:09 +02002366}
2367
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002368/* s._accept() -> (fd, address) */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002369
Guido van Rossum73624e91994-10-10 17:59:00 +00002370static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002371sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 sock_addr_t addrbuf;
Victor Stinner31bf2d52015-04-01 21:57:09 +02002374 SOCKET_T newfd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 socklen_t addrlen;
2376 PyObject *sock = NULL;
2377 PyObject *addr = NULL;
2378 PyObject *res = NULL;
Victor Stinner31bf2d52015-04-01 21:57:09 +02002379 struct sock_accept ctx;
Victor Stinnerdaf45552013-08-28 00:53:59 +02002380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 if (!getsockaddrlen(s, &addrlen))
2382 return NULL;
2383 memset(&addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 if (!IS_SELECTABLE(s))
2386 return select_error();
Neal Norwitz082b2df2006-02-07 07:04:46 +00002387
Victor Stinner31bf2d52015-04-01 21:57:09 +02002388 ctx.addrlen = &addrlen;
2389 ctx.addrbuf = &addrbuf;
2390 if (sock_call(s, 0, sock_accept_impl, &ctx) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 return NULL;
Victor Stinner31bf2d52015-04-01 21:57:09 +02002392 newfd = ctx.result;
Barry Warsaw752300b1997-01-03 17:18:10 +00002393
Victor Stinnerdaf45552013-08-28 00:53:59 +02002394#ifdef MS_WINDOWS
2395 if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) {
2396 PyErr_SetFromWindowsErr(0);
2397 SOCKETCLOSE(newfd);
2398 goto finally;
2399 }
2400#else
2401
2402#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
2403 if (!accept4_works)
2404#endif
2405 {
2406 if (_Py_set_inheritable(newfd, 0, NULL) < 0) {
2407 SOCKETCLOSE(newfd);
2408 goto finally;
2409 }
2410 }
2411#endif
2412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 sock = PyLong_FromSocket_t(newfd);
2414 if (sock == NULL) {
2415 SOCKETCLOSE(newfd);
2416 goto finally;
2417 }
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
2420 addrlen, s->sock_proto);
2421 if (addr == NULL)
2422 goto finally;
Barry Warsaw752300b1997-01-03 17:18:10 +00002423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 res = PyTuple_Pack(2, sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00002425
Guido van Rossum67f7a382002-06-06 21:08:16 +00002426finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 Py_XDECREF(sock);
2428 Py_XDECREF(addr);
2429 return res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002430}
2431
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002432PyDoc_STRVAR(accept_doc,
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002433"_accept() -> (integer, address info)\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002434\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002435Wait for an incoming connection. Return a new socket file descriptor\n\
2436representing the connection, and the address of the client.\n\
2437For IP sockets, the address info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002438
Guido van Rossum11ba0942002-06-13 15:07:44 +00002439/* s.setblocking(flag) method. Argument:
2440 False -- non-blocking mode; same as settimeout(0)
2441 True -- blocking mode; same as settimeout(None)
2442*/
Guido van Rossume4485b01994-09-07 14:32:49 +00002443
Guido van Rossum73624e91994-10-10 17:59:00 +00002444static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002445sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00002446{
Serhiy Storchaka78980432013-01-15 01:12:17 +02002447 long block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 block = PyLong_AsLong(arg);
2450 if (block == -1 && PyErr_Occurred())
2451 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002452
Victor Stinner9001d802015-04-06 23:06:01 +02002453 s->sock_timeout = _PyTime_FromSeconds(block ? -1 : 0);
Christian Heimes0b9d64e2016-09-09 00:28:57 +02002454 if (internal_setblocking(s, block) == -1) {
2455 return NULL;
2456 }
2457 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +00002458}
Guido van Rossume4485b01994-09-07 14:32:49 +00002459
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002460PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002461"setblocking(flag)\n\
2462\n\
2463Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00002464setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002465setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002466
Victor Stinner71694d52015-03-28 01:18:54 +01002467static int
2468socket_parse_timeout(_PyTime_t *timeout, PyObject *timeout_obj)
2469{
2470#ifdef MS_WINDOWS
2471 struct timeval tv;
2472#endif
Victor Stinner1bb0aef2015-03-31 16:31:19 +02002473#ifndef HAVE_POLL
2474 _PyTime_t ms;
2475#endif
Victor Stinner71694d52015-03-28 01:18:54 +01002476 int overflow = 0;
2477
2478 if (timeout_obj == Py_None) {
Victor Stinner10550cd2015-04-03 13:22:27 +02002479 *timeout = _PyTime_FromSeconds(-1);
Victor Stinner71694d52015-03-28 01:18:54 +01002480 return 0;
2481 }
2482
Victor Stinner869e1772015-03-30 03:49:14 +02002483 if (_PyTime_FromSecondsObject(timeout,
2484 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Victor Stinner71694d52015-03-28 01:18:54 +01002485 return -1;
2486
2487 if (*timeout < 0) {
2488 PyErr_SetString(PyExc_ValueError, "Timeout value out of range");
2489 return -1;
2490 }
2491
2492#ifdef MS_WINDOWS
Victor Stinner1bb0aef2015-03-31 16:31:19 +02002493 overflow |= (_PyTime_AsTimeval(*timeout, &tv, _PyTime_ROUND_CEILING) < 0);
Victor Stinner71694d52015-03-28 01:18:54 +01002494#endif
2495#ifndef HAVE_POLL
Victor Stinner1bb0aef2015-03-31 16:31:19 +02002496 ms = _PyTime_AsMilliseconds(*timeout, _PyTime_ROUND_CEILING);
2497 overflow |= (ms > INT_MAX);
Victor Stinner71694d52015-03-28 01:18:54 +01002498#endif
2499 if (overflow) {
2500 PyErr_SetString(PyExc_OverflowError,
2501 "timeout doesn't fit into C timeval");
2502 return -1;
2503 }
2504
2505 return 0;
2506}
2507
Guido van Rossum11ba0942002-06-13 15:07:44 +00002508/* s.settimeout(timeout) method. Argument:
2509 None -- no timeout, blocking mode; same as setblocking(True)
2510 0.0 -- non-blocking mode; same as setblocking(False)
2511 > 0 -- timeout mode; operations time out after timeout seconds
2512 < 0 -- illegal; raises an exception
2513*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00002514static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002515sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00002516{
Victor Stinner71694d52015-03-28 01:18:54 +01002517 _PyTime_t timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002518
Victor Stinner71694d52015-03-28 01:18:54 +01002519 if (socket_parse_timeout(&timeout, arg) < 0)
2520 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 s->sock_timeout = timeout;
Christian Heimes0b9d64e2016-09-09 00:28:57 +02002523 if (internal_setblocking(s, timeout < 0) == -1) {
2524 return NULL;
2525 }
2526 Py_RETURN_NONE;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002527}
2528
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002529PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002530"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00002531\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00002532Set a timeout on socket operations. 'timeout' can be a float,\n\
2533giving in seconds, or None. Setting a timeout of None disables\n\
2534the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002535Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00002536
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002537/* s.gettimeout() method.
2538 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00002539static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002540sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00002541{
Victor Stinner71694d52015-03-28 01:18:54 +01002542 if (s->sock_timeout < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 Py_INCREF(Py_None);
2544 return Py_None;
2545 }
Victor Stinner71694d52015-03-28 01:18:54 +01002546 else {
2547 double seconds = _PyTime_AsSecondsDouble(s->sock_timeout);
2548 return PyFloat_FromDouble(seconds);
2549 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002550}
2551
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002552PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002553"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00002554\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03002555Returns the timeout in seconds (float) associated with socket \n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00002556operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002557operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00002558
Guido van Rossumaee08791992-09-08 09:05:33 +00002559/* s.setsockopt() method.
Christian Heimesdffa3942016-09-05 23:54:41 +02002560 With an integer third argument, sets an integer optval with optlen=4.
2561 With None as third argument and an integer fourth argument, set
2562 optval=NULL with unsigned int as optlen.
Guido van Rossumaee08791992-09-08 09:05:33 +00002563 With a string third argument, sets an option from a buffer;
Christian Heimesdffa3942016-09-05 23:54:41 +02002564 use optional built-in module 'struct' to encode the string.
2565*/
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002566
Guido van Rossum73624e91994-10-10 17:59:00 +00002567static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002568sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 int level;
2571 int optname;
2572 int res;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02002573 Py_buffer optval;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 int flag;
Christian Heimesdffa3942016-09-05 23:54:41 +02002575 unsigned int optlen;
2576 PyObject *none;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002577
Christian Heimesdffa3942016-09-05 23:54:41 +02002578 /* setsockopt(level, opt, flag) */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 if (PyArg_ParseTuple(args, "iii:setsockopt",
2580 &level, &optname, &flag)) {
Victor Stinnerb6c15bc2015-03-31 16:35:35 +02002581 res = setsockopt(s->sock_fd, level, optname,
2582 (char*)&flag, sizeof flag);
Christian Heimesdffa3942016-09-05 23:54:41 +02002583 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 }
Christian Heimesdffa3942016-09-05 23:54:41 +02002585
2586 PyErr_Clear();
Christian Heimes8c21ab02016-09-06 00:07:02 +02002587 /* setsockopt(level, opt, None, flag) */
Christian Heimesdffa3942016-09-05 23:54:41 +02002588 if (PyArg_ParseTuple(args, "iiO!I:setsockopt",
2589 &level, &optname, Py_TYPE(Py_None), &none, &optlen)) {
2590 assert(sizeof(socklen_t) >= sizeof(unsigned int));
Victor Stinnercc739322016-03-23 21:35:29 +01002591 res = setsockopt(s->sock_fd, level, optname,
Christian Heimesdffa3942016-09-05 23:54:41 +02002592 NULL, (socklen_t)optlen);
2593 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 }
Christian Heimesdffa3942016-09-05 23:54:41 +02002595
2596 PyErr_Clear();
2597 /* setsockopt(level, opt, buffer) */
2598 if (!PyArg_ParseTuple(args, "iiy*:setsockopt",
2599 &level, &optname, &optval))
2600 return NULL;
2601
2602#ifdef MS_WINDOWS
2603 if (optval.len > INT_MAX) {
2604 PyBuffer_Release(&optval);
2605 PyErr_Format(PyExc_OverflowError,
2606 "socket option is larger than %i bytes",
2607 INT_MAX);
2608 return NULL;
2609 }
2610 res = setsockopt(s->sock_fd, level, optname,
2611 optval.buf, (int)optval.len);
2612#else
2613 res = setsockopt(s->sock_fd, level, optname, optval.buf, optval.len);
2614#endif
2615 PyBuffer_Release(&optval);
2616
2617done:
Victor Stinnercc739322016-03-23 21:35:29 +01002618 if (res < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 return s->errorhandler();
Victor Stinnercc739322016-03-23 21:35:29 +01002620 }
2621
2622 Py_RETURN_NONE;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002623}
2624
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002625PyDoc_STRVAR(setsockopt_doc,
Christian Heimesdffa3942016-09-05 23:54:41 +02002626"setsockopt(level, option, value: int)\n\
2627setsockopt(level, option, value: buffer)\n\
2628setsockopt(level, option, None, optlen: int)\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002629\n\
2630Set a socket option. See the Unix manual for level and option.\n\
Christian Heimesdffa3942016-09-05 23:54:41 +02002631The value argument can either be an integer, a string buffer, or \n\
2632None, optlen.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002633
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002634
Guido van Rossumaee08791992-09-08 09:05:33 +00002635/* s.getsockopt() method.
2636 With two arguments, retrieves an integer option.
2637 With a third integer argument, retrieves a string buffer of that size;
2638 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002639
Guido van Rossum73624e91994-10-10 17:59:00 +00002640static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002641sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643 int level;
2644 int optname;
2645 int res;
2646 PyObject *buf;
2647 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
2650 &level, &optname, &buflen))
2651 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 if (buflen == 0) {
2654 int flag = 0;
2655 socklen_t flagsize = sizeof flag;
2656 res = getsockopt(s->sock_fd, level, optname,
2657 (void *)&flag, &flagsize);
2658 if (res < 0)
2659 return s->errorhandler();
2660 return PyLong_FromLong(flag);
2661 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 if (buflen <= 0 || buflen > 1024) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002663 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 "getsockopt buflen out of range");
2665 return NULL;
2666 }
2667 buf = PyBytes_FromStringAndSize((char *)NULL, buflen);
2668 if (buf == NULL)
2669 return NULL;
2670 res = getsockopt(s->sock_fd, level, optname,
2671 (void *)PyBytes_AS_STRING(buf), &buflen);
2672 if (res < 0) {
2673 Py_DECREF(buf);
2674 return s->errorhandler();
2675 }
2676 _PyBytes_Resize(&buf, buflen);
2677 return buf;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002678}
2679
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002680PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002681"getsockopt(level, option[, buffersize]) -> value\n\
2682\n\
2683Get a socket option. See the Unix manual for level and option.\n\
2684If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002685string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002686
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002687
Fred Drake728819a2000-07-01 03:40:12 +00002688/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002689
Guido van Rossum73624e91994-10-10 17:59:00 +00002690static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002691sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002693 sock_addr_t addrbuf;
2694 int addrlen;
2695 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2698 return NULL;
2699 Py_BEGIN_ALLOW_THREADS
2700 res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);
2701 Py_END_ALLOW_THREADS
2702 if (res < 0)
2703 return s->errorhandler();
2704 Py_INCREF(Py_None);
2705 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002706}
2707
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002708PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002709"bind(address)\n\
2710\n\
2711Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00002712pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002713sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002714
Guido van Rossum30a685f1991-06-27 15:51:29 +00002715
2716/* s.close() method.
2717 Set the file descriptor to -1 so operations tried subsequently
2718 will surely fail. */
2719
Guido van Rossum73624e91994-10-10 17:59:00 +00002720static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002721sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 SOCKET_T fd;
Martin Panter50ab1a32016-04-11 00:38:12 +00002724 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002725
Victor Stinner19a8e842016-03-21 16:36:48 +01002726 fd = s->sock_fd;
Victor Stinner524714e2016-07-22 17:43:59 +02002727 if (fd != INVALID_SOCKET) {
2728 s->sock_fd = INVALID_SOCKET;
Victor Stinner19a8e842016-03-21 16:36:48 +01002729
2730 /* We do not want to retry upon EINTR: see
2731 http://lwn.net/Articles/576478/ and
2732 http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
2733 for more details. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 Py_BEGIN_ALLOW_THREADS
Martin Panter50ab1a32016-04-11 00:38:12 +00002735 res = SOCKETCLOSE(fd);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 Py_END_ALLOW_THREADS
Martin Panter50ab1a32016-04-11 00:38:12 +00002737 if (res < 0) {
2738 return s->errorhandler();
2739 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 }
2741 Py_INCREF(Py_None);
2742 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002743}
2744
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002745PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002746"close()\n\
2747\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002748Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002749
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002750static PyObject *
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002751sock_detach(PySocketSockObject *s)
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002752{
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002753 SOCKET_T fd = s->sock_fd;
Victor Stinner524714e2016-07-22 17:43:59 +02002754 s->sock_fd = INVALID_SOCKET;
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002755 return PyLong_FromSocket_t(fd);
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002756}
2757
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002758PyDoc_STRVAR(detach_doc,
2759"detach()\n\
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002760\n\
Guido van Rossum638aebd2013-12-04 11:50:09 -08002761Close the socket object without closing the underlying file descriptor.\n\
2762The object cannot be used after this call, but the file descriptor\n\
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002763can be reused for other purposes. The file descriptor is returned.");
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002764
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002765static int
Victor Stinner81c41db2015-04-02 11:50:57 +02002766sock_connect_impl(PySocketSockObject *s, void* Py_UNUSED(data))
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002767{
Victor Stinner81c41db2015-04-02 11:50:57 +02002768 int err;
2769 socklen_t size = sizeof err;
Victor Stinnerb6c15bc2015-03-31 16:35:35 +02002770
Victor Stinner81c41db2015-04-02 11:50:57 +02002771 if (getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR, (void *)&err, &size)) {
2772 /* getsockopt() failed */
2773 return 0;
2774 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002775
Victor Stinner81c41db2015-04-02 11:50:57 +02002776 if (err == EISCONN)
2777 return 1;
Victor Stinner38aec752015-04-02 14:37:20 +02002778 if (err != 0) {
2779 /* sock_call_ex() uses GET_SOCK_ERROR() to get the error code */
2780 SET_SOCK_ERROR(err);
Victor Stinner38aec752015-04-02 14:37:20 +02002781 return 0;
2782 }
2783 return 1;
Victor Stinner81c41db2015-04-02 11:50:57 +02002784}
2785
2786static int
2787internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
2788 int raise)
2789{
2790 int res, err, wait_connect;
Victor Stinnerb7df3142015-03-27 22:59:32 +01002791
2792 Py_BEGIN_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 res = connect(s->sock_fd, addr, addrlen);
Victor Stinnerb7df3142015-03-27 22:59:32 +01002794 Py_END_ALLOW_THREADS
2795
Victor Stinner70a46f62015-03-31 22:03:59 +02002796 if (!res) {
2797 /* connect() succeeded, the socket is connected */
2798 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002800
Victor Stinner81c41db2015-04-02 11:50:57 +02002801 /* connect() failed */
Victor Stinneree699e92015-03-31 21:28:42 +02002802
Victor Stinner81c41db2015-04-02 11:50:57 +02002803 /* save error, PyErr_CheckSignals() can replace it */
2804 err = GET_SOCK_ERROR;
2805 if (CHECK_ERRNO(EINTR)) {
2806 if (PyErr_CheckSignals())
Victor Stinner70a46f62015-03-31 22:03:59 +02002807 return -1;
Victor Stinner81c41db2015-04-02 11:50:57 +02002808
2809 /* Issue #23618: when connect() fails with EINTR, the connection is
2810 running asynchronously.
2811
2812 If the socket is blocking or has a timeout, wait until the
2813 connection completes, fails or timed out using select(), and then
2814 get the connection status using getsockopt(SO_ERROR).
2815
2816 If the socket is non-blocking, raise InterruptedError. The caller is
2817 responsible to wait until the connection completes, fails or timed
2818 out (it's the case in asyncio for example). */
2819 wait_connect = (s->sock_timeout != 0 && IS_SELECTABLE(s));
2820 }
2821 else {
2822 wait_connect = (s->sock_timeout > 0 && err == SOCK_INPROGRESS_ERR
2823 && IS_SELECTABLE(s));
Victor Stinner70a46f62015-03-31 22:03:59 +02002824 }
2825
Victor Stinner81c41db2015-04-02 11:50:57 +02002826 if (!wait_connect) {
2827 if (raise) {
2828 /* restore error, maybe replaced by PyErr_CheckSignals() */
2829 SET_SOCK_ERROR(err);
2830 s->errorhandler();
2831 return -1;
2832 }
2833 else
2834 return err;
Victor Stinner70a46f62015-03-31 22:03:59 +02002835 }
2836
Victor Stinner81c41db2015-04-02 11:50:57 +02002837 if (raise) {
2838 /* socket.connect() raises an exception on error */
Victor Stinner8912d142015-04-06 23:16:34 +02002839 if (sock_call_ex(s, 1, sock_connect_impl, NULL,
2840 1, NULL, s->sock_timeout) < 0)
Victor Stinner81c41db2015-04-02 11:50:57 +02002841 return -1;
Victor Stinner70a46f62015-03-31 22:03:59 +02002842 }
Victor Stinner81c41db2015-04-02 11:50:57 +02002843 else {
2844 /* socket.connect_ex() returns the error code on error */
Victor Stinner8912d142015-04-06 23:16:34 +02002845 if (sock_call_ex(s, 1, sock_connect_impl, NULL,
2846 1, &err, s->sock_timeout) < 0)
Victor Stinner81c41db2015-04-02 11:50:57 +02002847 return err;
2848 }
2849 return 0;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002850}
Guido van Rossum30a685f1991-06-27 15:51:29 +00002851
Fred Drake728819a2000-07-01 03:40:12 +00002852/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002853
Guido van Rossum73624e91994-10-10 17:59:00 +00002854static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002855sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 sock_addr_t addrbuf;
2858 int addrlen;
2859 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2862 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002863
Victor Stinner81c41db2015-04-02 11:50:57 +02002864 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, 1);
Victor Stinneree699e92015-03-31 21:28:42 +02002865 if (res < 0)
2866 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002867
Victor Stinneree699e92015-03-31 21:28:42 +02002868 Py_RETURN_NONE;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002869}
2870
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002871PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002872"connect(address)\n\
2873\n\
2874Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002875is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002876
Guido van Rossum30a685f1991-06-27 15:51:29 +00002877
Fred Drake728819a2000-07-01 03:40:12 +00002878/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002879
2880static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002881sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002882{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 sock_addr_t addrbuf;
2884 int addrlen;
2885 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2888 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002889
Victor Stinner81c41db2015-04-02 11:50:57 +02002890 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, 0);
Victor Stinner71694d52015-03-28 01:18:54 +01002891 if (res < 0)
2892 return NULL;
2893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 return PyLong_FromLong((long) res);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002895}
2896
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002897PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002898"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002899\n\
2900This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002901instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002902
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002903
Guido van Rossumed233a51992-06-23 09:07:03 +00002904/* s.fileno() method */
2905
Guido van Rossum73624e91994-10-10 17:59:00 +00002906static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002907sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00002908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 return PyLong_FromSocket_t(s->sock_fd);
Guido van Rossumed233a51992-06-23 09:07:03 +00002910}
2911
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002912PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002913"fileno() -> integer\n\
2914\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002915Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002916
Guido van Rossumed233a51992-06-23 09:07:03 +00002917
Guido van Rossumc89705d1992-11-26 08:54:07 +00002918/* s.getsockname() method */
2919
Guido van Rossum73624e91994-10-10 17:59:00 +00002920static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002921sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002922{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 sock_addr_t addrbuf;
2924 int res;
2925 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 if (!getsockaddrlen(s, &addrlen))
2928 return NULL;
2929 memset(&addrbuf, 0, addrlen);
2930 Py_BEGIN_ALLOW_THREADS
2931 res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2932 Py_END_ALLOW_THREADS
2933 if (res < 0)
2934 return s->errorhandler();
2935 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2936 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002937}
2938
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002939PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002940"getsockname() -> address info\n\
2941\n\
2942Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002943info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002944
Guido van Rossumc89705d1992-11-26 08:54:07 +00002945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002947/* s.getpeername() method */
2948
Guido van Rossum73624e91994-10-10 17:59:00 +00002949static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002950sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002951{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 sock_addr_t addrbuf;
2953 int res;
2954 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 if (!getsockaddrlen(s, &addrlen))
2957 return NULL;
2958 memset(&addrbuf, 0, addrlen);
2959 Py_BEGIN_ALLOW_THREADS
2960 res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2961 Py_END_ALLOW_THREADS
2962 if (res < 0)
2963 return s->errorhandler();
2964 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2965 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002966}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002967
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002968PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002969"getpeername() -> address info\n\
2970\n\
2971Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002972info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002973
Guido van Rossumb6775db1994-08-01 11:34:53 +00002974#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002975
2976
Guido van Rossum30a685f1991-06-27 15:51:29 +00002977/* s.listen(n) method */
2978
Guido van Rossum73624e91994-10-10 17:59:00 +00002979static PyObject *
Charles-François Natali644b8f52014-05-22 19:45:39 +01002980sock_listen(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002981{
Charles-François Natali644b8f52014-05-22 19:45:39 +01002982 /* We try to choose a default backlog high enough to avoid connection drops
2983 * for common workloads, yet not too high to limit resource usage. */
2984 int backlog = Py_MIN(SOMAXCONN, 128);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002986
Charles-François Natali644b8f52014-05-22 19:45:39 +01002987 if (!PyArg_ParseTuple(args, "|i:listen", &backlog))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 return NULL;
Charles-François Natali644b8f52014-05-22 19:45:39 +01002989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou1be815a2011-05-10 19:16:29 +02002991 /* To avoid problems on systems that don't allow a negative backlog
2992 * (which doesn't make sense anyway) we force a minimum value of 0. */
2993 if (backlog < 0)
2994 backlog = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995 res = listen(s->sock_fd, backlog);
2996 Py_END_ALLOW_THREADS
2997 if (res < 0)
2998 return s->errorhandler();
2999 Py_INCREF(Py_None);
3000 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003001}
3002
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003003PyDoc_STRVAR(listen_doc,
Charles-François Natali644b8f52014-05-22 19:45:39 +01003004"listen([backlog])\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003005\n\
Charles-François Natali644b8f52014-05-22 19:45:39 +01003006Enable a server to accept connections. If backlog is specified, it must be\n\
3007at least 0 (if it is lower, it is set to 0); it specifies the number of\n\
Antoine Pitrou1be815a2011-05-10 19:16:29 +02003008unaccepted connections that the system will allow before refusing new\n\
Charles-François Natali644b8f52014-05-22 19:45:39 +01003009connections. If not specified, a default reasonable value is chosen.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003010
Victor Stinner31bf2d52015-04-01 21:57:09 +02003011struct sock_recv {
3012 char *cbuf;
3013 Py_ssize_t len;
3014 int flags;
3015 Py_ssize_t result;
3016};
3017
3018static int
3019sock_recv_impl(PySocketSockObject *s, void *data)
3020{
3021 struct sock_recv *ctx = data;
3022
3023#ifdef MS_WINDOWS
3024 if (ctx->len > INT_MAX)
3025 ctx->len = INT_MAX;
3026 ctx->result = recv(s->sock_fd, ctx->cbuf, (int)ctx->len, ctx->flags);
3027#else
3028 ctx->result = recv(s->sock_fd, ctx->cbuf, ctx->len, ctx->flags);
3029#endif
3030 return (ctx->result >= 0);
3031}
3032
Guido van Rossum82a5c661998-07-07 20:45:43 +00003033
Thomas Wouters477c8d52006-05-27 19:21:47 +00003034/*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003035 * This is the guts of the recv() and recv_into() methods, which reads into a
Thomas Wouters902d6eb2007-01-09 23:18:33 +00003036 * char buffer. If you have any inc/dec ref to do to the objects that contain
Thomas Wouters477c8d52006-05-27 19:21:47 +00003037 * the buffer, do it in the caller. This function returns the number of bytes
Ezio Melotti13925002011-03-16 11:05:33 +02003038 * successfully read. If there was an error, it returns -1. Note that it is
Thomas Wouters477c8d52006-05-27 19:21:47 +00003039 * also possible that we return a number of bytes smaller than the request
3040 * bytes.
3041 */
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003042
Antoine Pitrou19467d22010-08-17 19:33:30 +00003043static Py_ssize_t
3044sock_recv_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags)
Thomas Wouters477c8d52006-05-27 19:21:47 +00003045{
Victor Stinner31bf2d52015-04-01 21:57:09 +02003046 struct sock_recv ctx;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 if (!IS_SELECTABLE(s)) {
3049 select_error();
3050 return -1;
3051 }
3052 if (len == 0) {
3053 /* If 0 bytes were requested, do nothing. */
3054 return 0;
3055 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003056
Victor Stinner31bf2d52015-04-01 21:57:09 +02003057 ctx.cbuf = cbuf;
3058 ctx.len = len;
3059 ctx.flags = flags;
3060 if (sock_call(s, 0, sock_recv_impl, &ctx) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 return -1;
Victor Stinner31bf2d52015-04-01 21:57:09 +02003062
3063 return ctx.result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003064}
3065
Guido van Rossum48a680c2001-03-02 06:34:14 +00003066
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00003067/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00003068
Guido van Rossum73624e91994-10-10 17:59:00 +00003069static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003070sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00003071{
Antoine Pitrou19467d22010-08-17 19:33:30 +00003072 Py_ssize_t recvlen, outlen;
3073 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 PyObject *buf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003075
Antoine Pitrou19467d22010-08-17 19:33:30 +00003076 if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 if (recvlen < 0) {
3080 PyErr_SetString(PyExc_ValueError,
3081 "negative buffersize in recv");
3082 return NULL;
3083 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 /* Allocate a new string. */
3086 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
3087 if (buf == NULL)
3088 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 /* Call the guts */
3091 outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags);
3092 if (outlen < 0) {
3093 /* An error occurred, release the string and return an
3094 error. */
3095 Py_DECREF(buf);
3096 return NULL;
3097 }
3098 if (outlen != recvlen) {
3099 /* We did not read as many bytes as we anticipated, resize the
3100 string if possible and be successful. */
3101 _PyBytes_Resize(&buf, outlen);
3102 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00003103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 return buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003105}
3106
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003107PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003108"recv(buffersize[, flags]) -> data\n\
3109\n\
3110Receive up to buffersize bytes from the socket. For the optional flags\n\
3111argument, see the Unix manual. When no data is available, block until\n\
3112at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003113the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003114
Guido van Rossum30a685f1991-06-27 15:51:29 +00003115
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003116/* s.recv_into(buffer, [nbytes [,flags]]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00003117
Thomas Wouters477c8d52006-05-27 19:21:47 +00003118static PyObject*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003119sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00003120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00003122
Antoine Pitrou19467d22010-08-17 19:33:30 +00003123 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003124 Py_buffer pbuf;
3125 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003126 Py_ssize_t buflen, readlen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 /* Get the buffer's memory */
Antoine Pitrou19467d22010-08-17 19:33:30 +00003129 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 &pbuf, &recvlen, &flags))
3131 return NULL;
3132 buf = pbuf.buf;
3133 buflen = pbuf.len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 if (recvlen < 0) {
3136 PyBuffer_Release(&pbuf);
3137 PyErr_SetString(PyExc_ValueError,
3138 "negative buffersize in recv_into");
3139 return NULL;
3140 }
3141 if (recvlen == 0) {
3142 /* If nbytes was not specified, use the buffer's length */
3143 recvlen = buflen;
3144 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146 /* Check if the buffer is large enough */
3147 if (buflen < recvlen) {
3148 PyBuffer_Release(&pbuf);
3149 PyErr_SetString(PyExc_ValueError,
3150 "buffer too small for requested bytes");
3151 return NULL;
3152 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 /* Call the guts */
3155 readlen = sock_recv_guts(s, buf, recvlen, flags);
3156 if (readlen < 0) {
3157 /* Return an error. */
3158 PyBuffer_Release(&pbuf);
3159 return NULL;
3160 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 PyBuffer_Release(&pbuf);
3163 /* Return the number of bytes read. Note that we do not do anything
3164 special here in the case that readlen < recvlen. */
3165 return PyLong_FromSsize_t(readlen);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003166}
3167
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003168PyDoc_STRVAR(recv_into_doc,
3169"recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00003170\n\
3171A version of recv() that stores its data into a buffer rather than creating \n\
3172a new string. Receive up to buffersize bytes from the socket. If buffersize \n\
3173is not specified (or 0), receive up to the size available in the given buffer.\n\
3174\n\
3175See recv() for documentation about the flags.");
3176
Victor Stinner31bf2d52015-04-01 21:57:09 +02003177struct sock_recvfrom {
3178 char* cbuf;
3179 Py_ssize_t len;
3180 int flags;
3181 socklen_t *addrlen;
3182 sock_addr_t *addrbuf;
3183 Py_ssize_t result;
3184};
3185
3186static int
3187sock_recvfrom_impl(PySocketSockObject *s, void *data)
3188{
3189 struct sock_recvfrom *ctx = data;
3190
3191 memset(ctx->addrbuf, 0, *ctx->addrlen);
3192
3193#ifdef MS_WINDOWS
3194 if (ctx->len > INT_MAX)
3195 ctx->len = INT_MAX;
3196 ctx->result = recvfrom(s->sock_fd, ctx->cbuf, (int)ctx->len, ctx->flags,
3197 SAS2SA(ctx->addrbuf), ctx->addrlen);
3198#else
3199 ctx->result = recvfrom(s->sock_fd, ctx->cbuf, ctx->len, ctx->flags,
3200 SAS2SA(ctx->addrbuf), ctx->addrlen);
3201#endif
3202 return (ctx->result >= 0);
3203}
3204
Thomas Wouters477c8d52006-05-27 19:21:47 +00003205
3206/*
Christian Heimes99170a52007-12-19 02:07:34 +00003207 * This is the guts of the recvfrom() and recvfrom_into() methods, which reads
3208 * into a char buffer. If you have any inc/def ref to do to the objects that
3209 * contain the buffer, do it in the caller. This function returns the number
Ezio Melotti13925002011-03-16 11:05:33 +02003210 * of bytes successfully read. If there was an error, it returns -1. Note
Christian Heimes99170a52007-12-19 02:07:34 +00003211 * that it is also possible that we return a number of bytes smaller than the
3212 * request bytes.
Thomas Wouters477c8d52006-05-27 19:21:47 +00003213 *
3214 * 'addr' is a return value for the address object. Note that you must decref
3215 * it yourself.
3216 */
Antoine Pitrou19467d22010-08-17 19:33:30 +00003217static Py_ssize_t
3218sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 PyObject** addr)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003221 sock_addr_t addrbuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003222 socklen_t addrlen;
Victor Stinner31bf2d52015-04-01 21:57:09 +02003223 struct sock_recvfrom ctx;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 *addr = NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 if (!getsockaddrlen(s, &addrlen))
3228 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 if (!IS_SELECTABLE(s)) {
3231 select_error();
3232 return -1;
3233 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00003234
Victor Stinner31bf2d52015-04-01 21:57:09 +02003235 ctx.cbuf = cbuf;
3236 ctx.len = len;
3237 ctx.flags = flags;
3238 ctx.addrbuf = &addrbuf;
3239 ctx.addrlen = &addrlen;
3240 if (sock_call(s, 0, sock_recvfrom_impl, &ctx) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003241 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003242
Victor Stinner31bf2d52015-04-01 21:57:09 +02003243 *addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
3244 s->sock_proto);
3245 if (*addr == NULL)
3246 return -1;
3247
3248 return ctx.result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003249}
3250
3251/* s.recvfrom(nbytes [,flags]) method */
3252
3253static PyObject *
3254sock_recvfrom(PySocketSockObject *s, PyObject *args)
3255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 PyObject *buf = NULL;
3257 PyObject *addr = NULL;
3258 PyObject *ret = NULL;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003259 int flags = 0;
3260 Py_ssize_t recvlen, outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003261
Antoine Pitrou19467d22010-08-17 19:33:30 +00003262 if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00003264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 if (recvlen < 0) {
3266 PyErr_SetString(PyExc_ValueError,
3267 "negative buffersize in recvfrom");
3268 return NULL;
3269 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003271 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
3272 if (buf == NULL)
3273 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003275 outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf),
3276 recvlen, flags, &addr);
3277 if (outlen < 0) {
3278 goto finally;
3279 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 if (outlen != recvlen) {
3282 /* We did not read as many bytes as we anticipated, resize the
Ezio Melotti13925002011-03-16 11:05:33 +02003283 string if possible and be successful. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 if (_PyBytes_Resize(&buf, outlen) < 0)
Ezio Melotti13925002011-03-16 11:05:33 +02003285 /* Oopsy, not so successful after all. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003286 goto finally;
3287 }
Barry Warsaw752300b1997-01-03 17:18:10 +00003288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003290
3291finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003292 Py_XDECREF(buf);
3293 Py_XDECREF(addr);
3294 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003295}
3296
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003297PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003298"recvfrom(buffersize[, flags]) -> (data, address info)\n\
3299\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003300Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003301
Thomas Wouters477c8d52006-05-27 19:21:47 +00003302
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003303/* s.recvfrom_into(buffer[, nbytes [,flags]]) method */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003304
3305static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003306sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00003307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00003309
Antoine Pitrou19467d22010-08-17 19:33:30 +00003310 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 Py_buffer pbuf;
3312 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003313 Py_ssize_t readlen, buflen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 PyObject *addr = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003316
Antoine Pitrou19467d22010-08-17 19:33:30 +00003317 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 kwlist, &pbuf,
3319 &recvlen, &flags))
3320 return NULL;
3321 buf = pbuf.buf;
3322 buflen = pbuf.len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 if (recvlen < 0) {
3325 PyBuffer_Release(&pbuf);
3326 PyErr_SetString(PyExc_ValueError,
3327 "negative buffersize in recvfrom_into");
3328 return NULL;
3329 }
3330 if (recvlen == 0) {
3331 /* If nbytes was not specified, use the buffer's length */
3332 recvlen = buflen;
Benjamin Petersonc6b37e22014-01-13 23:14:42 -05003333 } else if (recvlen > buflen) {
3334 PyBuffer_Release(&pbuf);
3335 PyErr_SetString(PyExc_ValueError,
3336 "nbytes is greater than the length of the buffer");
3337 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003340 readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
3341 if (readlen < 0) {
3342 PyBuffer_Release(&pbuf);
3343 /* Return an error */
3344 Py_XDECREF(addr);
3345 return NULL;
3346 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 PyBuffer_Release(&pbuf);
3349 /* Return the number of bytes read and the address. Note that we do
3350 not do anything special here in the case that readlen < recvlen. */
Antoine Pitrou19467d22010-08-17 19:33:30 +00003351 return Py_BuildValue("nN", readlen, addr);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003352}
3353
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003354PyDoc_STRVAR(recvfrom_into_doc,
3355"recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00003356\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003357Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00003358
Victor Stinner35bee932015-04-02 12:28:07 +02003359/* The sendmsg() and recvmsg[_into]() methods require a working
3360 CMSG_LEN(). See the comment near get_CMSG_LEN(). */
3361#ifdef CMSG_LEN
Victor Stinner31bf2d52015-04-01 21:57:09 +02003362struct sock_recvmsg {
3363 struct msghdr *msg;
3364 int flags;
3365 ssize_t result;
3366};
3367
3368static int
3369sock_recvmsg_impl(PySocketSockObject *s, void *data)
3370{
3371 struct sock_recvmsg *ctx = data;
3372
3373 ctx->result = recvmsg(s->sock_fd, ctx->msg, ctx->flags);
3374 return (ctx->result >= 0);
3375}
3376
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003377/*
3378 * Call recvmsg() with the supplied iovec structures, flags, and
3379 * ancillary data buffer size (controllen). Returns the tuple return
3380 * value for recvmsg() or recvmsg_into(), with the first item provided
3381 * by the supplied makeval() function. makeval() will be called with
3382 * the length read and makeval_data as arguments, and must return a
3383 * new reference (which will be decrefed if there is a subsequent
3384 * error). On error, closes any file descriptors received via
3385 * SCM_RIGHTS.
3386 */
3387static PyObject *
3388sock_recvmsg_guts(PySocketSockObject *s, struct iovec *iov, int iovlen,
3389 int flags, Py_ssize_t controllen,
3390 PyObject *(*makeval)(ssize_t, void *), void *makeval_data)
3391{
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003392 sock_addr_t addrbuf;
3393 socklen_t addrbuflen;
Charles-François Natalie9e95ae2011-08-24 21:40:53 +02003394 struct msghdr msg = {0};
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003395 PyObject *cmsg_list = NULL, *retval = NULL;
3396 void *controlbuf = NULL;
3397 struct cmsghdr *cmsgh;
3398 size_t cmsgdatalen = 0;
3399 int cmsg_status;
Victor Stinner31bf2d52015-04-01 21:57:09 +02003400 struct sock_recvmsg ctx;
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003401
3402 /* XXX: POSIX says that msg_name and msg_namelen "shall be
3403 ignored" when the socket is connected (Linux fills them in
3404 anyway for AF_UNIX sockets at least). Normally msg_namelen
3405 seems to be set to 0 if there's no address, but try to
3406 initialize msg_name to something that won't be mistaken for a
3407 real address if that doesn't happen. */
3408 if (!getsockaddrlen(s, &addrbuflen))
3409 return NULL;
3410 memset(&addrbuf, 0, addrbuflen);
3411 SAS2SA(&addrbuf)->sa_family = AF_UNSPEC;
3412
3413 if (controllen < 0 || controllen > SOCKLEN_T_LIMIT) {
3414 PyErr_SetString(PyExc_ValueError,
3415 "invalid ancillary data buffer length");
3416 return NULL;
3417 }
3418 if (controllen > 0 && (controlbuf = PyMem_Malloc(controllen)) == NULL)
3419 return PyErr_NoMemory();
3420
3421 /* Make the system call. */
3422 if (!IS_SELECTABLE(s)) {
3423 select_error();
3424 goto finally;
3425 }
3426
Victor Stinner31bf2d52015-04-01 21:57:09 +02003427 msg.msg_name = SAS2SA(&addrbuf);
3428 msg.msg_namelen = addrbuflen;
3429 msg.msg_iov = iov;
3430 msg.msg_iovlen = iovlen;
3431 msg.msg_control = controlbuf;
3432 msg.msg_controllen = controllen;
Victor Stinnerb7df3142015-03-27 22:59:32 +01003433
Victor Stinner31bf2d52015-04-01 21:57:09 +02003434 ctx.msg = &msg;
3435 ctx.flags = flags;
3436 if (sock_call(s, 0, sock_recvmsg_impl, &ctx) < 0)
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003437 goto finally;
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003438
3439 /* Make list of (level, type, data) tuples from control messages. */
3440 if ((cmsg_list = PyList_New(0)) == NULL)
3441 goto err_closefds;
3442 /* Check for empty ancillary data as old CMSG_FIRSTHDR()
3443 implementations didn't do so. */
3444 for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL);
3445 cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) {
3446 PyObject *bytes, *tuple;
3447 int tmp;
3448
3449 cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen);
3450 if (cmsg_status != 0) {
3451 if (PyErr_WarnEx(PyExc_RuntimeWarning,
3452 "received malformed or improperly-truncated "
3453 "ancillary data", 1) == -1)
3454 goto err_closefds;
3455 }
3456 if (cmsg_status < 0)
3457 break;
3458 if (cmsgdatalen > PY_SSIZE_T_MAX) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003459 PyErr_SetString(PyExc_OSError, "control message too long");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003460 goto err_closefds;
3461 }
3462
3463 bytes = PyBytes_FromStringAndSize((char *)CMSG_DATA(cmsgh),
3464 cmsgdatalen);
3465 tuple = Py_BuildValue("iiN", (int)cmsgh->cmsg_level,
3466 (int)cmsgh->cmsg_type, bytes);
3467 if (tuple == NULL)
3468 goto err_closefds;
3469 tmp = PyList_Append(cmsg_list, tuple);
3470 Py_DECREF(tuple);
3471 if (tmp != 0)
3472 goto err_closefds;
3473
3474 if (cmsg_status != 0)
3475 break;
3476 }
3477
3478 retval = Py_BuildValue("NOiN",
Victor Stinner31bf2d52015-04-01 21:57:09 +02003479 (*makeval)(ctx.result, makeval_data),
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003480 cmsg_list,
3481 (int)msg.msg_flags,
3482 makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
3483 ((msg.msg_namelen > addrbuflen) ?
3484 addrbuflen : msg.msg_namelen),
3485 s->sock_proto));
3486 if (retval == NULL)
3487 goto err_closefds;
3488
3489finally:
3490 Py_XDECREF(cmsg_list);
3491 PyMem_Free(controlbuf);
3492 return retval;
3493
3494err_closefds:
3495#ifdef SCM_RIGHTS
3496 /* Close all descriptors coming from SCM_RIGHTS, so they don't leak. */
3497 for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL);
3498 cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) {
3499 cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen);
3500 if (cmsg_status < 0)
3501 break;
3502 if (cmsgh->cmsg_level == SOL_SOCKET &&
3503 cmsgh->cmsg_type == SCM_RIGHTS) {
3504 size_t numfds;
3505 int *fdp;
3506
3507 numfds = cmsgdatalen / sizeof(int);
3508 fdp = (int *)CMSG_DATA(cmsgh);
3509 while (numfds-- > 0)
3510 close(*fdp++);
3511 }
3512 if (cmsg_status != 0)
3513 break;
3514 }
3515#endif /* SCM_RIGHTS */
3516 goto finally;
3517}
3518
3519
3520static PyObject *
3521makeval_recvmsg(ssize_t received, void *data)
3522{
3523 PyObject **buf = data;
3524
3525 if (received < PyBytes_GET_SIZE(*buf))
3526 _PyBytes_Resize(buf, received);
3527 Py_XINCREF(*buf);
3528 return *buf;
3529}
3530
3531/* s.recvmsg(bufsize[, ancbufsize[, flags]]) method */
3532
3533static PyObject *
3534sock_recvmsg(PySocketSockObject *s, PyObject *args)
3535{
3536 Py_ssize_t bufsize, ancbufsize = 0;
3537 int flags = 0;
3538 struct iovec iov;
3539 PyObject *buf = NULL, *retval = NULL;
3540
3541 if (!PyArg_ParseTuple(args, "n|ni:recvmsg", &bufsize, &ancbufsize, &flags))
3542 return NULL;
3543
3544 if (bufsize < 0) {
3545 PyErr_SetString(PyExc_ValueError, "negative buffer size in recvmsg()");
3546 return NULL;
3547 }
3548 if ((buf = PyBytes_FromStringAndSize(NULL, bufsize)) == NULL)
3549 return NULL;
3550 iov.iov_base = PyBytes_AS_STRING(buf);
3551 iov.iov_len = bufsize;
3552
3553 /* Note that we're passing a pointer to *our pointer* to the bytes
3554 object here (&buf); makeval_recvmsg() may incref the object, or
3555 deallocate it and set our pointer to NULL. */
3556 retval = sock_recvmsg_guts(s, &iov, 1, flags, ancbufsize,
3557 &makeval_recvmsg, &buf);
3558 Py_XDECREF(buf);
3559 return retval;
3560}
3561
3562PyDoc_STRVAR(recvmsg_doc,
3563"recvmsg(bufsize[, ancbufsize[, flags]]) -> (data, ancdata, msg_flags, address)\n\
3564\n\
3565Receive normal data (up to bufsize bytes) and ancillary data from the\n\
3566socket. The ancbufsize argument sets the size in bytes of the\n\
3567internal buffer used to receive the ancillary data; it defaults to 0,\n\
3568meaning that no ancillary data will be received. Appropriate buffer\n\
3569sizes for ancillary data can be calculated using CMSG_SPACE() or\n\
3570CMSG_LEN(), and items which do not fit into the buffer might be\n\
3571truncated or discarded. The flags argument defaults to 0 and has the\n\
3572same meaning as for recv().\n\
3573\n\
3574The return value is a 4-tuple: (data, ancdata, msg_flags, address).\n\
3575The data item is a bytes object holding the non-ancillary data\n\
3576received. The ancdata item is a list of zero or more tuples\n\
3577(cmsg_level, cmsg_type, cmsg_data) representing the ancillary data\n\
3578(control messages) received: cmsg_level and cmsg_type are integers\n\
3579specifying the protocol level and protocol-specific type respectively,\n\
3580and cmsg_data is a bytes object holding the associated data. The\n\
3581msg_flags item is the bitwise OR of various flags indicating\n\
3582conditions on the received message; see your system documentation for\n\
3583details. If the receiving socket is unconnected, address is the\n\
3584address of the sending socket, if available; otherwise, its value is\n\
3585unspecified.\n\
3586\n\
3587If recvmsg() raises an exception after the system call returns, it\n\
3588will first attempt to close any file descriptors received via the\n\
3589SCM_RIGHTS mechanism.");
3590
3591
3592static PyObject *
3593makeval_recvmsg_into(ssize_t received, void *data)
3594{
3595 return PyLong_FromSsize_t(received);
3596}
3597
3598/* s.recvmsg_into(buffers[, ancbufsize[, flags]]) method */
3599
3600static PyObject *
3601sock_recvmsg_into(PySocketSockObject *s, PyObject *args)
3602{
3603 Py_ssize_t ancbufsize = 0;
3604 int flags = 0;
3605 struct iovec *iovs = NULL;
3606 Py_ssize_t i, nitems, nbufs = 0;
3607 Py_buffer *bufs = NULL;
3608 PyObject *buffers_arg, *fast, *retval = NULL;
3609
3610 if (!PyArg_ParseTuple(args, "O|ni:recvmsg_into",
3611 &buffers_arg, &ancbufsize, &flags))
3612 return NULL;
3613
3614 if ((fast = PySequence_Fast(buffers_arg,
3615 "recvmsg_into() argument 1 must be an "
3616 "iterable")) == NULL)
3617 return NULL;
3618 nitems = PySequence_Fast_GET_SIZE(fast);
3619 if (nitems > INT_MAX) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003620 PyErr_SetString(PyExc_OSError, "recvmsg_into() argument 1 is too long");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003621 goto finally;
3622 }
3623
3624 /* Fill in an iovec for each item, and save the Py_buffer
3625 structs to release afterwards. */
3626 if (nitems > 0 && ((iovs = PyMem_New(struct iovec, nitems)) == NULL ||
3627 (bufs = PyMem_New(Py_buffer, nitems)) == NULL)) {
3628 PyErr_NoMemory();
3629 goto finally;
3630 }
3631 for (; nbufs < nitems; nbufs++) {
3632 if (!PyArg_Parse(PySequence_Fast_GET_ITEM(fast, nbufs),
3633 "w*;recvmsg_into() argument 1 must be an iterable "
3634 "of single-segment read-write buffers",
3635 &bufs[nbufs]))
3636 goto finally;
3637 iovs[nbufs].iov_base = bufs[nbufs].buf;
3638 iovs[nbufs].iov_len = bufs[nbufs].len;
3639 }
3640
3641 retval = sock_recvmsg_guts(s, iovs, nitems, flags, ancbufsize,
3642 &makeval_recvmsg_into, NULL);
3643finally:
3644 for (i = 0; i < nbufs; i++)
3645 PyBuffer_Release(&bufs[i]);
3646 PyMem_Free(bufs);
3647 PyMem_Free(iovs);
3648 Py_DECREF(fast);
3649 return retval;
3650}
3651
3652PyDoc_STRVAR(recvmsg_into_doc,
3653"recvmsg_into(buffers[, ancbufsize[, flags]]) -> (nbytes, ancdata, msg_flags, address)\n\
3654\n\
3655Receive normal data and ancillary data from the socket, scattering the\n\
3656non-ancillary data into a series of buffers. The buffers argument\n\
3657must be an iterable of objects that export writable buffers\n\
3658(e.g. bytearray objects); these will be filled with successive chunks\n\
3659of the non-ancillary data until it has all been written or there are\n\
3660no more buffers. The ancbufsize argument sets the size in bytes of\n\
3661the internal buffer used to receive the ancillary data; it defaults to\n\
36620, meaning that no ancillary data will be received. Appropriate\n\
3663buffer sizes for ancillary data can be calculated using CMSG_SPACE()\n\
3664or CMSG_LEN(), and items which do not fit into the buffer might be\n\
3665truncated or discarded. The flags argument defaults to 0 and has the\n\
3666same meaning as for recv().\n\
3667\n\
3668The return value is a 4-tuple: (nbytes, ancdata, msg_flags, address).\n\
3669The nbytes item is the total number of bytes of non-ancillary data\n\
3670written into the buffers. The ancdata item is a list of zero or more\n\
3671tuples (cmsg_level, cmsg_type, cmsg_data) representing the ancillary\n\
3672data (control messages) received: cmsg_level and cmsg_type are\n\
3673integers specifying the protocol level and protocol-specific type\n\
3674respectively, and cmsg_data is a bytes object holding the associated\n\
3675data. The msg_flags item is the bitwise OR of various flags\n\
3676indicating conditions on the received message; see your system\n\
3677documentation for details. If the receiving socket is unconnected,\n\
3678address is the address of the sending socket, if available; otherwise,\n\
3679its value is unspecified.\n\
3680\n\
3681If recvmsg_into() raises an exception after the system call returns,\n\
3682it will first attempt to close any file descriptors received via the\n\
3683SCM_RIGHTS mechanism.");
3684#endif /* CMSG_LEN */
3685
3686
Victor Stinner31bf2d52015-04-01 21:57:09 +02003687struct sock_send {
3688 char *buf;
3689 Py_ssize_t len;
3690 int flags;
3691 Py_ssize_t result;
3692};
3693
3694static int
3695sock_send_impl(PySocketSockObject *s, void *data)
3696{
3697 struct sock_send *ctx = data;
3698
3699#ifdef MS_WINDOWS
3700 if (ctx->len > INT_MAX)
3701 ctx->len = INT_MAX;
3702 ctx->result = send(s->sock_fd, ctx->buf, (int)ctx->len, ctx->flags);
3703#else
3704 ctx->result = send(s->sock_fd, ctx->buf, ctx->len, ctx->flags);
3705#endif
3706 return (ctx->result >= 0);
3707}
3708
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00003709/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00003710
Guido van Rossum73624e91994-10-10 17:59:00 +00003711static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003712sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00003713{
Victor Stinner31bf2d52015-04-01 21:57:09 +02003714 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 Py_buffer pbuf;
Victor Stinner31bf2d52015-04-01 21:57:09 +02003716 struct sock_send ctx;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags))
3719 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721 if (!IS_SELECTABLE(s)) {
3722 PyBuffer_Release(&pbuf);
3723 return select_error();
3724 }
Victor Stinner31bf2d52015-04-01 21:57:09 +02003725 ctx.buf = pbuf.buf;
3726 ctx.len = pbuf.len;
3727 ctx.flags = flags;
3728 if (sock_call(s, 1, sock_send_impl, &ctx) < 0) {
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003729 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 return NULL;
3731 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003732 PyBuffer_Release(&pbuf);
Victor Stinner31bf2d52015-04-01 21:57:09 +02003733
3734 return PyLong_FromSsize_t(ctx.result);
Guido van Rossum30a685f1991-06-27 15:51:29 +00003735}
3736
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003737PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003738"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003739\n\
3740Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003741argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003742sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003743
3744
3745/* s.sendall(data [,flags]) method */
3746
3747static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003748sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003750 char *buf;
Victor Stinner02f32ab2015-04-01 22:53:26 +02003751 Py_ssize_t len, n;
3752 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 Py_buffer pbuf;
Victor Stinner02f32ab2015-04-01 22:53:26 +02003754 struct sock_send ctx;
Victor Stinner8912d142015-04-06 23:16:34 +02003755 int has_timeout = (s->sock_timeout > 0);
3756 _PyTime_t interval = s->sock_timeout;
3757 _PyTime_t deadline = 0;
3758 int deadline_initialized = 0;
3759 PyObject *res = NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags))
3762 return NULL;
3763 buf = pbuf.buf;
3764 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 if (!IS_SELECTABLE(s)) {
3767 PyBuffer_Release(&pbuf);
3768 return select_error();
3769 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00003770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003771 do {
Victor Stinner8912d142015-04-06 23:16:34 +02003772 if (has_timeout) {
3773 if (deadline_initialized) {
3774 /* recompute the timeout */
3775 interval = deadline - _PyTime_GetMonotonicClock();
3776 }
3777 else {
3778 deadline_initialized = 1;
3779 deadline = _PyTime_GetMonotonicClock() + s->sock_timeout;
3780 }
3781
3782 if (interval <= 0) {
3783 PyErr_SetString(socket_timeout, "timed out");
3784 goto done;
3785 }
3786 }
3787
Victor Stinner02f32ab2015-04-01 22:53:26 +02003788 ctx.buf = buf;
3789 ctx.len = len;
3790 ctx.flags = flags;
Victor Stinner8912d142015-04-06 23:16:34 +02003791 if (sock_call_ex(s, 1, sock_send_impl, &ctx, 0, NULL, interval) < 0)
3792 goto done;
Victor Stinner02f32ab2015-04-01 22:53:26 +02003793 n = ctx.result;
3794 assert(n >= 0);
3795
3796 buf += n;
3797 len -= n;
3798
3799 /* We must run our signal handlers before looping again.
3800 send() can return a successful partial write when it is
3801 interrupted, so we can't restrict ourselves to EINTR. */
Victor Stinner8912d142015-04-06 23:16:34 +02003802 if (PyErr_CheckSignals())
3803 goto done;
Victor Stinner02f32ab2015-04-01 22:53:26 +02003804 } while (len > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003805 PyBuffer_Release(&pbuf);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003806
Victor Stinner8912d142015-04-06 23:16:34 +02003807 Py_INCREF(Py_None);
3808 res = Py_None;
3809
3810done:
3811 PyBuffer_Release(&pbuf);
3812 return res;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003813}
3814
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003815PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003816"sendall(data[, flags])\n\
3817\n\
3818Send a data string to the socket. For the optional flags\n\
3819argument, see the Unix manual. This calls send() repeatedly\n\
3820until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003821to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003822
Guido van Rossum30a685f1991-06-27 15:51:29 +00003823
Victor Stinner31bf2d52015-04-01 21:57:09 +02003824struct sock_sendto {
3825 char *buf;
3826 Py_ssize_t len;
3827 int flags;
3828 int addrlen;
3829 sock_addr_t *addrbuf;
3830 Py_ssize_t result;
3831};
3832
3833static int
3834sock_sendto_impl(PySocketSockObject *s, void *data)
3835{
3836 struct sock_sendto *ctx = data;
3837
3838#ifdef MS_WINDOWS
3839 if (ctx->len > INT_MAX)
3840 ctx->len = INT_MAX;
3841 ctx->result = sendto(s->sock_fd, ctx->buf, (int)ctx->len, ctx->flags,
3842 SAS2SA(ctx->addrbuf), ctx->addrlen);
3843#else
3844 ctx->result = sendto(s->sock_fd, ctx->buf, ctx->len, ctx->flags,
3845 SAS2SA(ctx->addrbuf), ctx->addrlen);
3846#endif
3847 return (ctx->result >= 0);
3848}
3849
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00003850/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00003851
Guido van Rossum73624e91994-10-10 17:59:00 +00003852static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003853sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003855 Py_buffer pbuf;
3856 PyObject *addro;
Victor Stinner31bf2d52015-04-01 21:57:09 +02003857 Py_ssize_t arglen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003858 sock_addr_t addrbuf;
Victor Stinner31bf2d52015-04-01 21:57:09 +02003859 int addrlen, flags;
3860 struct sock_sendto ctx;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003862 flags = 0;
Antoine Pitrou5e981412011-03-17 22:38:37 +01003863 arglen = PyTuple_Size(args);
3864 switch (arglen) {
3865 case 2:
3866 PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro);
3867 break;
3868 case 3:
3869 PyArg_ParseTuple(args, "y*iO:sendto",
3870 &pbuf, &flags, &addro);
3871 break;
3872 default:
3873 PyErr_Format(PyExc_TypeError,
3874 "sendto() takes 2 or 3 arguments (%d given)",
3875 arglen);
Victor Stinner77af1722011-05-26 14:05:59 +02003876 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 }
Antoine Pitrou5e981412011-03-17 22:38:37 +01003878 if (PyErr_Occurred())
3879 return NULL;
3880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003881 if (!IS_SELECTABLE(s)) {
3882 PyBuffer_Release(&pbuf);
3883 return select_error();
3884 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00003885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003886 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) {
3887 PyBuffer_Release(&pbuf);
3888 return NULL;
3889 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003890
Victor Stinner31bf2d52015-04-01 21:57:09 +02003891 ctx.buf = pbuf.buf;
3892 ctx.len = pbuf.len;
3893 ctx.flags = flags;
3894 ctx.addrlen = addrlen;
3895 ctx.addrbuf = &addrbuf;
3896 if (sock_call(s, 1, sock_sendto_impl, &ctx) < 0) {
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003897 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898 return NULL;
3899 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003900 PyBuffer_Release(&pbuf);
Victor Stinner31bf2d52015-04-01 21:57:09 +02003901
3902 return PyLong_FromSsize_t(ctx.result);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003903}
3904
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003905PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003906"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003907\n\
3908Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003909For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003910
Guido van Rossum30a685f1991-06-27 15:51:29 +00003911
Victor Stinner35bee932015-04-02 12:28:07 +02003912/* The sendmsg() and recvmsg[_into]() methods require a working
3913 CMSG_LEN(). See the comment near get_CMSG_LEN(). */
3914#ifdef CMSG_LEN
Victor Stinner31bf2d52015-04-01 21:57:09 +02003915struct sock_sendmsg {
3916 struct msghdr *msg;
3917 int flags;
3918 ssize_t result;
3919};
3920
3921static int
Christian Heimesdffa3942016-09-05 23:54:41 +02003922sock_sendmsg_iovec(PySocketSockObject *s, PyObject *data_arg,
3923 struct msghdr *msg,
3924 Py_buffer **databufsout, Py_ssize_t *ndatabufsout) {
3925 Py_ssize_t ndataparts, ndatabufs = 0;
3926 int result = -1;
3927 struct iovec *iovs = NULL;
3928 PyObject *data_fast = NULL;
3929 Py_buffer *databufs = NULL;
3930
3931 /* Fill in an iovec for each message part, and save the Py_buffer
3932 structs to release afterwards. */
Victor Stinner288b5bf2016-09-12 11:45:59 +02003933 data_fast = PySequence_Fast(data_arg,
3934 "sendmsg() argument 1 must be an "
3935 "iterable");
3936 if (data_fast == NULL) {
Christian Heimesdffa3942016-09-05 23:54:41 +02003937 goto finally;
Victor Stinner288b5bf2016-09-12 11:45:59 +02003938 }
3939
Christian Heimesdffa3942016-09-05 23:54:41 +02003940 ndataparts = PySequence_Fast_GET_SIZE(data_fast);
3941 if (ndataparts > INT_MAX) {
3942 PyErr_SetString(PyExc_OSError, "sendmsg() argument 1 is too long");
3943 goto finally;
3944 }
Victor Stinner288b5bf2016-09-12 11:45:59 +02003945
Christian Heimesdffa3942016-09-05 23:54:41 +02003946 msg->msg_iovlen = ndataparts;
Victor Stinner288b5bf2016-09-12 11:45:59 +02003947 if (ndataparts > 0) {
3948 iovs = PyMem_New(struct iovec, ndataparts);
3949 if (iovs == NULL) {
3950 PyErr_NoMemory();
3951 goto finally;
3952 }
3953 msg->msg_iov = iovs;
3954
3955 databufs = PyMem_New(Py_buffer, ndataparts);
Christian Heimesfaf2cea2016-09-13 10:07:16 +02003956 if (databufs == NULL) {
Victor Stinner288b5bf2016-09-12 11:45:59 +02003957 PyErr_NoMemory();
3958 goto finally;
3959 }
Christian Heimesdffa3942016-09-05 23:54:41 +02003960 }
3961 for (; ndatabufs < ndataparts; ndatabufs++) {
3962 if (!PyArg_Parse(PySequence_Fast_GET_ITEM(data_fast, ndatabufs),
3963 "y*;sendmsg() argument 1 must be an iterable of "
3964 "bytes-like objects",
3965 &databufs[ndatabufs]))
3966 goto finally;
3967 iovs[ndatabufs].iov_base = databufs[ndatabufs].buf;
3968 iovs[ndatabufs].iov_len = databufs[ndatabufs].len;
3969 }
3970 result = 0;
3971 finally:
3972 *databufsout = databufs;
3973 *ndatabufsout = ndatabufs;
3974 Py_XDECREF(data_fast);
3975 return result;
3976}
3977
3978static int
Victor Stinner31bf2d52015-04-01 21:57:09 +02003979sock_sendmsg_impl(PySocketSockObject *s, void *data)
3980{
3981 struct sock_sendmsg *ctx = data;
3982
3983 ctx->result = sendmsg(s->sock_fd, ctx->msg, ctx->flags);
3984 return (ctx->result >= 0);
3985}
3986
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003987/* s.sendmsg(buffers[, ancdata[, flags[, address]]]) method */
3988
3989static PyObject *
3990sock_sendmsg(PySocketSockObject *s, PyObject *args)
3991{
Christian Heimesdffa3942016-09-05 23:54:41 +02003992 Py_ssize_t i, ndatabufs = 0, ncmsgs, ncmsgbufs = 0;
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003993 Py_buffer *databufs = NULL;
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003994 sock_addr_t addrbuf;
Victor Stinner288b5bf2016-09-12 11:45:59 +02003995 struct msghdr msg;
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003996 struct cmsginfo {
3997 int level;
3998 int type;
3999 Py_buffer data;
4000 } *cmsgs = NULL;
4001 void *controlbuf = NULL;
4002 size_t controllen, controllen_last;
Victor Stinner31bf2d52015-04-01 21:57:09 +02004003 int addrlen, flags = 0;
Christian Heimesdffa3942016-09-05 23:54:41 +02004004 PyObject *data_arg, *cmsg_arg = NULL, *addr_arg = NULL,
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004005 *cmsg_fast = NULL, *retval = NULL;
Victor Stinner31bf2d52015-04-01 21:57:09 +02004006 struct sock_sendmsg ctx;
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004007
4008 if (!PyArg_ParseTuple(args, "O|OiO:sendmsg",
Victor Stinner288b5bf2016-09-12 11:45:59 +02004009 &data_arg, &cmsg_arg, &flags, &addr_arg)) {
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004010 return NULL;
Victor Stinner288b5bf2016-09-12 11:45:59 +02004011 }
4012
4013 memset(&msg, 0, sizeof(msg));
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004014
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004015 /* Parse destination address. */
4016 if (addr_arg != NULL && addr_arg != Py_None) {
4017 if (!getsockaddrarg(s, addr_arg, SAS2SA(&addrbuf), &addrlen))
4018 goto finally;
4019 msg.msg_name = &addrbuf;
4020 msg.msg_namelen = addrlen;
4021 }
4022
4023 /* Fill in an iovec for each message part, and save the Py_buffer
4024 structs to release afterwards. */
Christian Heimesdffa3942016-09-05 23:54:41 +02004025 if (sock_sendmsg_iovec(s, data_arg, &msg, &databufs, &ndatabufs) == -1) {
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004026 goto finally;
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004027 }
4028
4029 if (cmsg_arg == NULL)
4030 ncmsgs = 0;
4031 else {
4032 if ((cmsg_fast = PySequence_Fast(cmsg_arg,
4033 "sendmsg() argument 2 must be an "
4034 "iterable")) == NULL)
4035 goto finally;
4036 ncmsgs = PySequence_Fast_GET_SIZE(cmsg_fast);
4037 }
4038
4039#ifndef CMSG_SPACE
4040 if (ncmsgs > 1) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004041 PyErr_SetString(PyExc_OSError,
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004042 "sending multiple control messages is not supported "
4043 "on this system");
4044 goto finally;
4045 }
4046#endif
4047 /* Save level, type and Py_buffer for each control message,
4048 and calculate total size. */
4049 if (ncmsgs > 0 && (cmsgs = PyMem_New(struct cmsginfo, ncmsgs)) == NULL) {
4050 PyErr_NoMemory();
4051 goto finally;
4052 }
4053 controllen = controllen_last = 0;
4054 while (ncmsgbufs < ncmsgs) {
4055 size_t bufsize, space;
4056
4057 if (!PyArg_Parse(PySequence_Fast_GET_ITEM(cmsg_fast, ncmsgbufs),
4058 "(iiy*):[sendmsg() ancillary data items]",
4059 &cmsgs[ncmsgbufs].level,
4060 &cmsgs[ncmsgbufs].type,
4061 &cmsgs[ncmsgbufs].data))
4062 goto finally;
4063 bufsize = cmsgs[ncmsgbufs++].data.len;
4064
4065#ifdef CMSG_SPACE
4066 if (!get_CMSG_SPACE(bufsize, &space)) {
4067#else
4068 if (!get_CMSG_LEN(bufsize, &space)) {
4069#endif
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004070 PyErr_SetString(PyExc_OSError, "ancillary data item too large");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004071 goto finally;
4072 }
4073 controllen += space;
4074 if (controllen > SOCKLEN_T_LIMIT || controllen < controllen_last) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004075 PyErr_SetString(PyExc_OSError, "too much ancillary data");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004076 goto finally;
4077 }
4078 controllen_last = controllen;
4079 }
4080
4081 /* Construct ancillary data block from control message info. */
4082 if (ncmsgbufs > 0) {
4083 struct cmsghdr *cmsgh = NULL;
4084
Victor Stinner52d61e42016-09-12 11:41:58 +02004085 controlbuf = PyMem_Malloc(controllen);
4086 if (controlbuf == NULL) {
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004087 PyErr_NoMemory();
4088 goto finally;
4089 }
Victor Stinner52d61e42016-09-12 11:41:58 +02004090 msg.msg_control = controlbuf;
4091
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004092 msg.msg_controllen = controllen;
4093
4094 /* Need to zero out the buffer as a workaround for glibc's
4095 CMSG_NXTHDR() implementation. After getting the pointer to
4096 the next header, it checks its (uninitialized) cmsg_len
4097 member to see if the "message" fits in the buffer, and
4098 returns NULL if it doesn't. Zero-filling the buffer
Terry Jan Reedy0f847642013-03-11 18:34:00 -04004099 ensures that this doesn't happen. */
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004100 memset(controlbuf, 0, controllen);
4101
4102 for (i = 0; i < ncmsgbufs; i++) {
4103 size_t msg_len, data_len = cmsgs[i].data.len;
4104 int enough_space = 0;
4105
4106 cmsgh = (i == 0) ? CMSG_FIRSTHDR(&msg) : CMSG_NXTHDR(&msg, cmsgh);
4107 if (cmsgh == NULL) {
4108 PyErr_Format(PyExc_RuntimeError,
4109 "unexpected NULL result from %s()",
4110 (i == 0) ? "CMSG_FIRSTHDR" : "CMSG_NXTHDR");
4111 goto finally;
4112 }
4113 if (!get_CMSG_LEN(data_len, &msg_len)) {
4114 PyErr_SetString(PyExc_RuntimeError,
4115 "item size out of range for CMSG_LEN()");
4116 goto finally;
4117 }
4118 if (cmsg_min_space(&msg, cmsgh, msg_len)) {
4119 size_t space;
4120
4121 cmsgh->cmsg_len = msg_len;
4122 if (get_cmsg_data_space(&msg, cmsgh, &space))
4123 enough_space = (space >= data_len);
4124 }
4125 if (!enough_space) {
4126 PyErr_SetString(PyExc_RuntimeError,
4127 "ancillary data does not fit in calculated "
4128 "space");
4129 goto finally;
4130 }
4131 cmsgh->cmsg_level = cmsgs[i].level;
4132 cmsgh->cmsg_type = cmsgs[i].type;
4133 memcpy(CMSG_DATA(cmsgh), cmsgs[i].data.buf, data_len);
4134 }
4135 }
4136
4137 /* Make the system call. */
4138 if (!IS_SELECTABLE(s)) {
4139 select_error();
4140 goto finally;
4141 }
4142
Victor Stinner31bf2d52015-04-01 21:57:09 +02004143 ctx.msg = &msg;
4144 ctx.flags = flags;
4145 if (sock_call(s, 1, sock_sendmsg_impl, &ctx) < 0)
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004146 goto finally;
Victor Stinner31bf2d52015-04-01 21:57:09 +02004147
4148 retval = PyLong_FromSsize_t(ctx.result);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004149
4150finally:
4151 PyMem_Free(controlbuf);
4152 for (i = 0; i < ncmsgbufs; i++)
4153 PyBuffer_Release(&cmsgs[i].data);
4154 PyMem_Free(cmsgs);
4155 Py_XDECREF(cmsg_fast);
Victor Stinner52d61e42016-09-12 11:41:58 +02004156 PyMem_Free(msg.msg_iov);
4157 for (i = 0; i < ndatabufs; i++) {
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004158 PyBuffer_Release(&databufs[i]);
Victor Stinner52d61e42016-09-12 11:41:58 +02004159 }
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004160 PyMem_Free(databufs);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004161 return retval;
4162}
4163
4164PyDoc_STRVAR(sendmsg_doc,
4165"sendmsg(buffers[, ancdata[, flags[, address]]]) -> count\n\
4166\n\
4167Send normal and ancillary data to the socket, gathering the\n\
4168non-ancillary data from a series of buffers and concatenating it into\n\
4169a single message. The buffers argument specifies the non-ancillary\n\
Serhiy Storchakab757c832014-12-05 22:25:22 +02004170data as an iterable of bytes-like objects (e.g. bytes objects).\n\
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004171The ancdata argument specifies the ancillary data (control messages)\n\
4172as an iterable of zero or more tuples (cmsg_level, cmsg_type,\n\
4173cmsg_data), where cmsg_level and cmsg_type are integers specifying the\n\
4174protocol level and protocol-specific type respectively, and cmsg_data\n\
Serhiy Storchakab757c832014-12-05 22:25:22 +02004175is a bytes-like object holding the associated data. The flags\n\
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004176argument defaults to 0 and has the same meaning as for send(). If\n\
4177address is supplied and not None, it sets a destination address for\n\
4178the message. The return value is the number of bytes of non-ancillary\n\
4179data sent.");
4180#endif /* CMSG_LEN */
4181
Christian Heimesdffa3942016-09-05 23:54:41 +02004182#ifdef HAVE_SOCKADDR_ALG
4183static PyObject*
4184sock_sendmsg_afalg(PySocketSockObject *self, PyObject *args, PyObject *kwds)
4185{
4186 PyObject *retval = NULL;
4187
4188 Py_ssize_t i, ndatabufs = 0;
4189 Py_buffer *databufs = NULL;
4190 PyObject *data_arg = NULL;
4191
4192 Py_buffer iv = {NULL, NULL};
4193
4194 PyObject *opobj = NULL;
4195 int op = -1;
4196
4197 PyObject *assoclenobj = NULL;
4198 int assoclen = -1;
4199
4200 unsigned int *uiptr;
4201 int flags = 0;
4202
4203 struct msghdr msg;
4204 struct cmsghdr *header = NULL;
4205 struct af_alg_iv *alg_iv = NULL;
4206 struct sock_sendmsg ctx;
4207 Py_ssize_t controllen;
4208 void *controlbuf = NULL;
4209 static char *keywords[] = {"msg", "op", "iv", "assoclen", "flags", 0};
4210
4211 if (self->sock_family != AF_ALG) {
4212 PyErr_SetString(PyExc_OSError,
4213 "algset is only supported for AF_ALG");
4214 return NULL;
4215 }
4216
4217 if (!PyArg_ParseTupleAndKeywords(args, kwds,
4218 "|O$O!y*O!i:sendmsg_afalg", keywords,
4219 &data_arg,
4220 &PyLong_Type, &opobj, &iv,
Victor Stinner288b5bf2016-09-12 11:45:59 +02004221 &PyLong_Type, &assoclenobj, &flags)) {
Christian Heimesdffa3942016-09-05 23:54:41 +02004222 return NULL;
Victor Stinner288b5bf2016-09-12 11:45:59 +02004223 }
4224
4225 memset(&msg, 0, sizeof(msg));
Christian Heimesdffa3942016-09-05 23:54:41 +02004226
4227 /* op is a required, keyword-only argument >= 0 */
4228 if (opobj != NULL) {
4229 op = _PyLong_AsInt(opobj);
4230 }
4231 if (op < 0) {
4232 /* override exception from _PyLong_AsInt() */
4233 PyErr_SetString(PyExc_TypeError,
4234 "Invalid or missing argument 'op'");
4235 goto finally;
4236 }
4237 /* assoclen is optional but must be >= 0 */
4238 if (assoclenobj != NULL) {
4239 assoclen = _PyLong_AsInt(assoclenobj);
4240 if (assoclen == -1 && PyErr_Occurred()) {
4241 goto finally;
4242 }
4243 if (assoclen < 0) {
4244 PyErr_SetString(PyExc_TypeError,
4245 "assoclen must be positive");
4246 goto finally;
4247 }
4248 }
4249
4250 controllen = CMSG_SPACE(4);
4251 if (iv.buf != NULL) {
4252 controllen += CMSG_SPACE(sizeof(*alg_iv) + iv.len);
4253 }
4254 if (assoclen >= 0) {
4255 controllen += CMSG_SPACE(4);
4256 }
4257
4258 controlbuf = PyMem_Malloc(controllen);
4259 if (controlbuf == NULL) {
Victor Stinner52d61e42016-09-12 11:41:58 +02004260 PyErr_NoMemory();
4261 goto finally;
Christian Heimesdffa3942016-09-05 23:54:41 +02004262 }
4263 memset(controlbuf, 0, controllen);
4264
Christian Heimesdffa3942016-09-05 23:54:41 +02004265 msg.msg_controllen = controllen;
4266 msg.msg_control = controlbuf;
4267
4268 /* Fill in an iovec for each message part, and save the Py_buffer
4269 structs to release afterwards. */
4270 if (data_arg != NULL) {
4271 if (sock_sendmsg_iovec(self, data_arg, &msg, &databufs, &ndatabufs) == -1) {
4272 goto finally;
4273 }
4274 }
4275
4276 /* set operation to encrypt or decrypt */
4277 header = CMSG_FIRSTHDR(&msg);
4278 if (header == NULL) {
4279 PyErr_SetString(PyExc_RuntimeError,
4280 "unexpected NULL result from CMSG_FIRSTHDR");
4281 goto finally;
4282 }
4283 header->cmsg_level = SOL_ALG;
4284 header->cmsg_type = ALG_SET_OP;
4285 header->cmsg_len = CMSG_LEN(4);
4286 uiptr = (void*)CMSG_DATA(header);
4287 *uiptr = (unsigned int)op;
4288
4289 /* set initialization vector */
4290 if (iv.buf != NULL) {
4291 header = CMSG_NXTHDR(&msg, header);
4292 if (header == NULL) {
4293 PyErr_SetString(PyExc_RuntimeError,
4294 "unexpected NULL result from CMSG_NXTHDR(iv)");
4295 goto finally;
4296 }
4297 header->cmsg_level = SOL_ALG;
4298 header->cmsg_type = ALG_SET_IV;
4299 header->cmsg_len = CMSG_SPACE(sizeof(*alg_iv) + iv.len);
4300 alg_iv = (void*)CMSG_DATA(header);
4301 alg_iv->ivlen = iv.len;
4302 memcpy(alg_iv->iv, iv.buf, iv.len);
4303 }
4304
4305 /* set length of associated data for AEAD */
4306 if (assoclen >= 0) {
4307 header = CMSG_NXTHDR(&msg, header);
4308 if (header == NULL) {
4309 PyErr_SetString(PyExc_RuntimeError,
4310 "unexpected NULL result from CMSG_NXTHDR(assoc)");
4311 goto finally;
4312 }
4313 header->cmsg_level = SOL_ALG;
4314 header->cmsg_type = ALG_SET_AEAD_ASSOCLEN;
4315 header->cmsg_len = CMSG_LEN(4);
4316 uiptr = (void*)CMSG_DATA(header);
4317 *uiptr = (unsigned int)assoclen;
4318 }
4319
4320 ctx.msg = &msg;
4321 ctx.flags = flags;
Victor Stinner288b5bf2016-09-12 11:45:59 +02004322 if (sock_call(self, 1, sock_sendmsg_impl, &ctx) < 0) {
Christian Heimesdffa3942016-09-05 23:54:41 +02004323 goto finally;
Victor Stinner288b5bf2016-09-12 11:45:59 +02004324 }
Christian Heimesdffa3942016-09-05 23:54:41 +02004325
4326 retval = PyLong_FromSsize_t(ctx.result);
4327
4328 finally:
4329 PyMem_Free(controlbuf);
4330 if (iv.buf != NULL) {
4331 PyBuffer_Release(&iv);
4332 }
Victor Stinner52d61e42016-09-12 11:41:58 +02004333 PyMem_Free(msg.msg_iov);
4334 for (i = 0; i < ndatabufs; i++) {
Christian Heimesdffa3942016-09-05 23:54:41 +02004335 PyBuffer_Release(&databufs[i]);
Victor Stinner52d61e42016-09-12 11:41:58 +02004336 }
Christian Heimesdffa3942016-09-05 23:54:41 +02004337 PyMem_Free(databufs);
4338 return retval;
4339}
4340
4341PyDoc_STRVAR(sendmsg_afalg_doc,
4342"sendmsg_afalg([msg], *, op[, iv[, assoclen[, flags=MSG_MORE]]])\n\
4343\n\
4344Set operation mode, IV and length of associated data for an AF_ALG\n\
4345operation socket.");
4346#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004347
Guido van Rossum30a685f1991-06-27 15:51:29 +00004348/* s.shutdown(how) method */
4349
Guido van Rossum73624e91994-10-10 17:59:00 +00004350static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004351sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 int how;
4354 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00004355
Serhiy Storchaka78980432013-01-15 01:12:17 +02004356 how = _PyLong_AsInt(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004357 if (how == -1 && PyErr_Occurred())
4358 return NULL;
4359 Py_BEGIN_ALLOW_THREADS
4360 res = shutdown(s->sock_fd, how);
4361 Py_END_ALLOW_THREADS
4362 if (res < 0)
4363 return s->errorhandler();
4364 Py_INCREF(Py_None);
4365 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004366}
4367
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004368PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004369"shutdown(flag)\n\
4370\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00004371Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
4372of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004373
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00004374#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Christian Heimesfaf2f632008-01-06 16:59:19 +00004375static PyObject*
4376sock_ioctl(PySocketSockObject *s, PyObject *arg)
4377{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004378 unsigned long cmd = SIO_RCVALL;
4379 PyObject *argO;
4380 DWORD recv;
Christian Heimesfaf2f632008-01-06 16:59:19 +00004381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004382 if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO))
4383 return NULL;
Christian Heimesfaf2f632008-01-06 16:59:19 +00004384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004385 switch (cmd) {
4386 case SIO_RCVALL: {
4387 unsigned int option = RCVALL_ON;
4388 if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option))
4389 return NULL;
4390 if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option),
4391 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
4392 return set_error();
4393 }
4394 return PyLong_FromUnsignedLong(recv); }
4395 case SIO_KEEPALIVE_VALS: {
4396 struct tcp_keepalive ka;
4397 if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd,
4398 &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval))
4399 return NULL;
4400 if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka),
4401 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
4402 return set_error();
4403 }
4404 return PyLong_FromUnsignedLong(recv); }
Steve Dowerea93ac02016-06-17 12:52:18 -07004405#if defined(SIO_LOOPBACK_FAST_PATH)
4406 case SIO_LOOPBACK_FAST_PATH: {
4407 unsigned int option;
4408 if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option))
4409 return NULL;
4410 if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option),
4411 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
4412 return set_error();
4413 }
4414 return PyLong_FromUnsignedLong(recv); }
4415#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004416 default:
4417 PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd);
4418 return NULL;
4419 }
Christian Heimesfaf2f632008-01-06 16:59:19 +00004420}
4421PyDoc_STRVAR(sock_ioctl_doc,
4422"ioctl(cmd, option) -> long\n\
4423\n\
Kristján Valur Jónsson847ec752009-09-27 21:10:38 +00004424Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\
4425SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\
Steve Dowerea93ac02016-06-17 12:52:18 -07004426SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval).\n\
4427SIO_LOOPBACK_FAST_PATH: 'option' is a boolean value, and is disabled by default");
Kristján Valur Jónsson10f383a2012-04-07 11:23:31 +00004428#endif
4429
4430#if defined(MS_WINDOWS)
4431static PyObject*
4432sock_share(PySocketSockObject *s, PyObject *arg)
4433{
4434 WSAPROTOCOL_INFO info;
4435 DWORD processId;
4436 int result;
4437
4438 if (!PyArg_ParseTuple(arg, "I", &processId))
4439 return NULL;
4440
4441 Py_BEGIN_ALLOW_THREADS
4442 result = WSADuplicateSocket(s->sock_fd, processId, &info);
4443 Py_END_ALLOW_THREADS
4444 if (result == SOCKET_ERROR)
4445 return set_error();
4446 return PyBytes_FromStringAndSize((const char*)&info, sizeof(info));
4447}
4448PyDoc_STRVAR(sock_share_doc,
4449"share(process_id) -> bytes\n\
4450\n\
4451Share the socket with another process. The target process id\n\
4452must be provided and the resulting bytes object passed to the target\n\
4453process. There the shared socket can be instantiated by calling\n\
4454socket.fromshare().");
4455
Christian Heimesfaf2f632008-01-06 16:59:19 +00004456
4457#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00004458
4459/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004460
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004461static PyMethodDef sock_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 {"_accept", (PyCFunction)sock_accept, METH_NOARGS,
4463 accept_doc},
4464 {"bind", (PyCFunction)sock_bind, METH_O,
4465 bind_doc},
4466 {"close", (PyCFunction)sock_close, METH_NOARGS,
4467 close_doc},
4468 {"connect", (PyCFunction)sock_connect, METH_O,
4469 connect_doc},
4470 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
4471 connect_ex_doc},
Antoine Pitrou6e451df2010-08-09 20:39:54 +00004472 {"detach", (PyCFunction)sock_detach, METH_NOARGS,
4473 detach_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
4475 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00004476#ifdef HAVE_GETPEERNAME
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 {"getpeername", (PyCFunction)sock_getpeername,
4478 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00004479#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 {"getsockname", (PyCFunction)sock_getsockname,
4481 METH_NOARGS, getsockname_doc},
4482 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
4483 getsockopt_doc},
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00004484#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS,
4486 sock_ioctl_doc},
Christian Heimesfaf2f632008-01-06 16:59:19 +00004487#endif
Kristján Valur Jónsson10f383a2012-04-07 11:23:31 +00004488#if defined(MS_WINDOWS)
4489 {"share", (PyCFunction)sock_share, METH_VARARGS,
4490 sock_share_doc},
4491#endif
Charles-François Natali644b8f52014-05-22 19:45:39 +01004492 {"listen", (PyCFunction)sock_listen, METH_VARARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004493 listen_doc},
4494 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
4495 recv_doc},
4496 {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
4497 recv_into_doc},
4498 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
4499 recvfrom_doc},
4500 {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
4501 recvfrom_into_doc},
4502 {"send", (PyCFunction)sock_send, METH_VARARGS,
4503 send_doc},
4504 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
4505 sendall_doc},
4506 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
4507 sendto_doc},
4508 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
4509 setblocking_doc},
4510 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
4511 settimeout_doc},
4512 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
4513 gettimeout_doc},
4514 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
4515 setsockopt_doc},
4516 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
4517 shutdown_doc},
Nick Coghlan96fe56a2011-08-22 11:55:57 +10004518#ifdef CMSG_LEN
4519 {"recvmsg", (PyCFunction)sock_recvmsg, METH_VARARGS,
4520 recvmsg_doc},
4521 {"recvmsg_into", (PyCFunction)sock_recvmsg_into, METH_VARARGS,
4522 recvmsg_into_doc,},
4523 {"sendmsg", (PyCFunction)sock_sendmsg, METH_VARARGS,
4524 sendmsg_doc},
4525#endif
Christian Heimesdffa3942016-09-05 23:54:41 +02004526#ifdef HAVE_SOCKADDR_ALG
4527 {"sendmsg_afalg", (PyCFunction)sock_sendmsg_afalg, METH_VARARGS | METH_KEYWORDS,
4528 sendmsg_afalg_doc},
4529#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004531};
4532
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004533/* SockObject members */
4534static PyMemberDef sock_memberlist[] = {
4535 {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},
4536 {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},
4537 {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004538 {0},
4539};
Guido van Rossum30a685f1991-06-27 15:51:29 +00004540
Victor Stinner71694d52015-03-28 01:18:54 +01004541static PyGetSetDef sock_getsetlist[] = {
4542 {"timeout", (getter)sock_gettimeout, NULL, PyDoc_STR("the socket timeout")},
4543 {NULL} /* sentinel */
4544};
4545
Guido van Rossum73624e91994-10-10 17:59:00 +00004546/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00004547 First close the file description. */
4548
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004549static void
Victor Stinner19a8e842016-03-21 16:36:48 +01004550sock_finalize(PySocketSockObject *s)
4551{
4552 SOCKET_T fd;
4553 PyObject *error_type, *error_value, *error_traceback;
4554
4555 /* Save the current exception, if any. */
4556 PyErr_Fetch(&error_type, &error_value, &error_traceback);
4557
Victor Stinnerd3afb622016-07-22 17:47:09 +02004558 if (s->sock_fd != INVALID_SOCKET) {
Victor Stinner19a8e842016-03-21 16:36:48 +01004559 if (PyErr_ResourceWarning((PyObject *)s, 1, "unclosed %R", s)) {
4560 /* Spurious errors can appear at shutdown */
4561 if (PyErr_ExceptionMatches(PyExc_Warning)) {
4562 PyErr_WriteUnraisable((PyObject *)s);
4563 }
4564 }
4565
4566 /* Only close the socket *after* logging the ResourceWarning warning
4567 to allow the logger to call socket methods like
4568 socket.getsockname(). If the socket is closed before, socket
4569 methods fails with the EBADF error. */
4570 fd = s->sock_fd;
Victor Stinnerd3afb622016-07-22 17:47:09 +02004571 s->sock_fd = INVALID_SOCKET;
Victor Stinner19a8e842016-03-21 16:36:48 +01004572
4573 /* We do not want to retry upon EINTR: see sock_close() */
4574 Py_BEGIN_ALLOW_THREADS
4575 (void) SOCKETCLOSE(fd);
4576 Py_END_ALLOW_THREADS
4577 }
4578
4579 /* Restore the saved exception. */
4580 PyErr_Restore(error_type, error_value, error_traceback);
4581}
4582
4583static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004584sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004585{
Victor Stinner19a8e842016-03-21 16:36:48 +01004586 if (PyObject_CallFinalizerFromDealloc((PyObject *)s) < 0)
4587 return;
4588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 Py_TYPE(s)->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004590}
4591
Guido van Rossum30a685f1991-06-27 15:51:29 +00004592
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004593static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004594sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004595{
Victor Stinnere254e532014-07-26 14:36:55 +02004596 long sock_fd;
4597 /* On Windows, this test is needed because SOCKET_T is unsigned */
4598 if (s->sock_fd == INVALID_SOCKET) {
4599 sock_fd = -1;
4600 }
Fred Drakea04eaad2000-06-30 02:46:07 +00004601#if SIZEOF_SOCKET_T > SIZEOF_LONG
Victor Stinnere254e532014-07-26 14:36:55 +02004602 else if (s->sock_fd > LONG_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 /* this can occur on Win64, and actually there is a special
4604 ugly printf formatter for decimal pointer length integer
4605 printing, only bother if necessary*/
4606 PyErr_SetString(PyExc_OverflowError,
4607 "no printf formatter to display "
4608 "the socket descriptor in decimal");
4609 return NULL;
4610 }
Fred Drakea04eaad2000-06-30 02:46:07 +00004611#endif
Victor Stinnere254e532014-07-26 14:36:55 +02004612 else
4613 sock_fd = (long)s->sock_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004614 return PyUnicode_FromFormat(
4615 "<socket object, fd=%ld, family=%d, type=%d, proto=%d>",
Victor Stinnere254e532014-07-26 14:36:55 +02004616 sock_fd, s->sock_family,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004617 s->sock_type,
4618 s->sock_proto);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004619}
4620
4621
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004622/* Create a new, uninitialized socket object. */
4623
4624static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004625sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004627 PyObject *new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 new = type->tp_alloc(type, 0);
4630 if (new != NULL) {
Victor Stinner524714e2016-07-22 17:43:59 +02004631 ((PySocketSockObject *)new)->sock_fd = INVALID_SOCKET;
Victor Stinner9001d802015-04-06 23:06:01 +02004632 ((PySocketSockObject *)new)->sock_timeout = _PyTime_FromSeconds(-1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004633 ((PySocketSockObject *)new)->errorhandler = &set_error;
4634 }
4635 return new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004636}
4637
4638
4639/* Initialize a new socket object. */
4640
Victor Stinnerdaf45552013-08-28 00:53:59 +02004641#ifdef SOCK_CLOEXEC
4642/* socket() and socketpair() fail with EINVAL on Linux kernel older
4643 * than 2.6.27 if SOCK_CLOEXEC flag is set in the socket type. */
4644static int sock_cloexec_works = -1;
4645#endif
4646
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004647/*ARGSUSED*/
4648static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00004649sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004651 PySocketSockObject *s = (PySocketSockObject *)self;
4652 PyObject *fdobj = NULL;
4653 SOCKET_T fd = INVALID_SOCKET;
4654 int family = AF_INET, type = SOCK_STREAM, proto = 0;
4655 static char *keywords[] = {"family", "type", "proto", "fileno", 0};
Victor Stinnerdaf45552013-08-28 00:53:59 +02004656#ifndef MS_WINDOWS
4657#ifdef SOCK_CLOEXEC
4658 int *atomic_flag_works = &sock_cloexec_works;
4659#else
4660 int *atomic_flag_works = NULL;
4661#endif
4662#endif
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004664 if (!PyArg_ParseTupleAndKeywords(args, kwds,
4665 "|iiiO:socket", keywords,
4666 &family, &type, &proto, &fdobj))
4667 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 if (fdobj != NULL && fdobj != Py_None) {
Kristján Valur Jónsson10f383a2012-04-07 11:23:31 +00004670#ifdef MS_WINDOWS
4671 /* recreate a socket that was duplicated */
4672 if (PyBytes_Check(fdobj)) {
4673 WSAPROTOCOL_INFO info;
4674 if (PyBytes_GET_SIZE(fdobj) != sizeof(info)) {
4675 PyErr_Format(PyExc_ValueError,
4676 "socket descriptor string has wrong size, "
4677 "should be %zu bytes.", sizeof(info));
4678 return -1;
4679 }
4680 memcpy(&info, PyBytes_AS_STRING(fdobj), sizeof(info));
4681 Py_BEGIN_ALLOW_THREADS
4682 fd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
4683 FROM_PROTOCOL_INFO, &info, 0, WSA_FLAG_OVERLAPPED);
4684 Py_END_ALLOW_THREADS
4685 if (fd == INVALID_SOCKET) {
4686 set_error();
4687 return -1;
4688 }
4689 family = info.iAddressFamily;
4690 type = info.iSocketType;
4691 proto = info.iProtocol;
4692 }
4693 else
4694#endif
4695 {
4696 fd = PyLong_AsSocket_t(fdobj);
4697 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
4698 return -1;
4699 if (fd == INVALID_SOCKET) {
4700 PyErr_SetString(PyExc_ValueError,
4701 "can't use invalid socket value");
4702 return -1;
4703 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004704 }
4705 }
4706 else {
Victor Stinnerdaf45552013-08-28 00:53:59 +02004707#ifdef MS_WINDOWS
4708 /* Windows implementation */
4709#ifndef WSA_FLAG_NO_HANDLE_INHERIT
4710#define WSA_FLAG_NO_HANDLE_INHERIT 0x80
4711#endif
4712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004713 Py_BEGIN_ALLOW_THREADS
Victor Stinnerdaf45552013-08-28 00:53:59 +02004714 if (support_wsa_no_inherit) {
4715 fd = WSASocket(family, type, proto,
4716 NULL, 0,
4717 WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT);
4718 if (fd == INVALID_SOCKET) {
4719 /* Windows 7 or Windows 2008 R2 without SP1 or the hotfix */
4720 support_wsa_no_inherit = 0;
4721 fd = socket(family, type, proto);
4722 }
4723 }
4724 else {
4725 fd = socket(family, type, proto);
4726 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004727 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00004728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004729 if (fd == INVALID_SOCKET) {
4730 set_error();
4731 return -1;
4732 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02004733
4734 if (!support_wsa_no_inherit) {
4735 if (!SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0)) {
4736 closesocket(fd);
4737 PyErr_SetFromWindowsErr(0);
4738 return -1;
4739 }
4740 }
4741#else
4742 /* UNIX */
4743 Py_BEGIN_ALLOW_THREADS
4744#ifdef SOCK_CLOEXEC
4745 if (sock_cloexec_works != 0) {
4746 fd = socket(family, type | SOCK_CLOEXEC, proto);
4747 if (sock_cloexec_works == -1) {
4748 if (fd >= 0) {
4749 sock_cloexec_works = 1;
4750 }
4751 else if (errno == EINVAL) {
4752 /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */
4753 sock_cloexec_works = 0;
4754 fd = socket(family, type, proto);
4755 }
4756 }
4757 }
4758 else
4759#endif
4760 {
4761 fd = socket(family, type, proto);
4762 }
4763 Py_END_ALLOW_THREADS
4764
4765 if (fd == INVALID_SOCKET) {
4766 set_error();
4767 return -1;
4768 }
4769
4770 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
4771 SOCKETCLOSE(fd);
4772 return -1;
4773 }
4774#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004775 }
Christian Heimes0b9d64e2016-09-09 00:28:57 +02004776 if (init_sockobject(s, fd, family, type, proto) == -1) {
4777 SOCKETCLOSE(fd);
4778 return -1;
4779 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004781 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004782
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004783}
4784
4785
Guido van Rossumb6775db1994-08-01 11:34:53 +00004786/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00004787
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004788static PyTypeObject sock_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004789 PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
4790 "_socket.socket", /* tp_name */
4791 sizeof(PySocketSockObject), /* tp_basicsize */
4792 0, /* tp_itemsize */
4793 (destructor)sock_dealloc, /* tp_dealloc */
4794 0, /* tp_print */
4795 0, /* tp_getattr */
4796 0, /* tp_setattr */
4797 0, /* tp_reserved */
4798 (reprfunc)sock_repr, /* tp_repr */
4799 0, /* tp_as_number */
4800 0, /* tp_as_sequence */
4801 0, /* tp_as_mapping */
4802 0, /* tp_hash */
4803 0, /* tp_call */
4804 0, /* tp_str */
4805 PyObject_GenericGetAttr, /* tp_getattro */
4806 0, /* tp_setattro */
4807 0, /* tp_as_buffer */
Victor Stinner19a8e842016-03-21 16:36:48 +01004808 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
4809 | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004810 sock_doc, /* tp_doc */
4811 0, /* tp_traverse */
4812 0, /* tp_clear */
4813 0, /* tp_richcompare */
4814 0, /* tp_weaklistoffset */
4815 0, /* tp_iter */
4816 0, /* tp_iternext */
4817 sock_methods, /* tp_methods */
4818 sock_memberlist, /* tp_members */
Victor Stinner71694d52015-03-28 01:18:54 +01004819 sock_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004820 0, /* tp_base */
4821 0, /* tp_dict */
4822 0, /* tp_descr_get */
4823 0, /* tp_descr_set */
4824 0, /* tp_dictoffset */
4825 sock_initobj, /* tp_init */
4826 PyType_GenericAlloc, /* tp_alloc */
4827 sock_new, /* tp_new */
4828 PyObject_Del, /* tp_free */
Victor Stinner19a8e842016-03-21 16:36:48 +01004829 0, /* tp_is_gc */
4830 0, /* tp_bases */
4831 0, /* tp_mro */
4832 0, /* tp_cache */
4833 0, /* tp_subclasses */
4834 0, /* tp_weaklist */
4835 0, /* tp_del */
4836 0, /* tp_version_tag */
4837 (destructor)sock_finalize, /* tp_finalize */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004838};
4839
Guido van Rossum30a685f1991-06-27 15:51:29 +00004840
Guido van Rossum81194471991-07-27 21:42:02 +00004841/* Python interface to gethostname(). */
4842
4843/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00004844static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004845socket_gethostname(PyObject *self, PyObject *unused)
Guido van Rossum81194471991-07-27 21:42:02 +00004846{
Martin v. Löwis72f48422010-10-29 18:20:08 +00004847#ifdef MS_WINDOWS
4848 /* Don't use winsock's gethostname, as this returns the ANSI
4849 version of the hostname, whereas we need a Unicode string.
4850 Otherwise, gethostname apparently also returns the DNS name. */
Hirokazu Yamamoto09fff7a2010-11-05 17:24:13 +00004851 wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1];
Victor Stinner63941882011-09-29 00:42:28 +02004852 DWORD size = Py_ARRAY_LENGTH(buf);
Victor Stinner74168972011-11-17 01:11:36 +01004853 wchar_t *name;
Hirokazu Yamamoto09fff7a2010-11-05 17:24:13 +00004854 PyObject *result;
Victor Stinner74168972011-11-17 01:11:36 +01004855
4856 if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size))
Victor Stinner9d3b93b2011-11-22 02:27:30 +01004857 return PyUnicode_FromWideChar(buf, size);
Victor Stinner74168972011-11-17 01:11:36 +01004858
4859 if (GetLastError() != ERROR_MORE_DATA)
4860 return PyErr_SetFromWindowsErr(0);
4861
4862 if (size == 0)
4863 return PyUnicode_New(0, 0);
4864
4865 /* MSDN says ERROR_MORE_DATA may occur because DNS allows longer
4866 names */
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02004867 name = PyMem_New(wchar_t, size);
4868 if (!name) {
4869 PyErr_NoMemory();
Victor Stinner74168972011-11-17 01:11:36 +01004870 return NULL;
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02004871 }
Victor Stinner74168972011-11-17 01:11:36 +01004872 if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname,
4873 name,
4874 &size))
4875 {
4876 PyMem_Free(name);
4877 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis72f48422010-10-29 18:20:08 +00004878 }
Victor Stinner74168972011-11-17 01:11:36 +01004879
4880 result = PyUnicode_FromWideChar(name, size);
4881 PyMem_Free(name);
4882 return result;
Martin v. Löwis72f48422010-10-29 18:20:08 +00004883#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004884 char buf[1024];
4885 int res;
4886 Py_BEGIN_ALLOW_THREADS
4887 res = gethostname(buf, (int) sizeof buf - 1);
4888 Py_END_ALLOW_THREADS
4889 if (res < 0)
4890 return set_error();
4891 buf[sizeof buf - 1] = '\0';
Victor Stinnera534fc42013-06-03 22:07:27 +02004892 return PyUnicode_DecodeFSDefault(buf);
Martin v. Löwis72f48422010-10-29 18:20:08 +00004893#endif
Guido van Rossum81194471991-07-27 21:42:02 +00004894}
Guido van Rossumff4949e1992-08-05 19:58:53 +00004895
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004896PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004897"gethostname() -> string\n\
4898\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004899Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004900
Antoine Pitrou061cfb52011-02-28 22:25:22 +00004901#ifdef HAVE_SETHOSTNAME
4902PyDoc_STRVAR(sethostname_doc,
4903"sethostname(name)\n\n\
4904Sets the hostname to name.");
4905
4906static PyObject *
4907socket_sethostname(PyObject *self, PyObject *args)
4908{
4909 PyObject *hnobj;
4910 Py_buffer buf;
4911 int res, flag = 0;
4912
Christian Heimesd2774c72013-06-19 02:06:29 +02004913#ifdef _AIX
4914/* issue #18259, not declared in any useful header file */
4915extern int sethostname(const char *, size_t);
4916#endif
4917
Antoine Pitrou061cfb52011-02-28 22:25:22 +00004918 if (!PyArg_ParseTuple(args, "S:sethostname", &hnobj)) {
4919 PyErr_Clear();
4920 if (!PyArg_ParseTuple(args, "O&:sethostname",
4921 PyUnicode_FSConverter, &hnobj))
4922 return NULL;
4923 flag = 1;
4924 }
4925 res = PyObject_GetBuffer(hnobj, &buf, PyBUF_SIMPLE);
4926 if (!res) {
4927 res = sethostname(buf.buf, buf.len);
4928 PyBuffer_Release(&buf);
4929 }
4930 if (flag)
4931 Py_DECREF(hnobj);
4932 if (res)
4933 return set_error();
4934 Py_RETURN_NONE;
4935}
4936#endif
Guido van Rossumff4949e1992-08-05 19:58:53 +00004937
Guido van Rossum30a685f1991-06-27 15:51:29 +00004938/* Python interface to gethostbyname(name). */
4939
4940/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00004941static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004942socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00004943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004944 char *name;
4945 sock_addr_t addrbuf;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004946 PyObject *ret = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004947
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004948 if (!PyArg_ParseTuple(args, "et:gethostbyname", "idna", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004949 return NULL;
4950 if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004951 goto finally;
4952 ret = makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
4953finally:
4954 PyMem_Free(name);
4955 return ret;
Guido van Rossum30a685f1991-06-27 15:51:29 +00004956}
4957
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004958PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004959"gethostbyname(host) -> address\n\
4960\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004961Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004962
4963
Victor Stinner72400302016-01-28 15:41:01 +01004964static PyObject*
4965sock_decode_hostname(const char *name)
4966{
4967#ifdef MS_WINDOWS
4968 /* Issue #26227: gethostbyaddr() returns a string encoded
4969 * to the ANSI code page */
4970 return PyUnicode_DecodeFSDefault(name);
4971#else
4972 /* Decode from UTF-8 */
4973 return PyUnicode_FromString(name);
4974#endif
4975}
4976
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004977/* Convenience function common to gethostbyname_ex and gethostbyaddr */
4978
4979static PyObject *
Victor Stinner1a62a682014-08-17 19:33:28 +02004980gethost_common(struct hostent *h, struct sockaddr *addr, size_t alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004981{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004982 char **pch;
4983 PyObject *rtn_tuple = (PyObject *)NULL;
4984 PyObject *name_list = (PyObject *)NULL;
4985 PyObject *addr_list = (PyObject *)NULL;
4986 PyObject *tmp;
Victor Stinner72400302016-01-28 15:41:01 +01004987 PyObject *name;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004989 if (h == NULL) {
4990 /* Let's get real error message to return */
4991 set_herror(h_errno);
4992 return NULL;
4993 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004995 if (h->h_addrtype != af) {
4996 /* Let's get real error message to return */
Victor Stinner1f33f2b2011-12-17 04:45:09 +01004997 errno = EAFNOSUPPORT;
4998 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004999 return NULL;
5000 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00005001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005002 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00005003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005004 case AF_INET:
5005 if (alen < sizeof(struct sockaddr_in))
5006 return NULL;
5007 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00005008
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00005009#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005010 case AF_INET6:
5011 if (alen < sizeof(struct sockaddr_in6))
5012 return NULL;
5013 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005014#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00005015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005016 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00005017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005018 if ((name_list = PyList_New(0)) == NULL)
5019 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00005020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005021 if ((addr_list = PyList_New(0)) == NULL)
5022 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00005023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005024 /* SF #1511317: h_aliases can be NULL */
5025 if (h->h_aliases) {
5026 for (pch = h->h_aliases; *pch != NULL; pch++) {
5027 int status;
5028 tmp = PyUnicode_FromString(*pch);
5029 if (tmp == NULL)
5030 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00005031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005032 status = PyList_Append(name_list, tmp);
5033 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00005034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005035 if (status)
5036 goto err;
5037 }
5038 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00005039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005040 for (pch = h->h_addr_list; *pch != NULL; pch++) {
5041 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00005042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005043 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00005044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005045 case AF_INET:
5046 {
5047 struct sockaddr_in sin;
5048 memset(&sin, 0, sizeof(sin));
5049 sin.sin_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005050#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005051 sin.sin_len = sizeof(sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005052#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005053 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
5054 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00005055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005056 if (pch == h->h_addr_list && alen >= sizeof(sin))
5057 memcpy((char *) addr, &sin, sizeof(sin));
5058 break;
5059 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00005060
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00005061#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005062 case AF_INET6:
5063 {
5064 struct sockaddr_in6 sin6;
5065 memset(&sin6, 0, sizeof(sin6));
5066 sin6.sin6_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005067#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005068 sin6.sin6_len = sizeof(sin6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005069#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005070 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
5071 tmp = makeipaddr((struct sockaddr *)&sin6,
5072 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00005073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005074 if (pch == h->h_addr_list && alen >= sizeof(sin6))
5075 memcpy((char *) addr, &sin6, sizeof(sin6));
5076 break;
5077 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005078#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00005079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005080 default: /* can't happen */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005081 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005082 "unsupported address family");
5083 return NULL;
5084 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00005085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005086 if (tmp == NULL)
5087 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00005088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005089 status = PyList_Append(addr_list, tmp);
5090 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00005091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005092 if (status)
5093 goto err;
5094 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00005095
Victor Stinner72400302016-01-28 15:41:01 +01005096 name = sock_decode_hostname(h->h_name);
5097 if (name == NULL)
5098 goto err;
5099 rtn_tuple = Py_BuildValue("NOO", name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00005100
Guido van Rossum7d896ab1998-08-04 22:16:43 +00005101 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005102 Py_XDECREF(name_list);
5103 Py_XDECREF(addr_list);
5104 return rtn_tuple;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00005105}
5106
5107
5108/* Python interface to gethostbyname_ex(name). */
5109
5110/*ARGSUSED*/
5111static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005112socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00005113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005114 char *name;
5115 struct hostent *h;
Charles-François Natali8b759652011-12-23 16:44:51 +01005116 sock_addr_t addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005117 struct sockaddr *sa;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00005118 PyObject *ret = NULL;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00005119#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005120 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00005121#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005122 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00005123#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005124 char buf[16384];
5125 int buf_len = (sizeof buf) - 1;
5126 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00005127#endif
Ross Lagerwall8c159762012-03-06 21:36:18 +02005128#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005129 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00005130#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00005131#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005132
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00005133 if (!PyArg_ParseTuple(args, "et:gethostbyname_ex", "idna", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005134 return NULL;
Charles-François Natali8b759652011-12-23 16:44:51 +01005135 if (setipaddr(name, SAS2SA(&addr), sizeof(addr), AF_INET) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00005136 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005137 Py_BEGIN_ALLOW_THREADS
Guido van Rossum7d896ab1998-08-04 22:16:43 +00005138#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00005139#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Ross Lagerwall8c159762012-03-06 21:36:18 +02005140 gethostbyname_r(name, &hp_allocated, buf, buf_len,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005141 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00005142#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005143 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00005144#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005145 memset((void *) &data, '\0', sizeof(data));
5146 result = gethostbyname_r(name, &hp_allocated, &data);
5147 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00005148#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00005149#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00005150#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005151 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00005152#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005153 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00005154#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005155 Py_END_ALLOW_THREADS
5156 /* Some C libraries would require addr.__ss_family instead of
5157 addr.ss_family.
5158 Therefore, we cast the sockaddr_storage into sockaddr to
5159 access sa_family. */
Charles-François Natali8b759652011-12-23 16:44:51 +01005160 sa = SAS2SA(&addr);
5161 ret = gethost_common(h, SAS2SA(&addr), sizeof(addr),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005162 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00005163#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005164 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00005165#endif
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00005166finally:
5167 PyMem_Free(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005168 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00005169}
5170
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005171PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00005172"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
5173\n\
5174Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005175for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00005176
5177
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00005178/* Python interface to gethostbyaddr(IP). */
5179
5180/*ARGSUSED*/
5181static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005182socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00005183{
Charles-François Natali8b759652011-12-23 16:44:51 +01005184 sock_addr_t addr;
5185 struct sockaddr *sa = SAS2SA(&addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005186 char *ip_num;
5187 struct hostent *h;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00005188 PyObject *ret = NULL;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00005189#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005190 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00005191#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005192 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00005193#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005194 /* glibcs up to 2.10 assume that the buf argument to
5195 gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc
5196 does not ensure. The attribute below instructs the compiler
5197 to maintain this alignment. */
5198 char buf[16384] Py_ALIGNED(8);
5199 int buf_len = (sizeof buf) - 1;
5200 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00005201#endif
Ross Lagerwall8c159762012-03-06 21:36:18 +02005202#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005203 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00005204#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00005205#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005206 char *ap;
5207 int al;
5208 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00005209
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00005210 if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005211 return NULL;
5212 af = AF_UNSPEC;
5213 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00005214 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005215 af = sa->sa_family;
5216 ap = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00005217 /* al = 0; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005218 switch (af) {
5219 case AF_INET:
5220 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
5221 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
5222 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00005223#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005224 case AF_INET6:
5225 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
5226 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
5227 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005228#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005229 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005230 PyErr_SetString(PyExc_OSError, "unsupported address family");
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00005231 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005232 }
5233 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4f199ea1998-04-09 20:56:35 +00005234#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00005235#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Ross Lagerwall8c159762012-03-06 21:36:18 +02005236 gethostbyaddr_r(ap, al, af,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005237 &hp_allocated, buf, buf_len,
5238 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00005239#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005240 h = gethostbyaddr_r(ap, al, af,
5241 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00005242#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005243 memset((void *) &data, '\0', sizeof(data));
5244 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
5245 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00005246#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00005247#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00005248#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005249 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00005250#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005251 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00005252#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005253 Py_END_ALLOW_THREADS
Charles-François Natali8b759652011-12-23 16:44:51 +01005254 ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00005255#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005256 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00005257#endif
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00005258finally:
5259 PyMem_Free(ip_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005260 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00005261}
5262
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005263PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00005264"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
5265\n\
5266Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005267for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00005268
Guido van Rossum30a685f1991-06-27 15:51:29 +00005269
5270/* Python interface to getservbyname(name).
5271 This only returns the port number, since the other info is already
5272 known or not useful (like the list of aliases). */
5273
5274/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00005275static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005276socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00005277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005278 char *name, *proto=NULL;
5279 struct servent *sp;
5280 if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
5281 return NULL;
5282 Py_BEGIN_ALLOW_THREADS
5283 sp = getservbyname(name, proto);
5284 Py_END_ALLOW_THREADS
5285 if (sp == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005286 PyErr_SetString(PyExc_OSError, "service/proto not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005287 return NULL;
5288 }
5289 return PyLong_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00005290}
5291
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005292PyDoc_STRVAR(getservbyname_doc,
Barry Warsaw11b91a02004-06-28 00:50:43 +00005293"getservbyname(servicename[, protocolname]) -> integer\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00005294\n\
5295Return a port number from a service name and protocol name.\n\
Barry Warsaw11b91a02004-06-28 00:50:43 +00005296The optional protocol name, if given, should be 'tcp' or 'udp',\n\
5297otherwise any protocol will match.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00005298
Guido van Rossum30a685f1991-06-27 15:51:29 +00005299
Barry Warsaw11b91a02004-06-28 00:50:43 +00005300/* Python interface to getservbyport(port).
5301 This only returns the service name, since the other info is already
5302 known or not useful (like the list of aliases). */
5303
5304/*ARGSUSED*/
5305static PyObject *
5306socket_getservbyport(PyObject *self, PyObject *args)
5307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005308 int port;
5309 char *proto=NULL;
5310 struct servent *sp;
5311 if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto))
5312 return NULL;
5313 if (port < 0 || port > 0xffff) {
5314 PyErr_SetString(
5315 PyExc_OverflowError,
5316 "getservbyport: port must be 0-65535.");
5317 return NULL;
5318 }
5319 Py_BEGIN_ALLOW_THREADS
5320 sp = getservbyport(htons((short)port), proto);
5321 Py_END_ALLOW_THREADS
5322 if (sp == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005323 PyErr_SetString(PyExc_OSError, "port/proto not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005324 return NULL;
5325 }
5326 return PyUnicode_FromString(sp->s_name);
Barry Warsaw11b91a02004-06-28 00:50:43 +00005327}
5328
5329PyDoc_STRVAR(getservbyport_doc,
5330"getservbyport(port[, protocolname]) -> string\n\
5331\n\
5332Return the service name from a port number and protocol name.\n\
5333The optional protocol name, if given, should be 'tcp' or 'udp',\n\
5334otherwise any protocol will match.");
5335
Guido van Rossum3901d851996-12-19 16:35:04 +00005336/* Python interface to getprotobyname(name).
5337 This only returns the protocol number, since the other info is
5338 already known or not useful (like the list of aliases). */
5339
5340/*ARGSUSED*/
5341static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005342socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00005343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005344 char *name;
5345 struct protoent *sp;
5346 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
5347 return NULL;
5348 Py_BEGIN_ALLOW_THREADS
5349 sp = getprotobyname(name);
5350 Py_END_ALLOW_THREADS
5351 if (sp == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005352 PyErr_SetString(PyExc_OSError, "protocol not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005353 return NULL;
5354 }
5355 return PyLong_FromLong((long) sp->p_proto);
Guido van Rossum3901d851996-12-19 16:35:04 +00005356}
5357
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005358PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00005359"getprotobyname(name) -> integer\n\
5360\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005361Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00005362
Guido van Rossum3901d851996-12-19 16:35:04 +00005363
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005364#ifndef NO_DUP
5365/* dup() function for socket fds */
5366
5367static PyObject *
5368socket_dup(PyObject *self, PyObject *fdobj)
5369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005370 SOCKET_T fd, newfd;
5371 PyObject *newfdobj;
Victor Stinnerdaf45552013-08-28 00:53:59 +02005372#ifdef MS_WINDOWS
5373 WSAPROTOCOL_INFO info;
5374#endif
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005376 fd = PyLong_AsSocket_t(fdobj);
5377 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
5378 return NULL;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005379
Victor Stinnerdaf45552013-08-28 00:53:59 +02005380#ifdef MS_WINDOWS
5381 if (WSADuplicateSocket(fd, GetCurrentProcessId(), &info))
5382 return set_error();
5383
5384 newfd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
5385 FROM_PROTOCOL_INFO,
5386 &info, 0, WSA_FLAG_OVERLAPPED);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005387 if (newfd == INVALID_SOCKET)
5388 return set_error();
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005389
Victor Stinnerdaf45552013-08-28 00:53:59 +02005390 if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) {
5391 closesocket(newfd);
5392 PyErr_SetFromWindowsErr(0);
5393 return NULL;
5394 }
5395#else
5396 /* On UNIX, dup can be used to duplicate the file descriptor of a socket */
5397 newfd = _Py_dup(fd);
5398 if (newfd == INVALID_SOCKET)
Victor Stinnere990c6e2013-11-16 00:18:58 +01005399 return NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02005400#endif
5401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005402 newfdobj = PyLong_FromSocket_t(newfd);
5403 if (newfdobj == NULL)
5404 SOCKETCLOSE(newfd);
5405 return newfdobj;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005406}
5407
5408PyDoc_STRVAR(dup_doc,
5409"dup(integer) -> integer\n\
5410\n\
5411Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\
5412sockets; on some platforms os.dup() won't work for socket file descriptors.");
5413#endif
5414
5415
Dave Cole331708b2004-08-09 04:51:41 +00005416#ifdef HAVE_SOCKETPAIR
5417/* Create a pair of sockets using the socketpair() function.
Dave Cole07fda7e2004-08-23 05:16:23 +00005418 Arguments as for socket() except the default family is AF_UNIX if
Dave Colee8bbfe42004-08-26 00:51:16 +00005419 defined on the platform; otherwise, the default is AF_INET. */
Dave Cole331708b2004-08-09 04:51:41 +00005420
5421/*ARGSUSED*/
5422static PyObject *
5423socket_socketpair(PyObject *self, PyObject *args)
5424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005425 PySocketSockObject *s0 = NULL, *s1 = NULL;
5426 SOCKET_T sv[2];
5427 int family, type = SOCK_STREAM, proto = 0;
5428 PyObject *res = NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02005429#ifdef SOCK_CLOEXEC
5430 int *atomic_flag_works = &sock_cloexec_works;
5431#else
5432 int *atomic_flag_works = NULL;
5433#endif
5434 int ret;
Dave Cole331708b2004-08-09 04:51:41 +00005435
5436#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005437 family = AF_UNIX;
Dave Cole331708b2004-08-09 04:51:41 +00005438#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005439 family = AF_INET;
Dave Cole331708b2004-08-09 04:51:41 +00005440#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005441 if (!PyArg_ParseTuple(args, "|iii:socketpair",
5442 &family, &type, &proto))
5443 return NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02005444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005445 /* Create a pair of socket fds */
Victor Stinnerdaf45552013-08-28 00:53:59 +02005446 Py_BEGIN_ALLOW_THREADS
5447#ifdef SOCK_CLOEXEC
5448 if (sock_cloexec_works != 0) {
5449 ret = socketpair(family, type | SOCK_CLOEXEC, proto, sv);
5450 if (sock_cloexec_works == -1) {
5451 if (ret >= 0) {
5452 sock_cloexec_works = 1;
5453 }
5454 else if (errno == EINVAL) {
5455 /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */
5456 sock_cloexec_works = 0;
5457 ret = socketpair(family, type, proto, sv);
5458 }
5459 }
5460 }
5461 else
5462#endif
5463 {
5464 ret = socketpair(family, type, proto, sv);
5465 }
5466 Py_END_ALLOW_THREADS
5467
5468 if (ret < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005469 return set_error();
Victor Stinnerdaf45552013-08-28 00:53:59 +02005470
5471 if (_Py_set_inheritable(sv[0], 0, atomic_flag_works) < 0)
5472 goto finally;
5473 if (_Py_set_inheritable(sv[1], 0, atomic_flag_works) < 0)
5474 goto finally;
5475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005476 s0 = new_sockobject(sv[0], family, type, proto);
5477 if (s0 == NULL)
5478 goto finally;
5479 s1 = new_sockobject(sv[1], family, type, proto);
5480 if (s1 == NULL)
5481 goto finally;
5482 res = PyTuple_Pack(2, s0, s1);
Dave Cole331708b2004-08-09 04:51:41 +00005483
5484finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005485 if (res == NULL) {
5486 if (s0 == NULL)
5487 SOCKETCLOSE(sv[0]);
5488 if (s1 == NULL)
5489 SOCKETCLOSE(sv[1]);
5490 }
5491 Py_XDECREF(s0);
5492 Py_XDECREF(s1);
5493 return res;
Dave Cole331708b2004-08-09 04:51:41 +00005494}
5495
5496PyDoc_STRVAR(socketpair_doc,
Victor Stinnerdaf45552013-08-28 00:53:59 +02005497"socketpair([family[, type [, proto]]]) -> (socket object, socket object)\n\
Dave Cole331708b2004-08-09 04:51:41 +00005498\n\
5499Create a pair of socket objects from the sockets returned by the platform\n\
5500socketpair() function.\n\
Dave Cole07fda7e2004-08-23 05:16:23 +00005501The arguments are the same as for socket() except the default family is\n\
Dave Colee8bbfe42004-08-26 00:51:16 +00005502AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
Dave Cole331708b2004-08-09 04:51:41 +00005503
5504#endif /* HAVE_SOCKETPAIR */
5505
5506
Guido van Rossum006bf911996-06-12 04:04:55 +00005507static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005508socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00005509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005510 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00005511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005512 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
5513 return NULL;
5514 }
5515 if (x1 < 0) {
5516 PyErr_SetString(PyExc_OverflowError,
5517 "can't convert negative number to unsigned long");
5518 return NULL;
5519 }
5520 x2 = (unsigned int)ntohs((unsigned short)x1);
5521 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00005522}
5523
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005524PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00005525"ntohs(integer) -> integer\n\
5526\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005527Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00005528
5529
Guido van Rossum006bf911996-06-12 04:04:55 +00005530static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00005531socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00005532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005533 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00005534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005535 if (PyLong_Check(arg)) {
5536 x = PyLong_AsUnsignedLong(arg);
5537 if (x == (unsigned long) -1 && PyErr_Occurred())
5538 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00005539#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005540 {
5541 unsigned long y;
5542 /* only want the trailing 32 bits */
5543 y = x & 0xFFFFFFFFUL;
5544 if (y ^ x)
5545 return PyErr_Format(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03005546 "int larger than 32 bits");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005547 x = y;
5548 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00005549#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005550 }
5551 else
5552 return PyErr_Format(PyExc_TypeError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03005553 "expected int, %s found",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005554 Py_TYPE(arg)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005555 return PyLong_FromUnsignedLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00005556}
5557
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005558PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00005559"ntohl(integer) -> integer\n\
5560\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005561Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00005562
5563
Guido van Rossum006bf911996-06-12 04:04:55 +00005564static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005565socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00005566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005567 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00005568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005569 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
5570 return NULL;
5571 }
5572 if (x1 < 0) {
5573 PyErr_SetString(PyExc_OverflowError,
5574 "can't convert negative number to unsigned long");
5575 return NULL;
5576 }
5577 x2 = (unsigned int)htons((unsigned short)x1);
5578 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00005579}
5580
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005581PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00005582"htons(integer) -> integer\n\
5583\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005584Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00005585
5586
Guido van Rossum006bf911996-06-12 04:04:55 +00005587static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00005588socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00005589{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005590 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00005591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005592 if (PyLong_Check(arg)) {
5593 x = PyLong_AsUnsignedLong(arg);
5594 if (x == (unsigned long) -1 && PyErr_Occurred())
5595 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00005596#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005597 {
5598 unsigned long y;
5599 /* only want the trailing 32 bits */
5600 y = x & 0xFFFFFFFFUL;
5601 if (y ^ x)
5602 return PyErr_Format(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03005603 "int larger than 32 bits");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005604 x = y;
5605 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00005606#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005607 }
5608 else
5609 return PyErr_Format(PyExc_TypeError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03005610 "expected int, %s found",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005611 Py_TYPE(arg)->tp_name);
5612 return PyLong_FromUnsignedLong(htonl((unsigned long)x));
Guido van Rossum006bf911996-06-12 04:04:55 +00005613}
5614
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005615PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00005616"htonl(integer) -> integer\n\
5617\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005618Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00005619
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005620/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005621
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005622PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum7d0a8262007-05-21 23:13:11 +00005623"inet_aton(string) -> bytes giving packed 32-bit IP representation\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005624\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00005625Convert 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 +00005626binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005627
5628static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005629socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005630{
Neal Norwitz88f115b2003-02-13 02:15:42 +00005631#ifdef HAVE_INET_ATON
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005632 struct in_addr buf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00005633#endif
5634
5635#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
Benjamin Petersonf91df042009-02-13 02:50:59 +00005636#if (SIZEOF_INT != 4)
5637#error "Not sure if in_addr_t exists and int is not 32-bits."
5638#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005639 /* Have to use inet_addr() instead */
5640 unsigned int packed_addr;
Tim Peters1df9fdd2003-02-13 03:13:40 +00005641#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005642 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00005643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005644 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
5645 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00005646
Tim Peters1df9fdd2003-02-13 03:13:40 +00005647
5648#ifdef HAVE_INET_ATON
Thomas Wouters477c8d52006-05-27 19:21:47 +00005649
5650#ifdef USE_INET_ATON_WEAKLINK
5651 if (inet_aton != NULL) {
5652#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005653 if (inet_aton(ip_addr, &buf))
5654 return PyBytes_FromStringAndSize((char *)(&buf),
5655 sizeof(buf));
Guido van Rossumad05cdf2003-02-12 23:08:22 +00005656
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005657 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005658 "illegal IP address string passed to inet_aton");
5659 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00005660
Thomas Wouters477c8d52006-05-27 19:21:47 +00005661#ifdef USE_INET_ATON_WEAKLINK
5662 } else {
5663#endif
5664
5665#endif
5666
5667#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
5668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005669 /* special-case this address as inet_addr might return INADDR_NONE
5670 * for this */
5671 if (strcmp(ip_addr, "255.255.255.255") == 0) {
Charles-François Natali24aa0412013-08-31 14:48:25 +02005672 packed_addr = INADDR_BROADCAST;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005673 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00005674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005675 packed_addr = inet_addr(ip_addr);
Guido van Rossumc6a164b1999-08-20 19:11:27 +00005676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005677 if (packed_addr == INADDR_NONE) { /* invalid address */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005678 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005679 "illegal IP address string passed to inet_aton");
5680 return NULL;
5681 }
5682 }
5683 return PyBytes_FromStringAndSize((char *) &packed_addr,
5684 sizeof(packed_addr));
Thomas Wouters477c8d52006-05-27 19:21:47 +00005685
5686#ifdef USE_INET_ATON_WEAKLINK
5687 }
5688#endif
5689
Guido van Rossumad05cdf2003-02-12 23:08:22 +00005690#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005691}
5692
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005693PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00005694"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005695\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005696Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005697
5698static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005699socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005700{
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005701 Py_buffer packed_ip;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005702 struct in_addr packed_addr;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005703
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005704 if (!PyArg_ParseTuple(args, "y*:inet_ntoa", &packed_ip)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005705 return NULL;
5706 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00005707
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005708 if (packed_ip.len != sizeof(packed_addr)) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005709 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005710 "packed IP wrong length for inet_ntoa");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005711 PyBuffer_Release(&packed_ip);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005712 return NULL;
5713 }
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005714
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005715 memcpy(&packed_addr, packed_ip.buf, packed_ip.len);
5716 PyBuffer_Release(&packed_ip);
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005718 return PyUnicode_FromString(inet_ntoa(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005719}
Guido van Rossum82a5c661998-07-07 20:45:43 +00005720
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005721#if defined(HAVE_INET_PTON) || defined(MS_WINDOWS)
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005722
5723PyDoc_STRVAR(inet_pton_doc,
5724"inet_pton(af, ip) -> packed IP address string\n\
5725\n\
5726Convert an IP address from string format to a packed string suitable\n\
5727for use with low-level network functions.");
5728
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005729#endif
5730
5731#ifdef HAVE_INET_PTON
5732
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005733static PyObject *
5734socket_inet_pton(PyObject *self, PyObject *args)
5735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005736 int af;
5737 char* ip;
5738 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005739#ifdef ENABLE_IPV6
Victor Stinner640c35c2013-06-04 23:14:37 +02005740 char packed[Py_MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005741#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005742 char packed[sizeof(struct in_addr)];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005743#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005744 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
5745 return NULL;
5746 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005747
Martin v. Löwis04697e82004-06-02 12:35:29 +00005748#if !defined(ENABLE_IPV6) && defined(AF_INET6)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005749 if(af == AF_INET6) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005750 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005751 "can't use AF_INET6, IPv6 is disabled");
5752 return NULL;
5753 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00005754#endif
Martin v. Löwis10649092003-08-05 06:25:06 +00005755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005756 retval = inet_pton(af, ip, packed);
5757 if (retval < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005758 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005759 return NULL;
5760 } else if (retval == 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005761 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005762 "illegal IP address string passed to inet_pton");
5763 return NULL;
5764 } else if (af == AF_INET) {
5765 return PyBytes_FromStringAndSize(packed,
5766 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005767#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005768 } else if (af == AF_INET6) {
5769 return PyBytes_FromStringAndSize(packed,
5770 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005771#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005772 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005773 PyErr_SetString(PyExc_OSError, "unknown address family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005774 return NULL;
5775 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005776}
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005777#elif defined(MS_WINDOWS)
5778
5779static PyObject *
5780socket_inet_pton(PyObject *self, PyObject *args)
5781{
5782 int af;
5783 char* ip;
5784 struct sockaddr_in6 addr;
5785 INT ret, size;
5786
5787 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
5788 return NULL;
5789 }
5790
Victor Stinnere990c6e2013-11-16 00:18:58 +01005791 size = sizeof(addr);
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005792 ret = WSAStringToAddressA(ip, af, NULL, (LPSOCKADDR)&addr, &size);
5793
5794 if (ret) {
5795 PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
5796 return NULL;
5797 } else if(af == AF_INET) {
5798 struct sockaddr_in *addr4 = (struct sockaddr_in*)&addr;
Victor Stinnere990c6e2013-11-16 00:18:58 +01005799 return PyBytes_FromStringAndSize((const char *)&(addr4->sin_addr),
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005800 sizeof(addr4->sin_addr));
5801 } else if (af == AF_INET6) {
Victor Stinnere990c6e2013-11-16 00:18:58 +01005802 return PyBytes_FromStringAndSize((const char *)&(addr.sin6_addr),
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005803 sizeof(addr.sin6_addr));
5804 } else {
5805 PyErr_SetString(PyExc_OSError, "unknown address family");
5806 return NULL;
5807 }
5808}
5809
5810#endif
5811
5812#if defined(HAVE_INET_PTON) || defined(MS_WINDOWS)
Thomas Wouters477c8d52006-05-27 19:21:47 +00005813
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005814PyDoc_STRVAR(inet_ntop_doc,
5815"inet_ntop(af, packed_ip) -> string formatted IP address\n\
5816\n\
5817Convert a packed IP address of the given family to string format.");
5818
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005819#endif
5820
5821
5822#ifdef HAVE_INET_PTON
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005823static PyObject *
5824socket_inet_ntop(PyObject *self, PyObject *args)
5825{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005826 int af;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005827 Py_buffer packed_ip;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005828 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005829#ifdef ENABLE_IPV6
Victor Stinner640c35c2013-06-04 23:14:37 +02005830 char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005831#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005832 char ip[INET_ADDRSTRLEN + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005833#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00005834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005835 /* Guarantee NUL-termination for PyUnicode_FromString() below */
5836 memset((void *) &ip[0], '\0', sizeof(ip));
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005837
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005838 if (!PyArg_ParseTuple(args, "iy*:inet_ntop", &af, &packed_ip)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005839 return NULL;
5840 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005842 if (af == AF_INET) {
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005843 if (packed_ip.len != sizeof(struct in_addr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005844 PyErr_SetString(PyExc_ValueError,
5845 "invalid length of packed IP address string");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005846 PyBuffer_Release(&packed_ip);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005847 return NULL;
5848 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005849#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005850 } else if (af == AF_INET6) {
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005851 if (packed_ip.len != sizeof(struct in6_addr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005852 PyErr_SetString(PyExc_ValueError,
5853 "invalid length of packed IP address string");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005854 PyBuffer_Release(&packed_ip);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005855 return NULL;
5856 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005857#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005858 } else {
5859 PyErr_Format(PyExc_ValueError,
5860 "unknown address family %d", af);
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005861 PyBuffer_Release(&packed_ip);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005862 return NULL;
5863 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005864
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005865 retval = inet_ntop(af, packed_ip.buf, ip, sizeof(ip));
5866 PyBuffer_Release(&packed_ip);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005867 if (!retval) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005868 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005869 return NULL;
5870 } else {
5871 return PyUnicode_FromString(retval);
5872 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005873}
5874
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005875#elif defined(MS_WINDOWS)
5876
5877static PyObject *
5878socket_inet_ntop(PyObject *self, PyObject *args)
5879{
5880 int af;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005881 Py_buffer packed_ip;
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005882 struct sockaddr_in6 addr;
5883 DWORD addrlen, ret, retlen;
Jason R. Coombs8ec784c2013-11-10 13:43:22 -05005884#ifdef ENABLE_IPV6
5885 char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
5886#else
5887 char ip[INET_ADDRSTRLEN + 1];
5888#endif
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005889
5890 /* Guarantee NUL-termination for PyUnicode_FromString() below */
5891 memset((void *) &ip[0], '\0', sizeof(ip));
5892
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005893 if (!PyArg_ParseTuple(args, "iy*:inet_ntop", &af, &packed_ip)) {
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005894 return NULL;
5895 }
5896
5897 if (af == AF_INET) {
5898 struct sockaddr_in * addr4 = (struct sockaddr_in *)&addr;
5899
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005900 if (packed_ip.len != sizeof(struct in_addr)) {
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005901 PyErr_SetString(PyExc_ValueError,
5902 "invalid length of packed IP address string");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005903 PyBuffer_Release(&packed_ip);
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005904 return NULL;
5905 }
5906 memset(addr4, 0, sizeof(struct sockaddr_in));
5907 addr4->sin_family = AF_INET;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005908 memcpy(&(addr4->sin_addr), packed_ip.buf, sizeof(addr4->sin_addr));
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005909 addrlen = sizeof(struct sockaddr_in);
5910 } else if (af == AF_INET6) {
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005911 if (packed_ip.len != sizeof(struct in6_addr)) {
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005912 PyErr_SetString(PyExc_ValueError,
5913 "invalid length of packed IP address string");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005914 PyBuffer_Release(&packed_ip);
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005915 return NULL;
5916 }
5917
5918 memset(&addr, 0, sizeof(addr));
5919 addr.sin6_family = AF_INET6;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005920 memcpy(&(addr.sin6_addr), packed_ip.buf, sizeof(addr.sin6_addr));
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005921 addrlen = sizeof(addr);
5922 } else {
5923 PyErr_Format(PyExc_ValueError,
5924 "unknown address family %d", af);
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005925 PyBuffer_Release(&packed_ip);
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005926 return NULL;
5927 }
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005928 PyBuffer_Release(&packed_ip);
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005929
5930 retlen = sizeof(ip);
Victor Stinnere990c6e2013-11-16 00:18:58 +01005931 ret = WSAAddressToStringA((struct sockaddr*)&addr, addrlen, NULL,
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005932 ip, &retlen);
5933
5934 if (ret) {
5935 PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
5936 return NULL;
5937 } else {
5938 return PyUnicode_FromString(ip);
5939 }
5940}
5941
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005942#endif /* HAVE_INET_PTON */
5943
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005944/* Python interface to getaddrinfo(host, port). */
5945
5946/*ARGSUSED*/
5947static PyObject *
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005948socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005949{
Victor Stinner77af1722011-05-26 14:05:59 +02005950 static char* kwnames[] = {"host", "port", "family", "type", "proto",
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005951 "flags", 0};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005952 struct addrinfo hints, *res;
5953 struct addrinfo *res0 = NULL;
5954 PyObject *hobj = NULL;
5955 PyObject *pobj = (PyObject *)NULL;
5956 char pbuf[30];
5957 char *hptr, *pptr;
5958 int family, socktype, protocol, flags;
5959 int error;
5960 PyObject *all = (PyObject *)NULL;
5961 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005962
Georg Brandl6083a4b2013-10-14 06:51:46 +02005963 socktype = protocol = flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005964 family = AF_UNSPEC;
Victor Stinner77af1722011-05-26 14:05:59 +02005965 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iiii:getaddrinfo",
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005966 kwnames, &hobj, &pobj, &family, &socktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005967 &protocol, &flags)) {
5968 return NULL;
5969 }
5970 if (hobj == Py_None) {
5971 hptr = NULL;
5972 } else if (PyUnicode_Check(hobj)) {
Victor Stinnerec1a4982015-09-11 12:37:30 +02005973 idna = PyUnicode_AsEncodedString(hobj, "idna", NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005974 if (!idna)
5975 return NULL;
5976 assert(PyBytes_Check(idna));
5977 hptr = PyBytes_AS_STRING(idna);
5978 } else if (PyBytes_Check(hobj)) {
5979 hptr = PyBytes_AsString(hobj);
5980 } else {
5981 PyErr_SetString(PyExc_TypeError,
5982 "getaddrinfo() argument 1 must be string or None");
5983 return NULL;
5984 }
5985 if (PyLong_CheckExact(pobj)) {
5986 long value = PyLong_AsLong(pobj);
5987 if (value == -1 && PyErr_Occurred())
5988 goto err;
5989 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value);
5990 pptr = pbuf;
5991 } else if (PyUnicode_Check(pobj)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +02005992 pptr = PyUnicode_AsUTF8(pobj);
Alexander Belopolskye239d232010-12-08 23:31:48 +00005993 if (pptr == NULL)
5994 goto err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005995 } else if (PyBytes_Check(pobj)) {
Alexander Belopolskye239d232010-12-08 23:31:48 +00005996 pptr = PyBytes_AS_STRING(pobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005997 } else if (pobj == Py_None) {
5998 pptr = (char *)NULL;
5999 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02006000 PyErr_SetString(PyExc_OSError, "Int or String expected");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006001 goto err;
6002 }
Ronald Oussorena822d3662013-06-10 10:36:28 +02006003#if defined(__APPLE__) && defined(AI_NUMERICSERV)
Ronald Oussoren27a4ac52013-05-24 13:47:37 +02006004 if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) {
6005 /* On OSX upto at least OSX 10.8 getaddrinfo crashes
Christian Heimesaf01f662013-12-21 16:19:10 +01006006 * if AI_NUMERICSERV is set and the servname is NULL or "0".
6007 * This workaround avoids a segfault in libsystem.
6008 */
Ronald Oussoren27a4ac52013-05-24 13:47:37 +02006009 pptr = "00";
6010 }
6011#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006012 memset(&hints, 0, sizeof(hints));
6013 hints.ai_family = family;
6014 hints.ai_socktype = socktype;
6015 hints.ai_protocol = protocol;
6016 hints.ai_flags = flags;
6017 Py_BEGIN_ALLOW_THREADS
6018 ACQUIRE_GETADDRINFO_LOCK
6019 error = getaddrinfo(hptr, pptr, &hints, &res0);
6020 Py_END_ALLOW_THREADS
6021 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
6022 if (error) {
6023 set_gaierror(error);
6024 goto err;
6025 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006026
Benjamin Peterson4fd929b2015-04-01 11:16:40 -04006027 all = PyList_New(0);
6028 if (all == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006029 goto err;
6030 for (res = res0; res; res = res->ai_next) {
6031 PyObject *single;
6032 PyObject *addr =
6033 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
6034 if (addr == NULL)
6035 goto err;
6036 single = Py_BuildValue("iiisO", res->ai_family,
6037 res->ai_socktype, res->ai_protocol,
6038 res->ai_canonname ? res->ai_canonname : "",
6039 addr);
6040 Py_DECREF(addr);
6041 if (single == NULL)
6042 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006044 if (PyList_Append(all, single))
6045 goto err;
6046 Py_XDECREF(single);
6047 }
6048 Py_XDECREF(idna);
6049 if (res0)
6050 freeaddrinfo(res0);
6051 return all;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006052 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006053 Py_XDECREF(all);
6054 Py_XDECREF(idna);
6055 if (res0)
6056 freeaddrinfo(res0);
6057 return (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006058}
6059
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006060PyDoc_STRVAR(getaddrinfo_doc,
Berker Peksaga6ec5ee2014-06-30 11:28:40 +03006061"getaddrinfo(host, port [, family, type, proto, flags])\n\
6062 -> list of (family, type, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006063\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006064Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006065
6066/* Python interface to getnameinfo(sa, flags). */
6067
6068/*ARGSUSED*/
6069static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006070socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006071{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006072 PyObject *sa = (PyObject *)NULL;
6073 int flags;
6074 char *hostp;
Charles-François Natali366999a2012-01-02 15:47:29 +01006075 int port;
6076 unsigned int flowinfo, scope_id;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006077 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
6078 struct addrinfo hints, *res = NULL;
6079 int error;
6080 PyObject *ret = (PyObject *)NULL;
Victor Stinner72400302016-01-28 15:41:01 +01006081 PyObject *name;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006083 flags = flowinfo = scope_id = 0;
6084 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
6085 return NULL;
6086 if (!PyTuple_Check(sa)) {
6087 PyErr_SetString(PyExc_TypeError,
6088 "getnameinfo() argument 1 must be a tuple");
6089 return NULL;
6090 }
Charles-François Natali366999a2012-01-02 15:47:29 +01006091 if (!PyArg_ParseTuple(sa, "si|II",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006092 &hostp, &port, &flowinfo, &scope_id))
6093 return NULL;
Charles-François Natali9b0c0062012-06-23 10:17:05 +02006094 if (flowinfo > 0xfffff) {
Charles-François Natali366999a2012-01-02 15:47:29 +01006095 PyErr_SetString(PyExc_OverflowError,
6096 "getsockaddrarg: flowinfo must be 0-1048575.");
6097 return NULL;
6098 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006099 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
6100 memset(&hints, 0, sizeof(hints));
6101 hints.ai_family = AF_UNSPEC;
6102 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Martin v. Löwis112c0f32010-08-25 07:38:15 +00006103 hints.ai_flags = AI_NUMERICHOST; /* don't do any name resolution */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006104 Py_BEGIN_ALLOW_THREADS
6105 ACQUIRE_GETADDRINFO_LOCK
6106 error = getaddrinfo(hostp, pbuf, &hints, &res);
6107 Py_END_ALLOW_THREADS
6108 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
6109 if (error) {
6110 set_gaierror(error);
6111 goto fail;
6112 }
6113 if (res->ai_next) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02006114 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006115 "sockaddr resolved to multiple addresses");
6116 goto fail;
6117 }
6118 switch (res->ai_family) {
6119 case AF_INET:
6120 {
6121 if (PyTuple_GET_SIZE(sa) != 2) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02006122 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006123 "IPv4 sockaddr must be 2 tuple");
6124 goto fail;
6125 }
6126 break;
6127 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00006128#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006129 case AF_INET6:
6130 {
6131 struct sockaddr_in6 *sin6;
6132 sin6 = (struct sockaddr_in6 *)res->ai_addr;
Charles-François Natali366999a2012-01-02 15:47:29 +01006133 sin6->sin6_flowinfo = htonl(flowinfo);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006134 sin6->sin6_scope_id = scope_id;
6135 break;
6136 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006137#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006138 }
Antoine Pitrou19467d22010-08-17 19:33:30 +00006139 error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006140 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
6141 if (error) {
6142 set_gaierror(error);
6143 goto fail;
6144 }
Victor Stinner72400302016-01-28 15:41:01 +01006145
6146 name = sock_decode_hostname(hbuf);
6147 if (name == NULL)
6148 goto fail;
6149 ret = Py_BuildValue("Ns", name, pbuf);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006150
6151fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006152 if (res)
6153 freeaddrinfo(res);
6154 return ret;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006155}
6156
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006157PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00006158"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006159\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006160Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006161
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00006162
6163/* Python API to getting and setting the default timeout value. */
6164
6165static PyObject *
6166socket_getdefaulttimeout(PyObject *self)
6167{
Victor Stinner71694d52015-03-28 01:18:54 +01006168 if (defaulttimeout < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006169 Py_INCREF(Py_None);
6170 return Py_None;
6171 }
Victor Stinner71694d52015-03-28 01:18:54 +01006172 else {
6173 double seconds = _PyTime_AsSecondsDouble(defaulttimeout);
6174 return PyFloat_FromDouble(seconds);
6175 }
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00006176}
6177
6178PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00006179"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00006180\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03006181Returns the default timeout in seconds (float) for new socket objects.\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00006182A value of None indicates that new socket objects have no timeout.\n\
6183When the socket module is first imported, the default is None.");
6184
6185static PyObject *
6186socket_setdefaulttimeout(PyObject *self, PyObject *arg)
6187{
Victor Stinner71694d52015-03-28 01:18:54 +01006188 _PyTime_t timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00006189
Victor Stinner71694d52015-03-28 01:18:54 +01006190 if (socket_parse_timeout(&timeout, arg) < 0)
6191 return NULL;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00006192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006193 defaulttimeout = timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00006194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006195 Py_INCREF(Py_None);
6196 return Py_None;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00006197}
6198
6199PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00006200"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00006201\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03006202Set the default timeout in seconds (float) for new socket objects.\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00006203A value of None indicates that new socket objects have no timeout.\n\
6204When the socket module is first imported, the default is None.");
6205
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07006206#ifdef HAVE_IF_NAMEINDEX
6207/* Python API for getting interface indices and names */
6208
6209static PyObject *
6210socket_if_nameindex(PyObject *self, PyObject *arg)
6211{
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07006212 PyObject *list;
Charles-François Natali60713592011-05-20 16:55:06 +02006213 int i;
6214 struct if_nameindex *ni;
Victor Stinner77af1722011-05-26 14:05:59 +02006215
Charles-François Natali60713592011-05-20 16:55:06 +02006216 ni = if_nameindex();
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07006217 if (ni == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02006218 PyErr_SetFromErrno(PyExc_OSError);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07006219 return NULL;
6220 }
6221
6222 list = PyList_New(0);
6223 if (list == NULL) {
6224 if_freenameindex(ni);
6225 return NULL;
6226 }
6227
Charles-François Natali60713592011-05-20 16:55:06 +02006228 for (i = 0; ni[i].if_index != 0 && i < INT_MAX; i++) {
6229 PyObject *ni_tuple = Py_BuildValue("IO&",
6230 ni[i].if_index, PyUnicode_DecodeFSDefault, ni[i].if_name);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07006231
6232 if (ni_tuple == NULL || PyList_Append(list, ni_tuple) == -1) {
6233 Py_XDECREF(ni_tuple);
Charles-François Natali60713592011-05-20 16:55:06 +02006234 Py_DECREF(list);
6235 if_freenameindex(ni);
6236 return NULL;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07006237 }
6238 Py_DECREF(ni_tuple);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07006239 }
6240
6241 if_freenameindex(ni);
6242 return list;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07006243}
6244
6245PyDoc_STRVAR(if_nameindex_doc,
6246"if_nameindex()\n\
6247\n\
6248Returns a list of network interface information (index, name) tuples.");
6249
Charles-François Natali60713592011-05-20 16:55:06 +02006250static PyObject *
6251socket_if_nametoindex(PyObject *self, PyObject *args)
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07006252{
Charles-François Natali60713592011-05-20 16:55:06 +02006253 PyObject *oname;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07006254 unsigned long index;
6255
Charles-François Natali60713592011-05-20 16:55:06 +02006256 if (!PyArg_ParseTuple(args, "O&:if_nametoindex",
6257 PyUnicode_FSConverter, &oname))
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07006258 return NULL;
6259
Charles-François Natali60713592011-05-20 16:55:06 +02006260 index = if_nametoindex(PyBytes_AS_STRING(oname));
6261 Py_DECREF(oname);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07006262 if (index == 0) {
Charles-François Natali60713592011-05-20 16:55:06 +02006263 /* if_nametoindex() doesn't set errno */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02006264 PyErr_SetString(PyExc_OSError, "no interface with this name");
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07006265 return NULL;
6266 }
6267
6268 return PyLong_FromUnsignedLong(index);
6269}
6270
6271PyDoc_STRVAR(if_nametoindex_doc,
6272"if_nametoindex(if_name)\n\
6273\n\
6274Returns the interface index corresponding to the interface name if_name.");
6275
Charles-François Natali60713592011-05-20 16:55:06 +02006276static PyObject *
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07006277socket_if_indextoname(PyObject *self, PyObject *arg)
6278{
Charles-François Natali60713592011-05-20 16:55:06 +02006279 unsigned long index;
6280 char name[IF_NAMESIZE + 1];
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07006281
Charles-François Natali60713592011-05-20 16:55:06 +02006282 index = PyLong_AsUnsignedLong(arg);
6283 if (index == (unsigned long) -1)
6284 return NULL;
6285
6286 if (if_indextoname(index, name) == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02006287 PyErr_SetFromErrno(PyExc_OSError);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07006288 return NULL;
6289 }
6290
Charles-François Natali60713592011-05-20 16:55:06 +02006291 return PyUnicode_DecodeFSDefault(name);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07006292}
6293
6294PyDoc_STRVAR(if_indextoname_doc,
6295"if_indextoname(if_index)\n\
6296\n\
6297Returns the interface name corresponding to the interface index if_index.");
6298
6299#endif /* HAVE_IF_NAMEINDEX */
6300
6301
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006302#ifdef CMSG_LEN
6303/* Python interface to CMSG_LEN(length). */
6304
6305static PyObject *
6306socket_CMSG_LEN(PyObject *self, PyObject *args)
6307{
6308 Py_ssize_t length;
6309 size_t result;
6310
6311 if (!PyArg_ParseTuple(args, "n:CMSG_LEN", &length))
6312 return NULL;
6313 if (length < 0 || !get_CMSG_LEN(length, &result)) {
6314 PyErr_Format(PyExc_OverflowError, "CMSG_LEN() argument out of range");
6315 return NULL;
6316 }
6317 return PyLong_FromSize_t(result);
6318}
6319
6320PyDoc_STRVAR(CMSG_LEN_doc,
6321"CMSG_LEN(length) -> control message length\n\
6322\n\
6323Return the total length, without trailing padding, of an ancillary\n\
6324data item with associated data of the given length. This value can\n\
6325often be used as the buffer size for recvmsg() to receive a single\n\
6326item of ancillary data, but RFC 3542 requires portable applications to\n\
6327use CMSG_SPACE() and thus include space for padding, even when the\n\
6328item will be the last in the buffer. Raises OverflowError if length\n\
6329is outside the permissible range of values.");
6330
6331
6332#ifdef CMSG_SPACE
6333/* Python interface to CMSG_SPACE(length). */
6334
6335static PyObject *
6336socket_CMSG_SPACE(PyObject *self, PyObject *args)
6337{
6338 Py_ssize_t length;
6339 size_t result;
6340
6341 if (!PyArg_ParseTuple(args, "n:CMSG_SPACE", &length))
6342 return NULL;
6343 if (length < 0 || !get_CMSG_SPACE(length, &result)) {
6344 PyErr_SetString(PyExc_OverflowError,
6345 "CMSG_SPACE() argument out of range");
6346 return NULL;
6347 }
6348 return PyLong_FromSize_t(result);
6349}
6350
6351PyDoc_STRVAR(CMSG_SPACE_doc,
6352"CMSG_SPACE(length) -> buffer size\n\
6353\n\
6354Return the buffer size needed for recvmsg() to receive an ancillary\n\
6355data item with associated data of the given length, along with any\n\
6356trailing padding. The buffer space needed to receive multiple items\n\
6357is the sum of the CMSG_SPACE() values for their associated data\n\
6358lengths. Raises OverflowError if length is outside the permissible\n\
6359range of values.");
6360#endif /* CMSG_SPACE */
6361#endif /* CMSG_LEN */
6362
6363
Guido van Rossum30a685f1991-06-27 15:51:29 +00006364/* List of functions exported by this module. */
6365
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006366static PyMethodDef socket_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006367 {"gethostbyname", socket_gethostbyname,
6368 METH_VARARGS, gethostbyname_doc},
6369 {"gethostbyname_ex", socket_gethostbyname_ex,
6370 METH_VARARGS, ghbn_ex_doc},
6371 {"gethostbyaddr", socket_gethostbyaddr,
6372 METH_VARARGS, gethostbyaddr_doc},
6373 {"gethostname", socket_gethostname,
6374 METH_NOARGS, gethostname_doc},
Antoine Pitrou061cfb52011-02-28 22:25:22 +00006375#ifdef HAVE_SETHOSTNAME
6376 {"sethostname", socket_sethostname,
6377 METH_VARARGS, sethostname_doc},
6378#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006379 {"getservbyname", socket_getservbyname,
6380 METH_VARARGS, getservbyname_doc},
6381 {"getservbyport", socket_getservbyport,
6382 METH_VARARGS, getservbyport_doc},
6383 {"getprotobyname", socket_getprotobyname,
6384 METH_VARARGS, getprotobyname_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00006385#ifndef NO_DUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006386 {"dup", socket_dup,
6387 METH_O, dup_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00006388#endif
Dave Cole331708b2004-08-09 04:51:41 +00006389#ifdef HAVE_SOCKETPAIR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006390 {"socketpair", socket_socketpair,
6391 METH_VARARGS, socketpair_doc},
Dave Cole331708b2004-08-09 04:51:41 +00006392#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006393 {"ntohs", socket_ntohs,
6394 METH_VARARGS, ntohs_doc},
6395 {"ntohl", socket_ntohl,
6396 METH_O, ntohl_doc},
6397 {"htons", socket_htons,
6398 METH_VARARGS, htons_doc},
6399 {"htonl", socket_htonl,
6400 METH_O, htonl_doc},
6401 {"inet_aton", socket_inet_aton,
6402 METH_VARARGS, inet_aton_doc},
6403 {"inet_ntoa", socket_inet_ntoa,
6404 METH_VARARGS, inet_ntoa_doc},
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09006405#if defined(HAVE_INET_PTON) || defined(MS_WINDOWS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006406 {"inet_pton", socket_inet_pton,
6407 METH_VARARGS, inet_pton_doc},
6408 {"inet_ntop", socket_inet_ntop,
6409 METH_VARARGS, inet_ntop_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00006410#endif
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00006411 {"getaddrinfo", (PyCFunction)socket_getaddrinfo,
6412 METH_VARARGS | METH_KEYWORDS, getaddrinfo_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006413 {"getnameinfo", socket_getnameinfo,
6414 METH_VARARGS, getnameinfo_doc},
6415 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
6416 METH_NOARGS, getdefaulttimeout_doc},
6417 {"setdefaulttimeout", socket_setdefaulttimeout,
6418 METH_O, setdefaulttimeout_doc},
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07006419#ifdef HAVE_IF_NAMEINDEX
6420 {"if_nameindex", socket_if_nameindex,
6421 METH_NOARGS, if_nameindex_doc},
6422 {"if_nametoindex", socket_if_nametoindex,
Charles-François Natali60713592011-05-20 16:55:06 +02006423 METH_VARARGS, if_nametoindex_doc},
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07006424 {"if_indextoname", socket_if_indextoname,
6425 METH_O, if_indextoname_doc},
6426#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006427#ifdef CMSG_LEN
6428 {"CMSG_LEN", socket_CMSG_LEN,
6429 METH_VARARGS, CMSG_LEN_doc},
6430#ifdef CMSG_SPACE
6431 {"CMSG_SPACE", socket_CMSG_SPACE,
6432 METH_VARARGS, CMSG_SPACE_doc},
6433#endif
6434#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006435 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00006436};
6437
Guido van Rossum30a685f1991-06-27 15:51:29 +00006438
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006439#ifdef MS_WINDOWS
6440#define OS_INIT_DEFINED
6441
6442/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00006443
6444static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006445os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00006446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006447 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00006448}
6449
6450static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006451os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00006452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006453 WSADATA WSAData;
6454 int ret;
6455 ret = WSAStartup(0x0101, &WSAData);
6456 switch (ret) {
6457 case 0: /* No error */
6458 Py_AtExit(os_cleanup);
6459 return 1; /* Success */
6460 case WSASYSNOTREADY:
6461 PyErr_SetString(PyExc_ImportError,
6462 "WSAStartup failed: network not ready");
6463 break;
6464 case WSAVERNOTSUPPORTED:
6465 case WSAEINVAL:
6466 PyErr_SetString(
6467 PyExc_ImportError,
6468 "WSAStartup failed: requested version not supported");
6469 break;
6470 default:
6471 PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret);
6472 break;
6473 }
6474 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00006475}
6476
Guido van Rossum8d665e61996-06-26 18:22:49 +00006477#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00006478
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006479
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006480
6481#ifndef OS_INIT_DEFINED
6482static int
6483os_init(void)
6484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006485 return 1; /* Success */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006486}
6487#endif
6488
6489
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00006490/* C API table - always add new things to the end for binary
6491 compatibility. */
6492static
6493PySocketModule_APIObject PySocketModuleAPI =
6494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006495 &sock_type,
Antoine Pitrouc4df7842010-12-03 19:59:41 +00006496 NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006497 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00006498};
6499
Guido van Rossum3eede5a2002-06-07 02:08:35 +00006500
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006501/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00006502
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006503 This module is actually called "_socket", and there's a wrapper
Guido van Rossum7d0a8262007-05-21 23:13:11 +00006504 "socket.py" which implements some additional functionality.
6505 The import of "_socket" may fail with an ImportError exception if
6506 os-specific initialization fails. On Windows, this does WINSOCK
Ezio Melotti13925002011-03-16 11:05:33 +02006507 initialization. When WINSOCK is initialized successfully, a call to
Guido van Rossum7d0a8262007-05-21 23:13:11 +00006508 WSACleanup() is scheduled to be made at exit time.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006509*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00006510
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006511PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00006512"Implementation module for socket operations.\n\
6513\n\
6514See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00006515
Martin v. Löwis1a214512008-06-11 05:26:20 +00006516static struct PyModuleDef socketmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006517 PyModuleDef_HEAD_INIT,
6518 PySocket_MODULE_NAME,
6519 socket_doc,
6520 -1,
6521 socket_methods,
6522 NULL,
6523 NULL,
6524 NULL,
6525 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00006526};
6527
Mark Hammondfe51c6d2002-08-02 02:27:13 +00006528PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00006529PyInit__socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00006530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006531 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00006532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006533 if (!os_init())
6534 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006535
Victor Stinnerdaf45552013-08-28 00:53:59 +02006536#ifdef MS_WINDOWS
6537 if (support_wsa_no_inherit == -1) {
Steve Dower65e4cb12014-11-22 12:54:57 -08006538#if defined(_MSC_VER) && _MSC_VER >= 1800
6539 support_wsa_no_inherit = IsWindows7SP1OrGreater();
6540#else
Victor Stinnerdaf45552013-08-28 00:53:59 +02006541 DWORD version = GetVersion();
6542 DWORD major = (DWORD)LOBYTE(LOWORD(version));
6543 DWORD minor = (DWORD)HIBYTE(LOWORD(version));
6544 /* need Windows 7 SP1, 2008 R2 SP1 or later */
Steve Dower65e4cb12014-11-22 12:54:57 -08006545 support_wsa_no_inherit = major > 6 || (major == 6 && minor >= 1);
6546#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02006547 }
6548#endif
6549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006550 Py_TYPE(&sock_type) = &PyType_Type;
6551 m = PyModule_Create(&socketmodule);
6552 if (m == NULL)
6553 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006554
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02006555 Py_INCREF(PyExc_OSError);
6556 PySocketModuleAPI.error = PyExc_OSError;
6557 Py_INCREF(PyExc_OSError);
6558 PyModule_AddObject(m, "error", PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006559 socket_herror = PyErr_NewException("socket.herror",
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02006560 PyExc_OSError, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006561 if (socket_herror == NULL)
6562 return NULL;
6563 Py_INCREF(socket_herror);
6564 PyModule_AddObject(m, "herror", socket_herror);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02006565 socket_gaierror = PyErr_NewException("socket.gaierror", PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006566 NULL);
6567 if (socket_gaierror == NULL)
6568 return NULL;
6569 Py_INCREF(socket_gaierror);
6570 PyModule_AddObject(m, "gaierror", socket_gaierror);
6571 socket_timeout = PyErr_NewException("socket.timeout",
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02006572 PyExc_OSError, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006573 if (socket_timeout == NULL)
6574 return NULL;
Antoine Pitrouc4df7842010-12-03 19:59:41 +00006575 PySocketModuleAPI.timeout_error = socket_timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006576 Py_INCREF(socket_timeout);
6577 PyModule_AddObject(m, "timeout", socket_timeout);
6578 Py_INCREF((PyObject *)&sock_type);
6579 if (PyModule_AddObject(m, "SocketType",
6580 (PyObject *)&sock_type) != 0)
6581 return NULL;
6582 Py_INCREF((PyObject *)&sock_type);
6583 if (PyModule_AddObject(m, "socket",
6584 (PyObject *)&sock_type) != 0)
6585 return NULL;
Guido van Rossum09be4091999-08-09 14:40:40 +00006586
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00006587#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006588 has_ipv6 = Py_True;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00006589#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006590 has_ipv6 = Py_False;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00006591#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006592 Py_INCREF(has_ipv6);
6593 PyModule_AddObject(m, "has_ipv6", has_ipv6);
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00006594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006595 /* Export C API */
6596 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
6597 PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL)
6598 ) != 0)
6599 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00006600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006601 /* Address families (we only support AF_INET and AF_UNIX) */
Guido van Rossum09be4091999-08-09 14:40:40 +00006602#ifdef AF_UNSPEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006603 PyModule_AddIntMacro(m, AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00006604#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006605 PyModule_AddIntMacro(m, AF_INET);
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00006606#if defined(AF_UNIX)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006607 PyModule_AddIntMacro(m, AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006608#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00006609#ifdef AF_AX25
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006610 /* Amateur Radio AX.25 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006611 PyModule_AddIntMacro(m, AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00006612#endif
6613#ifdef AF_IPX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006614 PyModule_AddIntMacro(m, AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00006615#endif
6616#ifdef AF_APPLETALK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006617 /* Appletalk DDP */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006618 PyModule_AddIntMacro(m, AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00006619#endif
6620#ifdef AF_NETROM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006621 /* Amateur radio NetROM */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006622 PyModule_AddIntMacro(m, AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00006623#endif
6624#ifdef AF_BRIDGE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006625 /* Multiprotocol bridge */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006626 PyModule_AddIntMacro(m, AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00006627#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006628#ifdef AF_ATMPVC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006629 /* ATM PVCs */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006630 PyModule_AddIntMacro(m, AF_ATMPVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006631#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006632#ifdef AF_AAL5
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006633 /* Reserved for Werner's ATM */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006634 PyModule_AddIntMacro(m, AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00006635#endif
Christian Heimesdffa3942016-09-05 23:54:41 +02006636#ifdef HAVE_SOCKADDR_ALG
6637 PyModule_AddIntMacro(m, AF_ALG); /* Linux crypto */
6638#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006639#ifdef AF_X25
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006640 /* Reserved for X.25 project */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006641 PyModule_AddIntMacro(m, AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00006642#endif
6643#ifdef AF_INET6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006644 PyModule_AddIntMacro(m, AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00006645#endif
6646#ifdef AF_ROSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006647 /* Amateur Radio X.25 PLP */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006648 PyModule_AddIntMacro(m, AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00006649#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006650#ifdef AF_DECnet
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006651 /* Reserved for DECnet project */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006652 PyModule_AddIntMacro(m, AF_DECnet);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006653#endif
6654#ifdef AF_NETBEUI
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006655 /* Reserved for 802.2LLC project */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006656 PyModule_AddIntMacro(m, AF_NETBEUI);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006657#endif
6658#ifdef AF_SECURITY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006659 /* Security callback pseudo AF */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006660 PyModule_AddIntMacro(m, AF_SECURITY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006661#endif
6662#ifdef AF_KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006663 /* PF_KEY key management API */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006664 PyModule_AddIntMacro(m, AF_KEY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006665#endif
6666#ifdef AF_NETLINK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006667 /* */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006668 PyModule_AddIntMacro(m, AF_NETLINK);
6669 PyModule_AddIntMacro(m, NETLINK_ROUTE);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006670#ifdef NETLINK_SKIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006671 PyModule_AddIntMacro(m, NETLINK_SKIP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006672#endif
6673#ifdef NETLINK_W1
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006674 PyModule_AddIntMacro(m, NETLINK_W1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006675#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006676 PyModule_AddIntMacro(m, NETLINK_USERSOCK);
6677 PyModule_AddIntMacro(m, NETLINK_FIREWALL);
Guido van Rossum668a94a2006-02-21 01:07:27 +00006678#ifdef NETLINK_TCPDIAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006679 PyModule_AddIntMacro(m, NETLINK_TCPDIAG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00006680#endif
6681#ifdef NETLINK_NFLOG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006682 PyModule_AddIntMacro(m, NETLINK_NFLOG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00006683#endif
Neal Norwitz65851662006-01-16 04:31:40 +00006684#ifdef NETLINK_XFRM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006685 PyModule_AddIntMacro(m, NETLINK_XFRM);
Neal Norwitz65851662006-01-16 04:31:40 +00006686#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006687#ifdef NETLINK_ARPD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006688 PyModule_AddIntMacro(m, NETLINK_ARPD);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006689#endif
6690#ifdef NETLINK_ROUTE6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006691 PyModule_AddIntMacro(m, NETLINK_ROUTE6);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006692#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006693 PyModule_AddIntMacro(m, NETLINK_IP6_FW);
Thomas Wouterscf297e42007-02-23 15:07:44 +00006694#ifdef NETLINK_DNRTMSG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006695 PyModule_AddIntMacro(m, NETLINK_DNRTMSG);
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00006696#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006697#ifdef NETLINK_TAPBASE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006698 PyModule_AddIntMacro(m, NETLINK_TAPBASE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006699#endif
Christian Heimesdffa3942016-09-05 23:54:41 +02006700#ifdef NETLINK_CRYPTO
6701 PyModule_AddIntMacro(m, NETLINK_CRYPTO);
6702#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006703#endif /* AF_NETLINK */
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006704#ifdef AF_ROUTE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006705 /* Alias to emulate 4.4BSD */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006706 PyModule_AddIntMacro(m, AF_ROUTE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006707#endif
Giampaolo Rodola'80e1c432013-05-21 21:02:04 +02006708#ifdef AF_LINK
6709 PyModule_AddIntMacro(m, AF_LINK);
6710#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006711#ifdef AF_ASH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006712 /* Ash */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006713 PyModule_AddIntMacro(m, AF_ASH);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006714#endif
6715#ifdef AF_ECONET
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006716 /* Acorn Econet */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006717 PyModule_AddIntMacro(m, AF_ECONET);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006718#endif
6719#ifdef AF_ATMSVC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006720 /* ATM SVCs */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006721 PyModule_AddIntMacro(m, AF_ATMSVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006722#endif
6723#ifdef AF_SNA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006724 /* Linux SNA Project (nutters!) */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006725 PyModule_AddIntMacro(m, AF_SNA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006726#endif
6727#ifdef AF_IRDA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006728 /* IRDA sockets */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006729 PyModule_AddIntMacro(m, AF_IRDA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006730#endif
6731#ifdef AF_PPPOX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006732 /* PPPoX sockets */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006733 PyModule_AddIntMacro(m, AF_PPPOX);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006734#endif
6735#ifdef AF_WANPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006736 /* Wanpipe API Sockets */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006737 PyModule_AddIntMacro(m, AF_WANPIPE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006738#endif
6739#ifdef AF_LLC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006740 /* Linux LLC */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006741 PyModule_AddIntMacro(m, AF_LLC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006742#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00006743
Hye-Shik Chang81268602004-02-02 06:05:24 +00006744#ifdef USE_BLUETOOTH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006745 PyModule_AddIntMacro(m, AF_BLUETOOTH);
6746 PyModule_AddIntMacro(m, BTPROTO_L2CAP);
6747 PyModule_AddIntMacro(m, BTPROTO_HCI);
6748 PyModule_AddIntMacro(m, SOL_HCI);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00006749#if !defined(__NetBSD__) && !defined(__DragonFly__)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006750 PyModule_AddIntMacro(m, HCI_FILTER);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00006751#endif
Hye-Shik Chang81268602004-02-02 06:05:24 +00006752#if !defined(__FreeBSD__)
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00006753#if !defined(__NetBSD__) && !defined(__DragonFly__)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006754 PyModule_AddIntMacro(m, HCI_TIME_STAMP);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00006755#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006756 PyModule_AddIntMacro(m, HCI_DATA_DIR);
6757 PyModule_AddIntMacro(m, BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00006758#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006759 PyModule_AddIntMacro(m, BTPROTO_RFCOMM);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006760 PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
6761 PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
Martin v. Löwis12af0482004-01-31 12:34:17 +00006762#endif
6763
Charles-François Natali47413c12011-10-06 19:47:44 +02006764#ifdef AF_CAN
6765 /* Controller Area Network */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006766 PyModule_AddIntMacro(m, AF_CAN);
Charles-François Natali47413c12011-10-06 19:47:44 +02006767#endif
6768#ifdef PF_CAN
6769 /* Controller Area Network */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006770 PyModule_AddIntMacro(m, PF_CAN);
Charles-François Natali47413c12011-10-06 19:47:44 +02006771#endif
6772
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006773/* Reliable Datagram Sockets */
6774#ifdef AF_RDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006775 PyModule_AddIntMacro(m, AF_RDS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006776#endif
6777#ifdef PF_RDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006778 PyModule_AddIntMacro(m, PF_RDS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006779#endif
6780
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006781/* Kernel event messages */
6782#ifdef PF_SYSTEM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006783 PyModule_AddIntMacro(m, PF_SYSTEM);
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006784#endif
6785#ifdef AF_SYSTEM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006786 PyModule_AddIntMacro(m, AF_SYSTEM);
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006787#endif
6788
Antoine Pitroub156a462010-10-27 20:13:57 +00006789#ifdef AF_PACKET
6790 PyModule_AddIntMacro(m, AF_PACKET);
6791#endif
6792#ifdef PF_PACKET
6793 PyModule_AddIntMacro(m, PF_PACKET);
6794#endif
6795#ifdef PACKET_HOST
6796 PyModule_AddIntMacro(m, PACKET_HOST);
6797#endif
6798#ifdef PACKET_BROADCAST
6799 PyModule_AddIntMacro(m, PACKET_BROADCAST);
6800#endif
6801#ifdef PACKET_MULTICAST
6802 PyModule_AddIntMacro(m, PACKET_MULTICAST);
6803#endif
6804#ifdef PACKET_OTHERHOST
6805 PyModule_AddIntMacro(m, PACKET_OTHERHOST);
6806#endif
6807#ifdef PACKET_OUTGOING
6808 PyModule_AddIntMacro(m, PACKET_OUTGOING);
6809#endif
6810#ifdef PACKET_LOOPBACK
6811 PyModule_AddIntMacro(m, PACKET_LOOPBACK);
6812#endif
6813#ifdef PACKET_FASTROUTE
6814 PyModule_AddIntMacro(m, PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00006815#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006816
Christian Heimes043d6f62008-01-07 17:19:16 +00006817#ifdef HAVE_LINUX_TIPC_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006818 PyModule_AddIntMacro(m, AF_TIPC);
Christian Heimes043d6f62008-01-07 17:19:16 +00006819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006820 /* for addresses */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006821 PyModule_AddIntMacro(m, TIPC_ADDR_NAMESEQ);
6822 PyModule_AddIntMacro(m, TIPC_ADDR_NAME);
6823 PyModule_AddIntMacro(m, TIPC_ADDR_ID);
Christian Heimes043d6f62008-01-07 17:19:16 +00006824
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006825 PyModule_AddIntMacro(m, TIPC_ZONE_SCOPE);
6826 PyModule_AddIntMacro(m, TIPC_CLUSTER_SCOPE);
6827 PyModule_AddIntMacro(m, TIPC_NODE_SCOPE);
Christian Heimes043d6f62008-01-07 17:19:16 +00006828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006829 /* for setsockopt() */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006830 PyModule_AddIntMacro(m, SOL_TIPC);
6831 PyModule_AddIntMacro(m, TIPC_IMPORTANCE);
6832 PyModule_AddIntMacro(m, TIPC_SRC_DROPPABLE);
6833 PyModule_AddIntMacro(m, TIPC_DEST_DROPPABLE);
6834 PyModule_AddIntMacro(m, TIPC_CONN_TIMEOUT);
Christian Heimes043d6f62008-01-07 17:19:16 +00006835
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006836 PyModule_AddIntMacro(m, TIPC_LOW_IMPORTANCE);
6837 PyModule_AddIntMacro(m, TIPC_MEDIUM_IMPORTANCE);
6838 PyModule_AddIntMacro(m, TIPC_HIGH_IMPORTANCE);
6839 PyModule_AddIntMacro(m, TIPC_CRITICAL_IMPORTANCE);
Christian Heimes043d6f62008-01-07 17:19:16 +00006840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006841 /* for subscriptions */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006842 PyModule_AddIntMacro(m, TIPC_SUB_PORTS);
6843 PyModule_AddIntMacro(m, TIPC_SUB_SERVICE);
Christian Heimes25bb7832008-01-11 16:17:00 +00006844#ifdef TIPC_SUB_CANCEL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006845 /* doesn't seem to be available everywhere */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006846 PyModule_AddIntMacro(m, TIPC_SUB_CANCEL);
Christian Heimes25bb7832008-01-11 16:17:00 +00006847#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006848 PyModule_AddIntMacro(m, TIPC_WAIT_FOREVER);
6849 PyModule_AddIntMacro(m, TIPC_PUBLISHED);
6850 PyModule_AddIntMacro(m, TIPC_WITHDRAWN);
6851 PyModule_AddIntMacro(m, TIPC_SUBSCR_TIMEOUT);
6852 PyModule_AddIntMacro(m, TIPC_CFG_SRV);
6853 PyModule_AddIntMacro(m, TIPC_TOP_SRV);
Christian Heimes043d6f62008-01-07 17:19:16 +00006854#endif
6855
Christian Heimesdffa3942016-09-05 23:54:41 +02006856#ifdef HAVE_SOCKADDR_ALG
6857 /* Socket options */
6858 PyModule_AddIntMacro(m, ALG_SET_KEY);
6859 PyModule_AddIntMacro(m, ALG_SET_IV);
6860 PyModule_AddIntMacro(m, ALG_SET_OP);
6861 PyModule_AddIntMacro(m, ALG_SET_AEAD_ASSOCLEN);
6862 PyModule_AddIntMacro(m, ALG_SET_AEAD_AUTHSIZE);
6863 PyModule_AddIntMacro(m, ALG_SET_PUBKEY);
6864
6865 /* Operations */
6866 PyModule_AddIntMacro(m, ALG_OP_DECRYPT);
6867 PyModule_AddIntMacro(m, ALG_OP_ENCRYPT);
6868 PyModule_AddIntMacro(m, ALG_OP_SIGN);
6869 PyModule_AddIntMacro(m, ALG_OP_VERIFY);
6870#endif
6871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006872 /* Socket types */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006873 PyModule_AddIntMacro(m, SOCK_STREAM);
6874 PyModule_AddIntMacro(m, SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00006875/* We have incomplete socket support. */
Berker Peksag95e0df82016-08-08 14:07:05 +03006876#ifdef SOCK_RAW
6877 /* SOCK_RAW is marked as optional in the POSIX specification */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006878 PyModule_AddIntMacro(m, SOCK_RAW);
Berker Peksag95e0df82016-08-08 14:07:05 +03006879#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006880 PyModule_AddIntMacro(m, SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00006881#if defined(SOCK_RDM)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006882 PyModule_AddIntMacro(m, SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00006883#endif
Antoine Pitroub1c54962010-10-14 15:05:38 +00006884#ifdef SOCK_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006885 PyModule_AddIntMacro(m, SOCK_CLOEXEC);
Antoine Pitroub1c54962010-10-14 15:05:38 +00006886#endif
6887#ifdef SOCK_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006888 PyModule_AddIntMacro(m, SOCK_NONBLOCK);
Antoine Pitroub1c54962010-10-14 15:05:38 +00006889#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006891#ifdef SO_DEBUG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006892 PyModule_AddIntMacro(m, SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006893#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006894#ifdef SO_ACCEPTCONN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006895 PyModule_AddIntMacro(m, SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006896#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006897#ifdef SO_REUSEADDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006898 PyModule_AddIntMacro(m, SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006899#endif
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00006900#ifdef SO_EXCLUSIVEADDRUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006901 PyModule_AddIntMacro(m, SO_EXCLUSIVEADDRUSE);
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00006902#endif
6903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006904#ifdef SO_KEEPALIVE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006905 PyModule_AddIntMacro(m, SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006906#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006907#ifdef SO_DONTROUTE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006908 PyModule_AddIntMacro(m, SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006909#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006910#ifdef SO_BROADCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006911 PyModule_AddIntMacro(m, SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006912#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006913#ifdef SO_USELOOPBACK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006914 PyModule_AddIntMacro(m, SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006915#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006916#ifdef SO_LINGER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006917 PyModule_AddIntMacro(m, SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006918#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006919#ifdef SO_OOBINLINE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006920 PyModule_AddIntMacro(m, SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006921#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +02006922#ifndef __GNU__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006923#ifdef SO_REUSEPORT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006924 PyModule_AddIntMacro(m, SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006925#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +02006926#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006927#ifdef SO_SNDBUF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006928 PyModule_AddIntMacro(m, SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006929#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006930#ifdef SO_RCVBUF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006931 PyModule_AddIntMacro(m, SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006932#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006933#ifdef SO_SNDLOWAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006934 PyModule_AddIntMacro(m, SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006935#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006936#ifdef SO_RCVLOWAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006937 PyModule_AddIntMacro(m, SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006938#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006939#ifdef SO_SNDTIMEO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006940 PyModule_AddIntMacro(m, SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006941#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006942#ifdef SO_RCVTIMEO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006943 PyModule_AddIntMacro(m, SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006944#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006945#ifdef SO_ERROR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006946 PyModule_AddIntMacro(m, SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006947#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006948#ifdef SO_TYPE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006949 PyModule_AddIntMacro(m, SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006950#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006951#ifdef SO_SETFIB
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006952 PyModule_AddIntMacro(m, SO_SETFIB);
Larry Hastingsf0f37952010-04-02 11:47:10 +00006953#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006954#ifdef SO_PASSCRED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006955 PyModule_AddIntMacro(m, SO_PASSCRED);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006956#endif
6957#ifdef SO_PEERCRED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006958 PyModule_AddIntMacro(m, SO_PEERCRED);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006959#endif
6960#ifdef LOCAL_PEERCRED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006961 PyModule_AddIntMacro(m, LOCAL_PEERCRED);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006962#endif
R David Murraybdfa0eb2016-08-23 21:12:40 -04006963#ifdef SO_PASSSEC
6964 PyModule_AddIntMacro(m, SO_PASSSEC);
6965#endif
6966#ifdef SO_PEERSEC
6967 PyModule_AddIntMacro(m, SO_PEERSEC);
6968#endif
Victor Stinnercb98bed2011-10-25 13:45:48 +02006969#ifdef SO_BINDTODEVICE
6970 PyModule_AddIntMacro(m, SO_BINDTODEVICE);
6971#endif
Benjamin Peterson1314ef72013-11-27 09:18:54 -06006972#ifdef SO_PRIORITY
6973 PyModule_AddIntMacro(m, SO_PRIORITY);
6974#endif
Charles-François Nataliea07eb92014-09-18 23:18:46 +01006975#ifdef SO_MARK
6976 PyModule_AddIntMacro(m, SO_MARK);
6977#endif
R David Murraybdfa0eb2016-08-23 21:12:40 -04006978#ifdef SO_DOMAIN
6979 PyModule_AddIntMacro(m, SO_DOMAIN);
6980#endif
6981#ifdef SO_PROTOCOL
6982 PyModule_AddIntMacro(m, SO_PROTOCOL);
6983#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006985 /* Maximum number of connections for "listen" */
6986#ifdef SOMAXCONN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006987 PyModule_AddIntMacro(m, SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006988#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006989 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006990#endif
6991
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07006992 /* Ancillary message types */
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006993#ifdef SCM_RIGHTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006994 PyModule_AddIntMacro(m, SCM_RIGHTS);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006995#endif
6996#ifdef SCM_CREDENTIALS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006997 PyModule_AddIntMacro(m, SCM_CREDENTIALS);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006998#endif
6999#ifdef SCM_CREDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007000 PyModule_AddIntMacro(m, SCM_CREDS);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10007001#endif
7002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007003 /* Flags for send, recv */
7004#ifdef MSG_OOB
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007005 PyModule_AddIntMacro(m, MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007006#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007007#ifdef MSG_PEEK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007008 PyModule_AddIntMacro(m, MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007009#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007010#ifdef MSG_DONTROUTE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007011 PyModule_AddIntMacro(m, MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007012#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007013#ifdef MSG_DONTWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007014 PyModule_AddIntMacro(m, MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00007015#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007016#ifdef MSG_EOR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007017 PyModule_AddIntMacro(m, MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007018#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007019#ifdef MSG_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007020 PyModule_AddIntMacro(m, MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007021#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007022#ifdef MSG_CTRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007023 PyModule_AddIntMacro(m, MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007024#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007025#ifdef MSG_WAITALL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007026 PyModule_AddIntMacro(m, MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007027#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007028#ifdef MSG_BTAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007029 PyModule_AddIntMacro(m, MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007030#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007031#ifdef MSG_ETAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007032 PyModule_AddIntMacro(m, MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007033#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10007034#ifdef MSG_NOSIGNAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007035 PyModule_AddIntMacro(m, MSG_NOSIGNAL);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10007036#endif
7037#ifdef MSG_NOTIFICATION
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007038 PyModule_AddIntMacro(m, MSG_NOTIFICATION);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10007039#endif
7040#ifdef MSG_CMSG_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007041 PyModule_AddIntMacro(m, MSG_CMSG_CLOEXEC);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10007042#endif
7043#ifdef MSG_ERRQUEUE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007044 PyModule_AddIntMacro(m, MSG_ERRQUEUE);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10007045#endif
7046#ifdef MSG_CONFIRM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007047 PyModule_AddIntMacro(m, MSG_CONFIRM);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10007048#endif
7049#ifdef MSG_MORE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007050 PyModule_AddIntMacro(m, MSG_MORE);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10007051#endif
7052#ifdef MSG_EOF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007053 PyModule_AddIntMacro(m, MSG_EOF);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10007054#endif
7055#ifdef MSG_BCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007056 PyModule_AddIntMacro(m, MSG_BCAST);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10007057#endif
7058#ifdef MSG_MCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007059 PyModule_AddIntMacro(m, MSG_MCAST);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10007060#endif
Benjamin Peterson57b66722012-12-12 22:24:47 -05007061#ifdef MSG_FASTOPEN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007062 PyModule_AddIntMacro(m, MSG_FASTOPEN);
Benjamin Peterson57b66722012-12-12 22:24:47 -05007063#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007065 /* Protocol level and numbers, usable for [gs]etsockopt */
7066#ifdef SOL_SOCKET
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007067 PyModule_AddIntMacro(m, SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007068#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007069#ifdef SOL_IP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007070 PyModule_AddIntMacro(m, SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00007071#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007072 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00007073#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007074#ifdef SOL_IPX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007075 PyModule_AddIntMacro(m, SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00007076#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007077#ifdef SOL_AX25
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007078 PyModule_AddIntMacro(m, SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00007079#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007080#ifdef SOL_ATALK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007081 PyModule_AddIntMacro(m, SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00007082#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007083#ifdef SOL_NETROM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007084 PyModule_AddIntMacro(m, SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00007085#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007086#ifdef SOL_ROSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007087 PyModule_AddIntMacro(m, SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00007088#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007089#ifdef SOL_TCP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007090 PyModule_AddIntMacro(m, SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00007091#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007092 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00007093#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007094#ifdef SOL_UDP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007095 PyModule_AddIntMacro(m, SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00007096#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007097 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00007098#endif
Charles-François Natali47413c12011-10-06 19:47:44 +02007099#ifdef SOL_CAN_BASE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007100 PyModule_AddIntMacro(m, SOL_CAN_BASE);
Charles-François Natali47413c12011-10-06 19:47:44 +02007101#endif
7102#ifdef SOL_CAN_RAW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007103 PyModule_AddIntMacro(m, SOL_CAN_RAW);
7104 PyModule_AddIntMacro(m, CAN_RAW);
Charles-François Natali47413c12011-10-06 19:47:44 +02007105#endif
7106#ifdef HAVE_LINUX_CAN_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007107 PyModule_AddIntMacro(m, CAN_EFF_FLAG);
7108 PyModule_AddIntMacro(m, CAN_RTR_FLAG);
7109 PyModule_AddIntMacro(m, CAN_ERR_FLAG);
Charles-François Natali47413c12011-10-06 19:47:44 +02007110
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007111 PyModule_AddIntMacro(m, CAN_SFF_MASK);
7112 PyModule_AddIntMacro(m, CAN_EFF_MASK);
7113 PyModule_AddIntMacro(m, CAN_ERR_MASK);
Charles-François Natali47413c12011-10-06 19:47:44 +02007114#endif
7115#ifdef HAVE_LINUX_CAN_RAW_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007116 PyModule_AddIntMacro(m, CAN_RAW_FILTER);
7117 PyModule_AddIntMacro(m, CAN_RAW_ERR_FILTER);
7118 PyModule_AddIntMacro(m, CAN_RAW_LOOPBACK);
7119 PyModule_AddIntMacro(m, CAN_RAW_RECV_OWN_MSGS);
Charles-François Natali47413c12011-10-06 19:47:44 +02007120#endif
Larry Hastingsa6cc5512015-04-13 17:48:40 -04007121#ifdef HAVE_LINUX_CAN_RAW_FD_FRAMES
7122 PyModule_AddIntMacro(m, CAN_RAW_FD_FRAMES);
7123#endif
Charles-François Natali773e42d2013-02-05 19:42:01 +01007124#ifdef HAVE_LINUX_CAN_BCM_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007125 PyModule_AddIntMacro(m, CAN_BCM);
Charles-François Natali773e42d2013-02-05 19:42:01 +01007126 PyModule_AddIntConstant(m, "CAN_BCM_TX_SETUP", TX_SETUP);
7127 PyModule_AddIntConstant(m, "CAN_BCM_TX_DELETE", TX_DELETE);
7128 PyModule_AddIntConstant(m, "CAN_BCM_TX_READ", TX_READ);
7129 PyModule_AddIntConstant(m, "CAN_BCM_TX_SEND", TX_SEND);
7130 PyModule_AddIntConstant(m, "CAN_BCM_RX_SETUP", RX_SETUP);
7131 PyModule_AddIntConstant(m, "CAN_BCM_RX_DELETE", RX_DELETE);
7132 PyModule_AddIntConstant(m, "CAN_BCM_RX_READ", RX_READ);
7133 PyModule_AddIntConstant(m, "CAN_BCM_TX_STATUS", TX_STATUS);
7134 PyModule_AddIntConstant(m, "CAN_BCM_TX_EXPIRED", TX_EXPIRED);
7135 PyModule_AddIntConstant(m, "CAN_BCM_RX_STATUS", RX_STATUS);
7136 PyModule_AddIntConstant(m, "CAN_BCM_RX_TIMEOUT", RX_TIMEOUT);
7137 PyModule_AddIntConstant(m, "CAN_BCM_RX_CHANGED", RX_CHANGED);
7138#endif
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007139#ifdef SOL_RDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007140 PyModule_AddIntMacro(m, SOL_RDS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007141#endif
Christian Heimesdffa3942016-09-05 23:54:41 +02007142#ifdef HAVE_SOCKADDR_ALG
7143 PyModule_AddIntMacro(m, SOL_ALG);
7144#endif
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007145#ifdef RDS_CANCEL_SENT_TO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007146 PyModule_AddIntMacro(m, RDS_CANCEL_SENT_TO);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007147#endif
7148#ifdef RDS_GET_MR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007149 PyModule_AddIntMacro(m, RDS_GET_MR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007150#endif
7151#ifdef RDS_FREE_MR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007152 PyModule_AddIntMacro(m, RDS_FREE_MR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007153#endif
7154#ifdef RDS_RECVERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007155 PyModule_AddIntMacro(m, RDS_RECVERR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007156#endif
7157#ifdef RDS_CONG_MONITOR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007158 PyModule_AddIntMacro(m, RDS_CONG_MONITOR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007159#endif
7160#ifdef RDS_GET_MR_FOR_DEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007161 PyModule_AddIntMacro(m, RDS_GET_MR_FOR_DEST);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007162#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007163#ifdef IPPROTO_IP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007164 PyModule_AddIntMacro(m, IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00007165#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007166 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007167#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007168#ifdef IPPROTO_HOPOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007169 PyModule_AddIntMacro(m, IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007170#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007171#ifdef IPPROTO_ICMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007172 PyModule_AddIntMacro(m, IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00007173#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007174 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007175#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007176#ifdef IPPROTO_IGMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007177 PyModule_AddIntMacro(m, IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007178#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007179#ifdef IPPROTO_GGP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007180 PyModule_AddIntMacro(m, IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007181#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007182#ifdef IPPROTO_IPV4
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007183 PyModule_AddIntMacro(m, IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007184#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007185#ifdef IPPROTO_IPV6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007186 PyModule_AddIntMacro(m, IPPROTO_IPV6);
Martin v. Löwisa0f17342003-10-03 13:56:20 +00007187#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007188#ifdef IPPROTO_IPIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007189 PyModule_AddIntMacro(m, IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007190#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007191#ifdef IPPROTO_TCP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007192 PyModule_AddIntMacro(m, IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00007193#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007194 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007195#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007196#ifdef IPPROTO_EGP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007197 PyModule_AddIntMacro(m, IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007198#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007199#ifdef IPPROTO_PUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007200 PyModule_AddIntMacro(m, IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007201#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007202#ifdef IPPROTO_UDP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007203 PyModule_AddIntMacro(m, IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00007204#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007205 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007206#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007207#ifdef IPPROTO_IDP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007208 PyModule_AddIntMacro(m, IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007209#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007210#ifdef IPPROTO_HELLO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007211 PyModule_AddIntMacro(m, IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007212#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007213#ifdef IPPROTO_ND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007214 PyModule_AddIntMacro(m, IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007215#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007216#ifdef IPPROTO_TP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007217 PyModule_AddIntMacro(m, IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007218#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007219#ifdef IPPROTO_IPV6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007220 PyModule_AddIntMacro(m, IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007221#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007222#ifdef IPPROTO_ROUTING
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007223 PyModule_AddIntMacro(m, IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007224#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007225#ifdef IPPROTO_FRAGMENT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007226 PyModule_AddIntMacro(m, IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007227#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007228#ifdef IPPROTO_RSVP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007229 PyModule_AddIntMacro(m, IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007230#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007231#ifdef IPPROTO_GRE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007232 PyModule_AddIntMacro(m, IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007233#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007234#ifdef IPPROTO_ESP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007235 PyModule_AddIntMacro(m, IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007236#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007237#ifdef IPPROTO_AH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007238 PyModule_AddIntMacro(m, IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007239#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007240#ifdef IPPROTO_MOBILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007241 PyModule_AddIntMacro(m, IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007242#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007243#ifdef IPPROTO_ICMPV6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007244 PyModule_AddIntMacro(m, IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007245#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007246#ifdef IPPROTO_NONE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007247 PyModule_AddIntMacro(m, IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007248#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007249#ifdef IPPROTO_DSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007250 PyModule_AddIntMacro(m, IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007251#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007252#ifdef IPPROTO_XTP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007253 PyModule_AddIntMacro(m, IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007254#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007255#ifdef IPPROTO_EON
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007256 PyModule_AddIntMacro(m, IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007257#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007258#ifdef IPPROTO_PIM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007259 PyModule_AddIntMacro(m, IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007260#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007261#ifdef IPPROTO_IPCOMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007262 PyModule_AddIntMacro(m, IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007263#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007264#ifdef IPPROTO_VRRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007265 PyModule_AddIntMacro(m, IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007266#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10007267#ifdef IPPROTO_SCTP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007268 PyModule_AddIntMacro(m, IPPROTO_SCTP);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10007269#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007270#ifdef IPPROTO_BIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007271 PyModule_AddIntMacro(m, IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007272#endif
7273/**/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007274#ifdef IPPROTO_RAW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007275 PyModule_AddIntMacro(m, IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00007276#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007277 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007278#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007279#ifdef IPPROTO_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007280 PyModule_AddIntMacro(m, IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007281#endif
7282
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01007283#ifdef SYSPROTO_CONTROL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007284 PyModule_AddIntMacro(m, SYSPROTO_CONTROL);
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01007285#endif
7286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007287 /* Some port configuration */
7288#ifdef IPPORT_RESERVED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007289 PyModule_AddIntMacro(m, IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007290#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007291 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007292#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007293#ifdef IPPORT_USERRESERVED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007294 PyModule_AddIntMacro(m, IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007295#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007296 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007297#endif
7298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007299 /* Some reserved IP v.4 addresses */
7300#ifdef INADDR_ANY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007301 PyModule_AddIntMacro(m, INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00007302#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007303 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00007304#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007305#ifdef INADDR_BROADCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007306 PyModule_AddIntMacro(m, INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00007307#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007308 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00007309#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007310#ifdef INADDR_LOOPBACK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007311 PyModule_AddIntMacro(m, INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00007312#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007313 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00007314#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007315#ifdef INADDR_UNSPEC_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007316 PyModule_AddIntMacro(m, INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00007317#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007318 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00007319#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007320#ifdef INADDR_ALLHOSTS_GROUP
7321 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
7322 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00007323#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007324 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00007325#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007326#ifdef INADDR_MAX_LOCAL_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007327 PyModule_AddIntMacro(m, INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00007328#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007329 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00007330#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007331#ifdef INADDR_NONE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007332 PyModule_AddIntMacro(m, INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00007333#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007334 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00007335#endif
7336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007337 /* IPv4 [gs]etsockopt options */
7338#ifdef IP_OPTIONS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007339 PyModule_AddIntMacro(m, IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007340#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007341#ifdef IP_HDRINCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007342 PyModule_AddIntMacro(m, IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007343#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007344#ifdef IP_TOS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007345 PyModule_AddIntMacro(m, IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007346#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007347#ifdef IP_TTL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007348 PyModule_AddIntMacro(m, IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007349#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007350#ifdef IP_RECVOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007351 PyModule_AddIntMacro(m, IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007352#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007353#ifdef IP_RECVRETOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007354 PyModule_AddIntMacro(m, IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007355#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007356#ifdef IP_RECVDSTADDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007357 PyModule_AddIntMacro(m, IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007358#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007359#ifdef IP_RETOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007360 PyModule_AddIntMacro(m, IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007361#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007362#ifdef IP_MULTICAST_IF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007363 PyModule_AddIntMacro(m, IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007364#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007365#ifdef IP_MULTICAST_TTL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007366 PyModule_AddIntMacro(m, IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007367#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007368#ifdef IP_MULTICAST_LOOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007369 PyModule_AddIntMacro(m, IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007370#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007371#ifdef IP_ADD_MEMBERSHIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007372 PyModule_AddIntMacro(m, IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007373#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007374#ifdef IP_DROP_MEMBERSHIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007375 PyModule_AddIntMacro(m, IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00007376#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007377#ifdef IP_DEFAULT_MULTICAST_TTL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007378 PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00007379#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007380#ifdef IP_DEFAULT_MULTICAST_LOOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007381 PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00007382#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007383#ifdef IP_MAX_MEMBERSHIPS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007384 PyModule_AddIntMacro(m, IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00007385#endif
Charles-François Natali564a42c2011-12-17 14:59:56 +01007386#ifdef IP_TRANSPARENT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007387 PyModule_AddIntMacro(m, IP_TRANSPARENT);
Charles-François Natali564a42c2011-12-17 14:59:56 +01007388#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00007389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007390 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
7391#ifdef IPV6_JOIN_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007392 PyModule_AddIntMacro(m, IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007393#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007394#ifdef IPV6_LEAVE_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007395 PyModule_AddIntMacro(m, IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007396#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007397#ifdef IPV6_MULTICAST_HOPS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007398 PyModule_AddIntMacro(m, IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007399#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007400#ifdef IPV6_MULTICAST_IF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007401 PyModule_AddIntMacro(m, IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007402#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007403#ifdef IPV6_MULTICAST_LOOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007404 PyModule_AddIntMacro(m, IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007405#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007406#ifdef IPV6_UNICAST_HOPS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007407 PyModule_AddIntMacro(m, IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007408#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007409 /* Additional IPV6 socket options, defined in RFC 3493 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00007410#ifdef IPV6_V6ONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007411 PyModule_AddIntMacro(m, IPV6_V6ONLY);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007412#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007413 /* Advanced IPV6 socket options, from RFC 3542 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00007414#ifdef IPV6_CHECKSUM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007415 PyModule_AddIntMacro(m, IPV6_CHECKSUM);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007416#endif
7417#ifdef IPV6_DONTFRAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007418 PyModule_AddIntMacro(m, IPV6_DONTFRAG);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007419#endif
7420#ifdef IPV6_DSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007421 PyModule_AddIntMacro(m, IPV6_DSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007422#endif
7423#ifdef IPV6_HOPLIMIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007424 PyModule_AddIntMacro(m, IPV6_HOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007425#endif
7426#ifdef IPV6_HOPOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007427 PyModule_AddIntMacro(m, IPV6_HOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007428#endif
7429#ifdef IPV6_NEXTHOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007430 PyModule_AddIntMacro(m, IPV6_NEXTHOP);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007431#endif
7432#ifdef IPV6_PATHMTU
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007433 PyModule_AddIntMacro(m, IPV6_PATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007434#endif
7435#ifdef IPV6_PKTINFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007436 PyModule_AddIntMacro(m, IPV6_PKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007437#endif
7438#ifdef IPV6_RECVDSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007439 PyModule_AddIntMacro(m, IPV6_RECVDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007440#endif
7441#ifdef IPV6_RECVHOPLIMIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007442 PyModule_AddIntMacro(m, IPV6_RECVHOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007443#endif
7444#ifdef IPV6_RECVHOPOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007445 PyModule_AddIntMacro(m, IPV6_RECVHOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007446#endif
7447#ifdef IPV6_RECVPKTINFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007448 PyModule_AddIntMacro(m, IPV6_RECVPKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007449#endif
7450#ifdef IPV6_RECVRTHDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007451 PyModule_AddIntMacro(m, IPV6_RECVRTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007452#endif
7453#ifdef IPV6_RECVTCLASS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007454 PyModule_AddIntMacro(m, IPV6_RECVTCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007455#endif
7456#ifdef IPV6_RTHDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007457 PyModule_AddIntMacro(m, IPV6_RTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007458#endif
7459#ifdef IPV6_RTHDRDSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007460 PyModule_AddIntMacro(m, IPV6_RTHDRDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007461#endif
7462#ifdef IPV6_RTHDR_TYPE_0
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007463 PyModule_AddIntMacro(m, IPV6_RTHDR_TYPE_0);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007464#endif
7465#ifdef IPV6_RECVPATHMTU
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007466 PyModule_AddIntMacro(m, IPV6_RECVPATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007467#endif
7468#ifdef IPV6_TCLASS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007469 PyModule_AddIntMacro(m, IPV6_TCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007470#endif
7471#ifdef IPV6_USE_MIN_MTU
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007472 PyModule_AddIntMacro(m, IPV6_USE_MIN_MTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007473#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00007474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007475 /* TCP options */
7476#ifdef TCP_NODELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007477 PyModule_AddIntMacro(m, TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00007478#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007479#ifdef TCP_MAXSEG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007480 PyModule_AddIntMacro(m, TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00007481#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007482#ifdef TCP_CORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007483 PyModule_AddIntMacro(m, TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00007484#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007485#ifdef TCP_KEEPIDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007486 PyModule_AddIntMacro(m, TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00007487#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007488#ifdef TCP_KEEPINTVL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007489 PyModule_AddIntMacro(m, TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00007490#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007491#ifdef TCP_KEEPCNT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007492 PyModule_AddIntMacro(m, TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00007493#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007494#ifdef TCP_SYNCNT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007495 PyModule_AddIntMacro(m, TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00007496#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007497#ifdef TCP_LINGER2
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007498 PyModule_AddIntMacro(m, TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00007499#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007500#ifdef TCP_DEFER_ACCEPT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007501 PyModule_AddIntMacro(m, TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00007502#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007503#ifdef TCP_WINDOW_CLAMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007504 PyModule_AddIntMacro(m, TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00007505#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007506#ifdef TCP_INFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007507 PyModule_AddIntMacro(m, TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00007508#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007509#ifdef TCP_QUICKACK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007510 PyModule_AddIntMacro(m, TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00007511#endif
Benjamin Peterson57b66722012-12-12 22:24:47 -05007512#ifdef TCP_FASTOPEN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007513 PyModule_AddIntMacro(m, TCP_FASTOPEN);
Benjamin Peterson57b66722012-12-12 22:24:47 -05007514#endif
Victor Stinner11dd6042016-11-29 16:55:04 +01007515#ifdef TCP_CONGESTION
7516 PyModule_AddIntMacro(m, TCP_CONGESTION);
7517#endif
7518#ifdef TCP_USER_TIMEOUT
7519 PyModule_AddIntMacro(m, TCP_USER_TIMEOUT);
7520#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00007521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007522 /* IPX options */
7523#ifdef IPX_TYPE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007524 PyModule_AddIntMacro(m, IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00007525#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00007526
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007527/* Reliable Datagram Sockets */
7528#ifdef RDS_CMSG_RDMA_ARGS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007529 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_ARGS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007530#endif
7531#ifdef RDS_CMSG_RDMA_DEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007532 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_DEST);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007533#endif
7534#ifdef RDS_CMSG_RDMA_MAP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007535 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_MAP);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007536#endif
7537#ifdef RDS_CMSG_RDMA_STATUS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007538 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_STATUS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007539#endif
7540#ifdef RDS_CMSG_RDMA_UPDATE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007541 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_UPDATE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007542#endif
7543#ifdef RDS_RDMA_READWRITE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007544 PyModule_AddIntMacro(m, RDS_RDMA_READWRITE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007545#endif
7546#ifdef RDS_RDMA_FENCE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007547 PyModule_AddIntMacro(m, RDS_RDMA_FENCE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007548#endif
7549#ifdef RDS_RDMA_INVALIDATE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007550 PyModule_AddIntMacro(m, RDS_RDMA_INVALIDATE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007551#endif
7552#ifdef RDS_RDMA_USE_ONCE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007553 PyModule_AddIntMacro(m, RDS_RDMA_USE_ONCE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007554#endif
7555#ifdef RDS_RDMA_DONTWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007556 PyModule_AddIntMacro(m, RDS_RDMA_DONTWAIT);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007557#endif
7558#ifdef RDS_RDMA_NOTIFY_ME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007559 PyModule_AddIntMacro(m, RDS_RDMA_NOTIFY_ME);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007560#endif
7561#ifdef RDS_RDMA_SILENT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007562 PyModule_AddIntMacro(m, RDS_RDMA_SILENT);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01007563#endif
7564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007565 /* get{addr,name}info parameters */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007566#ifdef EAI_ADDRFAMILY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007567 PyModule_AddIntMacro(m, EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007568#endif
7569#ifdef EAI_AGAIN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007570 PyModule_AddIntMacro(m, EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007571#endif
7572#ifdef EAI_BADFLAGS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007573 PyModule_AddIntMacro(m, EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007574#endif
7575#ifdef EAI_FAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007576 PyModule_AddIntMacro(m, EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007577#endif
7578#ifdef EAI_FAMILY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007579 PyModule_AddIntMacro(m, EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007580#endif
7581#ifdef EAI_MEMORY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007582 PyModule_AddIntMacro(m, EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007583#endif
7584#ifdef EAI_NODATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007585 PyModule_AddIntMacro(m, EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007586#endif
7587#ifdef EAI_NONAME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007588 PyModule_AddIntMacro(m, EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007589#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00007590#ifdef EAI_OVERFLOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007591 PyModule_AddIntMacro(m, EAI_OVERFLOW);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007592#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007593#ifdef EAI_SERVICE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007594 PyModule_AddIntMacro(m, EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007595#endif
7596#ifdef EAI_SOCKTYPE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007597 PyModule_AddIntMacro(m, EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007598#endif
7599#ifdef EAI_SYSTEM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007600 PyModule_AddIntMacro(m, EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007601#endif
7602#ifdef EAI_BADHINTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007603 PyModule_AddIntMacro(m, EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007604#endif
7605#ifdef EAI_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007606 PyModule_AddIntMacro(m, EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007607#endif
7608#ifdef EAI_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007609 PyModule_AddIntMacro(m, EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007610#endif
7611#ifdef AI_PASSIVE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007612 PyModule_AddIntMacro(m, AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007613#endif
7614#ifdef AI_CANONNAME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007615 PyModule_AddIntMacro(m, AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007616#endif
7617#ifdef AI_NUMERICHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007618 PyModule_AddIntMacro(m, AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007619#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00007620#ifdef AI_NUMERICSERV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007621 PyModule_AddIntMacro(m, AI_NUMERICSERV);
Martin v. Löwisda91d022003-12-30 11:14:01 +00007622#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007623#ifdef AI_MASK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007624 PyModule_AddIntMacro(m, AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007625#endif
7626#ifdef AI_ALL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007627 PyModule_AddIntMacro(m, AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007628#endif
7629#ifdef AI_V4MAPPED_CFG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007630 PyModule_AddIntMacro(m, AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007631#endif
7632#ifdef AI_ADDRCONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007633 PyModule_AddIntMacro(m, AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007634#endif
7635#ifdef AI_V4MAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007636 PyModule_AddIntMacro(m, AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007637#endif
7638#ifdef AI_DEFAULT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007639 PyModule_AddIntMacro(m, AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007640#endif
7641#ifdef NI_MAXHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007642 PyModule_AddIntMacro(m, NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007643#endif
7644#ifdef NI_MAXSERV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007645 PyModule_AddIntMacro(m, NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007646#endif
7647#ifdef NI_NOFQDN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007648 PyModule_AddIntMacro(m, NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007649#endif
7650#ifdef NI_NUMERICHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007651 PyModule_AddIntMacro(m, NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007652#endif
7653#ifdef NI_NAMEREQD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007654 PyModule_AddIntMacro(m, NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007655#endif
7656#ifdef NI_NUMERICSERV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007657 PyModule_AddIntMacro(m, NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007658#endif
7659#ifdef NI_DGRAM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007660 PyModule_AddIntMacro(m, NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007661#endif
7662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007663 /* shutdown() parameters */
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007664#ifdef SHUT_RD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007665 PyModule_AddIntMacro(m, SHUT_RD);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007666#elif defined(SD_RECEIVE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007667 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007668#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007669 PyModule_AddIntConstant(m, "SHUT_RD", 0);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007670#endif
7671#ifdef SHUT_WR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007672 PyModule_AddIntMacro(m, SHUT_WR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007673#elif defined(SD_SEND)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007674 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007675#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007676 PyModule_AddIntConstant(m, "SHUT_WR", 1);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007677#endif
7678#ifdef SHUT_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007679 PyModule_AddIntMacro(m, SHUT_RDWR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007680#elif defined(SD_BOTH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007681 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007682#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007683 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007684#endif
7685
Christian Heimesfaf2f632008-01-06 16:59:19 +00007686#ifdef SIO_RCVALL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007687 {
Steve Dowerea93ac02016-06-17 12:52:18 -07007688 DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS,
7689#if defined(SIO_LOOPBACK_FAST_PATH)
Berker Peksag3583c3b2016-06-18 16:43:25 +03007690 SIO_LOOPBACK_FAST_PATH
Steve Dowerea93ac02016-06-17 12:52:18 -07007691#endif
Berker Peksag3583c3b2016-06-18 16:43:25 +03007692 };
Steve Dowerea93ac02016-06-17 12:52:18 -07007693 const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS",
7694#if defined(SIO_LOOPBACK_FAST_PATH)
Berker Peksag3583c3b2016-06-18 16:43:25 +03007695 "SIO_LOOPBACK_FAST_PATH"
Steve Dowerea93ac02016-06-17 12:52:18 -07007696#endif
Berker Peksag3583c3b2016-06-18 16:43:25 +03007697 };
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007698 int i;
Victor Stinner63941882011-09-29 00:42:28 +02007699 for(i = 0; i<Py_ARRAY_LENGTH(codes); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007700 PyObject *tmp;
7701 tmp = PyLong_FromUnsignedLong(codes[i]);
7702 if (tmp == NULL)
7703 return NULL;
7704 PyModule_AddObject(m, names[i], tmp);
7705 }
7706 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007707 PyModule_AddIntMacro(m, RCVALL_OFF);
7708 PyModule_AddIntMacro(m, RCVALL_ON);
7709 PyModule_AddIntMacro(m, RCVALL_SOCKETLEVELONLY);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00007710#ifdef RCVALL_IPLEVEL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007711 PyModule_AddIntMacro(m, RCVALL_IPLEVEL);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00007712#endif
7713#ifdef RCVALL_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007714 PyModule_AddIntMacro(m, RCVALL_MAX);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00007715#endif
Christian Heimesfaf2f632008-01-06 16:59:19 +00007716#endif /* _MSTCPIP_ */
7717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007718 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00007719#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007720 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00007721#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007722 return m;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00007723}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007724
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007725
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007726#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +00007727#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007728
7729/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00007730/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007731
Guido van Rossum3eede5a2002-06-07 02:08:35 +00007732int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007733inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007735 if (af == AF_INET) {
Benjamin Petersonf91df042009-02-13 02:50:59 +00007736#if (SIZEOF_INT != 4)
7737#error "Not sure if in_addr_t exists and int is not 32-bits."
7738#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007739 unsigned int packed_addr;
7740 packed_addr = inet_addr(src);
7741 if (packed_addr == INADDR_NONE)
7742 return 0;
7743 memcpy(dst, &packed_addr, 4);
7744 return 1;
7745 }
7746 /* Should set errno to EAFNOSUPPORT */
7747 return -1;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007748}
7749
Martin v. Löwisc925b1532001-07-21 09:42:15 +00007750const char *
7751inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007753 if (af == AF_INET) {
7754 struct in_addr packed_addr;
7755 if (size < 16)
7756 /* Should set errno to ENOSPC. */
7757 return NULL;
7758 memcpy(&packed_addr, src, sizeof(packed_addr));
7759 return strncpy(dst, inet_ntoa(packed_addr), size);
7760 }
7761 /* Should set errno to EAFNOSUPPORT */
7762 return NULL;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007763}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007764
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007765#endif
Christian Heimesb6150692007-11-15 23:37:07 +00007766#endif