blob: 39003be079152a6a8864fd5a33e9d3096ab7cfbc [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__
87 /*
88 * inet_aton is not available on OSX 10.3, yet we want to use a binary
89 * that was build on 10.4 or later to work on that release, weak linking
90 * comes to the rescue.
91 */
92# pragma weak inet_aton
93#endif
94
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000095#include "Python.h"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000096#include "structmember.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000097
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000098/* Socket object documentation */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000099PyDoc_STRVAR(sock_doc,
Victor Stinnerdaf45552013-08-28 00:53:59 +0200100"socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None) -> socket object\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000101\n\
102Open a socket of the given type. The family argument specifies the\n\
103address family; it defaults to AF_INET. The type argument specifies\n\
104whether this is a stream (SOCK_STREAM, this is the default)\n\
105or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
106specifying the default protocol. Keyword arguments are accepted.\n\
Victor Stinnerdaf45552013-08-28 00:53:59 +0200107The socket is created as non-inheritable.\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000108\n\
109A socket object represents one endpoint of a network connection.\n\
110\n\
111Methods of socket objects (keyword arguments not allowed):\n\
112\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000113_accept() -- accept connection, returning new socket fd and client address\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000114bind(addr) -- bind the socket to a local address\n\
115close() -- close the socket\n\
116connect(addr) -- connect the socket to a remote address\n\
117connect_ex(addr) -- connect, return an error code instead of an exception\n\
Victor Stinnerdaf45552013-08-28 00:53:59 +0200118dup() -- return a new socket fd duplicated from fileno()\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000119fileno() -- return underlying file descriptor\n\
120getpeername() -- return remote address [*]\n\
121getsockname() -- return local address\n\
122getsockopt(level, optname[, buflen]) -- get socket options\n\
123gettimeout() -- return timeout or None\n\
Charles-François Natali644b8f52014-05-22 19:45:39 +0100124listen([n]) -- start listening for incoming connections\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000125recv(buflen[, flags]) -- receive data\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000126recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000127recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000128recvfrom_into(buffer[, nbytes, [, flags])\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000129 -- receive data and sender\'s address (into a buffer)\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000130sendall(data[, flags]) -- send all data\n\
131send(data[, flags]) -- send data, may not send all of it\n\
132sendto(data[, flags], addr) -- send data to a given address\n\
133setblocking(0 | 1) -- set or clear the blocking I/O flag\n\
134setsockopt(level, optname, value) -- set socket options\n\
135settimeout(None | float) -- set or clear the timeout\n\
136shutdown(how) -- shut down traffic in one or both directions\n\
Gregory P. Smith5ed2e772011-05-15 00:26:45 -0700137if_nameindex() -- return all network interface indices and names\n\
Gregory P. Smithb6471db2011-05-22 22:47:55 -0700138if_nametoindex(name) -- return the corresponding interface index\n\
139if_indextoname(index) -- return the corresponding interface name\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000140\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000141 [*] not available on all platforms!");
Guido van Rossum3baaa131999-03-22 21:44:51 +0000142
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000143/* XXX This is a terrible mess of platform-dependent preprocessor hacks.
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000144 I hope some day someone can clean this up please... */
145
Guido van Rossum9376b741999-09-15 22:01:40 +0000146/* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
147 script doesn't get this right, so we hardcode some platform checks below.
148 On the other hand, not all Linux versions agree, so there the settings
149 computed by the configure script are needed! */
150
151#ifndef linux
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000152# undef HAVE_GETHOSTBYNAME_R_3_ARG
153# undef HAVE_GETHOSTBYNAME_R_5_ARG
154# undef HAVE_GETHOSTBYNAME_R_6_ARG
Guido van Rossum9376b741999-09-15 22:01:40 +0000155#endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000156
Victor Stinner710d27e2011-08-23 10:57:32 +0200157#if defined(__OpenBSD__)
158# include <sys/uio.h>
159#endif
160
Guido van Rossum7a122991999-04-13 04:07:32 +0000161#ifndef WITH_THREAD
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000162# undef HAVE_GETHOSTBYNAME_R
Guido van Rossum7a122991999-04-13 04:07:32 +0000163#endif
164
Guido van Rossume7de2061999-03-24 17:24:33 +0000165#ifdef HAVE_GETHOSTBYNAME_R
Jesus Cea736e7fc2011-03-14 17:36:54 +0100166# if defined(_AIX)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000167# define HAVE_GETHOSTBYNAME_R_3_ARG
168# elif defined(__sun) || defined(__sgi)
169# define HAVE_GETHOSTBYNAME_R_5_ARG
170# elif defined(linux)
Guido van Rossum9376b741999-09-15 22:01:40 +0000171/* Rely on the configure script */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000172# else
173# undef HAVE_GETHOSTBYNAME_R
174# endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000175#endif
176
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000177#if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \
178 !defined(MS_WINDOWS)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000179# define USE_GETHOSTBYNAME_LOCK
Guido van Rossum3baaa131999-03-22 21:44:51 +0000180#endif
181
Hye-Shik Chang9ceebd52005-09-24 14:58:47 +0000182/* To use __FreeBSD_version */
183#ifdef HAVE_SYS_PARAM_H
184#include <sys/param.h>
185#endif
Just van Rossum1040d2c2003-05-09 07:53:18 +0000186/* On systems on which getaddrinfo() is believed to not be thread-safe,
Just van Rossum09aecd72003-05-09 08:03:44 +0000187 (this includes the getaddrinfo emulation) protect access with a lock. */
Hye-Shik Chang9ceebd52005-09-24 14:58:47 +0000188#if defined(WITH_THREAD) && (defined(__APPLE__) || \
189 (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000190 defined(__OpenBSD__) || defined(__NetBSD__) || \
Christian Heimesaf01f662013-12-21 16:19:10 +0100191 !defined(HAVE_GETADDRINFO))
Just van Rossum1040d2c2003-05-09 07:53:18 +0000192#define USE_GETADDRINFO_LOCK
193#endif
194
195#ifdef USE_GETADDRINFO_LOCK
196#define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1);
197#define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock);
198#else
199#define ACQUIRE_GETADDRINFO_LOCK
200#define RELEASE_GETADDRINFO_LOCK
201#endif
202
203#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000204# include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000205#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000206
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000207#if defined(PYCC_VACPP)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000208# include <types.h>
209# include <io.h>
210# include <sys/ioctl.h>
211# include <utils.h>
212# include <ctype.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000213#endif
214
Martin v. Löwis9d6c6692012-02-03 17:44:58 +0100215#ifdef __APPLE__
216# include <sys/ioctl.h>
217#endif
218
219
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000220#if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000221/* make sure that the reentrant (gethostbyaddr_r etc)
222 functions are declared correctly if compiling with
223 MIPSPro 7.x in ANSI C mode (default) */
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000224
Thomas Wouters477c8d52006-05-27 19:21:47 +0000225/* XXX Using _SGIAPI is the wrong thing,
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000226 but I don't know what the right thing is. */
Trent Mick8ea5bdf2004-09-13 17:48:41 +0000227#undef _SGIAPI /* to avoid warning */
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000228#define _SGIAPI 1
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000229
Trent Mick8ea5bdf2004-09-13 17:48:41 +0000230#undef _XOPEN_SOURCE
231#include <sys/socket.h>
232#include <sys/types.h>
233#include <netinet/in.h>
234#ifdef _SS_ALIGNSIZE
235#define HAVE_GETADDRINFO 1
236#define HAVE_GETNAMEINFO 1
237#endif
238
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000239#define HAVE_INET_PTON
240#include <netdb.h>
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000241#endif
242
Thomas Wouters477c8d52006-05-27 19:21:47 +0000243/* Irix 6.5 fails to define this variable at all. This is needed
244 for both GCC and SGI's compiler. I'd say that the SGI headers
Georg Brandldbd83392006-02-20 09:42:33 +0000245 are just busted. Same thing for Solaris. */
246#if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN)
Anthony Baxterbab23cf2003-10-04 08:00:49 +0000247#define INET_ADDRSTRLEN 16
248#endif
249
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000250/* Generic includes */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000251#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000253#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000254
Gregory P. Smith3b1f2c32011-05-15 12:18:23 -0700255#ifdef HAVE_SYS_SOCKET_H
256#include <sys/socket.h>
257#endif
258
259#ifdef HAVE_NET_IF_H
260#include <net/if.h>
261#endif
262
Marc-André Lemburg976ade62002-02-16 18:47:07 +0000263/* Generic socket object definitions and includes */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000264#define PySocket_BUILDING_SOCKET
Marc-André Lemburgbb8b78b2002-02-16 18:44:52 +0000265#include "socketmodule.h"
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000266
267/* Addressing includes */
268
Guido van Rossum6f489d91996-06-28 20:15:15 +0000269#ifndef MS_WINDOWS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000270
271/* Non-MS WINDOWS includes */
272# include <netdb.h>
Nick Coghlan4ecf6cf2011-08-22 15:45:19 +1000273# include <unistd.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000274
Guido van Rossum9376b741999-09-15 22:01:40 +0000275/* Headers needed for inet_ntoa() and inet_addr() */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000276# include <arpa/inet.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000277
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000278# include <fcntl.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000279
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000280#else
Guido van Rossum48a680c2001-03-02 06:34:14 +0000281
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000282/* MS_WINDOWS includes */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000283# ifdef HAVE_FCNTL_H
284# include <fcntl.h>
285# endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000286
Steve Dower65e4cb12014-11-22 12:54:57 -0800287#if defined(_MSC_VER) && _MSC_VER >= 1800
288/* Provides the IsWindows7SP1OrGreater() function */
289#include <VersionHelpers.h>
290#endif
291
Jeremy Hylton22308652001-02-02 03:23:09 +0000292#endif
293
Skip Montanaro7befb992004-02-10 16:50:21 +0000294#include <stddef.h>
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000295
Neal Norwitz39d22e52002-11-02 19:55:21 +0000296#ifndef O_NONBLOCK
297# define O_NONBLOCK O_NDELAY
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000298#endif
299
Trent Micka708d6e2004-09-07 17:48:26 +0000300/* include Python's addrinfo.h unless it causes trouble */
301#if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE)
302 /* Do not include addinfo.h on some newer IRIX versions.
303 * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21,
304 * for example, but not by 6.5.10.
305 */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000306#elif defined(_MSC_VER) && _MSC_VER>1201
Trent Micka708d6e2004-09-07 17:48:26 +0000307 /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and
308 * EAI_* constants are defined in (the already included) ws2tcpip.h.
309 */
310#else
311# include "addrinfo.h"
312#endif
Jason Tishlerc246cb72004-08-09 13:25:59 +0000313
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000314#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +0000315#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000316int inet_pton(int af, const char *src, void *dst);
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000317const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000318#endif
Christian Heimesb6150692007-11-15 23:37:07 +0000319#endif
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000320
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000321#ifdef __APPLE__
322/* On OS X, getaddrinfo returns no error indication of lookup
323 failure, so we must use the emulation instead of the libinfo
324 implementation. Unfortunately, performing an autoconf test
325 for this bug would require DNS access for the machine performing
326 the configuration, which is not acceptable. Therefore, we
327 determine the bug just by checking for __APPLE__. If this bug
328 gets ever fixed, perhaps checking for sys/version.h would be
329 appropriate, which is 10/0 on the system with the bug. */
Jack Jansen84262fb2002-07-02 14:40:42 +0000330#ifndef HAVE_GETNAMEINFO
331/* This bug seems to be fixed in Jaguar. Ths easiest way I could
332 Find to check for Jaguar is that it has getnameinfo(), which
333 older releases don't have */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000334#undef HAVE_GETADDRINFO
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000335#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000336
337#ifdef HAVE_INET_ATON
338#define USE_INET_ATON_WEAKLINK
339#endif
340
Jack Jansen84262fb2002-07-02 14:40:42 +0000341#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000342
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000343/* I know this is a bad practice, but it is the easiest... */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000344#if !defined(HAVE_GETADDRINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000345/* avoid clashes with the C library definition of the symbol. */
346#define getaddrinfo fake_getaddrinfo
347#define gai_strerror fake_gai_strerror
348#define freeaddrinfo fake_freeaddrinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000349#include "getaddrinfo.c"
350#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000351#if !defined(HAVE_GETNAMEINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000352#define getnameinfo fake_getnameinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000353#include "getnameinfo.c"
354#endif
355
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000356#ifdef MS_WINDOWS
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000357#define SOCKETCLOSE closesocket
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000358#endif
359
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000360#ifdef MS_WIN32
Kristján Valur Jónsson310052c2012-03-31 13:35:00 +0000361#undef EAFNOSUPPORT
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000362#define EAFNOSUPPORT WSAEAFNOSUPPORT
363#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000364#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000365
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000366#ifndef SOCKETCLOSE
367#define SOCKETCLOSE close
368#endif
369
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +0000370#if (defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)) && !defined(__NetBSD__) && !defined(__DragonFly__)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000371#define USE_BLUETOOTH 1
372#if defined(__FreeBSD__)
373#define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP
374#define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM
Thomas Wouterscf297e42007-02-23 15:07:44 +0000375#define BTPROTO_HCI BLUETOOTH_PROTO_HCI
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000376#define SOL_HCI SOL_HCI_RAW
377#define HCI_FILTER SO_HCI_RAW_FILTER
Hye-Shik Chang81268602004-02-02 06:05:24 +0000378#define sockaddr_l2 sockaddr_l2cap
379#define sockaddr_rc sockaddr_rfcomm
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000380#define hci_dev hci_node
Hye-Shik Chang81268602004-02-02 06:05:24 +0000381#define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb)
382#define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000383#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +0000384#elif defined(__NetBSD__) || defined(__DragonFly__)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000385#define sockaddr_l2 sockaddr_bt
386#define sockaddr_rc sockaddr_bt
Thomas Wouterscf297e42007-02-23 15:07:44 +0000387#define sockaddr_hci sockaddr_bt
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000388#define sockaddr_sco sockaddr_bt
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +0000389#define SOL_HCI BTPROTO_HCI
390#define HCI_DATA_DIR SO_HCI_DIRECTION
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000391#define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb)
392#define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000393#define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000394#define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000395#else
Hye-Shik Chang81268602004-02-02 06:05:24 +0000396#define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)
397#define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000398#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000399#define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)
400#endif
401#endif
402
Charles-François Natali8b759652011-12-23 16:44:51 +0100403/* Convert "sock_addr_t *" to "struct sockaddr *". */
404#define SAS2SA(x) (&((x)->sa))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000405
Martin v. Löwise9416172003-05-03 10:12:45 +0000406/*
407 * Constants for getnameinfo()
408 */
409#if !defined(NI_MAXHOST)
410#define NI_MAXHOST 1025
411#endif
412#if !defined(NI_MAXSERV)
413#define NI_MAXSERV 32
414#endif
415
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000416#ifndef INVALID_SOCKET /* MS defines this */
417#define INVALID_SOCKET (-1)
418#endif
419
Charles-François Natali0cc86852013-09-13 19:53:08 +0200420#ifndef INADDR_NONE
421#define INADDR_NONE (-1)
422#endif
423
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000424/* XXX There's a problem here: *static* functions are not supposed to have
425 a Py prefix (or use CapitalizedWords). Later... */
426
Guido van Rossum30a685f1991-06-27 15:51:29 +0000427/* Global variable holding the exception type for errors detected
428 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000429static PyObject *socket_herror;
430static PyObject *socket_gaierror;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000431static PyObject *socket_timeout;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000432
Tim Peters643a7fc2002-02-17 04:13:21 +0000433/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000434 The sock_type variable contains pointers to various functions,
435 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000436 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000437static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000438
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000439#if defined(HAVE_POLL_H)
440#include <poll.h>
441#elif defined(HAVE_SYS_POLL_H)
442#include <sys/poll.h>
443#endif
444
Nick Coghlan96fe56a2011-08-22 11:55:57 +1000445/* Largest value to try to store in a socklen_t (used when handling
446 ancillary data). POSIX requires socklen_t to hold at least
447 (2**31)-1 and recommends against storing larger values, but
448 socklen_t was originally int in the BSD interface, so to be on the
449 safe side we use the smaller of (2**31)-1 and INT_MAX. */
450#if INT_MAX > 0x7fffffff
451#define SOCKLEN_T_LIMIT 0x7fffffff
452#else
453#define SOCKLEN_T_LIMIT INT_MAX
454#endif
455
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200456#ifdef HAVE_POLL
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000457/* Instead of select(), we'll use poll() since poll() works on any fd. */
458#define IS_SELECTABLE(s) 1
459/* Can we call select() with this socket without a buffer overrun? */
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000460#else
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200461/* If there's no timeout left, we don't have to call select, so it's a safe,
462 * little white lie. */
Victor Stinner71694d52015-03-28 01:18:54 +0100463#define IS_SELECTABLE(s) (_PyIsSelectable_fd((s)->sock_fd) || (s)->sock_timeout <= 0)
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000464#endif
Neal Norwitz082b2df2006-02-07 07:04:46 +0000465
466static PyObject*
467select_error(void)
468{
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200469 PyErr_SetString(PyExc_OSError, "unable to select on socket");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 return NULL;
Neal Norwitz082b2df2006-02-07 07:04:46 +0000471}
472
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000473#ifdef MS_WINDOWS
Antoine Pitrouc8f01962010-09-28 22:03:27 +0000474#ifndef WSAEAGAIN
475#define WSAEAGAIN WSAEWOULDBLOCK
476#endif
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000477#define CHECK_ERRNO(expected) \
478 (WSAGetLastError() == WSA ## expected)
479#else
480#define CHECK_ERRNO(expected) \
481 (errno == expected)
482#endif
483
Victor Stinnerdaf45552013-08-28 00:53:59 +0200484#ifdef MS_WINDOWS
485/* Does WSASocket() support the WSA_FLAG_NO_HANDLE_INHERIT flag? */
486static int support_wsa_no_inherit = -1;
487#endif
488
Guido van Rossum30a685f1991-06-27 15:51:29 +0000489/* Convenience function to raise an error according to errno
490 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000491
Guido van Rossum73624e91994-10-10 17:59:00 +0000492static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000493set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000494{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000495#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 int err_no = WSAGetLastError();
497 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
498 recognizes the error codes used by both GetLastError() and
499 WSAGetLastError */
500 if (err_no)
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200501 return PyErr_SetExcFromWindowsErr(PyExc_OSError, err_no);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000502#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000503
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200504 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000505}
506
Guido van Rossum30a685f1991-06-27 15:51:29 +0000507
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000508static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000509set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000512
513#ifdef HAVE_HSTRERROR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000515#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 v = Py_BuildValue("(is)", h_error, "host not found");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000517#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 if (v != NULL) {
519 PyErr_SetObject(socket_herror, v);
520 Py_DECREF(v);
521 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000524}
525
526
527static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000528set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000531
Martin v. Löwis272cb402002-03-01 08:31:07 +0000532#ifdef EAI_SYSTEM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 /* EAI_SYSTEM is not available on Windows XP. */
534 if (error == EAI_SYSTEM)
535 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000536#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000537
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000538#ifdef HAVE_GAI_STRERROR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000540#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000542#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 if (v != NULL) {
544 PyErr_SetObject(socket_gaierror, v);
545 Py_DECREF(v);
546 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000549}
550
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000551/* Function to perform the setting of socket blocking mode
552 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000553static int
554internal_setblocking(PySocketSockObject *s, int block)
555{
Victor Stinnerb6dab6b2014-07-23 22:56:55 +0200556#ifdef MS_WINDOWS
557 u_long arg;
558#endif
Victor Stinner9a954832013-12-04 00:41:24 +0100559#if !defined(MS_WINDOWS) \
Christian Heimesaf01f662013-12-21 16:19:10 +0100560 && !((defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)))
Victor Stinner9a954832013-12-04 00:41:24 +0100561 int delay_flag, new_delay_flag;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000562#endif
Antoine Pitroub1c54962010-10-14 15:05:38 +0000563#ifdef SOCK_NONBLOCK
564 if (block)
565 s->sock_type &= (~SOCK_NONBLOCK);
566 else
567 s->sock_type |= SOCK_NONBLOCK;
568#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +0000569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 Py_BEGIN_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000571#ifndef MS_WINDOWS
Christian Heimesaf01f662013-12-21 16:19:10 +0100572#if (defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 block = !block;
574 ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);
Victor Stinner9a954832013-12-04 00:41:24 +0100575#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
577 if (block)
Victor Stinner9a954832013-12-04 00:41:24 +0100578 new_delay_flag = delay_flag & (~O_NONBLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 else
Victor Stinner9a954832013-12-04 00:41:24 +0100580 new_delay_flag = delay_flag | O_NONBLOCK;
581 if (new_delay_flag != delay_flag)
582 fcntl(s->sock_fd, F_SETFL, new_delay_flag);
583#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +0000584#else /* MS_WINDOWS */
Victor Stinnerb6dab6b2014-07-23 22:56:55 +0200585 arg = !block;
586 ioctlsocket(s->sock_fd, FIONBIO, &arg);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000587#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 /* Since these don't return anything */
591 return 1;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000592}
593
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000594static int
Victor Stinnere6951c62015-03-31 13:50:44 +0200595internal_select_impl(PySocketSockObject *s, int writing, _PyTime_t interval)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 int n;
Victor Stinner71694d52015-03-28 01:18:54 +0100598#ifdef HAVE_POLL
599 struct pollfd pollfd;
Victor Stinnere6951c62015-03-31 13:50:44 +0200600 _PyTime_t ms;
Victor Stinner71694d52015-03-28 01:18:54 +0100601#else
602 fd_set fds;
603 struct timeval tv;
604#endif
Guido van Rossum11ba0942002-06-13 15:07:44 +0000605
Victor Stinnerb7df3142015-03-27 22:59:32 +0100606#ifdef WITH_THREAD
607 /* must be called with the GIL held */
608 assert(PyGILState_Check());
609#endif
610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 /* Nothing to do unless we're in timeout mode (not non-blocking) */
Victor Stinner71694d52015-03-28 01:18:54 +0100612 if (s->sock_timeout <= 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 /* Guard against closed socket */
616 if (s->sock_fd < 0)
617 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000618
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000619 /* Handling this condition here simplifies the select loops */
Victor Stinner71694d52015-03-28 01:18:54 +0100620 if (interval < 0)
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000621 return 1;
622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 /* Prefer poll, if available, since you can poll() any fd
624 * which can't be done with select(). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000625#ifdef HAVE_POLL
Victor Stinner71694d52015-03-28 01:18:54 +0100626 pollfd.fd = s->sock_fd;
627 pollfd.events = writing ? POLLOUT : POLLIN;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000628
Victor Stinner71694d52015-03-28 01:18:54 +0100629 /* s->sock_timeout is in seconds, timeout in ms */
Victor Stinnere6951c62015-03-31 13:50:44 +0200630 ms = _PyTime_AsMilliseconds(interval, _PyTime_ROUND_CEILING);
631 assert(ms <= INT_MAX);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000632
Victor Stinner71694d52015-03-28 01:18:54 +0100633 Py_BEGIN_ALLOW_THREADS;
Victor Stinnere6951c62015-03-31 13:50:44 +0200634 n = poll(&pollfd, 1, (int)ms);
Victor Stinner71694d52015-03-28 01:18:54 +0100635 Py_END_ALLOW_THREADS;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000636#else
Victor Stinner869e1772015-03-30 03:49:14 +0200637 _PyTime_AsTimeval_noraise(interval, &tv, _PyTime_ROUND_CEILING);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000638
Victor Stinner71694d52015-03-28 01:18:54 +0100639 FD_ZERO(&fds);
640 FD_SET(s->sock_fd, &fds);
641
642 /* See if the socket is ready */
643 Py_BEGIN_ALLOW_THREADS;
644 if (writing)
645 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
646 NULL, &fds, NULL, &tv);
647 else
648 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
649 &fds, NULL, NULL, &tv);
650 Py_END_ALLOW_THREADS;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000651#endif
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 if (n < 0)
654 return -1;
655 if (n == 0)
656 return 1;
657 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000658}
659
Victor Stinnere6951c62015-03-31 13:50:44 +0200660/* Do a select()/poll() on the socket, if necessary (sock_timeout > 0).
661 The argument writing indicates the direction.
662 This does not raise an exception; we'll let our caller do that
663 after they've reacquired the interpreter lock.
664 Returns 1 on timeout, -1 on error, 0 otherwise. */
665static int
666internal_select(PySocketSockObject *s, int writing, _PyTime_t interval)
667{
668 return internal_select_impl(s, writing, interval);
669}
670
671static int
672internal_connect_select(PySocketSockObject *s)
673{
674 return internal_select(s, 1, s->sock_timeout);
675}
676
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000677/*
678 Two macros for automatic retry of select() in case of false positives
679 (for example, select() could indicate a socket is ready for reading
680 but the data then discarded by the OS because of a wrong checksum).
681 Here is an example of use:
682
683 BEGIN_SELECT_LOOP(s)
Victor Stinnerb7df3142015-03-27 22:59:32 +0100684
Victor Stinner391fa712015-03-31 13:15:31 +0200685 timeout = internal_select(s, 0, interval);
Victor Stinnerb7df3142015-03-27 22:59:32 +0100686
687 if (!timeout) {
688 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000689 outlen = recv(s->sock_fd, cbuf, len, flags);
Victor Stinnerb7df3142015-03-27 22:59:32 +0100690 Py_END_ALLOW_THREADS
691 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000692 if (timeout == 1) {
693 PyErr_SetString(socket_timeout, "timed out");
694 return -1;
695 }
696 END_SELECT_LOOP(s)
697*/
698
699#define BEGIN_SELECT_LOOP(s) \
700 { \
Victor Stinner71694d52015-03-28 01:18:54 +0100701 _PyTime_t deadline = 0; \
702 _PyTime_t interval = s->sock_timeout; \
703 int has_timeout = (s->sock_timeout > 0); \
704 if (has_timeout) \
705 deadline = _PyTime_GetMonotonicClock() + interval; \
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000706 while (1) { \
707 errno = 0; \
708
709#define END_SELECT_LOOP(s) \
710 if (!has_timeout || \
711 (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \
712 break; \
Victor Stinner71694d52015-03-28 01:18:54 +0100713 interval = deadline - _PyTime_GetMonotonicClock(); \
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000714 } \
715 } \
716
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000717/* Initialize a new socket object. */
718
Victor Stinner71694d52015-03-28 01:18:54 +0100719static _PyTime_t defaulttimeout = -1; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000720
Martin v. Löwis1a214512008-06-11 05:26:20 +0000721static void
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000722init_sockobject(PySocketSockObject *s,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 SOCKET_T fd, int family, int type, int proto)
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 s->sock_fd = fd;
726 s->sock_family = family;
727 s->sock_type = type;
728 s->sock_proto = proto;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 s->errorhandler = &set_error;
Antoine Pitroub1c54962010-10-14 15:05:38 +0000731#ifdef SOCK_NONBLOCK
732 if (type & SOCK_NONBLOCK)
Victor Stinner71694d52015-03-28 01:18:54 +0100733 s->sock_timeout = 0;
Antoine Pitroub1c54962010-10-14 15:05:38 +0000734 else
735#endif
736 {
737 s->sock_timeout = defaulttimeout;
Victor Stinner71694d52015-03-28 01:18:54 +0100738 if (defaulttimeout >= 0)
Antoine Pitroub1c54962010-10-14 15:05:38 +0000739 internal_setblocking(s, 0);
740 }
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000741
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000742}
743
744
Guido van Rossum30a685f1991-06-27 15:51:29 +0000745/* Create a new socket object.
746 This just creates the object and initializes it.
747 If the creation fails, return NULL and set an exception (implicit
748 in NEWOBJ()). */
749
Guido van Rossum73624e91994-10-10 17:59:00 +0000750static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000751new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 PySocketSockObject *s;
754 s = (PySocketSockObject *)
755 PyType_GenericNew(&sock_type, NULL, NULL);
756 if (s != NULL)
757 init_sockobject(s, fd, family, type, proto);
758 return s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000759}
760
Guido van Rossum30a685f1991-06-27 15:51:29 +0000761
Guido van Rossum48a680c2001-03-02 06:34:14 +0000762/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000763 thread to be in gethostbyname or getaddrinfo */
764#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Matthias Klosed182a6c2012-08-14 17:29:04 +0200765static PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000766#endif
767
768
Guido van Rossum30a685f1991-06-27 15:51:29 +0000769/* Convert a string specifying a host name or one of a few symbolic
770 names to a numeric IP address. This usually calls gethostbyname()
771 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000772 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000773 an error occurred; then an exception is raised. */
774
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000775static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000776setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000777{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 struct addrinfo hints, *res;
779 int error;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
782 if (name[0] == '\0') {
783 int siz;
784 memset(&hints, 0, sizeof(hints));
785 hints.ai_family = af;
786 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
787 hints.ai_flags = AI_PASSIVE;
788 Py_BEGIN_ALLOW_THREADS
789 ACQUIRE_GETADDRINFO_LOCK
790 error = getaddrinfo(NULL, "0", &hints, &res);
791 Py_END_ALLOW_THREADS
792 /* We assume that those thread-unsafe getaddrinfo() versions
793 *are* safe regarding their return value, ie. that a
794 subsequent call to getaddrinfo() does not destroy the
795 outcome of the first call. */
796 RELEASE_GETADDRINFO_LOCK
797 if (error) {
798 set_gaierror(error);
799 return -1;
800 }
801 switch (res->ai_family) {
802 case AF_INET:
803 siz = 4;
804 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000805#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 case AF_INET6:
807 siz = 16;
808 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000809#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 default:
811 freeaddrinfo(res);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200812 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 "unsupported address family");
814 return -1;
815 }
816 if (res->ai_next) {
817 freeaddrinfo(res);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200818 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 "wildcard resolved to multiple address");
820 return -1;
821 }
822 if (res->ai_addrlen < addr_ret_size)
823 addr_ret_size = res->ai_addrlen;
824 memcpy(addr_ret, res->ai_addr, addr_ret_size);
825 freeaddrinfo(res);
826 return siz;
827 }
Charles-François Natali0cc86852013-09-13 19:53:08 +0200828 /* special-case broadcast - inet_addr() below can return INADDR_NONE for
Victor Stinnere990c6e2013-11-16 00:18:58 +0100829 * this */
Charles-François Natali0cc86852013-09-13 19:53:08 +0200830 if (strcmp(name, "255.255.255.255") == 0 ||
831 strcmp(name, "<broadcast>") == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 struct sockaddr_in *sin;
833 if (af != AF_INET && af != AF_UNSPEC) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200834 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 "address family mismatched");
836 return -1;
837 }
838 sin = (struct sockaddr_in *)addr_ret;
839 memset((void *) sin, '\0', sizeof(*sin));
840 sin->sin_family = AF_INET;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000841#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 sin->sin_len = sizeof(*sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000843#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 sin->sin_addr.s_addr = INADDR_BROADCAST;
845 return sizeof(sin->sin_addr);
846 }
Charles-François Natali0cc86852013-09-13 19:53:08 +0200847
848 /* avoid a name resolution in case of numeric address */
849#ifdef HAVE_INET_PTON
850 /* check for an IPv4 address */
851 if (af == AF_UNSPEC || af == AF_INET) {
852 struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret;
853 memset(sin, 0, sizeof(*sin));
854 if (inet_pton(AF_INET, name, &sin->sin_addr) > 0) {
855 sin->sin_family = AF_INET;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000856#ifdef HAVE_SOCKADDR_SA_LEN
Charles-François Natali0cc86852013-09-13 19:53:08 +0200857 sin->sin_len = sizeof(*sin);
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000858#endif
Charles-François Natali0cc86852013-09-13 19:53:08 +0200859 return 4;
860 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 }
Charles-François Natali0cc86852013-09-13 19:53:08 +0200862#ifdef ENABLE_IPV6
863 /* check for an IPv6 address - if the address contains a scope ID, we
864 * fallback to getaddrinfo(), which can handle translation from interface
865 * name to interface index */
866 if ((af == AF_UNSPEC || af == AF_INET6) && !strchr(name, '%')) {
867 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)addr_ret;
868 memset(sin, 0, sizeof(*sin));
869 if (inet_pton(AF_INET6, name, &sin->sin6_addr) > 0) {
870 sin->sin6_family = AF_INET6;
871#ifdef HAVE_SOCKADDR_SA_LEN
872 sin->sin6_len = sizeof(*sin);
873#endif
874 return 16;
875 }
876 }
877#endif /* ENABLE_IPV6 */
878#else /* HAVE_INET_PTON */
879 /* check for an IPv4 address */
880 if (af == AF_INET || af == AF_UNSPEC) {
881 struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret;
882 memset(sin, 0, sizeof(*sin));
883 if ((sin->sin_addr.s_addr = inet_addr(name)) != INADDR_NONE) {
884 sin->sin_family = AF_INET;
885#ifdef HAVE_SOCKADDR_SA_LEN
886 sin->sin_len = sizeof(*sin);
887#endif
888 return 4;
889 }
Victor Stinnere990c6e2013-11-16 00:18:58 +0100890 }
Charles-François Natali0cc86852013-09-13 19:53:08 +0200891#endif /* HAVE_INET_PTON */
892
893 /* perform a name resolution */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 memset(&hints, 0, sizeof(hints));
895 hints.ai_family = af;
896 Py_BEGIN_ALLOW_THREADS
897 ACQUIRE_GETADDRINFO_LOCK
898 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000899#if defined(__digital__) && defined(__unix__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 if (error == EAI_NONAME && af == AF_UNSPEC) {
901 /* On Tru64 V5.1, numeric-to-addr conversion fails
902 if no address family is given. Assume IPv4 for now.*/
903 hints.ai_family = AF_INET;
904 error = getaddrinfo(name, NULL, &hints, &res);
905 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000906#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 Py_END_ALLOW_THREADS
908 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
909 if (error) {
910 set_gaierror(error);
911 return -1;
912 }
913 if (res->ai_addrlen < addr_ret_size)
914 addr_ret_size = res->ai_addrlen;
915 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
916 freeaddrinfo(res);
917 switch (addr_ret->sa_family) {
918 case AF_INET:
919 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000920#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 case AF_INET6:
922 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000923#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200925 PyErr_SetString(PyExc_OSError, "unknown address family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 return -1;
927 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000928}
929
Guido van Rossum30a685f1991-06-27 15:51:29 +0000930
Guido van Rossum30a685f1991-06-27 15:51:29 +0000931/* Create a string object representing an IP address.
932 This is always a string of the form 'dd.dd.dd.dd' (with variable
933 size numbers). */
934
Guido van Rossum73624e91994-10-10 17:59:00 +0000935static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000936makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000937{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 char buf[NI_MAXHOST];
939 int error;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
942 NI_NUMERICHOST);
943 if (error) {
944 set_gaierror(error);
945 return NULL;
946 }
947 return PyUnicode_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000948}
949
950
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000951#ifdef USE_BLUETOOTH
952/* Convert a string representation of a Bluetooth address into a numeric
953 address. Returns the length (6), or raises an exception and returns -1 if
954 an error occurred. */
955
956static int
957setbdaddr(char *name, bdaddr_t *bdaddr)
958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 unsigned int b0, b1, b2, b3, b4, b5;
960 char ch;
961 int n;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
964 &b5, &b4, &b3, &b2, &b1, &b0, &ch);
965 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
966 bdaddr->b[0] = b0;
967 bdaddr->b[1] = b1;
968 bdaddr->b[2] = b2;
969 bdaddr->b[3] = b3;
970 bdaddr->b[4] = b4;
971 bdaddr->b[5] = b5;
972 return 6;
973 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200974 PyErr_SetString(PyExc_OSError, "bad bluetooth address");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 return -1;
976 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000977}
978
979/* Create a string representation of the Bluetooth address. This is always a
980 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
981 value (zero padded if necessary). */
982
983static PyObject *
984makebdaddr(bdaddr_t *bdaddr)
985{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 char buf[(6 * 2) + 5 + 1];
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
989 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
990 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
991 return PyUnicode_FromString(buf);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000992}
993#endif
994
995
Guido van Rossum30a685f1991-06-27 15:51:29 +0000996/* Create an object representing the given socket address,
997 suitable for passing it back to bind(), connect() etc.
998 The family field of the sockaddr structure is inspected
999 to determine what kind of address it really is. */
1000
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001001/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001002static PyObject *
Antoine Pitrou19467d22010-08-17 19:33:30 +00001003makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 if (addrlen == 0) {
1006 /* No address -- may be recvfrom() from known socket */
1007 Py_INCREF(Py_None);
1008 return Py_None;
1009 }
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 switch (addr->sa_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +00001012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 case AF_INET:
1014 {
1015 struct sockaddr_in *a;
1016 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
1017 PyObject *ret = NULL;
1018 if (addrobj) {
1019 a = (struct sockaddr_in *)addr;
1020 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
1021 Py_DECREF(addrobj);
1022 }
1023 return ret;
1024 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00001025
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001026#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 case AF_UNIX:
1028 {
1029 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001030#ifdef linux
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 if (a->sun_path[0] == 0) { /* Linux abstract namespace */
1032 addrlen -= offsetof(struct sockaddr_un, sun_path);
Charles-François Natalib10c71d2013-05-21 10:45:46 +02001033 return PyBytes_FromStringAndSize(a->sun_path, addrlen);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 }
1035 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001036#endif /* linux */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 {
1038 /* regular NULL-terminated string */
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001039 return PyUnicode_DecodeFSDefault(a->sun_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 }
1041 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001042#endif /* AF_UNIX */
1043
Martin v. Löwis11017b12006-01-14 18:12:57 +00001044#if defined(AF_NETLINK)
1045 case AF_NETLINK:
1046 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
1048 return Py_BuildValue("II", a->nl_pid, a->nl_groups);
Martin v. Löwis11017b12006-01-14 18:12:57 +00001049 }
1050#endif /* AF_NETLINK */
1051
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001052#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 case AF_INET6:
1054 {
1055 struct sockaddr_in6 *a;
1056 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
1057 PyObject *ret = NULL;
1058 if (addrobj) {
1059 a = (struct sockaddr_in6 *)addr;
Charles-François Natali366999a2012-01-02 15:47:29 +01001060 ret = Py_BuildValue("OiII",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 addrobj,
1062 ntohs(a->sin6_port),
Charles-François Natali366999a2012-01-02 15:47:29 +01001063 ntohl(a->sin6_flowinfo),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 a->sin6_scope_id);
1065 Py_DECREF(addrobj);
1066 }
1067 return ret;
1068 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001069#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001070
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001071#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 case AF_BLUETOOTH:
1073 switch (proto) {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 case BTPROTO_L2CAP:
1076 {
1077 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
1078 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
1079 PyObject *ret = NULL;
1080 if (addrobj) {
1081 ret = Py_BuildValue("Oi",
1082 addrobj,
1083 _BT_L2_MEMB(a, psm));
1084 Py_DECREF(addrobj);
1085 }
1086 return ret;
1087 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 case BTPROTO_RFCOMM:
1090 {
1091 struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
1092 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
1093 PyObject *ret = NULL;
1094 if (addrobj) {
1095 ret = Py_BuildValue("Oi",
1096 addrobj,
1097 _BT_RC_MEMB(a, channel));
1098 Py_DECREF(addrobj);
1099 }
1100 return ret;
1101 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 case BTPROTO_HCI:
1104 {
1105 struct sockaddr_hci *a = (struct sockaddr_hci *) addr;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001106#if defined(__NetBSD__) || defined(__DragonFly__)
1107 return makebdaddr(&_BT_HCI_MEMB(a, bdaddr));
1108#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 PyObject *ret = NULL;
1110 ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
1111 return ret;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001112#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 }
Thomas Wouterscf297e42007-02-23 15:07:44 +00001114
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001115#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 case BTPROTO_SCO:
1117 {
1118 struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
1119 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
1120 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001121#endif
1122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 default:
1124 PyErr_SetString(PyExc_ValueError,
1125 "Unknown Bluetooth protocol");
1126 return NULL;
1127 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001128#endif
1129
Antoine Pitroub156a462010-10-27 20:13:57 +00001130#if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 case AF_PACKET:
1132 {
1133 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
1134 char *ifname = "";
1135 struct ifreq ifr;
1136 /* need to look up interface name give index */
1137 if (a->sll_ifindex) {
1138 ifr.ifr_ifindex = a->sll_ifindex;
1139 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
1140 ifname = ifr.ifr_name;
1141 }
1142 return Py_BuildValue("shbhy#",
1143 ifname,
1144 ntohs(a->sll_protocol),
1145 a->sll_pkttype,
1146 a->sll_hatype,
1147 a->sll_addr,
1148 a->sll_halen);
1149 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001150#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001151
Christian Heimes043d6f62008-01-07 17:19:16 +00001152#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 case AF_TIPC:
1154 {
1155 struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr;
1156 if (a->addrtype == TIPC_ADDR_NAMESEQ) {
1157 return Py_BuildValue("IIIII",
1158 a->addrtype,
1159 a->addr.nameseq.type,
1160 a->addr.nameseq.lower,
1161 a->addr.nameseq.upper,
1162 a->scope);
1163 } else if (a->addrtype == TIPC_ADDR_NAME) {
1164 return Py_BuildValue("IIIII",
1165 a->addrtype,
1166 a->addr.name.name.type,
1167 a->addr.name.name.instance,
1168 a->addr.name.name.instance,
1169 a->scope);
1170 } else if (a->addrtype == TIPC_ADDR_ID) {
1171 return Py_BuildValue("IIIII",
1172 a->addrtype,
1173 a->addr.id.node,
1174 a->addr.id.ref,
1175 0,
1176 a->scope);
1177 } else {
1178 PyErr_SetString(PyExc_ValueError,
1179 "Invalid address type");
1180 return NULL;
1181 }
1182 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001183#endif
1184
Vinay Sajipecfc98c2014-03-20 12:42:42 +00001185#ifdef AF_CAN
Charles-François Natali47413c12011-10-06 19:47:44 +02001186 case AF_CAN:
1187 {
1188 struct sockaddr_can *a = (struct sockaddr_can *)addr;
1189 char *ifname = "";
1190 struct ifreq ifr;
1191 /* need to look up interface name given index */
1192 if (a->can_ifindex) {
1193 ifr.ifr_ifindex = a->can_ifindex;
1194 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
1195 ifname = ifr.ifr_name;
1196 }
1197
1198 return Py_BuildValue("O&h", PyUnicode_DecodeFSDefault,
1199 ifname,
1200 a->can_family);
1201 }
1202#endif
1203
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001204#ifdef PF_SYSTEM
1205 case PF_SYSTEM:
1206 switch(proto) {
1207#ifdef SYSPROTO_CONTROL
1208 case SYSPROTO_CONTROL:
1209 {
1210 struct sockaddr_ctl *a = (struct sockaddr_ctl *)addr;
1211 return Py_BuildValue("(II)", a->sc_id, a->sc_unit);
1212 }
1213#endif
1214 default:
1215 PyErr_SetString(PyExc_ValueError,
1216 "Invalid address type");
1217 return 0;
1218 }
1219#endif
1220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 default:
1224 /* If we don't know the address family, don't raise an
1225 exception -- return it as an (int, bytes) tuple. */
1226 return Py_BuildValue("iy#",
1227 addr->sa_family,
1228 addr->sa_data,
1229 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001232}
1233
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001234/* Helper for getsockaddrarg: bypass IDNA for ASCII-only host names
1235 (in particular, numeric IP addresses). */
1236struct maybe_idna {
1237 PyObject *obj;
1238 char *buf;
1239};
1240
1241static void
1242idna_cleanup(struct maybe_idna *data)
1243{
1244 Py_CLEAR(data->obj);
1245}
1246
1247static int
1248idna_converter(PyObject *obj, struct maybe_idna *data)
1249{
1250 size_t len;
1251 PyObject *obj2, *obj3;
1252 if (obj == NULL) {
1253 idna_cleanup(data);
1254 return 1;
1255 }
1256 data->obj = NULL;
1257 len = -1;
1258 if (PyBytes_Check(obj)) {
1259 data->buf = PyBytes_AsString(obj);
1260 len = PyBytes_Size(obj);
1261 }
1262 else if (PyByteArray_Check(obj)) {
1263 data->buf = PyByteArray_AsString(obj);
1264 len = PyByteArray_Size(obj);
1265 }
1266 else if (PyUnicode_Check(obj) && PyUnicode_READY(obj) == 0 && PyUnicode_IS_COMPACT_ASCII(obj)) {
1267 data->buf = PyUnicode_DATA(obj);
1268 len = PyUnicode_GET_LENGTH(obj);
1269 }
1270 else {
1271 obj2 = PyUnicode_FromObject(obj);
1272 if (!obj2) {
1273 PyErr_Format(PyExc_TypeError, "string or unicode text buffer expected, not %s",
1274 obj->ob_type->tp_name);
1275 return 0;
1276 }
1277 obj3 = PyUnicode_AsEncodedString(obj2, "idna", NULL);
1278 Py_DECREF(obj2);
1279 if (!obj3) {
1280 PyErr_SetString(PyExc_TypeError, "encoding of hostname failed");
1281 return 0;
1282 }
1283 if (!PyBytes_Check(obj3)) {
Martin v. Löwis17fd1e12014-08-05 16:13:50 +02001284 Py_DECREF(obj3);
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001285 PyErr_SetString(PyExc_TypeError, "encoding of hostname failed to return bytes");
1286 return 0;
1287 }
1288 data->obj = obj3;
1289 data->buf = PyBytes_AS_STRING(obj3);
1290 len = PyBytes_GET_SIZE(obj3);
1291 }
1292 if (strlen(data->buf) != len) {
1293 Py_CLEAR(data->obj);
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001294 PyErr_SetString(PyExc_TypeError, "host name must not contain null character");
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001295 return 0;
1296 }
1297 return Py_CLEANUP_SUPPORTED;
Victor Stinner1a62a682014-08-17 19:33:28 +02001298}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001299
1300/* Parse a socket address argument according to the socket object's
1301 address family. Return 1 if the address was in the proper format,
1302 0 of not. The address is returned through addr_ret, its length
1303 through len_ret. */
1304
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001305static int
Guido van Rossum48a680c2001-03-02 06:34:14 +00001306getsockaddrarg(PySocketSockObject *s, PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 struct sockaddr *addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 switch (s->sock_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +00001310
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001311#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 case AF_UNIX:
1313 {
1314 struct sockaddr_un* addr;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001315 Py_buffer path;
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001316 int retval = 0;
1317
1318 /* PEP 383. Not using PyUnicode_FSConverter since we need to
1319 allow embedded nulls on Linux. */
1320 if (PyUnicode_Check(args)) {
1321 if ((args = PyUnicode_EncodeFSDefault(args)) == NULL)
1322 return 0;
1323 }
1324 else
1325 Py_INCREF(args);
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001326 if (!PyArg_Parse(args, "y*", &path)) {
1327 Py_DECREF(args);
1328 return retval;
1329 }
1330 assert(path.len >= 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 addr = (struct sockaddr_un*)addr_ret;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001333#ifdef linux
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001334 if (path.len > 0 && *(const char *)path.buf == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 /* Linux abstract namespace extension */
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001336 if ((size_t)path.len > sizeof addr->sun_path) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001337 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 "AF_UNIX path too long");
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001339 goto unix_out;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 }
1341 }
1342 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001343#endif /* linux */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 {
1345 /* regular NULL-terminated string */
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001346 if ((size_t)path.len >= sizeof addr->sun_path) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001347 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 "AF_UNIX path too long");
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001349 goto unix_out;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 }
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001351 addr->sun_path[path.len] = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 }
1353 addr->sun_family = s->sock_family;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001354 memcpy(addr->sun_path, path.buf, path.len);
1355 *len_ret = path.len + offsetof(struct sockaddr_un, sun_path);
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001356 retval = 1;
1357 unix_out:
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001358 PyBuffer_Release(&path);
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001359 Py_DECREF(args);
1360 return retval;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001362#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001363
Martin v. Löwis11017b12006-01-14 18:12:57 +00001364#if defined(AF_NETLINK)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 case AF_NETLINK:
1366 {
1367 struct sockaddr_nl* addr;
1368 int pid, groups;
1369 addr = (struct sockaddr_nl *)addr_ret;
1370 if (!PyTuple_Check(args)) {
1371 PyErr_Format(
1372 PyExc_TypeError,
1373 "getsockaddrarg: "
1374 "AF_NETLINK address must be tuple, not %.500s",
1375 Py_TYPE(args)->tp_name);
1376 return 0;
1377 }
1378 if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
1379 return 0;
1380 addr->nl_family = AF_NETLINK;
1381 addr->nl_pid = pid;
1382 addr->nl_groups = groups;
1383 *len_ret = sizeof(*addr);
1384 return 1;
1385 }
Martin v. Löwis11017b12006-01-14 18:12:57 +00001386#endif
1387
Charles-François Natali10b8cf42011-11-10 19:21:37 +01001388#ifdef AF_RDS
1389 case AF_RDS:
1390 /* RDS sockets use sockaddr_in: fall-through */
1391#endif
1392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 case AF_INET:
1394 {
1395 struct sockaddr_in* addr;
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001396 struct maybe_idna host = {NULL, NULL};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 int port, result;
1398 if (!PyTuple_Check(args)) {
1399 PyErr_Format(
1400 PyExc_TypeError,
1401 "getsockaddrarg: "
1402 "AF_INET address must be tuple, not %.500s",
1403 Py_TYPE(args)->tp_name);
1404 return 0;
1405 }
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001406 if (!PyArg_ParseTuple(args, "O&i:getsockaddrarg",
1407 idna_converter, &host, &port))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 return 0;
1409 addr=(struct sockaddr_in*)addr_ret;
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001410 result = setipaddr(host.buf, (struct sockaddr *)addr,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 sizeof(*addr), AF_INET);
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001412 idna_cleanup(&host);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 if (result < 0)
1414 return 0;
1415 if (port < 0 || port > 0xffff) {
1416 PyErr_SetString(
1417 PyExc_OverflowError,
1418 "getsockaddrarg: port must be 0-65535.");
1419 return 0;
1420 }
1421 addr->sin_family = AF_INET;
1422 addr->sin_port = htons((short)port);
1423 *len_ret = sizeof *addr;
1424 return 1;
1425 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001426
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001427#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 case AF_INET6:
1429 {
1430 struct sockaddr_in6* addr;
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001431 struct maybe_idna host = {NULL, NULL};
Charles-François Natali366999a2012-01-02 15:47:29 +01001432 int port, result;
1433 unsigned int flowinfo, scope_id;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 flowinfo = scope_id = 0;
1435 if (!PyTuple_Check(args)) {
1436 PyErr_Format(
1437 PyExc_TypeError,
1438 "getsockaddrarg: "
1439 "AF_INET6 address must be tuple, not %.500s",
1440 Py_TYPE(args)->tp_name);
1441 return 0;
1442 }
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001443 if (!PyArg_ParseTuple(args, "O&i|II",
1444 idna_converter, &host, &port, &flowinfo,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 &scope_id)) {
1446 return 0;
1447 }
1448 addr = (struct sockaddr_in6*)addr_ret;
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001449 result = setipaddr(host.buf, (struct sockaddr *)addr,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 sizeof(*addr), AF_INET6);
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001451 idna_cleanup(&host);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 if (result < 0)
1453 return 0;
1454 if (port < 0 || port > 0xffff) {
1455 PyErr_SetString(
1456 PyExc_OverflowError,
1457 "getsockaddrarg: port must be 0-65535.");
1458 return 0;
1459 }
Charles-François Natali9b0c0062012-06-23 10:17:05 +02001460 if (flowinfo > 0xfffff) {
Charles-François Natali366999a2012-01-02 15:47:29 +01001461 PyErr_SetString(
1462 PyExc_OverflowError,
1463 "getsockaddrarg: flowinfo must be 0-1048575.");
1464 return 0;
1465 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 addr->sin6_family = s->sock_family;
1467 addr->sin6_port = htons((short)port);
Charles-François Natali366999a2012-01-02 15:47:29 +01001468 addr->sin6_flowinfo = htonl(flowinfo);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 addr->sin6_scope_id = scope_id;
1470 *len_ret = sizeof *addr;
1471 return 1;
1472 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001473#endif
1474
Hye-Shik Chang81268602004-02-02 06:05:24 +00001475#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 case AF_BLUETOOTH:
1477 {
1478 switch (s->sock_proto) {
1479 case BTPROTO_L2CAP:
1480 {
1481 struct sockaddr_l2 *addr;
1482 char *straddr;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 addr = (struct sockaddr_l2 *)addr_ret;
1485 memset(addr, 0, sizeof(struct sockaddr_l2));
1486 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
1487 if (!PyArg_ParseTuple(args, "si", &straddr,
1488 &_BT_L2_MEMB(addr, psm))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001489 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 "wrong format");
1491 return 0;
1492 }
1493 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
1494 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 *len_ret = sizeof *addr;
1497 return 1;
1498 }
1499 case BTPROTO_RFCOMM:
1500 {
1501 struct sockaddr_rc *addr;
1502 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 addr = (struct sockaddr_rc *)addr_ret;
1505 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
1506 if (!PyArg_ParseTuple(args, "si", &straddr,
1507 &_BT_RC_MEMB(addr, channel))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001508 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 "wrong format");
1510 return 0;
1511 }
1512 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
1513 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 *len_ret = sizeof *addr;
1516 return 1;
1517 }
1518 case BTPROTO_HCI:
1519 {
1520 struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001521#if defined(__NetBSD__) || defined(__DragonFly__)
Alexander Belopolskye239d232010-12-08 23:31:48 +00001522 char *straddr = PyBytes_AS_STRING(args);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001523
Alexander Belopolskye239d232010-12-08 23:31:48 +00001524 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001525 if (straddr == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001526 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001527 "wrong format");
1528 return 0;
1529 }
1530 if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0)
1531 return 0;
1532#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
1534 if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001535 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 "wrong format");
1537 return 0;
1538 }
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001539#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 *len_ret = sizeof *addr;
1541 return 1;
1542 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001543#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 case BTPROTO_SCO:
1545 {
1546 struct sockaddr_sco *addr;
1547 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 addr = (struct sockaddr_sco *)addr_ret;
1550 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
1551 if (!PyBytes_Check(args)) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001552 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 "wrong format");
1554 return 0;
1555 }
1556 straddr = PyBytes_AS_STRING(args);
1557 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
1558 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 *len_ret = sizeof *addr;
1561 return 1;
1562 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001563#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001565 PyErr_SetString(PyExc_OSError, "getsockaddrarg: unknown Bluetooth protocol");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 return 0;
1567 }
1568 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001569#endif
1570
Antoine Pitroub156a462010-10-27 20:13:57 +00001571#if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 case AF_PACKET:
1573 {
1574 struct sockaddr_ll* addr;
1575 struct ifreq ifr;
1576 char *interfaceName;
1577 int protoNumber;
1578 int hatype = 0;
1579 int pkttype = 0;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001580 Py_buffer haddr = {NULL, NULL};
Guido van Rossum48a680c2001-03-02 06:34:14 +00001581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 if (!PyTuple_Check(args)) {
1583 PyErr_Format(
1584 PyExc_TypeError,
1585 "getsockaddrarg: "
1586 "AF_PACKET address must be tuple, not %.500s",
1587 Py_TYPE(args)->tp_name);
1588 return 0;
1589 }
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001590 if (!PyArg_ParseTuple(args, "si|iiy*", &interfaceName,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 &protoNumber, &pkttype, &hatype,
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001592 &haddr))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 return 0;
1594 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1595 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
1596 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
1597 s->errorhandler();
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001598 PyBuffer_Release(&haddr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 return 0;
1600 }
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001601 if (haddr.buf && haddr.len > 8) {
1602 PyErr_SetString(PyExc_ValueError,
1603 "Hardware address must be 8 bytes or less");
1604 PyBuffer_Release(&haddr);
1605 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 }
1607 if (protoNumber < 0 || protoNumber > 0xffff) {
1608 PyErr_SetString(
1609 PyExc_OverflowError,
1610 "getsockaddrarg: protoNumber must be 0-65535.");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001611 PyBuffer_Release(&haddr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 return 0;
1613 }
1614 addr = (struct sockaddr_ll*)addr_ret;
1615 addr->sll_family = AF_PACKET;
1616 addr->sll_protocol = htons((short)protoNumber);
1617 addr->sll_ifindex = ifr.ifr_ifindex;
1618 addr->sll_pkttype = pkttype;
1619 addr->sll_hatype = hatype;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001620 if (haddr.buf) {
1621 memcpy(&addr->sll_addr, haddr.buf, haddr.len);
1622 addr->sll_halen = haddr.len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 }
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001624 else
1625 addr->sll_halen = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 *len_ret = sizeof *addr;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001627 PyBuffer_Release(&haddr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 return 1;
1629 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001630#endif
1631
Christian Heimes043d6f62008-01-07 17:19:16 +00001632#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 case AF_TIPC:
1634 {
1635 unsigned int atype, v1, v2, v3;
1636 unsigned int scope = TIPC_CLUSTER_SCOPE;
1637 struct sockaddr_tipc *addr;
Christian Heimes043d6f62008-01-07 17:19:16 +00001638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 if (!PyTuple_Check(args)) {
1640 PyErr_Format(
1641 PyExc_TypeError,
1642 "getsockaddrarg: "
1643 "AF_TIPC address must be tuple, not %.500s",
1644 Py_TYPE(args)->tp_name);
1645 return 0;
1646 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 if (!PyArg_ParseTuple(args,
1649 "IIII|I;Invalid TIPC address format",
1650 &atype, &v1, &v2, &v3, &scope))
1651 return 0;
Christian Heimes043d6f62008-01-07 17:19:16 +00001652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 addr = (struct sockaddr_tipc *) addr_ret;
1654 memset(addr, 0, sizeof(struct sockaddr_tipc));
Christian Heimes043d6f62008-01-07 17:19:16 +00001655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 addr->family = AF_TIPC;
1657 addr->scope = scope;
1658 addr->addrtype = atype;
Christian Heimes043d6f62008-01-07 17:19:16 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 if (atype == TIPC_ADDR_NAMESEQ) {
1661 addr->addr.nameseq.type = v1;
1662 addr->addr.nameseq.lower = v2;
1663 addr->addr.nameseq.upper = v3;
1664 } else if (atype == TIPC_ADDR_NAME) {
1665 addr->addr.name.name.type = v1;
1666 addr->addr.name.name.instance = v2;
1667 } else if (atype == TIPC_ADDR_ID) {
1668 addr->addr.id.node = v1;
1669 addr->addr.id.ref = v2;
1670 } else {
1671 /* Shouldn't happen */
1672 PyErr_SetString(PyExc_TypeError, "Invalid address type");
1673 return 0;
1674 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 *len_ret = sizeof(*addr);
Christian Heimes043d6f62008-01-07 17:19:16 +00001677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 return 1;
1679 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001680#endif
1681
Vinay Sajiped6783f2014-03-21 11:44:32 +00001682#if defined(AF_CAN) && defined(CAN_RAW) && defined(CAN_BCM)
Charles-François Natali47413c12011-10-06 19:47:44 +02001683 case AF_CAN:
1684 switch (s->sock_proto) {
1685 case CAN_RAW:
Charles-François Natali773e42d2013-02-05 19:42:01 +01001686 /* fall-through */
1687 case CAN_BCM:
Charles-François Natali47413c12011-10-06 19:47:44 +02001688 {
1689 struct sockaddr_can *addr;
1690 PyObject *interfaceName;
1691 struct ifreq ifr;
Charles-François Natali47413c12011-10-06 19:47:44 +02001692 Py_ssize_t len;
1693
Benjamin Peterson18b71912013-05-16 15:29:44 -05001694 addr = (struct sockaddr_can *)addr_ret;
1695
Charles-François Natali47413c12011-10-06 19:47:44 +02001696 if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter,
1697 &interfaceName))
1698 return 0;
1699
1700 len = PyBytes_GET_SIZE(interfaceName);
1701
1702 if (len == 0) {
1703 ifr.ifr_ifindex = 0;
Victor Stinner1a62a682014-08-17 19:33:28 +02001704 } else if ((size_t)len < sizeof(ifr.ifr_name)) {
Christian Heimes15b68852012-09-10 01:25:50 +02001705 strncpy(ifr.ifr_name, PyBytes_AS_STRING(interfaceName), sizeof(ifr.ifr_name));
1706 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Charles-François Natali47413c12011-10-06 19:47:44 +02001707 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
1708 s->errorhandler();
1709 Py_DECREF(interfaceName);
1710 return 0;
1711 }
1712 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001713 PyErr_SetString(PyExc_OSError,
Charles-François Natali47413c12011-10-06 19:47:44 +02001714 "AF_CAN interface name too long");
1715 Py_DECREF(interfaceName);
1716 return 0;
1717 }
1718
1719 addr->can_family = AF_CAN;
1720 addr->can_ifindex = ifr.ifr_ifindex;
1721
1722 *len_ret = sizeof(*addr);
1723 Py_DECREF(interfaceName);
1724 return 1;
1725 }
1726 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001727 PyErr_SetString(PyExc_OSError,
Charles-François Natali47413c12011-10-06 19:47:44 +02001728 "getsockaddrarg: unsupported CAN protocol");
1729 return 0;
1730 }
1731#endif
Victor Stinnera534fc42013-06-03 22:07:27 +02001732
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001733#ifdef PF_SYSTEM
1734 case PF_SYSTEM:
1735 switch (s->sock_proto) {
1736#ifdef SYSPROTO_CONTROL
1737 case SYSPROTO_CONTROL:
1738 {
1739 struct sockaddr_ctl *addr;
Victor Stinnera534fc42013-06-03 22:07:27 +02001740
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001741 addr = (struct sockaddr_ctl *)addr_ret;
1742 addr->sc_family = AF_SYSTEM;
Victor Stinnera534fc42013-06-03 22:07:27 +02001743 addr->ss_sysaddr = AF_SYS_CONTROL;
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001744
1745 if (PyUnicode_Check(args)) {
1746 struct ctl_info info;
1747 PyObject *ctl_name;
1748
1749 if (!PyArg_Parse(args, "O&",
1750 PyUnicode_FSConverter, &ctl_name)) {
1751 return 0;
1752 }
1753
Victor Stinnerf50e1872015-03-20 11:32:24 +01001754 if (PyBytes_GET_SIZE(ctl_name) > (Py_ssize_t)sizeof(info.ctl_name)) {
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001755 PyErr_SetString(PyExc_ValueError,
1756 "provided string is too long");
1757 Py_DECREF(ctl_name);
1758 return 0;
1759 }
1760 strncpy(info.ctl_name, PyBytes_AS_STRING(ctl_name),
1761 sizeof(info.ctl_name));
1762 Py_DECREF(ctl_name);
1763
1764 if (ioctl(s->sock_fd, CTLIOCGINFO, &info)) {
1765 PyErr_SetString(PyExc_OSError,
1766 "cannot find kernel control with provided name");
1767 return 0;
1768 }
Victor Stinnera534fc42013-06-03 22:07:27 +02001769
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001770 addr->sc_id = info.ctl_id;
1771 addr->sc_unit = 0;
1772 } else if (!PyArg_ParseTuple(args, "II",
1773 &(addr->sc_id), &(addr->sc_unit))) {
1774 PyErr_SetString(PyExc_TypeError, "getsockaddrarg: "
1775 "expected str or tuple of two ints");
Victor Stinnera534fc42013-06-03 22:07:27 +02001776
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001777 return 0;
1778 }
Victor Stinnera534fc42013-06-03 22:07:27 +02001779
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001780 *len_ret = sizeof(*addr);
1781 return 1;
1782 }
1783#endif
1784 default:
1785 PyErr_SetString(PyExc_OSError,
1786 "getsockaddrarg: unsupported PF_SYSTEM protocol");
1787 return 0;
1788 }
1789#endif
Charles-François Natali47413c12011-10-06 19:47:44 +02001790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001794 PyErr_SetString(PyExc_OSError, "getsockaddrarg: bad family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 return 0;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001798}
1799
Guido van Rossum30a685f1991-06-27 15:51:29 +00001800
Guido van Rossum48a680c2001-03-02 06:34:14 +00001801/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00001802 Return 1 if the family is known, 0 otherwise. The length is returned
1803 through len_ret. */
1804
1805static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00001806getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 switch (s->sock_family) {
Guido van Rossum710e1df1992-06-12 10:39:36 +00001809
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001810#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 case AF_UNIX:
1812 {
1813 *len_ret = sizeof (struct sockaddr_un);
1814 return 1;
1815 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001816#endif /* AF_UNIX */
Martin v. Löwis11017b12006-01-14 18:12:57 +00001817#if defined(AF_NETLINK)
1818 case AF_NETLINK:
1819 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 *len_ret = sizeof (struct sockaddr_nl);
1821 return 1;
Martin v. Löwis11017b12006-01-14 18:12:57 +00001822 }
1823#endif
Guido van Rossum710e1df1992-06-12 10:39:36 +00001824
Charles-François Natali10b8cf42011-11-10 19:21:37 +01001825#ifdef AF_RDS
1826 case AF_RDS:
1827 /* RDS sockets use sockaddr_in: fall-through */
1828#endif
1829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 case AF_INET:
1831 {
1832 *len_ret = sizeof (struct sockaddr_in);
1833 return 1;
1834 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00001835
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001836#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 case AF_INET6:
1838 {
1839 *len_ret = sizeof (struct sockaddr_in6);
1840 return 1;
1841 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001842#endif
1843
Hye-Shik Chang81268602004-02-02 06:05:24 +00001844#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 case AF_BLUETOOTH:
1846 {
1847 switch(s->sock_proto)
1848 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 case BTPROTO_L2CAP:
1851 *len_ret = sizeof (struct sockaddr_l2);
1852 return 1;
1853 case BTPROTO_RFCOMM:
1854 *len_ret = sizeof (struct sockaddr_rc);
1855 return 1;
1856 case BTPROTO_HCI:
1857 *len_ret = sizeof (struct sockaddr_hci);
1858 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001859#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 case BTPROTO_SCO:
1861 *len_ret = sizeof (struct sockaddr_sco);
1862 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001863#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001865 PyErr_SetString(PyExc_OSError, "getsockaddrlen: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 "unknown BT protocol");
1867 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 }
1870 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001871#endif
1872
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001873#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 case AF_PACKET:
1875 {
1876 *len_ret = sizeof (struct sockaddr_ll);
1877 return 1;
1878 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001879#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001880
Christian Heimes043d6f62008-01-07 17:19:16 +00001881#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 case AF_TIPC:
1883 {
1884 *len_ret = sizeof (struct sockaddr_tipc);
1885 return 1;
1886 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001887#endif
1888
Vinay Sajipecfc98c2014-03-20 12:42:42 +00001889#ifdef AF_CAN
Charles-François Natali47413c12011-10-06 19:47:44 +02001890 case AF_CAN:
1891 {
1892 *len_ret = sizeof (struct sockaddr_can);
1893 return 1;
1894 }
1895#endif
Victor Stinnera534fc42013-06-03 22:07:27 +02001896
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001897#ifdef PF_SYSTEM
1898 case PF_SYSTEM:
1899 switch(s->sock_proto) {
1900#ifdef SYSPROTO_CONTROL
1901 case SYSPROTO_CONTROL:
1902 *len_ret = sizeof (struct sockaddr_ctl);
1903 return 1;
1904#endif
1905 default:
1906 PyErr_SetString(PyExc_OSError, "getsockaddrlen: "
1907 "unknown PF_SYSTEM protocol");
1908 return 0;
1909 }
1910#endif
Charles-François Natali47413c12011-10-06 19:47:44 +02001911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 /* More cases here... */
Guido van Rossum710e1df1992-06-12 10:39:36 +00001913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001915 PyErr_SetString(PyExc_OSError, "getsockaddrlen: bad family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 return 0;
Guido van Rossum710e1df1992-06-12 10:39:36 +00001917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00001919}
1920
1921
Nick Coghlan96fe56a2011-08-22 11:55:57 +10001922/* Support functions for the sendmsg() and recvmsg[_into]() methods.
1923 Currently, these methods are only compiled if the RFC 2292/3542
1924 CMSG_LEN() macro is available. Older systems seem to have used
1925 sizeof(struct cmsghdr) + (length) where CMSG_LEN() is used now, so
1926 it may be possible to define CMSG_LEN() that way if it's not
1927 provided. Some architectures might need extra padding after the
1928 cmsghdr, however, and CMSG_LEN() would have to take account of
1929 this. */
1930#ifdef CMSG_LEN
1931/* If length is in range, set *result to CMSG_LEN(length) and return
1932 true; otherwise, return false. */
1933static int
1934get_CMSG_LEN(size_t length, size_t *result)
1935{
1936 size_t tmp;
1937
1938 if (length > (SOCKLEN_T_LIMIT - CMSG_LEN(0)))
1939 return 0;
1940 tmp = CMSG_LEN(length);
1941 if (tmp > SOCKLEN_T_LIMIT || tmp < length)
1942 return 0;
1943 *result = tmp;
1944 return 1;
1945}
1946
1947#ifdef CMSG_SPACE
1948/* If length is in range, set *result to CMSG_SPACE(length) and return
1949 true; otherwise, return false. */
1950static int
1951get_CMSG_SPACE(size_t length, size_t *result)
1952{
1953 size_t tmp;
1954
1955 /* Use CMSG_SPACE(1) here in order to take account of the padding
1956 necessary before *and* after the data. */
1957 if (length > (SOCKLEN_T_LIMIT - CMSG_SPACE(1)))
1958 return 0;
1959 tmp = CMSG_SPACE(length);
1960 if (tmp > SOCKLEN_T_LIMIT || tmp < length)
1961 return 0;
1962 *result = tmp;
1963 return 1;
1964}
1965#endif
1966
1967/* Return true iff msg->msg_controllen is valid, cmsgh is a valid
1968 pointer in msg->msg_control with at least "space" bytes after it,
1969 and its cmsg_len member inside the buffer. */
1970static int
1971cmsg_min_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t space)
1972{
1973 size_t cmsg_offset;
1974 static const size_t cmsg_len_end = (offsetof(struct cmsghdr, cmsg_len) +
1975 sizeof(cmsgh->cmsg_len));
1976
Charles-François Natali466517d2011-08-28 18:23:43 +02001977 /* Note that POSIX allows msg_controllen to be of signed type. */
Brett Cannonb05cbe62014-01-07 17:01:01 -05001978 if (cmsgh == NULL || msg->msg_control == NULL)
Nick Coghlan96fe56a2011-08-22 11:55:57 +10001979 return 0;
Brett Cannonb05cbe62014-01-07 17:01:01 -05001980 /* Note that POSIX allows msg_controllen to be of a signed type. This is
1981 annoying under OS X as it's unsigned there and so it triggers a
1982 tautological comparison warning under Clang when compared against 0.
1983 Since the check is valid on other platforms, silence the warning under
1984 Clang. */
1985 #ifdef __clang__
1986 #pragma clang diagnostic push
1987 #pragma clang diagnostic ignored "-Wtautological-compare"
1988 #endif
Serhiy Storchakacaa01f82015-03-05 23:42:24 +02001989 #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))
Serhiy Storchakab48af342015-02-26 15:27:57 +02001990 #pragma GCC diagnostic push
1991 #pragma GCC diagnostic ignored "-Wtype-limits"
1992 #endif
Brett Cannonb05cbe62014-01-07 17:01:01 -05001993 if (msg->msg_controllen < 0)
1994 return 0;
Serhiy Storchakacaa01f82015-03-05 23:42:24 +02001995 #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))
Serhiy Storchakab48af342015-02-26 15:27:57 +02001996 #pragma GCC diagnostic pop
1997 #endif
Brett Cannonb05cbe62014-01-07 17:01:01 -05001998 #ifdef __clang__
1999 #pragma clang diagnostic pop
2000 #endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10002001 if (space < cmsg_len_end)
2002 space = cmsg_len_end;
2003 cmsg_offset = (char *)cmsgh - (char *)msg->msg_control;
2004 return (cmsg_offset <= (size_t)-1 - space &&
2005 cmsg_offset + space <= msg->msg_controllen);
2006}
2007
2008/* If pointer CMSG_DATA(cmsgh) is in buffer msg->msg_control, set
2009 *space to number of bytes following it in the buffer and return
2010 true; otherwise, return false. Assumes cmsgh, msg->msg_control and
2011 msg->msg_controllen are valid. */
2012static int
2013get_cmsg_data_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *space)
2014{
2015 size_t data_offset;
2016 char *data_ptr;
2017
2018 if ((data_ptr = (char *)CMSG_DATA(cmsgh)) == NULL)
2019 return 0;
2020 data_offset = data_ptr - (char *)msg->msg_control;
2021 if (data_offset > msg->msg_controllen)
2022 return 0;
2023 *space = msg->msg_controllen - data_offset;
2024 return 1;
2025}
2026
2027/* If cmsgh is invalid or not contained in the buffer pointed to by
2028 msg->msg_control, return -1. If cmsgh is valid and its associated
2029 data is entirely contained in the buffer, set *data_len to the
2030 length of the associated data and return 0. If only part of the
2031 associated data is contained in the buffer but cmsgh is otherwise
2032 valid, set *data_len to the length contained in the buffer and
2033 return 1. */
2034static int
2035get_cmsg_data_len(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *data_len)
2036{
2037 size_t space, cmsg_data_len;
2038
2039 if (!cmsg_min_space(msg, cmsgh, CMSG_LEN(0)) ||
2040 cmsgh->cmsg_len < CMSG_LEN(0))
2041 return -1;
2042 cmsg_data_len = cmsgh->cmsg_len - CMSG_LEN(0);
2043 if (!get_cmsg_data_space(msg, cmsgh, &space))
2044 return -1;
2045 if (space >= cmsg_data_len) {
2046 *data_len = cmsg_data_len;
2047 return 0;
2048 }
2049 *data_len = space;
2050 return 1;
2051}
2052#endif /* CMSG_LEN */
2053
2054
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002055/* s._accept() -> (fd, address) */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002056
Guido van Rossum73624e91994-10-10 17:59:00 +00002057static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002058sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002059{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 sock_addr_t addrbuf;
2061 SOCKET_T newfd = INVALID_SOCKET;
2062 socklen_t addrlen;
2063 PyObject *sock = NULL;
2064 PyObject *addr = NULL;
2065 PyObject *res = NULL;
2066 int timeout;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002067 int async_err = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02002068#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
2069 /* accept4() is available on Linux 2.6.28+ and glibc 2.10 */
2070 static int accept4_works = -1;
2071#endif
2072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 if (!getsockaddrlen(s, &addrlen))
2074 return NULL;
2075 memset(&addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 if (!IS_SELECTABLE(s))
2078 return select_error();
Neal Norwitz082b2df2006-02-07 07:04:46 +00002079
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002080 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002081 do {
Victor Stinner391fa712015-03-31 13:15:31 +02002082 timeout = internal_select(s, 0, interval);
Victor Stinnerb7df3142015-03-27 22:59:32 +01002083
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002084 if (!timeout) {
Victor Stinnerb7df3142015-03-27 22:59:32 +01002085 Py_BEGIN_ALLOW_THREADS
Victor Stinnerdaf45552013-08-28 00:53:59 +02002086#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002087 if (accept4_works != 0) {
2088 newfd = accept4(s->sock_fd, SAS2SA(&addrbuf), &addrlen,
2089 SOCK_CLOEXEC);
2090 if (newfd == INVALID_SOCKET && accept4_works == -1) {
2091 /* On Linux older than 2.6.28, accept4() fails with ENOSYS */
2092 accept4_works = (errno != ENOSYS);
2093 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02002094 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002095 if (accept4_works == 0)
2096 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002097#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002098 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002099#endif
Victor Stinnerb7df3142015-03-27 22:59:32 +01002100 Py_END_ALLOW_THREADS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002101 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002102 } while (newfd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 if (timeout == 1) {
2104 PyErr_SetString(socket_timeout, "timed out");
2105 return NULL;
2106 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002107 END_SELECT_LOOP(s)
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 if (newfd == INVALID_SOCKET)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002110 return (!async_err) ? s->errorhandler() : NULL;
Barry Warsaw752300b1997-01-03 17:18:10 +00002111
Victor Stinnerdaf45552013-08-28 00:53:59 +02002112#ifdef MS_WINDOWS
2113 if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) {
2114 PyErr_SetFromWindowsErr(0);
2115 SOCKETCLOSE(newfd);
2116 goto finally;
2117 }
2118#else
2119
2120#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
2121 if (!accept4_works)
2122#endif
2123 {
2124 if (_Py_set_inheritable(newfd, 0, NULL) < 0) {
2125 SOCKETCLOSE(newfd);
2126 goto finally;
2127 }
2128 }
2129#endif
2130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 sock = PyLong_FromSocket_t(newfd);
2132 if (sock == NULL) {
2133 SOCKETCLOSE(newfd);
2134 goto finally;
2135 }
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
2138 addrlen, s->sock_proto);
2139 if (addr == NULL)
2140 goto finally;
Barry Warsaw752300b1997-01-03 17:18:10 +00002141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 res = PyTuple_Pack(2, sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00002143
Guido van Rossum67f7a382002-06-06 21:08:16 +00002144finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 Py_XDECREF(sock);
2146 Py_XDECREF(addr);
2147 return res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002148}
2149
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002150PyDoc_STRVAR(accept_doc,
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002151"_accept() -> (integer, address info)\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002152\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002153Wait for an incoming connection. Return a new socket file descriptor\n\
2154representing the connection, and the address of the client.\n\
2155For IP sockets, the address info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002156
Guido van Rossum11ba0942002-06-13 15:07:44 +00002157/* s.setblocking(flag) method. Argument:
2158 False -- non-blocking mode; same as settimeout(0)
2159 True -- blocking mode; same as settimeout(None)
2160*/
Guido van Rossume4485b01994-09-07 14:32:49 +00002161
Guido van Rossum73624e91994-10-10 17:59:00 +00002162static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002163sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00002164{
Serhiy Storchaka78980432013-01-15 01:12:17 +02002165 long block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 block = PyLong_AsLong(arg);
2168 if (block == -1 && PyErr_Occurred())
2169 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002170
Victor Stinner71694d52015-03-28 01:18:54 +01002171 s->sock_timeout = block ? -1 : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00002173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 Py_INCREF(Py_None);
2175 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00002176}
Guido van Rossume4485b01994-09-07 14:32:49 +00002177
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002178PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002179"setblocking(flag)\n\
2180\n\
2181Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00002182setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002183setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002184
Victor Stinner71694d52015-03-28 01:18:54 +01002185static int
2186socket_parse_timeout(_PyTime_t *timeout, PyObject *timeout_obj)
2187{
2188#ifdef MS_WINDOWS
2189 struct timeval tv;
2190#endif
2191 int overflow = 0;
2192
2193 if (timeout_obj == Py_None) {
2194 *timeout = -1;
2195 return 0;
2196 }
2197
Victor Stinner869e1772015-03-30 03:49:14 +02002198 if (_PyTime_FromSecondsObject(timeout,
2199 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Victor Stinner71694d52015-03-28 01:18:54 +01002200 return -1;
2201
2202 if (*timeout < 0) {
2203 PyErr_SetString(PyExc_ValueError, "Timeout value out of range");
2204 return -1;
2205 }
2206
2207#ifdef MS_WINDOWS
Victor Stinner869e1772015-03-30 03:49:14 +02002208 overflow = (_PyTime_AsTimeval(timeout, &tv, _PyTime_ROUND_CEILING) < 0);
Victor Stinner71694d52015-03-28 01:18:54 +01002209#endif
2210#ifndef HAVE_POLL
Victor Stinner869e1772015-03-30 03:49:14 +02002211 timeout = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING);
Victor Stinner71694d52015-03-28 01:18:54 +01002212 overflow = (timeout > INT_MAX);
2213#endif
2214 if (overflow) {
2215 PyErr_SetString(PyExc_OverflowError,
2216 "timeout doesn't fit into C timeval");
2217 return -1;
2218 }
2219
2220 return 0;
2221}
2222
Guido van Rossum11ba0942002-06-13 15:07:44 +00002223/* s.settimeout(timeout) method. Argument:
2224 None -- no timeout, blocking mode; same as setblocking(True)
2225 0.0 -- non-blocking mode; same as setblocking(False)
2226 > 0 -- timeout mode; operations time out after timeout seconds
2227 < 0 -- illegal; raises an exception
2228*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00002229static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002230sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00002231{
Victor Stinner71694d52015-03-28 01:18:54 +01002232 _PyTime_t timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002233
Victor Stinner71694d52015-03-28 01:18:54 +01002234 if (socket_parse_timeout(&timeout, arg) < 0)
2235 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 s->sock_timeout = timeout;
Victor Stinner71694d52015-03-28 01:18:54 +01002238 internal_setblocking(s, timeout < 0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002240 Py_INCREF(Py_None);
2241 return Py_None;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002242}
2243
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002244PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002245"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00002246\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00002247Set a timeout on socket operations. 'timeout' can be a float,\n\
2248giving in seconds, or None. Setting a timeout of None disables\n\
2249the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002250Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00002251
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002252/* s.gettimeout() method.
2253 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00002254static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002255sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00002256{
Victor Stinner71694d52015-03-28 01:18:54 +01002257 if (s->sock_timeout < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 Py_INCREF(Py_None);
2259 return Py_None;
2260 }
Victor Stinner71694d52015-03-28 01:18:54 +01002261 else {
2262 double seconds = _PyTime_AsSecondsDouble(s->sock_timeout);
2263 return PyFloat_FromDouble(seconds);
2264 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002265}
2266
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002267PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002268"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00002269\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03002270Returns the timeout in seconds (float) associated with socket \n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00002271operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002272operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00002273
Guido van Rossumaee08791992-09-08 09:05:33 +00002274/* s.setsockopt() method.
2275 With an integer third argument, sets an integer option.
2276 With a string third argument, sets an option from a buffer;
2277 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002278
Guido van Rossum73624e91994-10-10 17:59:00 +00002279static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002280sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 int level;
2283 int optname;
2284 int res;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02002285 Py_buffer optval;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 if (PyArg_ParseTuple(args, "iii:setsockopt",
2289 &level, &optname, &flag)) {
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02002290 res = setsockopt(s->sock_fd, level, optname, &flag, sizeof flag);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 }
2292 else {
2293 PyErr_Clear();
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02002294 if (!PyArg_ParseTuple(args, "iiy*:setsockopt",
2295 &level, &optname, &optval))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 return NULL;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02002297 res = setsockopt(s->sock_fd, level, optname, optval.buf, optval.len);
2298 PyBuffer_Release(&optval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 if (res < 0)
2301 return s->errorhandler();
2302 Py_INCREF(Py_None);
2303 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002304}
2305
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002306PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002307"setsockopt(level, option, value)\n\
2308\n\
2309Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002310The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002311
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002312
Guido van Rossumaee08791992-09-08 09:05:33 +00002313/* s.getsockopt() method.
2314 With two arguments, retrieves an integer option.
2315 With a third integer argument, retrieves a string buffer of that size;
2316 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002317
Guido van Rossum73624e91994-10-10 17:59:00 +00002318static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002319sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 int level;
2322 int optname;
2323 int res;
2324 PyObject *buf;
2325 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
2328 &level, &optname, &buflen))
2329 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 if (buflen == 0) {
2332 int flag = 0;
2333 socklen_t flagsize = sizeof flag;
2334 res = getsockopt(s->sock_fd, level, optname,
2335 (void *)&flag, &flagsize);
2336 if (res < 0)
2337 return s->errorhandler();
2338 return PyLong_FromLong(flag);
2339 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 if (buflen <= 0 || buflen > 1024) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002341 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 "getsockopt buflen out of range");
2343 return NULL;
2344 }
2345 buf = PyBytes_FromStringAndSize((char *)NULL, buflen);
2346 if (buf == NULL)
2347 return NULL;
2348 res = getsockopt(s->sock_fd, level, optname,
2349 (void *)PyBytes_AS_STRING(buf), &buflen);
2350 if (res < 0) {
2351 Py_DECREF(buf);
2352 return s->errorhandler();
2353 }
2354 _PyBytes_Resize(&buf, buflen);
2355 return buf;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002356}
2357
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002358PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002359"getsockopt(level, option[, buffersize]) -> value\n\
2360\n\
2361Get a socket option. See the Unix manual for level and option.\n\
2362If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002363string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002364
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002365
Fred Drake728819a2000-07-01 03:40:12 +00002366/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002367
Guido van Rossum73624e91994-10-10 17:59:00 +00002368static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002369sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 sock_addr_t addrbuf;
2372 int addrlen;
2373 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2376 return NULL;
2377 Py_BEGIN_ALLOW_THREADS
2378 res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);
2379 Py_END_ALLOW_THREADS
2380 if (res < 0)
2381 return s->errorhandler();
2382 Py_INCREF(Py_None);
2383 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002384}
2385
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002386PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002387"bind(address)\n\
2388\n\
2389Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00002390pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002391sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002392
Guido van Rossum30a685f1991-06-27 15:51:29 +00002393
2394/* s.close() method.
2395 Set the file descriptor to -1 so operations tried subsequently
2396 will surely fail. */
2397
Guido van Rossum73624e91994-10-10 17:59:00 +00002398static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002399sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002402
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002403 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
2404 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
2405 * for more details.
2406 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 if ((fd = s->sock_fd) != -1) {
2408 s->sock_fd = -1;
2409 Py_BEGIN_ALLOW_THREADS
2410 (void) SOCKETCLOSE(fd);
2411 Py_END_ALLOW_THREADS
2412 }
2413 Py_INCREF(Py_None);
2414 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002415}
2416
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002417PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002418"close()\n\
2419\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002420Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002421
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002422static PyObject *
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002423sock_detach(PySocketSockObject *s)
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002424{
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002425 SOCKET_T fd = s->sock_fd;
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002426 s->sock_fd = -1;
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002427 return PyLong_FromSocket_t(fd);
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002428}
2429
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002430PyDoc_STRVAR(detach_doc,
2431"detach()\n\
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002432\n\
Guido van Rossum638aebd2013-12-04 11:50:09 -08002433Close the socket object without closing the underlying file descriptor.\n\
2434The object cannot be used after this call, but the file descriptor\n\
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002435can be reused for other purposes. The file descriptor is returned.");
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002436
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002437static int
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002438internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 int *timeoutp)
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 int res, timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 timeout = 0;
Victor Stinnerb7df3142015-03-27 22:59:32 +01002444
2445 Py_BEGIN_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 res = connect(s->sock_fd, addr, addrlen);
Victor Stinnerb7df3142015-03-27 22:59:32 +01002447 Py_END_ALLOW_THREADS
2448
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002449#ifdef MS_WINDOWS
2450
Victor Stinner71694d52015-03-28 01:18:54 +01002451 if (s->sock_timeout > 0
Victor Stinnerb7df3142015-03-27 22:59:32 +01002452 && res < 0 && WSAGetLastError() == WSAEWOULDBLOCK
2453 && IS_SELECTABLE(s)) {
2454 /* This is a mess. Best solution: trust select */
2455 fd_set fds;
2456 fd_set fds_exc;
2457 struct timeval tv;
Victor Stinner71694d52015-03-28 01:18:54 +01002458 int conv;
2459
Victor Stinner869e1772015-03-30 03:49:14 +02002460 _PyTime_AsTimeval_noraise(s->sock_timeout, &tv, _PyTime_ROUND_CEILING);
Victor Stinnerb7df3142015-03-27 22:59:32 +01002461
2462 Py_BEGIN_ALLOW_THREADS
Victor Stinnerb7df3142015-03-27 22:59:32 +01002463 FD_ZERO(&fds);
2464 FD_SET(s->sock_fd, &fds);
2465 FD_ZERO(&fds_exc);
2466 FD_SET(s->sock_fd, &fds_exc);
2467 res = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
2468 NULL, &fds, &fds_exc, &tv);
2469 Py_END_ALLOW_THREADS
2470
2471 if (res == 0) {
2472 res = WSAEWOULDBLOCK;
2473 timeout = 1;
2474 } else if (res > 0) {
2475 if (FD_ISSET(s->sock_fd, &fds))
2476 /* The socket is in the writable set - this
2477 means connected */
2478 res = 0;
2479 else {
2480 /* As per MS docs, we need to call getsockopt()
2481 to get the underlying error */
2482 int res_size = sizeof res;
2483 /* It must be in the exception set */
2484 assert(FD_ISSET(s->sock_fd, &fds_exc));
2485 if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
2486 (char *)&res, &res_size))
2487 /* getsockopt also clears WSAGetLastError,
2488 so reset it back. */
2489 WSASetLastError(res);
2490 else
2491 res = WSAGetLastError();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 }
Victor Stinnerb7df3142015-03-27 22:59:32 +01002494 /* else if (res < 0) an error occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002495 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 if (res < 0)
2498 res = WSAGetLastError();
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002499
2500#else
2501
Victor Stinner71694d52015-03-28 01:18:54 +01002502 if (s->sock_timeout > 0
Victor Stinnerb7df3142015-03-27 22:59:32 +01002503 && res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) {
2504
Victor Stinnere6951c62015-03-31 13:50:44 +02002505 timeout = internal_connect_select(s);
Victor Stinnerb7df3142015-03-27 22:59:32 +01002506
2507 if (timeout == 0) {
2508 /* Bug #1019808: in case of an EINPROGRESS,
2509 use getsockopt(SO_ERROR) to get the real
2510 error. */
2511 socklen_t res_size = sizeof res;
2512 (void)getsockopt(s->sock_fd, SOL_SOCKET,
2513 SO_ERROR, &res, &res_size);
2514 if (res == EISCONN)
2515 res = 0;
2516 errno = res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002517 }
Victor Stinnerb7df3142015-03-27 22:59:32 +01002518 else if (timeout == -1) {
2519 res = errno; /* had error */
2520 }
2521 else
2522 res = EWOULDBLOCK; /* timed out */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 if (res < 0)
2526 res = errno;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002527
2528#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 *timeoutp = timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002530
Victor Stinner71694d52015-03-28 01:18:54 +01002531 assert(res >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 return res;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002533}
Guido van Rossum30a685f1991-06-27 15:51:29 +00002534
Fred Drake728819a2000-07-01 03:40:12 +00002535/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002536
Guido van Rossum73624e91994-10-10 17:59:00 +00002537static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002538sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 sock_addr_t addrbuf;
2541 int addrlen;
2542 int res;
2543 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002545 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2546 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 if (timeout == 1) {
2551 PyErr_SetString(socket_timeout, "timed out");
2552 return NULL;
2553 }
Victor Stinner71694d52015-03-28 01:18:54 +01002554 if (res < 0)
2555 return NULL;
2556 if (res != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 return s->errorhandler();
Victor Stinner71694d52015-03-28 01:18:54 +01002558 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002559 Py_INCREF(Py_None);
2560 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002561}
2562
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002563PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002564"connect(address)\n\
2565\n\
2566Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002567is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002568
Guido van Rossum30a685f1991-06-27 15:51:29 +00002569
Fred Drake728819a2000-07-01 03:40:12 +00002570/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002571
2572static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002573sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002574{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002575 sock_addr_t addrbuf;
2576 int addrlen;
2577 int res;
2578 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2581 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002584
Victor Stinner71694d52015-03-28 01:18:54 +01002585 if (res < 0)
2586 return NULL;
2587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 /* Signals are not errors (though they may raise exceptions). Adapted
2589 from PyErr_SetFromErrnoWithFilenameObject(). */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 if (res == EINTR && PyErr_CheckSignals())
2591 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 return PyLong_FromLong((long) res);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002594}
2595
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002596PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002597"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002598\n\
2599This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002600instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002601
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002602
Guido van Rossumed233a51992-06-23 09:07:03 +00002603/* s.fileno() method */
2604
Guido van Rossum73624e91994-10-10 17:59:00 +00002605static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002606sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00002607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002608 return PyLong_FromSocket_t(s->sock_fd);
Guido van Rossumed233a51992-06-23 09:07:03 +00002609}
2610
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002611PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002612"fileno() -> integer\n\
2613\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002614Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002615
Guido van Rossumed233a51992-06-23 09:07:03 +00002616
Guido van Rossumc89705d1992-11-26 08:54:07 +00002617/* s.getsockname() method */
2618
Guido van Rossum73624e91994-10-10 17:59:00 +00002619static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002620sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 sock_addr_t addrbuf;
2623 int res;
2624 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 if (!getsockaddrlen(s, &addrlen))
2627 return NULL;
2628 memset(&addrbuf, 0, addrlen);
2629 Py_BEGIN_ALLOW_THREADS
2630 res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2631 Py_END_ALLOW_THREADS
2632 if (res < 0)
2633 return s->errorhandler();
2634 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2635 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002636}
2637
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002638PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002639"getsockname() -> address info\n\
2640\n\
2641Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002642info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002643
Guido van Rossumc89705d1992-11-26 08:54:07 +00002644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002646/* s.getpeername() method */
2647
Guido van Rossum73624e91994-10-10 17:59:00 +00002648static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002649sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 sock_addr_t addrbuf;
2652 int res;
2653 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 if (!getsockaddrlen(s, &addrlen))
2656 return NULL;
2657 memset(&addrbuf, 0, addrlen);
2658 Py_BEGIN_ALLOW_THREADS
2659 res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2660 Py_END_ALLOW_THREADS
2661 if (res < 0)
2662 return s->errorhandler();
2663 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2664 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002665}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002666
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002667PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002668"getpeername() -> address info\n\
2669\n\
2670Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002671info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002672
Guido van Rossumb6775db1994-08-01 11:34:53 +00002673#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002674
2675
Guido van Rossum30a685f1991-06-27 15:51:29 +00002676/* s.listen(n) method */
2677
Guido van Rossum73624e91994-10-10 17:59:00 +00002678static PyObject *
Charles-François Natali644b8f52014-05-22 19:45:39 +01002679sock_listen(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002680{
Charles-François Natali644b8f52014-05-22 19:45:39 +01002681 /* We try to choose a default backlog high enough to avoid connection drops
2682 * for common workloads, yet not too high to limit resource usage. */
2683 int backlog = Py_MIN(SOMAXCONN, 128);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002685
Charles-François Natali644b8f52014-05-22 19:45:39 +01002686 if (!PyArg_ParseTuple(args, "|i:listen", &backlog))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002687 return NULL;
Charles-François Natali644b8f52014-05-22 19:45:39 +01002688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou1be815a2011-05-10 19:16:29 +02002690 /* To avoid problems on systems that don't allow a negative backlog
2691 * (which doesn't make sense anyway) we force a minimum value of 0. */
2692 if (backlog < 0)
2693 backlog = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002694 res = listen(s->sock_fd, backlog);
2695 Py_END_ALLOW_THREADS
2696 if (res < 0)
2697 return s->errorhandler();
2698 Py_INCREF(Py_None);
2699 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002700}
2701
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002702PyDoc_STRVAR(listen_doc,
Charles-François Natali644b8f52014-05-22 19:45:39 +01002703"listen([backlog])\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002704\n\
Charles-François Natali644b8f52014-05-22 19:45:39 +01002705Enable a server to accept connections. If backlog is specified, it must be\n\
2706at least 0 (if it is lower, it is set to 0); it specifies the number of\n\
Antoine Pitrou1be815a2011-05-10 19:16:29 +02002707unaccepted connections that the system will allow before refusing new\n\
Charles-François Natali644b8f52014-05-22 19:45:39 +01002708connections. If not specified, a default reasonable value is chosen.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002709
2710
Thomas Wouters477c8d52006-05-27 19:21:47 +00002711/*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002712 * This is the guts of the recv() and recv_into() methods, which reads into a
Thomas Wouters902d6eb2007-01-09 23:18:33 +00002713 * char buffer. If you have any inc/dec ref to do to the objects that contain
Thomas Wouters477c8d52006-05-27 19:21:47 +00002714 * the buffer, do it in the caller. This function returns the number of bytes
Ezio Melotti13925002011-03-16 11:05:33 +02002715 * successfully read. If there was an error, it returns -1. Note that it is
Thomas Wouters477c8d52006-05-27 19:21:47 +00002716 * also possible that we return a number of bytes smaller than the request
2717 * bytes.
2718 */
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002719
Antoine Pitrou19467d22010-08-17 19:33:30 +00002720static Py_ssize_t
2721sock_recv_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002722{
Antoine Pitrou19467d22010-08-17 19:33:30 +00002723 Py_ssize_t outlen = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 int timeout;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002725 int async_err = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 if (!IS_SELECTABLE(s)) {
2728 select_error();
2729 return -1;
2730 }
2731 if (len == 0) {
2732 /* If 0 bytes were requested, do nothing. */
2733 return 0;
2734 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002735
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002736 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002737 do {
Victor Stinner391fa712015-03-31 13:15:31 +02002738 timeout = internal_select(s, 0, interval);
Victor Stinnerb7df3142015-03-27 22:59:32 +01002739
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002740 if (!timeout) {
Victor Stinnerb7df3142015-03-27 22:59:32 +01002741 Py_BEGIN_ALLOW_THREADS
Victor Stinner14b9b112013-06-25 00:37:25 +02002742#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002743 if (len > INT_MAX)
2744 len = INT_MAX;
2745 outlen = recv(s->sock_fd, cbuf, (int)len, flags);
Victor Stinner9a644b22013-06-24 23:47:41 +02002746#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002747 outlen = recv(s->sock_fd, cbuf, len, flags);
Victor Stinner9a644b22013-06-24 23:47:41 +02002748#endif
Victor Stinnerb7df3142015-03-27 22:59:32 +01002749 Py_END_ALLOW_THREADS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002750 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002751 } while (outlen < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Thomas Wouters477c8d52006-05-27 19:21:47 +00002752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 if (timeout == 1) {
2754 PyErr_SetString(socket_timeout, "timed out");
2755 return -1;
2756 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002757 END_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 if (outlen < 0) {
2759 /* Note: the call to errorhandler() ALWAYS indirectly returned
2760 NULL, so ignore its return value */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002761 if (!async_err)
2762 s->errorhandler();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 return -1;
2764 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 return outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002766}
2767
Guido van Rossum48a680c2001-03-02 06:34:14 +00002768
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002769/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002770
Guido van Rossum73624e91994-10-10 17:59:00 +00002771static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002772sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002773{
Antoine Pitrou19467d22010-08-17 19:33:30 +00002774 Py_ssize_t recvlen, outlen;
2775 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 PyObject *buf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002777
Antoine Pitrou19467d22010-08-17 19:33:30 +00002778 if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002779 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 if (recvlen < 0) {
2782 PyErr_SetString(PyExc_ValueError,
2783 "negative buffersize in recv");
2784 return NULL;
2785 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 /* Allocate a new string. */
2788 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2789 if (buf == NULL)
2790 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 /* Call the guts */
2793 outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags);
2794 if (outlen < 0) {
2795 /* An error occurred, release the string and return an
2796 error. */
2797 Py_DECREF(buf);
2798 return NULL;
2799 }
2800 if (outlen != recvlen) {
2801 /* We did not read as many bytes as we anticipated, resize the
2802 string if possible and be successful. */
2803 _PyBytes_Resize(&buf, outlen);
2804 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 return buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002807}
2808
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002809PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002810"recv(buffersize[, flags]) -> data\n\
2811\n\
2812Receive up to buffersize bytes from the socket. For the optional flags\n\
2813argument, see the Unix manual. When no data is available, block until\n\
2814at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002815the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002816
Guido van Rossum30a685f1991-06-27 15:51:29 +00002817
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002818/* s.recv_into(buffer, [nbytes [,flags]]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002819
Thomas Wouters477c8d52006-05-27 19:21:47 +00002820static PyObject*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002821sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00002824
Antoine Pitrou19467d22010-08-17 19:33:30 +00002825 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 Py_buffer pbuf;
2827 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002828 Py_ssize_t buflen, readlen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 /* Get the buffer's memory */
Antoine Pitrou19467d22010-08-17 19:33:30 +00002831 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 &pbuf, &recvlen, &flags))
2833 return NULL;
2834 buf = pbuf.buf;
2835 buflen = pbuf.len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 if (recvlen < 0) {
2838 PyBuffer_Release(&pbuf);
2839 PyErr_SetString(PyExc_ValueError,
2840 "negative buffersize in recv_into");
2841 return NULL;
2842 }
2843 if (recvlen == 0) {
2844 /* If nbytes was not specified, use the buffer's length */
2845 recvlen = buflen;
2846 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 /* Check if the buffer is large enough */
2849 if (buflen < recvlen) {
2850 PyBuffer_Release(&pbuf);
2851 PyErr_SetString(PyExc_ValueError,
2852 "buffer too small for requested bytes");
2853 return NULL;
2854 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 /* Call the guts */
2857 readlen = sock_recv_guts(s, buf, recvlen, flags);
2858 if (readlen < 0) {
2859 /* Return an error. */
2860 PyBuffer_Release(&pbuf);
2861 return NULL;
2862 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 PyBuffer_Release(&pbuf);
2865 /* Return the number of bytes read. Note that we do not do anything
2866 special here in the case that readlen < recvlen. */
2867 return PyLong_FromSsize_t(readlen);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002868}
2869
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002870PyDoc_STRVAR(recv_into_doc,
2871"recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002872\n\
2873A version of recv() that stores its data into a buffer rather than creating \n\
2874a new string. Receive up to buffersize bytes from the socket. If buffersize \n\
2875is not specified (or 0), receive up to the size available in the given buffer.\n\
2876\n\
2877See recv() for documentation about the flags.");
2878
2879
2880/*
Christian Heimes99170a52007-12-19 02:07:34 +00002881 * This is the guts of the recvfrom() and recvfrom_into() methods, which reads
2882 * into a char buffer. If you have any inc/def ref to do to the objects that
2883 * contain the buffer, do it in the caller. This function returns the number
Ezio Melotti13925002011-03-16 11:05:33 +02002884 * of bytes successfully read. If there was an error, it returns -1. Note
Christian Heimes99170a52007-12-19 02:07:34 +00002885 * that it is also possible that we return a number of bytes smaller than the
2886 * request bytes.
Thomas Wouters477c8d52006-05-27 19:21:47 +00002887 *
2888 * 'addr' is a return value for the address object. Note that you must decref
2889 * it yourself.
2890 */
Antoine Pitrou19467d22010-08-17 19:33:30 +00002891static Py_ssize_t
2892sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 PyObject** addr)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 sock_addr_t addrbuf;
2896 int timeout;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002897 Py_ssize_t n = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 socklen_t addrlen;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002899 int async_err = 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 *addr = NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 if (!getsockaddrlen(s, &addrlen))
2904 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 if (!IS_SELECTABLE(s)) {
2907 select_error();
2908 return -1;
2909 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002910
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002911 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002912 do {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002913 memset(&addrbuf, 0, addrlen);
Victor Stinner391fa712015-03-31 13:15:31 +02002914 timeout = internal_select(s, 0, interval);
Victor Stinnerb7df3142015-03-27 22:59:32 +01002915
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002916 if (!timeout) {
Victor Stinnerb7df3142015-03-27 22:59:32 +01002917 Py_BEGIN_ALLOW_THREADS
Victor Stinnere990c6e2013-11-16 00:18:58 +01002918#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002919 if (len > INT_MAX)
2920 len = INT_MAX;
2921 n = recvfrom(s->sock_fd, cbuf, (int)len, flags,
2922 (void *) &addrbuf, &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002923#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002924 n = recvfrom(s->sock_fd, cbuf, len, flags,
2925 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002926#endif
Victor Stinnerb7df3142015-03-27 22:59:32 +01002927 Py_END_ALLOW_THREADS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002928 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002929 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 if (timeout == 1) {
2932 PyErr_SetString(socket_timeout, "timed out");
2933 return -1;
2934 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002935 END_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002937 if (!async_err)
2938 s->errorhandler();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 return -1;
2940 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
2943 addrlen, s->sock_proto)))
2944 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 return n;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002947}
2948
2949/* s.recvfrom(nbytes [,flags]) method */
2950
2951static PyObject *
2952sock_recvfrom(PySocketSockObject *s, PyObject *args)
2953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 PyObject *buf = NULL;
2955 PyObject *addr = NULL;
2956 PyObject *ret = NULL;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002957 int flags = 0;
2958 Py_ssize_t recvlen, outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002959
Antoine Pitrou19467d22010-08-17 19:33:30 +00002960 if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 if (recvlen < 0) {
2964 PyErr_SetString(PyExc_ValueError,
2965 "negative buffersize in recvfrom");
2966 return NULL;
2967 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002969 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2970 if (buf == NULL)
2971 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973 outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf),
2974 recvlen, flags, &addr);
2975 if (outlen < 0) {
2976 goto finally;
2977 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 if (outlen != recvlen) {
2980 /* We did not read as many bytes as we anticipated, resize the
Ezio Melotti13925002011-03-16 11:05:33 +02002981 string if possible and be successful. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982 if (_PyBytes_Resize(&buf, outlen) < 0)
Ezio Melotti13925002011-03-16 11:05:33 +02002983 /* Oopsy, not so successful after all. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 goto finally;
2985 }
Barry Warsaw752300b1997-01-03 17:18:10 +00002986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002988
2989finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 Py_XDECREF(buf);
2991 Py_XDECREF(addr);
2992 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002993}
2994
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002995PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002996"recvfrom(buffersize[, flags]) -> (data, address info)\n\
2997\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002998Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002999
Thomas Wouters477c8d52006-05-27 19:21:47 +00003000
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003001/* s.recvfrom_into(buffer[, nbytes [,flags]]) method */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003002
3003static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003004sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00003005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00003007
Antoine Pitrou19467d22010-08-17 19:33:30 +00003008 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 Py_buffer pbuf;
3010 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003011 Py_ssize_t readlen, buflen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 PyObject *addr = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003014
Antoine Pitrou19467d22010-08-17 19:33:30 +00003015 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 kwlist, &pbuf,
3017 &recvlen, &flags))
3018 return NULL;
3019 buf = pbuf.buf;
3020 buflen = pbuf.len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 if (recvlen < 0) {
3023 PyBuffer_Release(&pbuf);
3024 PyErr_SetString(PyExc_ValueError,
3025 "negative buffersize in recvfrom_into");
3026 return NULL;
3027 }
3028 if (recvlen == 0) {
3029 /* If nbytes was not specified, use the buffer's length */
3030 recvlen = buflen;
Benjamin Petersonc6b37e22014-01-13 23:14:42 -05003031 } else if (recvlen > buflen) {
3032 PyBuffer_Release(&pbuf);
3033 PyErr_SetString(PyExc_ValueError,
3034 "nbytes is greater than the length of the buffer");
3035 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
3039 if (readlen < 0) {
3040 PyBuffer_Release(&pbuf);
3041 /* Return an error */
3042 Py_XDECREF(addr);
3043 return NULL;
3044 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 PyBuffer_Release(&pbuf);
3047 /* Return the number of bytes read and the address. Note that we do
3048 not do anything special here in the case that readlen < recvlen. */
Antoine Pitrou19467d22010-08-17 19:33:30 +00003049 return Py_BuildValue("nN", readlen, addr);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003050}
3051
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003052PyDoc_STRVAR(recvfrom_into_doc,
3053"recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00003054\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003055Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00003056
3057
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003058/* The sendmsg() and recvmsg[_into]() methods require a working
3059 CMSG_LEN(). See the comment near get_CMSG_LEN(). */
3060#ifdef CMSG_LEN
3061/*
3062 * Call recvmsg() with the supplied iovec structures, flags, and
3063 * ancillary data buffer size (controllen). Returns the tuple return
3064 * value for recvmsg() or recvmsg_into(), with the first item provided
3065 * by the supplied makeval() function. makeval() will be called with
3066 * the length read and makeval_data as arguments, and must return a
3067 * new reference (which will be decrefed if there is a subsequent
3068 * error). On error, closes any file descriptors received via
3069 * SCM_RIGHTS.
3070 */
3071static PyObject *
3072sock_recvmsg_guts(PySocketSockObject *s, struct iovec *iov, int iovlen,
3073 int flags, Py_ssize_t controllen,
3074 PyObject *(*makeval)(ssize_t, void *), void *makeval_data)
3075{
3076 ssize_t bytes_received = -1;
3077 int timeout;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003078 int async_err = 0;
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003079 sock_addr_t addrbuf;
3080 socklen_t addrbuflen;
Charles-François Natalie9e95ae2011-08-24 21:40:53 +02003081 struct msghdr msg = {0};
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003082 PyObject *cmsg_list = NULL, *retval = NULL;
3083 void *controlbuf = NULL;
3084 struct cmsghdr *cmsgh;
3085 size_t cmsgdatalen = 0;
3086 int cmsg_status;
3087
3088 /* XXX: POSIX says that msg_name and msg_namelen "shall be
3089 ignored" when the socket is connected (Linux fills them in
3090 anyway for AF_UNIX sockets at least). Normally msg_namelen
3091 seems to be set to 0 if there's no address, but try to
3092 initialize msg_name to something that won't be mistaken for a
3093 real address if that doesn't happen. */
3094 if (!getsockaddrlen(s, &addrbuflen))
3095 return NULL;
3096 memset(&addrbuf, 0, addrbuflen);
3097 SAS2SA(&addrbuf)->sa_family = AF_UNSPEC;
3098
3099 if (controllen < 0 || controllen > SOCKLEN_T_LIMIT) {
3100 PyErr_SetString(PyExc_ValueError,
3101 "invalid ancillary data buffer length");
3102 return NULL;
3103 }
3104 if (controllen > 0 && (controlbuf = PyMem_Malloc(controllen)) == NULL)
3105 return PyErr_NoMemory();
3106
3107 /* Make the system call. */
3108 if (!IS_SELECTABLE(s)) {
3109 select_error();
3110 goto finally;
3111 }
3112
3113 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003114 do {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003115 msg.msg_name = SAS2SA(&addrbuf);
3116 msg.msg_namelen = addrbuflen;
3117 msg.msg_iov = iov;
3118 msg.msg_iovlen = iovlen;
3119 msg.msg_control = controlbuf;
3120 msg.msg_controllen = controllen;
Victor Stinner391fa712015-03-31 13:15:31 +02003121 timeout = internal_select(s, 0, interval);
Victor Stinnerb7df3142015-03-27 22:59:32 +01003122
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003123 if (timeout == 1) {
3124 PyErr_SetString(socket_timeout, "timed out");
3125 goto finally;
3126 }
Victor Stinnerb7df3142015-03-27 22:59:32 +01003127
3128 if (!timeout) {
3129 Py_BEGIN_ALLOW_THREADS;
3130 bytes_received = recvmsg(s->sock_fd, &msg, flags);
3131 Py_END_ALLOW_THREADS;
3132 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003133 } while (bytes_received < 0 && errno == EINTR &&
3134 !(async_err = PyErr_CheckSignals()));
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003135 END_SELECT_LOOP(s)
3136
3137 if (bytes_received < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003138 if (!async_err)
3139 s->errorhandler();
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003140 goto finally;
3141 }
3142
3143 /* Make list of (level, type, data) tuples from control messages. */
3144 if ((cmsg_list = PyList_New(0)) == NULL)
3145 goto err_closefds;
3146 /* Check for empty ancillary data as old CMSG_FIRSTHDR()
3147 implementations didn't do so. */
3148 for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL);
3149 cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) {
3150 PyObject *bytes, *tuple;
3151 int tmp;
3152
3153 cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen);
3154 if (cmsg_status != 0) {
3155 if (PyErr_WarnEx(PyExc_RuntimeWarning,
3156 "received malformed or improperly-truncated "
3157 "ancillary data", 1) == -1)
3158 goto err_closefds;
3159 }
3160 if (cmsg_status < 0)
3161 break;
3162 if (cmsgdatalen > PY_SSIZE_T_MAX) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003163 PyErr_SetString(PyExc_OSError, "control message too long");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003164 goto err_closefds;
3165 }
3166
3167 bytes = PyBytes_FromStringAndSize((char *)CMSG_DATA(cmsgh),
3168 cmsgdatalen);
3169 tuple = Py_BuildValue("iiN", (int)cmsgh->cmsg_level,
3170 (int)cmsgh->cmsg_type, bytes);
3171 if (tuple == NULL)
3172 goto err_closefds;
3173 tmp = PyList_Append(cmsg_list, tuple);
3174 Py_DECREF(tuple);
3175 if (tmp != 0)
3176 goto err_closefds;
3177
3178 if (cmsg_status != 0)
3179 break;
3180 }
3181
3182 retval = Py_BuildValue("NOiN",
3183 (*makeval)(bytes_received, makeval_data),
3184 cmsg_list,
3185 (int)msg.msg_flags,
3186 makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
3187 ((msg.msg_namelen > addrbuflen) ?
3188 addrbuflen : msg.msg_namelen),
3189 s->sock_proto));
3190 if (retval == NULL)
3191 goto err_closefds;
3192
3193finally:
3194 Py_XDECREF(cmsg_list);
3195 PyMem_Free(controlbuf);
3196 return retval;
3197
3198err_closefds:
3199#ifdef SCM_RIGHTS
3200 /* Close all descriptors coming from SCM_RIGHTS, so they don't leak. */
3201 for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL);
3202 cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) {
3203 cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen);
3204 if (cmsg_status < 0)
3205 break;
3206 if (cmsgh->cmsg_level == SOL_SOCKET &&
3207 cmsgh->cmsg_type == SCM_RIGHTS) {
3208 size_t numfds;
3209 int *fdp;
3210
3211 numfds = cmsgdatalen / sizeof(int);
3212 fdp = (int *)CMSG_DATA(cmsgh);
3213 while (numfds-- > 0)
3214 close(*fdp++);
3215 }
3216 if (cmsg_status != 0)
3217 break;
3218 }
3219#endif /* SCM_RIGHTS */
3220 goto finally;
3221}
3222
3223
3224static PyObject *
3225makeval_recvmsg(ssize_t received, void *data)
3226{
3227 PyObject **buf = data;
3228
3229 if (received < PyBytes_GET_SIZE(*buf))
3230 _PyBytes_Resize(buf, received);
3231 Py_XINCREF(*buf);
3232 return *buf;
3233}
3234
3235/* s.recvmsg(bufsize[, ancbufsize[, flags]]) method */
3236
3237static PyObject *
3238sock_recvmsg(PySocketSockObject *s, PyObject *args)
3239{
3240 Py_ssize_t bufsize, ancbufsize = 0;
3241 int flags = 0;
3242 struct iovec iov;
3243 PyObject *buf = NULL, *retval = NULL;
3244
3245 if (!PyArg_ParseTuple(args, "n|ni:recvmsg", &bufsize, &ancbufsize, &flags))
3246 return NULL;
3247
3248 if (bufsize < 0) {
3249 PyErr_SetString(PyExc_ValueError, "negative buffer size in recvmsg()");
3250 return NULL;
3251 }
3252 if ((buf = PyBytes_FromStringAndSize(NULL, bufsize)) == NULL)
3253 return NULL;
3254 iov.iov_base = PyBytes_AS_STRING(buf);
3255 iov.iov_len = bufsize;
3256
3257 /* Note that we're passing a pointer to *our pointer* to the bytes
3258 object here (&buf); makeval_recvmsg() may incref the object, or
3259 deallocate it and set our pointer to NULL. */
3260 retval = sock_recvmsg_guts(s, &iov, 1, flags, ancbufsize,
3261 &makeval_recvmsg, &buf);
3262 Py_XDECREF(buf);
3263 return retval;
3264}
3265
3266PyDoc_STRVAR(recvmsg_doc,
3267"recvmsg(bufsize[, ancbufsize[, flags]]) -> (data, ancdata, msg_flags, address)\n\
3268\n\
3269Receive normal data (up to bufsize bytes) and ancillary data from the\n\
3270socket. The ancbufsize argument sets the size in bytes of the\n\
3271internal buffer used to receive the ancillary data; it defaults to 0,\n\
3272meaning that no ancillary data will be received. Appropriate buffer\n\
3273sizes for ancillary data can be calculated using CMSG_SPACE() or\n\
3274CMSG_LEN(), and items which do not fit into the buffer might be\n\
3275truncated or discarded. The flags argument defaults to 0 and has the\n\
3276same meaning as for recv().\n\
3277\n\
3278The return value is a 4-tuple: (data, ancdata, msg_flags, address).\n\
3279The data item is a bytes object holding the non-ancillary data\n\
3280received. The ancdata item is a list of zero or more tuples\n\
3281(cmsg_level, cmsg_type, cmsg_data) representing the ancillary data\n\
3282(control messages) received: cmsg_level and cmsg_type are integers\n\
3283specifying the protocol level and protocol-specific type respectively,\n\
3284and cmsg_data is a bytes object holding the associated data. The\n\
3285msg_flags item is the bitwise OR of various flags indicating\n\
3286conditions on the received message; see your system documentation for\n\
3287details. If the receiving socket is unconnected, address is the\n\
3288address of the sending socket, if available; otherwise, its value is\n\
3289unspecified.\n\
3290\n\
3291If recvmsg() raises an exception after the system call returns, it\n\
3292will first attempt to close any file descriptors received via the\n\
3293SCM_RIGHTS mechanism.");
3294
3295
3296static PyObject *
3297makeval_recvmsg_into(ssize_t received, void *data)
3298{
3299 return PyLong_FromSsize_t(received);
3300}
3301
3302/* s.recvmsg_into(buffers[, ancbufsize[, flags]]) method */
3303
3304static PyObject *
3305sock_recvmsg_into(PySocketSockObject *s, PyObject *args)
3306{
3307 Py_ssize_t ancbufsize = 0;
3308 int flags = 0;
3309 struct iovec *iovs = NULL;
3310 Py_ssize_t i, nitems, nbufs = 0;
3311 Py_buffer *bufs = NULL;
3312 PyObject *buffers_arg, *fast, *retval = NULL;
3313
3314 if (!PyArg_ParseTuple(args, "O|ni:recvmsg_into",
3315 &buffers_arg, &ancbufsize, &flags))
3316 return NULL;
3317
3318 if ((fast = PySequence_Fast(buffers_arg,
3319 "recvmsg_into() argument 1 must be an "
3320 "iterable")) == NULL)
3321 return NULL;
3322 nitems = PySequence_Fast_GET_SIZE(fast);
3323 if (nitems > INT_MAX) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003324 PyErr_SetString(PyExc_OSError, "recvmsg_into() argument 1 is too long");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003325 goto finally;
3326 }
3327
3328 /* Fill in an iovec for each item, and save the Py_buffer
3329 structs to release afterwards. */
3330 if (nitems > 0 && ((iovs = PyMem_New(struct iovec, nitems)) == NULL ||
3331 (bufs = PyMem_New(Py_buffer, nitems)) == NULL)) {
3332 PyErr_NoMemory();
3333 goto finally;
3334 }
3335 for (; nbufs < nitems; nbufs++) {
3336 if (!PyArg_Parse(PySequence_Fast_GET_ITEM(fast, nbufs),
3337 "w*;recvmsg_into() argument 1 must be an iterable "
3338 "of single-segment read-write buffers",
3339 &bufs[nbufs]))
3340 goto finally;
3341 iovs[nbufs].iov_base = bufs[nbufs].buf;
3342 iovs[nbufs].iov_len = bufs[nbufs].len;
3343 }
3344
3345 retval = sock_recvmsg_guts(s, iovs, nitems, flags, ancbufsize,
3346 &makeval_recvmsg_into, NULL);
3347finally:
3348 for (i = 0; i < nbufs; i++)
3349 PyBuffer_Release(&bufs[i]);
3350 PyMem_Free(bufs);
3351 PyMem_Free(iovs);
3352 Py_DECREF(fast);
3353 return retval;
3354}
3355
3356PyDoc_STRVAR(recvmsg_into_doc,
3357"recvmsg_into(buffers[, ancbufsize[, flags]]) -> (nbytes, ancdata, msg_flags, address)\n\
3358\n\
3359Receive normal data and ancillary data from the socket, scattering the\n\
3360non-ancillary data into a series of buffers. The buffers argument\n\
3361must be an iterable of objects that export writable buffers\n\
3362(e.g. bytearray objects); these will be filled with successive chunks\n\
3363of the non-ancillary data until it has all been written or there are\n\
3364no more buffers. The ancbufsize argument sets the size in bytes of\n\
3365the internal buffer used to receive the ancillary data; it defaults to\n\
33660, meaning that no ancillary data will be received. Appropriate\n\
3367buffer sizes for ancillary data can be calculated using CMSG_SPACE()\n\
3368or CMSG_LEN(), and items which do not fit into the buffer might be\n\
3369truncated or discarded. The flags argument defaults to 0 and has the\n\
3370same meaning as for recv().\n\
3371\n\
3372The return value is a 4-tuple: (nbytes, ancdata, msg_flags, address).\n\
3373The nbytes item is the total number of bytes of non-ancillary data\n\
3374written into the buffers. The ancdata item is a list of zero or more\n\
3375tuples (cmsg_level, cmsg_type, cmsg_data) representing the ancillary\n\
3376data (control messages) received: cmsg_level and cmsg_type are\n\
3377integers specifying the protocol level and protocol-specific type\n\
3378respectively, and cmsg_data is a bytes object holding the associated\n\
3379data. The msg_flags item is the bitwise OR of various flags\n\
3380indicating conditions on the received message; see your system\n\
3381documentation for details. If the receiving socket is unconnected,\n\
3382address is the address of the sending socket, if available; otherwise,\n\
3383its value is unspecified.\n\
3384\n\
3385If recvmsg_into() raises an exception after the system call returns,\n\
3386it will first attempt to close any file descriptors received via the\n\
3387SCM_RIGHTS mechanism.");
3388#endif /* CMSG_LEN */
3389
3390
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00003391/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00003392
Guido van Rossum73624e91994-10-10 17:59:00 +00003393static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003394sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00003395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003396 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003397 Py_ssize_t len, n = -1;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003398 int async_err = 0;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003399 int flags = 0, timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003400 Py_buffer pbuf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags))
3403 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 if (!IS_SELECTABLE(s)) {
3406 PyBuffer_Release(&pbuf);
3407 return select_error();
3408 }
3409 buf = pbuf.buf;
3410 len = pbuf.len;
Neal Norwitz082b2df2006-02-07 07:04:46 +00003411
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003412 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003413 do {
Victor Stinner391fa712015-03-31 13:15:31 +02003414 timeout = internal_select(s, 1, interval);
Victor Stinnerb7df3142015-03-27 22:59:32 +01003415
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003416 if (!timeout) {
Victor Stinnerb7df3142015-03-27 22:59:32 +01003417 Py_BEGIN_ALLOW_THREADS
Christian Heimesaf01f662013-12-21 16:19:10 +01003418#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003419 if (len > INT_MAX)
3420 len = INT_MAX;
3421 n = send(s->sock_fd, buf, (int)len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003422#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003423 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003424#endif
Victor Stinnerb7df3142015-03-27 22:59:32 +01003425 Py_END_ALLOW_THREADS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003426 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003427 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 if (timeout == 1) {
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003429 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003430 PyErr_SetString(socket_timeout, "timed out");
3431 return NULL;
3432 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003433 END_SELECT_LOOP(s)
3434
3435 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 if (n < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003437 return (!async_err) ? s->errorhandler() : NULL;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003438 return PyLong_FromSsize_t(n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00003439}
3440
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003441PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003442"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003443\n\
3444Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003445argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003446sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003447
3448
3449/* s.sendall(data [,flags]) method */
3450
3451static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003452sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003455 Py_ssize_t len, n = -1;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003456 int async_err = 0;
3457 int flags = 0, timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 Py_buffer pbuf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags))
3461 return NULL;
3462 buf = pbuf.buf;
3463 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 if (!IS_SELECTABLE(s)) {
3466 PyBuffer_Release(&pbuf);
3467 return select_error();
3468 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00003469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003470 do {
Victor Stinner391fa712015-03-31 13:15:31 +02003471 timeout = internal_select(s, 1, s->sock_timeout);
Victor Stinnerb7df3142015-03-27 22:59:32 +01003472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003473 n = -1;
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003474 if (!timeout) {
Victor Stinnerb7df3142015-03-27 22:59:32 +01003475 Py_BEGIN_ALLOW_THREADS
Christian Heimesaf01f662013-12-21 16:19:10 +01003476#ifdef MS_WINDOWS
Victor Stinner9a644b22013-06-24 23:47:41 +02003477 if (len > INT_MAX)
3478 len = INT_MAX;
3479 n = send(s->sock_fd, buf, (int)len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003480#else
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003481 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003482#endif
Victor Stinnerb7df3142015-03-27 22:59:32 +01003483 Py_END_ALLOW_THREADS
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003484 }
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003485 if (timeout == 1) {
3486 PyBuffer_Release(&pbuf);
3487 PyErr_SetString(socket_timeout, "timed out");
3488 return NULL;
3489 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003490 if (n >= 0) {
3491 buf += n;
3492 len -= n;
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003493 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003494 } while (len > 0 && (n >= 0 || errno == EINTR) &&
3495 !(async_err = PyErr_CheckSignals()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 PyBuffer_Release(&pbuf);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003497
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003498 if (n < 0 || async_err)
3499 return (!async_err) ? s->errorhandler() : NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003501 Py_INCREF(Py_None);
3502 return Py_None;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003503}
3504
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003505PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003506"sendall(data[, flags])\n\
3507\n\
3508Send a data string to the socket. For the optional flags\n\
3509argument, see the Unix manual. This calls send() repeatedly\n\
3510until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003511to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003512
Guido van Rossum30a685f1991-06-27 15:51:29 +00003513
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00003514/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00003515
Guido van Rossum73624e91994-10-10 17:59:00 +00003516static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003517sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 Py_buffer pbuf;
3520 PyObject *addro;
3521 char *buf;
Antoine Pitrou5e981412011-03-17 22:38:37 +01003522 Py_ssize_t len, arglen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 sock_addr_t addrbuf;
3524 int addrlen, n = -1, flags, timeout;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003525 int async_err = 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003527 flags = 0;
Antoine Pitrou5e981412011-03-17 22:38:37 +01003528 arglen = PyTuple_Size(args);
3529 switch (arglen) {
3530 case 2:
3531 PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro);
3532 break;
3533 case 3:
3534 PyArg_ParseTuple(args, "y*iO:sendto",
3535 &pbuf, &flags, &addro);
3536 break;
3537 default:
3538 PyErr_Format(PyExc_TypeError,
3539 "sendto() takes 2 or 3 arguments (%d given)",
3540 arglen);
Victor Stinner77af1722011-05-26 14:05:59 +02003541 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 }
Antoine Pitrou5e981412011-03-17 22:38:37 +01003543 if (PyErr_Occurred())
3544 return NULL;
3545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546 buf = pbuf.buf;
3547 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003549 if (!IS_SELECTABLE(s)) {
3550 PyBuffer_Release(&pbuf);
3551 return select_error();
3552 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00003553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) {
3555 PyBuffer_Release(&pbuf);
3556 return NULL;
3557 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003558
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003559 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003560 do {
Victor Stinner391fa712015-03-31 13:15:31 +02003561 timeout = internal_select(s, 1, interval);
Victor Stinnerb7df3142015-03-27 22:59:32 +01003562
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003563 if (!timeout) {
Victor Stinnerb7df3142015-03-27 22:59:32 +01003564 Py_BEGIN_ALLOW_THREADS
Victor Stinner14b9b112013-06-25 00:37:25 +02003565#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003566 if (len > INT_MAX)
3567 len = INT_MAX;
3568 n = sendto(s->sock_fd, buf, (int)len, flags,
3569 SAS2SA(&addrbuf), addrlen);
Victor Stinner9a644b22013-06-24 23:47:41 +02003570#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003571 n = sendto(s->sock_fd, buf, len, flags,
3572 SAS2SA(&addrbuf), addrlen);
Victor Stinner9a644b22013-06-24 23:47:41 +02003573#endif
Victor Stinnerb7df3142015-03-27 22:59:32 +01003574 Py_END_ALLOW_THREADS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003575 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003576 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Guido van Rossum67f7a382002-06-06 21:08:16 +00003577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003578 if (timeout == 1) {
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003579 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003580 PyErr_SetString(socket_timeout, "timed out");
3581 return NULL;
3582 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003583 END_SELECT_LOOP(s)
3584 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 if (n < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003586 return (!async_err) ? s->errorhandler() : NULL;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003587 return PyLong_FromSsize_t(n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003588}
3589
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003590PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003591"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003592\n\
3593Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003594For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003595
Guido van Rossum30a685f1991-06-27 15:51:29 +00003596
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003597/* The sendmsg() and recvmsg[_into]() methods require a working
3598 CMSG_LEN(). See the comment near get_CMSG_LEN(). */
3599#ifdef CMSG_LEN
3600/* s.sendmsg(buffers[, ancdata[, flags[, address]]]) method */
3601
3602static PyObject *
3603sock_sendmsg(PySocketSockObject *s, PyObject *args)
3604{
3605 Py_ssize_t i, ndataparts, ndatabufs = 0, ncmsgs, ncmsgbufs = 0;
3606 Py_buffer *databufs = NULL;
3607 struct iovec *iovs = NULL;
3608 sock_addr_t addrbuf;
Charles-François Natalie9e95ae2011-08-24 21:40:53 +02003609 struct msghdr msg = {0};
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003610 struct cmsginfo {
3611 int level;
3612 int type;
3613 Py_buffer data;
3614 } *cmsgs = NULL;
3615 void *controlbuf = NULL;
3616 size_t controllen, controllen_last;
3617 ssize_t bytes_sent = -1;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003618 int async_err = 0;
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003619 int addrlen, timeout, flags = 0;
3620 PyObject *data_arg, *cmsg_arg = NULL, *addr_arg = NULL, *data_fast = NULL,
3621 *cmsg_fast = NULL, *retval = NULL;
3622
3623 if (!PyArg_ParseTuple(args, "O|OiO:sendmsg",
3624 &data_arg, &cmsg_arg, &flags, &addr_arg))
3625 return NULL;
3626
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003627 /* Parse destination address. */
3628 if (addr_arg != NULL && addr_arg != Py_None) {
3629 if (!getsockaddrarg(s, addr_arg, SAS2SA(&addrbuf), &addrlen))
3630 goto finally;
3631 msg.msg_name = &addrbuf;
3632 msg.msg_namelen = addrlen;
3633 }
3634
3635 /* Fill in an iovec for each message part, and save the Py_buffer
3636 structs to release afterwards. */
3637 if ((data_fast = PySequence_Fast(data_arg,
3638 "sendmsg() argument 1 must be an "
3639 "iterable")) == NULL)
3640 goto finally;
3641 ndataparts = PySequence_Fast_GET_SIZE(data_fast);
3642 if (ndataparts > INT_MAX) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003643 PyErr_SetString(PyExc_OSError, "sendmsg() argument 1 is too long");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003644 goto finally;
3645 }
3646 msg.msg_iovlen = ndataparts;
3647 if (ndataparts > 0 &&
3648 ((msg.msg_iov = iovs = PyMem_New(struct iovec, ndataparts)) == NULL ||
3649 (databufs = PyMem_New(Py_buffer, ndataparts)) == NULL)) {
3650 PyErr_NoMemory();
3651 goto finally;
3652 }
3653 for (; ndatabufs < ndataparts; ndatabufs++) {
3654 if (!PyArg_Parse(PySequence_Fast_GET_ITEM(data_fast, ndatabufs),
3655 "y*;sendmsg() argument 1 must be an iterable of "
Serhiy Storchakab757c832014-12-05 22:25:22 +02003656 "bytes-like objects",
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003657 &databufs[ndatabufs]))
3658 goto finally;
3659 iovs[ndatabufs].iov_base = databufs[ndatabufs].buf;
3660 iovs[ndatabufs].iov_len = databufs[ndatabufs].len;
3661 }
3662
3663 if (cmsg_arg == NULL)
3664 ncmsgs = 0;
3665 else {
3666 if ((cmsg_fast = PySequence_Fast(cmsg_arg,
3667 "sendmsg() argument 2 must be an "
3668 "iterable")) == NULL)
3669 goto finally;
3670 ncmsgs = PySequence_Fast_GET_SIZE(cmsg_fast);
3671 }
3672
3673#ifndef CMSG_SPACE
3674 if (ncmsgs > 1) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003675 PyErr_SetString(PyExc_OSError,
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003676 "sending multiple control messages is not supported "
3677 "on this system");
3678 goto finally;
3679 }
3680#endif
3681 /* Save level, type and Py_buffer for each control message,
3682 and calculate total size. */
3683 if (ncmsgs > 0 && (cmsgs = PyMem_New(struct cmsginfo, ncmsgs)) == NULL) {
3684 PyErr_NoMemory();
3685 goto finally;
3686 }
3687 controllen = controllen_last = 0;
3688 while (ncmsgbufs < ncmsgs) {
3689 size_t bufsize, space;
3690
3691 if (!PyArg_Parse(PySequence_Fast_GET_ITEM(cmsg_fast, ncmsgbufs),
3692 "(iiy*):[sendmsg() ancillary data items]",
3693 &cmsgs[ncmsgbufs].level,
3694 &cmsgs[ncmsgbufs].type,
3695 &cmsgs[ncmsgbufs].data))
3696 goto finally;
3697 bufsize = cmsgs[ncmsgbufs++].data.len;
3698
3699#ifdef CMSG_SPACE
3700 if (!get_CMSG_SPACE(bufsize, &space)) {
3701#else
3702 if (!get_CMSG_LEN(bufsize, &space)) {
3703#endif
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003704 PyErr_SetString(PyExc_OSError, "ancillary data item too large");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003705 goto finally;
3706 }
3707 controllen += space;
3708 if (controllen > SOCKLEN_T_LIMIT || controllen < controllen_last) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003709 PyErr_SetString(PyExc_OSError, "too much ancillary data");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003710 goto finally;
3711 }
3712 controllen_last = controllen;
3713 }
3714
3715 /* Construct ancillary data block from control message info. */
3716 if (ncmsgbufs > 0) {
3717 struct cmsghdr *cmsgh = NULL;
3718
3719 if ((msg.msg_control = controlbuf =
3720 PyMem_Malloc(controllen)) == NULL) {
3721 PyErr_NoMemory();
3722 goto finally;
3723 }
3724 msg.msg_controllen = controllen;
3725
3726 /* Need to zero out the buffer as a workaround for glibc's
3727 CMSG_NXTHDR() implementation. After getting the pointer to
3728 the next header, it checks its (uninitialized) cmsg_len
3729 member to see if the "message" fits in the buffer, and
3730 returns NULL if it doesn't. Zero-filling the buffer
Terry Jan Reedy0f847642013-03-11 18:34:00 -04003731 ensures that this doesn't happen. */
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003732 memset(controlbuf, 0, controllen);
3733
3734 for (i = 0; i < ncmsgbufs; i++) {
3735 size_t msg_len, data_len = cmsgs[i].data.len;
3736 int enough_space = 0;
3737
3738 cmsgh = (i == 0) ? CMSG_FIRSTHDR(&msg) : CMSG_NXTHDR(&msg, cmsgh);
3739 if (cmsgh == NULL) {
3740 PyErr_Format(PyExc_RuntimeError,
3741 "unexpected NULL result from %s()",
3742 (i == 0) ? "CMSG_FIRSTHDR" : "CMSG_NXTHDR");
3743 goto finally;
3744 }
3745 if (!get_CMSG_LEN(data_len, &msg_len)) {
3746 PyErr_SetString(PyExc_RuntimeError,
3747 "item size out of range for CMSG_LEN()");
3748 goto finally;
3749 }
3750 if (cmsg_min_space(&msg, cmsgh, msg_len)) {
3751 size_t space;
3752
3753 cmsgh->cmsg_len = msg_len;
3754 if (get_cmsg_data_space(&msg, cmsgh, &space))
3755 enough_space = (space >= data_len);
3756 }
3757 if (!enough_space) {
3758 PyErr_SetString(PyExc_RuntimeError,
3759 "ancillary data does not fit in calculated "
3760 "space");
3761 goto finally;
3762 }
3763 cmsgh->cmsg_level = cmsgs[i].level;
3764 cmsgh->cmsg_type = cmsgs[i].type;
3765 memcpy(CMSG_DATA(cmsgh), cmsgs[i].data.buf, data_len);
3766 }
3767 }
3768
3769 /* Make the system call. */
3770 if (!IS_SELECTABLE(s)) {
3771 select_error();
3772 goto finally;
3773 }
3774
3775 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003776 do {
Victor Stinner391fa712015-03-31 13:15:31 +02003777 timeout = internal_select(s, 1, interval);
Victor Stinnerb7df3142015-03-27 22:59:32 +01003778
3779 if (!timeout) {
3780 Py_BEGIN_ALLOW_THREADS;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003781 bytes_sent = sendmsg(s->sock_fd, &msg, flags);
Victor Stinnerb7df3142015-03-27 22:59:32 +01003782 Py_END_ALLOW_THREADS;
3783 }
3784
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003785 if (timeout == 1) {
3786 PyErr_SetString(socket_timeout, "timed out");
3787 goto finally;
3788 }
3789 } while (bytes_sent < 0 && errno == EINTR &&
3790 !(async_err = PyErr_CheckSignals()));
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003791 END_SELECT_LOOP(s)
3792
3793 if (bytes_sent < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003794 if (!async_err)
3795 s->errorhandler();
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003796 goto finally;
3797 }
3798 retval = PyLong_FromSsize_t(bytes_sent);
3799
3800finally:
3801 PyMem_Free(controlbuf);
3802 for (i = 0; i < ncmsgbufs; i++)
3803 PyBuffer_Release(&cmsgs[i].data);
3804 PyMem_Free(cmsgs);
3805 Py_XDECREF(cmsg_fast);
3806 for (i = 0; i < ndatabufs; i++)
3807 PyBuffer_Release(&databufs[i]);
3808 PyMem_Free(databufs);
3809 PyMem_Free(iovs);
3810 Py_XDECREF(data_fast);
3811 return retval;
3812}
3813
3814PyDoc_STRVAR(sendmsg_doc,
3815"sendmsg(buffers[, ancdata[, flags[, address]]]) -> count\n\
3816\n\
3817Send normal and ancillary data to the socket, gathering the\n\
3818non-ancillary data from a series of buffers and concatenating it into\n\
3819a single message. The buffers argument specifies the non-ancillary\n\
Serhiy Storchakab757c832014-12-05 22:25:22 +02003820data as an iterable of bytes-like objects (e.g. bytes objects).\n\
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003821The ancdata argument specifies the ancillary data (control messages)\n\
3822as an iterable of zero or more tuples (cmsg_level, cmsg_type,\n\
3823cmsg_data), where cmsg_level and cmsg_type are integers specifying the\n\
3824protocol level and protocol-specific type respectively, and cmsg_data\n\
Serhiy Storchakab757c832014-12-05 22:25:22 +02003825is a bytes-like object holding the associated data. The flags\n\
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003826argument defaults to 0 and has the same meaning as for send(). If\n\
3827address is supplied and not None, it sets a destination address for\n\
3828the message. The return value is the number of bytes of non-ancillary\n\
3829data sent.");
3830#endif /* CMSG_LEN */
3831
3832
Guido van Rossum30a685f1991-06-27 15:51:29 +00003833/* s.shutdown(how) method */
3834
Guido van Rossum73624e91994-10-10 17:59:00 +00003835static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003836sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003837{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 int how;
3839 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00003840
Serhiy Storchaka78980432013-01-15 01:12:17 +02003841 how = _PyLong_AsInt(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003842 if (how == -1 && PyErr_Occurred())
3843 return NULL;
3844 Py_BEGIN_ALLOW_THREADS
3845 res = shutdown(s->sock_fd, how);
3846 Py_END_ALLOW_THREADS
3847 if (res < 0)
3848 return s->errorhandler();
3849 Py_INCREF(Py_None);
3850 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003851}
3852
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003853PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003854"shutdown(flag)\n\
3855\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00003856Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
3857of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003858
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00003859#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Christian Heimesfaf2f632008-01-06 16:59:19 +00003860static PyObject*
3861sock_ioctl(PySocketSockObject *s, PyObject *arg)
3862{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003863 unsigned long cmd = SIO_RCVALL;
3864 PyObject *argO;
3865 DWORD recv;
Christian Heimesfaf2f632008-01-06 16:59:19 +00003866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003867 if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO))
3868 return NULL;
Christian Heimesfaf2f632008-01-06 16:59:19 +00003869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003870 switch (cmd) {
3871 case SIO_RCVALL: {
3872 unsigned int option = RCVALL_ON;
3873 if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option))
3874 return NULL;
3875 if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option),
3876 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
3877 return set_error();
3878 }
3879 return PyLong_FromUnsignedLong(recv); }
3880 case SIO_KEEPALIVE_VALS: {
3881 struct tcp_keepalive ka;
3882 if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd,
3883 &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval))
3884 return NULL;
3885 if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka),
3886 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
3887 return set_error();
3888 }
3889 return PyLong_FromUnsignedLong(recv); }
3890 default:
3891 PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd);
3892 return NULL;
3893 }
Christian Heimesfaf2f632008-01-06 16:59:19 +00003894}
3895PyDoc_STRVAR(sock_ioctl_doc,
3896"ioctl(cmd, option) -> long\n\
3897\n\
Kristján Valur Jónsson847ec752009-09-27 21:10:38 +00003898Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\
3899SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\
3900SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval).");
Kristján Valur Jónsson10f383a2012-04-07 11:23:31 +00003901#endif
3902
3903#if defined(MS_WINDOWS)
3904static PyObject*
3905sock_share(PySocketSockObject *s, PyObject *arg)
3906{
3907 WSAPROTOCOL_INFO info;
3908 DWORD processId;
3909 int result;
3910
3911 if (!PyArg_ParseTuple(arg, "I", &processId))
3912 return NULL;
3913
3914 Py_BEGIN_ALLOW_THREADS
3915 result = WSADuplicateSocket(s->sock_fd, processId, &info);
3916 Py_END_ALLOW_THREADS
3917 if (result == SOCKET_ERROR)
3918 return set_error();
3919 return PyBytes_FromStringAndSize((const char*)&info, sizeof(info));
3920}
3921PyDoc_STRVAR(sock_share_doc,
3922"share(process_id) -> bytes\n\
3923\n\
3924Share the socket with another process. The target process id\n\
3925must be provided and the resulting bytes object passed to the target\n\
3926process. There the shared socket can be instantiated by calling\n\
3927socket.fromshare().");
3928
Christian Heimesfaf2f632008-01-06 16:59:19 +00003929
3930#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00003931
3932/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003933
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003934static PyMethodDef sock_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003935 {"_accept", (PyCFunction)sock_accept, METH_NOARGS,
3936 accept_doc},
3937 {"bind", (PyCFunction)sock_bind, METH_O,
3938 bind_doc},
3939 {"close", (PyCFunction)sock_close, METH_NOARGS,
3940 close_doc},
3941 {"connect", (PyCFunction)sock_connect, METH_O,
3942 connect_doc},
3943 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
3944 connect_ex_doc},
Antoine Pitrou6e451df2010-08-09 20:39:54 +00003945 {"detach", (PyCFunction)sock_detach, METH_NOARGS,
3946 detach_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003947 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
3948 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003949#ifdef HAVE_GETPEERNAME
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003950 {"getpeername", (PyCFunction)sock_getpeername,
3951 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00003952#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003953 {"getsockname", (PyCFunction)sock_getsockname,
3954 METH_NOARGS, getsockname_doc},
3955 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
3956 getsockopt_doc},
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00003957#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003958 {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS,
3959 sock_ioctl_doc},
Christian Heimesfaf2f632008-01-06 16:59:19 +00003960#endif
Kristján Valur Jónsson10f383a2012-04-07 11:23:31 +00003961#if defined(MS_WINDOWS)
3962 {"share", (PyCFunction)sock_share, METH_VARARGS,
3963 sock_share_doc},
3964#endif
Charles-François Natali644b8f52014-05-22 19:45:39 +01003965 {"listen", (PyCFunction)sock_listen, METH_VARARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003966 listen_doc},
3967 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
3968 recv_doc},
3969 {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
3970 recv_into_doc},
3971 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
3972 recvfrom_doc},
3973 {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
3974 recvfrom_into_doc},
3975 {"send", (PyCFunction)sock_send, METH_VARARGS,
3976 send_doc},
3977 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
3978 sendall_doc},
3979 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
3980 sendto_doc},
3981 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
3982 setblocking_doc},
3983 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
3984 settimeout_doc},
3985 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
3986 gettimeout_doc},
3987 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
3988 setsockopt_doc},
3989 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
3990 shutdown_doc},
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003991#ifdef CMSG_LEN
3992 {"recvmsg", (PyCFunction)sock_recvmsg, METH_VARARGS,
3993 recvmsg_doc},
3994 {"recvmsg_into", (PyCFunction)sock_recvmsg_into, METH_VARARGS,
3995 recvmsg_into_doc,},
3996 {"sendmsg", (PyCFunction)sock_sendmsg, METH_VARARGS,
3997 sendmsg_doc},
3998#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003999 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004000};
4001
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004002/* SockObject members */
4003static PyMemberDef sock_memberlist[] = {
4004 {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},
4005 {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},
4006 {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004007 {0},
4008};
Guido van Rossum30a685f1991-06-27 15:51:29 +00004009
Victor Stinner71694d52015-03-28 01:18:54 +01004010static PyGetSetDef sock_getsetlist[] = {
4011 {"timeout", (getter)sock_gettimeout, NULL, PyDoc_STR("the socket timeout")},
4012 {NULL} /* sentinel */
4013};
4014
Guido van Rossum73624e91994-10-10 17:59:00 +00004015/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00004016 First close the file description. */
4017
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004018static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004019sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004020{
Antoine Pitroue033e062010-10-29 10:38:18 +00004021 if (s->sock_fd != -1) {
4022 PyObject *exc, *val, *tb;
4023 Py_ssize_t old_refcount = Py_REFCNT(s);
4024 ++Py_REFCNT(s);
4025 PyErr_Fetch(&exc, &val, &tb);
4026 if (PyErr_WarnFormat(PyExc_ResourceWarning, 1,
4027 "unclosed %R", s))
4028 /* Spurious errors can appear at shutdown */
4029 if (PyErr_ExceptionMatches(PyExc_Warning))
4030 PyErr_WriteUnraisable((PyObject *) s);
4031 PyErr_Restore(exc, val, tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004032 (void) SOCKETCLOSE(s->sock_fd);
Antoine Pitroue033e062010-10-29 10:38:18 +00004033 Py_REFCNT(s) = old_refcount;
4034 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004035 Py_TYPE(s)->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004036}
4037
Guido van Rossum30a685f1991-06-27 15:51:29 +00004038
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004039static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004040sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004041{
Victor Stinnere254e532014-07-26 14:36:55 +02004042 long sock_fd;
4043 /* On Windows, this test is needed because SOCKET_T is unsigned */
4044 if (s->sock_fd == INVALID_SOCKET) {
4045 sock_fd = -1;
4046 }
Fred Drakea04eaad2000-06-30 02:46:07 +00004047#if SIZEOF_SOCKET_T > SIZEOF_LONG
Victor Stinnere254e532014-07-26 14:36:55 +02004048 else if (s->sock_fd > LONG_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004049 /* this can occur on Win64, and actually there is a special
4050 ugly printf formatter for decimal pointer length integer
4051 printing, only bother if necessary*/
4052 PyErr_SetString(PyExc_OverflowError,
4053 "no printf formatter to display "
4054 "the socket descriptor in decimal");
4055 return NULL;
4056 }
Fred Drakea04eaad2000-06-30 02:46:07 +00004057#endif
Victor Stinnere254e532014-07-26 14:36:55 +02004058 else
4059 sock_fd = (long)s->sock_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 return PyUnicode_FromFormat(
4061 "<socket object, fd=%ld, family=%d, type=%d, proto=%d>",
Victor Stinnere254e532014-07-26 14:36:55 +02004062 sock_fd, s->sock_family,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 s->sock_type,
4064 s->sock_proto);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004065}
4066
4067
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004068/* Create a new, uninitialized socket object. */
4069
4070static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004071sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004072{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 PyObject *new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 new = type->tp_alloc(type, 0);
4076 if (new != NULL) {
4077 ((PySocketSockObject *)new)->sock_fd = -1;
Victor Stinner71694d52015-03-28 01:18:54 +01004078 ((PySocketSockObject *)new)->sock_timeout = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004079 ((PySocketSockObject *)new)->errorhandler = &set_error;
4080 }
4081 return new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004082}
4083
4084
4085/* Initialize a new socket object. */
4086
Victor Stinnerdaf45552013-08-28 00:53:59 +02004087#ifdef SOCK_CLOEXEC
4088/* socket() and socketpair() fail with EINVAL on Linux kernel older
4089 * than 2.6.27 if SOCK_CLOEXEC flag is set in the socket type. */
4090static int sock_cloexec_works = -1;
4091#endif
4092
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004093/*ARGSUSED*/
4094static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00004095sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004097 PySocketSockObject *s = (PySocketSockObject *)self;
4098 PyObject *fdobj = NULL;
4099 SOCKET_T fd = INVALID_SOCKET;
4100 int family = AF_INET, type = SOCK_STREAM, proto = 0;
4101 static char *keywords[] = {"family", "type", "proto", "fileno", 0};
Victor Stinnerdaf45552013-08-28 00:53:59 +02004102#ifndef MS_WINDOWS
4103#ifdef SOCK_CLOEXEC
4104 int *atomic_flag_works = &sock_cloexec_works;
4105#else
4106 int *atomic_flag_works = NULL;
4107#endif
4108#endif
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 if (!PyArg_ParseTupleAndKeywords(args, kwds,
4111 "|iiiO:socket", keywords,
4112 &family, &type, &proto, &fdobj))
4113 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004115 if (fdobj != NULL && fdobj != Py_None) {
Kristján Valur Jónsson10f383a2012-04-07 11:23:31 +00004116#ifdef MS_WINDOWS
4117 /* recreate a socket that was duplicated */
4118 if (PyBytes_Check(fdobj)) {
4119 WSAPROTOCOL_INFO info;
4120 if (PyBytes_GET_SIZE(fdobj) != sizeof(info)) {
4121 PyErr_Format(PyExc_ValueError,
4122 "socket descriptor string has wrong size, "
4123 "should be %zu bytes.", sizeof(info));
4124 return -1;
4125 }
4126 memcpy(&info, PyBytes_AS_STRING(fdobj), sizeof(info));
4127 Py_BEGIN_ALLOW_THREADS
4128 fd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
4129 FROM_PROTOCOL_INFO, &info, 0, WSA_FLAG_OVERLAPPED);
4130 Py_END_ALLOW_THREADS
4131 if (fd == INVALID_SOCKET) {
4132 set_error();
4133 return -1;
4134 }
4135 family = info.iAddressFamily;
4136 type = info.iSocketType;
4137 proto = info.iProtocol;
4138 }
4139 else
4140#endif
4141 {
4142 fd = PyLong_AsSocket_t(fdobj);
4143 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
4144 return -1;
4145 if (fd == INVALID_SOCKET) {
4146 PyErr_SetString(PyExc_ValueError,
4147 "can't use invalid socket value");
4148 return -1;
4149 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004150 }
4151 }
4152 else {
Victor Stinnerdaf45552013-08-28 00:53:59 +02004153#ifdef MS_WINDOWS
4154 /* Windows implementation */
4155#ifndef WSA_FLAG_NO_HANDLE_INHERIT
4156#define WSA_FLAG_NO_HANDLE_INHERIT 0x80
4157#endif
4158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004159 Py_BEGIN_ALLOW_THREADS
Victor Stinnerdaf45552013-08-28 00:53:59 +02004160 if (support_wsa_no_inherit) {
4161 fd = WSASocket(family, type, proto,
4162 NULL, 0,
4163 WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT);
4164 if (fd == INVALID_SOCKET) {
4165 /* Windows 7 or Windows 2008 R2 without SP1 or the hotfix */
4166 support_wsa_no_inherit = 0;
4167 fd = socket(family, type, proto);
4168 }
4169 }
4170 else {
4171 fd = socket(family, type, proto);
4172 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004173 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00004174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004175 if (fd == INVALID_SOCKET) {
4176 set_error();
4177 return -1;
4178 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02004179
4180 if (!support_wsa_no_inherit) {
4181 if (!SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0)) {
4182 closesocket(fd);
4183 PyErr_SetFromWindowsErr(0);
4184 return -1;
4185 }
4186 }
4187#else
4188 /* UNIX */
4189 Py_BEGIN_ALLOW_THREADS
4190#ifdef SOCK_CLOEXEC
4191 if (sock_cloexec_works != 0) {
4192 fd = socket(family, type | SOCK_CLOEXEC, proto);
4193 if (sock_cloexec_works == -1) {
4194 if (fd >= 0) {
4195 sock_cloexec_works = 1;
4196 }
4197 else if (errno == EINVAL) {
4198 /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */
4199 sock_cloexec_works = 0;
4200 fd = socket(family, type, proto);
4201 }
4202 }
4203 }
4204 else
4205#endif
4206 {
4207 fd = socket(family, type, proto);
4208 }
4209 Py_END_ALLOW_THREADS
4210
4211 if (fd == INVALID_SOCKET) {
4212 set_error();
4213 return -1;
4214 }
4215
4216 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
4217 SOCKETCLOSE(fd);
4218 return -1;
4219 }
4220#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004221 }
4222 init_sockobject(s, fd, family, type, proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00004223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004224 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004225
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004226}
4227
4228
Guido van Rossumb6775db1994-08-01 11:34:53 +00004229/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00004230
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004231static PyTypeObject sock_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004232 PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
4233 "_socket.socket", /* tp_name */
4234 sizeof(PySocketSockObject), /* tp_basicsize */
4235 0, /* tp_itemsize */
4236 (destructor)sock_dealloc, /* tp_dealloc */
4237 0, /* tp_print */
4238 0, /* tp_getattr */
4239 0, /* tp_setattr */
4240 0, /* tp_reserved */
4241 (reprfunc)sock_repr, /* tp_repr */
4242 0, /* tp_as_number */
4243 0, /* tp_as_sequence */
4244 0, /* tp_as_mapping */
4245 0, /* tp_hash */
4246 0, /* tp_call */
4247 0, /* tp_str */
4248 PyObject_GenericGetAttr, /* tp_getattro */
4249 0, /* tp_setattro */
4250 0, /* tp_as_buffer */
4251 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4252 sock_doc, /* tp_doc */
4253 0, /* tp_traverse */
4254 0, /* tp_clear */
4255 0, /* tp_richcompare */
4256 0, /* tp_weaklistoffset */
4257 0, /* tp_iter */
4258 0, /* tp_iternext */
4259 sock_methods, /* tp_methods */
4260 sock_memberlist, /* tp_members */
Victor Stinner71694d52015-03-28 01:18:54 +01004261 sock_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004262 0, /* tp_base */
4263 0, /* tp_dict */
4264 0, /* tp_descr_get */
4265 0, /* tp_descr_set */
4266 0, /* tp_dictoffset */
4267 sock_initobj, /* tp_init */
4268 PyType_GenericAlloc, /* tp_alloc */
4269 sock_new, /* tp_new */
4270 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004271};
4272
Guido van Rossum30a685f1991-06-27 15:51:29 +00004273
Guido van Rossum81194471991-07-27 21:42:02 +00004274/* Python interface to gethostname(). */
4275
4276/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00004277static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004278socket_gethostname(PyObject *self, PyObject *unused)
Guido van Rossum81194471991-07-27 21:42:02 +00004279{
Martin v. Löwis72f48422010-10-29 18:20:08 +00004280#ifdef MS_WINDOWS
4281 /* Don't use winsock's gethostname, as this returns the ANSI
4282 version of the hostname, whereas we need a Unicode string.
4283 Otherwise, gethostname apparently also returns the DNS name. */
Hirokazu Yamamoto09fff7a2010-11-05 17:24:13 +00004284 wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1];
Victor Stinner63941882011-09-29 00:42:28 +02004285 DWORD size = Py_ARRAY_LENGTH(buf);
Victor Stinner74168972011-11-17 01:11:36 +01004286 wchar_t *name;
Hirokazu Yamamoto09fff7a2010-11-05 17:24:13 +00004287 PyObject *result;
Victor Stinner74168972011-11-17 01:11:36 +01004288
4289 if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size))
Victor Stinner9d3b93b2011-11-22 02:27:30 +01004290 return PyUnicode_FromWideChar(buf, size);
Victor Stinner74168972011-11-17 01:11:36 +01004291
4292 if (GetLastError() != ERROR_MORE_DATA)
4293 return PyErr_SetFromWindowsErr(0);
4294
4295 if (size == 0)
4296 return PyUnicode_New(0, 0);
4297
4298 /* MSDN says ERROR_MORE_DATA may occur because DNS allows longer
4299 names */
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02004300 name = PyMem_New(wchar_t, size);
4301 if (!name) {
4302 PyErr_NoMemory();
Victor Stinner74168972011-11-17 01:11:36 +01004303 return NULL;
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02004304 }
Victor Stinner74168972011-11-17 01:11:36 +01004305 if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname,
4306 name,
4307 &size))
4308 {
4309 PyMem_Free(name);
4310 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis72f48422010-10-29 18:20:08 +00004311 }
Victor Stinner74168972011-11-17 01:11:36 +01004312
4313 result = PyUnicode_FromWideChar(name, size);
4314 PyMem_Free(name);
4315 return result;
Martin v. Löwis72f48422010-10-29 18:20:08 +00004316#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 char buf[1024];
4318 int res;
4319 Py_BEGIN_ALLOW_THREADS
4320 res = gethostname(buf, (int) sizeof buf - 1);
4321 Py_END_ALLOW_THREADS
4322 if (res < 0)
4323 return set_error();
4324 buf[sizeof buf - 1] = '\0';
Victor Stinnera534fc42013-06-03 22:07:27 +02004325 return PyUnicode_DecodeFSDefault(buf);
Martin v. Löwis72f48422010-10-29 18:20:08 +00004326#endif
Guido van Rossum81194471991-07-27 21:42:02 +00004327}
Guido van Rossumff4949e1992-08-05 19:58:53 +00004328
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004329PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004330"gethostname() -> string\n\
4331\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004332Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004333
Antoine Pitrou061cfb52011-02-28 22:25:22 +00004334#ifdef HAVE_SETHOSTNAME
4335PyDoc_STRVAR(sethostname_doc,
4336"sethostname(name)\n\n\
4337Sets the hostname to name.");
4338
4339static PyObject *
4340socket_sethostname(PyObject *self, PyObject *args)
4341{
4342 PyObject *hnobj;
4343 Py_buffer buf;
4344 int res, flag = 0;
4345
Christian Heimesd2774c72013-06-19 02:06:29 +02004346#ifdef _AIX
4347/* issue #18259, not declared in any useful header file */
4348extern int sethostname(const char *, size_t);
4349#endif
4350
Antoine Pitrou061cfb52011-02-28 22:25:22 +00004351 if (!PyArg_ParseTuple(args, "S:sethostname", &hnobj)) {
4352 PyErr_Clear();
4353 if (!PyArg_ParseTuple(args, "O&:sethostname",
4354 PyUnicode_FSConverter, &hnobj))
4355 return NULL;
4356 flag = 1;
4357 }
4358 res = PyObject_GetBuffer(hnobj, &buf, PyBUF_SIMPLE);
4359 if (!res) {
4360 res = sethostname(buf.buf, buf.len);
4361 PyBuffer_Release(&buf);
4362 }
4363 if (flag)
4364 Py_DECREF(hnobj);
4365 if (res)
4366 return set_error();
4367 Py_RETURN_NONE;
4368}
4369#endif
Guido van Rossumff4949e1992-08-05 19:58:53 +00004370
Guido van Rossum30a685f1991-06-27 15:51:29 +00004371/* Python interface to gethostbyname(name). */
4372
4373/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00004374static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004375socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00004376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 char *name;
4378 sock_addr_t addrbuf;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004379 PyObject *ret = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004380
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004381 if (!PyArg_ParseTuple(args, "et:gethostbyname", "idna", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004382 return NULL;
4383 if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004384 goto finally;
4385 ret = makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
4386finally:
4387 PyMem_Free(name);
4388 return ret;
Guido van Rossum30a685f1991-06-27 15:51:29 +00004389}
4390
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004391PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004392"gethostbyname(host) -> address\n\
4393\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004394Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004395
4396
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004397/* Convenience function common to gethostbyname_ex and gethostbyaddr */
4398
4399static PyObject *
Victor Stinner1a62a682014-08-17 19:33:28 +02004400gethost_common(struct hostent *h, struct sockaddr *addr, size_t alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004402 char **pch;
4403 PyObject *rtn_tuple = (PyObject *)NULL;
4404 PyObject *name_list = (PyObject *)NULL;
4405 PyObject *addr_list = (PyObject *)NULL;
4406 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 if (h == NULL) {
4409 /* Let's get real error message to return */
4410 set_herror(h_errno);
4411 return NULL;
4412 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 if (h->h_addrtype != af) {
4415 /* Let's get real error message to return */
Victor Stinner1f33f2b2011-12-17 04:45:09 +01004416 errno = EAFNOSUPPORT;
4417 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004418 return NULL;
4419 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004421 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00004422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004423 case AF_INET:
4424 if (alen < sizeof(struct sockaddr_in))
4425 return NULL;
4426 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004427
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00004428#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 case AF_INET6:
4430 if (alen < sizeof(struct sockaddr_in6))
4431 return NULL;
4432 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004433#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00004434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004435 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004437 if ((name_list = PyList_New(0)) == NULL)
4438 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 if ((addr_list = PyList_New(0)) == NULL)
4441 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004443 /* SF #1511317: h_aliases can be NULL */
4444 if (h->h_aliases) {
4445 for (pch = h->h_aliases; *pch != NULL; pch++) {
4446 int status;
4447 tmp = PyUnicode_FromString(*pch);
4448 if (tmp == NULL)
4449 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004451 status = PyList_Append(name_list, tmp);
4452 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00004453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 if (status)
4455 goto err;
4456 }
4457 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004459 for (pch = h->h_addr_list; *pch != NULL; pch++) {
4460 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00004463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004464 case AF_INET:
4465 {
4466 struct sockaddr_in sin;
4467 memset(&sin, 0, sizeof(sin));
4468 sin.sin_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004469#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004470 sin.sin_len = sizeof(sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004471#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
4473 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00004474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 if (pch == h->h_addr_list && alen >= sizeof(sin))
4476 memcpy((char *) addr, &sin, sizeof(sin));
4477 break;
4478 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004479
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00004480#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 case AF_INET6:
4482 {
4483 struct sockaddr_in6 sin6;
4484 memset(&sin6, 0, sizeof(sin6));
4485 sin6.sin6_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004486#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 sin6.sin6_len = sizeof(sin6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004488#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
4490 tmp = makeipaddr((struct sockaddr *)&sin6,
4491 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00004492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004493 if (pch == h->h_addr_list && alen >= sizeof(sin6))
4494 memcpy((char *) addr, &sin6, sizeof(sin6));
4495 break;
4496 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004497#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00004498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004499 default: /* can't happen */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004500 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 "unsupported address family");
4502 return NULL;
4503 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 if (tmp == NULL)
4506 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508 status = PyList_Append(addr_list, tmp);
4509 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00004510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 if (status)
4512 goto err;
4513 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004515 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00004516
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004517 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004518 Py_XDECREF(name_list);
4519 Py_XDECREF(addr_list);
4520 return rtn_tuple;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004521}
4522
4523
4524/* Python interface to gethostbyname_ex(name). */
4525
4526/*ARGSUSED*/
4527static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004528socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 char *name;
4531 struct hostent *h;
Charles-François Natali8b759652011-12-23 16:44:51 +01004532 sock_addr_t addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 struct sockaddr *sa;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004534 PyObject *ret = NULL;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004535#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004537#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004539#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004540 char buf[16384];
4541 int buf_len = (sizeof buf) - 1;
4542 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004543#endif
Ross Lagerwall8c159762012-03-06 21:36:18 +02004544#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004546#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004547#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004548
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004549 if (!PyArg_ParseTuple(args, "et:gethostbyname_ex", "idna", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004550 return NULL;
Charles-François Natali8b759652011-12-23 16:44:51 +01004551 if (setipaddr(name, SAS2SA(&addr), sizeof(addr), AF_INET) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004552 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 Py_BEGIN_ALLOW_THREADS
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004554#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004555#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Ross Lagerwall8c159762012-03-06 21:36:18 +02004556 gethostbyname_r(name, &hp_allocated, buf, buf_len,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004558#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004560#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004561 memset((void *) &data, '\0', sizeof(data));
4562 result = gethostbyname_r(name, &hp_allocated, &data);
4563 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004564#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004565#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00004566#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004567 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004568#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004569 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004570#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004571 Py_END_ALLOW_THREADS
4572 /* Some C libraries would require addr.__ss_family instead of
4573 addr.ss_family.
4574 Therefore, we cast the sockaddr_storage into sockaddr to
4575 access sa_family. */
Charles-François Natali8b759652011-12-23 16:44:51 +01004576 sa = SAS2SA(&addr);
4577 ret = gethost_common(h, SAS2SA(&addr), sizeof(addr),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004578 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00004579#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004580 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00004581#endif
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004582finally:
4583 PyMem_Free(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004584 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004585}
4586
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004587PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004588"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
4589\n\
4590Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004591for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004592
4593
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004594/* Python interface to gethostbyaddr(IP). */
4595
4596/*ARGSUSED*/
4597static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004598socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004599{
Charles-François Natali8b759652011-12-23 16:44:51 +01004600 sock_addr_t addr;
4601 struct sockaddr *sa = SAS2SA(&addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004602 char *ip_num;
4603 struct hostent *h;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004604 PyObject *ret = NULL;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004605#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004607#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004608 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004609#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004610 /* glibcs up to 2.10 assume that the buf argument to
4611 gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc
4612 does not ensure. The attribute below instructs the compiler
4613 to maintain this alignment. */
4614 char buf[16384] Py_ALIGNED(8);
4615 int buf_len = (sizeof buf) - 1;
4616 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004617#endif
Ross Lagerwall8c159762012-03-06 21:36:18 +02004618#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004619 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004620#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004621#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622 char *ap;
4623 int al;
4624 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004625
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004626 if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004627 return NULL;
4628 af = AF_UNSPEC;
4629 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004630 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004631 af = sa->sa_family;
4632 ap = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00004633 /* al = 0; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004634 switch (af) {
4635 case AF_INET:
4636 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
4637 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
4638 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00004639#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004640 case AF_INET6:
4641 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
4642 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
4643 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004644#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004645 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004646 PyErr_SetString(PyExc_OSError, "unsupported address family");
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004647 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004648 }
4649 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004650#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004651#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Ross Lagerwall8c159762012-03-06 21:36:18 +02004652 gethostbyaddr_r(ap, al, af,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004653 &hp_allocated, buf, buf_len,
4654 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004655#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004656 h = gethostbyaddr_r(ap, al, af,
4657 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004658#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004659 memset((void *) &data, '\0', sizeof(data));
4660 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
4661 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004662#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004663#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00004664#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004665 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004666#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004667 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004668#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 Py_END_ALLOW_THREADS
Charles-François Natali8b759652011-12-23 16:44:51 +01004670 ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00004671#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00004673#endif
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004674finally:
4675 PyMem_Free(ip_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004676 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004677}
4678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004679PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004680"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
4681\n\
4682Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004683for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004684
Guido van Rossum30a685f1991-06-27 15:51:29 +00004685
4686/* Python interface to getservbyname(name).
4687 This only returns the port number, since the other info is already
4688 known or not useful (like the list of aliases). */
4689
4690/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00004691static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004692socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00004693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 char *name, *proto=NULL;
4695 struct servent *sp;
4696 if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
4697 return NULL;
4698 Py_BEGIN_ALLOW_THREADS
4699 sp = getservbyname(name, proto);
4700 Py_END_ALLOW_THREADS
4701 if (sp == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004702 PyErr_SetString(PyExc_OSError, "service/proto not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004703 return NULL;
4704 }
4705 return PyLong_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00004706}
4707
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004708PyDoc_STRVAR(getservbyname_doc,
Barry Warsaw11b91a02004-06-28 00:50:43 +00004709"getservbyname(servicename[, protocolname]) -> integer\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00004710\n\
4711Return a port number from a service name and protocol name.\n\
Barry Warsaw11b91a02004-06-28 00:50:43 +00004712The optional protocol name, if given, should be 'tcp' or 'udp',\n\
4713otherwise any protocol will match.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004714
Guido van Rossum30a685f1991-06-27 15:51:29 +00004715
Barry Warsaw11b91a02004-06-28 00:50:43 +00004716/* Python interface to getservbyport(port).
4717 This only returns the service name, since the other info is already
4718 known or not useful (like the list of aliases). */
4719
4720/*ARGSUSED*/
4721static PyObject *
4722socket_getservbyport(PyObject *self, PyObject *args)
4723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004724 int port;
4725 char *proto=NULL;
4726 struct servent *sp;
4727 if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto))
4728 return NULL;
4729 if (port < 0 || port > 0xffff) {
4730 PyErr_SetString(
4731 PyExc_OverflowError,
4732 "getservbyport: port must be 0-65535.");
4733 return NULL;
4734 }
4735 Py_BEGIN_ALLOW_THREADS
4736 sp = getservbyport(htons((short)port), proto);
4737 Py_END_ALLOW_THREADS
4738 if (sp == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004739 PyErr_SetString(PyExc_OSError, "port/proto not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004740 return NULL;
4741 }
4742 return PyUnicode_FromString(sp->s_name);
Barry Warsaw11b91a02004-06-28 00:50:43 +00004743}
4744
4745PyDoc_STRVAR(getservbyport_doc,
4746"getservbyport(port[, protocolname]) -> string\n\
4747\n\
4748Return the service name from a port number and protocol name.\n\
4749The optional protocol name, if given, should be 'tcp' or 'udp',\n\
4750otherwise any protocol will match.");
4751
Guido van Rossum3901d851996-12-19 16:35:04 +00004752/* Python interface to getprotobyname(name).
4753 This only returns the protocol number, since the other info is
4754 already known or not useful (like the list of aliases). */
4755
4756/*ARGSUSED*/
4757static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004758socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00004759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004760 char *name;
4761 struct protoent *sp;
4762 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
4763 return NULL;
4764 Py_BEGIN_ALLOW_THREADS
4765 sp = getprotobyname(name);
4766 Py_END_ALLOW_THREADS
4767 if (sp == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004768 PyErr_SetString(PyExc_OSError, "protocol not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004769 return NULL;
4770 }
4771 return PyLong_FromLong((long) sp->p_proto);
Guido van Rossum3901d851996-12-19 16:35:04 +00004772}
4773
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004774PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004775"getprotobyname(name) -> integer\n\
4776\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004777Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004778
Guido van Rossum3901d851996-12-19 16:35:04 +00004779
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004780#ifndef NO_DUP
4781/* dup() function for socket fds */
4782
4783static PyObject *
4784socket_dup(PyObject *self, PyObject *fdobj)
4785{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004786 SOCKET_T fd, newfd;
4787 PyObject *newfdobj;
Victor Stinnerdaf45552013-08-28 00:53:59 +02004788#ifdef MS_WINDOWS
4789 WSAPROTOCOL_INFO info;
4790#endif
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004792 fd = PyLong_AsSocket_t(fdobj);
4793 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
4794 return NULL;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004795
Victor Stinnerdaf45552013-08-28 00:53:59 +02004796#ifdef MS_WINDOWS
4797 if (WSADuplicateSocket(fd, GetCurrentProcessId(), &info))
4798 return set_error();
4799
4800 newfd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
4801 FROM_PROTOCOL_INFO,
4802 &info, 0, WSA_FLAG_OVERLAPPED);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004803 if (newfd == INVALID_SOCKET)
4804 return set_error();
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004805
Victor Stinnerdaf45552013-08-28 00:53:59 +02004806 if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) {
4807 closesocket(newfd);
4808 PyErr_SetFromWindowsErr(0);
4809 return NULL;
4810 }
4811#else
4812 /* On UNIX, dup can be used to duplicate the file descriptor of a socket */
4813 newfd = _Py_dup(fd);
4814 if (newfd == INVALID_SOCKET)
Victor Stinnere990c6e2013-11-16 00:18:58 +01004815 return NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02004816#endif
4817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004818 newfdobj = PyLong_FromSocket_t(newfd);
4819 if (newfdobj == NULL)
4820 SOCKETCLOSE(newfd);
4821 return newfdobj;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004822}
4823
4824PyDoc_STRVAR(dup_doc,
4825"dup(integer) -> integer\n\
4826\n\
4827Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\
4828sockets; on some platforms os.dup() won't work for socket file descriptors.");
4829#endif
4830
4831
Dave Cole331708b2004-08-09 04:51:41 +00004832#ifdef HAVE_SOCKETPAIR
4833/* Create a pair of sockets using the socketpair() function.
Dave Cole07fda7e2004-08-23 05:16:23 +00004834 Arguments as for socket() except the default family is AF_UNIX if
Dave Colee8bbfe42004-08-26 00:51:16 +00004835 defined on the platform; otherwise, the default is AF_INET. */
Dave Cole331708b2004-08-09 04:51:41 +00004836
4837/*ARGSUSED*/
4838static PyObject *
4839socket_socketpair(PyObject *self, PyObject *args)
4840{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004841 PySocketSockObject *s0 = NULL, *s1 = NULL;
4842 SOCKET_T sv[2];
4843 int family, type = SOCK_STREAM, proto = 0;
4844 PyObject *res = NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02004845#ifdef SOCK_CLOEXEC
4846 int *atomic_flag_works = &sock_cloexec_works;
4847#else
4848 int *atomic_flag_works = NULL;
4849#endif
4850 int ret;
Dave Cole331708b2004-08-09 04:51:41 +00004851
4852#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004853 family = AF_UNIX;
Dave Cole331708b2004-08-09 04:51:41 +00004854#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004855 family = AF_INET;
Dave Cole331708b2004-08-09 04:51:41 +00004856#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004857 if (!PyArg_ParseTuple(args, "|iii:socketpair",
4858 &family, &type, &proto))
4859 return NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02004860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004861 /* Create a pair of socket fds */
Victor Stinnerdaf45552013-08-28 00:53:59 +02004862 Py_BEGIN_ALLOW_THREADS
4863#ifdef SOCK_CLOEXEC
4864 if (sock_cloexec_works != 0) {
4865 ret = socketpair(family, type | SOCK_CLOEXEC, proto, sv);
4866 if (sock_cloexec_works == -1) {
4867 if (ret >= 0) {
4868 sock_cloexec_works = 1;
4869 }
4870 else if (errno == EINVAL) {
4871 /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */
4872 sock_cloexec_works = 0;
4873 ret = socketpair(family, type, proto, sv);
4874 }
4875 }
4876 }
4877 else
4878#endif
4879 {
4880 ret = socketpair(family, type, proto, sv);
4881 }
4882 Py_END_ALLOW_THREADS
4883
4884 if (ret < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004885 return set_error();
Victor Stinnerdaf45552013-08-28 00:53:59 +02004886
4887 if (_Py_set_inheritable(sv[0], 0, atomic_flag_works) < 0)
4888 goto finally;
4889 if (_Py_set_inheritable(sv[1], 0, atomic_flag_works) < 0)
4890 goto finally;
4891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004892 s0 = new_sockobject(sv[0], family, type, proto);
4893 if (s0 == NULL)
4894 goto finally;
4895 s1 = new_sockobject(sv[1], family, type, proto);
4896 if (s1 == NULL)
4897 goto finally;
4898 res = PyTuple_Pack(2, s0, s1);
Dave Cole331708b2004-08-09 04:51:41 +00004899
4900finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004901 if (res == NULL) {
4902 if (s0 == NULL)
4903 SOCKETCLOSE(sv[0]);
4904 if (s1 == NULL)
4905 SOCKETCLOSE(sv[1]);
4906 }
4907 Py_XDECREF(s0);
4908 Py_XDECREF(s1);
4909 return res;
Dave Cole331708b2004-08-09 04:51:41 +00004910}
4911
4912PyDoc_STRVAR(socketpair_doc,
Victor Stinnerdaf45552013-08-28 00:53:59 +02004913"socketpair([family[, type [, proto]]]) -> (socket object, socket object)\n\
Dave Cole331708b2004-08-09 04:51:41 +00004914\n\
4915Create a pair of socket objects from the sockets returned by the platform\n\
4916socketpair() function.\n\
Dave Cole07fda7e2004-08-23 05:16:23 +00004917The arguments are the same as for socket() except the default family is\n\
Dave Colee8bbfe42004-08-26 00:51:16 +00004918AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
Dave Cole331708b2004-08-09 04:51:41 +00004919
4920#endif /* HAVE_SOCKETPAIR */
4921
4922
Guido van Rossum006bf911996-06-12 04:04:55 +00004923static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004924socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00004925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004926 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00004927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004928 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
4929 return NULL;
4930 }
4931 if (x1 < 0) {
4932 PyErr_SetString(PyExc_OverflowError,
4933 "can't convert negative number to unsigned long");
4934 return NULL;
4935 }
4936 x2 = (unsigned int)ntohs((unsigned short)x1);
4937 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00004938}
4939
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004940PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004941"ntohs(integer) -> integer\n\
4942\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004943Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004944
4945
Guido van Rossum006bf911996-06-12 04:04:55 +00004946static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004947socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00004948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004949 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00004950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004951 if (PyLong_Check(arg)) {
4952 x = PyLong_AsUnsignedLong(arg);
4953 if (x == (unsigned long) -1 && PyErr_Occurred())
4954 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004955#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004956 {
4957 unsigned long y;
4958 /* only want the trailing 32 bits */
4959 y = x & 0xFFFFFFFFUL;
4960 if (y ^ x)
4961 return PyErr_Format(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03004962 "int larger than 32 bits");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004963 x = y;
4964 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004965#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004966 }
4967 else
4968 return PyErr_Format(PyExc_TypeError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03004969 "expected int, %s found",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004970 Py_TYPE(arg)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004971 return PyLong_FromUnsignedLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00004972}
4973
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004974PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004975"ntohl(integer) -> integer\n\
4976\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004977Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004978
4979
Guido van Rossum006bf911996-06-12 04:04:55 +00004980static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004981socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00004982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004983 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00004984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004985 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
4986 return NULL;
4987 }
4988 if (x1 < 0) {
4989 PyErr_SetString(PyExc_OverflowError,
4990 "can't convert negative number to unsigned long");
4991 return NULL;
4992 }
4993 x2 = (unsigned int)htons((unsigned short)x1);
4994 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00004995}
4996
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004997PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004998"htons(integer) -> integer\n\
4999\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005000Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00005001
5002
Guido van Rossum006bf911996-06-12 04:04:55 +00005003static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00005004socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00005005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005006 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00005007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005008 if (PyLong_Check(arg)) {
5009 x = PyLong_AsUnsignedLong(arg);
5010 if (x == (unsigned long) -1 && PyErr_Occurred())
5011 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00005012#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005013 {
5014 unsigned long y;
5015 /* only want the trailing 32 bits */
5016 y = x & 0xFFFFFFFFUL;
5017 if (y ^ x)
5018 return PyErr_Format(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03005019 "int larger than 32 bits");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005020 x = y;
5021 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00005022#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005023 }
5024 else
5025 return PyErr_Format(PyExc_TypeError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03005026 "expected int, %s found",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005027 Py_TYPE(arg)->tp_name);
5028 return PyLong_FromUnsignedLong(htonl((unsigned long)x));
Guido van Rossum006bf911996-06-12 04:04:55 +00005029}
5030
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005031PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00005032"htonl(integer) -> integer\n\
5033\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005034Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00005035
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005036/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005037
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005038PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum7d0a8262007-05-21 23:13:11 +00005039"inet_aton(string) -> bytes giving packed 32-bit IP representation\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005040\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00005041Convert 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 +00005042binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005043
5044static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005045socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005046{
Neal Norwitz88f115b2003-02-13 02:15:42 +00005047#ifdef HAVE_INET_ATON
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005048 struct in_addr buf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00005049#endif
5050
5051#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
Benjamin Petersonf91df042009-02-13 02:50:59 +00005052#if (SIZEOF_INT != 4)
5053#error "Not sure if in_addr_t exists and int is not 32-bits."
5054#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005055 /* Have to use inet_addr() instead */
5056 unsigned int packed_addr;
Tim Peters1df9fdd2003-02-13 03:13:40 +00005057#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005058 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00005059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005060 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
5061 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00005062
Tim Peters1df9fdd2003-02-13 03:13:40 +00005063
5064#ifdef HAVE_INET_ATON
Thomas Wouters477c8d52006-05-27 19:21:47 +00005065
5066#ifdef USE_INET_ATON_WEAKLINK
5067 if (inet_aton != NULL) {
5068#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005069 if (inet_aton(ip_addr, &buf))
5070 return PyBytes_FromStringAndSize((char *)(&buf),
5071 sizeof(buf));
Guido van Rossumad05cdf2003-02-12 23:08:22 +00005072
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005073 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005074 "illegal IP address string passed to inet_aton");
5075 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00005076
Thomas Wouters477c8d52006-05-27 19:21:47 +00005077#ifdef USE_INET_ATON_WEAKLINK
5078 } else {
5079#endif
5080
5081#endif
5082
5083#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
5084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005085 /* special-case this address as inet_addr might return INADDR_NONE
5086 * for this */
5087 if (strcmp(ip_addr, "255.255.255.255") == 0) {
Charles-François Natali24aa0412013-08-31 14:48:25 +02005088 packed_addr = INADDR_BROADCAST;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005089 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00005090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005091 packed_addr = inet_addr(ip_addr);
Guido van Rossumc6a164b1999-08-20 19:11:27 +00005092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005093 if (packed_addr == INADDR_NONE) { /* invalid address */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005094 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005095 "illegal IP address string passed to inet_aton");
5096 return NULL;
5097 }
5098 }
5099 return PyBytes_FromStringAndSize((char *) &packed_addr,
5100 sizeof(packed_addr));
Thomas Wouters477c8d52006-05-27 19:21:47 +00005101
5102#ifdef USE_INET_ATON_WEAKLINK
5103 }
5104#endif
5105
Guido van Rossumad05cdf2003-02-12 23:08:22 +00005106#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005107}
5108
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005109PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00005110"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005111\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005112Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005113
5114static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005115socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005116{
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005117 Py_buffer packed_ip;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005118 struct in_addr packed_addr;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005119
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005120 if (!PyArg_ParseTuple(args, "y*:inet_ntoa", &packed_ip)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005121 return NULL;
5122 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00005123
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005124 if (packed_ip.len != sizeof(packed_addr)) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005125 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005126 "packed IP wrong length for inet_ntoa");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005127 PyBuffer_Release(&packed_ip);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005128 return NULL;
5129 }
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005130
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005131 memcpy(&packed_addr, packed_ip.buf, packed_ip.len);
5132 PyBuffer_Release(&packed_ip);
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005134 return PyUnicode_FromString(inet_ntoa(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005135}
Guido van Rossum82a5c661998-07-07 20:45:43 +00005136
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005137#if defined(HAVE_INET_PTON) || defined(MS_WINDOWS)
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005138
5139PyDoc_STRVAR(inet_pton_doc,
5140"inet_pton(af, ip) -> packed IP address string\n\
5141\n\
5142Convert an IP address from string format to a packed string suitable\n\
5143for use with low-level network functions.");
5144
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005145#endif
5146
5147#ifdef HAVE_INET_PTON
5148
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005149static PyObject *
5150socket_inet_pton(PyObject *self, PyObject *args)
5151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005152 int af;
5153 char* ip;
5154 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005155#ifdef ENABLE_IPV6
Victor Stinner640c35c2013-06-04 23:14:37 +02005156 char packed[Py_MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005157#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005158 char packed[sizeof(struct in_addr)];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005159#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005160 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
5161 return NULL;
5162 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005163
Martin v. Löwis04697e82004-06-02 12:35:29 +00005164#if !defined(ENABLE_IPV6) && defined(AF_INET6)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005165 if(af == AF_INET6) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005166 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005167 "can't use AF_INET6, IPv6 is disabled");
5168 return NULL;
5169 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00005170#endif
Martin v. Löwis10649092003-08-05 06:25:06 +00005171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005172 retval = inet_pton(af, ip, packed);
5173 if (retval < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005174 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005175 return NULL;
5176 } else if (retval == 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005177 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005178 "illegal IP address string passed to inet_pton");
5179 return NULL;
5180 } else if (af == AF_INET) {
5181 return PyBytes_FromStringAndSize(packed,
5182 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005183#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005184 } else if (af == AF_INET6) {
5185 return PyBytes_FromStringAndSize(packed,
5186 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005187#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005188 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005189 PyErr_SetString(PyExc_OSError, "unknown address family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005190 return NULL;
5191 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005192}
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005193#elif defined(MS_WINDOWS)
5194
5195static PyObject *
5196socket_inet_pton(PyObject *self, PyObject *args)
5197{
5198 int af;
5199 char* ip;
5200 struct sockaddr_in6 addr;
5201 INT ret, size;
5202
5203 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
5204 return NULL;
5205 }
5206
Victor Stinnere990c6e2013-11-16 00:18:58 +01005207 size = sizeof(addr);
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005208 ret = WSAStringToAddressA(ip, af, NULL, (LPSOCKADDR)&addr, &size);
5209
5210 if (ret) {
5211 PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
5212 return NULL;
5213 } else if(af == AF_INET) {
5214 struct sockaddr_in *addr4 = (struct sockaddr_in*)&addr;
Victor Stinnere990c6e2013-11-16 00:18:58 +01005215 return PyBytes_FromStringAndSize((const char *)&(addr4->sin_addr),
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005216 sizeof(addr4->sin_addr));
5217 } else if (af == AF_INET6) {
Victor Stinnere990c6e2013-11-16 00:18:58 +01005218 return PyBytes_FromStringAndSize((const char *)&(addr.sin6_addr),
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005219 sizeof(addr.sin6_addr));
5220 } else {
5221 PyErr_SetString(PyExc_OSError, "unknown address family");
5222 return NULL;
5223 }
5224}
5225
5226#endif
5227
5228#if defined(HAVE_INET_PTON) || defined(MS_WINDOWS)
Thomas Wouters477c8d52006-05-27 19:21:47 +00005229
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005230PyDoc_STRVAR(inet_ntop_doc,
5231"inet_ntop(af, packed_ip) -> string formatted IP address\n\
5232\n\
5233Convert a packed IP address of the given family to string format.");
5234
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005235#endif
5236
5237
5238#ifdef HAVE_INET_PTON
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005239static PyObject *
5240socket_inet_ntop(PyObject *self, PyObject *args)
5241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005242 int af;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005243 Py_buffer packed_ip;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005244 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005245#ifdef ENABLE_IPV6
Victor Stinner640c35c2013-06-04 23:14:37 +02005246 char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005247#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005248 char ip[INET_ADDRSTRLEN + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005249#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00005250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005251 /* Guarantee NUL-termination for PyUnicode_FromString() below */
5252 memset((void *) &ip[0], '\0', sizeof(ip));
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005253
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005254 if (!PyArg_ParseTuple(args, "iy*:inet_ntop", &af, &packed_ip)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005255 return NULL;
5256 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005258 if (af == AF_INET) {
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005259 if (packed_ip.len != sizeof(struct in_addr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005260 PyErr_SetString(PyExc_ValueError,
5261 "invalid length of packed IP address string");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005262 PyBuffer_Release(&packed_ip);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005263 return NULL;
5264 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005265#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005266 } else if (af == AF_INET6) {
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005267 if (packed_ip.len != sizeof(struct in6_addr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005268 PyErr_SetString(PyExc_ValueError,
5269 "invalid length of packed IP address string");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005270 PyBuffer_Release(&packed_ip);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005271 return NULL;
5272 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005273#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005274 } else {
5275 PyErr_Format(PyExc_ValueError,
5276 "unknown address family %d", af);
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005277 PyBuffer_Release(&packed_ip);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005278 return NULL;
5279 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005280
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005281 retval = inet_ntop(af, packed_ip.buf, ip, sizeof(ip));
5282 PyBuffer_Release(&packed_ip);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005283 if (!retval) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005284 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005285 return NULL;
5286 } else {
5287 return PyUnicode_FromString(retval);
5288 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005290 /* NOTREACHED */
5291 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
5292 return NULL;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005293}
5294
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005295#elif defined(MS_WINDOWS)
5296
5297static PyObject *
5298socket_inet_ntop(PyObject *self, PyObject *args)
5299{
5300 int af;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005301 Py_buffer packed_ip;
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005302 struct sockaddr_in6 addr;
5303 DWORD addrlen, ret, retlen;
Jason R. Coombs8ec784c2013-11-10 13:43:22 -05005304#ifdef ENABLE_IPV6
5305 char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
5306#else
5307 char ip[INET_ADDRSTRLEN + 1];
5308#endif
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005309
5310 /* Guarantee NUL-termination for PyUnicode_FromString() below */
5311 memset((void *) &ip[0], '\0', sizeof(ip));
5312
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005313 if (!PyArg_ParseTuple(args, "iy*:inet_ntop", &af, &packed_ip)) {
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005314 return NULL;
5315 }
5316
5317 if (af == AF_INET) {
5318 struct sockaddr_in * addr4 = (struct sockaddr_in *)&addr;
5319
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005320 if (packed_ip.len != sizeof(struct in_addr)) {
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005321 PyErr_SetString(PyExc_ValueError,
5322 "invalid length of packed IP address string");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005323 PyBuffer_Release(&packed_ip);
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005324 return NULL;
5325 }
5326 memset(addr4, 0, sizeof(struct sockaddr_in));
5327 addr4->sin_family = AF_INET;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005328 memcpy(&(addr4->sin_addr), packed_ip.buf, sizeof(addr4->sin_addr));
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005329 addrlen = sizeof(struct sockaddr_in);
5330 } else if (af == AF_INET6) {
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005331 if (packed_ip.len != sizeof(struct in6_addr)) {
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005332 PyErr_SetString(PyExc_ValueError,
5333 "invalid length of packed IP address string");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005334 PyBuffer_Release(&packed_ip);
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005335 return NULL;
5336 }
5337
5338 memset(&addr, 0, sizeof(addr));
5339 addr.sin6_family = AF_INET6;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005340 memcpy(&(addr.sin6_addr), packed_ip.buf, sizeof(addr.sin6_addr));
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005341 addrlen = sizeof(addr);
5342 } else {
5343 PyErr_Format(PyExc_ValueError,
5344 "unknown address family %d", af);
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005345 PyBuffer_Release(&packed_ip);
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005346 return NULL;
5347 }
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005348 PyBuffer_Release(&packed_ip);
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005349
5350 retlen = sizeof(ip);
Victor Stinnere990c6e2013-11-16 00:18:58 +01005351 ret = WSAAddressToStringA((struct sockaddr*)&addr, addrlen, NULL,
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005352 ip, &retlen);
5353
5354 if (ret) {
5355 PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
5356 return NULL;
5357 } else {
5358 return PyUnicode_FromString(ip);
5359 }
5360}
5361
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005362#endif /* HAVE_INET_PTON */
5363
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005364/* Python interface to getaddrinfo(host, port). */
5365
5366/*ARGSUSED*/
5367static PyObject *
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005368socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005369{
Victor Stinner77af1722011-05-26 14:05:59 +02005370 static char* kwnames[] = {"host", "port", "family", "type", "proto",
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005371 "flags", 0};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005372 struct addrinfo hints, *res;
5373 struct addrinfo *res0 = NULL;
5374 PyObject *hobj = NULL;
5375 PyObject *pobj = (PyObject *)NULL;
5376 char pbuf[30];
5377 char *hptr, *pptr;
5378 int family, socktype, protocol, flags;
5379 int error;
5380 PyObject *all = (PyObject *)NULL;
5381 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005382
Georg Brandl6083a4b2013-10-14 06:51:46 +02005383 socktype = protocol = flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005384 family = AF_UNSPEC;
Victor Stinner77af1722011-05-26 14:05:59 +02005385 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iiii:getaddrinfo",
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005386 kwnames, &hobj, &pobj, &family, &socktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005387 &protocol, &flags)) {
5388 return NULL;
5389 }
5390 if (hobj == Py_None) {
5391 hptr = NULL;
5392 } else if (PyUnicode_Check(hobj)) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005393 _Py_IDENTIFIER(encode);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005394
5395 idna = _PyObject_CallMethodId(hobj, &PyId_encode, "s", "idna");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005396 if (!idna)
5397 return NULL;
5398 assert(PyBytes_Check(idna));
5399 hptr = PyBytes_AS_STRING(idna);
5400 } else if (PyBytes_Check(hobj)) {
5401 hptr = PyBytes_AsString(hobj);
5402 } else {
5403 PyErr_SetString(PyExc_TypeError,
5404 "getaddrinfo() argument 1 must be string or None");
5405 return NULL;
5406 }
5407 if (PyLong_CheckExact(pobj)) {
5408 long value = PyLong_AsLong(pobj);
5409 if (value == -1 && PyErr_Occurred())
5410 goto err;
5411 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value);
5412 pptr = pbuf;
5413 } else if (PyUnicode_Check(pobj)) {
5414 pptr = _PyUnicode_AsString(pobj);
Alexander Belopolskye239d232010-12-08 23:31:48 +00005415 if (pptr == NULL)
5416 goto err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005417 } else if (PyBytes_Check(pobj)) {
Alexander Belopolskye239d232010-12-08 23:31:48 +00005418 pptr = PyBytes_AS_STRING(pobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005419 } else if (pobj == Py_None) {
5420 pptr = (char *)NULL;
5421 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005422 PyErr_SetString(PyExc_OSError, "Int or String expected");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005423 goto err;
5424 }
Ronald Oussorena822d3662013-06-10 10:36:28 +02005425#if defined(__APPLE__) && defined(AI_NUMERICSERV)
Ronald Oussoren27a4ac52013-05-24 13:47:37 +02005426 if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) {
5427 /* On OSX upto at least OSX 10.8 getaddrinfo crashes
Christian Heimesaf01f662013-12-21 16:19:10 +01005428 * if AI_NUMERICSERV is set and the servname is NULL or "0".
5429 * This workaround avoids a segfault in libsystem.
5430 */
Ronald Oussoren27a4ac52013-05-24 13:47:37 +02005431 pptr = "00";
5432 }
5433#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005434 memset(&hints, 0, sizeof(hints));
5435 hints.ai_family = family;
5436 hints.ai_socktype = socktype;
5437 hints.ai_protocol = protocol;
5438 hints.ai_flags = flags;
5439 Py_BEGIN_ALLOW_THREADS
5440 ACQUIRE_GETADDRINFO_LOCK
5441 error = getaddrinfo(hptr, pptr, &hints, &res0);
5442 Py_END_ALLOW_THREADS
5443 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
5444 if (error) {
5445 set_gaierror(error);
5446 goto err;
5447 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005449 if ((all = PyList_New(0)) == NULL)
5450 goto err;
5451 for (res = res0; res; res = res->ai_next) {
5452 PyObject *single;
5453 PyObject *addr =
5454 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
5455 if (addr == NULL)
5456 goto err;
5457 single = Py_BuildValue("iiisO", res->ai_family,
5458 res->ai_socktype, res->ai_protocol,
5459 res->ai_canonname ? res->ai_canonname : "",
5460 addr);
5461 Py_DECREF(addr);
5462 if (single == NULL)
5463 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005465 if (PyList_Append(all, single))
5466 goto err;
5467 Py_XDECREF(single);
5468 }
5469 Py_XDECREF(idna);
5470 if (res0)
5471 freeaddrinfo(res0);
5472 return all;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005473 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005474 Py_XDECREF(all);
5475 Py_XDECREF(idna);
5476 if (res0)
5477 freeaddrinfo(res0);
5478 return (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005479}
5480
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005481PyDoc_STRVAR(getaddrinfo_doc,
Berker Peksaga6ec5ee2014-06-30 11:28:40 +03005482"getaddrinfo(host, port [, family, type, proto, flags])\n\
5483 -> list of (family, type, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005484\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005485Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005486
5487/* Python interface to getnameinfo(sa, flags). */
5488
5489/*ARGSUSED*/
5490static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005491socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005493 PyObject *sa = (PyObject *)NULL;
5494 int flags;
5495 char *hostp;
Charles-François Natali366999a2012-01-02 15:47:29 +01005496 int port;
5497 unsigned int flowinfo, scope_id;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005498 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
5499 struct addrinfo hints, *res = NULL;
5500 int error;
5501 PyObject *ret = (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005503 flags = flowinfo = scope_id = 0;
5504 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
5505 return NULL;
5506 if (!PyTuple_Check(sa)) {
5507 PyErr_SetString(PyExc_TypeError,
5508 "getnameinfo() argument 1 must be a tuple");
5509 return NULL;
5510 }
Charles-François Natali366999a2012-01-02 15:47:29 +01005511 if (!PyArg_ParseTuple(sa, "si|II",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005512 &hostp, &port, &flowinfo, &scope_id))
5513 return NULL;
Charles-François Natali9b0c0062012-06-23 10:17:05 +02005514 if (flowinfo > 0xfffff) {
Charles-François Natali366999a2012-01-02 15:47:29 +01005515 PyErr_SetString(PyExc_OverflowError,
5516 "getsockaddrarg: flowinfo must be 0-1048575.");
5517 return NULL;
5518 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005519 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
5520 memset(&hints, 0, sizeof(hints));
5521 hints.ai_family = AF_UNSPEC;
5522 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Martin v. Löwis112c0f32010-08-25 07:38:15 +00005523 hints.ai_flags = AI_NUMERICHOST; /* don't do any name resolution */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005524 Py_BEGIN_ALLOW_THREADS
5525 ACQUIRE_GETADDRINFO_LOCK
5526 error = getaddrinfo(hostp, pbuf, &hints, &res);
5527 Py_END_ALLOW_THREADS
5528 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
5529 if (error) {
5530 set_gaierror(error);
5531 goto fail;
5532 }
5533 if (res->ai_next) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005534 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005535 "sockaddr resolved to multiple addresses");
5536 goto fail;
5537 }
5538 switch (res->ai_family) {
5539 case AF_INET:
5540 {
5541 if (PyTuple_GET_SIZE(sa) != 2) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005542 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005543 "IPv4 sockaddr must be 2 tuple");
5544 goto fail;
5545 }
5546 break;
5547 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00005548#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005549 case AF_INET6:
5550 {
5551 struct sockaddr_in6 *sin6;
5552 sin6 = (struct sockaddr_in6 *)res->ai_addr;
Charles-François Natali366999a2012-01-02 15:47:29 +01005553 sin6->sin6_flowinfo = htonl(flowinfo);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005554 sin6->sin6_scope_id = scope_id;
5555 break;
5556 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005557#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005558 }
Antoine Pitrou19467d22010-08-17 19:33:30 +00005559 error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005560 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
5561 if (error) {
5562 set_gaierror(error);
5563 goto fail;
5564 }
5565 ret = Py_BuildValue("ss", hbuf, pbuf);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005566
5567fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005568 if (res)
5569 freeaddrinfo(res);
5570 return ret;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005571}
5572
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005573PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005574"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005575\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005576Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005577
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005578
5579/* Python API to getting and setting the default timeout value. */
5580
5581static PyObject *
5582socket_getdefaulttimeout(PyObject *self)
5583{
Victor Stinner71694d52015-03-28 01:18:54 +01005584 if (defaulttimeout < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005585 Py_INCREF(Py_None);
5586 return Py_None;
5587 }
Victor Stinner71694d52015-03-28 01:18:54 +01005588 else {
5589 double seconds = _PyTime_AsSecondsDouble(defaulttimeout);
5590 return PyFloat_FromDouble(seconds);
5591 }
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005592}
5593
5594PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005595"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005596\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03005597Returns the default timeout in seconds (float) for new socket objects.\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005598A value of None indicates that new socket objects have no timeout.\n\
5599When the socket module is first imported, the default is None.");
5600
5601static PyObject *
5602socket_setdefaulttimeout(PyObject *self, PyObject *arg)
5603{
Victor Stinner71694d52015-03-28 01:18:54 +01005604 _PyTime_t timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005605
Victor Stinner71694d52015-03-28 01:18:54 +01005606 if (socket_parse_timeout(&timeout, arg) < 0)
5607 return NULL;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005609 defaulttimeout = timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005611 Py_INCREF(Py_None);
5612 return Py_None;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005613}
5614
5615PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005616"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005617\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03005618Set the default timeout in seconds (float) for new socket objects.\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005619A value of None indicates that new socket objects have no timeout.\n\
5620When the socket module is first imported, the default is None.");
5621
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005622#ifdef HAVE_IF_NAMEINDEX
5623/* Python API for getting interface indices and names */
5624
5625static PyObject *
5626socket_if_nameindex(PyObject *self, PyObject *arg)
5627{
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005628 PyObject *list;
Charles-François Natali60713592011-05-20 16:55:06 +02005629 int i;
5630 struct if_nameindex *ni;
Victor Stinner77af1722011-05-26 14:05:59 +02005631
Charles-François Natali60713592011-05-20 16:55:06 +02005632 ni = if_nameindex();
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005633 if (ni == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005634 PyErr_SetFromErrno(PyExc_OSError);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005635 return NULL;
5636 }
5637
5638 list = PyList_New(0);
5639 if (list == NULL) {
5640 if_freenameindex(ni);
5641 return NULL;
5642 }
5643
Charles-François Natali60713592011-05-20 16:55:06 +02005644 for (i = 0; ni[i].if_index != 0 && i < INT_MAX; i++) {
5645 PyObject *ni_tuple = Py_BuildValue("IO&",
5646 ni[i].if_index, PyUnicode_DecodeFSDefault, ni[i].if_name);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005647
5648 if (ni_tuple == NULL || PyList_Append(list, ni_tuple) == -1) {
5649 Py_XDECREF(ni_tuple);
Charles-François Natali60713592011-05-20 16:55:06 +02005650 Py_DECREF(list);
5651 if_freenameindex(ni);
5652 return NULL;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005653 }
5654 Py_DECREF(ni_tuple);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005655 }
5656
5657 if_freenameindex(ni);
5658 return list;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005659}
5660
5661PyDoc_STRVAR(if_nameindex_doc,
5662"if_nameindex()\n\
5663\n\
5664Returns a list of network interface information (index, name) tuples.");
5665
Charles-François Natali60713592011-05-20 16:55:06 +02005666static PyObject *
5667socket_if_nametoindex(PyObject *self, PyObject *args)
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005668{
Charles-François Natali60713592011-05-20 16:55:06 +02005669 PyObject *oname;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005670 unsigned long index;
5671
Charles-François Natali60713592011-05-20 16:55:06 +02005672 if (!PyArg_ParseTuple(args, "O&:if_nametoindex",
5673 PyUnicode_FSConverter, &oname))
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005674 return NULL;
5675
Charles-François Natali60713592011-05-20 16:55:06 +02005676 index = if_nametoindex(PyBytes_AS_STRING(oname));
5677 Py_DECREF(oname);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005678 if (index == 0) {
Charles-François Natali60713592011-05-20 16:55:06 +02005679 /* if_nametoindex() doesn't set errno */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005680 PyErr_SetString(PyExc_OSError, "no interface with this name");
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005681 return NULL;
5682 }
5683
5684 return PyLong_FromUnsignedLong(index);
5685}
5686
5687PyDoc_STRVAR(if_nametoindex_doc,
5688"if_nametoindex(if_name)\n\
5689\n\
5690Returns the interface index corresponding to the interface name if_name.");
5691
Charles-François Natali60713592011-05-20 16:55:06 +02005692static PyObject *
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005693socket_if_indextoname(PyObject *self, PyObject *arg)
5694{
Charles-François Natali60713592011-05-20 16:55:06 +02005695 unsigned long index;
5696 char name[IF_NAMESIZE + 1];
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005697
Charles-François Natali60713592011-05-20 16:55:06 +02005698 index = PyLong_AsUnsignedLong(arg);
5699 if (index == (unsigned long) -1)
5700 return NULL;
5701
5702 if (if_indextoname(index, name) == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005703 PyErr_SetFromErrno(PyExc_OSError);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005704 return NULL;
5705 }
5706
Charles-François Natali60713592011-05-20 16:55:06 +02005707 return PyUnicode_DecodeFSDefault(name);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005708}
5709
5710PyDoc_STRVAR(if_indextoname_doc,
5711"if_indextoname(if_index)\n\
5712\n\
5713Returns the interface name corresponding to the interface index if_index.");
5714
5715#endif /* HAVE_IF_NAMEINDEX */
5716
5717
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005718#ifdef CMSG_LEN
5719/* Python interface to CMSG_LEN(length). */
5720
5721static PyObject *
5722socket_CMSG_LEN(PyObject *self, PyObject *args)
5723{
5724 Py_ssize_t length;
5725 size_t result;
5726
5727 if (!PyArg_ParseTuple(args, "n:CMSG_LEN", &length))
5728 return NULL;
5729 if (length < 0 || !get_CMSG_LEN(length, &result)) {
5730 PyErr_Format(PyExc_OverflowError, "CMSG_LEN() argument out of range");
5731 return NULL;
5732 }
5733 return PyLong_FromSize_t(result);
5734}
5735
5736PyDoc_STRVAR(CMSG_LEN_doc,
5737"CMSG_LEN(length) -> control message length\n\
5738\n\
5739Return the total length, without trailing padding, of an ancillary\n\
5740data item with associated data of the given length. This value can\n\
5741often be used as the buffer size for recvmsg() to receive a single\n\
5742item of ancillary data, but RFC 3542 requires portable applications to\n\
5743use CMSG_SPACE() and thus include space for padding, even when the\n\
5744item will be the last in the buffer. Raises OverflowError if length\n\
5745is outside the permissible range of values.");
5746
5747
5748#ifdef CMSG_SPACE
5749/* Python interface to CMSG_SPACE(length). */
5750
5751static PyObject *
5752socket_CMSG_SPACE(PyObject *self, PyObject *args)
5753{
5754 Py_ssize_t length;
5755 size_t result;
5756
5757 if (!PyArg_ParseTuple(args, "n:CMSG_SPACE", &length))
5758 return NULL;
5759 if (length < 0 || !get_CMSG_SPACE(length, &result)) {
5760 PyErr_SetString(PyExc_OverflowError,
5761 "CMSG_SPACE() argument out of range");
5762 return NULL;
5763 }
5764 return PyLong_FromSize_t(result);
5765}
5766
5767PyDoc_STRVAR(CMSG_SPACE_doc,
5768"CMSG_SPACE(length) -> buffer size\n\
5769\n\
5770Return the buffer size needed for recvmsg() to receive an ancillary\n\
5771data item with associated data of the given length, along with any\n\
5772trailing padding. The buffer space needed to receive multiple items\n\
5773is the sum of the CMSG_SPACE() values for their associated data\n\
5774lengths. Raises OverflowError if length is outside the permissible\n\
5775range of values.");
5776#endif /* CMSG_SPACE */
5777#endif /* CMSG_LEN */
5778
5779
Guido van Rossum30a685f1991-06-27 15:51:29 +00005780/* List of functions exported by this module. */
5781
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005782static PyMethodDef socket_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005783 {"gethostbyname", socket_gethostbyname,
5784 METH_VARARGS, gethostbyname_doc},
5785 {"gethostbyname_ex", socket_gethostbyname_ex,
5786 METH_VARARGS, ghbn_ex_doc},
5787 {"gethostbyaddr", socket_gethostbyaddr,
5788 METH_VARARGS, gethostbyaddr_doc},
5789 {"gethostname", socket_gethostname,
5790 METH_NOARGS, gethostname_doc},
Antoine Pitrou061cfb52011-02-28 22:25:22 +00005791#ifdef HAVE_SETHOSTNAME
5792 {"sethostname", socket_sethostname,
5793 METH_VARARGS, sethostname_doc},
5794#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005795 {"getservbyname", socket_getservbyname,
5796 METH_VARARGS, getservbyname_doc},
5797 {"getservbyport", socket_getservbyport,
5798 METH_VARARGS, getservbyport_doc},
5799 {"getprotobyname", socket_getprotobyname,
5800 METH_VARARGS, getprotobyname_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005801#ifndef NO_DUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005802 {"dup", socket_dup,
5803 METH_O, dup_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005804#endif
Dave Cole331708b2004-08-09 04:51:41 +00005805#ifdef HAVE_SOCKETPAIR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005806 {"socketpair", socket_socketpair,
5807 METH_VARARGS, socketpair_doc},
Dave Cole331708b2004-08-09 04:51:41 +00005808#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005809 {"ntohs", socket_ntohs,
5810 METH_VARARGS, ntohs_doc},
5811 {"ntohl", socket_ntohl,
5812 METH_O, ntohl_doc},
5813 {"htons", socket_htons,
5814 METH_VARARGS, htons_doc},
5815 {"htonl", socket_htonl,
5816 METH_O, htonl_doc},
5817 {"inet_aton", socket_inet_aton,
5818 METH_VARARGS, inet_aton_doc},
5819 {"inet_ntoa", socket_inet_ntoa,
5820 METH_VARARGS, inet_ntoa_doc},
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005821#if defined(HAVE_INET_PTON) || defined(MS_WINDOWS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005822 {"inet_pton", socket_inet_pton,
5823 METH_VARARGS, inet_pton_doc},
5824 {"inet_ntop", socket_inet_ntop,
5825 METH_VARARGS, inet_ntop_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005826#endif
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005827 {"getaddrinfo", (PyCFunction)socket_getaddrinfo,
5828 METH_VARARGS | METH_KEYWORDS, getaddrinfo_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005829 {"getnameinfo", socket_getnameinfo,
5830 METH_VARARGS, getnameinfo_doc},
5831 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
5832 METH_NOARGS, getdefaulttimeout_doc},
5833 {"setdefaulttimeout", socket_setdefaulttimeout,
5834 METH_O, setdefaulttimeout_doc},
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005835#ifdef HAVE_IF_NAMEINDEX
5836 {"if_nameindex", socket_if_nameindex,
5837 METH_NOARGS, if_nameindex_doc},
5838 {"if_nametoindex", socket_if_nametoindex,
Charles-François Natali60713592011-05-20 16:55:06 +02005839 METH_VARARGS, if_nametoindex_doc},
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005840 {"if_indextoname", socket_if_indextoname,
5841 METH_O, if_indextoname_doc},
5842#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005843#ifdef CMSG_LEN
5844 {"CMSG_LEN", socket_CMSG_LEN,
5845 METH_VARARGS, CMSG_LEN_doc},
5846#ifdef CMSG_SPACE
5847 {"CMSG_SPACE", socket_CMSG_SPACE,
5848 METH_VARARGS, CMSG_SPACE_doc},
5849#endif
5850#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005851 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005852};
5853
Guido van Rossum30a685f1991-06-27 15:51:29 +00005854
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005855#ifdef MS_WINDOWS
5856#define OS_INIT_DEFINED
5857
5858/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00005859
5860static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005861os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00005862{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005863 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00005864}
5865
5866static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005867os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00005868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005869 WSADATA WSAData;
5870 int ret;
5871 ret = WSAStartup(0x0101, &WSAData);
5872 switch (ret) {
5873 case 0: /* No error */
5874 Py_AtExit(os_cleanup);
5875 return 1; /* Success */
5876 case WSASYSNOTREADY:
5877 PyErr_SetString(PyExc_ImportError,
5878 "WSAStartup failed: network not ready");
5879 break;
5880 case WSAVERNOTSUPPORTED:
5881 case WSAEINVAL:
5882 PyErr_SetString(
5883 PyExc_ImportError,
5884 "WSAStartup failed: requested version not supported");
5885 break;
5886 default:
5887 PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret);
5888 break;
5889 }
5890 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00005891}
5892
Guido van Rossum8d665e61996-06-26 18:22:49 +00005893#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00005894
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005895
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005896
5897#ifndef OS_INIT_DEFINED
5898static int
5899os_init(void)
5900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005901 return 1; /* Success */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005902}
5903#endif
5904
5905
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00005906/* C API table - always add new things to the end for binary
5907 compatibility. */
5908static
5909PySocketModule_APIObject PySocketModuleAPI =
5910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005911 &sock_type,
Antoine Pitrouc4df7842010-12-03 19:59:41 +00005912 NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005913 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00005914};
5915
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005916
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005917/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005918
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005919 This module is actually called "_socket", and there's a wrapper
Guido van Rossum7d0a8262007-05-21 23:13:11 +00005920 "socket.py" which implements some additional functionality.
5921 The import of "_socket" may fail with an ImportError exception if
5922 os-specific initialization fails. On Windows, this does WINSOCK
Ezio Melotti13925002011-03-16 11:05:33 +02005923 initialization. When WINSOCK is initialized successfully, a call to
Guido van Rossum7d0a8262007-05-21 23:13:11 +00005924 WSACleanup() is scheduled to be made at exit time.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005925*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005926
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005927PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005928"Implementation module for socket operations.\n\
5929\n\
5930See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00005931
Martin v. Löwis1a214512008-06-11 05:26:20 +00005932static struct PyModuleDef socketmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005933 PyModuleDef_HEAD_INIT,
5934 PySocket_MODULE_NAME,
5935 socket_doc,
5936 -1,
5937 socket_methods,
5938 NULL,
5939 NULL,
5940 NULL,
5941 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00005942};
5943
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005944PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00005945PyInit__socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005946{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005947 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00005948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005949 if (!os_init())
5950 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005951
Victor Stinnerdaf45552013-08-28 00:53:59 +02005952#ifdef MS_WINDOWS
5953 if (support_wsa_no_inherit == -1) {
Steve Dower65e4cb12014-11-22 12:54:57 -08005954#if defined(_MSC_VER) && _MSC_VER >= 1800
5955 support_wsa_no_inherit = IsWindows7SP1OrGreater();
5956#else
Victor Stinnerdaf45552013-08-28 00:53:59 +02005957 DWORD version = GetVersion();
5958 DWORD major = (DWORD)LOBYTE(LOWORD(version));
5959 DWORD minor = (DWORD)HIBYTE(LOWORD(version));
5960 /* need Windows 7 SP1, 2008 R2 SP1 or later */
Steve Dower65e4cb12014-11-22 12:54:57 -08005961 support_wsa_no_inherit = major > 6 || (major == 6 && minor >= 1);
5962#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02005963 }
5964#endif
5965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005966 Py_TYPE(&sock_type) = &PyType_Type;
5967 m = PyModule_Create(&socketmodule);
5968 if (m == NULL)
5969 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005970
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005971 Py_INCREF(PyExc_OSError);
5972 PySocketModuleAPI.error = PyExc_OSError;
5973 Py_INCREF(PyExc_OSError);
5974 PyModule_AddObject(m, "error", PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005975 socket_herror = PyErr_NewException("socket.herror",
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005976 PyExc_OSError, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005977 if (socket_herror == NULL)
5978 return NULL;
5979 Py_INCREF(socket_herror);
5980 PyModule_AddObject(m, "herror", socket_herror);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005981 socket_gaierror = PyErr_NewException("socket.gaierror", PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005982 NULL);
5983 if (socket_gaierror == NULL)
5984 return NULL;
5985 Py_INCREF(socket_gaierror);
5986 PyModule_AddObject(m, "gaierror", socket_gaierror);
5987 socket_timeout = PyErr_NewException("socket.timeout",
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005988 PyExc_OSError, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005989 if (socket_timeout == NULL)
5990 return NULL;
Antoine Pitrouc4df7842010-12-03 19:59:41 +00005991 PySocketModuleAPI.timeout_error = socket_timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005992 Py_INCREF(socket_timeout);
5993 PyModule_AddObject(m, "timeout", socket_timeout);
5994 Py_INCREF((PyObject *)&sock_type);
5995 if (PyModule_AddObject(m, "SocketType",
5996 (PyObject *)&sock_type) != 0)
5997 return NULL;
5998 Py_INCREF((PyObject *)&sock_type);
5999 if (PyModule_AddObject(m, "socket",
6000 (PyObject *)&sock_type) != 0)
6001 return NULL;
Guido van Rossum09be4091999-08-09 14:40:40 +00006002
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00006003#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006004 has_ipv6 = Py_True;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00006005#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006006 has_ipv6 = Py_False;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00006007#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006008 Py_INCREF(has_ipv6);
6009 PyModule_AddObject(m, "has_ipv6", has_ipv6);
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00006010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006011 /* Export C API */
6012 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
6013 PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL)
6014 ) != 0)
6015 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00006016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006017 /* Address families (we only support AF_INET and AF_UNIX) */
Guido van Rossum09be4091999-08-09 14:40:40 +00006018#ifdef AF_UNSPEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006019 PyModule_AddIntMacro(m, AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00006020#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006021 PyModule_AddIntMacro(m, AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006022#ifdef AF_INET6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006023 PyModule_AddIntMacro(m, AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006024#endif /* AF_INET6 */
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00006025#if defined(AF_UNIX)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006026 PyModule_AddIntMacro(m, AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006027#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00006028#ifdef AF_AX25
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006029 /* Amateur Radio AX.25 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006030 PyModule_AddIntMacro(m, AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00006031#endif
6032#ifdef AF_IPX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006033 PyModule_AddIntMacro(m, AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00006034#endif
6035#ifdef AF_APPLETALK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006036 /* Appletalk DDP */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006037 PyModule_AddIntMacro(m, AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00006038#endif
6039#ifdef AF_NETROM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006040 /* Amateur radio NetROM */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006041 PyModule_AddIntMacro(m, AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00006042#endif
6043#ifdef AF_BRIDGE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006044 /* Multiprotocol bridge */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006045 PyModule_AddIntMacro(m, AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00006046#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006047#ifdef AF_ATMPVC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006048 /* ATM PVCs */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006049 PyModule_AddIntMacro(m, AF_ATMPVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006050#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006051#ifdef AF_AAL5
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006052 /* Reserved for Werner's ATM */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006053 PyModule_AddIntMacro(m, AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00006054#endif
6055#ifdef AF_X25
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006056 /* Reserved for X.25 project */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006057 PyModule_AddIntMacro(m, AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00006058#endif
6059#ifdef AF_INET6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006060 PyModule_AddIntMacro(m, AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00006061#endif
6062#ifdef AF_ROSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006063 /* Amateur Radio X.25 PLP */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006064 PyModule_AddIntMacro(m, AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00006065#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006066#ifdef AF_DECnet
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006067 /* Reserved for DECnet project */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006068 PyModule_AddIntMacro(m, AF_DECnet);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006069#endif
6070#ifdef AF_NETBEUI
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006071 /* Reserved for 802.2LLC project */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006072 PyModule_AddIntMacro(m, AF_NETBEUI);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006073#endif
6074#ifdef AF_SECURITY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006075 /* Security callback pseudo AF */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006076 PyModule_AddIntMacro(m, AF_SECURITY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006077#endif
6078#ifdef AF_KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006079 /* PF_KEY key management API */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006080 PyModule_AddIntMacro(m, AF_KEY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006081#endif
6082#ifdef AF_NETLINK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006083 /* */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006084 PyModule_AddIntMacro(m, AF_NETLINK);
6085 PyModule_AddIntMacro(m, NETLINK_ROUTE);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006086#ifdef NETLINK_SKIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006087 PyModule_AddIntMacro(m, NETLINK_SKIP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006088#endif
6089#ifdef NETLINK_W1
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006090 PyModule_AddIntMacro(m, NETLINK_W1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006091#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006092 PyModule_AddIntMacro(m, NETLINK_USERSOCK);
6093 PyModule_AddIntMacro(m, NETLINK_FIREWALL);
Guido van Rossum668a94a2006-02-21 01:07:27 +00006094#ifdef NETLINK_TCPDIAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006095 PyModule_AddIntMacro(m, NETLINK_TCPDIAG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00006096#endif
6097#ifdef NETLINK_NFLOG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006098 PyModule_AddIntMacro(m, NETLINK_NFLOG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00006099#endif
Neal Norwitz65851662006-01-16 04:31:40 +00006100#ifdef NETLINK_XFRM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006101 PyModule_AddIntMacro(m, NETLINK_XFRM);
Neal Norwitz65851662006-01-16 04:31:40 +00006102#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006103#ifdef NETLINK_ARPD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006104 PyModule_AddIntMacro(m, NETLINK_ARPD);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006105#endif
6106#ifdef NETLINK_ROUTE6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006107 PyModule_AddIntMacro(m, NETLINK_ROUTE6);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006108#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006109 PyModule_AddIntMacro(m, NETLINK_IP6_FW);
Thomas Wouterscf297e42007-02-23 15:07:44 +00006110#ifdef NETLINK_DNRTMSG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006111 PyModule_AddIntMacro(m, NETLINK_DNRTMSG);
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00006112#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006113#ifdef NETLINK_TAPBASE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006114 PyModule_AddIntMacro(m, NETLINK_TAPBASE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006115#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006116#endif /* AF_NETLINK */
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006117#ifdef AF_ROUTE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006118 /* Alias to emulate 4.4BSD */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006119 PyModule_AddIntMacro(m, AF_ROUTE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006120#endif
Giampaolo Rodola'80e1c432013-05-21 21:02:04 +02006121#ifdef AF_LINK
6122 PyModule_AddIntMacro(m, AF_LINK);
6123#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006124#ifdef AF_ASH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006125 /* Ash */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006126 PyModule_AddIntMacro(m, AF_ASH);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006127#endif
6128#ifdef AF_ECONET
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006129 /* Acorn Econet */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006130 PyModule_AddIntMacro(m, AF_ECONET);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006131#endif
6132#ifdef AF_ATMSVC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006133 /* ATM SVCs */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006134 PyModule_AddIntMacro(m, AF_ATMSVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006135#endif
6136#ifdef AF_SNA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006137 /* Linux SNA Project (nutters!) */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006138 PyModule_AddIntMacro(m, AF_SNA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006139#endif
6140#ifdef AF_IRDA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006141 /* IRDA sockets */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006142 PyModule_AddIntMacro(m, AF_IRDA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006143#endif
6144#ifdef AF_PPPOX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006145 /* PPPoX sockets */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006146 PyModule_AddIntMacro(m, AF_PPPOX);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006147#endif
6148#ifdef AF_WANPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006149 /* Wanpipe API Sockets */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006150 PyModule_AddIntMacro(m, AF_WANPIPE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006151#endif
6152#ifdef AF_LLC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006153 /* Linux LLC */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006154 PyModule_AddIntMacro(m, AF_LLC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006155#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00006156
Hye-Shik Chang81268602004-02-02 06:05:24 +00006157#ifdef USE_BLUETOOTH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006158 PyModule_AddIntMacro(m, AF_BLUETOOTH);
6159 PyModule_AddIntMacro(m, BTPROTO_L2CAP);
6160 PyModule_AddIntMacro(m, BTPROTO_HCI);
6161 PyModule_AddIntMacro(m, SOL_HCI);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00006162#if !defined(__NetBSD__) && !defined(__DragonFly__)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006163 PyModule_AddIntMacro(m, HCI_FILTER);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00006164#endif
Hye-Shik Chang81268602004-02-02 06:05:24 +00006165#if !defined(__FreeBSD__)
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00006166#if !defined(__NetBSD__) && !defined(__DragonFly__)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006167 PyModule_AddIntMacro(m, HCI_TIME_STAMP);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00006168#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006169 PyModule_AddIntMacro(m, HCI_DATA_DIR);
6170 PyModule_AddIntMacro(m, BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00006171#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006172 PyModule_AddIntMacro(m, BTPROTO_RFCOMM);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006173 PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
6174 PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
Martin v. Löwis12af0482004-01-31 12:34:17 +00006175#endif
6176
Charles-François Natali47413c12011-10-06 19:47:44 +02006177#ifdef AF_CAN
6178 /* Controller Area Network */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006179 PyModule_AddIntMacro(m, AF_CAN);
Charles-François Natali47413c12011-10-06 19:47:44 +02006180#endif
6181#ifdef PF_CAN
6182 /* Controller Area Network */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006183 PyModule_AddIntMacro(m, PF_CAN);
Charles-François Natali47413c12011-10-06 19:47:44 +02006184#endif
6185
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006186/* Reliable Datagram Sockets */
6187#ifdef AF_RDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006188 PyModule_AddIntMacro(m, AF_RDS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006189#endif
6190#ifdef PF_RDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006191 PyModule_AddIntMacro(m, PF_RDS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006192#endif
6193
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006194/* Kernel event messages */
6195#ifdef PF_SYSTEM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006196 PyModule_AddIntMacro(m, PF_SYSTEM);
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006197#endif
6198#ifdef AF_SYSTEM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006199 PyModule_AddIntMacro(m, AF_SYSTEM);
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006200#endif
6201
Antoine Pitroub156a462010-10-27 20:13:57 +00006202#ifdef AF_PACKET
6203 PyModule_AddIntMacro(m, AF_PACKET);
6204#endif
6205#ifdef PF_PACKET
6206 PyModule_AddIntMacro(m, PF_PACKET);
6207#endif
6208#ifdef PACKET_HOST
6209 PyModule_AddIntMacro(m, PACKET_HOST);
6210#endif
6211#ifdef PACKET_BROADCAST
6212 PyModule_AddIntMacro(m, PACKET_BROADCAST);
6213#endif
6214#ifdef PACKET_MULTICAST
6215 PyModule_AddIntMacro(m, PACKET_MULTICAST);
6216#endif
6217#ifdef PACKET_OTHERHOST
6218 PyModule_AddIntMacro(m, PACKET_OTHERHOST);
6219#endif
6220#ifdef PACKET_OUTGOING
6221 PyModule_AddIntMacro(m, PACKET_OUTGOING);
6222#endif
6223#ifdef PACKET_LOOPBACK
6224 PyModule_AddIntMacro(m, PACKET_LOOPBACK);
6225#endif
6226#ifdef PACKET_FASTROUTE
6227 PyModule_AddIntMacro(m, PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00006228#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006229
Christian Heimes043d6f62008-01-07 17:19:16 +00006230#ifdef HAVE_LINUX_TIPC_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006231 PyModule_AddIntMacro(m, AF_TIPC);
Christian Heimes043d6f62008-01-07 17:19:16 +00006232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006233 /* for addresses */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006234 PyModule_AddIntMacro(m, TIPC_ADDR_NAMESEQ);
6235 PyModule_AddIntMacro(m, TIPC_ADDR_NAME);
6236 PyModule_AddIntMacro(m, TIPC_ADDR_ID);
Christian Heimes043d6f62008-01-07 17:19:16 +00006237
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006238 PyModule_AddIntMacro(m, TIPC_ZONE_SCOPE);
6239 PyModule_AddIntMacro(m, TIPC_CLUSTER_SCOPE);
6240 PyModule_AddIntMacro(m, TIPC_NODE_SCOPE);
Christian Heimes043d6f62008-01-07 17:19:16 +00006241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006242 /* for setsockopt() */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006243 PyModule_AddIntMacro(m, SOL_TIPC);
6244 PyModule_AddIntMacro(m, TIPC_IMPORTANCE);
6245 PyModule_AddIntMacro(m, TIPC_SRC_DROPPABLE);
6246 PyModule_AddIntMacro(m, TIPC_DEST_DROPPABLE);
6247 PyModule_AddIntMacro(m, TIPC_CONN_TIMEOUT);
Christian Heimes043d6f62008-01-07 17:19:16 +00006248
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006249 PyModule_AddIntMacro(m, TIPC_LOW_IMPORTANCE);
6250 PyModule_AddIntMacro(m, TIPC_MEDIUM_IMPORTANCE);
6251 PyModule_AddIntMacro(m, TIPC_HIGH_IMPORTANCE);
6252 PyModule_AddIntMacro(m, TIPC_CRITICAL_IMPORTANCE);
Christian Heimes043d6f62008-01-07 17:19:16 +00006253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006254 /* for subscriptions */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006255 PyModule_AddIntMacro(m, TIPC_SUB_PORTS);
6256 PyModule_AddIntMacro(m, TIPC_SUB_SERVICE);
Christian Heimes25bb7832008-01-11 16:17:00 +00006257#ifdef TIPC_SUB_CANCEL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006258 /* doesn't seem to be available everywhere */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006259 PyModule_AddIntMacro(m, TIPC_SUB_CANCEL);
Christian Heimes25bb7832008-01-11 16:17:00 +00006260#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006261 PyModule_AddIntMacro(m, TIPC_WAIT_FOREVER);
6262 PyModule_AddIntMacro(m, TIPC_PUBLISHED);
6263 PyModule_AddIntMacro(m, TIPC_WITHDRAWN);
6264 PyModule_AddIntMacro(m, TIPC_SUBSCR_TIMEOUT);
6265 PyModule_AddIntMacro(m, TIPC_CFG_SRV);
6266 PyModule_AddIntMacro(m, TIPC_TOP_SRV);
Christian Heimes043d6f62008-01-07 17:19:16 +00006267#endif
6268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006269 /* Socket types */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006270 PyModule_AddIntMacro(m, SOCK_STREAM);
6271 PyModule_AddIntMacro(m, SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00006272/* We have incomplete socket support. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006273 PyModule_AddIntMacro(m, SOCK_RAW);
6274 PyModule_AddIntMacro(m, SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00006275#if defined(SOCK_RDM)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006276 PyModule_AddIntMacro(m, SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00006277#endif
Antoine Pitroub1c54962010-10-14 15:05:38 +00006278#ifdef SOCK_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006279 PyModule_AddIntMacro(m, SOCK_CLOEXEC);
Antoine Pitroub1c54962010-10-14 15:05:38 +00006280#endif
6281#ifdef SOCK_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006282 PyModule_AddIntMacro(m, SOCK_NONBLOCK);
Antoine Pitroub1c54962010-10-14 15:05:38 +00006283#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006285#ifdef SO_DEBUG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006286 PyModule_AddIntMacro(m, SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006287#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006288#ifdef SO_ACCEPTCONN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006289 PyModule_AddIntMacro(m, SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006290#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006291#ifdef SO_REUSEADDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006292 PyModule_AddIntMacro(m, SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006293#endif
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00006294#ifdef SO_EXCLUSIVEADDRUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006295 PyModule_AddIntMacro(m, SO_EXCLUSIVEADDRUSE);
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00006296#endif
6297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006298#ifdef SO_KEEPALIVE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006299 PyModule_AddIntMacro(m, SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006300#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006301#ifdef SO_DONTROUTE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006302 PyModule_AddIntMacro(m, SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006303#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006304#ifdef SO_BROADCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006305 PyModule_AddIntMacro(m, SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006306#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006307#ifdef SO_USELOOPBACK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006308 PyModule_AddIntMacro(m, SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006309#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006310#ifdef SO_LINGER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006311 PyModule_AddIntMacro(m, SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006312#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006313#ifdef SO_OOBINLINE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006314 PyModule_AddIntMacro(m, SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006315#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006316#ifdef SO_REUSEPORT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006317 PyModule_AddIntMacro(m, SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006318#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006319#ifdef SO_SNDBUF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006320 PyModule_AddIntMacro(m, SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006321#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006322#ifdef SO_RCVBUF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006323 PyModule_AddIntMacro(m, SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006324#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006325#ifdef SO_SNDLOWAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006326 PyModule_AddIntMacro(m, SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006327#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006328#ifdef SO_RCVLOWAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006329 PyModule_AddIntMacro(m, SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006330#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006331#ifdef SO_SNDTIMEO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006332 PyModule_AddIntMacro(m, SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006333#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006334#ifdef SO_RCVTIMEO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006335 PyModule_AddIntMacro(m, SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006336#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006337#ifdef SO_ERROR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006338 PyModule_AddIntMacro(m, SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006339#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006340#ifdef SO_TYPE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006341 PyModule_AddIntMacro(m, SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006342#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006343#ifdef SO_SETFIB
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006344 PyModule_AddIntMacro(m, SO_SETFIB);
Larry Hastingsf0f37952010-04-02 11:47:10 +00006345#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006346#ifdef SO_PASSCRED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006347 PyModule_AddIntMacro(m, SO_PASSCRED);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006348#endif
6349#ifdef SO_PEERCRED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006350 PyModule_AddIntMacro(m, SO_PEERCRED);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006351#endif
6352#ifdef LOCAL_PEERCRED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006353 PyModule_AddIntMacro(m, LOCAL_PEERCRED);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006354#endif
Victor Stinnercb98bed2011-10-25 13:45:48 +02006355#ifdef SO_BINDTODEVICE
6356 PyModule_AddIntMacro(m, SO_BINDTODEVICE);
6357#endif
Benjamin Peterson1314ef72013-11-27 09:18:54 -06006358#ifdef SO_PRIORITY
6359 PyModule_AddIntMacro(m, SO_PRIORITY);
6360#endif
Charles-François Nataliea07eb92014-09-18 23:18:46 +01006361#ifdef SO_MARK
6362 PyModule_AddIntMacro(m, SO_MARK);
6363#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006365 /* Maximum number of connections for "listen" */
6366#ifdef SOMAXCONN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006367 PyModule_AddIntMacro(m, SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006368#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006369 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006370#endif
6371
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006372 /* Ancilliary message types */
6373#ifdef SCM_RIGHTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006374 PyModule_AddIntMacro(m, SCM_RIGHTS);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006375#endif
6376#ifdef SCM_CREDENTIALS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006377 PyModule_AddIntMacro(m, SCM_CREDENTIALS);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006378#endif
6379#ifdef SCM_CREDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006380 PyModule_AddIntMacro(m, SCM_CREDS);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006381#endif
6382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006383 /* Flags for send, recv */
6384#ifdef MSG_OOB
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006385 PyModule_AddIntMacro(m, MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006386#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006387#ifdef MSG_PEEK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006388 PyModule_AddIntMacro(m, MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006389#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006390#ifdef MSG_DONTROUTE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006391 PyModule_AddIntMacro(m, MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006392#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006393#ifdef MSG_DONTWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006394 PyModule_AddIntMacro(m, MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00006395#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006396#ifdef MSG_EOR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006397 PyModule_AddIntMacro(m, MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006398#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006399#ifdef MSG_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006400 PyModule_AddIntMacro(m, MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006401#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006402#ifdef MSG_CTRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006403 PyModule_AddIntMacro(m, MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006404#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006405#ifdef MSG_WAITALL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006406 PyModule_AddIntMacro(m, MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006407#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006408#ifdef MSG_BTAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006409 PyModule_AddIntMacro(m, MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006410#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006411#ifdef MSG_ETAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006412 PyModule_AddIntMacro(m, MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006413#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006414#ifdef MSG_NOSIGNAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006415 PyModule_AddIntMacro(m, MSG_NOSIGNAL);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006416#endif
6417#ifdef MSG_NOTIFICATION
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006418 PyModule_AddIntMacro(m, MSG_NOTIFICATION);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006419#endif
6420#ifdef MSG_CMSG_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006421 PyModule_AddIntMacro(m, MSG_CMSG_CLOEXEC);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006422#endif
6423#ifdef MSG_ERRQUEUE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006424 PyModule_AddIntMacro(m, MSG_ERRQUEUE);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006425#endif
6426#ifdef MSG_CONFIRM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006427 PyModule_AddIntMacro(m, MSG_CONFIRM);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006428#endif
6429#ifdef MSG_MORE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006430 PyModule_AddIntMacro(m, MSG_MORE);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006431#endif
6432#ifdef MSG_EOF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006433 PyModule_AddIntMacro(m, MSG_EOF);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006434#endif
6435#ifdef MSG_BCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006436 PyModule_AddIntMacro(m, MSG_BCAST);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006437#endif
6438#ifdef MSG_MCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006439 PyModule_AddIntMacro(m, MSG_MCAST);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006440#endif
Benjamin Peterson57b66722012-12-12 22:24:47 -05006441#ifdef MSG_FASTOPEN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006442 PyModule_AddIntMacro(m, MSG_FASTOPEN);
Benjamin Peterson57b66722012-12-12 22:24:47 -05006443#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006445 /* Protocol level and numbers, usable for [gs]etsockopt */
6446#ifdef SOL_SOCKET
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006447 PyModule_AddIntMacro(m, SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006448#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006449#ifdef SOL_IP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006450 PyModule_AddIntMacro(m, SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006451#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006452 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00006453#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006454#ifdef SOL_IPX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006455 PyModule_AddIntMacro(m, SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00006456#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006457#ifdef SOL_AX25
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006458 PyModule_AddIntMacro(m, SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00006459#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006460#ifdef SOL_ATALK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006461 PyModule_AddIntMacro(m, SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00006462#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006463#ifdef SOL_NETROM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006464 PyModule_AddIntMacro(m, SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00006465#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006466#ifdef SOL_ROSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006467 PyModule_AddIntMacro(m, SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00006468#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006469#ifdef SOL_TCP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006470 PyModule_AddIntMacro(m, SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006471#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006472 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00006473#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006474#ifdef SOL_UDP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006475 PyModule_AddIntMacro(m, SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006476#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006477 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00006478#endif
Charles-François Natali47413c12011-10-06 19:47:44 +02006479#ifdef SOL_CAN_BASE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006480 PyModule_AddIntMacro(m, SOL_CAN_BASE);
Charles-François Natali47413c12011-10-06 19:47:44 +02006481#endif
6482#ifdef SOL_CAN_RAW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006483 PyModule_AddIntMacro(m, SOL_CAN_RAW);
6484 PyModule_AddIntMacro(m, CAN_RAW);
Charles-François Natali47413c12011-10-06 19:47:44 +02006485#endif
6486#ifdef HAVE_LINUX_CAN_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006487 PyModule_AddIntMacro(m, CAN_EFF_FLAG);
6488 PyModule_AddIntMacro(m, CAN_RTR_FLAG);
6489 PyModule_AddIntMacro(m, CAN_ERR_FLAG);
Charles-François Natali47413c12011-10-06 19:47:44 +02006490
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006491 PyModule_AddIntMacro(m, CAN_SFF_MASK);
6492 PyModule_AddIntMacro(m, CAN_EFF_MASK);
6493 PyModule_AddIntMacro(m, CAN_ERR_MASK);
Charles-François Natali47413c12011-10-06 19:47:44 +02006494#endif
6495#ifdef HAVE_LINUX_CAN_RAW_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006496 PyModule_AddIntMacro(m, CAN_RAW_FILTER);
6497 PyModule_AddIntMacro(m, CAN_RAW_ERR_FILTER);
6498 PyModule_AddIntMacro(m, CAN_RAW_LOOPBACK);
6499 PyModule_AddIntMacro(m, CAN_RAW_RECV_OWN_MSGS);
Charles-François Natali47413c12011-10-06 19:47:44 +02006500#endif
Charles-François Natali773e42d2013-02-05 19:42:01 +01006501#ifdef HAVE_LINUX_CAN_BCM_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006502 PyModule_AddIntMacro(m, CAN_BCM);
Charles-François Natali773e42d2013-02-05 19:42:01 +01006503 PyModule_AddIntConstant(m, "CAN_BCM_TX_SETUP", TX_SETUP);
6504 PyModule_AddIntConstant(m, "CAN_BCM_TX_DELETE", TX_DELETE);
6505 PyModule_AddIntConstant(m, "CAN_BCM_TX_READ", TX_READ);
6506 PyModule_AddIntConstant(m, "CAN_BCM_TX_SEND", TX_SEND);
6507 PyModule_AddIntConstant(m, "CAN_BCM_RX_SETUP", RX_SETUP);
6508 PyModule_AddIntConstant(m, "CAN_BCM_RX_DELETE", RX_DELETE);
6509 PyModule_AddIntConstant(m, "CAN_BCM_RX_READ", RX_READ);
6510 PyModule_AddIntConstant(m, "CAN_BCM_TX_STATUS", TX_STATUS);
6511 PyModule_AddIntConstant(m, "CAN_BCM_TX_EXPIRED", TX_EXPIRED);
6512 PyModule_AddIntConstant(m, "CAN_BCM_RX_STATUS", RX_STATUS);
6513 PyModule_AddIntConstant(m, "CAN_BCM_RX_TIMEOUT", RX_TIMEOUT);
6514 PyModule_AddIntConstant(m, "CAN_BCM_RX_CHANGED", RX_CHANGED);
6515#endif
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006516#ifdef SOL_RDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006517 PyModule_AddIntMacro(m, SOL_RDS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006518#endif
6519#ifdef RDS_CANCEL_SENT_TO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006520 PyModule_AddIntMacro(m, RDS_CANCEL_SENT_TO);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006521#endif
6522#ifdef RDS_GET_MR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006523 PyModule_AddIntMacro(m, RDS_GET_MR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006524#endif
6525#ifdef RDS_FREE_MR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006526 PyModule_AddIntMacro(m, RDS_FREE_MR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006527#endif
6528#ifdef RDS_RECVERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006529 PyModule_AddIntMacro(m, RDS_RECVERR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006530#endif
6531#ifdef RDS_CONG_MONITOR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006532 PyModule_AddIntMacro(m, RDS_CONG_MONITOR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006533#endif
6534#ifdef RDS_GET_MR_FOR_DEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006535 PyModule_AddIntMacro(m, RDS_GET_MR_FOR_DEST);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006536#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006537#ifdef IPPROTO_IP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006538 PyModule_AddIntMacro(m, IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00006539#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006540 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006541#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006542#ifdef IPPROTO_HOPOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006543 PyModule_AddIntMacro(m, IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006544#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006545#ifdef IPPROTO_ICMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006546 PyModule_AddIntMacro(m, IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00006547#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006548 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006549#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006550#ifdef IPPROTO_IGMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006551 PyModule_AddIntMacro(m, IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006552#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006553#ifdef IPPROTO_GGP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006554 PyModule_AddIntMacro(m, IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006555#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006556#ifdef IPPROTO_IPV4
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006557 PyModule_AddIntMacro(m, IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006558#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006559#ifdef IPPROTO_IPV6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006560 PyModule_AddIntMacro(m, IPPROTO_IPV6);
Martin v. Löwisa0f17342003-10-03 13:56:20 +00006561#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006562#ifdef IPPROTO_IPIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006563 PyModule_AddIntMacro(m, IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006564#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006565#ifdef IPPROTO_TCP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006566 PyModule_AddIntMacro(m, IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00006567#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006568 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006569#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006570#ifdef IPPROTO_EGP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006571 PyModule_AddIntMacro(m, IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006572#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006573#ifdef IPPROTO_PUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006574 PyModule_AddIntMacro(m, IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006575#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006576#ifdef IPPROTO_UDP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006577 PyModule_AddIntMacro(m, IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00006578#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006579 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006580#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006581#ifdef IPPROTO_IDP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006582 PyModule_AddIntMacro(m, IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006583#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006584#ifdef IPPROTO_HELLO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006585 PyModule_AddIntMacro(m, IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006586#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006587#ifdef IPPROTO_ND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006588 PyModule_AddIntMacro(m, IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006589#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006590#ifdef IPPROTO_TP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006591 PyModule_AddIntMacro(m, IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006592#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006593#ifdef IPPROTO_IPV6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006594 PyModule_AddIntMacro(m, IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006595#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006596#ifdef IPPROTO_ROUTING
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006597 PyModule_AddIntMacro(m, IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006598#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006599#ifdef IPPROTO_FRAGMENT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006600 PyModule_AddIntMacro(m, IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006601#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006602#ifdef IPPROTO_RSVP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006603 PyModule_AddIntMacro(m, IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006604#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006605#ifdef IPPROTO_GRE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006606 PyModule_AddIntMacro(m, IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006607#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006608#ifdef IPPROTO_ESP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006609 PyModule_AddIntMacro(m, IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006610#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006611#ifdef IPPROTO_AH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006612 PyModule_AddIntMacro(m, IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006613#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006614#ifdef IPPROTO_MOBILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006615 PyModule_AddIntMacro(m, IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006616#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006617#ifdef IPPROTO_ICMPV6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006618 PyModule_AddIntMacro(m, IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006619#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006620#ifdef IPPROTO_NONE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006621 PyModule_AddIntMacro(m, IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006622#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006623#ifdef IPPROTO_DSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006624 PyModule_AddIntMacro(m, IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006625#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006626#ifdef IPPROTO_XTP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006627 PyModule_AddIntMacro(m, IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006628#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006629#ifdef IPPROTO_EON
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006630 PyModule_AddIntMacro(m, IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006631#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006632#ifdef IPPROTO_PIM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006633 PyModule_AddIntMacro(m, IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006634#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006635#ifdef IPPROTO_IPCOMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006636 PyModule_AddIntMacro(m, IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006637#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006638#ifdef IPPROTO_VRRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006639 PyModule_AddIntMacro(m, IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006640#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006641#ifdef IPPROTO_SCTP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006642 PyModule_AddIntMacro(m, IPPROTO_SCTP);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006643#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006644#ifdef IPPROTO_BIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006645 PyModule_AddIntMacro(m, IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006646#endif
6647/**/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006648#ifdef IPPROTO_RAW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006649 PyModule_AddIntMacro(m, IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00006650#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006651 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006652#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006653#ifdef IPPROTO_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006654 PyModule_AddIntMacro(m, IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006655#endif
6656
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006657#ifdef SYSPROTO_CONTROL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006658 PyModule_AddIntMacro(m, SYSPROTO_CONTROL);
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006659#endif
6660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006661 /* Some port configuration */
6662#ifdef IPPORT_RESERVED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006663 PyModule_AddIntMacro(m, IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006664#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006665 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006666#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006667#ifdef IPPORT_USERRESERVED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006668 PyModule_AddIntMacro(m, IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006669#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006670 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006671#endif
6672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006673 /* Some reserved IP v.4 addresses */
6674#ifdef INADDR_ANY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006675 PyModule_AddIntMacro(m, INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006676#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006677 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006678#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006679#ifdef INADDR_BROADCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006680 PyModule_AddIntMacro(m, INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006681#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006682 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006683#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006684#ifdef INADDR_LOOPBACK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006685 PyModule_AddIntMacro(m, INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006686#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006687 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006688#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006689#ifdef INADDR_UNSPEC_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006690 PyModule_AddIntMacro(m, INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006691#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006692 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006693#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006694#ifdef INADDR_ALLHOSTS_GROUP
6695 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
6696 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006697#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006698 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006699#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006700#ifdef INADDR_MAX_LOCAL_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006701 PyModule_AddIntMacro(m, INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006702#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006703 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006704#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006705#ifdef INADDR_NONE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006706 PyModule_AddIntMacro(m, INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006707#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006708 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006709#endif
6710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006711 /* IPv4 [gs]etsockopt options */
6712#ifdef IP_OPTIONS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006713 PyModule_AddIntMacro(m, IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006714#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006715#ifdef IP_HDRINCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006716 PyModule_AddIntMacro(m, IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006717#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006718#ifdef IP_TOS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006719 PyModule_AddIntMacro(m, IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006720#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006721#ifdef IP_TTL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006722 PyModule_AddIntMacro(m, IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006723#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006724#ifdef IP_RECVOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006725 PyModule_AddIntMacro(m, IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006726#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006727#ifdef IP_RECVRETOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006728 PyModule_AddIntMacro(m, IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006729#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006730#ifdef IP_RECVDSTADDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006731 PyModule_AddIntMacro(m, IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006732#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006733#ifdef IP_RETOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006734 PyModule_AddIntMacro(m, IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006735#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006736#ifdef IP_MULTICAST_IF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006737 PyModule_AddIntMacro(m, IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006738#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006739#ifdef IP_MULTICAST_TTL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006740 PyModule_AddIntMacro(m, IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006741#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006742#ifdef IP_MULTICAST_LOOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006743 PyModule_AddIntMacro(m, IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006744#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006745#ifdef IP_ADD_MEMBERSHIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006746 PyModule_AddIntMacro(m, IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006747#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006748#ifdef IP_DROP_MEMBERSHIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006749 PyModule_AddIntMacro(m, IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006750#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006751#ifdef IP_DEFAULT_MULTICAST_TTL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006752 PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00006753#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006754#ifdef IP_DEFAULT_MULTICAST_LOOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006755 PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006756#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006757#ifdef IP_MAX_MEMBERSHIPS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006758 PyModule_AddIntMacro(m, IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00006759#endif
Charles-François Natali564a42c2011-12-17 14:59:56 +01006760#ifdef IP_TRANSPARENT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006761 PyModule_AddIntMacro(m, IP_TRANSPARENT);
Charles-François Natali564a42c2011-12-17 14:59:56 +01006762#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006764 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
6765#ifdef IPV6_JOIN_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006766 PyModule_AddIntMacro(m, IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006767#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006768#ifdef IPV6_LEAVE_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006769 PyModule_AddIntMacro(m, IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006770#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006771#ifdef IPV6_MULTICAST_HOPS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006772 PyModule_AddIntMacro(m, IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006773#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006774#ifdef IPV6_MULTICAST_IF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006775 PyModule_AddIntMacro(m, IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006776#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006777#ifdef IPV6_MULTICAST_LOOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006778 PyModule_AddIntMacro(m, IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006779#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006780#ifdef IPV6_UNICAST_HOPS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006781 PyModule_AddIntMacro(m, IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006782#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006783 /* Additional IPV6 socket options, defined in RFC 3493 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00006784#ifdef IPV6_V6ONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006785 PyModule_AddIntMacro(m, IPV6_V6ONLY);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006786#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006787 /* Advanced IPV6 socket options, from RFC 3542 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00006788#ifdef IPV6_CHECKSUM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006789 PyModule_AddIntMacro(m, IPV6_CHECKSUM);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006790#endif
6791#ifdef IPV6_DONTFRAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006792 PyModule_AddIntMacro(m, IPV6_DONTFRAG);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006793#endif
6794#ifdef IPV6_DSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006795 PyModule_AddIntMacro(m, IPV6_DSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006796#endif
6797#ifdef IPV6_HOPLIMIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006798 PyModule_AddIntMacro(m, IPV6_HOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006799#endif
6800#ifdef IPV6_HOPOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006801 PyModule_AddIntMacro(m, IPV6_HOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006802#endif
6803#ifdef IPV6_NEXTHOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006804 PyModule_AddIntMacro(m, IPV6_NEXTHOP);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006805#endif
6806#ifdef IPV6_PATHMTU
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006807 PyModule_AddIntMacro(m, IPV6_PATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006808#endif
6809#ifdef IPV6_PKTINFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006810 PyModule_AddIntMacro(m, IPV6_PKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006811#endif
6812#ifdef IPV6_RECVDSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006813 PyModule_AddIntMacro(m, IPV6_RECVDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006814#endif
6815#ifdef IPV6_RECVHOPLIMIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006816 PyModule_AddIntMacro(m, IPV6_RECVHOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006817#endif
6818#ifdef IPV6_RECVHOPOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006819 PyModule_AddIntMacro(m, IPV6_RECVHOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006820#endif
6821#ifdef IPV6_RECVPKTINFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006822 PyModule_AddIntMacro(m, IPV6_RECVPKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006823#endif
6824#ifdef IPV6_RECVRTHDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006825 PyModule_AddIntMacro(m, IPV6_RECVRTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006826#endif
6827#ifdef IPV6_RECVTCLASS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006828 PyModule_AddIntMacro(m, IPV6_RECVTCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006829#endif
6830#ifdef IPV6_RTHDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006831 PyModule_AddIntMacro(m, IPV6_RTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006832#endif
6833#ifdef IPV6_RTHDRDSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006834 PyModule_AddIntMacro(m, IPV6_RTHDRDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006835#endif
6836#ifdef IPV6_RTHDR_TYPE_0
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006837 PyModule_AddIntMacro(m, IPV6_RTHDR_TYPE_0);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006838#endif
6839#ifdef IPV6_RECVPATHMTU
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006840 PyModule_AddIntMacro(m, IPV6_RECVPATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006841#endif
6842#ifdef IPV6_TCLASS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006843 PyModule_AddIntMacro(m, IPV6_TCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006844#endif
6845#ifdef IPV6_USE_MIN_MTU
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006846 PyModule_AddIntMacro(m, IPV6_USE_MIN_MTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006847#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006849 /* TCP options */
6850#ifdef TCP_NODELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006851 PyModule_AddIntMacro(m, TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00006852#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006853#ifdef TCP_MAXSEG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006854 PyModule_AddIntMacro(m, TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00006855#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006856#ifdef TCP_CORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006857 PyModule_AddIntMacro(m, TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006858#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006859#ifdef TCP_KEEPIDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006860 PyModule_AddIntMacro(m, TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006861#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006862#ifdef TCP_KEEPINTVL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006863 PyModule_AddIntMacro(m, TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006864#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006865#ifdef TCP_KEEPCNT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006866 PyModule_AddIntMacro(m, TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006867#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006868#ifdef TCP_SYNCNT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006869 PyModule_AddIntMacro(m, TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006870#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006871#ifdef TCP_LINGER2
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006872 PyModule_AddIntMacro(m, TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006873#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006874#ifdef TCP_DEFER_ACCEPT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006875 PyModule_AddIntMacro(m, TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006876#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006877#ifdef TCP_WINDOW_CLAMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006878 PyModule_AddIntMacro(m, TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006879#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006880#ifdef TCP_INFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006881 PyModule_AddIntMacro(m, TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006882#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006883#ifdef TCP_QUICKACK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006884 PyModule_AddIntMacro(m, TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006885#endif
Benjamin Peterson57b66722012-12-12 22:24:47 -05006886#ifdef TCP_FASTOPEN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006887 PyModule_AddIntMacro(m, TCP_FASTOPEN);
Benjamin Peterson57b66722012-12-12 22:24:47 -05006888#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006890 /* IPX options */
6891#ifdef IPX_TYPE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006892 PyModule_AddIntMacro(m, IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00006893#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00006894
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006895/* Reliable Datagram Sockets */
6896#ifdef RDS_CMSG_RDMA_ARGS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006897 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_ARGS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006898#endif
6899#ifdef RDS_CMSG_RDMA_DEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006900 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_DEST);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006901#endif
6902#ifdef RDS_CMSG_RDMA_MAP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006903 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_MAP);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006904#endif
6905#ifdef RDS_CMSG_RDMA_STATUS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006906 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_STATUS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006907#endif
6908#ifdef RDS_CMSG_RDMA_UPDATE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006909 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_UPDATE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006910#endif
6911#ifdef RDS_RDMA_READWRITE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006912 PyModule_AddIntMacro(m, RDS_RDMA_READWRITE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006913#endif
6914#ifdef RDS_RDMA_FENCE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006915 PyModule_AddIntMacro(m, RDS_RDMA_FENCE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006916#endif
6917#ifdef RDS_RDMA_INVALIDATE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006918 PyModule_AddIntMacro(m, RDS_RDMA_INVALIDATE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006919#endif
6920#ifdef RDS_RDMA_USE_ONCE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006921 PyModule_AddIntMacro(m, RDS_RDMA_USE_ONCE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006922#endif
6923#ifdef RDS_RDMA_DONTWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006924 PyModule_AddIntMacro(m, RDS_RDMA_DONTWAIT);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006925#endif
6926#ifdef RDS_RDMA_NOTIFY_ME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006927 PyModule_AddIntMacro(m, RDS_RDMA_NOTIFY_ME);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006928#endif
6929#ifdef RDS_RDMA_SILENT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006930 PyModule_AddIntMacro(m, RDS_RDMA_SILENT);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006931#endif
6932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006933 /* get{addr,name}info parameters */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006934#ifdef EAI_ADDRFAMILY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006935 PyModule_AddIntMacro(m, EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006936#endif
6937#ifdef EAI_AGAIN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006938 PyModule_AddIntMacro(m, EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006939#endif
6940#ifdef EAI_BADFLAGS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006941 PyModule_AddIntMacro(m, EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006942#endif
6943#ifdef EAI_FAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006944 PyModule_AddIntMacro(m, EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006945#endif
6946#ifdef EAI_FAMILY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006947 PyModule_AddIntMacro(m, EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006948#endif
6949#ifdef EAI_MEMORY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006950 PyModule_AddIntMacro(m, EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006951#endif
6952#ifdef EAI_NODATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006953 PyModule_AddIntMacro(m, EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006954#endif
6955#ifdef EAI_NONAME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006956 PyModule_AddIntMacro(m, EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006957#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00006958#ifdef EAI_OVERFLOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006959 PyModule_AddIntMacro(m, EAI_OVERFLOW);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006960#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006961#ifdef EAI_SERVICE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006962 PyModule_AddIntMacro(m, EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006963#endif
6964#ifdef EAI_SOCKTYPE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006965 PyModule_AddIntMacro(m, EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006966#endif
6967#ifdef EAI_SYSTEM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006968 PyModule_AddIntMacro(m, EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006969#endif
6970#ifdef EAI_BADHINTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006971 PyModule_AddIntMacro(m, EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006972#endif
6973#ifdef EAI_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006974 PyModule_AddIntMacro(m, EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006975#endif
6976#ifdef EAI_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006977 PyModule_AddIntMacro(m, EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006978#endif
6979#ifdef AI_PASSIVE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006980 PyModule_AddIntMacro(m, AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006981#endif
6982#ifdef AI_CANONNAME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006983 PyModule_AddIntMacro(m, AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006984#endif
6985#ifdef AI_NUMERICHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006986 PyModule_AddIntMacro(m, AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006987#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00006988#ifdef AI_NUMERICSERV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006989 PyModule_AddIntMacro(m, AI_NUMERICSERV);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006990#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006991#ifdef AI_MASK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006992 PyModule_AddIntMacro(m, AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006993#endif
6994#ifdef AI_ALL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006995 PyModule_AddIntMacro(m, AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006996#endif
6997#ifdef AI_V4MAPPED_CFG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006998 PyModule_AddIntMacro(m, AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006999#endif
7000#ifdef AI_ADDRCONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007001 PyModule_AddIntMacro(m, AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007002#endif
7003#ifdef AI_V4MAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007004 PyModule_AddIntMacro(m, AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007005#endif
7006#ifdef AI_DEFAULT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007007 PyModule_AddIntMacro(m, AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007008#endif
7009#ifdef NI_MAXHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007010 PyModule_AddIntMacro(m, NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007011#endif
7012#ifdef NI_MAXSERV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007013 PyModule_AddIntMacro(m, NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007014#endif
7015#ifdef NI_NOFQDN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007016 PyModule_AddIntMacro(m, NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007017#endif
7018#ifdef NI_NUMERICHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007019 PyModule_AddIntMacro(m, NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007020#endif
7021#ifdef NI_NAMEREQD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007022 PyModule_AddIntMacro(m, NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007023#endif
7024#ifdef NI_NUMERICSERV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007025 PyModule_AddIntMacro(m, NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007026#endif
7027#ifdef NI_DGRAM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007028 PyModule_AddIntMacro(m, NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007029#endif
7030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007031 /* shutdown() parameters */
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007032#ifdef SHUT_RD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007033 PyModule_AddIntMacro(m, SHUT_RD);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007034#elif defined(SD_RECEIVE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007035 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007036#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007037 PyModule_AddIntConstant(m, "SHUT_RD", 0);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007038#endif
7039#ifdef SHUT_WR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007040 PyModule_AddIntMacro(m, SHUT_WR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007041#elif defined(SD_SEND)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007042 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007043#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007044 PyModule_AddIntConstant(m, "SHUT_WR", 1);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007045#endif
7046#ifdef SHUT_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007047 PyModule_AddIntMacro(m, SHUT_RDWR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007048#elif defined(SD_BOTH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007049 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007050#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007051 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007052#endif
7053
Christian Heimesfaf2f632008-01-06 16:59:19 +00007054#ifdef SIO_RCVALL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007055 {
7056 DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS};
7057 const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"};
7058 int i;
Victor Stinner63941882011-09-29 00:42:28 +02007059 for(i = 0; i<Py_ARRAY_LENGTH(codes); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007060 PyObject *tmp;
7061 tmp = PyLong_FromUnsignedLong(codes[i]);
7062 if (tmp == NULL)
7063 return NULL;
7064 PyModule_AddObject(m, names[i], tmp);
7065 }
7066 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007067 PyModule_AddIntMacro(m, RCVALL_OFF);
7068 PyModule_AddIntMacro(m, RCVALL_ON);
7069 PyModule_AddIntMacro(m, RCVALL_SOCKETLEVELONLY);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00007070#ifdef RCVALL_IPLEVEL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007071 PyModule_AddIntMacro(m, RCVALL_IPLEVEL);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00007072#endif
7073#ifdef RCVALL_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007074 PyModule_AddIntMacro(m, RCVALL_MAX);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00007075#endif
Christian Heimesfaf2f632008-01-06 16:59:19 +00007076#endif /* _MSTCPIP_ */
7077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007078 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00007079#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007080 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00007081#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007082 return m;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00007083}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007084
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007085
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007086#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +00007087#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007088
7089/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00007090/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007091
Guido van Rossum3eede5a2002-06-07 02:08:35 +00007092int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007093inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007095 if (af == AF_INET) {
Benjamin Petersonf91df042009-02-13 02:50:59 +00007096#if (SIZEOF_INT != 4)
7097#error "Not sure if in_addr_t exists and int is not 32-bits."
7098#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007099 unsigned int packed_addr;
7100 packed_addr = inet_addr(src);
7101 if (packed_addr == INADDR_NONE)
7102 return 0;
7103 memcpy(dst, &packed_addr, 4);
7104 return 1;
7105 }
7106 /* Should set errno to EAFNOSUPPORT */
7107 return -1;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007108}
7109
Martin v. Löwisc925b1532001-07-21 09:42:15 +00007110const char *
7111inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007113 if (af == AF_INET) {
7114 struct in_addr packed_addr;
7115 if (size < 16)
7116 /* Should set errno to ENOSPC. */
7117 return NULL;
7118 memcpy(&packed_addr, src, sizeof(packed_addr));
7119 return strncpy(dst, inet_ntoa(packed_addr), size);
7120 }
7121 /* Should set errno to EAFNOSUPPORT */
7122 return NULL;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007123}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007124
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007125#endif
Christian Heimesb6150692007-11-15 23:37:07 +00007126#endif