blob: 057430b7040c6205acaa5f7be3090dfcab37e9a0 [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. */
463#define IS_SELECTABLE(s) (_PyIsSelectable_fd((s)->sock_fd) || (s)->sock_timeout <= 0.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
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000594/* Do a select()/poll() on the socket, if necessary (sock_timeout > 0).
Guido van Rossum11ba0942002-06-13 15:07:44 +0000595 The argument writing indicates the direction.
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000596 This does not raise an exception; we'll let our caller do that
597 after they've reacquired the interpreter lock.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000598 Returns 1 on timeout, -1 on error, 0 otherwise. */
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000599static int
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000600internal_select_ex(PySocketSockObject *s, int writing, double interval)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 int n;
Guido van Rossum11ba0942002-06-13 15:07:44 +0000603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 /* Nothing to do unless we're in timeout mode (not non-blocking) */
605 if (s->sock_timeout <= 0.0)
606 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 /* Guard against closed socket */
609 if (s->sock_fd < 0)
610 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000611
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000612 /* Handling this condition here simplifies the select loops */
613 if (interval < 0.0)
614 return 1;
615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 /* Prefer poll, if available, since you can poll() any fd
617 * which can't be done with select(). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000618#ifdef HAVE_POLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 {
620 struct pollfd pollfd;
621 int timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 pollfd.fd = s->sock_fd;
624 pollfd.events = writing ? POLLOUT : POLLIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 /* s->sock_timeout is in seconds, timeout in ms */
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000627 timeout = (int)(interval * 1000 + 0.5);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 n = poll(&pollfd, 1, timeout);
629 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000630#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 {
632 /* Construct the arguments to select */
633 fd_set fds;
634 struct timeval tv;
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000635 tv.tv_sec = (int)interval;
636 tv.tv_usec = (int)((interval - tv.tv_sec) * 1e6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 FD_ZERO(&fds);
638 FD_SET(s->sock_fd, &fds);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 /* See if the socket is ready */
641 if (writing)
Antoine Pitrou19467d22010-08-17 19:33:30 +0000642 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
643 NULL, &fds, NULL, &tv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 else
Antoine Pitrou19467d22010-08-17 19:33:30 +0000645 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
646 &fds, NULL, NULL, &tv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000648#endif
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 if (n < 0)
651 return -1;
652 if (n == 0)
653 return 1;
654 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000655}
656
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000657static int
658internal_select(PySocketSockObject *s, int writing)
659{
660 return internal_select_ex(s, writing, s->sock_timeout);
661}
662
663/*
664 Two macros for automatic retry of select() in case of false positives
665 (for example, select() could indicate a socket is ready for reading
666 but the data then discarded by the OS because of a wrong checksum).
667 Here is an example of use:
668
669 BEGIN_SELECT_LOOP(s)
670 Py_BEGIN_ALLOW_THREADS
671 timeout = internal_select_ex(s, 0, interval);
672 if (!timeout)
673 outlen = recv(s->sock_fd, cbuf, len, flags);
674 Py_END_ALLOW_THREADS
675 if (timeout == 1) {
676 PyErr_SetString(socket_timeout, "timed out");
677 return -1;
678 }
679 END_SELECT_LOOP(s)
680*/
681
682#define BEGIN_SELECT_LOOP(s) \
683 { \
684 _PyTime_timeval now, deadline = {0, 0}; \
685 double interval = s->sock_timeout; \
686 int has_timeout = s->sock_timeout > 0.0; \
687 if (has_timeout) { \
Victor Stinnerae586492014-09-02 23:18:25 +0200688 _PyTime_monotonic(&now); \
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000689 deadline = now; \
690 _PyTime_ADD_SECONDS(deadline, s->sock_timeout); \
691 } \
692 while (1) { \
693 errno = 0; \
694
695#define END_SELECT_LOOP(s) \
696 if (!has_timeout || \
697 (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \
698 break; \
Victor Stinnerae586492014-09-02 23:18:25 +0200699 _PyTime_monotonic(&now); \
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000700 interval = _PyTime_INTERVAL(now, deadline); \
701 } \
702 } \
703
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000704/* Initialize a new socket object. */
705
Tim Petersa12b4cf2002-07-18 22:38:44 +0000706static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000707
Martin v. Löwis1a214512008-06-11 05:26:20 +0000708static void
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000709init_sockobject(PySocketSockObject *s,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 SOCKET_T fd, int family, int type, int proto)
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 s->sock_fd = fd;
713 s->sock_family = family;
714 s->sock_type = type;
715 s->sock_proto = proto;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 s->errorhandler = &set_error;
Antoine Pitroub1c54962010-10-14 15:05:38 +0000718#ifdef SOCK_NONBLOCK
719 if (type & SOCK_NONBLOCK)
720 s->sock_timeout = 0.0;
721 else
722#endif
723 {
724 s->sock_timeout = defaulttimeout;
725 if (defaulttimeout >= 0.0)
726 internal_setblocking(s, 0);
727 }
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000728
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000729}
730
731
Guido van Rossum30a685f1991-06-27 15:51:29 +0000732/* Create a new socket object.
733 This just creates the object and initializes it.
734 If the creation fails, return NULL and set an exception (implicit
735 in NEWOBJ()). */
736
Guido van Rossum73624e91994-10-10 17:59:00 +0000737static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000738new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 PySocketSockObject *s;
741 s = (PySocketSockObject *)
742 PyType_GenericNew(&sock_type, NULL, NULL);
743 if (s != NULL)
744 init_sockobject(s, fd, family, type, proto);
745 return s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000746}
747
Guido van Rossum30a685f1991-06-27 15:51:29 +0000748
Guido van Rossum48a680c2001-03-02 06:34:14 +0000749/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000750 thread to be in gethostbyname or getaddrinfo */
751#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Matthias Klosed182a6c2012-08-14 17:29:04 +0200752static PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000753#endif
754
755
Guido van Rossum30a685f1991-06-27 15:51:29 +0000756/* Convert a string specifying a host name or one of a few symbolic
757 names to a numeric IP address. This usually calls gethostbyname()
758 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000759 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000760 an error occurred; then an exception is raised. */
761
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000762static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000763setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 struct addrinfo hints, *res;
766 int error;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
769 if (name[0] == '\0') {
770 int siz;
771 memset(&hints, 0, sizeof(hints));
772 hints.ai_family = af;
773 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
774 hints.ai_flags = AI_PASSIVE;
775 Py_BEGIN_ALLOW_THREADS
776 ACQUIRE_GETADDRINFO_LOCK
777 error = getaddrinfo(NULL, "0", &hints, &res);
778 Py_END_ALLOW_THREADS
779 /* We assume that those thread-unsafe getaddrinfo() versions
780 *are* safe regarding their return value, ie. that a
781 subsequent call to getaddrinfo() does not destroy the
782 outcome of the first call. */
783 RELEASE_GETADDRINFO_LOCK
784 if (error) {
785 set_gaierror(error);
786 return -1;
787 }
788 switch (res->ai_family) {
789 case AF_INET:
790 siz = 4;
791 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000792#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 case AF_INET6:
794 siz = 16;
795 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000796#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 default:
798 freeaddrinfo(res);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200799 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 "unsupported address family");
801 return -1;
802 }
803 if (res->ai_next) {
804 freeaddrinfo(res);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200805 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 "wildcard resolved to multiple address");
807 return -1;
808 }
809 if (res->ai_addrlen < addr_ret_size)
810 addr_ret_size = res->ai_addrlen;
811 memcpy(addr_ret, res->ai_addr, addr_ret_size);
812 freeaddrinfo(res);
813 return siz;
814 }
Charles-François Natali0cc86852013-09-13 19:53:08 +0200815 /* special-case broadcast - inet_addr() below can return INADDR_NONE for
Victor Stinnere990c6e2013-11-16 00:18:58 +0100816 * this */
Charles-François Natali0cc86852013-09-13 19:53:08 +0200817 if (strcmp(name, "255.255.255.255") == 0 ||
818 strcmp(name, "<broadcast>") == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 struct sockaddr_in *sin;
820 if (af != AF_INET && af != AF_UNSPEC) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200821 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 "address family mismatched");
823 return -1;
824 }
825 sin = (struct sockaddr_in *)addr_ret;
826 memset((void *) sin, '\0', sizeof(*sin));
827 sin->sin_family = AF_INET;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000828#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 sin->sin_len = sizeof(*sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000830#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 sin->sin_addr.s_addr = INADDR_BROADCAST;
832 return sizeof(sin->sin_addr);
833 }
Charles-François Natali0cc86852013-09-13 19:53:08 +0200834
835 /* avoid a name resolution in case of numeric address */
836#ifdef HAVE_INET_PTON
837 /* check for an IPv4 address */
838 if (af == AF_UNSPEC || af == AF_INET) {
839 struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret;
840 memset(sin, 0, sizeof(*sin));
841 if (inet_pton(AF_INET, name, &sin->sin_addr) > 0) {
842 sin->sin_family = AF_INET;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000843#ifdef HAVE_SOCKADDR_SA_LEN
Charles-François Natali0cc86852013-09-13 19:53:08 +0200844 sin->sin_len = sizeof(*sin);
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000845#endif
Charles-François Natali0cc86852013-09-13 19:53:08 +0200846 return 4;
847 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 }
Charles-François Natali0cc86852013-09-13 19:53:08 +0200849#ifdef ENABLE_IPV6
850 /* check for an IPv6 address - if the address contains a scope ID, we
851 * fallback to getaddrinfo(), which can handle translation from interface
852 * name to interface index */
853 if ((af == AF_UNSPEC || af == AF_INET6) && !strchr(name, '%')) {
854 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)addr_ret;
855 memset(sin, 0, sizeof(*sin));
856 if (inet_pton(AF_INET6, name, &sin->sin6_addr) > 0) {
857 sin->sin6_family = AF_INET6;
858#ifdef HAVE_SOCKADDR_SA_LEN
859 sin->sin6_len = sizeof(*sin);
860#endif
861 return 16;
862 }
863 }
864#endif /* ENABLE_IPV6 */
865#else /* HAVE_INET_PTON */
866 /* check for an IPv4 address */
867 if (af == AF_INET || af == AF_UNSPEC) {
868 struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret;
869 memset(sin, 0, sizeof(*sin));
870 if ((sin->sin_addr.s_addr = inet_addr(name)) != INADDR_NONE) {
871 sin->sin_family = AF_INET;
872#ifdef HAVE_SOCKADDR_SA_LEN
873 sin->sin_len = sizeof(*sin);
874#endif
875 return 4;
876 }
Victor Stinnere990c6e2013-11-16 00:18:58 +0100877 }
Charles-François Natali0cc86852013-09-13 19:53:08 +0200878#endif /* HAVE_INET_PTON */
879
880 /* perform a name resolution */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 memset(&hints, 0, sizeof(hints));
882 hints.ai_family = af;
883 Py_BEGIN_ALLOW_THREADS
884 ACQUIRE_GETADDRINFO_LOCK
885 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000886#if defined(__digital__) && defined(__unix__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 if (error == EAI_NONAME && af == AF_UNSPEC) {
888 /* On Tru64 V5.1, numeric-to-addr conversion fails
889 if no address family is given. Assume IPv4 for now.*/
890 hints.ai_family = AF_INET;
891 error = getaddrinfo(name, NULL, &hints, &res);
892 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000893#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 Py_END_ALLOW_THREADS
895 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
896 if (error) {
897 set_gaierror(error);
898 return -1;
899 }
900 if (res->ai_addrlen < addr_ret_size)
901 addr_ret_size = res->ai_addrlen;
902 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
903 freeaddrinfo(res);
904 switch (addr_ret->sa_family) {
905 case AF_INET:
906 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000907#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 case AF_INET6:
909 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000910#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200912 PyErr_SetString(PyExc_OSError, "unknown address family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 return -1;
914 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000915}
916
Guido van Rossum30a685f1991-06-27 15:51:29 +0000917
Guido van Rossum30a685f1991-06-27 15:51:29 +0000918/* Create a string object representing an IP address.
919 This is always a string of the form 'dd.dd.dd.dd' (with variable
920 size numbers). */
921
Guido van Rossum73624e91994-10-10 17:59:00 +0000922static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000923makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 char buf[NI_MAXHOST];
926 int error;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
929 NI_NUMERICHOST);
930 if (error) {
931 set_gaierror(error);
932 return NULL;
933 }
934 return PyUnicode_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000935}
936
937
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000938#ifdef USE_BLUETOOTH
939/* Convert a string representation of a Bluetooth address into a numeric
940 address. Returns the length (6), or raises an exception and returns -1 if
941 an error occurred. */
942
943static int
944setbdaddr(char *name, bdaddr_t *bdaddr)
945{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 unsigned int b0, b1, b2, b3, b4, b5;
947 char ch;
948 int n;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
951 &b5, &b4, &b3, &b2, &b1, &b0, &ch);
952 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
953 bdaddr->b[0] = b0;
954 bdaddr->b[1] = b1;
955 bdaddr->b[2] = b2;
956 bdaddr->b[3] = b3;
957 bdaddr->b[4] = b4;
958 bdaddr->b[5] = b5;
959 return 6;
960 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200961 PyErr_SetString(PyExc_OSError, "bad bluetooth address");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 return -1;
963 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000964}
965
966/* Create a string representation of the Bluetooth address. This is always a
967 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
968 value (zero padded if necessary). */
969
970static PyObject *
971makebdaddr(bdaddr_t *bdaddr)
972{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 char buf[(6 * 2) + 5 + 1];
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
976 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
977 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
978 return PyUnicode_FromString(buf);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000979}
980#endif
981
982
Guido van Rossum30a685f1991-06-27 15:51:29 +0000983/* Create an object representing the given socket address,
984 suitable for passing it back to bind(), connect() etc.
985 The family field of the sockaddr structure is inspected
986 to determine what kind of address it really is. */
987
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000988/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000989static PyObject *
Antoine Pitrou19467d22010-08-17 19:33:30 +0000990makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000991{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 if (addrlen == 0) {
993 /* No address -- may be recvfrom() from known socket */
994 Py_INCREF(Py_None);
995 return Py_None;
996 }
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 switch (addr->sa_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +0000999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 case AF_INET:
1001 {
1002 struct sockaddr_in *a;
1003 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
1004 PyObject *ret = NULL;
1005 if (addrobj) {
1006 a = (struct sockaddr_in *)addr;
1007 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
1008 Py_DECREF(addrobj);
1009 }
1010 return ret;
1011 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00001012
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001013#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 case AF_UNIX:
1015 {
1016 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001017#ifdef linux
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 if (a->sun_path[0] == 0) { /* Linux abstract namespace */
1019 addrlen -= offsetof(struct sockaddr_un, sun_path);
Charles-François Natalib10c71d2013-05-21 10:45:46 +02001020 return PyBytes_FromStringAndSize(a->sun_path, addrlen);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 }
1022 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001023#endif /* linux */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 {
1025 /* regular NULL-terminated string */
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001026 return PyUnicode_DecodeFSDefault(a->sun_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 }
1028 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001029#endif /* AF_UNIX */
1030
Martin v. Löwis11017b12006-01-14 18:12:57 +00001031#if defined(AF_NETLINK)
1032 case AF_NETLINK:
1033 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
1035 return Py_BuildValue("II", a->nl_pid, a->nl_groups);
Martin v. Löwis11017b12006-01-14 18:12:57 +00001036 }
1037#endif /* AF_NETLINK */
1038
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001039#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 case AF_INET6:
1041 {
1042 struct sockaddr_in6 *a;
1043 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
1044 PyObject *ret = NULL;
1045 if (addrobj) {
1046 a = (struct sockaddr_in6 *)addr;
Charles-François Natali366999a2012-01-02 15:47:29 +01001047 ret = Py_BuildValue("OiII",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 addrobj,
1049 ntohs(a->sin6_port),
Charles-François Natali366999a2012-01-02 15:47:29 +01001050 ntohl(a->sin6_flowinfo),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 a->sin6_scope_id);
1052 Py_DECREF(addrobj);
1053 }
1054 return ret;
1055 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001056#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001057
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001058#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 case AF_BLUETOOTH:
1060 switch (proto) {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 case BTPROTO_L2CAP:
1063 {
1064 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
1065 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
1066 PyObject *ret = NULL;
1067 if (addrobj) {
1068 ret = Py_BuildValue("Oi",
1069 addrobj,
1070 _BT_L2_MEMB(a, psm));
1071 Py_DECREF(addrobj);
1072 }
1073 return ret;
1074 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 case BTPROTO_RFCOMM:
1077 {
1078 struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
1079 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
1080 PyObject *ret = NULL;
1081 if (addrobj) {
1082 ret = Py_BuildValue("Oi",
1083 addrobj,
1084 _BT_RC_MEMB(a, channel));
1085 Py_DECREF(addrobj);
1086 }
1087 return ret;
1088 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 case BTPROTO_HCI:
1091 {
1092 struct sockaddr_hci *a = (struct sockaddr_hci *) addr;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001093#if defined(__NetBSD__) || defined(__DragonFly__)
1094 return makebdaddr(&_BT_HCI_MEMB(a, bdaddr));
1095#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 PyObject *ret = NULL;
1097 ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
1098 return ret;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001099#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 }
Thomas Wouterscf297e42007-02-23 15:07:44 +00001101
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001102#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 case BTPROTO_SCO:
1104 {
1105 struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
1106 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
1107 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001108#endif
1109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 default:
1111 PyErr_SetString(PyExc_ValueError,
1112 "Unknown Bluetooth protocol");
1113 return NULL;
1114 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001115#endif
1116
Antoine Pitroub156a462010-10-27 20:13:57 +00001117#if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 case AF_PACKET:
1119 {
1120 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
1121 char *ifname = "";
1122 struct ifreq ifr;
1123 /* need to look up interface name give index */
1124 if (a->sll_ifindex) {
1125 ifr.ifr_ifindex = a->sll_ifindex;
1126 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
1127 ifname = ifr.ifr_name;
1128 }
1129 return Py_BuildValue("shbhy#",
1130 ifname,
1131 ntohs(a->sll_protocol),
1132 a->sll_pkttype,
1133 a->sll_hatype,
1134 a->sll_addr,
1135 a->sll_halen);
1136 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001137#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001138
Christian Heimes043d6f62008-01-07 17:19:16 +00001139#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 case AF_TIPC:
1141 {
1142 struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr;
1143 if (a->addrtype == TIPC_ADDR_NAMESEQ) {
1144 return Py_BuildValue("IIIII",
1145 a->addrtype,
1146 a->addr.nameseq.type,
1147 a->addr.nameseq.lower,
1148 a->addr.nameseq.upper,
1149 a->scope);
1150 } else if (a->addrtype == TIPC_ADDR_NAME) {
1151 return Py_BuildValue("IIIII",
1152 a->addrtype,
1153 a->addr.name.name.type,
1154 a->addr.name.name.instance,
1155 a->addr.name.name.instance,
1156 a->scope);
1157 } else if (a->addrtype == TIPC_ADDR_ID) {
1158 return Py_BuildValue("IIIII",
1159 a->addrtype,
1160 a->addr.id.node,
1161 a->addr.id.ref,
1162 0,
1163 a->scope);
1164 } else {
1165 PyErr_SetString(PyExc_ValueError,
1166 "Invalid address type");
1167 return NULL;
1168 }
1169 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001170#endif
1171
Vinay Sajipecfc98c2014-03-20 12:42:42 +00001172#ifdef AF_CAN
Charles-François Natali47413c12011-10-06 19:47:44 +02001173 case AF_CAN:
1174 {
1175 struct sockaddr_can *a = (struct sockaddr_can *)addr;
1176 char *ifname = "";
1177 struct ifreq ifr;
1178 /* need to look up interface name given index */
1179 if (a->can_ifindex) {
1180 ifr.ifr_ifindex = a->can_ifindex;
1181 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
1182 ifname = ifr.ifr_name;
1183 }
1184
1185 return Py_BuildValue("O&h", PyUnicode_DecodeFSDefault,
1186 ifname,
1187 a->can_family);
1188 }
1189#endif
1190
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001191#ifdef PF_SYSTEM
1192 case PF_SYSTEM:
1193 switch(proto) {
1194#ifdef SYSPROTO_CONTROL
1195 case SYSPROTO_CONTROL:
1196 {
1197 struct sockaddr_ctl *a = (struct sockaddr_ctl *)addr;
1198 return Py_BuildValue("(II)", a->sc_id, a->sc_unit);
1199 }
1200#endif
1201 default:
1202 PyErr_SetString(PyExc_ValueError,
1203 "Invalid address type");
1204 return 0;
1205 }
1206#endif
1207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 default:
1211 /* If we don't know the address family, don't raise an
1212 exception -- return it as an (int, bytes) tuple. */
1213 return Py_BuildValue("iy#",
1214 addr->sa_family,
1215 addr->sa_data,
1216 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001219}
1220
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001221/* Helper for getsockaddrarg: bypass IDNA for ASCII-only host names
1222 (in particular, numeric IP addresses). */
1223struct maybe_idna {
1224 PyObject *obj;
1225 char *buf;
1226};
1227
1228static void
1229idna_cleanup(struct maybe_idna *data)
1230{
1231 Py_CLEAR(data->obj);
1232}
1233
1234static int
1235idna_converter(PyObject *obj, struct maybe_idna *data)
1236{
1237 size_t len;
1238 PyObject *obj2, *obj3;
1239 if (obj == NULL) {
1240 idna_cleanup(data);
1241 return 1;
1242 }
1243 data->obj = NULL;
1244 len = -1;
1245 if (PyBytes_Check(obj)) {
1246 data->buf = PyBytes_AsString(obj);
1247 len = PyBytes_Size(obj);
1248 }
1249 else if (PyByteArray_Check(obj)) {
1250 data->buf = PyByteArray_AsString(obj);
1251 len = PyByteArray_Size(obj);
1252 }
1253 else if (PyUnicode_Check(obj) && PyUnicode_READY(obj) == 0 && PyUnicode_IS_COMPACT_ASCII(obj)) {
1254 data->buf = PyUnicode_DATA(obj);
1255 len = PyUnicode_GET_LENGTH(obj);
1256 }
1257 else {
1258 obj2 = PyUnicode_FromObject(obj);
1259 if (!obj2) {
1260 PyErr_Format(PyExc_TypeError, "string or unicode text buffer expected, not %s",
1261 obj->ob_type->tp_name);
1262 return 0;
1263 }
1264 obj3 = PyUnicode_AsEncodedString(obj2, "idna", NULL);
1265 Py_DECREF(obj2);
1266 if (!obj3) {
1267 PyErr_SetString(PyExc_TypeError, "encoding of hostname failed");
1268 return 0;
1269 }
1270 if (!PyBytes_Check(obj3)) {
Martin v. Löwis17fd1e12014-08-05 16:13:50 +02001271 Py_DECREF(obj3);
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001272 PyErr_SetString(PyExc_TypeError, "encoding of hostname failed to return bytes");
1273 return 0;
1274 }
1275 data->obj = obj3;
1276 data->buf = PyBytes_AS_STRING(obj3);
1277 len = PyBytes_GET_SIZE(obj3);
1278 }
1279 if (strlen(data->buf) != len) {
1280 Py_CLEAR(data->obj);
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001281 PyErr_SetString(PyExc_TypeError, "host name must not contain null character");
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001282 return 0;
1283 }
1284 return Py_CLEANUP_SUPPORTED;
Victor Stinner1a62a682014-08-17 19:33:28 +02001285}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001286
1287/* Parse a socket address argument according to the socket object's
1288 address family. Return 1 if the address was in the proper format,
1289 0 of not. The address is returned through addr_ret, its length
1290 through len_ret. */
1291
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001292static int
Guido van Rossum48a680c2001-03-02 06:34:14 +00001293getsockaddrarg(PySocketSockObject *s, PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 struct sockaddr *addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 switch (s->sock_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +00001297
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001298#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 case AF_UNIX:
1300 {
1301 struct sockaddr_un* addr;
1302 char *path;
1303 int len;
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001304 int retval = 0;
1305
1306 /* PEP 383. Not using PyUnicode_FSConverter since we need to
1307 allow embedded nulls on Linux. */
1308 if (PyUnicode_Check(args)) {
1309 if ((args = PyUnicode_EncodeFSDefault(args)) == NULL)
1310 return 0;
1311 }
1312 else
1313 Py_INCREF(args);
1314 if (!PyArg_Parse(args, "y#", &path, &len))
1315 goto unix_out;
Victor Stinner1a62a682014-08-17 19:33:28 +02001316 assert(len >= 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 addr = (struct sockaddr_un*)addr_ret;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001319#ifdef linux
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 if (len > 0 && path[0] == 0) {
1321 /* Linux abstract namespace extension */
Victor Stinner1a62a682014-08-17 19:33:28 +02001322 if ((size_t)len > sizeof addr->sun_path) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001323 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 "AF_UNIX path too long");
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001325 goto unix_out;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 }
1327 }
1328 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001329#endif /* linux */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 {
1331 /* regular NULL-terminated string */
Victor Stinner1a62a682014-08-17 19:33:28 +02001332 if ((size_t)len >= sizeof addr->sun_path) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001333 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 "AF_UNIX path too long");
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001335 goto unix_out;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 }
1337 addr->sun_path[len] = 0;
1338 }
1339 addr->sun_family = s->sock_family;
1340 memcpy(addr->sun_path, path, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 *len_ret = len + offsetof(struct sockaddr_un, sun_path);
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001342 retval = 1;
1343 unix_out:
1344 Py_DECREF(args);
1345 return retval;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001347#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001348
Martin v. Löwis11017b12006-01-14 18:12:57 +00001349#if defined(AF_NETLINK)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 case AF_NETLINK:
1351 {
1352 struct sockaddr_nl* addr;
1353 int pid, groups;
1354 addr = (struct sockaddr_nl *)addr_ret;
1355 if (!PyTuple_Check(args)) {
1356 PyErr_Format(
1357 PyExc_TypeError,
1358 "getsockaddrarg: "
1359 "AF_NETLINK address must be tuple, not %.500s",
1360 Py_TYPE(args)->tp_name);
1361 return 0;
1362 }
1363 if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
1364 return 0;
1365 addr->nl_family = AF_NETLINK;
1366 addr->nl_pid = pid;
1367 addr->nl_groups = groups;
1368 *len_ret = sizeof(*addr);
1369 return 1;
1370 }
Martin v. Löwis11017b12006-01-14 18:12:57 +00001371#endif
1372
Charles-François Natali10b8cf42011-11-10 19:21:37 +01001373#ifdef AF_RDS
1374 case AF_RDS:
1375 /* RDS sockets use sockaddr_in: fall-through */
1376#endif
1377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 case AF_INET:
1379 {
1380 struct sockaddr_in* addr;
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001381 struct maybe_idna host = {NULL, NULL};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 int port, result;
1383 if (!PyTuple_Check(args)) {
1384 PyErr_Format(
1385 PyExc_TypeError,
1386 "getsockaddrarg: "
1387 "AF_INET address must be tuple, not %.500s",
1388 Py_TYPE(args)->tp_name);
1389 return 0;
1390 }
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001391 if (!PyArg_ParseTuple(args, "O&i:getsockaddrarg",
1392 idna_converter, &host, &port))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 return 0;
1394 addr=(struct sockaddr_in*)addr_ret;
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001395 result = setipaddr(host.buf, (struct sockaddr *)addr,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 sizeof(*addr), AF_INET);
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001397 idna_cleanup(&host);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 if (result < 0)
1399 return 0;
1400 if (port < 0 || port > 0xffff) {
1401 PyErr_SetString(
1402 PyExc_OverflowError,
1403 "getsockaddrarg: port must be 0-65535.");
1404 return 0;
1405 }
1406 addr->sin_family = AF_INET;
1407 addr->sin_port = htons((short)port);
1408 *len_ret = sizeof *addr;
1409 return 1;
1410 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001411
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001412#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 case AF_INET6:
1414 {
1415 struct sockaddr_in6* addr;
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001416 struct maybe_idna host = {NULL, NULL};
Charles-François Natali366999a2012-01-02 15:47:29 +01001417 int port, result;
1418 unsigned int flowinfo, scope_id;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 flowinfo = scope_id = 0;
1420 if (!PyTuple_Check(args)) {
1421 PyErr_Format(
1422 PyExc_TypeError,
1423 "getsockaddrarg: "
1424 "AF_INET6 address must be tuple, not %.500s",
1425 Py_TYPE(args)->tp_name);
1426 return 0;
1427 }
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001428 if (!PyArg_ParseTuple(args, "O&i|II",
1429 idna_converter, &host, &port, &flowinfo,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 &scope_id)) {
1431 return 0;
1432 }
1433 addr = (struct sockaddr_in6*)addr_ret;
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001434 result = setipaddr(host.buf, (struct sockaddr *)addr,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 sizeof(*addr), AF_INET6);
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001436 idna_cleanup(&host);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 if (result < 0)
1438 return 0;
1439 if (port < 0 || port > 0xffff) {
1440 PyErr_SetString(
1441 PyExc_OverflowError,
1442 "getsockaddrarg: port must be 0-65535.");
1443 return 0;
1444 }
Charles-François Natali9b0c0062012-06-23 10:17:05 +02001445 if (flowinfo > 0xfffff) {
Charles-François Natali366999a2012-01-02 15:47:29 +01001446 PyErr_SetString(
1447 PyExc_OverflowError,
1448 "getsockaddrarg: flowinfo must be 0-1048575.");
1449 return 0;
1450 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 addr->sin6_family = s->sock_family;
1452 addr->sin6_port = htons((short)port);
Charles-François Natali366999a2012-01-02 15:47:29 +01001453 addr->sin6_flowinfo = htonl(flowinfo);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 addr->sin6_scope_id = scope_id;
1455 *len_ret = sizeof *addr;
1456 return 1;
1457 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001458#endif
1459
Hye-Shik Chang81268602004-02-02 06:05:24 +00001460#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 case AF_BLUETOOTH:
1462 {
1463 switch (s->sock_proto) {
1464 case BTPROTO_L2CAP:
1465 {
1466 struct sockaddr_l2 *addr;
1467 char *straddr;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 addr = (struct sockaddr_l2 *)addr_ret;
1470 memset(addr, 0, sizeof(struct sockaddr_l2));
1471 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
1472 if (!PyArg_ParseTuple(args, "si", &straddr,
1473 &_BT_L2_MEMB(addr, psm))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001474 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 "wrong format");
1476 return 0;
1477 }
1478 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
1479 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 *len_ret = sizeof *addr;
1482 return 1;
1483 }
1484 case BTPROTO_RFCOMM:
1485 {
1486 struct sockaddr_rc *addr;
1487 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 addr = (struct sockaddr_rc *)addr_ret;
1490 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
1491 if (!PyArg_ParseTuple(args, "si", &straddr,
1492 &_BT_RC_MEMB(addr, channel))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001493 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 "wrong format");
1495 return 0;
1496 }
1497 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
1498 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 *len_ret = sizeof *addr;
1501 return 1;
1502 }
1503 case BTPROTO_HCI:
1504 {
1505 struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001506#if defined(__NetBSD__) || defined(__DragonFly__)
Alexander Belopolskye239d232010-12-08 23:31:48 +00001507 char *straddr = PyBytes_AS_STRING(args);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001508
Alexander Belopolskye239d232010-12-08 23:31:48 +00001509 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001510 if (straddr == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001511 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001512 "wrong format");
1513 return 0;
1514 }
1515 if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0)
1516 return 0;
1517#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
1519 if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001520 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 "wrong format");
1522 return 0;
1523 }
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001524#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 *len_ret = sizeof *addr;
1526 return 1;
1527 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001528#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 case BTPROTO_SCO:
1530 {
1531 struct sockaddr_sco *addr;
1532 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 addr = (struct sockaddr_sco *)addr_ret;
1535 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
1536 if (!PyBytes_Check(args)) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001537 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 "wrong format");
1539 return 0;
1540 }
1541 straddr = PyBytes_AS_STRING(args);
1542 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
1543 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 *len_ret = sizeof *addr;
1546 return 1;
1547 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001548#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001550 PyErr_SetString(PyExc_OSError, "getsockaddrarg: unknown Bluetooth protocol");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 return 0;
1552 }
1553 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001554#endif
1555
Antoine Pitroub156a462010-10-27 20:13:57 +00001556#if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 case AF_PACKET:
1558 {
1559 struct sockaddr_ll* addr;
1560 struct ifreq ifr;
1561 char *interfaceName;
1562 int protoNumber;
1563 int hatype = 0;
1564 int pkttype = 0;
1565 char *haddr = NULL;
1566 unsigned int halen = 0;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 if (!PyTuple_Check(args)) {
1569 PyErr_Format(
1570 PyExc_TypeError,
1571 "getsockaddrarg: "
1572 "AF_PACKET address must be tuple, not %.500s",
1573 Py_TYPE(args)->tp_name);
1574 return 0;
1575 }
1576 if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName,
1577 &protoNumber, &pkttype, &hatype,
1578 &haddr, &halen))
1579 return 0;
1580 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1581 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
1582 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
1583 s->errorhandler();
1584 return 0;
1585 }
1586 if (halen > 8) {
1587 PyErr_SetString(PyExc_ValueError,
1588 "Hardware address must be 8 bytes or less");
1589 return 0;
1590 }
1591 if (protoNumber < 0 || protoNumber > 0xffff) {
1592 PyErr_SetString(
1593 PyExc_OverflowError,
1594 "getsockaddrarg: protoNumber must be 0-65535.");
1595 return 0;
1596 }
1597 addr = (struct sockaddr_ll*)addr_ret;
1598 addr->sll_family = AF_PACKET;
1599 addr->sll_protocol = htons((short)protoNumber);
1600 addr->sll_ifindex = ifr.ifr_ifindex;
1601 addr->sll_pkttype = pkttype;
1602 addr->sll_hatype = hatype;
1603 if (halen != 0) {
1604 memcpy(&addr->sll_addr, haddr, halen);
1605 }
1606 addr->sll_halen = halen;
1607 *len_ret = sizeof *addr;
1608 return 1;
1609 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001610#endif
1611
Christian Heimes043d6f62008-01-07 17:19:16 +00001612#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 case AF_TIPC:
1614 {
1615 unsigned int atype, v1, v2, v3;
1616 unsigned int scope = TIPC_CLUSTER_SCOPE;
1617 struct sockaddr_tipc *addr;
Christian Heimes043d6f62008-01-07 17:19:16 +00001618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 if (!PyTuple_Check(args)) {
1620 PyErr_Format(
1621 PyExc_TypeError,
1622 "getsockaddrarg: "
1623 "AF_TIPC address must be tuple, not %.500s",
1624 Py_TYPE(args)->tp_name);
1625 return 0;
1626 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 if (!PyArg_ParseTuple(args,
1629 "IIII|I;Invalid TIPC address format",
1630 &atype, &v1, &v2, &v3, &scope))
1631 return 0;
Christian Heimes043d6f62008-01-07 17:19:16 +00001632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 addr = (struct sockaddr_tipc *) addr_ret;
1634 memset(addr, 0, sizeof(struct sockaddr_tipc));
Christian Heimes043d6f62008-01-07 17:19:16 +00001635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 addr->family = AF_TIPC;
1637 addr->scope = scope;
1638 addr->addrtype = atype;
Christian Heimes043d6f62008-01-07 17:19:16 +00001639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 if (atype == TIPC_ADDR_NAMESEQ) {
1641 addr->addr.nameseq.type = v1;
1642 addr->addr.nameseq.lower = v2;
1643 addr->addr.nameseq.upper = v3;
1644 } else if (atype == TIPC_ADDR_NAME) {
1645 addr->addr.name.name.type = v1;
1646 addr->addr.name.name.instance = v2;
1647 } else if (atype == TIPC_ADDR_ID) {
1648 addr->addr.id.node = v1;
1649 addr->addr.id.ref = v2;
1650 } else {
1651 /* Shouldn't happen */
1652 PyErr_SetString(PyExc_TypeError, "Invalid address type");
1653 return 0;
1654 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 *len_ret = sizeof(*addr);
Christian Heimes043d6f62008-01-07 17:19:16 +00001657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 return 1;
1659 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001660#endif
1661
Vinay Sajiped6783f2014-03-21 11:44:32 +00001662#if defined(AF_CAN) && defined(CAN_RAW) && defined(CAN_BCM)
Charles-François Natali47413c12011-10-06 19:47:44 +02001663 case AF_CAN:
1664 switch (s->sock_proto) {
1665 case CAN_RAW:
Charles-François Natali773e42d2013-02-05 19:42:01 +01001666 /* fall-through */
1667 case CAN_BCM:
Charles-François Natali47413c12011-10-06 19:47:44 +02001668 {
1669 struct sockaddr_can *addr;
1670 PyObject *interfaceName;
1671 struct ifreq ifr;
Charles-François Natali47413c12011-10-06 19:47:44 +02001672 Py_ssize_t len;
1673
Benjamin Peterson18b71912013-05-16 15:29:44 -05001674 addr = (struct sockaddr_can *)addr_ret;
1675
Charles-François Natali47413c12011-10-06 19:47:44 +02001676 if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter,
1677 &interfaceName))
1678 return 0;
1679
1680 len = PyBytes_GET_SIZE(interfaceName);
1681
1682 if (len == 0) {
1683 ifr.ifr_ifindex = 0;
Victor Stinner1a62a682014-08-17 19:33:28 +02001684 } else if ((size_t)len < sizeof(ifr.ifr_name)) {
Christian Heimes15b68852012-09-10 01:25:50 +02001685 strncpy(ifr.ifr_name, PyBytes_AS_STRING(interfaceName), sizeof(ifr.ifr_name));
1686 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Charles-François Natali47413c12011-10-06 19:47:44 +02001687 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
1688 s->errorhandler();
1689 Py_DECREF(interfaceName);
1690 return 0;
1691 }
1692 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001693 PyErr_SetString(PyExc_OSError,
Charles-François Natali47413c12011-10-06 19:47:44 +02001694 "AF_CAN interface name too long");
1695 Py_DECREF(interfaceName);
1696 return 0;
1697 }
1698
1699 addr->can_family = AF_CAN;
1700 addr->can_ifindex = ifr.ifr_ifindex;
1701
1702 *len_ret = sizeof(*addr);
1703 Py_DECREF(interfaceName);
1704 return 1;
1705 }
1706 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001707 PyErr_SetString(PyExc_OSError,
Charles-François Natali47413c12011-10-06 19:47:44 +02001708 "getsockaddrarg: unsupported CAN protocol");
1709 return 0;
1710 }
1711#endif
Victor Stinnera534fc42013-06-03 22:07:27 +02001712
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001713#ifdef PF_SYSTEM
1714 case PF_SYSTEM:
1715 switch (s->sock_proto) {
1716#ifdef SYSPROTO_CONTROL
1717 case SYSPROTO_CONTROL:
1718 {
1719 struct sockaddr_ctl *addr;
Victor Stinnera534fc42013-06-03 22:07:27 +02001720
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001721 addr = (struct sockaddr_ctl *)addr_ret;
1722 addr->sc_family = AF_SYSTEM;
Victor Stinnera534fc42013-06-03 22:07:27 +02001723 addr->ss_sysaddr = AF_SYS_CONTROL;
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001724
1725 if (PyUnicode_Check(args)) {
1726 struct ctl_info info;
1727 PyObject *ctl_name;
1728
1729 if (!PyArg_Parse(args, "O&",
1730 PyUnicode_FSConverter, &ctl_name)) {
1731 return 0;
1732 }
1733
1734 if (PyBytes_GET_SIZE(ctl_name) > sizeof(info.ctl_name)) {
1735 PyErr_SetString(PyExc_ValueError,
1736 "provided string is too long");
1737 Py_DECREF(ctl_name);
1738 return 0;
1739 }
1740 strncpy(info.ctl_name, PyBytes_AS_STRING(ctl_name),
1741 sizeof(info.ctl_name));
1742 Py_DECREF(ctl_name);
1743
1744 if (ioctl(s->sock_fd, CTLIOCGINFO, &info)) {
1745 PyErr_SetString(PyExc_OSError,
1746 "cannot find kernel control with provided name");
1747 return 0;
1748 }
Victor Stinnera534fc42013-06-03 22:07:27 +02001749
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001750 addr->sc_id = info.ctl_id;
1751 addr->sc_unit = 0;
1752 } else if (!PyArg_ParseTuple(args, "II",
1753 &(addr->sc_id), &(addr->sc_unit))) {
1754 PyErr_SetString(PyExc_TypeError, "getsockaddrarg: "
1755 "expected str or tuple of two ints");
Victor Stinnera534fc42013-06-03 22:07:27 +02001756
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001757 return 0;
1758 }
Victor Stinnera534fc42013-06-03 22:07:27 +02001759
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001760 *len_ret = sizeof(*addr);
1761 return 1;
1762 }
1763#endif
1764 default:
1765 PyErr_SetString(PyExc_OSError,
1766 "getsockaddrarg: unsupported PF_SYSTEM protocol");
1767 return 0;
1768 }
1769#endif
Charles-François Natali47413c12011-10-06 19:47:44 +02001770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001774 PyErr_SetString(PyExc_OSError, "getsockaddrarg: bad family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 return 0;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001778}
1779
Guido van Rossum30a685f1991-06-27 15:51:29 +00001780
Guido van Rossum48a680c2001-03-02 06:34:14 +00001781/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00001782 Return 1 if the family is known, 0 otherwise. The length is returned
1783 through len_ret. */
1784
1785static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00001786getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 switch (s->sock_family) {
Guido van Rossum710e1df1992-06-12 10:39:36 +00001789
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001790#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 case AF_UNIX:
1792 {
1793 *len_ret = sizeof (struct sockaddr_un);
1794 return 1;
1795 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001796#endif /* AF_UNIX */
Martin v. Löwis11017b12006-01-14 18:12:57 +00001797#if defined(AF_NETLINK)
1798 case AF_NETLINK:
1799 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 *len_ret = sizeof (struct sockaddr_nl);
1801 return 1;
Martin v. Löwis11017b12006-01-14 18:12:57 +00001802 }
1803#endif
Guido van Rossum710e1df1992-06-12 10:39:36 +00001804
Charles-François Natali10b8cf42011-11-10 19:21:37 +01001805#ifdef AF_RDS
1806 case AF_RDS:
1807 /* RDS sockets use sockaddr_in: fall-through */
1808#endif
1809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 case AF_INET:
1811 {
1812 *len_ret = sizeof (struct sockaddr_in);
1813 return 1;
1814 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00001815
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001816#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 case AF_INET6:
1818 {
1819 *len_ret = sizeof (struct sockaddr_in6);
1820 return 1;
1821 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001822#endif
1823
Hye-Shik Chang81268602004-02-02 06:05:24 +00001824#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 case AF_BLUETOOTH:
1826 {
1827 switch(s->sock_proto)
1828 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 case BTPROTO_L2CAP:
1831 *len_ret = sizeof (struct sockaddr_l2);
1832 return 1;
1833 case BTPROTO_RFCOMM:
1834 *len_ret = sizeof (struct sockaddr_rc);
1835 return 1;
1836 case BTPROTO_HCI:
1837 *len_ret = sizeof (struct sockaddr_hci);
1838 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001839#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 case BTPROTO_SCO:
1841 *len_ret = sizeof (struct sockaddr_sco);
1842 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001843#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001845 PyErr_SetString(PyExc_OSError, "getsockaddrlen: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 "unknown BT protocol");
1847 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 }
1850 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001851#endif
1852
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001853#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 case AF_PACKET:
1855 {
1856 *len_ret = sizeof (struct sockaddr_ll);
1857 return 1;
1858 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001859#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001860
Christian Heimes043d6f62008-01-07 17:19:16 +00001861#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 case AF_TIPC:
1863 {
1864 *len_ret = sizeof (struct sockaddr_tipc);
1865 return 1;
1866 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001867#endif
1868
Vinay Sajipecfc98c2014-03-20 12:42:42 +00001869#ifdef AF_CAN
Charles-François Natali47413c12011-10-06 19:47:44 +02001870 case AF_CAN:
1871 {
1872 *len_ret = sizeof (struct sockaddr_can);
1873 return 1;
1874 }
1875#endif
Victor Stinnera534fc42013-06-03 22:07:27 +02001876
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001877#ifdef PF_SYSTEM
1878 case PF_SYSTEM:
1879 switch(s->sock_proto) {
1880#ifdef SYSPROTO_CONTROL
1881 case SYSPROTO_CONTROL:
1882 *len_ret = sizeof (struct sockaddr_ctl);
1883 return 1;
1884#endif
1885 default:
1886 PyErr_SetString(PyExc_OSError, "getsockaddrlen: "
1887 "unknown PF_SYSTEM protocol");
1888 return 0;
1889 }
1890#endif
Charles-François Natali47413c12011-10-06 19:47:44 +02001891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 /* More cases here... */
Guido van Rossum710e1df1992-06-12 10:39:36 +00001893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001895 PyErr_SetString(PyExc_OSError, "getsockaddrlen: bad family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 return 0;
Guido van Rossum710e1df1992-06-12 10:39:36 +00001897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00001899}
1900
1901
Nick Coghlan96fe56a2011-08-22 11:55:57 +10001902/* Support functions for the sendmsg() and recvmsg[_into]() methods.
1903 Currently, these methods are only compiled if the RFC 2292/3542
1904 CMSG_LEN() macro is available. Older systems seem to have used
1905 sizeof(struct cmsghdr) + (length) where CMSG_LEN() is used now, so
1906 it may be possible to define CMSG_LEN() that way if it's not
1907 provided. Some architectures might need extra padding after the
1908 cmsghdr, however, and CMSG_LEN() would have to take account of
1909 this. */
1910#ifdef CMSG_LEN
1911/* If length is in range, set *result to CMSG_LEN(length) and return
1912 true; otherwise, return false. */
1913static int
1914get_CMSG_LEN(size_t length, size_t *result)
1915{
1916 size_t tmp;
1917
1918 if (length > (SOCKLEN_T_LIMIT - CMSG_LEN(0)))
1919 return 0;
1920 tmp = CMSG_LEN(length);
1921 if (tmp > SOCKLEN_T_LIMIT || tmp < length)
1922 return 0;
1923 *result = tmp;
1924 return 1;
1925}
1926
1927#ifdef CMSG_SPACE
1928/* If length is in range, set *result to CMSG_SPACE(length) and return
1929 true; otherwise, return false. */
1930static int
1931get_CMSG_SPACE(size_t length, size_t *result)
1932{
1933 size_t tmp;
1934
1935 /* Use CMSG_SPACE(1) here in order to take account of the padding
1936 necessary before *and* after the data. */
1937 if (length > (SOCKLEN_T_LIMIT - CMSG_SPACE(1)))
1938 return 0;
1939 tmp = CMSG_SPACE(length);
1940 if (tmp > SOCKLEN_T_LIMIT || tmp < length)
1941 return 0;
1942 *result = tmp;
1943 return 1;
1944}
1945#endif
1946
1947/* Return true iff msg->msg_controllen is valid, cmsgh is a valid
1948 pointer in msg->msg_control with at least "space" bytes after it,
1949 and its cmsg_len member inside the buffer. */
1950static int
1951cmsg_min_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t space)
1952{
1953 size_t cmsg_offset;
1954 static const size_t cmsg_len_end = (offsetof(struct cmsghdr, cmsg_len) +
1955 sizeof(cmsgh->cmsg_len));
1956
Charles-François Natali466517d2011-08-28 18:23:43 +02001957 /* Note that POSIX allows msg_controllen to be of signed type. */
Brett Cannonb05cbe62014-01-07 17:01:01 -05001958 if (cmsgh == NULL || msg->msg_control == NULL)
Nick Coghlan96fe56a2011-08-22 11:55:57 +10001959 return 0;
Brett Cannonb05cbe62014-01-07 17:01:01 -05001960 /* Note that POSIX allows msg_controllen to be of a signed type. This is
1961 annoying under OS X as it's unsigned there and so it triggers a
1962 tautological comparison warning under Clang when compared against 0.
1963 Since the check is valid on other platforms, silence the warning under
1964 Clang. */
1965 #ifdef __clang__
1966 #pragma clang diagnostic push
1967 #pragma clang diagnostic ignored "-Wtautological-compare"
1968 #endif
Serhiy Storchakacaa01f82015-03-05 23:42:24 +02001969 #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))
Serhiy Storchakab48af342015-02-26 15:27:57 +02001970 #pragma GCC diagnostic push
1971 #pragma GCC diagnostic ignored "-Wtype-limits"
1972 #endif
Brett Cannonb05cbe62014-01-07 17:01:01 -05001973 if (msg->msg_controllen < 0)
1974 return 0;
Serhiy Storchakacaa01f82015-03-05 23:42:24 +02001975 #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))
Serhiy Storchakab48af342015-02-26 15:27:57 +02001976 #pragma GCC diagnostic pop
1977 #endif
Brett Cannonb05cbe62014-01-07 17:01:01 -05001978 #ifdef __clang__
1979 #pragma clang diagnostic pop
1980 #endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10001981 if (space < cmsg_len_end)
1982 space = cmsg_len_end;
1983 cmsg_offset = (char *)cmsgh - (char *)msg->msg_control;
1984 return (cmsg_offset <= (size_t)-1 - space &&
1985 cmsg_offset + space <= msg->msg_controllen);
1986}
1987
1988/* If pointer CMSG_DATA(cmsgh) is in buffer msg->msg_control, set
1989 *space to number of bytes following it in the buffer and return
1990 true; otherwise, return false. Assumes cmsgh, msg->msg_control and
1991 msg->msg_controllen are valid. */
1992static int
1993get_cmsg_data_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *space)
1994{
1995 size_t data_offset;
1996 char *data_ptr;
1997
1998 if ((data_ptr = (char *)CMSG_DATA(cmsgh)) == NULL)
1999 return 0;
2000 data_offset = data_ptr - (char *)msg->msg_control;
2001 if (data_offset > msg->msg_controllen)
2002 return 0;
2003 *space = msg->msg_controllen - data_offset;
2004 return 1;
2005}
2006
2007/* If cmsgh is invalid or not contained in the buffer pointed to by
2008 msg->msg_control, return -1. If cmsgh is valid and its associated
2009 data is entirely contained in the buffer, set *data_len to the
2010 length of the associated data and return 0. If only part of the
2011 associated data is contained in the buffer but cmsgh is otherwise
2012 valid, set *data_len to the length contained in the buffer and
2013 return 1. */
2014static int
2015get_cmsg_data_len(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *data_len)
2016{
2017 size_t space, cmsg_data_len;
2018
2019 if (!cmsg_min_space(msg, cmsgh, CMSG_LEN(0)) ||
2020 cmsgh->cmsg_len < CMSG_LEN(0))
2021 return -1;
2022 cmsg_data_len = cmsgh->cmsg_len - CMSG_LEN(0);
2023 if (!get_cmsg_data_space(msg, cmsgh, &space))
2024 return -1;
2025 if (space >= cmsg_data_len) {
2026 *data_len = cmsg_data_len;
2027 return 0;
2028 }
2029 *data_len = space;
2030 return 1;
2031}
2032#endif /* CMSG_LEN */
2033
2034
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002035/* s._accept() -> (fd, address) */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002036
Guido van Rossum73624e91994-10-10 17:59:00 +00002037static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002038sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040 sock_addr_t addrbuf;
2041 SOCKET_T newfd = INVALID_SOCKET;
2042 socklen_t addrlen;
2043 PyObject *sock = NULL;
2044 PyObject *addr = NULL;
2045 PyObject *res = NULL;
2046 int timeout;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002047 int async_err = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02002048#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
2049 /* accept4() is available on Linux 2.6.28+ and glibc 2.10 */
2050 static int accept4_works = -1;
2051#endif
2052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 if (!getsockaddrlen(s, &addrlen))
2054 return NULL;
2055 memset(&addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 if (!IS_SELECTABLE(s))
2058 return select_error();
Neal Norwitz082b2df2006-02-07 07:04:46 +00002059
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002060 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002061 do {
2062 Py_BEGIN_ALLOW_THREADS
2063 timeout = internal_select_ex(s, 0, interval);
2064 if (!timeout) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02002065#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002066 if (accept4_works != 0) {
2067 newfd = accept4(s->sock_fd, SAS2SA(&addrbuf), &addrlen,
2068 SOCK_CLOEXEC);
2069 if (newfd == INVALID_SOCKET && accept4_works == -1) {
2070 /* On Linux older than 2.6.28, accept4() fails with ENOSYS */
2071 accept4_works = (errno != ENOSYS);
2072 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02002073 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002074 if (accept4_works == 0)
2075 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002076#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002077 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002078#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002079 }
2080 Py_END_ALLOW_THREADS
2081 } while (newfd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 if (timeout == 1) {
2083 PyErr_SetString(socket_timeout, "timed out");
2084 return NULL;
2085 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002086 END_SELECT_LOOP(s)
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 if (newfd == INVALID_SOCKET)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002089 return (!async_err) ? s->errorhandler() : NULL;
Barry Warsaw752300b1997-01-03 17:18:10 +00002090
Victor Stinnerdaf45552013-08-28 00:53:59 +02002091#ifdef MS_WINDOWS
2092 if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) {
2093 PyErr_SetFromWindowsErr(0);
2094 SOCKETCLOSE(newfd);
2095 goto finally;
2096 }
2097#else
2098
2099#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
2100 if (!accept4_works)
2101#endif
2102 {
2103 if (_Py_set_inheritable(newfd, 0, NULL) < 0) {
2104 SOCKETCLOSE(newfd);
2105 goto finally;
2106 }
2107 }
2108#endif
2109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 sock = PyLong_FromSocket_t(newfd);
2111 if (sock == NULL) {
2112 SOCKETCLOSE(newfd);
2113 goto finally;
2114 }
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
2117 addrlen, s->sock_proto);
2118 if (addr == NULL)
2119 goto finally;
Barry Warsaw752300b1997-01-03 17:18:10 +00002120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 res = PyTuple_Pack(2, sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00002122
Guido van Rossum67f7a382002-06-06 21:08:16 +00002123finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 Py_XDECREF(sock);
2125 Py_XDECREF(addr);
2126 return res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002127}
2128
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002129PyDoc_STRVAR(accept_doc,
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002130"_accept() -> (integer, address info)\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002131\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002132Wait for an incoming connection. Return a new socket file descriptor\n\
2133representing the connection, and the address of the client.\n\
2134For IP sockets, the address info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002135
Guido van Rossum11ba0942002-06-13 15:07:44 +00002136/* s.setblocking(flag) method. Argument:
2137 False -- non-blocking mode; same as settimeout(0)
2138 True -- blocking mode; same as settimeout(None)
2139*/
Guido van Rossume4485b01994-09-07 14:32:49 +00002140
Guido van Rossum73624e91994-10-10 17:59:00 +00002141static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002142sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00002143{
Serhiy Storchaka78980432013-01-15 01:12:17 +02002144 long block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 block = PyLong_AsLong(arg);
2147 if (block == -1 && PyErr_Occurred())
2148 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 s->sock_timeout = block ? -1.0 : 0.0;
2151 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00002152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 Py_INCREF(Py_None);
2154 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00002155}
Guido van Rossume4485b01994-09-07 14:32:49 +00002156
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002157PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002158"setblocking(flag)\n\
2159\n\
2160Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00002161setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002162setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002163
Guido van Rossum11ba0942002-06-13 15:07:44 +00002164/* s.settimeout(timeout) method. Argument:
2165 None -- no timeout, blocking mode; same as setblocking(True)
2166 0.0 -- non-blocking mode; same as setblocking(False)
2167 > 0 -- timeout mode; operations time out after timeout seconds
2168 < 0 -- illegal; raises an exception
2169*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00002170static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002171sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00002172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 double timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 if (arg == Py_None)
2176 timeout = -1.0;
2177 else {
2178 timeout = PyFloat_AsDouble(arg);
2179 if (timeout < 0.0) {
2180 if (!PyErr_Occurred())
2181 PyErr_SetString(PyExc_ValueError,
2182 "Timeout value out of range");
2183 return NULL;
2184 }
2185 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 s->sock_timeout = timeout;
2188 internal_setblocking(s, timeout < 0.0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 Py_INCREF(Py_None);
2191 return Py_None;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002192}
2193
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002194PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002195"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00002196\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00002197Set a timeout on socket operations. 'timeout' can be a float,\n\
2198giving in seconds, or None. Setting a timeout of None disables\n\
2199the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002200Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00002201
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002202/* s.gettimeout() method.
2203 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00002204static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002205sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00002206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 if (s->sock_timeout < 0.0) {
2208 Py_INCREF(Py_None);
2209 return Py_None;
2210 }
2211 else
2212 return PyFloat_FromDouble(s->sock_timeout);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002213}
2214
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002215PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002216"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00002217\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03002218Returns the timeout in seconds (float) associated with socket \n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00002219operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002220operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00002221
Guido van Rossumaee08791992-09-08 09:05:33 +00002222/* s.setsockopt() method.
2223 With an integer third argument, sets an integer option.
2224 With a string third argument, sets an option from a buffer;
2225 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002226
Guido van Rossum73624e91994-10-10 17:59:00 +00002227static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002228sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 int level;
2231 int optname;
2232 int res;
2233 char *buf;
2234 int buflen;
2235 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 if (PyArg_ParseTuple(args, "iii:setsockopt",
2238 &level, &optname, &flag)) {
2239 buf = (char *) &flag;
2240 buflen = sizeof flag;
2241 }
2242 else {
2243 PyErr_Clear();
2244 if (!PyArg_ParseTuple(args, "iiy#:setsockopt",
2245 &level, &optname, &buf, &buflen))
2246 return NULL;
2247 }
2248 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
2249 if (res < 0)
2250 return s->errorhandler();
2251 Py_INCREF(Py_None);
2252 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002253}
2254
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002255PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002256"setsockopt(level, option, value)\n\
2257\n\
2258Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002259The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002260
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002261
Guido van Rossumaee08791992-09-08 09:05:33 +00002262/* s.getsockopt() method.
2263 With two arguments, retrieves an integer option.
2264 With a third integer argument, retrieves a string buffer of that size;
2265 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002266
Guido van Rossum73624e91994-10-10 17:59:00 +00002267static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002268sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 int level;
2271 int optname;
2272 int res;
2273 PyObject *buf;
2274 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
2277 &level, &optname, &buflen))
2278 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 if (buflen == 0) {
2281 int flag = 0;
2282 socklen_t flagsize = sizeof flag;
2283 res = getsockopt(s->sock_fd, level, optname,
2284 (void *)&flag, &flagsize);
2285 if (res < 0)
2286 return s->errorhandler();
2287 return PyLong_FromLong(flag);
2288 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 if (buflen <= 0 || buflen > 1024) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002290 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 "getsockopt buflen out of range");
2292 return NULL;
2293 }
2294 buf = PyBytes_FromStringAndSize((char *)NULL, buflen);
2295 if (buf == NULL)
2296 return NULL;
2297 res = getsockopt(s->sock_fd, level, optname,
2298 (void *)PyBytes_AS_STRING(buf), &buflen);
2299 if (res < 0) {
2300 Py_DECREF(buf);
2301 return s->errorhandler();
2302 }
2303 _PyBytes_Resize(&buf, buflen);
2304 return buf;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002305}
2306
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002307PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002308"getsockopt(level, option[, buffersize]) -> value\n\
2309\n\
2310Get a socket option. See the Unix manual for level and option.\n\
2311If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002312string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002313
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002314
Fred Drake728819a2000-07-01 03:40:12 +00002315/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002316
Guido van Rossum73624e91994-10-10 17:59:00 +00002317static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002318sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 sock_addr_t addrbuf;
2321 int addrlen;
2322 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2325 return NULL;
2326 Py_BEGIN_ALLOW_THREADS
2327 res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);
2328 Py_END_ALLOW_THREADS
2329 if (res < 0)
2330 return s->errorhandler();
2331 Py_INCREF(Py_None);
2332 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002333}
2334
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002335PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002336"bind(address)\n\
2337\n\
2338Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00002339pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002340sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002341
Guido van Rossum30a685f1991-06-27 15:51:29 +00002342
2343/* s.close() method.
2344 Set the file descriptor to -1 so operations tried subsequently
2345 will surely fail. */
2346
Guido van Rossum73624e91994-10-10 17:59:00 +00002347static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002348sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002351
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002352 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
2353 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
2354 * for more details.
2355 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 if ((fd = s->sock_fd) != -1) {
2357 s->sock_fd = -1;
2358 Py_BEGIN_ALLOW_THREADS
2359 (void) SOCKETCLOSE(fd);
2360 Py_END_ALLOW_THREADS
2361 }
2362 Py_INCREF(Py_None);
2363 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002364}
2365
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002366PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002367"close()\n\
2368\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002369Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002370
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002371static PyObject *
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002372sock_detach(PySocketSockObject *s)
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002373{
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002374 SOCKET_T fd = s->sock_fd;
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002375 s->sock_fd = -1;
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002376 return PyLong_FromSocket_t(fd);
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002377}
2378
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002379PyDoc_STRVAR(detach_doc,
2380"detach()\n\
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002381\n\
Guido van Rossum638aebd2013-12-04 11:50:09 -08002382Close the socket object without closing the underlying file descriptor.\n\
2383The object cannot be used after this call, but the file descriptor\n\
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002384can be reused for other purposes. The file descriptor is returned.");
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002385
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002386static int
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002387internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 int *timeoutp)
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 int res, timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 timeout = 0;
2393 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002394
2395#ifdef MS_WINDOWS
2396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 if (s->sock_timeout > 0.0) {
2398 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK &&
2399 IS_SELECTABLE(s)) {
2400 /* This is a mess. Best solution: trust select */
2401 fd_set fds;
2402 fd_set fds_exc;
2403 struct timeval tv;
2404 tv.tv_sec = (int)s->sock_timeout;
2405 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
2406 FD_ZERO(&fds);
2407 FD_SET(s->sock_fd, &fds);
2408 FD_ZERO(&fds_exc);
2409 FD_SET(s->sock_fd, &fds_exc);
Antoine Pitrou19467d22010-08-17 19:33:30 +00002410 res = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
2411 NULL, &fds, &fds_exc, &tv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 if (res == 0) {
2413 res = WSAEWOULDBLOCK;
2414 timeout = 1;
2415 } else if (res > 0) {
2416 if (FD_ISSET(s->sock_fd, &fds))
2417 /* The socket is in the writable set - this
2418 means connected */
2419 res = 0;
2420 else {
2421 /* As per MS docs, we need to call getsockopt()
2422 to get the underlying error */
2423 int res_size = sizeof res;
2424 /* It must be in the exception set */
2425 assert(FD_ISSET(s->sock_fd, &fds_exc));
2426 if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
2427 (char *)&res, &res_size))
2428 /* getsockopt also clears WSAGetLastError,
2429 so reset it back. */
2430 WSASetLastError(res);
2431 else
2432 res = WSAGetLastError();
2433 }
2434 }
2435 /* else if (res < 0) an error occurred */
2436 }
2437 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 if (res < 0)
2440 res = WSAGetLastError();
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002441
2442#else
2443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 if (s->sock_timeout > 0.0) {
2445 if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) {
2446 timeout = internal_select(s, 1);
2447 if (timeout == 0) {
2448 /* Bug #1019808: in case of an EINPROGRESS,
2449 use getsockopt(SO_ERROR) to get the real
2450 error. */
2451 socklen_t res_size = sizeof res;
2452 (void)getsockopt(s->sock_fd, SOL_SOCKET,
2453 SO_ERROR, &res, &res_size);
2454 if (res == EISCONN)
2455 res = 0;
2456 errno = res;
2457 }
2458 else if (timeout == -1) {
2459 res = errno; /* had error */
2460 }
2461 else
2462 res = EWOULDBLOCK; /* timed out */
2463 }
2464 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 if (res < 0)
2467 res = errno;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002468
2469#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 *timeoutp = timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 return res;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002473}
Guido van Rossum30a685f1991-06-27 15:51:29 +00002474
Fred Drake728819a2000-07-01 03:40:12 +00002475/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002476
Guido van Rossum73624e91994-10-10 17:59:00 +00002477static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002478sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 sock_addr_t addrbuf;
2481 int addrlen;
2482 int res;
2483 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2486 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 Py_BEGIN_ALLOW_THREADS
2489 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
2490 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 if (timeout == 1) {
2493 PyErr_SetString(socket_timeout, "timed out");
2494 return NULL;
2495 }
2496 if (res != 0)
2497 return s->errorhandler();
2498 Py_INCREF(Py_None);
2499 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002500}
2501
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002502PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002503"connect(address)\n\
2504\n\
2505Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002506is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002507
Guido van Rossum30a685f1991-06-27 15:51:29 +00002508
Fred Drake728819a2000-07-01 03:40:12 +00002509/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002510
2511static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002512sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 sock_addr_t addrbuf;
2515 int addrlen;
2516 int res;
2517 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2520 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 Py_BEGIN_ALLOW_THREADS
2523 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
2524 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 /* Signals are not errors (though they may raise exceptions). Adapted
2527 from PyErr_SetFromErrnoWithFilenameObject(). */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 if (res == EINTR && PyErr_CheckSignals())
2529 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 return PyLong_FromLong((long) res);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002532}
2533
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002534PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002535"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002536\n\
2537This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002538instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002539
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002540
Guido van Rossumed233a51992-06-23 09:07:03 +00002541/* s.fileno() method */
2542
Guido van Rossum73624e91994-10-10 17:59:00 +00002543static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002544sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00002545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 return PyLong_FromSocket_t(s->sock_fd);
Guido van Rossumed233a51992-06-23 09:07:03 +00002547}
2548
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002549PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002550"fileno() -> integer\n\
2551\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002552Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002553
Guido van Rossumed233a51992-06-23 09:07:03 +00002554
Guido van Rossumc89705d1992-11-26 08:54:07 +00002555/* s.getsockname() method */
2556
Guido van Rossum73624e91994-10-10 17:59:00 +00002557static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002558sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002560 sock_addr_t addrbuf;
2561 int res;
2562 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002564 if (!getsockaddrlen(s, &addrlen))
2565 return NULL;
2566 memset(&addrbuf, 0, addrlen);
2567 Py_BEGIN_ALLOW_THREADS
2568 res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2569 Py_END_ALLOW_THREADS
2570 if (res < 0)
2571 return s->errorhandler();
2572 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2573 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002574}
2575
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002576PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002577"getsockname() -> address info\n\
2578\n\
2579Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002580info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002581
Guido van Rossumc89705d1992-11-26 08:54:07 +00002582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002584/* s.getpeername() method */
2585
Guido van Rossum73624e91994-10-10 17:59:00 +00002586static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002587sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 sock_addr_t addrbuf;
2590 int res;
2591 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 if (!getsockaddrlen(s, &addrlen))
2594 return NULL;
2595 memset(&addrbuf, 0, addrlen);
2596 Py_BEGIN_ALLOW_THREADS
2597 res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2598 Py_END_ALLOW_THREADS
2599 if (res < 0)
2600 return s->errorhandler();
2601 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2602 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002603}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002604
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002605PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002606"getpeername() -> address info\n\
2607\n\
2608Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002609info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002610
Guido van Rossumb6775db1994-08-01 11:34:53 +00002611#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002612
2613
Guido van Rossum30a685f1991-06-27 15:51:29 +00002614/* s.listen(n) method */
2615
Guido van Rossum73624e91994-10-10 17:59:00 +00002616static PyObject *
Charles-François Natali644b8f52014-05-22 19:45:39 +01002617sock_listen(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002618{
Charles-François Natali644b8f52014-05-22 19:45:39 +01002619 /* We try to choose a default backlog high enough to avoid connection drops
2620 * for common workloads, yet not too high to limit resource usage. */
2621 int backlog = Py_MIN(SOMAXCONN, 128);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002623
Charles-François Natali644b8f52014-05-22 19:45:39 +01002624 if (!PyArg_ParseTuple(args, "|i:listen", &backlog))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 return NULL;
Charles-François Natali644b8f52014-05-22 19:45:39 +01002626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou1be815a2011-05-10 19:16:29 +02002628 /* To avoid problems on systems that don't allow a negative backlog
2629 * (which doesn't make sense anyway) we force a minimum value of 0. */
2630 if (backlog < 0)
2631 backlog = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 res = listen(s->sock_fd, backlog);
2633 Py_END_ALLOW_THREADS
2634 if (res < 0)
2635 return s->errorhandler();
2636 Py_INCREF(Py_None);
2637 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002638}
2639
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002640PyDoc_STRVAR(listen_doc,
Charles-François Natali644b8f52014-05-22 19:45:39 +01002641"listen([backlog])\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002642\n\
Charles-François Natali644b8f52014-05-22 19:45:39 +01002643Enable a server to accept connections. If backlog is specified, it must be\n\
2644at least 0 (if it is lower, it is set to 0); it specifies the number of\n\
Antoine Pitrou1be815a2011-05-10 19:16:29 +02002645unaccepted connections that the system will allow before refusing new\n\
Charles-François Natali644b8f52014-05-22 19:45:39 +01002646connections. If not specified, a default reasonable value is chosen.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002647
2648
Thomas Wouters477c8d52006-05-27 19:21:47 +00002649/*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002650 * This is the guts of the recv() and recv_into() methods, which reads into a
Thomas Wouters902d6eb2007-01-09 23:18:33 +00002651 * char buffer. If you have any inc/dec ref to do to the objects that contain
Thomas Wouters477c8d52006-05-27 19:21:47 +00002652 * the buffer, do it in the caller. This function returns the number of bytes
Ezio Melotti13925002011-03-16 11:05:33 +02002653 * successfully read. If there was an error, it returns -1. Note that it is
Thomas Wouters477c8d52006-05-27 19:21:47 +00002654 * also possible that we return a number of bytes smaller than the request
2655 * bytes.
2656 */
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002657
Antoine Pitrou19467d22010-08-17 19:33:30 +00002658static Py_ssize_t
2659sock_recv_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002660{
Antoine Pitrou19467d22010-08-17 19:33:30 +00002661 Py_ssize_t outlen = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 int timeout;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002663 int async_err = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 if (!IS_SELECTABLE(s)) {
2666 select_error();
2667 return -1;
2668 }
2669 if (len == 0) {
2670 /* If 0 bytes were requested, do nothing. */
2671 return 0;
2672 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002673
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002674 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002675 do {
2676 Py_BEGIN_ALLOW_THREADS
2677 timeout = internal_select_ex(s, 0, interval);
2678 if (!timeout) {
Victor Stinner14b9b112013-06-25 00:37:25 +02002679#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002680 if (len > INT_MAX)
2681 len = INT_MAX;
2682 outlen = recv(s->sock_fd, cbuf, (int)len, flags);
Victor Stinner9a644b22013-06-24 23:47:41 +02002683#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002684 outlen = recv(s->sock_fd, cbuf, len, flags);
Victor Stinner9a644b22013-06-24 23:47:41 +02002685#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002686 }
2687 Py_END_ALLOW_THREADS
2688 } while (outlen < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Thomas Wouters477c8d52006-05-27 19:21:47 +00002689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002690 if (timeout == 1) {
2691 PyErr_SetString(socket_timeout, "timed out");
2692 return -1;
2693 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002694 END_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002695 if (outlen < 0) {
2696 /* Note: the call to errorhandler() ALWAYS indirectly returned
2697 NULL, so ignore its return value */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002698 if (!async_err)
2699 s->errorhandler();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 return -1;
2701 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 return outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002703}
2704
Guido van Rossum48a680c2001-03-02 06:34:14 +00002705
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002706/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002707
Guido van Rossum73624e91994-10-10 17:59:00 +00002708static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002709sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002710{
Antoine Pitrou19467d22010-08-17 19:33:30 +00002711 Py_ssize_t recvlen, outlen;
2712 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002713 PyObject *buf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002714
Antoine Pitrou19467d22010-08-17 19:33:30 +00002715 if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 if (recvlen < 0) {
2719 PyErr_SetString(PyExc_ValueError,
2720 "negative buffersize in recv");
2721 return NULL;
2722 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 /* Allocate a new string. */
2725 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2726 if (buf == NULL)
2727 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002729 /* Call the guts */
2730 outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags);
2731 if (outlen < 0) {
2732 /* An error occurred, release the string and return an
2733 error. */
2734 Py_DECREF(buf);
2735 return NULL;
2736 }
2737 if (outlen != recvlen) {
2738 /* We did not read as many bytes as we anticipated, resize the
2739 string if possible and be successful. */
2740 _PyBytes_Resize(&buf, outlen);
2741 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 return buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002744}
2745
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002746PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002747"recv(buffersize[, flags]) -> data\n\
2748\n\
2749Receive up to buffersize bytes from the socket. For the optional flags\n\
2750argument, see the Unix manual. When no data is available, block until\n\
2751at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002752the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002753
Guido van Rossum30a685f1991-06-27 15:51:29 +00002754
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002755/* s.recv_into(buffer, [nbytes [,flags]]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002756
Thomas Wouters477c8d52006-05-27 19:21:47 +00002757static PyObject*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002758sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00002761
Antoine Pitrou19467d22010-08-17 19:33:30 +00002762 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 Py_buffer pbuf;
2764 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002765 Py_ssize_t buflen, readlen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 /* Get the buffer's memory */
Antoine Pitrou19467d22010-08-17 19:33:30 +00002768 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 &pbuf, &recvlen, &flags))
2770 return NULL;
2771 buf = pbuf.buf;
2772 buflen = pbuf.len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 if (recvlen < 0) {
2775 PyBuffer_Release(&pbuf);
2776 PyErr_SetString(PyExc_ValueError,
2777 "negative buffersize in recv_into");
2778 return NULL;
2779 }
2780 if (recvlen == 0) {
2781 /* If nbytes was not specified, use the buffer's length */
2782 recvlen = buflen;
2783 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 /* Check if the buffer is large enough */
2786 if (buflen < recvlen) {
2787 PyBuffer_Release(&pbuf);
2788 PyErr_SetString(PyExc_ValueError,
2789 "buffer too small for requested bytes");
2790 return NULL;
2791 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 /* Call the guts */
2794 readlen = sock_recv_guts(s, buf, recvlen, flags);
2795 if (readlen < 0) {
2796 /* Return an error. */
2797 PyBuffer_Release(&pbuf);
2798 return NULL;
2799 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 PyBuffer_Release(&pbuf);
2802 /* Return the number of bytes read. Note that we do not do anything
2803 special here in the case that readlen < recvlen. */
2804 return PyLong_FromSsize_t(readlen);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002805}
2806
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002807PyDoc_STRVAR(recv_into_doc,
2808"recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002809\n\
2810A version of recv() that stores its data into a buffer rather than creating \n\
2811a new string. Receive up to buffersize bytes from the socket. If buffersize \n\
2812is not specified (or 0), receive up to the size available in the given buffer.\n\
2813\n\
2814See recv() for documentation about the flags.");
2815
2816
2817/*
Christian Heimes99170a52007-12-19 02:07:34 +00002818 * This is the guts of the recvfrom() and recvfrom_into() methods, which reads
2819 * into a char buffer. If you have any inc/def ref to do to the objects that
2820 * contain the buffer, do it in the caller. This function returns the number
Ezio Melotti13925002011-03-16 11:05:33 +02002821 * of bytes successfully read. If there was an error, it returns -1. Note
Christian Heimes99170a52007-12-19 02:07:34 +00002822 * that it is also possible that we return a number of bytes smaller than the
2823 * request bytes.
Thomas Wouters477c8d52006-05-27 19:21:47 +00002824 *
2825 * 'addr' is a return value for the address object. Note that you must decref
2826 * it yourself.
2827 */
Antoine Pitrou19467d22010-08-17 19:33:30 +00002828static Py_ssize_t
2829sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 PyObject** addr)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002831{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 sock_addr_t addrbuf;
2833 int timeout;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002834 Py_ssize_t n = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 socklen_t addrlen;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002836 int async_err = 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 *addr = NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 if (!getsockaddrlen(s, &addrlen))
2841 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 if (!IS_SELECTABLE(s)) {
2844 select_error();
2845 return -1;
2846 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002847
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002848 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002849 do {
2850 Py_BEGIN_ALLOW_THREADS
2851 memset(&addrbuf, 0, addrlen);
2852 timeout = internal_select_ex(s, 0, interval);
2853 if (!timeout) {
Victor Stinnere990c6e2013-11-16 00:18:58 +01002854#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002855 if (len > INT_MAX)
2856 len = INT_MAX;
2857 n = recvfrom(s->sock_fd, cbuf, (int)len, flags,
2858 (void *) &addrbuf, &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002859#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002860 n = recvfrom(s->sock_fd, cbuf, len, flags,
2861 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002862#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002863 }
2864 Py_END_ALLOW_THREADS
2865 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 if (timeout == 1) {
2868 PyErr_SetString(socket_timeout, "timed out");
2869 return -1;
2870 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002871 END_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002873 if (!async_err)
2874 s->errorhandler();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 return -1;
2876 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
2879 addrlen, s->sock_proto)))
2880 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 return n;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002883}
2884
2885/* s.recvfrom(nbytes [,flags]) method */
2886
2887static PyObject *
2888sock_recvfrom(PySocketSockObject *s, PyObject *args)
2889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 PyObject *buf = NULL;
2891 PyObject *addr = NULL;
2892 PyObject *ret = NULL;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002893 int flags = 0;
2894 Py_ssize_t recvlen, outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002895
Antoine Pitrou19467d22010-08-17 19:33:30 +00002896 if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 if (recvlen < 0) {
2900 PyErr_SetString(PyExc_ValueError,
2901 "negative buffersize in recvfrom");
2902 return NULL;
2903 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2906 if (buf == NULL)
2907 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf),
2910 recvlen, flags, &addr);
2911 if (outlen < 0) {
2912 goto finally;
2913 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 if (outlen != recvlen) {
2916 /* We did not read as many bytes as we anticipated, resize the
Ezio Melotti13925002011-03-16 11:05:33 +02002917 string if possible and be successful. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 if (_PyBytes_Resize(&buf, outlen) < 0)
Ezio Melotti13925002011-03-16 11:05:33 +02002919 /* Oopsy, not so successful after all. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 goto finally;
2921 }
Barry Warsaw752300b1997-01-03 17:18:10 +00002922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002924
2925finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 Py_XDECREF(buf);
2927 Py_XDECREF(addr);
2928 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002929}
2930
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002931PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002932"recvfrom(buffersize[, flags]) -> (data, address info)\n\
2933\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002934Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002935
Thomas Wouters477c8d52006-05-27 19:21:47 +00002936
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002937/* s.recvfrom_into(buffer[, nbytes [,flags]]) method */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002938
2939static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002940sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002941{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00002943
Antoine Pitrou19467d22010-08-17 19:33:30 +00002944 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 Py_buffer pbuf;
2946 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002947 Py_ssize_t readlen, buflen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 PyObject *addr = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002950
Antoine Pitrou19467d22010-08-17 19:33:30 +00002951 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 kwlist, &pbuf,
2953 &recvlen, &flags))
2954 return NULL;
2955 buf = pbuf.buf;
2956 buflen = pbuf.len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 if (recvlen < 0) {
2959 PyBuffer_Release(&pbuf);
2960 PyErr_SetString(PyExc_ValueError,
2961 "negative buffersize in recvfrom_into");
2962 return NULL;
2963 }
2964 if (recvlen == 0) {
2965 /* If nbytes was not specified, use the buffer's length */
2966 recvlen = buflen;
Benjamin Petersonc6b37e22014-01-13 23:14:42 -05002967 } else if (recvlen > buflen) {
2968 PyBuffer_Release(&pbuf);
2969 PyErr_SetString(PyExc_ValueError,
2970 "nbytes is greater than the length of the buffer");
2971 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974 readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
2975 if (readlen < 0) {
2976 PyBuffer_Release(&pbuf);
2977 /* Return an error */
2978 Py_XDECREF(addr);
2979 return NULL;
2980 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982 PyBuffer_Release(&pbuf);
2983 /* Return the number of bytes read and the address. Note that we do
2984 not do anything special here in the case that readlen < recvlen. */
Antoine Pitrou19467d22010-08-17 19:33:30 +00002985 return Py_BuildValue("nN", readlen, addr);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002986}
2987
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002988PyDoc_STRVAR(recvfrom_into_doc,
2989"recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002990\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002991Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002992
2993
Nick Coghlan96fe56a2011-08-22 11:55:57 +10002994/* The sendmsg() and recvmsg[_into]() methods require a working
2995 CMSG_LEN(). See the comment near get_CMSG_LEN(). */
2996#ifdef CMSG_LEN
2997/*
2998 * Call recvmsg() with the supplied iovec structures, flags, and
2999 * ancillary data buffer size (controllen). Returns the tuple return
3000 * value for recvmsg() or recvmsg_into(), with the first item provided
3001 * by the supplied makeval() function. makeval() will be called with
3002 * the length read and makeval_data as arguments, and must return a
3003 * new reference (which will be decrefed if there is a subsequent
3004 * error). On error, closes any file descriptors received via
3005 * SCM_RIGHTS.
3006 */
3007static PyObject *
3008sock_recvmsg_guts(PySocketSockObject *s, struct iovec *iov, int iovlen,
3009 int flags, Py_ssize_t controllen,
3010 PyObject *(*makeval)(ssize_t, void *), void *makeval_data)
3011{
3012 ssize_t bytes_received = -1;
3013 int timeout;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003014 int async_err = 0;
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003015 sock_addr_t addrbuf;
3016 socklen_t addrbuflen;
Charles-François Natalie9e95ae2011-08-24 21:40:53 +02003017 struct msghdr msg = {0};
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003018 PyObject *cmsg_list = NULL, *retval = NULL;
3019 void *controlbuf = NULL;
3020 struct cmsghdr *cmsgh;
3021 size_t cmsgdatalen = 0;
3022 int cmsg_status;
3023
3024 /* XXX: POSIX says that msg_name and msg_namelen "shall be
3025 ignored" when the socket is connected (Linux fills them in
3026 anyway for AF_UNIX sockets at least). Normally msg_namelen
3027 seems to be set to 0 if there's no address, but try to
3028 initialize msg_name to something that won't be mistaken for a
3029 real address if that doesn't happen. */
3030 if (!getsockaddrlen(s, &addrbuflen))
3031 return NULL;
3032 memset(&addrbuf, 0, addrbuflen);
3033 SAS2SA(&addrbuf)->sa_family = AF_UNSPEC;
3034
3035 if (controllen < 0 || controllen > SOCKLEN_T_LIMIT) {
3036 PyErr_SetString(PyExc_ValueError,
3037 "invalid ancillary data buffer length");
3038 return NULL;
3039 }
3040 if (controllen > 0 && (controlbuf = PyMem_Malloc(controllen)) == NULL)
3041 return PyErr_NoMemory();
3042
3043 /* Make the system call. */
3044 if (!IS_SELECTABLE(s)) {
3045 select_error();
3046 goto finally;
3047 }
3048
3049 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003050 do {
3051 Py_BEGIN_ALLOW_THREADS;
3052 msg.msg_name = SAS2SA(&addrbuf);
3053 msg.msg_namelen = addrbuflen;
3054 msg.msg_iov = iov;
3055 msg.msg_iovlen = iovlen;
3056 msg.msg_control = controlbuf;
3057 msg.msg_controllen = controllen;
3058 timeout = internal_select_ex(s, 0, interval);
3059 if (!timeout)
3060 bytes_received = recvmsg(s->sock_fd, &msg, flags);
3061 Py_END_ALLOW_THREADS;
3062 if (timeout == 1) {
3063 PyErr_SetString(socket_timeout, "timed out");
3064 goto finally;
3065 }
3066 } while (bytes_received < 0 && errno == EINTR &&
3067 !(async_err = PyErr_CheckSignals()));
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003068 END_SELECT_LOOP(s)
3069
3070 if (bytes_received < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003071 if (!async_err)
3072 s->errorhandler();
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003073 goto finally;
3074 }
3075
3076 /* Make list of (level, type, data) tuples from control messages. */
3077 if ((cmsg_list = PyList_New(0)) == NULL)
3078 goto err_closefds;
3079 /* Check for empty ancillary data as old CMSG_FIRSTHDR()
3080 implementations didn't do so. */
3081 for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL);
3082 cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) {
3083 PyObject *bytes, *tuple;
3084 int tmp;
3085
3086 cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen);
3087 if (cmsg_status != 0) {
3088 if (PyErr_WarnEx(PyExc_RuntimeWarning,
3089 "received malformed or improperly-truncated "
3090 "ancillary data", 1) == -1)
3091 goto err_closefds;
3092 }
3093 if (cmsg_status < 0)
3094 break;
3095 if (cmsgdatalen > PY_SSIZE_T_MAX) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003096 PyErr_SetString(PyExc_OSError, "control message too long");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003097 goto err_closefds;
3098 }
3099
3100 bytes = PyBytes_FromStringAndSize((char *)CMSG_DATA(cmsgh),
3101 cmsgdatalen);
3102 tuple = Py_BuildValue("iiN", (int)cmsgh->cmsg_level,
3103 (int)cmsgh->cmsg_type, bytes);
3104 if (tuple == NULL)
3105 goto err_closefds;
3106 tmp = PyList_Append(cmsg_list, tuple);
3107 Py_DECREF(tuple);
3108 if (tmp != 0)
3109 goto err_closefds;
3110
3111 if (cmsg_status != 0)
3112 break;
3113 }
3114
3115 retval = Py_BuildValue("NOiN",
3116 (*makeval)(bytes_received, makeval_data),
3117 cmsg_list,
3118 (int)msg.msg_flags,
3119 makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
3120 ((msg.msg_namelen > addrbuflen) ?
3121 addrbuflen : msg.msg_namelen),
3122 s->sock_proto));
3123 if (retval == NULL)
3124 goto err_closefds;
3125
3126finally:
3127 Py_XDECREF(cmsg_list);
3128 PyMem_Free(controlbuf);
3129 return retval;
3130
3131err_closefds:
3132#ifdef SCM_RIGHTS
3133 /* Close all descriptors coming from SCM_RIGHTS, so they don't leak. */
3134 for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL);
3135 cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) {
3136 cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen);
3137 if (cmsg_status < 0)
3138 break;
3139 if (cmsgh->cmsg_level == SOL_SOCKET &&
3140 cmsgh->cmsg_type == SCM_RIGHTS) {
3141 size_t numfds;
3142 int *fdp;
3143
3144 numfds = cmsgdatalen / sizeof(int);
3145 fdp = (int *)CMSG_DATA(cmsgh);
3146 while (numfds-- > 0)
3147 close(*fdp++);
3148 }
3149 if (cmsg_status != 0)
3150 break;
3151 }
3152#endif /* SCM_RIGHTS */
3153 goto finally;
3154}
3155
3156
3157static PyObject *
3158makeval_recvmsg(ssize_t received, void *data)
3159{
3160 PyObject **buf = data;
3161
3162 if (received < PyBytes_GET_SIZE(*buf))
3163 _PyBytes_Resize(buf, received);
3164 Py_XINCREF(*buf);
3165 return *buf;
3166}
3167
3168/* s.recvmsg(bufsize[, ancbufsize[, flags]]) method */
3169
3170static PyObject *
3171sock_recvmsg(PySocketSockObject *s, PyObject *args)
3172{
3173 Py_ssize_t bufsize, ancbufsize = 0;
3174 int flags = 0;
3175 struct iovec iov;
3176 PyObject *buf = NULL, *retval = NULL;
3177
3178 if (!PyArg_ParseTuple(args, "n|ni:recvmsg", &bufsize, &ancbufsize, &flags))
3179 return NULL;
3180
3181 if (bufsize < 0) {
3182 PyErr_SetString(PyExc_ValueError, "negative buffer size in recvmsg()");
3183 return NULL;
3184 }
3185 if ((buf = PyBytes_FromStringAndSize(NULL, bufsize)) == NULL)
3186 return NULL;
3187 iov.iov_base = PyBytes_AS_STRING(buf);
3188 iov.iov_len = bufsize;
3189
3190 /* Note that we're passing a pointer to *our pointer* to the bytes
3191 object here (&buf); makeval_recvmsg() may incref the object, or
3192 deallocate it and set our pointer to NULL. */
3193 retval = sock_recvmsg_guts(s, &iov, 1, flags, ancbufsize,
3194 &makeval_recvmsg, &buf);
3195 Py_XDECREF(buf);
3196 return retval;
3197}
3198
3199PyDoc_STRVAR(recvmsg_doc,
3200"recvmsg(bufsize[, ancbufsize[, flags]]) -> (data, ancdata, msg_flags, address)\n\
3201\n\
3202Receive normal data (up to bufsize bytes) and ancillary data from the\n\
3203socket. The ancbufsize argument sets the size in bytes of the\n\
3204internal buffer used to receive the ancillary data; it defaults to 0,\n\
3205meaning that no ancillary data will be received. Appropriate buffer\n\
3206sizes for ancillary data can be calculated using CMSG_SPACE() or\n\
3207CMSG_LEN(), and items which do not fit into the buffer might be\n\
3208truncated or discarded. The flags argument defaults to 0 and has the\n\
3209same meaning as for recv().\n\
3210\n\
3211The return value is a 4-tuple: (data, ancdata, msg_flags, address).\n\
3212The data item is a bytes object holding the non-ancillary data\n\
3213received. The ancdata item is a list of zero or more tuples\n\
3214(cmsg_level, cmsg_type, cmsg_data) representing the ancillary data\n\
3215(control messages) received: cmsg_level and cmsg_type are integers\n\
3216specifying the protocol level and protocol-specific type respectively,\n\
3217and cmsg_data is a bytes object holding the associated data. The\n\
3218msg_flags item is the bitwise OR of various flags indicating\n\
3219conditions on the received message; see your system documentation for\n\
3220details. If the receiving socket is unconnected, address is the\n\
3221address of the sending socket, if available; otherwise, its value is\n\
3222unspecified.\n\
3223\n\
3224If recvmsg() raises an exception after the system call returns, it\n\
3225will first attempt to close any file descriptors received via the\n\
3226SCM_RIGHTS mechanism.");
3227
3228
3229static PyObject *
3230makeval_recvmsg_into(ssize_t received, void *data)
3231{
3232 return PyLong_FromSsize_t(received);
3233}
3234
3235/* s.recvmsg_into(buffers[, ancbufsize[, flags]]) method */
3236
3237static PyObject *
3238sock_recvmsg_into(PySocketSockObject *s, PyObject *args)
3239{
3240 Py_ssize_t ancbufsize = 0;
3241 int flags = 0;
3242 struct iovec *iovs = NULL;
3243 Py_ssize_t i, nitems, nbufs = 0;
3244 Py_buffer *bufs = NULL;
3245 PyObject *buffers_arg, *fast, *retval = NULL;
3246
3247 if (!PyArg_ParseTuple(args, "O|ni:recvmsg_into",
3248 &buffers_arg, &ancbufsize, &flags))
3249 return NULL;
3250
3251 if ((fast = PySequence_Fast(buffers_arg,
3252 "recvmsg_into() argument 1 must be an "
3253 "iterable")) == NULL)
3254 return NULL;
3255 nitems = PySequence_Fast_GET_SIZE(fast);
3256 if (nitems > INT_MAX) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003257 PyErr_SetString(PyExc_OSError, "recvmsg_into() argument 1 is too long");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003258 goto finally;
3259 }
3260
3261 /* Fill in an iovec for each item, and save the Py_buffer
3262 structs to release afterwards. */
3263 if (nitems > 0 && ((iovs = PyMem_New(struct iovec, nitems)) == NULL ||
3264 (bufs = PyMem_New(Py_buffer, nitems)) == NULL)) {
3265 PyErr_NoMemory();
3266 goto finally;
3267 }
3268 for (; nbufs < nitems; nbufs++) {
3269 if (!PyArg_Parse(PySequence_Fast_GET_ITEM(fast, nbufs),
3270 "w*;recvmsg_into() argument 1 must be an iterable "
3271 "of single-segment read-write buffers",
3272 &bufs[nbufs]))
3273 goto finally;
3274 iovs[nbufs].iov_base = bufs[nbufs].buf;
3275 iovs[nbufs].iov_len = bufs[nbufs].len;
3276 }
3277
3278 retval = sock_recvmsg_guts(s, iovs, nitems, flags, ancbufsize,
3279 &makeval_recvmsg_into, NULL);
3280finally:
3281 for (i = 0; i < nbufs; i++)
3282 PyBuffer_Release(&bufs[i]);
3283 PyMem_Free(bufs);
3284 PyMem_Free(iovs);
3285 Py_DECREF(fast);
3286 return retval;
3287}
3288
3289PyDoc_STRVAR(recvmsg_into_doc,
3290"recvmsg_into(buffers[, ancbufsize[, flags]]) -> (nbytes, ancdata, msg_flags, address)\n\
3291\n\
3292Receive normal data and ancillary data from the socket, scattering the\n\
3293non-ancillary data into a series of buffers. The buffers argument\n\
3294must be an iterable of objects that export writable buffers\n\
3295(e.g. bytearray objects); these will be filled with successive chunks\n\
3296of the non-ancillary data until it has all been written or there are\n\
3297no more buffers. The ancbufsize argument sets the size in bytes of\n\
3298the internal buffer used to receive the ancillary data; it defaults to\n\
32990, meaning that no ancillary data will be received. Appropriate\n\
3300buffer sizes for ancillary data can be calculated using CMSG_SPACE()\n\
3301or CMSG_LEN(), and items which do not fit into the buffer might be\n\
3302truncated or discarded. The flags argument defaults to 0 and has the\n\
3303same meaning as for recv().\n\
3304\n\
3305The return value is a 4-tuple: (nbytes, ancdata, msg_flags, address).\n\
3306The nbytes item is the total number of bytes of non-ancillary data\n\
3307written into the buffers. The ancdata item is a list of zero or more\n\
3308tuples (cmsg_level, cmsg_type, cmsg_data) representing the ancillary\n\
3309data (control messages) received: cmsg_level and cmsg_type are\n\
3310integers specifying the protocol level and protocol-specific type\n\
3311respectively, and cmsg_data is a bytes object holding the associated\n\
3312data. The msg_flags item is the bitwise OR of various flags\n\
3313indicating conditions on the received message; see your system\n\
3314documentation for details. If the receiving socket is unconnected,\n\
3315address is the address of the sending socket, if available; otherwise,\n\
3316its value is unspecified.\n\
3317\n\
3318If recvmsg_into() raises an exception after the system call returns,\n\
3319it will first attempt to close any file descriptors received via the\n\
3320SCM_RIGHTS mechanism.");
3321#endif /* CMSG_LEN */
3322
3323
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00003324/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00003325
Guido van Rossum73624e91994-10-10 17:59:00 +00003326static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003327sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00003328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003330 Py_ssize_t len, n = -1;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003331 int async_err = 0;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003332 int flags = 0, timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 Py_buffer pbuf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags))
3336 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 if (!IS_SELECTABLE(s)) {
3339 PyBuffer_Release(&pbuf);
3340 return select_error();
3341 }
3342 buf = pbuf.buf;
3343 len = pbuf.len;
Neal Norwitz082b2df2006-02-07 07:04:46 +00003344
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003345 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003346 do {
3347 Py_BEGIN_ALLOW_THREADS
3348 timeout = internal_select_ex(s, 1, interval);
3349 if (!timeout) {
Christian Heimesaf01f662013-12-21 16:19:10 +01003350#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003351 if (len > INT_MAX)
3352 len = INT_MAX;
3353 n = send(s->sock_fd, buf, (int)len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003354#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003355 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003356#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003357 }
3358 Py_END_ALLOW_THREADS
3359 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003360 if (timeout == 1) {
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003361 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 PyErr_SetString(socket_timeout, "timed out");
3363 return NULL;
3364 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003365 END_SELECT_LOOP(s)
3366
3367 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003368 if (n < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003369 return (!async_err) ? s->errorhandler() : NULL;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003370 return PyLong_FromSsize_t(n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00003371}
3372
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003373PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003374"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003375\n\
3376Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003377argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003378sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003379
3380
3381/* s.sendall(data [,flags]) method */
3382
3383static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003384sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003386 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003387 Py_ssize_t len, n = -1;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003388 int async_err = 0;
3389 int flags = 0, timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 Py_buffer pbuf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags))
3393 return NULL;
3394 buf = pbuf.buf;
3395 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 if (!IS_SELECTABLE(s)) {
3398 PyBuffer_Release(&pbuf);
3399 return select_error();
3400 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00003401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 do {
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003403 Py_BEGIN_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 timeout = internal_select(s, 1);
3405 n = -1;
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003406 if (!timeout) {
Christian Heimesaf01f662013-12-21 16:19:10 +01003407#ifdef MS_WINDOWS
Victor Stinner9a644b22013-06-24 23:47:41 +02003408 if (len > INT_MAX)
3409 len = INT_MAX;
3410 n = send(s->sock_fd, buf, (int)len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003411#else
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003412 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003413#endif
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003414 }
3415 Py_END_ALLOW_THREADS
3416 if (timeout == 1) {
3417 PyBuffer_Release(&pbuf);
3418 PyErr_SetString(socket_timeout, "timed out");
3419 return NULL;
3420 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003421 if (n >= 0) {
3422 buf += n;
3423 len -= n;
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003424 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003425 } while (len > 0 && (n >= 0 || errno == EINTR) &&
3426 !(async_err = PyErr_CheckSignals()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 PyBuffer_Release(&pbuf);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003428
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003429 if (n < 0 || async_err)
3430 return (!async_err) ? s->errorhandler() : NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 Py_INCREF(Py_None);
3433 return Py_None;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003434}
3435
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003436PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003437"sendall(data[, flags])\n\
3438\n\
3439Send a data string to the socket. For the optional flags\n\
3440argument, see the Unix manual. This calls send() repeatedly\n\
3441until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003442to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003443
Guido van Rossum30a685f1991-06-27 15:51:29 +00003444
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00003445/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00003446
Guido van Rossum73624e91994-10-10 17:59:00 +00003447static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003448sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 Py_buffer pbuf;
3451 PyObject *addro;
3452 char *buf;
Antoine Pitrou5e981412011-03-17 22:38:37 +01003453 Py_ssize_t len, arglen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454 sock_addr_t addrbuf;
3455 int addrlen, n = -1, flags, timeout;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003456 int async_err = 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 flags = 0;
Antoine Pitrou5e981412011-03-17 22:38:37 +01003459 arglen = PyTuple_Size(args);
3460 switch (arglen) {
3461 case 2:
3462 PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro);
3463 break;
3464 case 3:
3465 PyArg_ParseTuple(args, "y*iO:sendto",
3466 &pbuf, &flags, &addro);
3467 break;
3468 default:
3469 PyErr_Format(PyExc_TypeError,
3470 "sendto() takes 2 or 3 arguments (%d given)",
3471 arglen);
Victor Stinner77af1722011-05-26 14:05:59 +02003472 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003473 }
Antoine Pitrou5e981412011-03-17 22:38:37 +01003474 if (PyErr_Occurred())
3475 return NULL;
3476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 buf = pbuf.buf;
3478 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 if (!IS_SELECTABLE(s)) {
3481 PyBuffer_Release(&pbuf);
3482 return select_error();
3483 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00003484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) {
3486 PyBuffer_Release(&pbuf);
3487 return NULL;
3488 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003489
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003490 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003491 do {
3492 Py_BEGIN_ALLOW_THREADS
3493 timeout = internal_select_ex(s, 1, interval);
3494 if (!timeout) {
Victor Stinner14b9b112013-06-25 00:37:25 +02003495#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003496 if (len > INT_MAX)
3497 len = INT_MAX;
3498 n = sendto(s->sock_fd, buf, (int)len, flags,
3499 SAS2SA(&addrbuf), addrlen);
Victor Stinner9a644b22013-06-24 23:47:41 +02003500#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003501 n = sendto(s->sock_fd, buf, len, flags,
3502 SAS2SA(&addrbuf), addrlen);
Victor Stinner9a644b22013-06-24 23:47:41 +02003503#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003504 }
3505 Py_END_ALLOW_THREADS
3506 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Guido van Rossum67f7a382002-06-06 21:08:16 +00003507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 if (timeout == 1) {
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003509 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003510 PyErr_SetString(socket_timeout, "timed out");
3511 return NULL;
3512 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003513 END_SELECT_LOOP(s)
3514 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003515 if (n < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003516 return (!async_err) ? s->errorhandler() : NULL;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003517 return PyLong_FromSsize_t(n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003518}
3519
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003520PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003521"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003522\n\
3523Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003524For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003525
Guido van Rossum30a685f1991-06-27 15:51:29 +00003526
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003527/* The sendmsg() and recvmsg[_into]() methods require a working
3528 CMSG_LEN(). See the comment near get_CMSG_LEN(). */
3529#ifdef CMSG_LEN
3530/* s.sendmsg(buffers[, ancdata[, flags[, address]]]) method */
3531
3532static PyObject *
3533sock_sendmsg(PySocketSockObject *s, PyObject *args)
3534{
3535 Py_ssize_t i, ndataparts, ndatabufs = 0, ncmsgs, ncmsgbufs = 0;
3536 Py_buffer *databufs = NULL;
3537 struct iovec *iovs = NULL;
3538 sock_addr_t addrbuf;
Charles-François Natalie9e95ae2011-08-24 21:40:53 +02003539 struct msghdr msg = {0};
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003540 struct cmsginfo {
3541 int level;
3542 int type;
3543 Py_buffer data;
3544 } *cmsgs = NULL;
3545 void *controlbuf = NULL;
3546 size_t controllen, controllen_last;
3547 ssize_t bytes_sent = -1;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003548 int async_err = 0;
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003549 int addrlen, timeout, flags = 0;
3550 PyObject *data_arg, *cmsg_arg = NULL, *addr_arg = NULL, *data_fast = NULL,
3551 *cmsg_fast = NULL, *retval = NULL;
3552
3553 if (!PyArg_ParseTuple(args, "O|OiO:sendmsg",
3554 &data_arg, &cmsg_arg, &flags, &addr_arg))
3555 return NULL;
3556
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003557 /* Parse destination address. */
3558 if (addr_arg != NULL && addr_arg != Py_None) {
3559 if (!getsockaddrarg(s, addr_arg, SAS2SA(&addrbuf), &addrlen))
3560 goto finally;
3561 msg.msg_name = &addrbuf;
3562 msg.msg_namelen = addrlen;
3563 }
3564
3565 /* Fill in an iovec for each message part, and save the Py_buffer
3566 structs to release afterwards. */
3567 if ((data_fast = PySequence_Fast(data_arg,
3568 "sendmsg() argument 1 must be an "
3569 "iterable")) == NULL)
3570 goto finally;
3571 ndataparts = PySequence_Fast_GET_SIZE(data_fast);
3572 if (ndataparts > INT_MAX) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003573 PyErr_SetString(PyExc_OSError, "sendmsg() argument 1 is too long");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003574 goto finally;
3575 }
3576 msg.msg_iovlen = ndataparts;
3577 if (ndataparts > 0 &&
3578 ((msg.msg_iov = iovs = PyMem_New(struct iovec, ndataparts)) == NULL ||
3579 (databufs = PyMem_New(Py_buffer, ndataparts)) == NULL)) {
3580 PyErr_NoMemory();
3581 goto finally;
3582 }
3583 for (; ndatabufs < ndataparts; ndatabufs++) {
3584 if (!PyArg_Parse(PySequence_Fast_GET_ITEM(data_fast, ndatabufs),
3585 "y*;sendmsg() argument 1 must be an iterable of "
Serhiy Storchakab757c832014-12-05 22:25:22 +02003586 "bytes-like objects",
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003587 &databufs[ndatabufs]))
3588 goto finally;
3589 iovs[ndatabufs].iov_base = databufs[ndatabufs].buf;
3590 iovs[ndatabufs].iov_len = databufs[ndatabufs].len;
3591 }
3592
3593 if (cmsg_arg == NULL)
3594 ncmsgs = 0;
3595 else {
3596 if ((cmsg_fast = PySequence_Fast(cmsg_arg,
3597 "sendmsg() argument 2 must be an "
3598 "iterable")) == NULL)
3599 goto finally;
3600 ncmsgs = PySequence_Fast_GET_SIZE(cmsg_fast);
3601 }
3602
3603#ifndef CMSG_SPACE
3604 if (ncmsgs > 1) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003605 PyErr_SetString(PyExc_OSError,
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003606 "sending multiple control messages is not supported "
3607 "on this system");
3608 goto finally;
3609 }
3610#endif
3611 /* Save level, type and Py_buffer for each control message,
3612 and calculate total size. */
3613 if (ncmsgs > 0 && (cmsgs = PyMem_New(struct cmsginfo, ncmsgs)) == NULL) {
3614 PyErr_NoMemory();
3615 goto finally;
3616 }
3617 controllen = controllen_last = 0;
3618 while (ncmsgbufs < ncmsgs) {
3619 size_t bufsize, space;
3620
3621 if (!PyArg_Parse(PySequence_Fast_GET_ITEM(cmsg_fast, ncmsgbufs),
3622 "(iiy*):[sendmsg() ancillary data items]",
3623 &cmsgs[ncmsgbufs].level,
3624 &cmsgs[ncmsgbufs].type,
3625 &cmsgs[ncmsgbufs].data))
3626 goto finally;
3627 bufsize = cmsgs[ncmsgbufs++].data.len;
3628
3629#ifdef CMSG_SPACE
3630 if (!get_CMSG_SPACE(bufsize, &space)) {
3631#else
3632 if (!get_CMSG_LEN(bufsize, &space)) {
3633#endif
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003634 PyErr_SetString(PyExc_OSError, "ancillary data item too large");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003635 goto finally;
3636 }
3637 controllen += space;
3638 if (controllen > SOCKLEN_T_LIMIT || controllen < controllen_last) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003639 PyErr_SetString(PyExc_OSError, "too much ancillary data");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003640 goto finally;
3641 }
3642 controllen_last = controllen;
3643 }
3644
3645 /* Construct ancillary data block from control message info. */
3646 if (ncmsgbufs > 0) {
3647 struct cmsghdr *cmsgh = NULL;
3648
3649 if ((msg.msg_control = controlbuf =
3650 PyMem_Malloc(controllen)) == NULL) {
3651 PyErr_NoMemory();
3652 goto finally;
3653 }
3654 msg.msg_controllen = controllen;
3655
3656 /* Need to zero out the buffer as a workaround for glibc's
3657 CMSG_NXTHDR() implementation. After getting the pointer to
3658 the next header, it checks its (uninitialized) cmsg_len
3659 member to see if the "message" fits in the buffer, and
3660 returns NULL if it doesn't. Zero-filling the buffer
Terry Jan Reedy0f847642013-03-11 18:34:00 -04003661 ensures that this doesn't happen. */
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003662 memset(controlbuf, 0, controllen);
3663
3664 for (i = 0; i < ncmsgbufs; i++) {
3665 size_t msg_len, data_len = cmsgs[i].data.len;
3666 int enough_space = 0;
3667
3668 cmsgh = (i == 0) ? CMSG_FIRSTHDR(&msg) : CMSG_NXTHDR(&msg, cmsgh);
3669 if (cmsgh == NULL) {
3670 PyErr_Format(PyExc_RuntimeError,
3671 "unexpected NULL result from %s()",
3672 (i == 0) ? "CMSG_FIRSTHDR" : "CMSG_NXTHDR");
3673 goto finally;
3674 }
3675 if (!get_CMSG_LEN(data_len, &msg_len)) {
3676 PyErr_SetString(PyExc_RuntimeError,
3677 "item size out of range for CMSG_LEN()");
3678 goto finally;
3679 }
3680 if (cmsg_min_space(&msg, cmsgh, msg_len)) {
3681 size_t space;
3682
3683 cmsgh->cmsg_len = msg_len;
3684 if (get_cmsg_data_space(&msg, cmsgh, &space))
3685 enough_space = (space >= data_len);
3686 }
3687 if (!enough_space) {
3688 PyErr_SetString(PyExc_RuntimeError,
3689 "ancillary data does not fit in calculated "
3690 "space");
3691 goto finally;
3692 }
3693 cmsgh->cmsg_level = cmsgs[i].level;
3694 cmsgh->cmsg_type = cmsgs[i].type;
3695 memcpy(CMSG_DATA(cmsgh), cmsgs[i].data.buf, data_len);
3696 }
3697 }
3698
3699 /* Make the system call. */
3700 if (!IS_SELECTABLE(s)) {
3701 select_error();
3702 goto finally;
3703 }
3704
3705 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003706 do {
3707 Py_BEGIN_ALLOW_THREADS;
3708 timeout = internal_select_ex(s, 1, interval);
3709 if (!timeout)
3710 bytes_sent = sendmsg(s->sock_fd, &msg, flags);
3711 Py_END_ALLOW_THREADS;
3712 if (timeout == 1) {
3713 PyErr_SetString(socket_timeout, "timed out");
3714 goto finally;
3715 }
3716 } while (bytes_sent < 0 && errno == EINTR &&
3717 !(async_err = PyErr_CheckSignals()));
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003718 END_SELECT_LOOP(s)
3719
3720 if (bytes_sent < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003721 if (!async_err)
3722 s->errorhandler();
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003723 goto finally;
3724 }
3725 retval = PyLong_FromSsize_t(bytes_sent);
3726
3727finally:
3728 PyMem_Free(controlbuf);
3729 for (i = 0; i < ncmsgbufs; i++)
3730 PyBuffer_Release(&cmsgs[i].data);
3731 PyMem_Free(cmsgs);
3732 Py_XDECREF(cmsg_fast);
3733 for (i = 0; i < ndatabufs; i++)
3734 PyBuffer_Release(&databufs[i]);
3735 PyMem_Free(databufs);
3736 PyMem_Free(iovs);
3737 Py_XDECREF(data_fast);
3738 return retval;
3739}
3740
3741PyDoc_STRVAR(sendmsg_doc,
3742"sendmsg(buffers[, ancdata[, flags[, address]]]) -> count\n\
3743\n\
3744Send normal and ancillary data to the socket, gathering the\n\
3745non-ancillary data from a series of buffers and concatenating it into\n\
3746a single message. The buffers argument specifies the non-ancillary\n\
Serhiy Storchakab757c832014-12-05 22:25:22 +02003747data as an iterable of bytes-like objects (e.g. bytes objects).\n\
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003748The ancdata argument specifies the ancillary data (control messages)\n\
3749as an iterable of zero or more tuples (cmsg_level, cmsg_type,\n\
3750cmsg_data), where cmsg_level and cmsg_type are integers specifying the\n\
3751protocol level and protocol-specific type respectively, and cmsg_data\n\
Serhiy Storchakab757c832014-12-05 22:25:22 +02003752is a bytes-like object holding the associated data. The flags\n\
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003753argument defaults to 0 and has the same meaning as for send(). If\n\
3754address is supplied and not None, it sets a destination address for\n\
3755the message. The return value is the number of bytes of non-ancillary\n\
3756data sent.");
3757#endif /* CMSG_LEN */
3758
3759
Guido van Rossum30a685f1991-06-27 15:51:29 +00003760/* s.shutdown(how) method */
3761
Guido van Rossum73624e91994-10-10 17:59:00 +00003762static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003763sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003765 int how;
3766 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00003767
Serhiy Storchaka78980432013-01-15 01:12:17 +02003768 how = _PyLong_AsInt(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003769 if (how == -1 && PyErr_Occurred())
3770 return NULL;
3771 Py_BEGIN_ALLOW_THREADS
3772 res = shutdown(s->sock_fd, how);
3773 Py_END_ALLOW_THREADS
3774 if (res < 0)
3775 return s->errorhandler();
3776 Py_INCREF(Py_None);
3777 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003778}
3779
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003780PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003781"shutdown(flag)\n\
3782\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00003783Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
3784of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003785
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00003786#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Christian Heimesfaf2f632008-01-06 16:59:19 +00003787static PyObject*
3788sock_ioctl(PySocketSockObject *s, PyObject *arg)
3789{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003790 unsigned long cmd = SIO_RCVALL;
3791 PyObject *argO;
3792 DWORD recv;
Christian Heimesfaf2f632008-01-06 16:59:19 +00003793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO))
3795 return NULL;
Christian Heimesfaf2f632008-01-06 16:59:19 +00003796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 switch (cmd) {
3798 case SIO_RCVALL: {
3799 unsigned int option = RCVALL_ON;
3800 if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option))
3801 return NULL;
3802 if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option),
3803 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
3804 return set_error();
3805 }
3806 return PyLong_FromUnsignedLong(recv); }
3807 case SIO_KEEPALIVE_VALS: {
3808 struct tcp_keepalive ka;
3809 if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd,
3810 &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval))
3811 return NULL;
3812 if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka),
3813 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
3814 return set_error();
3815 }
3816 return PyLong_FromUnsignedLong(recv); }
3817 default:
3818 PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd);
3819 return NULL;
3820 }
Christian Heimesfaf2f632008-01-06 16:59:19 +00003821}
3822PyDoc_STRVAR(sock_ioctl_doc,
3823"ioctl(cmd, option) -> long\n\
3824\n\
Kristján Valur Jónsson847ec752009-09-27 21:10:38 +00003825Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\
3826SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\
3827SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval).");
Kristján Valur Jónsson10f383a2012-04-07 11:23:31 +00003828#endif
3829
3830#if defined(MS_WINDOWS)
3831static PyObject*
3832sock_share(PySocketSockObject *s, PyObject *arg)
3833{
3834 WSAPROTOCOL_INFO info;
3835 DWORD processId;
3836 int result;
3837
3838 if (!PyArg_ParseTuple(arg, "I", &processId))
3839 return NULL;
3840
3841 Py_BEGIN_ALLOW_THREADS
3842 result = WSADuplicateSocket(s->sock_fd, processId, &info);
3843 Py_END_ALLOW_THREADS
3844 if (result == SOCKET_ERROR)
3845 return set_error();
3846 return PyBytes_FromStringAndSize((const char*)&info, sizeof(info));
3847}
3848PyDoc_STRVAR(sock_share_doc,
3849"share(process_id) -> bytes\n\
3850\n\
3851Share the socket with another process. The target process id\n\
3852must be provided and the resulting bytes object passed to the target\n\
3853process. There the shared socket can be instantiated by calling\n\
3854socket.fromshare().");
3855
Christian Heimesfaf2f632008-01-06 16:59:19 +00003856
3857#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00003858
3859/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003860
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003861static PyMethodDef sock_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003862 {"_accept", (PyCFunction)sock_accept, METH_NOARGS,
3863 accept_doc},
3864 {"bind", (PyCFunction)sock_bind, METH_O,
3865 bind_doc},
3866 {"close", (PyCFunction)sock_close, METH_NOARGS,
3867 close_doc},
3868 {"connect", (PyCFunction)sock_connect, METH_O,
3869 connect_doc},
3870 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
3871 connect_ex_doc},
Antoine Pitrou6e451df2010-08-09 20:39:54 +00003872 {"detach", (PyCFunction)sock_detach, METH_NOARGS,
3873 detach_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003874 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
3875 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003876#ifdef HAVE_GETPEERNAME
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 {"getpeername", (PyCFunction)sock_getpeername,
3878 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00003879#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003880 {"getsockname", (PyCFunction)sock_getsockname,
3881 METH_NOARGS, getsockname_doc},
3882 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
3883 getsockopt_doc},
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00003884#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003885 {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS,
3886 sock_ioctl_doc},
Christian Heimesfaf2f632008-01-06 16:59:19 +00003887#endif
Kristján Valur Jónsson10f383a2012-04-07 11:23:31 +00003888#if defined(MS_WINDOWS)
3889 {"share", (PyCFunction)sock_share, METH_VARARGS,
3890 sock_share_doc},
3891#endif
Charles-François Natali644b8f52014-05-22 19:45:39 +01003892 {"listen", (PyCFunction)sock_listen, METH_VARARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 listen_doc},
3894 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
3895 recv_doc},
3896 {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
3897 recv_into_doc},
3898 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
3899 recvfrom_doc},
3900 {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
3901 recvfrom_into_doc},
3902 {"send", (PyCFunction)sock_send, METH_VARARGS,
3903 send_doc},
3904 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
3905 sendall_doc},
3906 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
3907 sendto_doc},
3908 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
3909 setblocking_doc},
3910 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
3911 settimeout_doc},
3912 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
3913 gettimeout_doc},
3914 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
3915 setsockopt_doc},
3916 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
3917 shutdown_doc},
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003918#ifdef CMSG_LEN
3919 {"recvmsg", (PyCFunction)sock_recvmsg, METH_VARARGS,
3920 recvmsg_doc},
3921 {"recvmsg_into", (PyCFunction)sock_recvmsg_into, METH_VARARGS,
3922 recvmsg_into_doc,},
3923 {"sendmsg", (PyCFunction)sock_sendmsg, METH_VARARGS,
3924 sendmsg_doc},
3925#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003926 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003927};
3928
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003929/* SockObject members */
3930static PyMemberDef sock_memberlist[] = {
3931 {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},
3932 {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},
3933 {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},
3934 {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"},
3935 {0},
3936};
Guido van Rossum30a685f1991-06-27 15:51:29 +00003937
Guido van Rossum73624e91994-10-10 17:59:00 +00003938/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00003939 First close the file description. */
3940
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003941static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003942sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003943{
Antoine Pitroue033e062010-10-29 10:38:18 +00003944 if (s->sock_fd != -1) {
3945 PyObject *exc, *val, *tb;
3946 Py_ssize_t old_refcount = Py_REFCNT(s);
3947 ++Py_REFCNT(s);
3948 PyErr_Fetch(&exc, &val, &tb);
3949 if (PyErr_WarnFormat(PyExc_ResourceWarning, 1,
3950 "unclosed %R", s))
3951 /* Spurious errors can appear at shutdown */
3952 if (PyErr_ExceptionMatches(PyExc_Warning))
3953 PyErr_WriteUnraisable((PyObject *) s);
3954 PyErr_Restore(exc, val, tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003955 (void) SOCKETCLOSE(s->sock_fd);
Antoine Pitroue033e062010-10-29 10:38:18 +00003956 Py_REFCNT(s) = old_refcount;
3957 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003958 Py_TYPE(s)->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003959}
3960
Guido van Rossum30a685f1991-06-27 15:51:29 +00003961
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003962static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003963sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003964{
Victor Stinnere254e532014-07-26 14:36:55 +02003965 long sock_fd;
3966 /* On Windows, this test is needed because SOCKET_T is unsigned */
3967 if (s->sock_fd == INVALID_SOCKET) {
3968 sock_fd = -1;
3969 }
Fred Drakea04eaad2000-06-30 02:46:07 +00003970#if SIZEOF_SOCKET_T > SIZEOF_LONG
Victor Stinnere254e532014-07-26 14:36:55 +02003971 else if (s->sock_fd > LONG_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003972 /* this can occur on Win64, and actually there is a special
3973 ugly printf formatter for decimal pointer length integer
3974 printing, only bother if necessary*/
3975 PyErr_SetString(PyExc_OverflowError,
3976 "no printf formatter to display "
3977 "the socket descriptor in decimal");
3978 return NULL;
3979 }
Fred Drakea04eaad2000-06-30 02:46:07 +00003980#endif
Victor Stinnere254e532014-07-26 14:36:55 +02003981 else
3982 sock_fd = (long)s->sock_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003983 return PyUnicode_FromFormat(
3984 "<socket object, fd=%ld, family=%d, type=%d, proto=%d>",
Victor Stinnere254e532014-07-26 14:36:55 +02003985 sock_fd, s->sock_family,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003986 s->sock_type,
3987 s->sock_proto);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003988}
3989
3990
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003991/* Create a new, uninitialized socket object. */
3992
3993static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003994sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003995{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003996 PyObject *new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003998 new = type->tp_alloc(type, 0);
3999 if (new != NULL) {
4000 ((PySocketSockObject *)new)->sock_fd = -1;
4001 ((PySocketSockObject *)new)->sock_timeout = -1.0;
4002 ((PySocketSockObject *)new)->errorhandler = &set_error;
4003 }
4004 return new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004005}
4006
4007
4008/* Initialize a new socket object. */
4009
Victor Stinnerdaf45552013-08-28 00:53:59 +02004010#ifdef SOCK_CLOEXEC
4011/* socket() and socketpair() fail with EINVAL on Linux kernel older
4012 * than 2.6.27 if SOCK_CLOEXEC flag is set in the socket type. */
4013static int sock_cloexec_works = -1;
4014#endif
4015
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004016/*ARGSUSED*/
4017static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00004018sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004020 PySocketSockObject *s = (PySocketSockObject *)self;
4021 PyObject *fdobj = NULL;
4022 SOCKET_T fd = INVALID_SOCKET;
4023 int family = AF_INET, type = SOCK_STREAM, proto = 0;
4024 static char *keywords[] = {"family", "type", "proto", "fileno", 0};
Victor Stinnerdaf45552013-08-28 00:53:59 +02004025#ifndef MS_WINDOWS
4026#ifdef SOCK_CLOEXEC
4027 int *atomic_flag_works = &sock_cloexec_works;
4028#else
4029 int *atomic_flag_works = NULL;
4030#endif
4031#endif
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004033 if (!PyArg_ParseTupleAndKeywords(args, kwds,
4034 "|iiiO:socket", keywords,
4035 &family, &type, &proto, &fdobj))
4036 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004038 if (fdobj != NULL && fdobj != Py_None) {
Kristján Valur Jónsson10f383a2012-04-07 11:23:31 +00004039#ifdef MS_WINDOWS
4040 /* recreate a socket that was duplicated */
4041 if (PyBytes_Check(fdobj)) {
4042 WSAPROTOCOL_INFO info;
4043 if (PyBytes_GET_SIZE(fdobj) != sizeof(info)) {
4044 PyErr_Format(PyExc_ValueError,
4045 "socket descriptor string has wrong size, "
4046 "should be %zu bytes.", sizeof(info));
4047 return -1;
4048 }
4049 memcpy(&info, PyBytes_AS_STRING(fdobj), sizeof(info));
4050 Py_BEGIN_ALLOW_THREADS
4051 fd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
4052 FROM_PROTOCOL_INFO, &info, 0, WSA_FLAG_OVERLAPPED);
4053 Py_END_ALLOW_THREADS
4054 if (fd == INVALID_SOCKET) {
4055 set_error();
4056 return -1;
4057 }
4058 family = info.iAddressFamily;
4059 type = info.iSocketType;
4060 proto = info.iProtocol;
4061 }
4062 else
4063#endif
4064 {
4065 fd = PyLong_AsSocket_t(fdobj);
4066 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
4067 return -1;
4068 if (fd == INVALID_SOCKET) {
4069 PyErr_SetString(PyExc_ValueError,
4070 "can't use invalid socket value");
4071 return -1;
4072 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 }
4074 }
4075 else {
Victor Stinnerdaf45552013-08-28 00:53:59 +02004076#ifdef MS_WINDOWS
4077 /* Windows implementation */
4078#ifndef WSA_FLAG_NO_HANDLE_INHERIT
4079#define WSA_FLAG_NO_HANDLE_INHERIT 0x80
4080#endif
4081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004082 Py_BEGIN_ALLOW_THREADS
Victor Stinnerdaf45552013-08-28 00:53:59 +02004083 if (support_wsa_no_inherit) {
4084 fd = WSASocket(family, type, proto,
4085 NULL, 0,
4086 WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT);
4087 if (fd == INVALID_SOCKET) {
4088 /* Windows 7 or Windows 2008 R2 without SP1 or the hotfix */
4089 support_wsa_no_inherit = 0;
4090 fd = socket(family, type, proto);
4091 }
4092 }
4093 else {
4094 fd = socket(family, type, proto);
4095 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004096 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00004097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004098 if (fd == INVALID_SOCKET) {
4099 set_error();
4100 return -1;
4101 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02004102
4103 if (!support_wsa_no_inherit) {
4104 if (!SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0)) {
4105 closesocket(fd);
4106 PyErr_SetFromWindowsErr(0);
4107 return -1;
4108 }
4109 }
4110#else
4111 /* UNIX */
4112 Py_BEGIN_ALLOW_THREADS
4113#ifdef SOCK_CLOEXEC
4114 if (sock_cloexec_works != 0) {
4115 fd = socket(family, type | SOCK_CLOEXEC, proto);
4116 if (sock_cloexec_works == -1) {
4117 if (fd >= 0) {
4118 sock_cloexec_works = 1;
4119 }
4120 else if (errno == EINVAL) {
4121 /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */
4122 sock_cloexec_works = 0;
4123 fd = socket(family, type, proto);
4124 }
4125 }
4126 }
4127 else
4128#endif
4129 {
4130 fd = socket(family, type, proto);
4131 }
4132 Py_END_ALLOW_THREADS
4133
4134 if (fd == INVALID_SOCKET) {
4135 set_error();
4136 return -1;
4137 }
4138
4139 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
4140 SOCKETCLOSE(fd);
4141 return -1;
4142 }
4143#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004144 }
4145 init_sockobject(s, fd, family, type, proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00004146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004148
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004149}
4150
4151
Guido van Rossumb6775db1994-08-01 11:34:53 +00004152/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00004153
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004154static PyTypeObject sock_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
4156 "_socket.socket", /* tp_name */
4157 sizeof(PySocketSockObject), /* tp_basicsize */
4158 0, /* tp_itemsize */
4159 (destructor)sock_dealloc, /* tp_dealloc */
4160 0, /* tp_print */
4161 0, /* tp_getattr */
4162 0, /* tp_setattr */
4163 0, /* tp_reserved */
4164 (reprfunc)sock_repr, /* tp_repr */
4165 0, /* tp_as_number */
4166 0, /* tp_as_sequence */
4167 0, /* tp_as_mapping */
4168 0, /* tp_hash */
4169 0, /* tp_call */
4170 0, /* tp_str */
4171 PyObject_GenericGetAttr, /* tp_getattro */
4172 0, /* tp_setattro */
4173 0, /* tp_as_buffer */
4174 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4175 sock_doc, /* tp_doc */
4176 0, /* tp_traverse */
4177 0, /* tp_clear */
4178 0, /* tp_richcompare */
4179 0, /* tp_weaklistoffset */
4180 0, /* tp_iter */
4181 0, /* tp_iternext */
4182 sock_methods, /* tp_methods */
4183 sock_memberlist, /* tp_members */
4184 0, /* tp_getset */
4185 0, /* tp_base */
4186 0, /* tp_dict */
4187 0, /* tp_descr_get */
4188 0, /* tp_descr_set */
4189 0, /* tp_dictoffset */
4190 sock_initobj, /* tp_init */
4191 PyType_GenericAlloc, /* tp_alloc */
4192 sock_new, /* tp_new */
4193 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004194};
4195
Guido van Rossum30a685f1991-06-27 15:51:29 +00004196
Guido van Rossum81194471991-07-27 21:42:02 +00004197/* Python interface to gethostname(). */
4198
4199/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00004200static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004201socket_gethostname(PyObject *self, PyObject *unused)
Guido van Rossum81194471991-07-27 21:42:02 +00004202{
Martin v. Löwis72f48422010-10-29 18:20:08 +00004203#ifdef MS_WINDOWS
4204 /* Don't use winsock's gethostname, as this returns the ANSI
4205 version of the hostname, whereas we need a Unicode string.
4206 Otherwise, gethostname apparently also returns the DNS name. */
Hirokazu Yamamoto09fff7a2010-11-05 17:24:13 +00004207 wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1];
Victor Stinner63941882011-09-29 00:42:28 +02004208 DWORD size = Py_ARRAY_LENGTH(buf);
Victor Stinner74168972011-11-17 01:11:36 +01004209 wchar_t *name;
Hirokazu Yamamoto09fff7a2010-11-05 17:24:13 +00004210 PyObject *result;
Victor Stinner74168972011-11-17 01:11:36 +01004211
4212 if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size))
Victor Stinner9d3b93b2011-11-22 02:27:30 +01004213 return PyUnicode_FromWideChar(buf, size);
Victor Stinner74168972011-11-17 01:11:36 +01004214
4215 if (GetLastError() != ERROR_MORE_DATA)
4216 return PyErr_SetFromWindowsErr(0);
4217
4218 if (size == 0)
4219 return PyUnicode_New(0, 0);
4220
4221 /* MSDN says ERROR_MORE_DATA may occur because DNS allows longer
4222 names */
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02004223 name = PyMem_New(wchar_t, size);
4224 if (!name) {
4225 PyErr_NoMemory();
Victor Stinner74168972011-11-17 01:11:36 +01004226 return NULL;
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02004227 }
Victor Stinner74168972011-11-17 01:11:36 +01004228 if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname,
4229 name,
4230 &size))
4231 {
4232 PyMem_Free(name);
4233 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis72f48422010-10-29 18:20:08 +00004234 }
Victor Stinner74168972011-11-17 01:11:36 +01004235
4236 result = PyUnicode_FromWideChar(name, size);
4237 PyMem_Free(name);
4238 return result;
Martin v. Löwis72f48422010-10-29 18:20:08 +00004239#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004240 char buf[1024];
4241 int res;
4242 Py_BEGIN_ALLOW_THREADS
4243 res = gethostname(buf, (int) sizeof buf - 1);
4244 Py_END_ALLOW_THREADS
4245 if (res < 0)
4246 return set_error();
4247 buf[sizeof buf - 1] = '\0';
Victor Stinnera534fc42013-06-03 22:07:27 +02004248 return PyUnicode_DecodeFSDefault(buf);
Martin v. Löwis72f48422010-10-29 18:20:08 +00004249#endif
Guido van Rossum81194471991-07-27 21:42:02 +00004250}
Guido van Rossumff4949e1992-08-05 19:58:53 +00004251
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004252PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004253"gethostname() -> string\n\
4254\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004255Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004256
Antoine Pitrou061cfb52011-02-28 22:25:22 +00004257#ifdef HAVE_SETHOSTNAME
4258PyDoc_STRVAR(sethostname_doc,
4259"sethostname(name)\n\n\
4260Sets the hostname to name.");
4261
4262static PyObject *
4263socket_sethostname(PyObject *self, PyObject *args)
4264{
4265 PyObject *hnobj;
4266 Py_buffer buf;
4267 int res, flag = 0;
4268
Christian Heimesd2774c72013-06-19 02:06:29 +02004269#ifdef _AIX
4270/* issue #18259, not declared in any useful header file */
4271extern int sethostname(const char *, size_t);
4272#endif
4273
Antoine Pitrou061cfb52011-02-28 22:25:22 +00004274 if (!PyArg_ParseTuple(args, "S:sethostname", &hnobj)) {
4275 PyErr_Clear();
4276 if (!PyArg_ParseTuple(args, "O&:sethostname",
4277 PyUnicode_FSConverter, &hnobj))
4278 return NULL;
4279 flag = 1;
4280 }
4281 res = PyObject_GetBuffer(hnobj, &buf, PyBUF_SIMPLE);
4282 if (!res) {
4283 res = sethostname(buf.buf, buf.len);
4284 PyBuffer_Release(&buf);
4285 }
4286 if (flag)
4287 Py_DECREF(hnobj);
4288 if (res)
4289 return set_error();
4290 Py_RETURN_NONE;
4291}
4292#endif
Guido van Rossumff4949e1992-08-05 19:58:53 +00004293
Guido van Rossum30a685f1991-06-27 15:51:29 +00004294/* Python interface to gethostbyname(name). */
4295
4296/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00004297static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004298socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00004299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 char *name;
4301 sock_addr_t addrbuf;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004302 PyObject *ret = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004303
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004304 if (!PyArg_ParseTuple(args, "et:gethostbyname", "idna", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 return NULL;
4306 if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004307 goto finally;
4308 ret = makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
4309finally:
4310 PyMem_Free(name);
4311 return ret;
Guido van Rossum30a685f1991-06-27 15:51:29 +00004312}
4313
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004314PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004315"gethostbyname(host) -> address\n\
4316\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004317Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004318
4319
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004320/* Convenience function common to gethostbyname_ex and gethostbyaddr */
4321
4322static PyObject *
Victor Stinner1a62a682014-08-17 19:33:28 +02004323gethost_common(struct hostent *h, struct sockaddr *addr, size_t alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 char **pch;
4326 PyObject *rtn_tuple = (PyObject *)NULL;
4327 PyObject *name_list = (PyObject *)NULL;
4328 PyObject *addr_list = (PyObject *)NULL;
4329 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004331 if (h == NULL) {
4332 /* Let's get real error message to return */
4333 set_herror(h_errno);
4334 return NULL;
4335 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004337 if (h->h_addrtype != af) {
4338 /* Let's get real error message to return */
Victor Stinner1f33f2b2011-12-17 04:45:09 +01004339 errno = EAFNOSUPPORT;
4340 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004341 return NULL;
4342 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00004345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004346 case AF_INET:
4347 if (alen < sizeof(struct sockaddr_in))
4348 return NULL;
4349 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004350
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00004351#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004352 case AF_INET6:
4353 if (alen < sizeof(struct sockaddr_in6))
4354 return NULL;
4355 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004356#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00004357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004360 if ((name_list = PyList_New(0)) == NULL)
4361 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004363 if ((addr_list = PyList_New(0)) == NULL)
4364 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004366 /* SF #1511317: h_aliases can be NULL */
4367 if (h->h_aliases) {
4368 for (pch = h->h_aliases; *pch != NULL; pch++) {
4369 int status;
4370 tmp = PyUnicode_FromString(*pch);
4371 if (tmp == NULL)
4372 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 status = PyList_Append(name_list, tmp);
4375 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00004376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 if (status)
4378 goto err;
4379 }
4380 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004382 for (pch = h->h_addr_list; *pch != NULL; pch++) {
4383 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004385 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00004386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004387 case AF_INET:
4388 {
4389 struct sockaddr_in sin;
4390 memset(&sin, 0, sizeof(sin));
4391 sin.sin_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004392#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004393 sin.sin_len = sizeof(sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004394#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004395 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
4396 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00004397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004398 if (pch == h->h_addr_list && alen >= sizeof(sin))
4399 memcpy((char *) addr, &sin, sizeof(sin));
4400 break;
4401 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004402
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00004403#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 case AF_INET6:
4405 {
4406 struct sockaddr_in6 sin6;
4407 memset(&sin6, 0, sizeof(sin6));
4408 sin6.sin6_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004409#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004410 sin6.sin6_len = sizeof(sin6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004411#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
4413 tmp = makeipaddr((struct sockaddr *)&sin6,
4414 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00004415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004416 if (pch == h->h_addr_list && alen >= sizeof(sin6))
4417 memcpy((char *) addr, &sin6, sizeof(sin6));
4418 break;
4419 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004420#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00004421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 default: /* can't happen */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004423 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004424 "unsupported address family");
4425 return NULL;
4426 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 if (tmp == NULL)
4429 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 status = PyList_Append(addr_list, tmp);
4432 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00004433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 if (status)
4435 goto err;
4436 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004438 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00004439
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004440 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004441 Py_XDECREF(name_list);
4442 Py_XDECREF(addr_list);
4443 return rtn_tuple;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004444}
4445
4446
4447/* Python interface to gethostbyname_ex(name). */
4448
4449/*ARGSUSED*/
4450static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004451socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004453 char *name;
4454 struct hostent *h;
Charles-François Natali8b759652011-12-23 16:44:51 +01004455 sock_addr_t addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004456 struct sockaddr *sa;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004457 PyObject *ret = NULL;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004458#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004459 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004460#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004462#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 char buf[16384];
4464 int buf_len = (sizeof buf) - 1;
4465 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004466#endif
Ross Lagerwall8c159762012-03-06 21:36:18 +02004467#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004468 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004469#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004470#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004471
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004472 if (!PyArg_ParseTuple(args, "et:gethostbyname_ex", "idna", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004473 return NULL;
Charles-François Natali8b759652011-12-23 16:44:51 +01004474 if (setipaddr(name, SAS2SA(&addr), sizeof(addr), AF_INET) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004475 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004476 Py_BEGIN_ALLOW_THREADS
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004477#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004478#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Ross Lagerwall8c159762012-03-06 21:36:18 +02004479 gethostbyname_r(name, &hp_allocated, buf, buf_len,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004481#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004483#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 memset((void *) &data, '\0', sizeof(data));
4485 result = gethostbyname_r(name, &hp_allocated, &data);
4486 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004487#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004488#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00004489#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004490 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004491#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004493#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 Py_END_ALLOW_THREADS
4495 /* Some C libraries would require addr.__ss_family instead of
4496 addr.ss_family.
4497 Therefore, we cast the sockaddr_storage into sockaddr to
4498 access sa_family. */
Charles-François Natali8b759652011-12-23 16:44:51 +01004499 sa = SAS2SA(&addr);
4500 ret = gethost_common(h, SAS2SA(&addr), sizeof(addr),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00004502#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00004504#endif
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004505finally:
4506 PyMem_Free(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004508}
4509
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004510PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004511"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
4512\n\
4513Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004514for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004515
4516
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004517/* Python interface to gethostbyaddr(IP). */
4518
4519/*ARGSUSED*/
4520static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004521socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004522{
Charles-François Natali8b759652011-12-23 16:44:51 +01004523 sock_addr_t addr;
4524 struct sockaddr *sa = SAS2SA(&addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 char *ip_num;
4526 struct hostent *h;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004527 PyObject *ret = NULL;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004528#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004529 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004530#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004532#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 /* glibcs up to 2.10 assume that the buf argument to
4534 gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc
4535 does not ensure. The attribute below instructs the compiler
4536 to maintain this alignment. */
4537 char buf[16384] Py_ALIGNED(8);
4538 int buf_len = (sizeof buf) - 1;
4539 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004540#endif
Ross Lagerwall8c159762012-03-06 21:36:18 +02004541#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004543#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004544#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545 char *ap;
4546 int al;
4547 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004548
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004549 if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004550 return NULL;
4551 af = AF_UNSPEC;
4552 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004553 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004554 af = sa->sa_family;
4555 ap = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00004556 /* al = 0; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 switch (af) {
4558 case AF_INET:
4559 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
4560 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
4561 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00004562#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 case AF_INET6:
4564 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
4565 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
4566 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004567#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004568 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004569 PyErr_SetString(PyExc_OSError, "unsupported address family");
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004570 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004571 }
4572 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004573#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004574#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Ross Lagerwall8c159762012-03-06 21:36:18 +02004575 gethostbyaddr_r(ap, al, af,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004576 &hp_allocated, buf, buf_len,
4577 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004578#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004579 h = gethostbyaddr_r(ap, al, af,
4580 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004581#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004582 memset((void *) &data, '\0', sizeof(data));
4583 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
4584 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004585#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004586#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00004587#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004588 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004589#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004590 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004591#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 Py_END_ALLOW_THREADS
Charles-François Natali8b759652011-12-23 16:44:51 +01004593 ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00004594#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00004596#endif
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004597finally:
4598 PyMem_Free(ip_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004599 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004600}
4601
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004602PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004603"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
4604\n\
4605Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004606for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004607
Guido van Rossum30a685f1991-06-27 15:51:29 +00004608
4609/* Python interface to getservbyname(name).
4610 This only returns the port number, since the other info is already
4611 known or not useful (like the list of aliases). */
4612
4613/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00004614static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004615socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00004616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004617 char *name, *proto=NULL;
4618 struct servent *sp;
4619 if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
4620 return NULL;
4621 Py_BEGIN_ALLOW_THREADS
4622 sp = getservbyname(name, proto);
4623 Py_END_ALLOW_THREADS
4624 if (sp == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004625 PyErr_SetString(PyExc_OSError, "service/proto not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004626 return NULL;
4627 }
4628 return PyLong_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00004629}
4630
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004631PyDoc_STRVAR(getservbyname_doc,
Barry Warsaw11b91a02004-06-28 00:50:43 +00004632"getservbyname(servicename[, protocolname]) -> integer\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00004633\n\
4634Return a port number from a service name and protocol name.\n\
Barry Warsaw11b91a02004-06-28 00:50:43 +00004635The optional protocol name, if given, should be 'tcp' or 'udp',\n\
4636otherwise any protocol will match.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004637
Guido van Rossum30a685f1991-06-27 15:51:29 +00004638
Barry Warsaw11b91a02004-06-28 00:50:43 +00004639/* Python interface to getservbyport(port).
4640 This only returns the service name, since the other info is already
4641 known or not useful (like the list of aliases). */
4642
4643/*ARGSUSED*/
4644static PyObject *
4645socket_getservbyport(PyObject *self, PyObject *args)
4646{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004647 int port;
4648 char *proto=NULL;
4649 struct servent *sp;
4650 if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto))
4651 return NULL;
4652 if (port < 0 || port > 0xffff) {
4653 PyErr_SetString(
4654 PyExc_OverflowError,
4655 "getservbyport: port must be 0-65535.");
4656 return NULL;
4657 }
4658 Py_BEGIN_ALLOW_THREADS
4659 sp = getservbyport(htons((short)port), proto);
4660 Py_END_ALLOW_THREADS
4661 if (sp == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004662 PyErr_SetString(PyExc_OSError, "port/proto not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004663 return NULL;
4664 }
4665 return PyUnicode_FromString(sp->s_name);
Barry Warsaw11b91a02004-06-28 00:50:43 +00004666}
4667
4668PyDoc_STRVAR(getservbyport_doc,
4669"getservbyport(port[, protocolname]) -> string\n\
4670\n\
4671Return the service name from a port number and protocol name.\n\
4672The optional protocol name, if given, should be 'tcp' or 'udp',\n\
4673otherwise any protocol will match.");
4674
Guido van Rossum3901d851996-12-19 16:35:04 +00004675/* Python interface to getprotobyname(name).
4676 This only returns the protocol number, since the other info is
4677 already known or not useful (like the list of aliases). */
4678
4679/*ARGSUSED*/
4680static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004681socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00004682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004683 char *name;
4684 struct protoent *sp;
4685 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
4686 return NULL;
4687 Py_BEGIN_ALLOW_THREADS
4688 sp = getprotobyname(name);
4689 Py_END_ALLOW_THREADS
4690 if (sp == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004691 PyErr_SetString(PyExc_OSError, "protocol not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004692 return NULL;
4693 }
4694 return PyLong_FromLong((long) sp->p_proto);
Guido van Rossum3901d851996-12-19 16:35:04 +00004695}
4696
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004697PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004698"getprotobyname(name) -> integer\n\
4699\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004700Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004701
Guido van Rossum3901d851996-12-19 16:35:04 +00004702
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004703#ifndef NO_DUP
4704/* dup() function for socket fds */
4705
4706static PyObject *
4707socket_dup(PyObject *self, PyObject *fdobj)
4708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004709 SOCKET_T fd, newfd;
4710 PyObject *newfdobj;
Victor Stinnerdaf45552013-08-28 00:53:59 +02004711#ifdef MS_WINDOWS
4712 WSAPROTOCOL_INFO info;
4713#endif
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004715 fd = PyLong_AsSocket_t(fdobj);
4716 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
4717 return NULL;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004718
Victor Stinnerdaf45552013-08-28 00:53:59 +02004719#ifdef MS_WINDOWS
4720 if (WSADuplicateSocket(fd, GetCurrentProcessId(), &info))
4721 return set_error();
4722
4723 newfd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
4724 FROM_PROTOCOL_INFO,
4725 &info, 0, WSA_FLAG_OVERLAPPED);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004726 if (newfd == INVALID_SOCKET)
4727 return set_error();
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004728
Victor Stinnerdaf45552013-08-28 00:53:59 +02004729 if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) {
4730 closesocket(newfd);
4731 PyErr_SetFromWindowsErr(0);
4732 return NULL;
4733 }
4734#else
4735 /* On UNIX, dup can be used to duplicate the file descriptor of a socket */
4736 newfd = _Py_dup(fd);
4737 if (newfd == INVALID_SOCKET)
Victor Stinnere990c6e2013-11-16 00:18:58 +01004738 return NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02004739#endif
4740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004741 newfdobj = PyLong_FromSocket_t(newfd);
4742 if (newfdobj == NULL)
4743 SOCKETCLOSE(newfd);
4744 return newfdobj;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004745}
4746
4747PyDoc_STRVAR(dup_doc,
4748"dup(integer) -> integer\n\
4749\n\
4750Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\
4751sockets; on some platforms os.dup() won't work for socket file descriptors.");
4752#endif
4753
4754
Dave Cole331708b2004-08-09 04:51:41 +00004755#ifdef HAVE_SOCKETPAIR
4756/* Create a pair of sockets using the socketpair() function.
Dave Cole07fda7e2004-08-23 05:16:23 +00004757 Arguments as for socket() except the default family is AF_UNIX if
Dave Colee8bbfe42004-08-26 00:51:16 +00004758 defined on the platform; otherwise, the default is AF_INET. */
Dave Cole331708b2004-08-09 04:51:41 +00004759
4760/*ARGSUSED*/
4761static PyObject *
4762socket_socketpair(PyObject *self, PyObject *args)
4763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004764 PySocketSockObject *s0 = NULL, *s1 = NULL;
4765 SOCKET_T sv[2];
4766 int family, type = SOCK_STREAM, proto = 0;
4767 PyObject *res = NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02004768#ifdef SOCK_CLOEXEC
4769 int *atomic_flag_works = &sock_cloexec_works;
4770#else
4771 int *atomic_flag_works = NULL;
4772#endif
4773 int ret;
Dave Cole331708b2004-08-09 04:51:41 +00004774
4775#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004776 family = AF_UNIX;
Dave Cole331708b2004-08-09 04:51:41 +00004777#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004778 family = AF_INET;
Dave Cole331708b2004-08-09 04:51:41 +00004779#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004780 if (!PyArg_ParseTuple(args, "|iii:socketpair",
4781 &family, &type, &proto))
4782 return NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02004783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004784 /* Create a pair of socket fds */
Victor Stinnerdaf45552013-08-28 00:53:59 +02004785 Py_BEGIN_ALLOW_THREADS
4786#ifdef SOCK_CLOEXEC
4787 if (sock_cloexec_works != 0) {
4788 ret = socketpair(family, type | SOCK_CLOEXEC, proto, sv);
4789 if (sock_cloexec_works == -1) {
4790 if (ret >= 0) {
4791 sock_cloexec_works = 1;
4792 }
4793 else if (errno == EINVAL) {
4794 /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */
4795 sock_cloexec_works = 0;
4796 ret = socketpair(family, type, proto, sv);
4797 }
4798 }
4799 }
4800 else
4801#endif
4802 {
4803 ret = socketpair(family, type, proto, sv);
4804 }
4805 Py_END_ALLOW_THREADS
4806
4807 if (ret < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004808 return set_error();
Victor Stinnerdaf45552013-08-28 00:53:59 +02004809
4810 if (_Py_set_inheritable(sv[0], 0, atomic_flag_works) < 0)
4811 goto finally;
4812 if (_Py_set_inheritable(sv[1], 0, atomic_flag_works) < 0)
4813 goto finally;
4814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004815 s0 = new_sockobject(sv[0], family, type, proto);
4816 if (s0 == NULL)
4817 goto finally;
4818 s1 = new_sockobject(sv[1], family, type, proto);
4819 if (s1 == NULL)
4820 goto finally;
4821 res = PyTuple_Pack(2, s0, s1);
Dave Cole331708b2004-08-09 04:51:41 +00004822
4823finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004824 if (res == NULL) {
4825 if (s0 == NULL)
4826 SOCKETCLOSE(sv[0]);
4827 if (s1 == NULL)
4828 SOCKETCLOSE(sv[1]);
4829 }
4830 Py_XDECREF(s0);
4831 Py_XDECREF(s1);
4832 return res;
Dave Cole331708b2004-08-09 04:51:41 +00004833}
4834
4835PyDoc_STRVAR(socketpair_doc,
Victor Stinnerdaf45552013-08-28 00:53:59 +02004836"socketpair([family[, type [, proto]]]) -> (socket object, socket object)\n\
Dave Cole331708b2004-08-09 04:51:41 +00004837\n\
4838Create a pair of socket objects from the sockets returned by the platform\n\
4839socketpair() function.\n\
Dave Cole07fda7e2004-08-23 05:16:23 +00004840The arguments are the same as for socket() except the default family is\n\
Dave Colee8bbfe42004-08-26 00:51:16 +00004841AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
Dave Cole331708b2004-08-09 04:51:41 +00004842
4843#endif /* HAVE_SOCKETPAIR */
4844
4845
Guido van Rossum006bf911996-06-12 04:04:55 +00004846static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004847socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00004848{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004849 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00004850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004851 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
4852 return NULL;
4853 }
4854 if (x1 < 0) {
4855 PyErr_SetString(PyExc_OverflowError,
4856 "can't convert negative number to unsigned long");
4857 return NULL;
4858 }
4859 x2 = (unsigned int)ntohs((unsigned short)x1);
4860 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00004861}
4862
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004863PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004864"ntohs(integer) -> integer\n\
4865\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004866Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004867
4868
Guido van Rossum006bf911996-06-12 04:04:55 +00004869static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004870socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00004871{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004872 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00004873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004874 if (PyLong_Check(arg)) {
4875 x = PyLong_AsUnsignedLong(arg);
4876 if (x == (unsigned long) -1 && PyErr_Occurred())
4877 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004878#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004879 {
4880 unsigned long y;
4881 /* only want the trailing 32 bits */
4882 y = x & 0xFFFFFFFFUL;
4883 if (y ^ x)
4884 return PyErr_Format(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03004885 "int larger than 32 bits");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004886 x = y;
4887 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004888#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004889 }
4890 else
4891 return PyErr_Format(PyExc_TypeError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03004892 "expected int, %s found",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004893 Py_TYPE(arg)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004894 return PyLong_FromUnsignedLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00004895}
4896
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004897PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004898"ntohl(integer) -> integer\n\
4899\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004900Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004901
4902
Guido van Rossum006bf911996-06-12 04:04:55 +00004903static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004904socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00004905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004906 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00004907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004908 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
4909 return NULL;
4910 }
4911 if (x1 < 0) {
4912 PyErr_SetString(PyExc_OverflowError,
4913 "can't convert negative number to unsigned long");
4914 return NULL;
4915 }
4916 x2 = (unsigned int)htons((unsigned short)x1);
4917 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00004918}
4919
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004920PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004921"htons(integer) -> integer\n\
4922\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004923Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004924
4925
Guido van Rossum006bf911996-06-12 04:04:55 +00004926static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004927socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00004928{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004929 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00004930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004931 if (PyLong_Check(arg)) {
4932 x = PyLong_AsUnsignedLong(arg);
4933 if (x == (unsigned long) -1 && PyErr_Occurred())
4934 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004935#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004936 {
4937 unsigned long y;
4938 /* only want the trailing 32 bits */
4939 y = x & 0xFFFFFFFFUL;
4940 if (y ^ x)
4941 return PyErr_Format(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03004942 "int larger than 32 bits");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004943 x = y;
4944 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004945#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004946 }
4947 else
4948 return PyErr_Format(PyExc_TypeError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03004949 "expected int, %s found",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004950 Py_TYPE(arg)->tp_name);
4951 return PyLong_FromUnsignedLong(htonl((unsigned long)x));
Guido van Rossum006bf911996-06-12 04:04:55 +00004952}
4953
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004954PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004955"htonl(integer) -> integer\n\
4956\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004957Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004958
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004959/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004960
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004961PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum7d0a8262007-05-21 23:13:11 +00004962"inet_aton(string) -> bytes giving packed 32-bit IP representation\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004963\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00004964Convert 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 +00004965binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004966
4967static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004968socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004969{
Neal Norwitz88f115b2003-02-13 02:15:42 +00004970#ifdef HAVE_INET_ATON
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004971 struct in_addr buf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00004972#endif
4973
4974#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
Benjamin Petersonf91df042009-02-13 02:50:59 +00004975#if (SIZEOF_INT != 4)
4976#error "Not sure if in_addr_t exists and int is not 32-bits."
4977#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004978 /* Have to use inet_addr() instead */
4979 unsigned int packed_addr;
Tim Peters1df9fdd2003-02-13 03:13:40 +00004980#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004981 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00004982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004983 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
4984 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00004985
Tim Peters1df9fdd2003-02-13 03:13:40 +00004986
4987#ifdef HAVE_INET_ATON
Thomas Wouters477c8d52006-05-27 19:21:47 +00004988
4989#ifdef USE_INET_ATON_WEAKLINK
4990 if (inet_aton != NULL) {
4991#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004992 if (inet_aton(ip_addr, &buf))
4993 return PyBytes_FromStringAndSize((char *)(&buf),
4994 sizeof(buf));
Guido van Rossumad05cdf2003-02-12 23:08:22 +00004995
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004996 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004997 "illegal IP address string passed to inet_aton");
4998 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00004999
Thomas Wouters477c8d52006-05-27 19:21:47 +00005000#ifdef USE_INET_ATON_WEAKLINK
5001 } else {
5002#endif
5003
5004#endif
5005
5006#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
5007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005008 /* special-case this address as inet_addr might return INADDR_NONE
5009 * for this */
5010 if (strcmp(ip_addr, "255.255.255.255") == 0) {
Charles-François Natali24aa0412013-08-31 14:48:25 +02005011 packed_addr = INADDR_BROADCAST;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005012 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00005013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005014 packed_addr = inet_addr(ip_addr);
Guido van Rossumc6a164b1999-08-20 19:11:27 +00005015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005016 if (packed_addr == INADDR_NONE) { /* invalid address */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005017 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005018 "illegal IP address string passed to inet_aton");
5019 return NULL;
5020 }
5021 }
5022 return PyBytes_FromStringAndSize((char *) &packed_addr,
5023 sizeof(packed_addr));
Thomas Wouters477c8d52006-05-27 19:21:47 +00005024
5025#ifdef USE_INET_ATON_WEAKLINK
5026 }
5027#endif
5028
Guido van Rossumad05cdf2003-02-12 23:08:22 +00005029#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005030}
5031
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005032PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00005033"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005034\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005035Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005036
5037static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005038socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005040 char *packed_str;
5041 int addr_len;
5042 struct in_addr packed_addr;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005044 if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) {
5045 return NULL;
5046 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00005047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005048 if (addr_len != sizeof(packed_addr)) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005049 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005050 "packed IP wrong length for inet_ntoa");
5051 return NULL;
5052 }
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005054 memcpy(&packed_addr, packed_str, addr_len);
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005056 return PyUnicode_FromString(inet_ntoa(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005057}
Guido van Rossum82a5c661998-07-07 20:45:43 +00005058
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005059#if defined(HAVE_INET_PTON) || defined(MS_WINDOWS)
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005060
5061PyDoc_STRVAR(inet_pton_doc,
5062"inet_pton(af, ip) -> packed IP address string\n\
5063\n\
5064Convert an IP address from string format to a packed string suitable\n\
5065for use with low-level network functions.");
5066
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005067#endif
5068
5069#ifdef HAVE_INET_PTON
5070
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005071static PyObject *
5072socket_inet_pton(PyObject *self, PyObject *args)
5073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005074 int af;
5075 char* ip;
5076 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005077#ifdef ENABLE_IPV6
Victor Stinner640c35c2013-06-04 23:14:37 +02005078 char packed[Py_MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005079#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005080 char packed[sizeof(struct in_addr)];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005081#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005082 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
5083 return NULL;
5084 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005085
Martin v. Löwis04697e82004-06-02 12:35:29 +00005086#if !defined(ENABLE_IPV6) && defined(AF_INET6)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005087 if(af == AF_INET6) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005088 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005089 "can't use AF_INET6, IPv6 is disabled");
5090 return NULL;
5091 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00005092#endif
Martin v. Löwis10649092003-08-05 06:25:06 +00005093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005094 retval = inet_pton(af, ip, packed);
5095 if (retval < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005096 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005097 return NULL;
5098 } else if (retval == 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005099 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005100 "illegal IP address string passed to inet_pton");
5101 return NULL;
5102 } else if (af == AF_INET) {
5103 return PyBytes_FromStringAndSize(packed,
5104 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005105#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005106 } else if (af == AF_INET6) {
5107 return PyBytes_FromStringAndSize(packed,
5108 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005109#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005110 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005111 PyErr_SetString(PyExc_OSError, "unknown address family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005112 return NULL;
5113 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005114}
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005115#elif defined(MS_WINDOWS)
5116
5117static PyObject *
5118socket_inet_pton(PyObject *self, PyObject *args)
5119{
5120 int af;
5121 char* ip;
5122 struct sockaddr_in6 addr;
5123 INT ret, size;
5124
5125 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
5126 return NULL;
5127 }
5128
Victor Stinnere990c6e2013-11-16 00:18:58 +01005129 size = sizeof(addr);
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005130 ret = WSAStringToAddressA(ip, af, NULL, (LPSOCKADDR)&addr, &size);
5131
5132 if (ret) {
5133 PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
5134 return NULL;
5135 } else if(af == AF_INET) {
5136 struct sockaddr_in *addr4 = (struct sockaddr_in*)&addr;
Victor Stinnere990c6e2013-11-16 00:18:58 +01005137 return PyBytes_FromStringAndSize((const char *)&(addr4->sin_addr),
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005138 sizeof(addr4->sin_addr));
5139 } else if (af == AF_INET6) {
Victor Stinnere990c6e2013-11-16 00:18:58 +01005140 return PyBytes_FromStringAndSize((const char *)&(addr.sin6_addr),
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005141 sizeof(addr.sin6_addr));
5142 } else {
5143 PyErr_SetString(PyExc_OSError, "unknown address family");
5144 return NULL;
5145 }
5146}
5147
5148#endif
5149
5150#if defined(HAVE_INET_PTON) || defined(MS_WINDOWS)
Thomas Wouters477c8d52006-05-27 19:21:47 +00005151
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005152PyDoc_STRVAR(inet_ntop_doc,
5153"inet_ntop(af, packed_ip) -> string formatted IP address\n\
5154\n\
5155Convert a packed IP address of the given family to string format.");
5156
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005157#endif
5158
5159
5160#ifdef HAVE_INET_PTON
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005161static PyObject *
5162socket_inet_ntop(PyObject *self, PyObject *args)
5163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005164 int af;
5165 char* packed;
5166 int len;
5167 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005168#ifdef ENABLE_IPV6
Victor Stinner640c35c2013-06-04 23:14:37 +02005169 char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005170#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005171 char ip[INET_ADDRSTRLEN + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005172#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00005173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005174 /* Guarantee NUL-termination for PyUnicode_FromString() below */
5175 memset((void *) &ip[0], '\0', sizeof(ip));
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005177 if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) {
5178 return NULL;
5179 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005181 if (af == AF_INET) {
5182 if (len != sizeof(struct in_addr)) {
5183 PyErr_SetString(PyExc_ValueError,
5184 "invalid length of packed IP address string");
5185 return NULL;
5186 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005187#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005188 } else if (af == AF_INET6) {
5189 if (len != sizeof(struct in6_addr)) {
5190 PyErr_SetString(PyExc_ValueError,
5191 "invalid length of packed IP address string");
5192 return NULL;
5193 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005194#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005195 } else {
5196 PyErr_Format(PyExc_ValueError,
5197 "unknown address family %d", af);
5198 return NULL;
5199 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005201 retval = inet_ntop(af, packed, ip, sizeof(ip));
5202 if (!retval) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005203 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005204 return NULL;
5205 } else {
5206 return PyUnicode_FromString(retval);
5207 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005209 /* NOTREACHED */
5210 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
5211 return NULL;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005212}
5213
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005214#elif defined(MS_WINDOWS)
5215
5216static PyObject *
5217socket_inet_ntop(PyObject *self, PyObject *args)
5218{
5219 int af;
5220 char* packed;
5221 int len;
5222 struct sockaddr_in6 addr;
5223 DWORD addrlen, ret, retlen;
Jason R. Coombs8ec784c2013-11-10 13:43:22 -05005224#ifdef ENABLE_IPV6
5225 char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
5226#else
5227 char ip[INET_ADDRSTRLEN + 1];
5228#endif
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005229
5230 /* Guarantee NUL-termination for PyUnicode_FromString() below */
5231 memset((void *) &ip[0], '\0', sizeof(ip));
5232
5233 if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) {
5234 return NULL;
5235 }
5236
5237 if (af == AF_INET) {
5238 struct sockaddr_in * addr4 = (struct sockaddr_in *)&addr;
5239
5240 if (len != sizeof(struct in_addr)) {
5241 PyErr_SetString(PyExc_ValueError,
5242 "invalid length of packed IP address string");
5243 return NULL;
5244 }
5245 memset(addr4, 0, sizeof(struct sockaddr_in));
5246 addr4->sin_family = AF_INET;
5247 memcpy(&(addr4->sin_addr), packed, sizeof(addr4->sin_addr));
5248 addrlen = sizeof(struct sockaddr_in);
5249 } else if (af == AF_INET6) {
5250 if (len != sizeof(struct in6_addr)) {
5251 PyErr_SetString(PyExc_ValueError,
5252 "invalid length of packed IP address string");
5253 return NULL;
5254 }
5255
5256 memset(&addr, 0, sizeof(addr));
5257 addr.sin6_family = AF_INET6;
5258 memcpy(&(addr.sin6_addr), packed, sizeof(addr.sin6_addr));
5259 addrlen = sizeof(addr);
5260 } else {
5261 PyErr_Format(PyExc_ValueError,
5262 "unknown address family %d", af);
5263 return NULL;
5264 }
5265
5266 retlen = sizeof(ip);
Victor Stinnere990c6e2013-11-16 00:18:58 +01005267 ret = WSAAddressToStringA((struct sockaddr*)&addr, addrlen, NULL,
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005268 ip, &retlen);
5269
5270 if (ret) {
5271 PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
5272 return NULL;
5273 } else {
5274 return PyUnicode_FromString(ip);
5275 }
5276}
5277
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005278#endif /* HAVE_INET_PTON */
5279
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005280/* Python interface to getaddrinfo(host, port). */
5281
5282/*ARGSUSED*/
5283static PyObject *
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005284socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005285{
Victor Stinner77af1722011-05-26 14:05:59 +02005286 static char* kwnames[] = {"host", "port", "family", "type", "proto",
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005287 "flags", 0};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005288 struct addrinfo hints, *res;
5289 struct addrinfo *res0 = NULL;
5290 PyObject *hobj = NULL;
5291 PyObject *pobj = (PyObject *)NULL;
5292 char pbuf[30];
5293 char *hptr, *pptr;
5294 int family, socktype, protocol, flags;
5295 int error;
5296 PyObject *all = (PyObject *)NULL;
5297 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005298
Georg Brandl6083a4b2013-10-14 06:51:46 +02005299 socktype = protocol = flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005300 family = AF_UNSPEC;
Victor Stinner77af1722011-05-26 14:05:59 +02005301 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iiii:getaddrinfo",
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005302 kwnames, &hobj, &pobj, &family, &socktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005303 &protocol, &flags)) {
5304 return NULL;
5305 }
5306 if (hobj == Py_None) {
5307 hptr = NULL;
5308 } else if (PyUnicode_Check(hobj)) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005309 _Py_IDENTIFIER(encode);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005310
5311 idna = _PyObject_CallMethodId(hobj, &PyId_encode, "s", "idna");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005312 if (!idna)
5313 return NULL;
5314 assert(PyBytes_Check(idna));
5315 hptr = PyBytes_AS_STRING(idna);
5316 } else if (PyBytes_Check(hobj)) {
5317 hptr = PyBytes_AsString(hobj);
5318 } else {
5319 PyErr_SetString(PyExc_TypeError,
5320 "getaddrinfo() argument 1 must be string or None");
5321 return NULL;
5322 }
5323 if (PyLong_CheckExact(pobj)) {
5324 long value = PyLong_AsLong(pobj);
5325 if (value == -1 && PyErr_Occurred())
5326 goto err;
5327 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value);
5328 pptr = pbuf;
5329 } else if (PyUnicode_Check(pobj)) {
5330 pptr = _PyUnicode_AsString(pobj);
Alexander Belopolskye239d232010-12-08 23:31:48 +00005331 if (pptr == NULL)
5332 goto err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005333 } else if (PyBytes_Check(pobj)) {
Alexander Belopolskye239d232010-12-08 23:31:48 +00005334 pptr = PyBytes_AS_STRING(pobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005335 } else if (pobj == Py_None) {
5336 pptr = (char *)NULL;
5337 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005338 PyErr_SetString(PyExc_OSError, "Int or String expected");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005339 goto err;
5340 }
Ronald Oussorena822d3662013-06-10 10:36:28 +02005341#if defined(__APPLE__) && defined(AI_NUMERICSERV)
Ronald Oussoren27a4ac52013-05-24 13:47:37 +02005342 if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) {
5343 /* On OSX upto at least OSX 10.8 getaddrinfo crashes
Christian Heimesaf01f662013-12-21 16:19:10 +01005344 * if AI_NUMERICSERV is set and the servname is NULL or "0".
5345 * This workaround avoids a segfault in libsystem.
5346 */
Ronald Oussoren27a4ac52013-05-24 13:47:37 +02005347 pptr = "00";
5348 }
5349#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005350 memset(&hints, 0, sizeof(hints));
5351 hints.ai_family = family;
5352 hints.ai_socktype = socktype;
5353 hints.ai_protocol = protocol;
5354 hints.ai_flags = flags;
5355 Py_BEGIN_ALLOW_THREADS
5356 ACQUIRE_GETADDRINFO_LOCK
5357 error = getaddrinfo(hptr, pptr, &hints, &res0);
5358 Py_END_ALLOW_THREADS
5359 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
5360 if (error) {
5361 set_gaierror(error);
5362 goto err;
5363 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005365 if ((all = PyList_New(0)) == NULL)
5366 goto err;
5367 for (res = res0; res; res = res->ai_next) {
5368 PyObject *single;
5369 PyObject *addr =
5370 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
5371 if (addr == NULL)
5372 goto err;
5373 single = Py_BuildValue("iiisO", res->ai_family,
5374 res->ai_socktype, res->ai_protocol,
5375 res->ai_canonname ? res->ai_canonname : "",
5376 addr);
5377 Py_DECREF(addr);
5378 if (single == NULL)
5379 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005381 if (PyList_Append(all, single))
5382 goto err;
5383 Py_XDECREF(single);
5384 }
5385 Py_XDECREF(idna);
5386 if (res0)
5387 freeaddrinfo(res0);
5388 return all;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005389 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005390 Py_XDECREF(all);
5391 Py_XDECREF(idna);
5392 if (res0)
5393 freeaddrinfo(res0);
5394 return (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005395}
5396
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005397PyDoc_STRVAR(getaddrinfo_doc,
Berker Peksaga6ec5ee2014-06-30 11:28:40 +03005398"getaddrinfo(host, port [, family, type, proto, flags])\n\
5399 -> list of (family, type, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005400\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005401Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005402
5403/* Python interface to getnameinfo(sa, flags). */
5404
5405/*ARGSUSED*/
5406static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005407socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005409 PyObject *sa = (PyObject *)NULL;
5410 int flags;
5411 char *hostp;
Charles-François Natali366999a2012-01-02 15:47:29 +01005412 int port;
5413 unsigned int flowinfo, scope_id;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005414 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
5415 struct addrinfo hints, *res = NULL;
5416 int error;
5417 PyObject *ret = (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005419 flags = flowinfo = scope_id = 0;
5420 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
5421 return NULL;
5422 if (!PyTuple_Check(sa)) {
5423 PyErr_SetString(PyExc_TypeError,
5424 "getnameinfo() argument 1 must be a tuple");
5425 return NULL;
5426 }
Charles-François Natali366999a2012-01-02 15:47:29 +01005427 if (!PyArg_ParseTuple(sa, "si|II",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005428 &hostp, &port, &flowinfo, &scope_id))
5429 return NULL;
Charles-François Natali9b0c0062012-06-23 10:17:05 +02005430 if (flowinfo > 0xfffff) {
Charles-François Natali366999a2012-01-02 15:47:29 +01005431 PyErr_SetString(PyExc_OverflowError,
5432 "getsockaddrarg: flowinfo must be 0-1048575.");
5433 return NULL;
5434 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005435 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
5436 memset(&hints, 0, sizeof(hints));
5437 hints.ai_family = AF_UNSPEC;
5438 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Martin v. Löwis112c0f32010-08-25 07:38:15 +00005439 hints.ai_flags = AI_NUMERICHOST; /* don't do any name resolution */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005440 Py_BEGIN_ALLOW_THREADS
5441 ACQUIRE_GETADDRINFO_LOCK
5442 error = getaddrinfo(hostp, pbuf, &hints, &res);
5443 Py_END_ALLOW_THREADS
5444 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
5445 if (error) {
5446 set_gaierror(error);
5447 goto fail;
5448 }
5449 if (res->ai_next) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005450 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005451 "sockaddr resolved to multiple addresses");
5452 goto fail;
5453 }
5454 switch (res->ai_family) {
5455 case AF_INET:
5456 {
5457 if (PyTuple_GET_SIZE(sa) != 2) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005458 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005459 "IPv4 sockaddr must be 2 tuple");
5460 goto fail;
5461 }
5462 break;
5463 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00005464#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005465 case AF_INET6:
5466 {
5467 struct sockaddr_in6 *sin6;
5468 sin6 = (struct sockaddr_in6 *)res->ai_addr;
Charles-François Natali366999a2012-01-02 15:47:29 +01005469 sin6->sin6_flowinfo = htonl(flowinfo);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005470 sin6->sin6_scope_id = scope_id;
5471 break;
5472 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005473#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005474 }
Antoine Pitrou19467d22010-08-17 19:33:30 +00005475 error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005476 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
5477 if (error) {
5478 set_gaierror(error);
5479 goto fail;
5480 }
5481 ret = Py_BuildValue("ss", hbuf, pbuf);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005482
5483fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005484 if (res)
5485 freeaddrinfo(res);
5486 return ret;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005487}
5488
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005489PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005490"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005491\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005492Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005493
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005494
5495/* Python API to getting and setting the default timeout value. */
5496
5497static PyObject *
5498socket_getdefaulttimeout(PyObject *self)
5499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005500 if (defaulttimeout < 0.0) {
5501 Py_INCREF(Py_None);
5502 return Py_None;
5503 }
5504 else
5505 return PyFloat_FromDouble(defaulttimeout);
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005506}
5507
5508PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005509"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005510\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03005511Returns the default timeout in seconds (float) for new socket objects.\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005512A value of None indicates that new socket objects have no timeout.\n\
5513When the socket module is first imported, the default is None.");
5514
5515static PyObject *
5516socket_setdefaulttimeout(PyObject *self, PyObject *arg)
5517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005518 double timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005520 if (arg == Py_None)
5521 timeout = -1.0;
5522 else {
5523 timeout = PyFloat_AsDouble(arg);
5524 if (timeout < 0.0) {
5525 if (!PyErr_Occurred())
5526 PyErr_SetString(PyExc_ValueError,
5527 "Timeout value out of range");
5528 return NULL;
5529 }
5530 }
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005532 defaulttimeout = timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005534 Py_INCREF(Py_None);
5535 return Py_None;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005536}
5537
5538PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005539"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005540\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03005541Set the default timeout in seconds (float) for new socket objects.\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005542A value of None indicates that new socket objects have no timeout.\n\
5543When the socket module is first imported, the default is None.");
5544
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005545#ifdef HAVE_IF_NAMEINDEX
5546/* Python API for getting interface indices and names */
5547
5548static PyObject *
5549socket_if_nameindex(PyObject *self, PyObject *arg)
5550{
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005551 PyObject *list;
Charles-François Natali60713592011-05-20 16:55:06 +02005552 int i;
5553 struct if_nameindex *ni;
Victor Stinner77af1722011-05-26 14:05:59 +02005554
Charles-François Natali60713592011-05-20 16:55:06 +02005555 ni = if_nameindex();
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005556 if (ni == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005557 PyErr_SetFromErrno(PyExc_OSError);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005558 return NULL;
5559 }
5560
5561 list = PyList_New(0);
5562 if (list == NULL) {
5563 if_freenameindex(ni);
5564 return NULL;
5565 }
5566
Charles-François Natali60713592011-05-20 16:55:06 +02005567 for (i = 0; ni[i].if_index != 0 && i < INT_MAX; i++) {
5568 PyObject *ni_tuple = Py_BuildValue("IO&",
5569 ni[i].if_index, PyUnicode_DecodeFSDefault, ni[i].if_name);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005570
5571 if (ni_tuple == NULL || PyList_Append(list, ni_tuple) == -1) {
5572 Py_XDECREF(ni_tuple);
Charles-François Natali60713592011-05-20 16:55:06 +02005573 Py_DECREF(list);
5574 if_freenameindex(ni);
5575 return NULL;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005576 }
5577 Py_DECREF(ni_tuple);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005578 }
5579
5580 if_freenameindex(ni);
5581 return list;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005582}
5583
5584PyDoc_STRVAR(if_nameindex_doc,
5585"if_nameindex()\n\
5586\n\
5587Returns a list of network interface information (index, name) tuples.");
5588
Charles-François Natali60713592011-05-20 16:55:06 +02005589static PyObject *
5590socket_if_nametoindex(PyObject *self, PyObject *args)
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005591{
Charles-François Natali60713592011-05-20 16:55:06 +02005592 PyObject *oname;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005593 unsigned long index;
5594
Charles-François Natali60713592011-05-20 16:55:06 +02005595 if (!PyArg_ParseTuple(args, "O&:if_nametoindex",
5596 PyUnicode_FSConverter, &oname))
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005597 return NULL;
5598
Charles-François Natali60713592011-05-20 16:55:06 +02005599 index = if_nametoindex(PyBytes_AS_STRING(oname));
5600 Py_DECREF(oname);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005601 if (index == 0) {
Charles-François Natali60713592011-05-20 16:55:06 +02005602 /* if_nametoindex() doesn't set errno */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005603 PyErr_SetString(PyExc_OSError, "no interface with this name");
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005604 return NULL;
5605 }
5606
5607 return PyLong_FromUnsignedLong(index);
5608}
5609
5610PyDoc_STRVAR(if_nametoindex_doc,
5611"if_nametoindex(if_name)\n\
5612\n\
5613Returns the interface index corresponding to the interface name if_name.");
5614
Charles-François Natali60713592011-05-20 16:55:06 +02005615static PyObject *
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005616socket_if_indextoname(PyObject *self, PyObject *arg)
5617{
Charles-François Natali60713592011-05-20 16:55:06 +02005618 unsigned long index;
5619 char name[IF_NAMESIZE + 1];
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005620
Charles-François Natali60713592011-05-20 16:55:06 +02005621 index = PyLong_AsUnsignedLong(arg);
5622 if (index == (unsigned long) -1)
5623 return NULL;
5624
5625 if (if_indextoname(index, name) == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005626 PyErr_SetFromErrno(PyExc_OSError);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005627 return NULL;
5628 }
5629
Charles-François Natali60713592011-05-20 16:55:06 +02005630 return PyUnicode_DecodeFSDefault(name);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005631}
5632
5633PyDoc_STRVAR(if_indextoname_doc,
5634"if_indextoname(if_index)\n\
5635\n\
5636Returns the interface name corresponding to the interface index if_index.");
5637
5638#endif /* HAVE_IF_NAMEINDEX */
5639
5640
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005641#ifdef CMSG_LEN
5642/* Python interface to CMSG_LEN(length). */
5643
5644static PyObject *
5645socket_CMSG_LEN(PyObject *self, PyObject *args)
5646{
5647 Py_ssize_t length;
5648 size_t result;
5649
5650 if (!PyArg_ParseTuple(args, "n:CMSG_LEN", &length))
5651 return NULL;
5652 if (length < 0 || !get_CMSG_LEN(length, &result)) {
5653 PyErr_Format(PyExc_OverflowError, "CMSG_LEN() argument out of range");
5654 return NULL;
5655 }
5656 return PyLong_FromSize_t(result);
5657}
5658
5659PyDoc_STRVAR(CMSG_LEN_doc,
5660"CMSG_LEN(length) -> control message length\n\
5661\n\
5662Return the total length, without trailing padding, of an ancillary\n\
5663data item with associated data of the given length. This value can\n\
5664often be used as the buffer size for recvmsg() to receive a single\n\
5665item of ancillary data, but RFC 3542 requires portable applications to\n\
5666use CMSG_SPACE() and thus include space for padding, even when the\n\
5667item will be the last in the buffer. Raises OverflowError if length\n\
5668is outside the permissible range of values.");
5669
5670
5671#ifdef CMSG_SPACE
5672/* Python interface to CMSG_SPACE(length). */
5673
5674static PyObject *
5675socket_CMSG_SPACE(PyObject *self, PyObject *args)
5676{
5677 Py_ssize_t length;
5678 size_t result;
5679
5680 if (!PyArg_ParseTuple(args, "n:CMSG_SPACE", &length))
5681 return NULL;
5682 if (length < 0 || !get_CMSG_SPACE(length, &result)) {
5683 PyErr_SetString(PyExc_OverflowError,
5684 "CMSG_SPACE() argument out of range");
5685 return NULL;
5686 }
5687 return PyLong_FromSize_t(result);
5688}
5689
5690PyDoc_STRVAR(CMSG_SPACE_doc,
5691"CMSG_SPACE(length) -> buffer size\n\
5692\n\
5693Return the buffer size needed for recvmsg() to receive an ancillary\n\
5694data item with associated data of the given length, along with any\n\
5695trailing padding. The buffer space needed to receive multiple items\n\
5696is the sum of the CMSG_SPACE() values for their associated data\n\
5697lengths. Raises OverflowError if length is outside the permissible\n\
5698range of values.");
5699#endif /* CMSG_SPACE */
5700#endif /* CMSG_LEN */
5701
5702
Guido van Rossum30a685f1991-06-27 15:51:29 +00005703/* List of functions exported by this module. */
5704
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005705static PyMethodDef socket_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005706 {"gethostbyname", socket_gethostbyname,
5707 METH_VARARGS, gethostbyname_doc},
5708 {"gethostbyname_ex", socket_gethostbyname_ex,
5709 METH_VARARGS, ghbn_ex_doc},
5710 {"gethostbyaddr", socket_gethostbyaddr,
5711 METH_VARARGS, gethostbyaddr_doc},
5712 {"gethostname", socket_gethostname,
5713 METH_NOARGS, gethostname_doc},
Antoine Pitrou061cfb52011-02-28 22:25:22 +00005714#ifdef HAVE_SETHOSTNAME
5715 {"sethostname", socket_sethostname,
5716 METH_VARARGS, sethostname_doc},
5717#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005718 {"getservbyname", socket_getservbyname,
5719 METH_VARARGS, getservbyname_doc},
5720 {"getservbyport", socket_getservbyport,
5721 METH_VARARGS, getservbyport_doc},
5722 {"getprotobyname", socket_getprotobyname,
5723 METH_VARARGS, getprotobyname_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005724#ifndef NO_DUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005725 {"dup", socket_dup,
5726 METH_O, dup_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005727#endif
Dave Cole331708b2004-08-09 04:51:41 +00005728#ifdef HAVE_SOCKETPAIR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005729 {"socketpair", socket_socketpair,
5730 METH_VARARGS, socketpair_doc},
Dave Cole331708b2004-08-09 04:51:41 +00005731#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005732 {"ntohs", socket_ntohs,
5733 METH_VARARGS, ntohs_doc},
5734 {"ntohl", socket_ntohl,
5735 METH_O, ntohl_doc},
5736 {"htons", socket_htons,
5737 METH_VARARGS, htons_doc},
5738 {"htonl", socket_htonl,
5739 METH_O, htonl_doc},
5740 {"inet_aton", socket_inet_aton,
5741 METH_VARARGS, inet_aton_doc},
5742 {"inet_ntoa", socket_inet_ntoa,
5743 METH_VARARGS, inet_ntoa_doc},
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005744#if defined(HAVE_INET_PTON) || defined(MS_WINDOWS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005745 {"inet_pton", socket_inet_pton,
5746 METH_VARARGS, inet_pton_doc},
5747 {"inet_ntop", socket_inet_ntop,
5748 METH_VARARGS, inet_ntop_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005749#endif
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005750 {"getaddrinfo", (PyCFunction)socket_getaddrinfo,
5751 METH_VARARGS | METH_KEYWORDS, getaddrinfo_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005752 {"getnameinfo", socket_getnameinfo,
5753 METH_VARARGS, getnameinfo_doc},
5754 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
5755 METH_NOARGS, getdefaulttimeout_doc},
5756 {"setdefaulttimeout", socket_setdefaulttimeout,
5757 METH_O, setdefaulttimeout_doc},
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005758#ifdef HAVE_IF_NAMEINDEX
5759 {"if_nameindex", socket_if_nameindex,
5760 METH_NOARGS, if_nameindex_doc},
5761 {"if_nametoindex", socket_if_nametoindex,
Charles-François Natali60713592011-05-20 16:55:06 +02005762 METH_VARARGS, if_nametoindex_doc},
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005763 {"if_indextoname", socket_if_indextoname,
5764 METH_O, if_indextoname_doc},
5765#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005766#ifdef CMSG_LEN
5767 {"CMSG_LEN", socket_CMSG_LEN,
5768 METH_VARARGS, CMSG_LEN_doc},
5769#ifdef CMSG_SPACE
5770 {"CMSG_SPACE", socket_CMSG_SPACE,
5771 METH_VARARGS, CMSG_SPACE_doc},
5772#endif
5773#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005774 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005775};
5776
Guido van Rossum30a685f1991-06-27 15:51:29 +00005777
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005778#ifdef MS_WINDOWS
5779#define OS_INIT_DEFINED
5780
5781/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00005782
5783static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005784os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00005785{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005786 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00005787}
5788
5789static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005790os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00005791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005792 WSADATA WSAData;
5793 int ret;
5794 ret = WSAStartup(0x0101, &WSAData);
5795 switch (ret) {
5796 case 0: /* No error */
5797 Py_AtExit(os_cleanup);
5798 return 1; /* Success */
5799 case WSASYSNOTREADY:
5800 PyErr_SetString(PyExc_ImportError,
5801 "WSAStartup failed: network not ready");
5802 break;
5803 case WSAVERNOTSUPPORTED:
5804 case WSAEINVAL:
5805 PyErr_SetString(
5806 PyExc_ImportError,
5807 "WSAStartup failed: requested version not supported");
5808 break;
5809 default:
5810 PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret);
5811 break;
5812 }
5813 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00005814}
5815
Guido van Rossum8d665e61996-06-26 18:22:49 +00005816#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00005817
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005818
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005819
5820#ifndef OS_INIT_DEFINED
5821static int
5822os_init(void)
5823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005824 return 1; /* Success */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005825}
5826#endif
5827
5828
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00005829/* C API table - always add new things to the end for binary
5830 compatibility. */
5831static
5832PySocketModule_APIObject PySocketModuleAPI =
5833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005834 &sock_type,
Antoine Pitrouc4df7842010-12-03 19:59:41 +00005835 NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005836 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00005837};
5838
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005839
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005840/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005841
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005842 This module is actually called "_socket", and there's a wrapper
Guido van Rossum7d0a8262007-05-21 23:13:11 +00005843 "socket.py" which implements some additional functionality.
5844 The import of "_socket" may fail with an ImportError exception if
5845 os-specific initialization fails. On Windows, this does WINSOCK
Ezio Melotti13925002011-03-16 11:05:33 +02005846 initialization. When WINSOCK is initialized successfully, a call to
Guido van Rossum7d0a8262007-05-21 23:13:11 +00005847 WSACleanup() is scheduled to be made at exit time.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005848*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005849
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005850PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005851"Implementation module for socket operations.\n\
5852\n\
5853See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00005854
Martin v. Löwis1a214512008-06-11 05:26:20 +00005855static struct PyModuleDef socketmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005856 PyModuleDef_HEAD_INIT,
5857 PySocket_MODULE_NAME,
5858 socket_doc,
5859 -1,
5860 socket_methods,
5861 NULL,
5862 NULL,
5863 NULL,
5864 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00005865};
5866
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005867PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00005868PyInit__socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005869{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005870 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00005871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005872 if (!os_init())
5873 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005874
Victor Stinnerdaf45552013-08-28 00:53:59 +02005875#ifdef MS_WINDOWS
5876 if (support_wsa_no_inherit == -1) {
Steve Dower65e4cb12014-11-22 12:54:57 -08005877#if defined(_MSC_VER) && _MSC_VER >= 1800
5878 support_wsa_no_inherit = IsWindows7SP1OrGreater();
5879#else
Victor Stinnerdaf45552013-08-28 00:53:59 +02005880 DWORD version = GetVersion();
5881 DWORD major = (DWORD)LOBYTE(LOWORD(version));
5882 DWORD minor = (DWORD)HIBYTE(LOWORD(version));
5883 /* need Windows 7 SP1, 2008 R2 SP1 or later */
Steve Dower65e4cb12014-11-22 12:54:57 -08005884 support_wsa_no_inherit = major > 6 || (major == 6 && minor >= 1);
5885#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02005886 }
5887#endif
5888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005889 Py_TYPE(&sock_type) = &PyType_Type;
5890 m = PyModule_Create(&socketmodule);
5891 if (m == NULL)
5892 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005893
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005894 Py_INCREF(PyExc_OSError);
5895 PySocketModuleAPI.error = PyExc_OSError;
5896 Py_INCREF(PyExc_OSError);
5897 PyModule_AddObject(m, "error", PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005898 socket_herror = PyErr_NewException("socket.herror",
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005899 PyExc_OSError, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005900 if (socket_herror == NULL)
5901 return NULL;
5902 Py_INCREF(socket_herror);
5903 PyModule_AddObject(m, "herror", socket_herror);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005904 socket_gaierror = PyErr_NewException("socket.gaierror", PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005905 NULL);
5906 if (socket_gaierror == NULL)
5907 return NULL;
5908 Py_INCREF(socket_gaierror);
5909 PyModule_AddObject(m, "gaierror", socket_gaierror);
5910 socket_timeout = PyErr_NewException("socket.timeout",
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005911 PyExc_OSError, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005912 if (socket_timeout == NULL)
5913 return NULL;
Antoine Pitrouc4df7842010-12-03 19:59:41 +00005914 PySocketModuleAPI.timeout_error = socket_timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005915 Py_INCREF(socket_timeout);
5916 PyModule_AddObject(m, "timeout", socket_timeout);
5917 Py_INCREF((PyObject *)&sock_type);
5918 if (PyModule_AddObject(m, "SocketType",
5919 (PyObject *)&sock_type) != 0)
5920 return NULL;
5921 Py_INCREF((PyObject *)&sock_type);
5922 if (PyModule_AddObject(m, "socket",
5923 (PyObject *)&sock_type) != 0)
5924 return NULL;
Guido van Rossum09be4091999-08-09 14:40:40 +00005925
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005926#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005927 has_ipv6 = Py_True;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005928#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005929 has_ipv6 = Py_False;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005930#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005931 Py_INCREF(has_ipv6);
5932 PyModule_AddObject(m, "has_ipv6", has_ipv6);
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005934 /* Export C API */
5935 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
5936 PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL)
5937 ) != 0)
5938 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00005939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005940 /* Address families (we only support AF_INET and AF_UNIX) */
Guido van Rossum09be4091999-08-09 14:40:40 +00005941#ifdef AF_UNSPEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005942 PyModule_AddIntMacro(m, AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00005943#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005944 PyModule_AddIntMacro(m, AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005945#ifdef AF_INET6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005946 PyModule_AddIntMacro(m, AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005947#endif /* AF_INET6 */
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00005948#if defined(AF_UNIX)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005949 PyModule_AddIntMacro(m, AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005950#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00005951#ifdef AF_AX25
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005952 /* Amateur Radio AX.25 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005953 PyModule_AddIntMacro(m, AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00005954#endif
5955#ifdef AF_IPX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005956 PyModule_AddIntMacro(m, AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00005957#endif
5958#ifdef AF_APPLETALK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005959 /* Appletalk DDP */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005960 PyModule_AddIntMacro(m, AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00005961#endif
5962#ifdef AF_NETROM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005963 /* Amateur radio NetROM */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005964 PyModule_AddIntMacro(m, AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00005965#endif
5966#ifdef AF_BRIDGE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005967 /* Multiprotocol bridge */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005968 PyModule_AddIntMacro(m, AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00005969#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005970#ifdef AF_ATMPVC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005971 /* ATM PVCs */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005972 PyModule_AddIntMacro(m, AF_ATMPVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005973#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00005974#ifdef AF_AAL5
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005975 /* Reserved for Werner's ATM */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005976 PyModule_AddIntMacro(m, AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00005977#endif
5978#ifdef AF_X25
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005979 /* Reserved for X.25 project */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005980 PyModule_AddIntMacro(m, AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00005981#endif
5982#ifdef AF_INET6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005983 PyModule_AddIntMacro(m, AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00005984#endif
5985#ifdef AF_ROSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005986 /* Amateur Radio X.25 PLP */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005987 PyModule_AddIntMacro(m, AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00005988#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005989#ifdef AF_DECnet
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005990 /* Reserved for DECnet project */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005991 PyModule_AddIntMacro(m, AF_DECnet);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005992#endif
5993#ifdef AF_NETBEUI
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005994 /* Reserved for 802.2LLC project */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005995 PyModule_AddIntMacro(m, AF_NETBEUI);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005996#endif
5997#ifdef AF_SECURITY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005998 /* Security callback pseudo AF */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005999 PyModule_AddIntMacro(m, AF_SECURITY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006000#endif
6001#ifdef AF_KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006002 /* PF_KEY key management API */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006003 PyModule_AddIntMacro(m, AF_KEY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006004#endif
6005#ifdef AF_NETLINK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006006 /* */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006007 PyModule_AddIntMacro(m, AF_NETLINK);
6008 PyModule_AddIntMacro(m, NETLINK_ROUTE);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006009#ifdef NETLINK_SKIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006010 PyModule_AddIntMacro(m, NETLINK_SKIP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006011#endif
6012#ifdef NETLINK_W1
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006013 PyModule_AddIntMacro(m, NETLINK_W1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006014#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006015 PyModule_AddIntMacro(m, NETLINK_USERSOCK);
6016 PyModule_AddIntMacro(m, NETLINK_FIREWALL);
Guido van Rossum668a94a2006-02-21 01:07:27 +00006017#ifdef NETLINK_TCPDIAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006018 PyModule_AddIntMacro(m, NETLINK_TCPDIAG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00006019#endif
6020#ifdef NETLINK_NFLOG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006021 PyModule_AddIntMacro(m, NETLINK_NFLOG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00006022#endif
Neal Norwitz65851662006-01-16 04:31:40 +00006023#ifdef NETLINK_XFRM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006024 PyModule_AddIntMacro(m, NETLINK_XFRM);
Neal Norwitz65851662006-01-16 04:31:40 +00006025#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006026#ifdef NETLINK_ARPD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006027 PyModule_AddIntMacro(m, NETLINK_ARPD);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006028#endif
6029#ifdef NETLINK_ROUTE6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006030 PyModule_AddIntMacro(m, NETLINK_ROUTE6);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006031#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006032 PyModule_AddIntMacro(m, NETLINK_IP6_FW);
Thomas Wouterscf297e42007-02-23 15:07:44 +00006033#ifdef NETLINK_DNRTMSG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006034 PyModule_AddIntMacro(m, NETLINK_DNRTMSG);
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00006035#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006036#ifdef NETLINK_TAPBASE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006037 PyModule_AddIntMacro(m, NETLINK_TAPBASE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006038#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006039#endif /* AF_NETLINK */
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006040#ifdef AF_ROUTE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006041 /* Alias to emulate 4.4BSD */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006042 PyModule_AddIntMacro(m, AF_ROUTE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006043#endif
Giampaolo Rodola'80e1c432013-05-21 21:02:04 +02006044#ifdef AF_LINK
6045 PyModule_AddIntMacro(m, AF_LINK);
6046#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006047#ifdef AF_ASH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006048 /* Ash */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006049 PyModule_AddIntMacro(m, AF_ASH);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006050#endif
6051#ifdef AF_ECONET
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006052 /* Acorn Econet */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006053 PyModule_AddIntMacro(m, AF_ECONET);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006054#endif
6055#ifdef AF_ATMSVC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006056 /* ATM SVCs */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006057 PyModule_AddIntMacro(m, AF_ATMSVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006058#endif
6059#ifdef AF_SNA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006060 /* Linux SNA Project (nutters!) */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006061 PyModule_AddIntMacro(m, AF_SNA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006062#endif
6063#ifdef AF_IRDA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006064 /* IRDA sockets */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006065 PyModule_AddIntMacro(m, AF_IRDA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006066#endif
6067#ifdef AF_PPPOX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006068 /* PPPoX sockets */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006069 PyModule_AddIntMacro(m, AF_PPPOX);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006070#endif
6071#ifdef AF_WANPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006072 /* Wanpipe API Sockets */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006073 PyModule_AddIntMacro(m, AF_WANPIPE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006074#endif
6075#ifdef AF_LLC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006076 /* Linux LLC */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006077 PyModule_AddIntMacro(m, AF_LLC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006078#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00006079
Hye-Shik Chang81268602004-02-02 06:05:24 +00006080#ifdef USE_BLUETOOTH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006081 PyModule_AddIntMacro(m, AF_BLUETOOTH);
6082 PyModule_AddIntMacro(m, BTPROTO_L2CAP);
6083 PyModule_AddIntMacro(m, BTPROTO_HCI);
6084 PyModule_AddIntMacro(m, SOL_HCI);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00006085#if !defined(__NetBSD__) && !defined(__DragonFly__)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006086 PyModule_AddIntMacro(m, HCI_FILTER);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00006087#endif
Hye-Shik Chang81268602004-02-02 06:05:24 +00006088#if !defined(__FreeBSD__)
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00006089#if !defined(__NetBSD__) && !defined(__DragonFly__)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006090 PyModule_AddIntMacro(m, HCI_TIME_STAMP);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00006091#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006092 PyModule_AddIntMacro(m, HCI_DATA_DIR);
6093 PyModule_AddIntMacro(m, BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00006094#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006095 PyModule_AddIntMacro(m, BTPROTO_RFCOMM);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006096 PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
6097 PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
Martin v. Löwis12af0482004-01-31 12:34:17 +00006098#endif
6099
Charles-François Natali47413c12011-10-06 19:47:44 +02006100#ifdef AF_CAN
6101 /* Controller Area Network */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006102 PyModule_AddIntMacro(m, AF_CAN);
Charles-François Natali47413c12011-10-06 19:47:44 +02006103#endif
6104#ifdef PF_CAN
6105 /* Controller Area Network */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006106 PyModule_AddIntMacro(m, PF_CAN);
Charles-François Natali47413c12011-10-06 19:47:44 +02006107#endif
6108
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006109/* Reliable Datagram Sockets */
6110#ifdef AF_RDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006111 PyModule_AddIntMacro(m, AF_RDS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006112#endif
6113#ifdef PF_RDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006114 PyModule_AddIntMacro(m, PF_RDS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006115#endif
6116
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006117/* Kernel event messages */
6118#ifdef PF_SYSTEM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006119 PyModule_AddIntMacro(m, PF_SYSTEM);
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006120#endif
6121#ifdef AF_SYSTEM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006122 PyModule_AddIntMacro(m, AF_SYSTEM);
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006123#endif
6124
Antoine Pitroub156a462010-10-27 20:13:57 +00006125#ifdef AF_PACKET
6126 PyModule_AddIntMacro(m, AF_PACKET);
6127#endif
6128#ifdef PF_PACKET
6129 PyModule_AddIntMacro(m, PF_PACKET);
6130#endif
6131#ifdef PACKET_HOST
6132 PyModule_AddIntMacro(m, PACKET_HOST);
6133#endif
6134#ifdef PACKET_BROADCAST
6135 PyModule_AddIntMacro(m, PACKET_BROADCAST);
6136#endif
6137#ifdef PACKET_MULTICAST
6138 PyModule_AddIntMacro(m, PACKET_MULTICAST);
6139#endif
6140#ifdef PACKET_OTHERHOST
6141 PyModule_AddIntMacro(m, PACKET_OTHERHOST);
6142#endif
6143#ifdef PACKET_OUTGOING
6144 PyModule_AddIntMacro(m, PACKET_OUTGOING);
6145#endif
6146#ifdef PACKET_LOOPBACK
6147 PyModule_AddIntMacro(m, PACKET_LOOPBACK);
6148#endif
6149#ifdef PACKET_FASTROUTE
6150 PyModule_AddIntMacro(m, PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00006151#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006152
Christian Heimes043d6f62008-01-07 17:19:16 +00006153#ifdef HAVE_LINUX_TIPC_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006154 PyModule_AddIntMacro(m, AF_TIPC);
Christian Heimes043d6f62008-01-07 17:19:16 +00006155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006156 /* for addresses */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006157 PyModule_AddIntMacro(m, TIPC_ADDR_NAMESEQ);
6158 PyModule_AddIntMacro(m, TIPC_ADDR_NAME);
6159 PyModule_AddIntMacro(m, TIPC_ADDR_ID);
Christian Heimes043d6f62008-01-07 17:19:16 +00006160
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006161 PyModule_AddIntMacro(m, TIPC_ZONE_SCOPE);
6162 PyModule_AddIntMacro(m, TIPC_CLUSTER_SCOPE);
6163 PyModule_AddIntMacro(m, TIPC_NODE_SCOPE);
Christian Heimes043d6f62008-01-07 17:19:16 +00006164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006165 /* for setsockopt() */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006166 PyModule_AddIntMacro(m, SOL_TIPC);
6167 PyModule_AddIntMacro(m, TIPC_IMPORTANCE);
6168 PyModule_AddIntMacro(m, TIPC_SRC_DROPPABLE);
6169 PyModule_AddIntMacro(m, TIPC_DEST_DROPPABLE);
6170 PyModule_AddIntMacro(m, TIPC_CONN_TIMEOUT);
Christian Heimes043d6f62008-01-07 17:19:16 +00006171
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006172 PyModule_AddIntMacro(m, TIPC_LOW_IMPORTANCE);
6173 PyModule_AddIntMacro(m, TIPC_MEDIUM_IMPORTANCE);
6174 PyModule_AddIntMacro(m, TIPC_HIGH_IMPORTANCE);
6175 PyModule_AddIntMacro(m, TIPC_CRITICAL_IMPORTANCE);
Christian Heimes043d6f62008-01-07 17:19:16 +00006176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006177 /* for subscriptions */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006178 PyModule_AddIntMacro(m, TIPC_SUB_PORTS);
6179 PyModule_AddIntMacro(m, TIPC_SUB_SERVICE);
Christian Heimes25bb7832008-01-11 16:17:00 +00006180#ifdef TIPC_SUB_CANCEL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006181 /* doesn't seem to be available everywhere */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006182 PyModule_AddIntMacro(m, TIPC_SUB_CANCEL);
Christian Heimes25bb7832008-01-11 16:17:00 +00006183#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006184 PyModule_AddIntMacro(m, TIPC_WAIT_FOREVER);
6185 PyModule_AddIntMacro(m, TIPC_PUBLISHED);
6186 PyModule_AddIntMacro(m, TIPC_WITHDRAWN);
6187 PyModule_AddIntMacro(m, TIPC_SUBSCR_TIMEOUT);
6188 PyModule_AddIntMacro(m, TIPC_CFG_SRV);
6189 PyModule_AddIntMacro(m, TIPC_TOP_SRV);
Christian Heimes043d6f62008-01-07 17:19:16 +00006190#endif
6191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006192 /* Socket types */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006193 PyModule_AddIntMacro(m, SOCK_STREAM);
6194 PyModule_AddIntMacro(m, SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00006195/* We have incomplete socket support. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006196 PyModule_AddIntMacro(m, SOCK_RAW);
6197 PyModule_AddIntMacro(m, SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00006198#if defined(SOCK_RDM)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006199 PyModule_AddIntMacro(m, SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00006200#endif
Antoine Pitroub1c54962010-10-14 15:05:38 +00006201#ifdef SOCK_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006202 PyModule_AddIntMacro(m, SOCK_CLOEXEC);
Antoine Pitroub1c54962010-10-14 15:05:38 +00006203#endif
6204#ifdef SOCK_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006205 PyModule_AddIntMacro(m, SOCK_NONBLOCK);
Antoine Pitroub1c54962010-10-14 15:05:38 +00006206#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006208#ifdef SO_DEBUG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006209 PyModule_AddIntMacro(m, SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006210#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006211#ifdef SO_ACCEPTCONN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006212 PyModule_AddIntMacro(m, SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006213#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006214#ifdef SO_REUSEADDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006215 PyModule_AddIntMacro(m, SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006216#endif
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00006217#ifdef SO_EXCLUSIVEADDRUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006218 PyModule_AddIntMacro(m, SO_EXCLUSIVEADDRUSE);
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00006219#endif
6220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006221#ifdef SO_KEEPALIVE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006222 PyModule_AddIntMacro(m, SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006223#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006224#ifdef SO_DONTROUTE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006225 PyModule_AddIntMacro(m, SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006226#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006227#ifdef SO_BROADCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006228 PyModule_AddIntMacro(m, SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006229#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006230#ifdef SO_USELOOPBACK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006231 PyModule_AddIntMacro(m, SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006232#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006233#ifdef SO_LINGER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006234 PyModule_AddIntMacro(m, SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006235#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006236#ifdef SO_OOBINLINE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006237 PyModule_AddIntMacro(m, SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006238#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006239#ifdef SO_REUSEPORT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006240 PyModule_AddIntMacro(m, SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006241#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006242#ifdef SO_SNDBUF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006243 PyModule_AddIntMacro(m, SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006244#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006245#ifdef SO_RCVBUF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006246 PyModule_AddIntMacro(m, SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006247#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006248#ifdef SO_SNDLOWAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006249 PyModule_AddIntMacro(m, SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006250#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006251#ifdef SO_RCVLOWAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006252 PyModule_AddIntMacro(m, SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006253#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006254#ifdef SO_SNDTIMEO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006255 PyModule_AddIntMacro(m, SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006256#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006257#ifdef SO_RCVTIMEO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006258 PyModule_AddIntMacro(m, SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006259#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006260#ifdef SO_ERROR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006261 PyModule_AddIntMacro(m, SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006262#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006263#ifdef SO_TYPE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006264 PyModule_AddIntMacro(m, SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006265#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006266#ifdef SO_SETFIB
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006267 PyModule_AddIntMacro(m, SO_SETFIB);
Larry Hastingsf0f37952010-04-02 11:47:10 +00006268#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006269#ifdef SO_PASSCRED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006270 PyModule_AddIntMacro(m, SO_PASSCRED);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006271#endif
6272#ifdef SO_PEERCRED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006273 PyModule_AddIntMacro(m, SO_PEERCRED);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006274#endif
6275#ifdef LOCAL_PEERCRED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006276 PyModule_AddIntMacro(m, LOCAL_PEERCRED);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006277#endif
Victor Stinnercb98bed2011-10-25 13:45:48 +02006278#ifdef SO_BINDTODEVICE
6279 PyModule_AddIntMacro(m, SO_BINDTODEVICE);
6280#endif
Benjamin Peterson1314ef72013-11-27 09:18:54 -06006281#ifdef SO_PRIORITY
6282 PyModule_AddIntMacro(m, SO_PRIORITY);
6283#endif
Charles-François Nataliea07eb92014-09-18 23:18:46 +01006284#ifdef SO_MARK
6285 PyModule_AddIntMacro(m, SO_MARK);
6286#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006288 /* Maximum number of connections for "listen" */
6289#ifdef SOMAXCONN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006290 PyModule_AddIntMacro(m, SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006291#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006292 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006293#endif
6294
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006295 /* Ancilliary message types */
6296#ifdef SCM_RIGHTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006297 PyModule_AddIntMacro(m, SCM_RIGHTS);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006298#endif
6299#ifdef SCM_CREDENTIALS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006300 PyModule_AddIntMacro(m, SCM_CREDENTIALS);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006301#endif
6302#ifdef SCM_CREDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006303 PyModule_AddIntMacro(m, SCM_CREDS);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006304#endif
6305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006306 /* Flags for send, recv */
6307#ifdef MSG_OOB
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006308 PyModule_AddIntMacro(m, MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006309#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006310#ifdef MSG_PEEK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006311 PyModule_AddIntMacro(m, MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006312#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006313#ifdef MSG_DONTROUTE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006314 PyModule_AddIntMacro(m, MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006315#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006316#ifdef MSG_DONTWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006317 PyModule_AddIntMacro(m, MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00006318#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006319#ifdef MSG_EOR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006320 PyModule_AddIntMacro(m, MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006321#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006322#ifdef MSG_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006323 PyModule_AddIntMacro(m, MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006324#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006325#ifdef MSG_CTRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006326 PyModule_AddIntMacro(m, MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006327#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006328#ifdef MSG_WAITALL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006329 PyModule_AddIntMacro(m, MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006330#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006331#ifdef MSG_BTAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006332 PyModule_AddIntMacro(m, MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006333#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006334#ifdef MSG_ETAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006335 PyModule_AddIntMacro(m, MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006336#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006337#ifdef MSG_NOSIGNAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006338 PyModule_AddIntMacro(m, MSG_NOSIGNAL);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006339#endif
6340#ifdef MSG_NOTIFICATION
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006341 PyModule_AddIntMacro(m, MSG_NOTIFICATION);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006342#endif
6343#ifdef MSG_CMSG_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006344 PyModule_AddIntMacro(m, MSG_CMSG_CLOEXEC);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006345#endif
6346#ifdef MSG_ERRQUEUE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006347 PyModule_AddIntMacro(m, MSG_ERRQUEUE);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006348#endif
6349#ifdef MSG_CONFIRM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006350 PyModule_AddIntMacro(m, MSG_CONFIRM);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006351#endif
6352#ifdef MSG_MORE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006353 PyModule_AddIntMacro(m, MSG_MORE);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006354#endif
6355#ifdef MSG_EOF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006356 PyModule_AddIntMacro(m, MSG_EOF);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006357#endif
6358#ifdef MSG_BCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006359 PyModule_AddIntMacro(m, MSG_BCAST);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006360#endif
6361#ifdef MSG_MCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006362 PyModule_AddIntMacro(m, MSG_MCAST);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006363#endif
Benjamin Peterson57b66722012-12-12 22:24:47 -05006364#ifdef MSG_FASTOPEN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006365 PyModule_AddIntMacro(m, MSG_FASTOPEN);
Benjamin Peterson57b66722012-12-12 22:24:47 -05006366#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006368 /* Protocol level and numbers, usable for [gs]etsockopt */
6369#ifdef SOL_SOCKET
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006370 PyModule_AddIntMacro(m, SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006371#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006372#ifdef SOL_IP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006373 PyModule_AddIntMacro(m, SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006374#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006375 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00006376#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006377#ifdef SOL_IPX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006378 PyModule_AddIntMacro(m, SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00006379#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006380#ifdef SOL_AX25
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006381 PyModule_AddIntMacro(m, SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00006382#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006383#ifdef SOL_ATALK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006384 PyModule_AddIntMacro(m, SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00006385#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006386#ifdef SOL_NETROM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006387 PyModule_AddIntMacro(m, SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00006388#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006389#ifdef SOL_ROSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006390 PyModule_AddIntMacro(m, SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00006391#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006392#ifdef SOL_TCP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006393 PyModule_AddIntMacro(m, SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006394#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006395 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00006396#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006397#ifdef SOL_UDP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006398 PyModule_AddIntMacro(m, SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006399#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006400 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00006401#endif
Charles-François Natali47413c12011-10-06 19:47:44 +02006402#ifdef SOL_CAN_BASE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006403 PyModule_AddIntMacro(m, SOL_CAN_BASE);
Charles-François Natali47413c12011-10-06 19:47:44 +02006404#endif
6405#ifdef SOL_CAN_RAW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006406 PyModule_AddIntMacro(m, SOL_CAN_RAW);
6407 PyModule_AddIntMacro(m, CAN_RAW);
Charles-François Natali47413c12011-10-06 19:47:44 +02006408#endif
6409#ifdef HAVE_LINUX_CAN_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006410 PyModule_AddIntMacro(m, CAN_EFF_FLAG);
6411 PyModule_AddIntMacro(m, CAN_RTR_FLAG);
6412 PyModule_AddIntMacro(m, CAN_ERR_FLAG);
Charles-François Natali47413c12011-10-06 19:47:44 +02006413
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006414 PyModule_AddIntMacro(m, CAN_SFF_MASK);
6415 PyModule_AddIntMacro(m, CAN_EFF_MASK);
6416 PyModule_AddIntMacro(m, CAN_ERR_MASK);
Charles-François Natali47413c12011-10-06 19:47:44 +02006417#endif
6418#ifdef HAVE_LINUX_CAN_RAW_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006419 PyModule_AddIntMacro(m, CAN_RAW_FILTER);
6420 PyModule_AddIntMacro(m, CAN_RAW_ERR_FILTER);
6421 PyModule_AddIntMacro(m, CAN_RAW_LOOPBACK);
6422 PyModule_AddIntMacro(m, CAN_RAW_RECV_OWN_MSGS);
Charles-François Natali47413c12011-10-06 19:47:44 +02006423#endif
Charles-François Natali773e42d2013-02-05 19:42:01 +01006424#ifdef HAVE_LINUX_CAN_BCM_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006425 PyModule_AddIntMacro(m, CAN_BCM);
Charles-François Natali773e42d2013-02-05 19:42:01 +01006426 PyModule_AddIntConstant(m, "CAN_BCM_TX_SETUP", TX_SETUP);
6427 PyModule_AddIntConstant(m, "CAN_BCM_TX_DELETE", TX_DELETE);
6428 PyModule_AddIntConstant(m, "CAN_BCM_TX_READ", TX_READ);
6429 PyModule_AddIntConstant(m, "CAN_BCM_TX_SEND", TX_SEND);
6430 PyModule_AddIntConstant(m, "CAN_BCM_RX_SETUP", RX_SETUP);
6431 PyModule_AddIntConstant(m, "CAN_BCM_RX_DELETE", RX_DELETE);
6432 PyModule_AddIntConstant(m, "CAN_BCM_RX_READ", RX_READ);
6433 PyModule_AddIntConstant(m, "CAN_BCM_TX_STATUS", TX_STATUS);
6434 PyModule_AddIntConstant(m, "CAN_BCM_TX_EXPIRED", TX_EXPIRED);
6435 PyModule_AddIntConstant(m, "CAN_BCM_RX_STATUS", RX_STATUS);
6436 PyModule_AddIntConstant(m, "CAN_BCM_RX_TIMEOUT", RX_TIMEOUT);
6437 PyModule_AddIntConstant(m, "CAN_BCM_RX_CHANGED", RX_CHANGED);
6438#endif
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006439#ifdef SOL_RDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006440 PyModule_AddIntMacro(m, SOL_RDS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006441#endif
6442#ifdef RDS_CANCEL_SENT_TO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006443 PyModule_AddIntMacro(m, RDS_CANCEL_SENT_TO);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006444#endif
6445#ifdef RDS_GET_MR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006446 PyModule_AddIntMacro(m, RDS_GET_MR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006447#endif
6448#ifdef RDS_FREE_MR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006449 PyModule_AddIntMacro(m, RDS_FREE_MR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006450#endif
6451#ifdef RDS_RECVERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006452 PyModule_AddIntMacro(m, RDS_RECVERR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006453#endif
6454#ifdef RDS_CONG_MONITOR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006455 PyModule_AddIntMacro(m, RDS_CONG_MONITOR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006456#endif
6457#ifdef RDS_GET_MR_FOR_DEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006458 PyModule_AddIntMacro(m, RDS_GET_MR_FOR_DEST);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006459#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006460#ifdef IPPROTO_IP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006461 PyModule_AddIntMacro(m, IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00006462#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006463 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006464#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006465#ifdef IPPROTO_HOPOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006466 PyModule_AddIntMacro(m, IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006467#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006468#ifdef IPPROTO_ICMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006469 PyModule_AddIntMacro(m, IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00006470#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006471 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006472#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006473#ifdef IPPROTO_IGMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006474 PyModule_AddIntMacro(m, IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006475#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006476#ifdef IPPROTO_GGP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006477 PyModule_AddIntMacro(m, IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006478#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006479#ifdef IPPROTO_IPV4
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006480 PyModule_AddIntMacro(m, IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006481#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006482#ifdef IPPROTO_IPV6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006483 PyModule_AddIntMacro(m, IPPROTO_IPV6);
Martin v. Löwisa0f17342003-10-03 13:56:20 +00006484#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006485#ifdef IPPROTO_IPIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006486 PyModule_AddIntMacro(m, IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006487#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006488#ifdef IPPROTO_TCP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006489 PyModule_AddIntMacro(m, IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00006490#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006491 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006492#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006493#ifdef IPPROTO_EGP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006494 PyModule_AddIntMacro(m, IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006495#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006496#ifdef IPPROTO_PUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006497 PyModule_AddIntMacro(m, IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006498#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006499#ifdef IPPROTO_UDP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006500 PyModule_AddIntMacro(m, IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00006501#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006502 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006503#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006504#ifdef IPPROTO_IDP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006505 PyModule_AddIntMacro(m, IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006506#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006507#ifdef IPPROTO_HELLO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006508 PyModule_AddIntMacro(m, IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006509#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006510#ifdef IPPROTO_ND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006511 PyModule_AddIntMacro(m, IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006512#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006513#ifdef IPPROTO_TP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006514 PyModule_AddIntMacro(m, IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006515#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006516#ifdef IPPROTO_IPV6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006517 PyModule_AddIntMacro(m, IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006518#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006519#ifdef IPPROTO_ROUTING
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006520 PyModule_AddIntMacro(m, IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006521#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006522#ifdef IPPROTO_FRAGMENT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006523 PyModule_AddIntMacro(m, IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006524#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006525#ifdef IPPROTO_RSVP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006526 PyModule_AddIntMacro(m, IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006527#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006528#ifdef IPPROTO_GRE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006529 PyModule_AddIntMacro(m, IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006530#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006531#ifdef IPPROTO_ESP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006532 PyModule_AddIntMacro(m, IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006533#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006534#ifdef IPPROTO_AH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006535 PyModule_AddIntMacro(m, IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006536#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006537#ifdef IPPROTO_MOBILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006538 PyModule_AddIntMacro(m, IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006539#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006540#ifdef IPPROTO_ICMPV6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006541 PyModule_AddIntMacro(m, IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006542#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006543#ifdef IPPROTO_NONE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006544 PyModule_AddIntMacro(m, IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006545#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006546#ifdef IPPROTO_DSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006547 PyModule_AddIntMacro(m, IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006548#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006549#ifdef IPPROTO_XTP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006550 PyModule_AddIntMacro(m, IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006551#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006552#ifdef IPPROTO_EON
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006553 PyModule_AddIntMacro(m, IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006554#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006555#ifdef IPPROTO_PIM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006556 PyModule_AddIntMacro(m, IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006557#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006558#ifdef IPPROTO_IPCOMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006559 PyModule_AddIntMacro(m, IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006560#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006561#ifdef IPPROTO_VRRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006562 PyModule_AddIntMacro(m, IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006563#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006564#ifdef IPPROTO_SCTP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006565 PyModule_AddIntMacro(m, IPPROTO_SCTP);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006566#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006567#ifdef IPPROTO_BIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006568 PyModule_AddIntMacro(m, IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006569#endif
6570/**/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006571#ifdef IPPROTO_RAW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006572 PyModule_AddIntMacro(m, IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00006573#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006574 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006575#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006576#ifdef IPPROTO_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006577 PyModule_AddIntMacro(m, IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006578#endif
6579
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006580#ifdef SYSPROTO_CONTROL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006581 PyModule_AddIntMacro(m, SYSPROTO_CONTROL);
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006582#endif
6583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006584 /* Some port configuration */
6585#ifdef IPPORT_RESERVED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006586 PyModule_AddIntMacro(m, IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006587#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006588 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006589#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006590#ifdef IPPORT_USERRESERVED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006591 PyModule_AddIntMacro(m, IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006592#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006593 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006594#endif
6595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006596 /* Some reserved IP v.4 addresses */
6597#ifdef INADDR_ANY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006598 PyModule_AddIntMacro(m, INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006599#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006600 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006601#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006602#ifdef INADDR_BROADCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006603 PyModule_AddIntMacro(m, INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006604#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006605 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006606#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006607#ifdef INADDR_LOOPBACK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006608 PyModule_AddIntMacro(m, INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006609#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006610 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006611#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006612#ifdef INADDR_UNSPEC_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006613 PyModule_AddIntMacro(m, INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006614#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006615 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006616#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006617#ifdef INADDR_ALLHOSTS_GROUP
6618 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
6619 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006620#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006621 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006622#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006623#ifdef INADDR_MAX_LOCAL_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006624 PyModule_AddIntMacro(m, INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006625#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006626 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006627#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006628#ifdef INADDR_NONE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006629 PyModule_AddIntMacro(m, INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006630#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006631 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006632#endif
6633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006634 /* IPv4 [gs]etsockopt options */
6635#ifdef IP_OPTIONS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006636 PyModule_AddIntMacro(m, IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006637#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006638#ifdef IP_HDRINCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006639 PyModule_AddIntMacro(m, IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006640#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006641#ifdef IP_TOS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006642 PyModule_AddIntMacro(m, IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006643#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006644#ifdef IP_TTL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006645 PyModule_AddIntMacro(m, IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006646#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006647#ifdef IP_RECVOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006648 PyModule_AddIntMacro(m, IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006649#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006650#ifdef IP_RECVRETOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006651 PyModule_AddIntMacro(m, IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006652#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006653#ifdef IP_RECVDSTADDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006654 PyModule_AddIntMacro(m, IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006655#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006656#ifdef IP_RETOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006657 PyModule_AddIntMacro(m, IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006658#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006659#ifdef IP_MULTICAST_IF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006660 PyModule_AddIntMacro(m, IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006661#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006662#ifdef IP_MULTICAST_TTL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006663 PyModule_AddIntMacro(m, IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006664#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006665#ifdef IP_MULTICAST_LOOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006666 PyModule_AddIntMacro(m, IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006667#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006668#ifdef IP_ADD_MEMBERSHIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006669 PyModule_AddIntMacro(m, IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006670#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006671#ifdef IP_DROP_MEMBERSHIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006672 PyModule_AddIntMacro(m, IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006673#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006674#ifdef IP_DEFAULT_MULTICAST_TTL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006675 PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00006676#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006677#ifdef IP_DEFAULT_MULTICAST_LOOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006678 PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006679#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006680#ifdef IP_MAX_MEMBERSHIPS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006681 PyModule_AddIntMacro(m, IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00006682#endif
Charles-François Natali564a42c2011-12-17 14:59:56 +01006683#ifdef IP_TRANSPARENT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006684 PyModule_AddIntMacro(m, IP_TRANSPARENT);
Charles-François Natali564a42c2011-12-17 14:59:56 +01006685#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006687 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
6688#ifdef IPV6_JOIN_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006689 PyModule_AddIntMacro(m, IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006690#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006691#ifdef IPV6_LEAVE_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006692 PyModule_AddIntMacro(m, IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006693#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006694#ifdef IPV6_MULTICAST_HOPS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006695 PyModule_AddIntMacro(m, IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006696#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006697#ifdef IPV6_MULTICAST_IF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006698 PyModule_AddIntMacro(m, IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006699#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006700#ifdef IPV6_MULTICAST_LOOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006701 PyModule_AddIntMacro(m, IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006702#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006703#ifdef IPV6_UNICAST_HOPS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006704 PyModule_AddIntMacro(m, IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006705#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006706 /* Additional IPV6 socket options, defined in RFC 3493 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00006707#ifdef IPV6_V6ONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006708 PyModule_AddIntMacro(m, IPV6_V6ONLY);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006709#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006710 /* Advanced IPV6 socket options, from RFC 3542 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00006711#ifdef IPV6_CHECKSUM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006712 PyModule_AddIntMacro(m, IPV6_CHECKSUM);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006713#endif
6714#ifdef IPV6_DONTFRAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006715 PyModule_AddIntMacro(m, IPV6_DONTFRAG);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006716#endif
6717#ifdef IPV6_DSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006718 PyModule_AddIntMacro(m, IPV6_DSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006719#endif
6720#ifdef IPV6_HOPLIMIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006721 PyModule_AddIntMacro(m, IPV6_HOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006722#endif
6723#ifdef IPV6_HOPOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006724 PyModule_AddIntMacro(m, IPV6_HOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006725#endif
6726#ifdef IPV6_NEXTHOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006727 PyModule_AddIntMacro(m, IPV6_NEXTHOP);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006728#endif
6729#ifdef IPV6_PATHMTU
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006730 PyModule_AddIntMacro(m, IPV6_PATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006731#endif
6732#ifdef IPV6_PKTINFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006733 PyModule_AddIntMacro(m, IPV6_PKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006734#endif
6735#ifdef IPV6_RECVDSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006736 PyModule_AddIntMacro(m, IPV6_RECVDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006737#endif
6738#ifdef IPV6_RECVHOPLIMIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006739 PyModule_AddIntMacro(m, IPV6_RECVHOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006740#endif
6741#ifdef IPV6_RECVHOPOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006742 PyModule_AddIntMacro(m, IPV6_RECVHOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006743#endif
6744#ifdef IPV6_RECVPKTINFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006745 PyModule_AddIntMacro(m, IPV6_RECVPKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006746#endif
6747#ifdef IPV6_RECVRTHDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006748 PyModule_AddIntMacro(m, IPV6_RECVRTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006749#endif
6750#ifdef IPV6_RECVTCLASS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006751 PyModule_AddIntMacro(m, IPV6_RECVTCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006752#endif
6753#ifdef IPV6_RTHDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006754 PyModule_AddIntMacro(m, IPV6_RTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006755#endif
6756#ifdef IPV6_RTHDRDSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006757 PyModule_AddIntMacro(m, IPV6_RTHDRDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006758#endif
6759#ifdef IPV6_RTHDR_TYPE_0
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006760 PyModule_AddIntMacro(m, IPV6_RTHDR_TYPE_0);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006761#endif
6762#ifdef IPV6_RECVPATHMTU
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006763 PyModule_AddIntMacro(m, IPV6_RECVPATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006764#endif
6765#ifdef IPV6_TCLASS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006766 PyModule_AddIntMacro(m, IPV6_TCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006767#endif
6768#ifdef IPV6_USE_MIN_MTU
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006769 PyModule_AddIntMacro(m, IPV6_USE_MIN_MTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006770#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006772 /* TCP options */
6773#ifdef TCP_NODELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006774 PyModule_AddIntMacro(m, TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00006775#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006776#ifdef TCP_MAXSEG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006777 PyModule_AddIntMacro(m, TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00006778#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006779#ifdef TCP_CORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006780 PyModule_AddIntMacro(m, TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006781#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006782#ifdef TCP_KEEPIDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006783 PyModule_AddIntMacro(m, TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006784#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006785#ifdef TCP_KEEPINTVL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006786 PyModule_AddIntMacro(m, TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006787#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006788#ifdef TCP_KEEPCNT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006789 PyModule_AddIntMacro(m, TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006790#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006791#ifdef TCP_SYNCNT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006792 PyModule_AddIntMacro(m, TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006793#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006794#ifdef TCP_LINGER2
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006795 PyModule_AddIntMacro(m, TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006796#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006797#ifdef TCP_DEFER_ACCEPT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006798 PyModule_AddIntMacro(m, TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006799#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006800#ifdef TCP_WINDOW_CLAMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006801 PyModule_AddIntMacro(m, TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006802#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006803#ifdef TCP_INFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006804 PyModule_AddIntMacro(m, TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006805#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006806#ifdef TCP_QUICKACK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006807 PyModule_AddIntMacro(m, TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006808#endif
Benjamin Peterson57b66722012-12-12 22:24:47 -05006809#ifdef TCP_FASTOPEN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006810 PyModule_AddIntMacro(m, TCP_FASTOPEN);
Benjamin Peterson57b66722012-12-12 22:24:47 -05006811#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006813 /* IPX options */
6814#ifdef IPX_TYPE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006815 PyModule_AddIntMacro(m, IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00006816#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00006817
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006818/* Reliable Datagram Sockets */
6819#ifdef RDS_CMSG_RDMA_ARGS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006820 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_ARGS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006821#endif
6822#ifdef RDS_CMSG_RDMA_DEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006823 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_DEST);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006824#endif
6825#ifdef RDS_CMSG_RDMA_MAP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006826 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_MAP);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006827#endif
6828#ifdef RDS_CMSG_RDMA_STATUS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006829 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_STATUS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006830#endif
6831#ifdef RDS_CMSG_RDMA_UPDATE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006832 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_UPDATE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006833#endif
6834#ifdef RDS_RDMA_READWRITE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006835 PyModule_AddIntMacro(m, RDS_RDMA_READWRITE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006836#endif
6837#ifdef RDS_RDMA_FENCE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006838 PyModule_AddIntMacro(m, RDS_RDMA_FENCE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006839#endif
6840#ifdef RDS_RDMA_INVALIDATE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006841 PyModule_AddIntMacro(m, RDS_RDMA_INVALIDATE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006842#endif
6843#ifdef RDS_RDMA_USE_ONCE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006844 PyModule_AddIntMacro(m, RDS_RDMA_USE_ONCE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006845#endif
6846#ifdef RDS_RDMA_DONTWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006847 PyModule_AddIntMacro(m, RDS_RDMA_DONTWAIT);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006848#endif
6849#ifdef RDS_RDMA_NOTIFY_ME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006850 PyModule_AddIntMacro(m, RDS_RDMA_NOTIFY_ME);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006851#endif
6852#ifdef RDS_RDMA_SILENT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006853 PyModule_AddIntMacro(m, RDS_RDMA_SILENT);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006854#endif
6855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006856 /* get{addr,name}info parameters */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006857#ifdef EAI_ADDRFAMILY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006858 PyModule_AddIntMacro(m, EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006859#endif
6860#ifdef EAI_AGAIN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006861 PyModule_AddIntMacro(m, EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006862#endif
6863#ifdef EAI_BADFLAGS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006864 PyModule_AddIntMacro(m, EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006865#endif
6866#ifdef EAI_FAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006867 PyModule_AddIntMacro(m, EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006868#endif
6869#ifdef EAI_FAMILY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006870 PyModule_AddIntMacro(m, EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006871#endif
6872#ifdef EAI_MEMORY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006873 PyModule_AddIntMacro(m, EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006874#endif
6875#ifdef EAI_NODATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006876 PyModule_AddIntMacro(m, EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006877#endif
6878#ifdef EAI_NONAME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006879 PyModule_AddIntMacro(m, EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006880#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00006881#ifdef EAI_OVERFLOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006882 PyModule_AddIntMacro(m, EAI_OVERFLOW);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006883#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006884#ifdef EAI_SERVICE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006885 PyModule_AddIntMacro(m, EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006886#endif
6887#ifdef EAI_SOCKTYPE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006888 PyModule_AddIntMacro(m, EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006889#endif
6890#ifdef EAI_SYSTEM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006891 PyModule_AddIntMacro(m, EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006892#endif
6893#ifdef EAI_BADHINTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006894 PyModule_AddIntMacro(m, EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006895#endif
6896#ifdef EAI_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006897 PyModule_AddIntMacro(m, EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006898#endif
6899#ifdef EAI_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006900 PyModule_AddIntMacro(m, EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006901#endif
6902#ifdef AI_PASSIVE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006903 PyModule_AddIntMacro(m, AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006904#endif
6905#ifdef AI_CANONNAME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006906 PyModule_AddIntMacro(m, AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006907#endif
6908#ifdef AI_NUMERICHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006909 PyModule_AddIntMacro(m, AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006910#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00006911#ifdef AI_NUMERICSERV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006912 PyModule_AddIntMacro(m, AI_NUMERICSERV);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006913#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006914#ifdef AI_MASK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006915 PyModule_AddIntMacro(m, AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006916#endif
6917#ifdef AI_ALL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006918 PyModule_AddIntMacro(m, AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006919#endif
6920#ifdef AI_V4MAPPED_CFG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006921 PyModule_AddIntMacro(m, AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006922#endif
6923#ifdef AI_ADDRCONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006924 PyModule_AddIntMacro(m, AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006925#endif
6926#ifdef AI_V4MAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006927 PyModule_AddIntMacro(m, AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006928#endif
6929#ifdef AI_DEFAULT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006930 PyModule_AddIntMacro(m, AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006931#endif
6932#ifdef NI_MAXHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006933 PyModule_AddIntMacro(m, NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006934#endif
6935#ifdef NI_MAXSERV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006936 PyModule_AddIntMacro(m, NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006937#endif
6938#ifdef NI_NOFQDN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006939 PyModule_AddIntMacro(m, NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006940#endif
6941#ifdef NI_NUMERICHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006942 PyModule_AddIntMacro(m, NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006943#endif
6944#ifdef NI_NAMEREQD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006945 PyModule_AddIntMacro(m, NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006946#endif
6947#ifdef NI_NUMERICSERV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006948 PyModule_AddIntMacro(m, NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006949#endif
6950#ifdef NI_DGRAM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006951 PyModule_AddIntMacro(m, NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006952#endif
6953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006954 /* shutdown() parameters */
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006955#ifdef SHUT_RD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006956 PyModule_AddIntMacro(m, SHUT_RD);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006957#elif defined(SD_RECEIVE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006958 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006959#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006960 PyModule_AddIntConstant(m, "SHUT_RD", 0);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006961#endif
6962#ifdef SHUT_WR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006963 PyModule_AddIntMacro(m, SHUT_WR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006964#elif defined(SD_SEND)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006965 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006966#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006967 PyModule_AddIntConstant(m, "SHUT_WR", 1);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006968#endif
6969#ifdef SHUT_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006970 PyModule_AddIntMacro(m, SHUT_RDWR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006971#elif defined(SD_BOTH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006972 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006973#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006974 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006975#endif
6976
Christian Heimesfaf2f632008-01-06 16:59:19 +00006977#ifdef SIO_RCVALL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006978 {
6979 DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS};
6980 const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"};
6981 int i;
Victor Stinner63941882011-09-29 00:42:28 +02006982 for(i = 0; i<Py_ARRAY_LENGTH(codes); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006983 PyObject *tmp;
6984 tmp = PyLong_FromUnsignedLong(codes[i]);
6985 if (tmp == NULL)
6986 return NULL;
6987 PyModule_AddObject(m, names[i], tmp);
6988 }
6989 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006990 PyModule_AddIntMacro(m, RCVALL_OFF);
6991 PyModule_AddIntMacro(m, RCVALL_ON);
6992 PyModule_AddIntMacro(m, RCVALL_SOCKETLEVELONLY);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00006993#ifdef RCVALL_IPLEVEL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006994 PyModule_AddIntMacro(m, RCVALL_IPLEVEL);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00006995#endif
6996#ifdef RCVALL_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006997 PyModule_AddIntMacro(m, RCVALL_MAX);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00006998#endif
Christian Heimesfaf2f632008-01-06 16:59:19 +00006999#endif /* _MSTCPIP_ */
7000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007001 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00007002#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007003 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00007004#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007005 return m;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00007006}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007007
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007008
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007009#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +00007010#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007011
7012/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00007013/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007014
Guido van Rossum3eede5a2002-06-07 02:08:35 +00007015int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007016inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007018 if (af == AF_INET) {
Benjamin Petersonf91df042009-02-13 02:50:59 +00007019#if (SIZEOF_INT != 4)
7020#error "Not sure if in_addr_t exists and int is not 32-bits."
7021#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007022 unsigned int packed_addr;
7023 packed_addr = inet_addr(src);
7024 if (packed_addr == INADDR_NONE)
7025 return 0;
7026 memcpy(dst, &packed_addr, 4);
7027 return 1;
7028 }
7029 /* Should set errno to EAFNOSUPPORT */
7030 return -1;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007031}
7032
Martin v. Löwisc925b1532001-07-21 09:42:15 +00007033const char *
7034inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007035{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007036 if (af == AF_INET) {
7037 struct in_addr packed_addr;
7038 if (size < 16)
7039 /* Should set errno to ENOSPC. */
7040 return NULL;
7041 memcpy(&packed_addr, src, sizeof(packed_addr));
7042 return strncpy(dst, inet_ntoa(packed_addr), size);
7043 }
7044 /* Should set errno to EAFNOSUPPORT */
7045 return NULL;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007046}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007047
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007048#endif
Christian Heimesb6150692007-11-15 23:37:07 +00007049#endif