blob: 6429c21b85704f4cc1314eb1b8b1155e649a7cdc [file] [log] [blame]
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001/* Socket module */
2
3/*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005This module provides an interface to Berkeley socket IPC.
6
7Limitations:
8
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00009- Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a
Christian Heimes043d6f62008-01-07 17:19:16 +000010 portable manner, though AF_PACKET, AF_NETLINK and AF_TIPC are supported
11 under Linux.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000012- No read/write operations (use sendall/recv or makefile instead).
13- Additional restrictions apply on some non-Unix platforms (compensated
14 for by socket.py).
Guido van Rossum6574b3e1991-06-25 21:36:08 +000015
Guido van Rossum27e177d1995-03-16 15:43:47 +000016Module interface:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000017
Andrew Svetlov0832af62012-12-18 23:10:48 +020018- socket.error: exception raised for socket specific errors, alias for OSError
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000019- socket.gaierror: exception raised for getaddrinfo/getnameinfo errors,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 a subclass of socket.error
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000021- socket.herror: exception raised for gethostby* errors,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 a subclass of socket.error
Guido van Rossum30a685f1991-06-27 15:51:29 +000023- socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000024- socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])
Guido van Rossum27e177d1995-03-16 15:43:47 +000025- socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')
Guido van Rossum25405c71996-12-19 16:42:52 +000026- socket.getprotobyname(protocolname) --> protocol number
Barry Warsaw11b91a02004-06-28 00:50:43 +000027- socket.getservbyname(servicename[, protocolname]) --> port number
28- socket.getservbyport(portnumber[, protocolname]) --> service name
Guido van Rossum7d0a8262007-05-21 23:13:11 +000029- socket.socket([family[, type [, proto, fileno]]]) --> new socket object
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 (fileno specifies a pre-existing socket file descriptor)
Dave Cole331708b2004-08-09 04:51:41 +000031- socket.socketpair([family[, type [, proto]]]) --> (socket, socket)
Guido van Rossum006bf911996-06-12 04:04:55 +000032- socket.ntohs(16 bit value) --> new int object
33- socket.ntohl(32 bit value) --> new int object
34- socket.htons(16 bit value) --> new int object
35- socket.htonl(32 bit value) --> new int object
Berker Peksaga6ec5ee2014-06-30 11:28:40 +030036- socket.getaddrinfo(host, port [, family, type, proto, flags])
37 --> List of (family, type, proto, canonname, sockaddr)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000038- socket.getnameinfo(sockaddr, flags) --> (host, port)
Guido van Rossum27e177d1995-03-16 15:43:47 +000039- socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
Guido van Rossum47dfa4a2003-04-25 05:48:32 +000040- socket.has_ipv6: boolean value indicating if IPv6 is supported
Guido van Rossum5c9eb211999-08-20 18:21:51 +000041- socket.inet_aton(IP address) -> 32-bit packed IP representation
42- socket.inet_ntoa(packed IP) -> IP address string
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +000043- socket.getdefaulttimeout() -> None | float
44- socket.setdefaulttimeout(None | float)
Gregory P. Smith5ed2e772011-05-15 00:26:45 -070045- socket.if_nameindex() -> list of tuples (if_index, if_name)
46- socket.if_nametoindex(name) -> corresponding interface index
47- socket.if_indextoname(index) -> corresponding interface name
Guido van Rossum6574b3e1991-06-25 21:36:08 +000048- an Internet socket address is a pair (hostname, port)
49 where hostname can be anything recognized by gethostbyname()
50 (including the dd.dd.dd.dd notation) and port is in host byte order
51- where a hostname is returned, the dd.dd.dd.dd notation is used
Guido van Rossum27e177d1995-03-16 15:43:47 +000052- a UNIX domain socket address is a string specifying the pathname
Jeremy Hylton22308652001-02-02 03:23:09 +000053- an AF_PACKET socket address is a tuple containing a string
54 specifying the ethernet interface and an integer specifying
55 the Ethernet protocol number to be received. For example:
Jeremy Hyltondbfb6622001-02-02 19:55:17 +000056 ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +000057 specify packet-type and ha-type/addr.
Christian Heimes043d6f62008-01-07 17:19:16 +000058- an AF_TIPC socket address is expressed as
59 (addr_type, v1, v2, v3 [, scope]); where addr_type can be one of:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID;
Christian Heimes043d6f62008-01-07 17:19:16 +000061 and scope can be one of:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE.
Christian Heimes043d6f62008-01-07 17:19:16 +000063 The meaning of v1, v2 and v3 depends on the value of addr_type:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 if addr_type is TIPC_ADDR_NAME:
65 v1 is the server type
66 v2 is the port identifier
67 v3 is ignored
68 if addr_type is TIPC_ADDR_NAMESEQ:
69 v1 is the server type
70 v2 is the lower port number
71 v3 is the upper port number
72 if addr_type is TIPC_ADDR_ID:
73 v1 is the node
74 v2 is the ref
75 v3 is ignored
Christian Heimes043d6f62008-01-07 17:19:16 +000076
Guido van Rossum6574b3e1991-06-25 21:36:08 +000077
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000078Local naming conventions:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000079
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000080- names starting with sock_ are socket object methods
81- names starting with socket_ are module-level functions
82- names starting with PySocket are exported through socketmodule.h
Guido van Rossum30a685f1991-06-27 15:51:29 +000083
Guido van Rossum6574b3e1991-06-25 21:36:08 +000084*/
85
Thomas Wouters477c8d52006-05-27 19:21:47 +000086#ifdef __APPLE__
87 /*
88 * inet_aton is not available on OSX 10.3, yet we want to use a binary
89 * that was build on 10.4 or later to work on that release, weak linking
90 * comes to the rescue.
91 */
92# pragma weak inet_aton
93#endif
94
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000095#include "Python.h"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000096#include "structmember.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000097
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000098/* Socket object documentation */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000099PyDoc_STRVAR(sock_doc,
Victor Stinnerdaf45552013-08-28 00:53:59 +0200100"socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None) -> socket object\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000101\n\
102Open a socket of the given type. The family argument specifies the\n\
103address family; it defaults to AF_INET. The type argument specifies\n\
104whether this is a stream (SOCK_STREAM, this is the default)\n\
105or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
106specifying the default protocol. Keyword arguments are accepted.\n\
Victor Stinnerdaf45552013-08-28 00:53:59 +0200107The socket is created as non-inheritable.\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000108\n\
109A socket object represents one endpoint of a network connection.\n\
110\n\
111Methods of socket objects (keyword arguments not allowed):\n\
112\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000113_accept() -- accept connection, returning new socket fd and client address\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000114bind(addr) -- bind the socket to a local address\n\
115close() -- close the socket\n\
116connect(addr) -- connect the socket to a remote address\n\
117connect_ex(addr) -- connect, return an error code instead of an exception\n\
Victor Stinnerdaf45552013-08-28 00:53:59 +0200118dup() -- return a new socket fd duplicated from fileno()\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000119fileno() -- return underlying file descriptor\n\
120getpeername() -- return remote address [*]\n\
121getsockname() -- return local address\n\
122getsockopt(level, optname[, buflen]) -- get socket options\n\
123gettimeout() -- return timeout or None\n\
Charles-François Natali644b8f52014-05-22 19:45:39 +0100124listen([n]) -- start listening for incoming connections\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000125recv(buflen[, flags]) -- receive data\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000126recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000127recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000128recvfrom_into(buffer[, nbytes, [, flags])\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000129 -- receive data and sender\'s address (into a buffer)\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000130sendall(data[, flags]) -- send all data\n\
131send(data[, flags]) -- send data, may not send all of it\n\
132sendto(data[, flags], addr) -- send data to a given address\n\
133setblocking(0 | 1) -- set or clear the blocking I/O flag\n\
134setsockopt(level, optname, value) -- set socket options\n\
135settimeout(None | float) -- set or clear the timeout\n\
136shutdown(how) -- shut down traffic in one or both directions\n\
Gregory P. Smith5ed2e772011-05-15 00:26:45 -0700137if_nameindex() -- return all network interface indices and names\n\
Gregory P. Smithb6471db2011-05-22 22:47:55 -0700138if_nametoindex(name) -- return the corresponding interface index\n\
139if_indextoname(index) -- return the corresponding interface name\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000140\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000141 [*] not available on all platforms!");
Guido van Rossum3baaa131999-03-22 21:44:51 +0000142
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000143/* XXX This is a terrible mess of platform-dependent preprocessor hacks.
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000144 I hope some day someone can clean this up please... */
145
Guido van Rossum9376b741999-09-15 22:01:40 +0000146/* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
147 script doesn't get this right, so we hardcode some platform checks below.
148 On the other hand, not all Linux versions agree, so there the settings
149 computed by the configure script are needed! */
150
151#ifndef linux
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000152# undef HAVE_GETHOSTBYNAME_R_3_ARG
153# undef HAVE_GETHOSTBYNAME_R_5_ARG
154# undef HAVE_GETHOSTBYNAME_R_6_ARG
Guido van Rossum9376b741999-09-15 22:01:40 +0000155#endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000156
Victor Stinner710d27e2011-08-23 10:57:32 +0200157#if defined(__OpenBSD__)
158# include <sys/uio.h>
159#endif
160
Guido van Rossum7a122991999-04-13 04:07:32 +0000161#ifndef WITH_THREAD
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000162# undef HAVE_GETHOSTBYNAME_R
Guido van Rossum7a122991999-04-13 04:07:32 +0000163#endif
164
Guido van Rossume7de2061999-03-24 17:24:33 +0000165#ifdef HAVE_GETHOSTBYNAME_R
Jesus Cea736e7fc2011-03-14 17:36:54 +0100166# if defined(_AIX)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000167# define HAVE_GETHOSTBYNAME_R_3_ARG
168# elif defined(__sun) || defined(__sgi)
169# define HAVE_GETHOSTBYNAME_R_5_ARG
170# elif defined(linux)
Guido van Rossum9376b741999-09-15 22:01:40 +0000171/* Rely on the configure script */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000172# else
173# undef HAVE_GETHOSTBYNAME_R
174# endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000175#endif
176
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000177#if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \
178 !defined(MS_WINDOWS)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000179# define USE_GETHOSTBYNAME_LOCK
Guido van Rossum3baaa131999-03-22 21:44:51 +0000180#endif
181
Hye-Shik Chang9ceebd52005-09-24 14:58:47 +0000182/* To use __FreeBSD_version */
183#ifdef HAVE_SYS_PARAM_H
184#include <sys/param.h>
185#endif
Just van Rossum1040d2c2003-05-09 07:53:18 +0000186/* On systems on which getaddrinfo() is believed to not be thread-safe,
Just van Rossum09aecd72003-05-09 08:03:44 +0000187 (this includes the getaddrinfo emulation) protect access with a lock. */
Hye-Shik Chang9ceebd52005-09-24 14:58:47 +0000188#if defined(WITH_THREAD) && (defined(__APPLE__) || \
189 (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000190 defined(__OpenBSD__) || defined(__NetBSD__) || \
Christian Heimesaf01f662013-12-21 16:19:10 +0100191 !defined(HAVE_GETADDRINFO))
Just van Rossum1040d2c2003-05-09 07:53:18 +0000192#define USE_GETADDRINFO_LOCK
193#endif
194
195#ifdef USE_GETADDRINFO_LOCK
196#define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1);
197#define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock);
198#else
199#define ACQUIRE_GETADDRINFO_LOCK
200#define RELEASE_GETADDRINFO_LOCK
201#endif
202
203#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000204# include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000205#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000206
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000207#if defined(PYCC_VACPP)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000208# include <types.h>
209# include <io.h>
210# include <sys/ioctl.h>
211# include <utils.h>
212# include <ctype.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000213#endif
214
Martin v. Löwis9d6c6692012-02-03 17:44:58 +0100215#ifdef __APPLE__
216# include <sys/ioctl.h>
217#endif
218
219
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000220#if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000221/* make sure that the reentrant (gethostbyaddr_r etc)
222 functions are declared correctly if compiling with
223 MIPSPro 7.x in ANSI C mode (default) */
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000224
Thomas Wouters477c8d52006-05-27 19:21:47 +0000225/* XXX Using _SGIAPI is the wrong thing,
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000226 but I don't know what the right thing is. */
Trent Mick8ea5bdf2004-09-13 17:48:41 +0000227#undef _SGIAPI /* to avoid warning */
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000228#define _SGIAPI 1
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000229
Trent Mick8ea5bdf2004-09-13 17:48:41 +0000230#undef _XOPEN_SOURCE
231#include <sys/socket.h>
232#include <sys/types.h>
233#include <netinet/in.h>
234#ifdef _SS_ALIGNSIZE
235#define HAVE_GETADDRINFO 1
236#define HAVE_GETNAMEINFO 1
237#endif
238
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000239#define HAVE_INET_PTON
240#include <netdb.h>
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000241#endif
242
Thomas Wouters477c8d52006-05-27 19:21:47 +0000243/* Irix 6.5 fails to define this variable at all. This is needed
244 for both GCC and SGI's compiler. I'd say that the SGI headers
Georg Brandldbd83392006-02-20 09:42:33 +0000245 are just busted. Same thing for Solaris. */
246#if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN)
Anthony Baxterbab23cf2003-10-04 08:00:49 +0000247#define INET_ADDRSTRLEN 16
248#endif
249
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000250/* Generic includes */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000251#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000253#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000254
Gregory P. Smith3b1f2c32011-05-15 12:18:23 -0700255#ifdef HAVE_SYS_SOCKET_H
256#include <sys/socket.h>
257#endif
258
259#ifdef HAVE_NET_IF_H
260#include <net/if.h>
261#endif
262
Marc-André Lemburg976ade62002-02-16 18:47:07 +0000263/* Generic socket object definitions and includes */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000264#define PySocket_BUILDING_SOCKET
Marc-André Lemburgbb8b78b2002-02-16 18:44:52 +0000265#include "socketmodule.h"
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000266
267/* Addressing includes */
268
Guido van Rossum6f489d91996-06-28 20:15:15 +0000269#ifndef MS_WINDOWS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000270
271/* Non-MS WINDOWS includes */
272# include <netdb.h>
Nick Coghlan4ecf6cf2011-08-22 15:45:19 +1000273# include <unistd.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000274
Guido van Rossum9376b741999-09-15 22:01:40 +0000275/* Headers needed for inet_ntoa() and inet_addr() */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000276# include <arpa/inet.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000277
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000278# include <fcntl.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000279
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000280#else
Guido van Rossum48a680c2001-03-02 06:34:14 +0000281
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000282/* MS_WINDOWS includes */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000283# ifdef HAVE_FCNTL_H
284# include <fcntl.h>
285# endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000286
Steve Dower65e4cb12014-11-22 12:54:57 -0800287#if defined(_MSC_VER) && _MSC_VER >= 1800
288/* Provides the IsWindows7SP1OrGreater() function */
289#include <VersionHelpers.h>
290#endif
291
Jeremy Hylton22308652001-02-02 03:23:09 +0000292#endif
293
Skip Montanaro7befb992004-02-10 16:50:21 +0000294#include <stddef.h>
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000295
Neal Norwitz39d22e52002-11-02 19:55:21 +0000296#ifndef O_NONBLOCK
297# define O_NONBLOCK O_NDELAY
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000298#endif
299
Trent Micka708d6e2004-09-07 17:48:26 +0000300/* include Python's addrinfo.h unless it causes trouble */
301#if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE)
302 /* Do not include addinfo.h on some newer IRIX versions.
303 * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21,
304 * for example, but not by 6.5.10.
305 */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000306#elif defined(_MSC_VER) && _MSC_VER>1201
Trent Micka708d6e2004-09-07 17:48:26 +0000307 /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and
308 * EAI_* constants are defined in (the already included) ws2tcpip.h.
309 */
310#else
311# include "addrinfo.h"
312#endif
Jason Tishlerc246cb72004-08-09 13:25:59 +0000313
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000314#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +0000315#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000316int inet_pton(int af, const char *src, void *dst);
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000317const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000318#endif
Christian Heimesb6150692007-11-15 23:37:07 +0000319#endif
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000320
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000321#ifdef __APPLE__
322/* On OS X, getaddrinfo returns no error indication of lookup
323 failure, so we must use the emulation instead of the libinfo
324 implementation. Unfortunately, performing an autoconf test
325 for this bug would require DNS access for the machine performing
326 the configuration, which is not acceptable. Therefore, we
327 determine the bug just by checking for __APPLE__. If this bug
328 gets ever fixed, perhaps checking for sys/version.h would be
329 appropriate, which is 10/0 on the system with the bug. */
Jack Jansen84262fb2002-07-02 14:40:42 +0000330#ifndef HAVE_GETNAMEINFO
331/* This bug seems to be fixed in Jaguar. Ths easiest way I could
332 Find to check for Jaguar is that it has getnameinfo(), which
333 older releases don't have */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000334#undef HAVE_GETADDRINFO
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000335#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000336
337#ifdef HAVE_INET_ATON
338#define USE_INET_ATON_WEAKLINK
339#endif
340
Jack Jansen84262fb2002-07-02 14:40:42 +0000341#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000342
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000343/* I know this is a bad practice, but it is the easiest... */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000344#if !defined(HAVE_GETADDRINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000345/* avoid clashes with the C library definition of the symbol. */
346#define getaddrinfo fake_getaddrinfo
347#define gai_strerror fake_gai_strerror
348#define freeaddrinfo fake_freeaddrinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000349#include "getaddrinfo.c"
350#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000351#if !defined(HAVE_GETNAMEINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000352#define getnameinfo fake_getnameinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000353#include "getnameinfo.c"
354#endif
355
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000356#ifdef MS_WINDOWS
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000357#define SOCKETCLOSE closesocket
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000358#endif
359
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000360#ifdef MS_WIN32
Kristján Valur Jónsson310052c2012-03-31 13:35:00 +0000361#undef EAFNOSUPPORT
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000362#define EAFNOSUPPORT WSAEAFNOSUPPORT
363#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000364#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000365
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000366#ifndef SOCKETCLOSE
367#define SOCKETCLOSE close
368#endif
369
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +0000370#if (defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)) && !defined(__NetBSD__) && !defined(__DragonFly__)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000371#define USE_BLUETOOTH 1
372#if defined(__FreeBSD__)
373#define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP
374#define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM
Thomas Wouterscf297e42007-02-23 15:07:44 +0000375#define BTPROTO_HCI BLUETOOTH_PROTO_HCI
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000376#define SOL_HCI SOL_HCI_RAW
377#define HCI_FILTER SO_HCI_RAW_FILTER
Hye-Shik Chang81268602004-02-02 06:05:24 +0000378#define sockaddr_l2 sockaddr_l2cap
379#define sockaddr_rc sockaddr_rfcomm
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000380#define hci_dev hci_node
Hye-Shik Chang81268602004-02-02 06:05:24 +0000381#define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb)
382#define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000383#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +0000384#elif defined(__NetBSD__) || defined(__DragonFly__)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000385#define sockaddr_l2 sockaddr_bt
386#define sockaddr_rc sockaddr_bt
Thomas Wouterscf297e42007-02-23 15:07:44 +0000387#define sockaddr_hci sockaddr_bt
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000388#define sockaddr_sco sockaddr_bt
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +0000389#define SOL_HCI BTPROTO_HCI
390#define HCI_DATA_DIR SO_HCI_DIRECTION
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000391#define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb)
392#define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000393#define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000394#define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000395#else
Hye-Shik Chang81268602004-02-02 06:05:24 +0000396#define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)
397#define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000398#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000399#define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)
400#endif
401#endif
402
Charles-François Natali8b759652011-12-23 16:44:51 +0100403/* Convert "sock_addr_t *" to "struct sockaddr *". */
404#define SAS2SA(x) (&((x)->sa))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000405
Martin v. Löwise9416172003-05-03 10:12:45 +0000406/*
407 * Constants for getnameinfo()
408 */
409#if !defined(NI_MAXHOST)
410#define NI_MAXHOST 1025
411#endif
412#if !defined(NI_MAXSERV)
413#define NI_MAXSERV 32
414#endif
415
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000416#ifndef INVALID_SOCKET /* MS defines this */
417#define INVALID_SOCKET (-1)
418#endif
419
Charles-François Natali0cc86852013-09-13 19:53:08 +0200420#ifndef INADDR_NONE
421#define INADDR_NONE (-1)
422#endif
423
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000424/* XXX There's a problem here: *static* functions are not supposed to have
425 a Py prefix (or use CapitalizedWords). Later... */
426
Guido van Rossum30a685f1991-06-27 15:51:29 +0000427/* Global variable holding the exception type for errors detected
428 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000429static PyObject *socket_herror;
430static PyObject *socket_gaierror;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000431static PyObject *socket_timeout;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000432
Tim Peters643a7fc2002-02-17 04:13:21 +0000433/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000434 The sock_type variable contains pointers to various functions,
435 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000436 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000437static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000438
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000439#if defined(HAVE_POLL_H)
440#include <poll.h>
441#elif defined(HAVE_SYS_POLL_H)
442#include <sys/poll.h>
443#endif
444
Nick Coghlan96fe56a2011-08-22 11:55:57 +1000445/* Largest value to try to store in a socklen_t (used when handling
446 ancillary data). POSIX requires socklen_t to hold at least
447 (2**31)-1 and recommends against storing larger values, but
448 socklen_t was originally int in the BSD interface, so to be on the
449 safe side we use the smaller of (2**31)-1 and INT_MAX. */
450#if INT_MAX > 0x7fffffff
451#define SOCKLEN_T_LIMIT 0x7fffffff
452#else
453#define SOCKLEN_T_LIMIT INT_MAX
454#endif
455
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200456#ifdef HAVE_POLL
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000457/* Instead of select(), we'll use poll() since poll() works on any fd. */
458#define IS_SELECTABLE(s) 1
459/* Can we call select() with this socket without a buffer overrun? */
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000460#else
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200461/* If there's no timeout left, we don't have to call select, so it's a safe,
462 * little white lie. */
Victor Stinner71694d52015-03-28 01:18:54 +0100463#define IS_SELECTABLE(s) (_PyIsSelectable_fd((s)->sock_fd) || (s)->sock_timeout <= 0)
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000464#endif
Neal Norwitz082b2df2006-02-07 07:04:46 +0000465
466static PyObject*
467select_error(void)
468{
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200469 PyErr_SetString(PyExc_OSError, "unable to select on socket");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 return NULL;
Neal Norwitz082b2df2006-02-07 07:04:46 +0000471}
472
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000473#ifdef MS_WINDOWS
Antoine Pitrouc8f01962010-09-28 22:03:27 +0000474#ifndef WSAEAGAIN
475#define WSAEAGAIN WSAEWOULDBLOCK
476#endif
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000477#define CHECK_ERRNO(expected) \
478 (WSAGetLastError() == WSA ## expected)
479#else
480#define CHECK_ERRNO(expected) \
481 (errno == expected)
482#endif
483
Victor Stinnerdaf45552013-08-28 00:53:59 +0200484#ifdef MS_WINDOWS
485/* Does WSASocket() support the WSA_FLAG_NO_HANDLE_INHERIT flag? */
486static int support_wsa_no_inherit = -1;
487#endif
488
Guido van Rossum30a685f1991-06-27 15:51:29 +0000489/* Convenience function to raise an error according to errno
490 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000491
Guido van Rossum73624e91994-10-10 17:59:00 +0000492static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000493set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000494{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000495#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 int err_no = WSAGetLastError();
497 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
498 recognizes the error codes used by both GetLastError() and
499 WSAGetLastError */
500 if (err_no)
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200501 return PyErr_SetExcFromWindowsErr(PyExc_OSError, err_no);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000502#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000503
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200504 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000505}
506
Guido van Rossum30a685f1991-06-27 15:51:29 +0000507
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000508static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000509set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000512
513#ifdef HAVE_HSTRERROR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000515#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 v = Py_BuildValue("(is)", h_error, "host not found");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000517#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 if (v != NULL) {
519 PyErr_SetObject(socket_herror, v);
520 Py_DECREF(v);
521 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000524}
525
526
527static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000528set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000531
Martin v. Löwis272cb402002-03-01 08:31:07 +0000532#ifdef EAI_SYSTEM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 /* EAI_SYSTEM is not available on Windows XP. */
534 if (error == EAI_SYSTEM)
535 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000536#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000537
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000538#ifdef HAVE_GAI_STRERROR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000540#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000542#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 if (v != NULL) {
544 PyErr_SetObject(socket_gaierror, v);
545 Py_DECREF(v);
546 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000549}
550
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000551/* Function to perform the setting of socket blocking mode
552 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000553static int
554internal_setblocking(PySocketSockObject *s, int block)
555{
Victor Stinnerb6dab6b2014-07-23 22:56:55 +0200556#ifdef MS_WINDOWS
557 u_long arg;
558#endif
Victor Stinner9a954832013-12-04 00:41:24 +0100559#if !defined(MS_WINDOWS) \
Christian Heimesaf01f662013-12-21 16:19:10 +0100560 && !((defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)))
Victor Stinner9a954832013-12-04 00:41:24 +0100561 int delay_flag, new_delay_flag;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000562#endif
Antoine Pitroub1c54962010-10-14 15:05:38 +0000563#ifdef SOCK_NONBLOCK
564 if (block)
565 s->sock_type &= (~SOCK_NONBLOCK);
566 else
567 s->sock_type |= SOCK_NONBLOCK;
568#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +0000569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 Py_BEGIN_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000571#ifndef MS_WINDOWS
Christian Heimesaf01f662013-12-21 16:19:10 +0100572#if (defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 block = !block;
574 ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);
Victor Stinner9a954832013-12-04 00:41:24 +0100575#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
577 if (block)
Victor Stinner9a954832013-12-04 00:41:24 +0100578 new_delay_flag = delay_flag & (~O_NONBLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 else
Victor Stinner9a954832013-12-04 00:41:24 +0100580 new_delay_flag = delay_flag | O_NONBLOCK;
581 if (new_delay_flag != delay_flag)
582 fcntl(s->sock_fd, F_SETFL, new_delay_flag);
583#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +0000584#else /* MS_WINDOWS */
Victor Stinnerb6dab6b2014-07-23 22:56:55 +0200585 arg = !block;
586 ioctlsocket(s->sock_fd, FIONBIO, &arg);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000587#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 /* Since these don't return anything */
591 return 1;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000592}
593
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
Victor Stinner391fa712015-03-31 13:15:31 +0200600internal_select(PySocketSockObject *s, int writing, _PyTime_t interval)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 int n;
Victor Stinner71694d52015-03-28 01:18:54 +0100603#ifdef HAVE_POLL
604 struct pollfd pollfd;
605 _PyTime_t timeout;
606 int timeout_int;
607#else
608 fd_set fds;
609 struct timeval tv;
610#endif
Guido van Rossum11ba0942002-06-13 15:07:44 +0000611
Victor Stinnerb7df3142015-03-27 22:59:32 +0100612#ifdef WITH_THREAD
613 /* must be called with the GIL held */
614 assert(PyGILState_Check());
615#endif
616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 /* Nothing to do unless we're in timeout mode (not non-blocking) */
Victor Stinner71694d52015-03-28 01:18:54 +0100618 if (s->sock_timeout <= 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 /* Guard against closed socket */
622 if (s->sock_fd < 0)
623 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000624
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000625 /* Handling this condition here simplifies the select loops */
Victor Stinner71694d52015-03-28 01:18:54 +0100626 if (interval < 0)
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000627 return 1;
628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 /* Prefer poll, if available, since you can poll() any fd
630 * which can't be done with select(). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000631#ifdef HAVE_POLL
Victor Stinner71694d52015-03-28 01:18:54 +0100632 pollfd.fd = s->sock_fd;
633 pollfd.events = writing ? POLLOUT : POLLIN;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000634
Victor Stinner71694d52015-03-28 01:18:54 +0100635 /* s->sock_timeout is in seconds, timeout in ms */
Victor Stinner869e1772015-03-30 03:49:14 +0200636 timeout = _PyTime_AsMilliseconds(interval, _PyTime_ROUND_CEILING);
Victor Stinner71694d52015-03-28 01:18:54 +0100637 assert(timeout <= INT_MAX);
638 timeout_int = (int)timeout;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000639
Victor Stinner71694d52015-03-28 01:18:54 +0100640 Py_BEGIN_ALLOW_THREADS;
641 n = poll(&pollfd, 1, timeout_int);
642 Py_END_ALLOW_THREADS;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000643#else
Victor Stinner869e1772015-03-30 03:49:14 +0200644 _PyTime_AsTimeval_noraise(interval, &tv, _PyTime_ROUND_CEILING);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000645
Victor Stinner71694d52015-03-28 01:18:54 +0100646 FD_ZERO(&fds);
647 FD_SET(s->sock_fd, &fds);
648
649 /* See if the socket is ready */
650 Py_BEGIN_ALLOW_THREADS;
651 if (writing)
652 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
653 NULL, &fds, NULL, &tv);
654 else
655 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
656 &fds, NULL, NULL, &tv);
657 Py_END_ALLOW_THREADS;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000658#endif
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 if (n < 0)
661 return -1;
662 if (n == 0)
663 return 1;
664 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000665}
666
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000667/*
668 Two macros for automatic retry of select() in case of false positives
669 (for example, select() could indicate a socket is ready for reading
670 but the data then discarded by the OS because of a wrong checksum).
671 Here is an example of use:
672
673 BEGIN_SELECT_LOOP(s)
Victor Stinnerb7df3142015-03-27 22:59:32 +0100674
Victor Stinner391fa712015-03-31 13:15:31 +0200675 timeout = internal_select(s, 0, interval);
Victor Stinnerb7df3142015-03-27 22:59:32 +0100676
677 if (!timeout) {
678 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000679 outlen = recv(s->sock_fd, cbuf, len, flags);
Victor Stinnerb7df3142015-03-27 22:59:32 +0100680 Py_END_ALLOW_THREADS
681 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000682 if (timeout == 1) {
683 PyErr_SetString(socket_timeout, "timed out");
684 return -1;
685 }
686 END_SELECT_LOOP(s)
687*/
688
689#define BEGIN_SELECT_LOOP(s) \
690 { \
Victor Stinner71694d52015-03-28 01:18:54 +0100691 _PyTime_t deadline = 0; \
692 _PyTime_t interval = s->sock_timeout; \
693 int has_timeout = (s->sock_timeout > 0); \
694 if (has_timeout) \
695 deadline = _PyTime_GetMonotonicClock() + interval; \
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000696 while (1) { \
697 errno = 0; \
698
699#define END_SELECT_LOOP(s) \
700 if (!has_timeout || \
701 (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \
702 break; \
Victor Stinner71694d52015-03-28 01:18:54 +0100703 interval = deadline - _PyTime_GetMonotonicClock(); \
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000704 } \
705 } \
706
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000707/* Initialize a new socket object. */
708
Victor Stinner71694d52015-03-28 01:18:54 +0100709static _PyTime_t defaulttimeout = -1; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000710
Martin v. Löwis1a214512008-06-11 05:26:20 +0000711static void
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000712init_sockobject(PySocketSockObject *s,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 SOCKET_T fd, int family, int type, int proto)
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 s->sock_fd = fd;
716 s->sock_family = family;
717 s->sock_type = type;
718 s->sock_proto = proto;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 s->errorhandler = &set_error;
Antoine Pitroub1c54962010-10-14 15:05:38 +0000721#ifdef SOCK_NONBLOCK
722 if (type & SOCK_NONBLOCK)
Victor Stinner71694d52015-03-28 01:18:54 +0100723 s->sock_timeout = 0;
Antoine Pitroub1c54962010-10-14 15:05:38 +0000724 else
725#endif
726 {
727 s->sock_timeout = defaulttimeout;
Victor Stinner71694d52015-03-28 01:18:54 +0100728 if (defaulttimeout >= 0)
Antoine Pitroub1c54962010-10-14 15:05:38 +0000729 internal_setblocking(s, 0);
730 }
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000731
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000732}
733
734
Guido van Rossum30a685f1991-06-27 15:51:29 +0000735/* Create a new socket object.
736 This just creates the object and initializes it.
737 If the creation fails, return NULL and set an exception (implicit
738 in NEWOBJ()). */
739
Guido van Rossum73624e91994-10-10 17:59:00 +0000740static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000741new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 PySocketSockObject *s;
744 s = (PySocketSockObject *)
745 PyType_GenericNew(&sock_type, NULL, NULL);
746 if (s != NULL)
747 init_sockobject(s, fd, family, type, proto);
748 return s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000749}
750
Guido van Rossum30a685f1991-06-27 15:51:29 +0000751
Guido van Rossum48a680c2001-03-02 06:34:14 +0000752/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000753 thread to be in gethostbyname or getaddrinfo */
754#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Matthias Klosed182a6c2012-08-14 17:29:04 +0200755static PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000756#endif
757
758
Guido van Rossum30a685f1991-06-27 15:51:29 +0000759/* Convert a string specifying a host name or one of a few symbolic
760 names to a numeric IP address. This usually calls gethostbyname()
761 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000762 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000763 an error occurred; then an exception is raised. */
764
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000765static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000766setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 struct addrinfo hints, *res;
769 int error;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
772 if (name[0] == '\0') {
773 int siz;
774 memset(&hints, 0, sizeof(hints));
775 hints.ai_family = af;
776 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
777 hints.ai_flags = AI_PASSIVE;
778 Py_BEGIN_ALLOW_THREADS
779 ACQUIRE_GETADDRINFO_LOCK
780 error = getaddrinfo(NULL, "0", &hints, &res);
781 Py_END_ALLOW_THREADS
782 /* We assume that those thread-unsafe getaddrinfo() versions
783 *are* safe regarding their return value, ie. that a
784 subsequent call to getaddrinfo() does not destroy the
785 outcome of the first call. */
786 RELEASE_GETADDRINFO_LOCK
787 if (error) {
788 set_gaierror(error);
789 return -1;
790 }
791 switch (res->ai_family) {
792 case AF_INET:
793 siz = 4;
794 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000795#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 case AF_INET6:
797 siz = 16;
798 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000799#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 default:
801 freeaddrinfo(res);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200802 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 "unsupported address family");
804 return -1;
805 }
806 if (res->ai_next) {
807 freeaddrinfo(res);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200808 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 "wildcard resolved to multiple address");
810 return -1;
811 }
812 if (res->ai_addrlen < addr_ret_size)
813 addr_ret_size = res->ai_addrlen;
814 memcpy(addr_ret, res->ai_addr, addr_ret_size);
815 freeaddrinfo(res);
816 return siz;
817 }
Charles-François Natali0cc86852013-09-13 19:53:08 +0200818 /* special-case broadcast - inet_addr() below can return INADDR_NONE for
Victor Stinnere990c6e2013-11-16 00:18:58 +0100819 * this */
Charles-François Natali0cc86852013-09-13 19:53:08 +0200820 if (strcmp(name, "255.255.255.255") == 0 ||
821 strcmp(name, "<broadcast>") == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 struct sockaddr_in *sin;
823 if (af != AF_INET && af != AF_UNSPEC) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200824 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 "address family mismatched");
826 return -1;
827 }
828 sin = (struct sockaddr_in *)addr_ret;
829 memset((void *) sin, '\0', sizeof(*sin));
830 sin->sin_family = AF_INET;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000831#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 sin->sin_len = sizeof(*sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000833#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 sin->sin_addr.s_addr = INADDR_BROADCAST;
835 return sizeof(sin->sin_addr);
836 }
Charles-François Natali0cc86852013-09-13 19:53:08 +0200837
838 /* avoid a name resolution in case of numeric address */
839#ifdef HAVE_INET_PTON
840 /* check for an IPv4 address */
841 if (af == AF_UNSPEC || af == AF_INET) {
842 struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret;
843 memset(sin, 0, sizeof(*sin));
844 if (inet_pton(AF_INET, name, &sin->sin_addr) > 0) {
845 sin->sin_family = AF_INET;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000846#ifdef HAVE_SOCKADDR_SA_LEN
Charles-François Natali0cc86852013-09-13 19:53:08 +0200847 sin->sin_len = sizeof(*sin);
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000848#endif
Charles-François Natali0cc86852013-09-13 19:53:08 +0200849 return 4;
850 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 }
Charles-François Natali0cc86852013-09-13 19:53:08 +0200852#ifdef ENABLE_IPV6
853 /* check for an IPv6 address - if the address contains a scope ID, we
854 * fallback to getaddrinfo(), which can handle translation from interface
855 * name to interface index */
856 if ((af == AF_UNSPEC || af == AF_INET6) && !strchr(name, '%')) {
857 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)addr_ret;
858 memset(sin, 0, sizeof(*sin));
859 if (inet_pton(AF_INET6, name, &sin->sin6_addr) > 0) {
860 sin->sin6_family = AF_INET6;
861#ifdef HAVE_SOCKADDR_SA_LEN
862 sin->sin6_len = sizeof(*sin);
863#endif
864 return 16;
865 }
866 }
867#endif /* ENABLE_IPV6 */
868#else /* HAVE_INET_PTON */
869 /* check for an IPv4 address */
870 if (af == AF_INET || af == AF_UNSPEC) {
871 struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret;
872 memset(sin, 0, sizeof(*sin));
873 if ((sin->sin_addr.s_addr = inet_addr(name)) != INADDR_NONE) {
874 sin->sin_family = AF_INET;
875#ifdef HAVE_SOCKADDR_SA_LEN
876 sin->sin_len = sizeof(*sin);
877#endif
878 return 4;
879 }
Victor Stinnere990c6e2013-11-16 00:18:58 +0100880 }
Charles-François Natali0cc86852013-09-13 19:53:08 +0200881#endif /* HAVE_INET_PTON */
882
883 /* perform a name resolution */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 memset(&hints, 0, sizeof(hints));
885 hints.ai_family = af;
886 Py_BEGIN_ALLOW_THREADS
887 ACQUIRE_GETADDRINFO_LOCK
888 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000889#if defined(__digital__) && defined(__unix__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 if (error == EAI_NONAME && af == AF_UNSPEC) {
891 /* On Tru64 V5.1, numeric-to-addr conversion fails
892 if no address family is given. Assume IPv4 for now.*/
893 hints.ai_family = AF_INET;
894 error = getaddrinfo(name, NULL, &hints, &res);
895 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000896#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 Py_END_ALLOW_THREADS
898 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
899 if (error) {
900 set_gaierror(error);
901 return -1;
902 }
903 if (res->ai_addrlen < addr_ret_size)
904 addr_ret_size = res->ai_addrlen;
905 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
906 freeaddrinfo(res);
907 switch (addr_ret->sa_family) {
908 case AF_INET:
909 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000910#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 case AF_INET6:
912 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000913#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200915 PyErr_SetString(PyExc_OSError, "unknown address family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 return -1;
917 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000918}
919
Guido van Rossum30a685f1991-06-27 15:51:29 +0000920
Guido van Rossum30a685f1991-06-27 15:51:29 +0000921/* Create a string object representing an IP address.
922 This is always a string of the form 'dd.dd.dd.dd' (with variable
923 size numbers). */
924
Guido van Rossum73624e91994-10-10 17:59:00 +0000925static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000926makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000927{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 char buf[NI_MAXHOST];
929 int error;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
932 NI_NUMERICHOST);
933 if (error) {
934 set_gaierror(error);
935 return NULL;
936 }
937 return PyUnicode_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000938}
939
940
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000941#ifdef USE_BLUETOOTH
942/* Convert a string representation of a Bluetooth address into a numeric
943 address. Returns the length (6), or raises an exception and returns -1 if
944 an error occurred. */
945
946static int
947setbdaddr(char *name, bdaddr_t *bdaddr)
948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 unsigned int b0, b1, b2, b3, b4, b5;
950 char ch;
951 int n;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
954 &b5, &b4, &b3, &b2, &b1, &b0, &ch);
955 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
956 bdaddr->b[0] = b0;
957 bdaddr->b[1] = b1;
958 bdaddr->b[2] = b2;
959 bdaddr->b[3] = b3;
960 bdaddr->b[4] = b4;
961 bdaddr->b[5] = b5;
962 return 6;
963 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200964 PyErr_SetString(PyExc_OSError, "bad bluetooth address");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 return -1;
966 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000967}
968
969/* Create a string representation of the Bluetooth address. This is always a
970 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
971 value (zero padded if necessary). */
972
973static PyObject *
974makebdaddr(bdaddr_t *bdaddr)
975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 char buf[(6 * 2) + 5 + 1];
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
979 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
980 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
981 return PyUnicode_FromString(buf);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000982}
983#endif
984
985
Guido van Rossum30a685f1991-06-27 15:51:29 +0000986/* Create an object representing the given socket address,
987 suitable for passing it back to bind(), connect() etc.
988 The family field of the sockaddr structure is inspected
989 to determine what kind of address it really is. */
990
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000991/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000992static PyObject *
Antoine Pitrou19467d22010-08-17 19:33:30 +0000993makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 if (addrlen == 0) {
996 /* No address -- may be recvfrom() from known socket */
997 Py_INCREF(Py_None);
998 return Py_None;
999 }
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 switch (addr->sa_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +00001002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 case AF_INET:
1004 {
1005 struct sockaddr_in *a;
1006 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
1007 PyObject *ret = NULL;
1008 if (addrobj) {
1009 a = (struct sockaddr_in *)addr;
1010 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
1011 Py_DECREF(addrobj);
1012 }
1013 return ret;
1014 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00001015
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001016#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 case AF_UNIX:
1018 {
1019 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001020#ifdef linux
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 if (a->sun_path[0] == 0) { /* Linux abstract namespace */
1022 addrlen -= offsetof(struct sockaddr_un, sun_path);
Charles-François Natalib10c71d2013-05-21 10:45:46 +02001023 return PyBytes_FromStringAndSize(a->sun_path, addrlen);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 }
1025 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001026#endif /* linux */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 {
1028 /* regular NULL-terminated string */
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001029 return PyUnicode_DecodeFSDefault(a->sun_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 }
1031 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001032#endif /* AF_UNIX */
1033
Martin v. Löwis11017b12006-01-14 18:12:57 +00001034#if defined(AF_NETLINK)
1035 case AF_NETLINK:
1036 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
1038 return Py_BuildValue("II", a->nl_pid, a->nl_groups);
Martin v. Löwis11017b12006-01-14 18:12:57 +00001039 }
1040#endif /* AF_NETLINK */
1041
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001042#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 case AF_INET6:
1044 {
1045 struct sockaddr_in6 *a;
1046 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
1047 PyObject *ret = NULL;
1048 if (addrobj) {
1049 a = (struct sockaddr_in6 *)addr;
Charles-François Natali366999a2012-01-02 15:47:29 +01001050 ret = Py_BuildValue("OiII",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 addrobj,
1052 ntohs(a->sin6_port),
Charles-François Natali366999a2012-01-02 15:47:29 +01001053 ntohl(a->sin6_flowinfo),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 a->sin6_scope_id);
1055 Py_DECREF(addrobj);
1056 }
1057 return ret;
1058 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001059#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001060
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001061#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 case AF_BLUETOOTH:
1063 switch (proto) {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 case BTPROTO_L2CAP:
1066 {
1067 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
1068 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
1069 PyObject *ret = NULL;
1070 if (addrobj) {
1071 ret = Py_BuildValue("Oi",
1072 addrobj,
1073 _BT_L2_MEMB(a, psm));
1074 Py_DECREF(addrobj);
1075 }
1076 return ret;
1077 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 case BTPROTO_RFCOMM:
1080 {
1081 struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
1082 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
1083 PyObject *ret = NULL;
1084 if (addrobj) {
1085 ret = Py_BuildValue("Oi",
1086 addrobj,
1087 _BT_RC_MEMB(a, channel));
1088 Py_DECREF(addrobj);
1089 }
1090 return ret;
1091 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 case BTPROTO_HCI:
1094 {
1095 struct sockaddr_hci *a = (struct sockaddr_hci *) addr;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001096#if defined(__NetBSD__) || defined(__DragonFly__)
1097 return makebdaddr(&_BT_HCI_MEMB(a, bdaddr));
1098#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 PyObject *ret = NULL;
1100 ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
1101 return ret;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001102#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 }
Thomas Wouterscf297e42007-02-23 15:07:44 +00001104
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001105#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 case BTPROTO_SCO:
1107 {
1108 struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
1109 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
1110 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001111#endif
1112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 default:
1114 PyErr_SetString(PyExc_ValueError,
1115 "Unknown Bluetooth protocol");
1116 return NULL;
1117 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001118#endif
1119
Antoine Pitroub156a462010-10-27 20:13:57 +00001120#if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 case AF_PACKET:
1122 {
1123 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
1124 char *ifname = "";
1125 struct ifreq ifr;
1126 /* need to look up interface name give index */
1127 if (a->sll_ifindex) {
1128 ifr.ifr_ifindex = a->sll_ifindex;
1129 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
1130 ifname = ifr.ifr_name;
1131 }
1132 return Py_BuildValue("shbhy#",
1133 ifname,
1134 ntohs(a->sll_protocol),
1135 a->sll_pkttype,
1136 a->sll_hatype,
1137 a->sll_addr,
1138 a->sll_halen);
1139 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001140#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001141
Christian Heimes043d6f62008-01-07 17:19:16 +00001142#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 case AF_TIPC:
1144 {
1145 struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr;
1146 if (a->addrtype == TIPC_ADDR_NAMESEQ) {
1147 return Py_BuildValue("IIIII",
1148 a->addrtype,
1149 a->addr.nameseq.type,
1150 a->addr.nameseq.lower,
1151 a->addr.nameseq.upper,
1152 a->scope);
1153 } else if (a->addrtype == TIPC_ADDR_NAME) {
1154 return Py_BuildValue("IIIII",
1155 a->addrtype,
1156 a->addr.name.name.type,
1157 a->addr.name.name.instance,
1158 a->addr.name.name.instance,
1159 a->scope);
1160 } else if (a->addrtype == TIPC_ADDR_ID) {
1161 return Py_BuildValue("IIIII",
1162 a->addrtype,
1163 a->addr.id.node,
1164 a->addr.id.ref,
1165 0,
1166 a->scope);
1167 } else {
1168 PyErr_SetString(PyExc_ValueError,
1169 "Invalid address type");
1170 return NULL;
1171 }
1172 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001173#endif
1174
Vinay Sajipecfc98c2014-03-20 12:42:42 +00001175#ifdef AF_CAN
Charles-François Natali47413c12011-10-06 19:47:44 +02001176 case AF_CAN:
1177 {
1178 struct sockaddr_can *a = (struct sockaddr_can *)addr;
1179 char *ifname = "";
1180 struct ifreq ifr;
1181 /* need to look up interface name given index */
1182 if (a->can_ifindex) {
1183 ifr.ifr_ifindex = a->can_ifindex;
1184 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
1185 ifname = ifr.ifr_name;
1186 }
1187
1188 return Py_BuildValue("O&h", PyUnicode_DecodeFSDefault,
1189 ifname,
1190 a->can_family);
1191 }
1192#endif
1193
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001194#ifdef PF_SYSTEM
1195 case PF_SYSTEM:
1196 switch(proto) {
1197#ifdef SYSPROTO_CONTROL
1198 case SYSPROTO_CONTROL:
1199 {
1200 struct sockaddr_ctl *a = (struct sockaddr_ctl *)addr;
1201 return Py_BuildValue("(II)", a->sc_id, a->sc_unit);
1202 }
1203#endif
1204 default:
1205 PyErr_SetString(PyExc_ValueError,
1206 "Invalid address type");
1207 return 0;
1208 }
1209#endif
1210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 default:
1214 /* If we don't know the address family, don't raise an
1215 exception -- return it as an (int, bytes) tuple. */
1216 return Py_BuildValue("iy#",
1217 addr->sa_family,
1218 addr->sa_data,
1219 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001222}
1223
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001224/* Helper for getsockaddrarg: bypass IDNA for ASCII-only host names
1225 (in particular, numeric IP addresses). */
1226struct maybe_idna {
1227 PyObject *obj;
1228 char *buf;
1229};
1230
1231static void
1232idna_cleanup(struct maybe_idna *data)
1233{
1234 Py_CLEAR(data->obj);
1235}
1236
1237static int
1238idna_converter(PyObject *obj, struct maybe_idna *data)
1239{
1240 size_t len;
1241 PyObject *obj2, *obj3;
1242 if (obj == NULL) {
1243 idna_cleanup(data);
1244 return 1;
1245 }
1246 data->obj = NULL;
1247 len = -1;
1248 if (PyBytes_Check(obj)) {
1249 data->buf = PyBytes_AsString(obj);
1250 len = PyBytes_Size(obj);
1251 }
1252 else if (PyByteArray_Check(obj)) {
1253 data->buf = PyByteArray_AsString(obj);
1254 len = PyByteArray_Size(obj);
1255 }
1256 else if (PyUnicode_Check(obj) && PyUnicode_READY(obj) == 0 && PyUnicode_IS_COMPACT_ASCII(obj)) {
1257 data->buf = PyUnicode_DATA(obj);
1258 len = PyUnicode_GET_LENGTH(obj);
1259 }
1260 else {
1261 obj2 = PyUnicode_FromObject(obj);
1262 if (!obj2) {
1263 PyErr_Format(PyExc_TypeError, "string or unicode text buffer expected, not %s",
1264 obj->ob_type->tp_name);
1265 return 0;
1266 }
1267 obj3 = PyUnicode_AsEncodedString(obj2, "idna", NULL);
1268 Py_DECREF(obj2);
1269 if (!obj3) {
1270 PyErr_SetString(PyExc_TypeError, "encoding of hostname failed");
1271 return 0;
1272 }
1273 if (!PyBytes_Check(obj3)) {
Martin v. Löwis17fd1e12014-08-05 16:13:50 +02001274 Py_DECREF(obj3);
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001275 PyErr_SetString(PyExc_TypeError, "encoding of hostname failed to return bytes");
1276 return 0;
1277 }
1278 data->obj = obj3;
1279 data->buf = PyBytes_AS_STRING(obj3);
1280 len = PyBytes_GET_SIZE(obj3);
1281 }
1282 if (strlen(data->buf) != len) {
1283 Py_CLEAR(data->obj);
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001284 PyErr_SetString(PyExc_TypeError, "host name must not contain null character");
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001285 return 0;
1286 }
1287 return Py_CLEANUP_SUPPORTED;
Victor Stinner1a62a682014-08-17 19:33:28 +02001288}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001289
1290/* Parse a socket address argument according to the socket object's
1291 address family. Return 1 if the address was in the proper format,
1292 0 of not. The address is returned through addr_ret, its length
1293 through len_ret. */
1294
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001295static int
Guido van Rossum48a680c2001-03-02 06:34:14 +00001296getsockaddrarg(PySocketSockObject *s, PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 struct sockaddr *addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 switch (s->sock_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +00001300
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001301#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 case AF_UNIX:
1303 {
1304 struct sockaddr_un* addr;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001305 Py_buffer path;
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001306 int retval = 0;
1307
1308 /* PEP 383. Not using PyUnicode_FSConverter since we need to
1309 allow embedded nulls on Linux. */
1310 if (PyUnicode_Check(args)) {
1311 if ((args = PyUnicode_EncodeFSDefault(args)) == NULL)
1312 return 0;
1313 }
1314 else
1315 Py_INCREF(args);
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001316 if (!PyArg_Parse(args, "y*", &path)) {
1317 Py_DECREF(args);
1318 return retval;
1319 }
1320 assert(path.len >= 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 addr = (struct sockaddr_un*)addr_ret;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001323#ifdef linux
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001324 if (path.len > 0 && *(const char *)path.buf == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 /* Linux abstract namespace extension */
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001326 if ((size_t)path.len > sizeof addr->sun_path) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001327 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 "AF_UNIX path too long");
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001329 goto unix_out;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 }
1331 }
1332 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001333#endif /* linux */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 {
1335 /* regular NULL-terminated string */
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001336 if ((size_t)path.len >= sizeof addr->sun_path) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001337 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 "AF_UNIX path too long");
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001339 goto unix_out;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 }
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001341 addr->sun_path[path.len] = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 }
1343 addr->sun_family = s->sock_family;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001344 memcpy(addr->sun_path, path.buf, path.len);
1345 *len_ret = path.len + offsetof(struct sockaddr_un, sun_path);
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001346 retval = 1;
1347 unix_out:
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001348 PyBuffer_Release(&path);
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001349 Py_DECREF(args);
1350 return retval;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001352#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001353
Martin v. Löwis11017b12006-01-14 18:12:57 +00001354#if defined(AF_NETLINK)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 case AF_NETLINK:
1356 {
1357 struct sockaddr_nl* addr;
1358 int pid, groups;
1359 addr = (struct sockaddr_nl *)addr_ret;
1360 if (!PyTuple_Check(args)) {
1361 PyErr_Format(
1362 PyExc_TypeError,
1363 "getsockaddrarg: "
1364 "AF_NETLINK address must be tuple, not %.500s",
1365 Py_TYPE(args)->tp_name);
1366 return 0;
1367 }
1368 if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
1369 return 0;
1370 addr->nl_family = AF_NETLINK;
1371 addr->nl_pid = pid;
1372 addr->nl_groups = groups;
1373 *len_ret = sizeof(*addr);
1374 return 1;
1375 }
Martin v. Löwis11017b12006-01-14 18:12:57 +00001376#endif
1377
Charles-François Natali10b8cf42011-11-10 19:21:37 +01001378#ifdef AF_RDS
1379 case AF_RDS:
1380 /* RDS sockets use sockaddr_in: fall-through */
1381#endif
1382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 case AF_INET:
1384 {
1385 struct sockaddr_in* addr;
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001386 struct maybe_idna host = {NULL, NULL};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 int port, result;
1388 if (!PyTuple_Check(args)) {
1389 PyErr_Format(
1390 PyExc_TypeError,
1391 "getsockaddrarg: "
1392 "AF_INET address must be tuple, not %.500s",
1393 Py_TYPE(args)->tp_name);
1394 return 0;
1395 }
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001396 if (!PyArg_ParseTuple(args, "O&i:getsockaddrarg",
1397 idna_converter, &host, &port))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 return 0;
1399 addr=(struct sockaddr_in*)addr_ret;
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001400 result = setipaddr(host.buf, (struct sockaddr *)addr,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 sizeof(*addr), AF_INET);
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001402 idna_cleanup(&host);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 if (result < 0)
1404 return 0;
1405 if (port < 0 || port > 0xffff) {
1406 PyErr_SetString(
1407 PyExc_OverflowError,
1408 "getsockaddrarg: port must be 0-65535.");
1409 return 0;
1410 }
1411 addr->sin_family = AF_INET;
1412 addr->sin_port = htons((short)port);
1413 *len_ret = sizeof *addr;
1414 return 1;
1415 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001416
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001417#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 case AF_INET6:
1419 {
1420 struct sockaddr_in6* addr;
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001421 struct maybe_idna host = {NULL, NULL};
Charles-François Natali366999a2012-01-02 15:47:29 +01001422 int port, result;
1423 unsigned int flowinfo, scope_id;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 flowinfo = scope_id = 0;
1425 if (!PyTuple_Check(args)) {
1426 PyErr_Format(
1427 PyExc_TypeError,
1428 "getsockaddrarg: "
1429 "AF_INET6 address must be tuple, not %.500s",
1430 Py_TYPE(args)->tp_name);
1431 return 0;
1432 }
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001433 if (!PyArg_ParseTuple(args, "O&i|II",
1434 idna_converter, &host, &port, &flowinfo,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 &scope_id)) {
1436 return 0;
1437 }
1438 addr = (struct sockaddr_in6*)addr_ret;
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001439 result = setipaddr(host.buf, (struct sockaddr *)addr,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 sizeof(*addr), AF_INET6);
Martin v. Löwiseb1c28a2014-08-05 16:10:38 +02001441 idna_cleanup(&host);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 if (result < 0)
1443 return 0;
1444 if (port < 0 || port > 0xffff) {
1445 PyErr_SetString(
1446 PyExc_OverflowError,
1447 "getsockaddrarg: port must be 0-65535.");
1448 return 0;
1449 }
Charles-François Natali9b0c0062012-06-23 10:17:05 +02001450 if (flowinfo > 0xfffff) {
Charles-François Natali366999a2012-01-02 15:47:29 +01001451 PyErr_SetString(
1452 PyExc_OverflowError,
1453 "getsockaddrarg: flowinfo must be 0-1048575.");
1454 return 0;
1455 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 addr->sin6_family = s->sock_family;
1457 addr->sin6_port = htons((short)port);
Charles-François Natali366999a2012-01-02 15:47:29 +01001458 addr->sin6_flowinfo = htonl(flowinfo);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 addr->sin6_scope_id = scope_id;
1460 *len_ret = sizeof *addr;
1461 return 1;
1462 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001463#endif
1464
Hye-Shik Chang81268602004-02-02 06:05:24 +00001465#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 case AF_BLUETOOTH:
1467 {
1468 switch (s->sock_proto) {
1469 case BTPROTO_L2CAP:
1470 {
1471 struct sockaddr_l2 *addr;
1472 char *straddr;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 addr = (struct sockaddr_l2 *)addr_ret;
1475 memset(addr, 0, sizeof(struct sockaddr_l2));
1476 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
1477 if (!PyArg_ParseTuple(args, "si", &straddr,
1478 &_BT_L2_MEMB(addr, psm))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001479 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 "wrong format");
1481 return 0;
1482 }
1483 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
1484 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 *len_ret = sizeof *addr;
1487 return 1;
1488 }
1489 case BTPROTO_RFCOMM:
1490 {
1491 struct sockaddr_rc *addr;
1492 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 addr = (struct sockaddr_rc *)addr_ret;
1495 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
1496 if (!PyArg_ParseTuple(args, "si", &straddr,
1497 &_BT_RC_MEMB(addr, channel))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001498 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 "wrong format");
1500 return 0;
1501 }
1502 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
1503 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 *len_ret = sizeof *addr;
1506 return 1;
1507 }
1508 case BTPROTO_HCI:
1509 {
1510 struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001511#if defined(__NetBSD__) || defined(__DragonFly__)
Alexander Belopolskye239d232010-12-08 23:31:48 +00001512 char *straddr = PyBytes_AS_STRING(args);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001513
Alexander Belopolskye239d232010-12-08 23:31:48 +00001514 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001515 if (straddr == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001516 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001517 "wrong format");
1518 return 0;
1519 }
1520 if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0)
1521 return 0;
1522#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
1524 if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001525 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 "wrong format");
1527 return 0;
1528 }
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001529#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 *len_ret = sizeof *addr;
1531 return 1;
1532 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001533#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 case BTPROTO_SCO:
1535 {
1536 struct sockaddr_sco *addr;
1537 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 addr = (struct sockaddr_sco *)addr_ret;
1540 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
1541 if (!PyBytes_Check(args)) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001542 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 "wrong format");
1544 return 0;
1545 }
1546 straddr = PyBytes_AS_STRING(args);
1547 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
1548 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 *len_ret = sizeof *addr;
1551 return 1;
1552 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001553#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001555 PyErr_SetString(PyExc_OSError, "getsockaddrarg: unknown Bluetooth protocol");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 return 0;
1557 }
1558 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001559#endif
1560
Antoine Pitroub156a462010-10-27 20:13:57 +00001561#if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 case AF_PACKET:
1563 {
1564 struct sockaddr_ll* addr;
1565 struct ifreq ifr;
1566 char *interfaceName;
1567 int protoNumber;
1568 int hatype = 0;
1569 int pkttype = 0;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001570 Py_buffer haddr = {NULL, NULL};
Guido van Rossum48a680c2001-03-02 06:34:14 +00001571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 if (!PyTuple_Check(args)) {
1573 PyErr_Format(
1574 PyExc_TypeError,
1575 "getsockaddrarg: "
1576 "AF_PACKET address must be tuple, not %.500s",
1577 Py_TYPE(args)->tp_name);
1578 return 0;
1579 }
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001580 if (!PyArg_ParseTuple(args, "si|iiy*", &interfaceName,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 &protoNumber, &pkttype, &hatype,
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001582 &haddr))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 return 0;
1584 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1585 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
1586 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
1587 s->errorhandler();
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001588 PyBuffer_Release(&haddr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 return 0;
1590 }
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001591 if (haddr.buf && haddr.len > 8) {
1592 PyErr_SetString(PyExc_ValueError,
1593 "Hardware address must be 8 bytes or less");
1594 PyBuffer_Release(&haddr);
1595 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 }
1597 if (protoNumber < 0 || protoNumber > 0xffff) {
1598 PyErr_SetString(
1599 PyExc_OverflowError,
1600 "getsockaddrarg: protoNumber must be 0-65535.");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001601 PyBuffer_Release(&haddr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 return 0;
1603 }
1604 addr = (struct sockaddr_ll*)addr_ret;
1605 addr->sll_family = AF_PACKET;
1606 addr->sll_protocol = htons((short)protoNumber);
1607 addr->sll_ifindex = ifr.ifr_ifindex;
1608 addr->sll_pkttype = pkttype;
1609 addr->sll_hatype = hatype;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001610 if (haddr.buf) {
1611 memcpy(&addr->sll_addr, haddr.buf, haddr.len);
1612 addr->sll_halen = haddr.len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 }
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001614 else
1615 addr->sll_halen = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 *len_ret = sizeof *addr;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02001617 PyBuffer_Release(&haddr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 return 1;
1619 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001620#endif
1621
Christian Heimes043d6f62008-01-07 17:19:16 +00001622#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 case AF_TIPC:
1624 {
1625 unsigned int atype, v1, v2, v3;
1626 unsigned int scope = TIPC_CLUSTER_SCOPE;
1627 struct sockaddr_tipc *addr;
Christian Heimes043d6f62008-01-07 17:19:16 +00001628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 if (!PyTuple_Check(args)) {
1630 PyErr_Format(
1631 PyExc_TypeError,
1632 "getsockaddrarg: "
1633 "AF_TIPC address must be tuple, not %.500s",
1634 Py_TYPE(args)->tp_name);
1635 return 0;
1636 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 if (!PyArg_ParseTuple(args,
1639 "IIII|I;Invalid TIPC address format",
1640 &atype, &v1, &v2, &v3, &scope))
1641 return 0;
Christian Heimes043d6f62008-01-07 17:19:16 +00001642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 addr = (struct sockaddr_tipc *) addr_ret;
1644 memset(addr, 0, sizeof(struct sockaddr_tipc));
Christian Heimes043d6f62008-01-07 17:19:16 +00001645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 addr->family = AF_TIPC;
1647 addr->scope = scope;
1648 addr->addrtype = atype;
Christian Heimes043d6f62008-01-07 17:19:16 +00001649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 if (atype == TIPC_ADDR_NAMESEQ) {
1651 addr->addr.nameseq.type = v1;
1652 addr->addr.nameseq.lower = v2;
1653 addr->addr.nameseq.upper = v3;
1654 } else if (atype == TIPC_ADDR_NAME) {
1655 addr->addr.name.name.type = v1;
1656 addr->addr.name.name.instance = v2;
1657 } else if (atype == TIPC_ADDR_ID) {
1658 addr->addr.id.node = v1;
1659 addr->addr.id.ref = v2;
1660 } else {
1661 /* Shouldn't happen */
1662 PyErr_SetString(PyExc_TypeError, "Invalid address type");
1663 return 0;
1664 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 *len_ret = sizeof(*addr);
Christian Heimes043d6f62008-01-07 17:19:16 +00001667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 return 1;
1669 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001670#endif
1671
Vinay Sajiped6783f2014-03-21 11:44:32 +00001672#if defined(AF_CAN) && defined(CAN_RAW) && defined(CAN_BCM)
Charles-François Natali47413c12011-10-06 19:47:44 +02001673 case AF_CAN:
1674 switch (s->sock_proto) {
1675 case CAN_RAW:
Charles-François Natali773e42d2013-02-05 19:42:01 +01001676 /* fall-through */
1677 case CAN_BCM:
Charles-François Natali47413c12011-10-06 19:47:44 +02001678 {
1679 struct sockaddr_can *addr;
1680 PyObject *interfaceName;
1681 struct ifreq ifr;
Charles-François Natali47413c12011-10-06 19:47:44 +02001682 Py_ssize_t len;
1683
Benjamin Peterson18b71912013-05-16 15:29:44 -05001684 addr = (struct sockaddr_can *)addr_ret;
1685
Charles-François Natali47413c12011-10-06 19:47:44 +02001686 if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter,
1687 &interfaceName))
1688 return 0;
1689
1690 len = PyBytes_GET_SIZE(interfaceName);
1691
1692 if (len == 0) {
1693 ifr.ifr_ifindex = 0;
Victor Stinner1a62a682014-08-17 19:33:28 +02001694 } else if ((size_t)len < sizeof(ifr.ifr_name)) {
Christian Heimes15b68852012-09-10 01:25:50 +02001695 strncpy(ifr.ifr_name, PyBytes_AS_STRING(interfaceName), sizeof(ifr.ifr_name));
1696 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Charles-François Natali47413c12011-10-06 19:47:44 +02001697 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
1698 s->errorhandler();
1699 Py_DECREF(interfaceName);
1700 return 0;
1701 }
1702 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001703 PyErr_SetString(PyExc_OSError,
Charles-François Natali47413c12011-10-06 19:47:44 +02001704 "AF_CAN interface name too long");
1705 Py_DECREF(interfaceName);
1706 return 0;
1707 }
1708
1709 addr->can_family = AF_CAN;
1710 addr->can_ifindex = ifr.ifr_ifindex;
1711
1712 *len_ret = sizeof(*addr);
1713 Py_DECREF(interfaceName);
1714 return 1;
1715 }
1716 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001717 PyErr_SetString(PyExc_OSError,
Charles-François Natali47413c12011-10-06 19:47:44 +02001718 "getsockaddrarg: unsupported CAN protocol");
1719 return 0;
1720 }
1721#endif
Victor Stinnera534fc42013-06-03 22:07:27 +02001722
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001723#ifdef PF_SYSTEM
1724 case PF_SYSTEM:
1725 switch (s->sock_proto) {
1726#ifdef SYSPROTO_CONTROL
1727 case SYSPROTO_CONTROL:
1728 {
1729 struct sockaddr_ctl *addr;
Victor Stinnera534fc42013-06-03 22:07:27 +02001730
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001731 addr = (struct sockaddr_ctl *)addr_ret;
1732 addr->sc_family = AF_SYSTEM;
Victor Stinnera534fc42013-06-03 22:07:27 +02001733 addr->ss_sysaddr = AF_SYS_CONTROL;
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001734
1735 if (PyUnicode_Check(args)) {
1736 struct ctl_info info;
1737 PyObject *ctl_name;
1738
1739 if (!PyArg_Parse(args, "O&",
1740 PyUnicode_FSConverter, &ctl_name)) {
1741 return 0;
1742 }
1743
Victor Stinnerf50e1872015-03-20 11:32:24 +01001744 if (PyBytes_GET_SIZE(ctl_name) > (Py_ssize_t)sizeof(info.ctl_name)) {
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001745 PyErr_SetString(PyExc_ValueError,
1746 "provided string is too long");
1747 Py_DECREF(ctl_name);
1748 return 0;
1749 }
1750 strncpy(info.ctl_name, PyBytes_AS_STRING(ctl_name),
1751 sizeof(info.ctl_name));
1752 Py_DECREF(ctl_name);
1753
1754 if (ioctl(s->sock_fd, CTLIOCGINFO, &info)) {
1755 PyErr_SetString(PyExc_OSError,
1756 "cannot find kernel control with provided name");
1757 return 0;
1758 }
Victor Stinnera534fc42013-06-03 22:07:27 +02001759
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001760 addr->sc_id = info.ctl_id;
1761 addr->sc_unit = 0;
1762 } else if (!PyArg_ParseTuple(args, "II",
1763 &(addr->sc_id), &(addr->sc_unit))) {
1764 PyErr_SetString(PyExc_TypeError, "getsockaddrarg: "
1765 "expected str or tuple of two ints");
Victor Stinnera534fc42013-06-03 22:07:27 +02001766
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001767 return 0;
1768 }
Victor Stinnera534fc42013-06-03 22:07:27 +02001769
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001770 *len_ret = sizeof(*addr);
1771 return 1;
1772 }
1773#endif
1774 default:
1775 PyErr_SetString(PyExc_OSError,
1776 "getsockaddrarg: unsupported PF_SYSTEM protocol");
1777 return 0;
1778 }
1779#endif
Charles-François Natali47413c12011-10-06 19:47:44 +02001780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001784 PyErr_SetString(PyExc_OSError, "getsockaddrarg: bad family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 return 0;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001788}
1789
Guido van Rossum30a685f1991-06-27 15:51:29 +00001790
Guido van Rossum48a680c2001-03-02 06:34:14 +00001791/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00001792 Return 1 if the family is known, 0 otherwise. The length is returned
1793 through len_ret. */
1794
1795static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00001796getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 switch (s->sock_family) {
Guido van Rossum710e1df1992-06-12 10:39:36 +00001799
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001800#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 case AF_UNIX:
1802 {
1803 *len_ret = sizeof (struct sockaddr_un);
1804 return 1;
1805 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001806#endif /* AF_UNIX */
Martin v. Löwis11017b12006-01-14 18:12:57 +00001807#if defined(AF_NETLINK)
1808 case AF_NETLINK:
1809 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 *len_ret = sizeof (struct sockaddr_nl);
1811 return 1;
Martin v. Löwis11017b12006-01-14 18:12:57 +00001812 }
1813#endif
Guido van Rossum710e1df1992-06-12 10:39:36 +00001814
Charles-François Natali10b8cf42011-11-10 19:21:37 +01001815#ifdef AF_RDS
1816 case AF_RDS:
1817 /* RDS sockets use sockaddr_in: fall-through */
1818#endif
1819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 case AF_INET:
1821 {
1822 *len_ret = sizeof (struct sockaddr_in);
1823 return 1;
1824 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00001825
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001826#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 case AF_INET6:
1828 {
1829 *len_ret = sizeof (struct sockaddr_in6);
1830 return 1;
1831 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001832#endif
1833
Hye-Shik Chang81268602004-02-02 06:05:24 +00001834#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 case AF_BLUETOOTH:
1836 {
1837 switch(s->sock_proto)
1838 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 case BTPROTO_L2CAP:
1841 *len_ret = sizeof (struct sockaddr_l2);
1842 return 1;
1843 case BTPROTO_RFCOMM:
1844 *len_ret = sizeof (struct sockaddr_rc);
1845 return 1;
1846 case BTPROTO_HCI:
1847 *len_ret = sizeof (struct sockaddr_hci);
1848 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001849#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 case BTPROTO_SCO:
1851 *len_ret = sizeof (struct sockaddr_sco);
1852 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001853#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001855 PyErr_SetString(PyExc_OSError, "getsockaddrlen: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 "unknown BT protocol");
1857 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 }
1860 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001861#endif
1862
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001863#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 case AF_PACKET:
1865 {
1866 *len_ret = sizeof (struct sockaddr_ll);
1867 return 1;
1868 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001869#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001870
Christian Heimes043d6f62008-01-07 17:19:16 +00001871#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 case AF_TIPC:
1873 {
1874 *len_ret = sizeof (struct sockaddr_tipc);
1875 return 1;
1876 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001877#endif
1878
Vinay Sajipecfc98c2014-03-20 12:42:42 +00001879#ifdef AF_CAN
Charles-François Natali47413c12011-10-06 19:47:44 +02001880 case AF_CAN:
1881 {
1882 *len_ret = sizeof (struct sockaddr_can);
1883 return 1;
1884 }
1885#endif
Victor Stinnera534fc42013-06-03 22:07:27 +02001886
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001887#ifdef PF_SYSTEM
1888 case PF_SYSTEM:
1889 switch(s->sock_proto) {
1890#ifdef SYSPROTO_CONTROL
1891 case SYSPROTO_CONTROL:
1892 *len_ret = sizeof (struct sockaddr_ctl);
1893 return 1;
1894#endif
1895 default:
1896 PyErr_SetString(PyExc_OSError, "getsockaddrlen: "
1897 "unknown PF_SYSTEM protocol");
1898 return 0;
1899 }
1900#endif
Charles-François Natali47413c12011-10-06 19:47:44 +02001901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 /* More cases here... */
Guido van Rossum710e1df1992-06-12 10:39:36 +00001903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001905 PyErr_SetString(PyExc_OSError, "getsockaddrlen: bad family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 return 0;
Guido van Rossum710e1df1992-06-12 10:39:36 +00001907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00001909}
1910
1911
Nick Coghlan96fe56a2011-08-22 11:55:57 +10001912/* Support functions for the sendmsg() and recvmsg[_into]() methods.
1913 Currently, these methods are only compiled if the RFC 2292/3542
1914 CMSG_LEN() macro is available. Older systems seem to have used
1915 sizeof(struct cmsghdr) + (length) where CMSG_LEN() is used now, so
1916 it may be possible to define CMSG_LEN() that way if it's not
1917 provided. Some architectures might need extra padding after the
1918 cmsghdr, however, and CMSG_LEN() would have to take account of
1919 this. */
1920#ifdef CMSG_LEN
1921/* If length is in range, set *result to CMSG_LEN(length) and return
1922 true; otherwise, return false. */
1923static int
1924get_CMSG_LEN(size_t length, size_t *result)
1925{
1926 size_t tmp;
1927
1928 if (length > (SOCKLEN_T_LIMIT - CMSG_LEN(0)))
1929 return 0;
1930 tmp = CMSG_LEN(length);
1931 if (tmp > SOCKLEN_T_LIMIT || tmp < length)
1932 return 0;
1933 *result = tmp;
1934 return 1;
1935}
1936
1937#ifdef CMSG_SPACE
1938/* If length is in range, set *result to CMSG_SPACE(length) and return
1939 true; otherwise, return false. */
1940static int
1941get_CMSG_SPACE(size_t length, size_t *result)
1942{
1943 size_t tmp;
1944
1945 /* Use CMSG_SPACE(1) here in order to take account of the padding
1946 necessary before *and* after the data. */
1947 if (length > (SOCKLEN_T_LIMIT - CMSG_SPACE(1)))
1948 return 0;
1949 tmp = CMSG_SPACE(length);
1950 if (tmp > SOCKLEN_T_LIMIT || tmp < length)
1951 return 0;
1952 *result = tmp;
1953 return 1;
1954}
1955#endif
1956
1957/* Return true iff msg->msg_controllen is valid, cmsgh is a valid
1958 pointer in msg->msg_control with at least "space" bytes after it,
1959 and its cmsg_len member inside the buffer. */
1960static int
1961cmsg_min_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t space)
1962{
1963 size_t cmsg_offset;
1964 static const size_t cmsg_len_end = (offsetof(struct cmsghdr, cmsg_len) +
1965 sizeof(cmsgh->cmsg_len));
1966
Charles-François Natali466517d2011-08-28 18:23:43 +02001967 /* Note that POSIX allows msg_controllen to be of signed type. */
Brett Cannonb05cbe62014-01-07 17:01:01 -05001968 if (cmsgh == NULL || msg->msg_control == NULL)
Nick Coghlan96fe56a2011-08-22 11:55:57 +10001969 return 0;
Brett Cannonb05cbe62014-01-07 17:01:01 -05001970 /* Note that POSIX allows msg_controllen to be of a signed type. This is
1971 annoying under OS X as it's unsigned there and so it triggers a
1972 tautological comparison warning under Clang when compared against 0.
1973 Since the check is valid on other platforms, silence the warning under
1974 Clang. */
1975 #ifdef __clang__
1976 #pragma clang diagnostic push
1977 #pragma clang diagnostic ignored "-Wtautological-compare"
1978 #endif
Serhiy Storchakacaa01f82015-03-05 23:42:24 +02001979 #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))
Serhiy Storchakab48af342015-02-26 15:27:57 +02001980 #pragma GCC diagnostic push
1981 #pragma GCC diagnostic ignored "-Wtype-limits"
1982 #endif
Brett Cannonb05cbe62014-01-07 17:01:01 -05001983 if (msg->msg_controllen < 0)
1984 return 0;
Serhiy Storchakacaa01f82015-03-05 23:42:24 +02001985 #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))
Serhiy Storchakab48af342015-02-26 15:27:57 +02001986 #pragma GCC diagnostic pop
1987 #endif
Brett Cannonb05cbe62014-01-07 17:01:01 -05001988 #ifdef __clang__
1989 #pragma clang diagnostic pop
1990 #endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10001991 if (space < cmsg_len_end)
1992 space = cmsg_len_end;
1993 cmsg_offset = (char *)cmsgh - (char *)msg->msg_control;
1994 return (cmsg_offset <= (size_t)-1 - space &&
1995 cmsg_offset + space <= msg->msg_controllen);
1996}
1997
1998/* If pointer CMSG_DATA(cmsgh) is in buffer msg->msg_control, set
1999 *space to number of bytes following it in the buffer and return
2000 true; otherwise, return false. Assumes cmsgh, msg->msg_control and
2001 msg->msg_controllen are valid. */
2002static int
2003get_cmsg_data_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *space)
2004{
2005 size_t data_offset;
2006 char *data_ptr;
2007
2008 if ((data_ptr = (char *)CMSG_DATA(cmsgh)) == NULL)
2009 return 0;
2010 data_offset = data_ptr - (char *)msg->msg_control;
2011 if (data_offset > msg->msg_controllen)
2012 return 0;
2013 *space = msg->msg_controllen - data_offset;
2014 return 1;
2015}
2016
2017/* If cmsgh is invalid or not contained in the buffer pointed to by
2018 msg->msg_control, return -1. If cmsgh is valid and its associated
2019 data is entirely contained in the buffer, set *data_len to the
2020 length of the associated data and return 0. If only part of the
2021 associated data is contained in the buffer but cmsgh is otherwise
2022 valid, set *data_len to the length contained in the buffer and
2023 return 1. */
2024static int
2025get_cmsg_data_len(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *data_len)
2026{
2027 size_t space, cmsg_data_len;
2028
2029 if (!cmsg_min_space(msg, cmsgh, CMSG_LEN(0)) ||
2030 cmsgh->cmsg_len < CMSG_LEN(0))
2031 return -1;
2032 cmsg_data_len = cmsgh->cmsg_len - CMSG_LEN(0);
2033 if (!get_cmsg_data_space(msg, cmsgh, &space))
2034 return -1;
2035 if (space >= cmsg_data_len) {
2036 *data_len = cmsg_data_len;
2037 return 0;
2038 }
2039 *data_len = space;
2040 return 1;
2041}
2042#endif /* CMSG_LEN */
2043
2044
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002045/* s._accept() -> (fd, address) */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002046
Guido van Rossum73624e91994-10-10 17:59:00 +00002047static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002048sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002049{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 sock_addr_t addrbuf;
2051 SOCKET_T newfd = INVALID_SOCKET;
2052 socklen_t addrlen;
2053 PyObject *sock = NULL;
2054 PyObject *addr = NULL;
2055 PyObject *res = NULL;
2056 int timeout;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002057 int async_err = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02002058#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
2059 /* accept4() is available on Linux 2.6.28+ and glibc 2.10 */
2060 static int accept4_works = -1;
2061#endif
2062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 if (!getsockaddrlen(s, &addrlen))
2064 return NULL;
2065 memset(&addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 if (!IS_SELECTABLE(s))
2068 return select_error();
Neal Norwitz082b2df2006-02-07 07:04:46 +00002069
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002070 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002071 do {
Victor Stinner391fa712015-03-31 13:15:31 +02002072 timeout = internal_select(s, 0, interval);
Victor Stinnerb7df3142015-03-27 22:59:32 +01002073
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002074 if (!timeout) {
Victor Stinnerb7df3142015-03-27 22:59:32 +01002075 Py_BEGIN_ALLOW_THREADS
Victor Stinnerdaf45552013-08-28 00:53:59 +02002076#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002077 if (accept4_works != 0) {
2078 newfd = accept4(s->sock_fd, SAS2SA(&addrbuf), &addrlen,
2079 SOCK_CLOEXEC);
2080 if (newfd == INVALID_SOCKET && accept4_works == -1) {
2081 /* On Linux older than 2.6.28, accept4() fails with ENOSYS */
2082 accept4_works = (errno != ENOSYS);
2083 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02002084 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002085 if (accept4_works == 0)
2086 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002087#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002088 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002089#endif
Victor Stinnerb7df3142015-03-27 22:59:32 +01002090 Py_END_ALLOW_THREADS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002091 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002092 } while (newfd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 if (timeout == 1) {
2094 PyErr_SetString(socket_timeout, "timed out");
2095 return NULL;
2096 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002097 END_SELECT_LOOP(s)
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 if (newfd == INVALID_SOCKET)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002100 return (!async_err) ? s->errorhandler() : NULL;
Barry Warsaw752300b1997-01-03 17:18:10 +00002101
Victor Stinnerdaf45552013-08-28 00:53:59 +02002102#ifdef MS_WINDOWS
2103 if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) {
2104 PyErr_SetFromWindowsErr(0);
2105 SOCKETCLOSE(newfd);
2106 goto finally;
2107 }
2108#else
2109
2110#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
2111 if (!accept4_works)
2112#endif
2113 {
2114 if (_Py_set_inheritable(newfd, 0, NULL) < 0) {
2115 SOCKETCLOSE(newfd);
2116 goto finally;
2117 }
2118 }
2119#endif
2120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 sock = PyLong_FromSocket_t(newfd);
2122 if (sock == NULL) {
2123 SOCKETCLOSE(newfd);
2124 goto finally;
2125 }
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
2128 addrlen, s->sock_proto);
2129 if (addr == NULL)
2130 goto finally;
Barry Warsaw752300b1997-01-03 17:18:10 +00002131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 res = PyTuple_Pack(2, sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00002133
Guido van Rossum67f7a382002-06-06 21:08:16 +00002134finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 Py_XDECREF(sock);
2136 Py_XDECREF(addr);
2137 return res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002138}
2139
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002140PyDoc_STRVAR(accept_doc,
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002141"_accept() -> (integer, address info)\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002142\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002143Wait for an incoming connection. Return a new socket file descriptor\n\
2144representing the connection, and the address of the client.\n\
2145For IP sockets, the address info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002146
Guido van Rossum11ba0942002-06-13 15:07:44 +00002147/* s.setblocking(flag) method. Argument:
2148 False -- non-blocking mode; same as settimeout(0)
2149 True -- blocking mode; same as settimeout(None)
2150*/
Guido van Rossume4485b01994-09-07 14:32:49 +00002151
Guido van Rossum73624e91994-10-10 17:59:00 +00002152static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002153sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00002154{
Serhiy Storchaka78980432013-01-15 01:12:17 +02002155 long block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 block = PyLong_AsLong(arg);
2158 if (block == -1 && PyErr_Occurred())
2159 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002160
Victor Stinner71694d52015-03-28 01:18:54 +01002161 s->sock_timeout = block ? -1 : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00002163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 Py_INCREF(Py_None);
2165 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00002166}
Guido van Rossume4485b01994-09-07 14:32:49 +00002167
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002168PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002169"setblocking(flag)\n\
2170\n\
2171Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00002172setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002173setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002174
Victor Stinner71694d52015-03-28 01:18:54 +01002175static int
2176socket_parse_timeout(_PyTime_t *timeout, PyObject *timeout_obj)
2177{
2178#ifdef MS_WINDOWS
2179 struct timeval tv;
2180#endif
2181 int overflow = 0;
2182
2183 if (timeout_obj == Py_None) {
2184 *timeout = -1;
2185 return 0;
2186 }
2187
Victor Stinner869e1772015-03-30 03:49:14 +02002188 if (_PyTime_FromSecondsObject(timeout,
2189 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Victor Stinner71694d52015-03-28 01:18:54 +01002190 return -1;
2191
2192 if (*timeout < 0) {
2193 PyErr_SetString(PyExc_ValueError, "Timeout value out of range");
2194 return -1;
2195 }
2196
2197#ifdef MS_WINDOWS
Victor Stinner869e1772015-03-30 03:49:14 +02002198 overflow = (_PyTime_AsTimeval(timeout, &tv, _PyTime_ROUND_CEILING) < 0);
Victor Stinner71694d52015-03-28 01:18:54 +01002199#endif
2200#ifndef HAVE_POLL
Victor Stinner869e1772015-03-30 03:49:14 +02002201 timeout = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING);
Victor Stinner71694d52015-03-28 01:18:54 +01002202 overflow = (timeout > INT_MAX);
2203#endif
2204 if (overflow) {
2205 PyErr_SetString(PyExc_OverflowError,
2206 "timeout doesn't fit into C timeval");
2207 return -1;
2208 }
2209
2210 return 0;
2211}
2212
Guido van Rossum11ba0942002-06-13 15:07:44 +00002213/* s.settimeout(timeout) method. Argument:
2214 None -- no timeout, blocking mode; same as setblocking(True)
2215 0.0 -- non-blocking mode; same as setblocking(False)
2216 > 0 -- timeout mode; operations time out after timeout seconds
2217 < 0 -- illegal; raises an exception
2218*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00002219static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002220sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00002221{
Victor Stinner71694d52015-03-28 01:18:54 +01002222 _PyTime_t timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002223
Victor Stinner71694d52015-03-28 01:18:54 +01002224 if (socket_parse_timeout(&timeout, arg) < 0)
2225 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 s->sock_timeout = timeout;
Victor Stinner71694d52015-03-28 01:18:54 +01002228 internal_setblocking(s, timeout < 0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 Py_INCREF(Py_None);
2231 return Py_None;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002232}
2233
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002234PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002235"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00002236\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00002237Set a timeout on socket operations. 'timeout' can be a float,\n\
2238giving in seconds, or None. Setting a timeout of None disables\n\
2239the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002240Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00002241
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002242/* s.gettimeout() method.
2243 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00002244static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002245sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00002246{
Victor Stinner71694d52015-03-28 01:18:54 +01002247 if (s->sock_timeout < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 Py_INCREF(Py_None);
2249 return Py_None;
2250 }
Victor Stinner71694d52015-03-28 01:18:54 +01002251 else {
2252 double seconds = _PyTime_AsSecondsDouble(s->sock_timeout);
2253 return PyFloat_FromDouble(seconds);
2254 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002255}
2256
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002257PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002258"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00002259\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03002260Returns the timeout in seconds (float) associated with socket \n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00002261operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002262operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00002263
Guido van Rossumaee08791992-09-08 09:05:33 +00002264/* s.setsockopt() method.
2265 With an integer third argument, sets an integer option.
2266 With a string third argument, sets an option from a buffer;
2267 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002268
Guido van Rossum73624e91994-10-10 17:59:00 +00002269static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002270sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 int level;
2273 int optname;
2274 int res;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02002275 Py_buffer optval;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 if (PyArg_ParseTuple(args, "iii:setsockopt",
2279 &level, &optname, &flag)) {
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02002280 res = setsockopt(s->sock_fd, level, optname, &flag, sizeof flag);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 }
2282 else {
2283 PyErr_Clear();
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02002284 if (!PyArg_ParseTuple(args, "iiy*:setsockopt",
2285 &level, &optname, &optval))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 return NULL;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02002287 res = setsockopt(s->sock_fd, level, optname, optval.buf, optval.len);
2288 PyBuffer_Release(&optval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 if (res < 0)
2291 return s->errorhandler();
2292 Py_INCREF(Py_None);
2293 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002294}
2295
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002296PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002297"setsockopt(level, option, value)\n\
2298\n\
2299Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002300The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002301
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002302
Guido van Rossumaee08791992-09-08 09:05:33 +00002303/* s.getsockopt() method.
2304 With two arguments, retrieves an integer option.
2305 With a third integer argument, retrieves a string buffer of that size;
2306 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002307
Guido van Rossum73624e91994-10-10 17:59:00 +00002308static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002309sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 int level;
2312 int optname;
2313 int res;
2314 PyObject *buf;
2315 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
2318 &level, &optname, &buflen))
2319 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 if (buflen == 0) {
2322 int flag = 0;
2323 socklen_t flagsize = sizeof flag;
2324 res = getsockopt(s->sock_fd, level, optname,
2325 (void *)&flag, &flagsize);
2326 if (res < 0)
2327 return s->errorhandler();
2328 return PyLong_FromLong(flag);
2329 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 if (buflen <= 0 || buflen > 1024) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002331 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 "getsockopt buflen out of range");
2333 return NULL;
2334 }
2335 buf = PyBytes_FromStringAndSize((char *)NULL, buflen);
2336 if (buf == NULL)
2337 return NULL;
2338 res = getsockopt(s->sock_fd, level, optname,
2339 (void *)PyBytes_AS_STRING(buf), &buflen);
2340 if (res < 0) {
2341 Py_DECREF(buf);
2342 return s->errorhandler();
2343 }
2344 _PyBytes_Resize(&buf, buflen);
2345 return buf;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002346}
2347
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002348PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002349"getsockopt(level, option[, buffersize]) -> value\n\
2350\n\
2351Get a socket option. See the Unix manual for level and option.\n\
2352If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002353string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002354
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002355
Fred Drake728819a2000-07-01 03:40:12 +00002356/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002357
Guido van Rossum73624e91994-10-10 17:59:00 +00002358static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002359sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 sock_addr_t addrbuf;
2362 int addrlen;
2363 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2366 return NULL;
2367 Py_BEGIN_ALLOW_THREADS
2368 res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);
2369 Py_END_ALLOW_THREADS
2370 if (res < 0)
2371 return s->errorhandler();
2372 Py_INCREF(Py_None);
2373 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002374}
2375
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002376PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002377"bind(address)\n\
2378\n\
2379Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00002380pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002381sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002382
Guido van Rossum30a685f1991-06-27 15:51:29 +00002383
2384/* s.close() method.
2385 Set the file descriptor to -1 so operations tried subsequently
2386 will surely fail. */
2387
Guido van Rossum73624e91994-10-10 17:59:00 +00002388static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002389sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002392
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002393 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
2394 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
2395 * for more details.
2396 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 if ((fd = s->sock_fd) != -1) {
2398 s->sock_fd = -1;
2399 Py_BEGIN_ALLOW_THREADS
2400 (void) SOCKETCLOSE(fd);
2401 Py_END_ALLOW_THREADS
2402 }
2403 Py_INCREF(Py_None);
2404 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002405}
2406
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002407PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002408"close()\n\
2409\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002410Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002411
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002412static PyObject *
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002413sock_detach(PySocketSockObject *s)
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002414{
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002415 SOCKET_T fd = s->sock_fd;
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002416 s->sock_fd = -1;
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002417 return PyLong_FromSocket_t(fd);
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002418}
2419
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002420PyDoc_STRVAR(detach_doc,
2421"detach()\n\
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002422\n\
Guido van Rossum638aebd2013-12-04 11:50:09 -08002423Close the socket object without closing the underlying file descriptor.\n\
2424The object cannot be used after this call, but the file descriptor\n\
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002425can be reused for other purposes. The file descriptor is returned.");
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002426
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002427static int
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002428internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 int *timeoutp)
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 int res, timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 timeout = 0;
Victor Stinnerb7df3142015-03-27 22:59:32 +01002434
2435 Py_BEGIN_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 res = connect(s->sock_fd, addr, addrlen);
Victor Stinnerb7df3142015-03-27 22:59:32 +01002437 Py_END_ALLOW_THREADS
2438
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002439#ifdef MS_WINDOWS
2440
Victor Stinner71694d52015-03-28 01:18:54 +01002441 if (s->sock_timeout > 0
Victor Stinnerb7df3142015-03-27 22:59:32 +01002442 && res < 0 && WSAGetLastError() == WSAEWOULDBLOCK
2443 && IS_SELECTABLE(s)) {
2444 /* This is a mess. Best solution: trust select */
2445 fd_set fds;
2446 fd_set fds_exc;
2447 struct timeval tv;
Victor Stinner71694d52015-03-28 01:18:54 +01002448 int conv;
2449
Victor Stinner869e1772015-03-30 03:49:14 +02002450 _PyTime_AsTimeval_noraise(s->sock_timeout, &tv, _PyTime_ROUND_CEILING);
Victor Stinnerb7df3142015-03-27 22:59:32 +01002451
2452 Py_BEGIN_ALLOW_THREADS
Victor Stinnerb7df3142015-03-27 22:59:32 +01002453 FD_ZERO(&fds);
2454 FD_SET(s->sock_fd, &fds);
2455 FD_ZERO(&fds_exc);
2456 FD_SET(s->sock_fd, &fds_exc);
2457 res = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
2458 NULL, &fds, &fds_exc, &tv);
2459 Py_END_ALLOW_THREADS
2460
2461 if (res == 0) {
2462 res = WSAEWOULDBLOCK;
2463 timeout = 1;
2464 } else if (res > 0) {
2465 if (FD_ISSET(s->sock_fd, &fds))
2466 /* The socket is in the writable set - this
2467 means connected */
2468 res = 0;
2469 else {
2470 /* As per MS docs, we need to call getsockopt()
2471 to get the underlying error */
2472 int res_size = sizeof res;
2473 /* It must be in the exception set */
2474 assert(FD_ISSET(s->sock_fd, &fds_exc));
2475 if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
2476 (char *)&res, &res_size))
2477 /* getsockopt also clears WSAGetLastError,
2478 so reset it back. */
2479 WSASetLastError(res);
2480 else
2481 res = WSAGetLastError();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 }
Victor Stinnerb7df3142015-03-27 22:59:32 +01002484 /* else if (res < 0) an error occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 if (res < 0)
2488 res = WSAGetLastError();
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002489
2490#else
2491
Victor Stinner71694d52015-03-28 01:18:54 +01002492 if (s->sock_timeout > 0
Victor Stinnerb7df3142015-03-27 22:59:32 +01002493 && res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) {
2494
Victor Stinner391fa712015-03-31 13:15:31 +02002495 timeout = internal_select(s, 1, s->sock_timeout);
Victor Stinnerb7df3142015-03-27 22:59:32 +01002496
2497 if (timeout == 0) {
2498 /* Bug #1019808: in case of an EINPROGRESS,
2499 use getsockopt(SO_ERROR) to get the real
2500 error. */
2501 socklen_t res_size = sizeof res;
2502 (void)getsockopt(s->sock_fd, SOL_SOCKET,
2503 SO_ERROR, &res, &res_size);
2504 if (res == EISCONN)
2505 res = 0;
2506 errno = res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 }
Victor Stinnerb7df3142015-03-27 22:59:32 +01002508 else if (timeout == -1) {
2509 res = errno; /* had error */
2510 }
2511 else
2512 res = EWOULDBLOCK; /* timed out */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 if (res < 0)
2516 res = errno;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002517
2518#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 *timeoutp = timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002520
Victor Stinner71694d52015-03-28 01:18:54 +01002521 assert(res >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 return res;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002523}
Guido van Rossum30a685f1991-06-27 15:51:29 +00002524
Fred Drake728819a2000-07-01 03:40:12 +00002525/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002526
Guido van Rossum73624e91994-10-10 17:59:00 +00002527static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002528sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 sock_addr_t addrbuf;
2531 int addrlen;
2532 int res;
2533 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2536 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 if (timeout == 1) {
2541 PyErr_SetString(socket_timeout, "timed out");
2542 return NULL;
2543 }
Victor Stinner71694d52015-03-28 01:18:54 +01002544 if (res < 0)
2545 return NULL;
2546 if (res != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 return s->errorhandler();
Victor Stinner71694d52015-03-28 01:18:54 +01002548 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 Py_INCREF(Py_None);
2550 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002551}
2552
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002553PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002554"connect(address)\n\
2555\n\
2556Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002557is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002558
Guido van Rossum30a685f1991-06-27 15:51:29 +00002559
Fred Drake728819a2000-07-01 03:40:12 +00002560/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002561
2562static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002563sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002564{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 sock_addr_t addrbuf;
2566 int addrlen;
2567 int res;
2568 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2571 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002573 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002574
Victor Stinner71694d52015-03-28 01:18:54 +01002575 if (res < 0)
2576 return NULL;
2577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 /* Signals are not errors (though they may raise exceptions). Adapted
2579 from PyErr_SetFromErrnoWithFilenameObject(). */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 if (res == EINTR && PyErr_CheckSignals())
2581 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 return PyLong_FromLong((long) res);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002584}
2585
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002586PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002587"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002588\n\
2589This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002590instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002591
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002592
Guido van Rossumed233a51992-06-23 09:07:03 +00002593/* s.fileno() method */
2594
Guido van Rossum73624e91994-10-10 17:59:00 +00002595static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002596sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00002597{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002598 return PyLong_FromSocket_t(s->sock_fd);
Guido van Rossumed233a51992-06-23 09:07:03 +00002599}
2600
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002601PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002602"fileno() -> integer\n\
2603\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002604Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002605
Guido van Rossumed233a51992-06-23 09:07:03 +00002606
Guido van Rossumc89705d1992-11-26 08:54:07 +00002607/* s.getsockname() method */
2608
Guido van Rossum73624e91994-10-10 17:59:00 +00002609static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002610sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612 sock_addr_t addrbuf;
2613 int res;
2614 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 if (!getsockaddrlen(s, &addrlen))
2617 return NULL;
2618 memset(&addrbuf, 0, addrlen);
2619 Py_BEGIN_ALLOW_THREADS
2620 res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2621 Py_END_ALLOW_THREADS
2622 if (res < 0)
2623 return s->errorhandler();
2624 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2625 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002626}
2627
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002628PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002629"getsockname() -> address info\n\
2630\n\
2631Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002632info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002633
Guido van Rossumc89705d1992-11-26 08:54:07 +00002634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002636/* s.getpeername() method */
2637
Guido van Rossum73624e91994-10-10 17:59:00 +00002638static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002639sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002641 sock_addr_t addrbuf;
2642 int res;
2643 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 if (!getsockaddrlen(s, &addrlen))
2646 return NULL;
2647 memset(&addrbuf, 0, addrlen);
2648 Py_BEGIN_ALLOW_THREADS
2649 res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2650 Py_END_ALLOW_THREADS
2651 if (res < 0)
2652 return s->errorhandler();
2653 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2654 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002655}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002656
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002657PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002658"getpeername() -> address info\n\
2659\n\
2660Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002661info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002662
Guido van Rossumb6775db1994-08-01 11:34:53 +00002663#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002664
2665
Guido van Rossum30a685f1991-06-27 15:51:29 +00002666/* s.listen(n) method */
2667
Guido van Rossum73624e91994-10-10 17:59:00 +00002668static PyObject *
Charles-François Natali644b8f52014-05-22 19:45:39 +01002669sock_listen(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002670{
Charles-François Natali644b8f52014-05-22 19:45:39 +01002671 /* We try to choose a default backlog high enough to avoid connection drops
2672 * for common workloads, yet not too high to limit resource usage. */
2673 int backlog = Py_MIN(SOMAXCONN, 128);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002675
Charles-François Natali644b8f52014-05-22 19:45:39 +01002676 if (!PyArg_ParseTuple(args, "|i:listen", &backlog))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 return NULL;
Charles-François Natali644b8f52014-05-22 19:45:39 +01002678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou1be815a2011-05-10 19:16:29 +02002680 /* To avoid problems on systems that don't allow a negative backlog
2681 * (which doesn't make sense anyway) we force a minimum value of 0. */
2682 if (backlog < 0)
2683 backlog = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 res = listen(s->sock_fd, backlog);
2685 Py_END_ALLOW_THREADS
2686 if (res < 0)
2687 return s->errorhandler();
2688 Py_INCREF(Py_None);
2689 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002690}
2691
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002692PyDoc_STRVAR(listen_doc,
Charles-François Natali644b8f52014-05-22 19:45:39 +01002693"listen([backlog])\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002694\n\
Charles-François Natali644b8f52014-05-22 19:45:39 +01002695Enable a server to accept connections. If backlog is specified, it must be\n\
2696at least 0 (if it is lower, it is set to 0); it specifies the number of\n\
Antoine Pitrou1be815a2011-05-10 19:16:29 +02002697unaccepted connections that the system will allow before refusing new\n\
Charles-François Natali644b8f52014-05-22 19:45:39 +01002698connections. If not specified, a default reasonable value is chosen.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002699
2700
Thomas Wouters477c8d52006-05-27 19:21:47 +00002701/*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002702 * This is the guts of the recv() and recv_into() methods, which reads into a
Thomas Wouters902d6eb2007-01-09 23:18:33 +00002703 * char buffer. If you have any inc/dec ref to do to the objects that contain
Thomas Wouters477c8d52006-05-27 19:21:47 +00002704 * the buffer, do it in the caller. This function returns the number of bytes
Ezio Melotti13925002011-03-16 11:05:33 +02002705 * successfully read. If there was an error, it returns -1. Note that it is
Thomas Wouters477c8d52006-05-27 19:21:47 +00002706 * also possible that we return a number of bytes smaller than the request
2707 * bytes.
2708 */
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002709
Antoine Pitrou19467d22010-08-17 19:33:30 +00002710static Py_ssize_t
2711sock_recv_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002712{
Antoine Pitrou19467d22010-08-17 19:33:30 +00002713 Py_ssize_t outlen = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002714 int timeout;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002715 int async_err = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717 if (!IS_SELECTABLE(s)) {
2718 select_error();
2719 return -1;
2720 }
2721 if (len == 0) {
2722 /* If 0 bytes were requested, do nothing. */
2723 return 0;
2724 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002725
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002726 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002727 do {
Victor Stinner391fa712015-03-31 13:15:31 +02002728 timeout = internal_select(s, 0, interval);
Victor Stinnerb7df3142015-03-27 22:59:32 +01002729
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002730 if (!timeout) {
Victor Stinnerb7df3142015-03-27 22:59:32 +01002731 Py_BEGIN_ALLOW_THREADS
Victor Stinner14b9b112013-06-25 00:37:25 +02002732#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002733 if (len > INT_MAX)
2734 len = INT_MAX;
2735 outlen = recv(s->sock_fd, cbuf, (int)len, flags);
Victor Stinner9a644b22013-06-24 23:47:41 +02002736#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002737 outlen = recv(s->sock_fd, cbuf, len, flags);
Victor Stinner9a644b22013-06-24 23:47:41 +02002738#endif
Victor Stinnerb7df3142015-03-27 22:59:32 +01002739 Py_END_ALLOW_THREADS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002740 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002741 } while (outlen < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Thomas Wouters477c8d52006-05-27 19:21:47 +00002742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 if (timeout == 1) {
2744 PyErr_SetString(socket_timeout, "timed out");
2745 return -1;
2746 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002747 END_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002748 if (outlen < 0) {
2749 /* Note: the call to errorhandler() ALWAYS indirectly returned
2750 NULL, so ignore its return value */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002751 if (!async_err)
2752 s->errorhandler();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 return -1;
2754 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 return outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002756}
2757
Guido van Rossum48a680c2001-03-02 06:34:14 +00002758
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002759/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002760
Guido van Rossum73624e91994-10-10 17:59:00 +00002761static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002762sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002763{
Antoine Pitrou19467d22010-08-17 19:33:30 +00002764 Py_ssize_t recvlen, outlen;
2765 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 PyObject *buf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002767
Antoine Pitrou19467d22010-08-17 19:33:30 +00002768 if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 if (recvlen < 0) {
2772 PyErr_SetString(PyExc_ValueError,
2773 "negative buffersize in recv");
2774 return NULL;
2775 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 /* Allocate a new string. */
2778 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2779 if (buf == NULL)
2780 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002782 /* Call the guts */
2783 outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags);
2784 if (outlen < 0) {
2785 /* An error occurred, release the string and return an
2786 error. */
2787 Py_DECREF(buf);
2788 return NULL;
2789 }
2790 if (outlen != recvlen) {
2791 /* We did not read as many bytes as we anticipated, resize the
2792 string if possible and be successful. */
2793 _PyBytes_Resize(&buf, outlen);
2794 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 return buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002797}
2798
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002799PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002800"recv(buffersize[, flags]) -> data\n\
2801\n\
2802Receive up to buffersize bytes from the socket. For the optional flags\n\
2803argument, see the Unix manual. When no data is available, block until\n\
2804at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002805the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002806
Guido van Rossum30a685f1991-06-27 15:51:29 +00002807
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002808/* s.recv_into(buffer, [nbytes [,flags]]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002809
Thomas Wouters477c8d52006-05-27 19:21:47 +00002810static PyObject*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002811sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00002814
Antoine Pitrou19467d22010-08-17 19:33:30 +00002815 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 Py_buffer pbuf;
2817 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002818 Py_ssize_t buflen, readlen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 /* Get the buffer's memory */
Antoine Pitrou19467d22010-08-17 19:33:30 +00002821 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 &pbuf, &recvlen, &flags))
2823 return NULL;
2824 buf = pbuf.buf;
2825 buflen = pbuf.len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 if (recvlen < 0) {
2828 PyBuffer_Release(&pbuf);
2829 PyErr_SetString(PyExc_ValueError,
2830 "negative buffersize in recv_into");
2831 return NULL;
2832 }
2833 if (recvlen == 0) {
2834 /* If nbytes was not specified, use the buffer's length */
2835 recvlen = buflen;
2836 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 /* Check if the buffer is large enough */
2839 if (buflen < recvlen) {
2840 PyBuffer_Release(&pbuf);
2841 PyErr_SetString(PyExc_ValueError,
2842 "buffer too small for requested bytes");
2843 return NULL;
2844 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 /* Call the guts */
2847 readlen = sock_recv_guts(s, buf, recvlen, flags);
2848 if (readlen < 0) {
2849 /* Return an error. */
2850 PyBuffer_Release(&pbuf);
2851 return NULL;
2852 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 PyBuffer_Release(&pbuf);
2855 /* Return the number of bytes read. Note that we do not do anything
2856 special here in the case that readlen < recvlen. */
2857 return PyLong_FromSsize_t(readlen);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002858}
2859
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002860PyDoc_STRVAR(recv_into_doc,
2861"recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002862\n\
2863A version of recv() that stores its data into a buffer rather than creating \n\
2864a new string. Receive up to buffersize bytes from the socket. If buffersize \n\
2865is not specified (or 0), receive up to the size available in the given buffer.\n\
2866\n\
2867See recv() for documentation about the flags.");
2868
2869
2870/*
Christian Heimes99170a52007-12-19 02:07:34 +00002871 * This is the guts of the recvfrom() and recvfrom_into() methods, which reads
2872 * into a char buffer. If you have any inc/def ref to do to the objects that
2873 * contain the buffer, do it in the caller. This function returns the number
Ezio Melotti13925002011-03-16 11:05:33 +02002874 * of bytes successfully read. If there was an error, it returns -1. Note
Christian Heimes99170a52007-12-19 02:07:34 +00002875 * that it is also possible that we return a number of bytes smaller than the
2876 * request bytes.
Thomas Wouters477c8d52006-05-27 19:21:47 +00002877 *
2878 * 'addr' is a return value for the address object. Note that you must decref
2879 * it yourself.
2880 */
Antoine Pitrou19467d22010-08-17 19:33:30 +00002881static Py_ssize_t
2882sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 PyObject** addr)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 sock_addr_t addrbuf;
2886 int timeout;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002887 Py_ssize_t n = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 socklen_t addrlen;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002889 int async_err = 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 *addr = NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 if (!getsockaddrlen(s, &addrlen))
2894 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 if (!IS_SELECTABLE(s)) {
2897 select_error();
2898 return -1;
2899 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002900
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002901 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002902 do {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002903 memset(&addrbuf, 0, addrlen);
Victor Stinner391fa712015-03-31 13:15:31 +02002904 timeout = internal_select(s, 0, interval);
Victor Stinnerb7df3142015-03-27 22:59:32 +01002905
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002906 if (!timeout) {
Victor Stinnerb7df3142015-03-27 22:59:32 +01002907 Py_BEGIN_ALLOW_THREADS
Victor Stinnere990c6e2013-11-16 00:18:58 +01002908#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002909 if (len > INT_MAX)
2910 len = INT_MAX;
2911 n = recvfrom(s->sock_fd, cbuf, (int)len, flags,
2912 (void *) &addrbuf, &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002913#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002914 n = recvfrom(s->sock_fd, cbuf, len, flags,
2915 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002916#endif
Victor Stinnerb7df3142015-03-27 22:59:32 +01002917 Py_END_ALLOW_THREADS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002918 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002919 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 if (timeout == 1) {
2922 PyErr_SetString(socket_timeout, "timed out");
2923 return -1;
2924 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002925 END_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002927 if (!async_err)
2928 s->errorhandler();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 return -1;
2930 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
2933 addrlen, s->sock_proto)))
2934 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 return n;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002937}
2938
2939/* s.recvfrom(nbytes [,flags]) method */
2940
2941static PyObject *
2942sock_recvfrom(PySocketSockObject *s, PyObject *args)
2943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 PyObject *buf = NULL;
2945 PyObject *addr = NULL;
2946 PyObject *ret = NULL;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002947 int flags = 0;
2948 Py_ssize_t recvlen, outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002949
Antoine Pitrou19467d22010-08-17 19:33:30 +00002950 if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 if (recvlen < 0) {
2954 PyErr_SetString(PyExc_ValueError,
2955 "negative buffersize in recvfrom");
2956 return NULL;
2957 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2960 if (buf == NULL)
2961 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf),
2964 recvlen, flags, &addr);
2965 if (outlen < 0) {
2966 goto finally;
2967 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002969 if (outlen != recvlen) {
2970 /* We did not read as many bytes as we anticipated, resize the
Ezio Melotti13925002011-03-16 11:05:33 +02002971 string if possible and be successful. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972 if (_PyBytes_Resize(&buf, outlen) < 0)
Ezio Melotti13925002011-03-16 11:05:33 +02002973 /* Oopsy, not so successful after all. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974 goto finally;
2975 }
Barry Warsaw752300b1997-01-03 17:18:10 +00002976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002978
2979finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 Py_XDECREF(buf);
2981 Py_XDECREF(addr);
2982 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002983}
2984
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002985PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002986"recvfrom(buffersize[, flags]) -> (data, address info)\n\
2987\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002988Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002989
Thomas Wouters477c8d52006-05-27 19:21:47 +00002990
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002991/* s.recvfrom_into(buffer[, nbytes [,flags]]) method */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002992
2993static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002994sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002995{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00002997
Antoine Pitrou19467d22010-08-17 19:33:30 +00002998 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 Py_buffer pbuf;
3000 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003001 Py_ssize_t readlen, buflen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 PyObject *addr = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003004
Antoine Pitrou19467d22010-08-17 19:33:30 +00003005 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 kwlist, &pbuf,
3007 &recvlen, &flags))
3008 return NULL;
3009 buf = pbuf.buf;
3010 buflen = pbuf.len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012 if (recvlen < 0) {
3013 PyBuffer_Release(&pbuf);
3014 PyErr_SetString(PyExc_ValueError,
3015 "negative buffersize in recvfrom_into");
3016 return NULL;
3017 }
3018 if (recvlen == 0) {
3019 /* If nbytes was not specified, use the buffer's length */
3020 recvlen = buflen;
Benjamin Petersonc6b37e22014-01-13 23:14:42 -05003021 } else if (recvlen > buflen) {
3022 PyBuffer_Release(&pbuf);
3023 PyErr_SetString(PyExc_ValueError,
3024 "nbytes is greater than the length of the buffer");
3025 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
3029 if (readlen < 0) {
3030 PyBuffer_Release(&pbuf);
3031 /* Return an error */
3032 Py_XDECREF(addr);
3033 return NULL;
3034 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 PyBuffer_Release(&pbuf);
3037 /* Return the number of bytes read and the address. Note that we do
3038 not do anything special here in the case that readlen < recvlen. */
Antoine Pitrou19467d22010-08-17 19:33:30 +00003039 return Py_BuildValue("nN", readlen, addr);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003040}
3041
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003042PyDoc_STRVAR(recvfrom_into_doc,
3043"recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00003044\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003045Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00003046
3047
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003048/* The sendmsg() and recvmsg[_into]() methods require a working
3049 CMSG_LEN(). See the comment near get_CMSG_LEN(). */
3050#ifdef CMSG_LEN
3051/*
3052 * Call recvmsg() with the supplied iovec structures, flags, and
3053 * ancillary data buffer size (controllen). Returns the tuple return
3054 * value for recvmsg() or recvmsg_into(), with the first item provided
3055 * by the supplied makeval() function. makeval() will be called with
3056 * the length read and makeval_data as arguments, and must return a
3057 * new reference (which will be decrefed if there is a subsequent
3058 * error). On error, closes any file descriptors received via
3059 * SCM_RIGHTS.
3060 */
3061static PyObject *
3062sock_recvmsg_guts(PySocketSockObject *s, struct iovec *iov, int iovlen,
3063 int flags, Py_ssize_t controllen,
3064 PyObject *(*makeval)(ssize_t, void *), void *makeval_data)
3065{
3066 ssize_t bytes_received = -1;
3067 int timeout;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003068 int async_err = 0;
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003069 sock_addr_t addrbuf;
3070 socklen_t addrbuflen;
Charles-François Natalie9e95ae2011-08-24 21:40:53 +02003071 struct msghdr msg = {0};
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003072 PyObject *cmsg_list = NULL, *retval = NULL;
3073 void *controlbuf = NULL;
3074 struct cmsghdr *cmsgh;
3075 size_t cmsgdatalen = 0;
3076 int cmsg_status;
3077
3078 /* XXX: POSIX says that msg_name and msg_namelen "shall be
3079 ignored" when the socket is connected (Linux fills them in
3080 anyway for AF_UNIX sockets at least). Normally msg_namelen
3081 seems to be set to 0 if there's no address, but try to
3082 initialize msg_name to something that won't be mistaken for a
3083 real address if that doesn't happen. */
3084 if (!getsockaddrlen(s, &addrbuflen))
3085 return NULL;
3086 memset(&addrbuf, 0, addrbuflen);
3087 SAS2SA(&addrbuf)->sa_family = AF_UNSPEC;
3088
3089 if (controllen < 0 || controllen > SOCKLEN_T_LIMIT) {
3090 PyErr_SetString(PyExc_ValueError,
3091 "invalid ancillary data buffer length");
3092 return NULL;
3093 }
3094 if (controllen > 0 && (controlbuf = PyMem_Malloc(controllen)) == NULL)
3095 return PyErr_NoMemory();
3096
3097 /* Make the system call. */
3098 if (!IS_SELECTABLE(s)) {
3099 select_error();
3100 goto finally;
3101 }
3102
3103 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003104 do {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003105 msg.msg_name = SAS2SA(&addrbuf);
3106 msg.msg_namelen = addrbuflen;
3107 msg.msg_iov = iov;
3108 msg.msg_iovlen = iovlen;
3109 msg.msg_control = controlbuf;
3110 msg.msg_controllen = controllen;
Victor Stinner391fa712015-03-31 13:15:31 +02003111 timeout = internal_select(s, 0, interval);
Victor Stinnerb7df3142015-03-27 22:59:32 +01003112
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003113 if (timeout == 1) {
3114 PyErr_SetString(socket_timeout, "timed out");
3115 goto finally;
3116 }
Victor Stinnerb7df3142015-03-27 22:59:32 +01003117
3118 if (!timeout) {
3119 Py_BEGIN_ALLOW_THREADS;
3120 bytes_received = recvmsg(s->sock_fd, &msg, flags);
3121 Py_END_ALLOW_THREADS;
3122 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003123 } while (bytes_received < 0 && errno == EINTR &&
3124 !(async_err = PyErr_CheckSignals()));
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003125 END_SELECT_LOOP(s)
3126
3127 if (bytes_received < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003128 if (!async_err)
3129 s->errorhandler();
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003130 goto finally;
3131 }
3132
3133 /* Make list of (level, type, data) tuples from control messages. */
3134 if ((cmsg_list = PyList_New(0)) == NULL)
3135 goto err_closefds;
3136 /* Check for empty ancillary data as old CMSG_FIRSTHDR()
3137 implementations didn't do so. */
3138 for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL);
3139 cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) {
3140 PyObject *bytes, *tuple;
3141 int tmp;
3142
3143 cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen);
3144 if (cmsg_status != 0) {
3145 if (PyErr_WarnEx(PyExc_RuntimeWarning,
3146 "received malformed or improperly-truncated "
3147 "ancillary data", 1) == -1)
3148 goto err_closefds;
3149 }
3150 if (cmsg_status < 0)
3151 break;
3152 if (cmsgdatalen > PY_SSIZE_T_MAX) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003153 PyErr_SetString(PyExc_OSError, "control message too long");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003154 goto err_closefds;
3155 }
3156
3157 bytes = PyBytes_FromStringAndSize((char *)CMSG_DATA(cmsgh),
3158 cmsgdatalen);
3159 tuple = Py_BuildValue("iiN", (int)cmsgh->cmsg_level,
3160 (int)cmsgh->cmsg_type, bytes);
3161 if (tuple == NULL)
3162 goto err_closefds;
3163 tmp = PyList_Append(cmsg_list, tuple);
3164 Py_DECREF(tuple);
3165 if (tmp != 0)
3166 goto err_closefds;
3167
3168 if (cmsg_status != 0)
3169 break;
3170 }
3171
3172 retval = Py_BuildValue("NOiN",
3173 (*makeval)(bytes_received, makeval_data),
3174 cmsg_list,
3175 (int)msg.msg_flags,
3176 makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
3177 ((msg.msg_namelen > addrbuflen) ?
3178 addrbuflen : msg.msg_namelen),
3179 s->sock_proto));
3180 if (retval == NULL)
3181 goto err_closefds;
3182
3183finally:
3184 Py_XDECREF(cmsg_list);
3185 PyMem_Free(controlbuf);
3186 return retval;
3187
3188err_closefds:
3189#ifdef SCM_RIGHTS
3190 /* Close all descriptors coming from SCM_RIGHTS, so they don't leak. */
3191 for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL);
3192 cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) {
3193 cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen);
3194 if (cmsg_status < 0)
3195 break;
3196 if (cmsgh->cmsg_level == SOL_SOCKET &&
3197 cmsgh->cmsg_type == SCM_RIGHTS) {
3198 size_t numfds;
3199 int *fdp;
3200
3201 numfds = cmsgdatalen / sizeof(int);
3202 fdp = (int *)CMSG_DATA(cmsgh);
3203 while (numfds-- > 0)
3204 close(*fdp++);
3205 }
3206 if (cmsg_status != 0)
3207 break;
3208 }
3209#endif /* SCM_RIGHTS */
3210 goto finally;
3211}
3212
3213
3214static PyObject *
3215makeval_recvmsg(ssize_t received, void *data)
3216{
3217 PyObject **buf = data;
3218
3219 if (received < PyBytes_GET_SIZE(*buf))
3220 _PyBytes_Resize(buf, received);
3221 Py_XINCREF(*buf);
3222 return *buf;
3223}
3224
3225/* s.recvmsg(bufsize[, ancbufsize[, flags]]) method */
3226
3227static PyObject *
3228sock_recvmsg(PySocketSockObject *s, PyObject *args)
3229{
3230 Py_ssize_t bufsize, ancbufsize = 0;
3231 int flags = 0;
3232 struct iovec iov;
3233 PyObject *buf = NULL, *retval = NULL;
3234
3235 if (!PyArg_ParseTuple(args, "n|ni:recvmsg", &bufsize, &ancbufsize, &flags))
3236 return NULL;
3237
3238 if (bufsize < 0) {
3239 PyErr_SetString(PyExc_ValueError, "negative buffer size in recvmsg()");
3240 return NULL;
3241 }
3242 if ((buf = PyBytes_FromStringAndSize(NULL, bufsize)) == NULL)
3243 return NULL;
3244 iov.iov_base = PyBytes_AS_STRING(buf);
3245 iov.iov_len = bufsize;
3246
3247 /* Note that we're passing a pointer to *our pointer* to the bytes
3248 object here (&buf); makeval_recvmsg() may incref the object, or
3249 deallocate it and set our pointer to NULL. */
3250 retval = sock_recvmsg_guts(s, &iov, 1, flags, ancbufsize,
3251 &makeval_recvmsg, &buf);
3252 Py_XDECREF(buf);
3253 return retval;
3254}
3255
3256PyDoc_STRVAR(recvmsg_doc,
3257"recvmsg(bufsize[, ancbufsize[, flags]]) -> (data, ancdata, msg_flags, address)\n\
3258\n\
3259Receive normal data (up to bufsize bytes) and ancillary data from the\n\
3260socket. The ancbufsize argument sets the size in bytes of the\n\
3261internal buffer used to receive the ancillary data; it defaults to 0,\n\
3262meaning that no ancillary data will be received. Appropriate buffer\n\
3263sizes for ancillary data can be calculated using CMSG_SPACE() or\n\
3264CMSG_LEN(), and items which do not fit into the buffer might be\n\
3265truncated or discarded. The flags argument defaults to 0 and has the\n\
3266same meaning as for recv().\n\
3267\n\
3268The return value is a 4-tuple: (data, ancdata, msg_flags, address).\n\
3269The data item is a bytes object holding the non-ancillary data\n\
3270received. The ancdata item is a list of zero or more tuples\n\
3271(cmsg_level, cmsg_type, cmsg_data) representing the ancillary data\n\
3272(control messages) received: cmsg_level and cmsg_type are integers\n\
3273specifying the protocol level and protocol-specific type respectively,\n\
3274and cmsg_data is a bytes object holding the associated data. The\n\
3275msg_flags item is the bitwise OR of various flags indicating\n\
3276conditions on the received message; see your system documentation for\n\
3277details. If the receiving socket is unconnected, address is the\n\
3278address of the sending socket, if available; otherwise, its value is\n\
3279unspecified.\n\
3280\n\
3281If recvmsg() raises an exception after the system call returns, it\n\
3282will first attempt to close any file descriptors received via the\n\
3283SCM_RIGHTS mechanism.");
3284
3285
3286static PyObject *
3287makeval_recvmsg_into(ssize_t received, void *data)
3288{
3289 return PyLong_FromSsize_t(received);
3290}
3291
3292/* s.recvmsg_into(buffers[, ancbufsize[, flags]]) method */
3293
3294static PyObject *
3295sock_recvmsg_into(PySocketSockObject *s, PyObject *args)
3296{
3297 Py_ssize_t ancbufsize = 0;
3298 int flags = 0;
3299 struct iovec *iovs = NULL;
3300 Py_ssize_t i, nitems, nbufs = 0;
3301 Py_buffer *bufs = NULL;
3302 PyObject *buffers_arg, *fast, *retval = NULL;
3303
3304 if (!PyArg_ParseTuple(args, "O|ni:recvmsg_into",
3305 &buffers_arg, &ancbufsize, &flags))
3306 return NULL;
3307
3308 if ((fast = PySequence_Fast(buffers_arg,
3309 "recvmsg_into() argument 1 must be an "
3310 "iterable")) == NULL)
3311 return NULL;
3312 nitems = PySequence_Fast_GET_SIZE(fast);
3313 if (nitems > INT_MAX) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003314 PyErr_SetString(PyExc_OSError, "recvmsg_into() argument 1 is too long");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003315 goto finally;
3316 }
3317
3318 /* Fill in an iovec for each item, and save the Py_buffer
3319 structs to release afterwards. */
3320 if (nitems > 0 && ((iovs = PyMem_New(struct iovec, nitems)) == NULL ||
3321 (bufs = PyMem_New(Py_buffer, nitems)) == NULL)) {
3322 PyErr_NoMemory();
3323 goto finally;
3324 }
3325 for (; nbufs < nitems; nbufs++) {
3326 if (!PyArg_Parse(PySequence_Fast_GET_ITEM(fast, nbufs),
3327 "w*;recvmsg_into() argument 1 must be an iterable "
3328 "of single-segment read-write buffers",
3329 &bufs[nbufs]))
3330 goto finally;
3331 iovs[nbufs].iov_base = bufs[nbufs].buf;
3332 iovs[nbufs].iov_len = bufs[nbufs].len;
3333 }
3334
3335 retval = sock_recvmsg_guts(s, iovs, nitems, flags, ancbufsize,
3336 &makeval_recvmsg_into, NULL);
3337finally:
3338 for (i = 0; i < nbufs; i++)
3339 PyBuffer_Release(&bufs[i]);
3340 PyMem_Free(bufs);
3341 PyMem_Free(iovs);
3342 Py_DECREF(fast);
3343 return retval;
3344}
3345
3346PyDoc_STRVAR(recvmsg_into_doc,
3347"recvmsg_into(buffers[, ancbufsize[, flags]]) -> (nbytes, ancdata, msg_flags, address)\n\
3348\n\
3349Receive normal data and ancillary data from the socket, scattering the\n\
3350non-ancillary data into a series of buffers. The buffers argument\n\
3351must be an iterable of objects that export writable buffers\n\
3352(e.g. bytearray objects); these will be filled with successive chunks\n\
3353of the non-ancillary data until it has all been written or there are\n\
3354no more buffers. The ancbufsize argument sets the size in bytes of\n\
3355the internal buffer used to receive the ancillary data; it defaults to\n\
33560, meaning that no ancillary data will be received. Appropriate\n\
3357buffer sizes for ancillary data can be calculated using CMSG_SPACE()\n\
3358or CMSG_LEN(), and items which do not fit into the buffer might be\n\
3359truncated or discarded. The flags argument defaults to 0 and has the\n\
3360same meaning as for recv().\n\
3361\n\
3362The return value is a 4-tuple: (nbytes, ancdata, msg_flags, address).\n\
3363The nbytes item is the total number of bytes of non-ancillary data\n\
3364written into the buffers. The ancdata item is a list of zero or more\n\
3365tuples (cmsg_level, cmsg_type, cmsg_data) representing the ancillary\n\
3366data (control messages) received: cmsg_level and cmsg_type are\n\
3367integers specifying the protocol level and protocol-specific type\n\
3368respectively, and cmsg_data is a bytes object holding the associated\n\
3369data. The msg_flags item is the bitwise OR of various flags\n\
3370indicating conditions on the received message; see your system\n\
3371documentation for details. If the receiving socket is unconnected,\n\
3372address is the address of the sending socket, if available; otherwise,\n\
3373its value is unspecified.\n\
3374\n\
3375If recvmsg_into() raises an exception after the system call returns,\n\
3376it will first attempt to close any file descriptors received via the\n\
3377SCM_RIGHTS mechanism.");
3378#endif /* CMSG_LEN */
3379
3380
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00003381/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00003382
Guido van Rossum73624e91994-10-10 17:59:00 +00003383static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003384sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +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;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003389 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:send", &pbuf, &flags))
3393 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003395 if (!IS_SELECTABLE(s)) {
3396 PyBuffer_Release(&pbuf);
3397 return select_error();
3398 }
3399 buf = pbuf.buf;
3400 len = pbuf.len;
Neal Norwitz082b2df2006-02-07 07:04:46 +00003401
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003402 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003403 do {
Victor Stinner391fa712015-03-31 13:15:31 +02003404 timeout = internal_select(s, 1, interval);
Victor Stinnerb7df3142015-03-27 22:59:32 +01003405
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003406 if (!timeout) {
Victor Stinnerb7df3142015-03-27 22:59:32 +01003407 Py_BEGIN_ALLOW_THREADS
Christian Heimesaf01f662013-12-21 16:19:10 +01003408#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003409 if (len > INT_MAX)
3410 len = INT_MAX;
3411 n = send(s->sock_fd, buf, (int)len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003412#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003413 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003414#endif
Victor Stinnerb7df3142015-03-27 22:59:32 +01003415 Py_END_ALLOW_THREADS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003416 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003417 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 if (timeout == 1) {
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003419 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003420 PyErr_SetString(socket_timeout, "timed out");
3421 return NULL;
3422 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003423 END_SELECT_LOOP(s)
3424
3425 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 if (n < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003427 return (!async_err) ? s->errorhandler() : NULL;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003428 return PyLong_FromSsize_t(n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00003429}
3430
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003431PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003432"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003433\n\
3434Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003435argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003436sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003437
3438
3439/* s.sendall(data [,flags]) method */
3440
3441static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003442sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003445 Py_ssize_t len, n = -1;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003446 int async_err = 0;
3447 int flags = 0, timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 Py_buffer pbuf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags))
3451 return NULL;
3452 buf = pbuf.buf;
3453 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 if (!IS_SELECTABLE(s)) {
3456 PyBuffer_Release(&pbuf);
3457 return select_error();
3458 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00003459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 do {
Victor Stinner391fa712015-03-31 13:15:31 +02003461 timeout = internal_select(s, 1, s->sock_timeout);
Victor Stinnerb7df3142015-03-27 22:59:32 +01003462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 n = -1;
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003464 if (!timeout) {
Victor Stinnerb7df3142015-03-27 22:59:32 +01003465 Py_BEGIN_ALLOW_THREADS
Christian Heimesaf01f662013-12-21 16:19:10 +01003466#ifdef MS_WINDOWS
Victor Stinner9a644b22013-06-24 23:47:41 +02003467 if (len > INT_MAX)
3468 len = INT_MAX;
3469 n = send(s->sock_fd, buf, (int)len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003470#else
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003471 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003472#endif
Victor Stinnerb7df3142015-03-27 22:59:32 +01003473 Py_END_ALLOW_THREADS
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003474 }
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003475 if (timeout == 1) {
3476 PyBuffer_Release(&pbuf);
3477 PyErr_SetString(socket_timeout, "timed out");
3478 return NULL;
3479 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003480 if (n >= 0) {
3481 buf += n;
3482 len -= n;
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003483 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003484 } while (len > 0 && (n >= 0 || errno == EINTR) &&
3485 !(async_err = PyErr_CheckSignals()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 PyBuffer_Release(&pbuf);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003487
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003488 if (n < 0 || async_err)
3489 return (!async_err) ? s->errorhandler() : NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003491 Py_INCREF(Py_None);
3492 return Py_None;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003493}
3494
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003495PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003496"sendall(data[, flags])\n\
3497\n\
3498Send a data string to the socket. For the optional flags\n\
3499argument, see the Unix manual. This calls send() repeatedly\n\
3500until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003501to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003502
Guido van Rossum30a685f1991-06-27 15:51:29 +00003503
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00003504/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00003505
Guido van Rossum73624e91994-10-10 17:59:00 +00003506static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003507sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003509 Py_buffer pbuf;
3510 PyObject *addro;
3511 char *buf;
Antoine Pitrou5e981412011-03-17 22:38:37 +01003512 Py_ssize_t len, arglen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003513 sock_addr_t addrbuf;
3514 int addrlen, n = -1, flags, timeout;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003515 int async_err = 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 flags = 0;
Antoine Pitrou5e981412011-03-17 22:38:37 +01003518 arglen = PyTuple_Size(args);
3519 switch (arglen) {
3520 case 2:
3521 PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro);
3522 break;
3523 case 3:
3524 PyArg_ParseTuple(args, "y*iO:sendto",
3525 &pbuf, &flags, &addro);
3526 break;
3527 default:
3528 PyErr_Format(PyExc_TypeError,
3529 "sendto() takes 2 or 3 arguments (%d given)",
3530 arglen);
Victor Stinner77af1722011-05-26 14:05:59 +02003531 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 }
Antoine Pitrou5e981412011-03-17 22:38:37 +01003533 if (PyErr_Occurred())
3534 return NULL;
3535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 buf = pbuf.buf;
3537 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003539 if (!IS_SELECTABLE(s)) {
3540 PyBuffer_Release(&pbuf);
3541 return select_error();
3542 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00003543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) {
3545 PyBuffer_Release(&pbuf);
3546 return NULL;
3547 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003548
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003549 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003550 do {
Victor Stinner391fa712015-03-31 13:15:31 +02003551 timeout = internal_select(s, 1, interval);
Victor Stinnerb7df3142015-03-27 22:59:32 +01003552
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003553 if (!timeout) {
Victor Stinnerb7df3142015-03-27 22:59:32 +01003554 Py_BEGIN_ALLOW_THREADS
Victor Stinner14b9b112013-06-25 00:37:25 +02003555#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003556 if (len > INT_MAX)
3557 len = INT_MAX;
3558 n = sendto(s->sock_fd, buf, (int)len, flags,
3559 SAS2SA(&addrbuf), addrlen);
Victor Stinner9a644b22013-06-24 23:47:41 +02003560#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003561 n = sendto(s->sock_fd, buf, len, flags,
3562 SAS2SA(&addrbuf), addrlen);
Victor Stinner9a644b22013-06-24 23:47:41 +02003563#endif
Victor Stinnerb7df3142015-03-27 22:59:32 +01003564 Py_END_ALLOW_THREADS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003565 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003566 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Guido van Rossum67f7a382002-06-06 21:08:16 +00003567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 if (timeout == 1) {
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003569 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 PyErr_SetString(socket_timeout, "timed out");
3571 return NULL;
3572 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003573 END_SELECT_LOOP(s)
3574 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 if (n < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003576 return (!async_err) ? s->errorhandler() : NULL;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003577 return PyLong_FromSsize_t(n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003578}
3579
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003580PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003581"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003582\n\
3583Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003584For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003585
Guido van Rossum30a685f1991-06-27 15:51:29 +00003586
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003587/* The sendmsg() and recvmsg[_into]() methods require a working
3588 CMSG_LEN(). See the comment near get_CMSG_LEN(). */
3589#ifdef CMSG_LEN
3590/* s.sendmsg(buffers[, ancdata[, flags[, address]]]) method */
3591
3592static PyObject *
3593sock_sendmsg(PySocketSockObject *s, PyObject *args)
3594{
3595 Py_ssize_t i, ndataparts, ndatabufs = 0, ncmsgs, ncmsgbufs = 0;
3596 Py_buffer *databufs = NULL;
3597 struct iovec *iovs = NULL;
3598 sock_addr_t addrbuf;
Charles-François Natalie9e95ae2011-08-24 21:40:53 +02003599 struct msghdr msg = {0};
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003600 struct cmsginfo {
3601 int level;
3602 int type;
3603 Py_buffer data;
3604 } *cmsgs = NULL;
3605 void *controlbuf = NULL;
3606 size_t controllen, controllen_last;
3607 ssize_t bytes_sent = -1;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003608 int async_err = 0;
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003609 int addrlen, timeout, flags = 0;
3610 PyObject *data_arg, *cmsg_arg = NULL, *addr_arg = NULL, *data_fast = NULL,
3611 *cmsg_fast = NULL, *retval = NULL;
3612
3613 if (!PyArg_ParseTuple(args, "O|OiO:sendmsg",
3614 &data_arg, &cmsg_arg, &flags, &addr_arg))
3615 return NULL;
3616
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003617 /* Parse destination address. */
3618 if (addr_arg != NULL && addr_arg != Py_None) {
3619 if (!getsockaddrarg(s, addr_arg, SAS2SA(&addrbuf), &addrlen))
3620 goto finally;
3621 msg.msg_name = &addrbuf;
3622 msg.msg_namelen = addrlen;
3623 }
3624
3625 /* Fill in an iovec for each message part, and save the Py_buffer
3626 structs to release afterwards. */
3627 if ((data_fast = PySequence_Fast(data_arg,
3628 "sendmsg() argument 1 must be an "
3629 "iterable")) == NULL)
3630 goto finally;
3631 ndataparts = PySequence_Fast_GET_SIZE(data_fast);
3632 if (ndataparts > INT_MAX) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003633 PyErr_SetString(PyExc_OSError, "sendmsg() argument 1 is too long");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003634 goto finally;
3635 }
3636 msg.msg_iovlen = ndataparts;
3637 if (ndataparts > 0 &&
3638 ((msg.msg_iov = iovs = PyMem_New(struct iovec, ndataparts)) == NULL ||
3639 (databufs = PyMem_New(Py_buffer, ndataparts)) == NULL)) {
3640 PyErr_NoMemory();
3641 goto finally;
3642 }
3643 for (; ndatabufs < ndataparts; ndatabufs++) {
3644 if (!PyArg_Parse(PySequence_Fast_GET_ITEM(data_fast, ndatabufs),
3645 "y*;sendmsg() argument 1 must be an iterable of "
Serhiy Storchakab757c832014-12-05 22:25:22 +02003646 "bytes-like objects",
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003647 &databufs[ndatabufs]))
3648 goto finally;
3649 iovs[ndatabufs].iov_base = databufs[ndatabufs].buf;
3650 iovs[ndatabufs].iov_len = databufs[ndatabufs].len;
3651 }
3652
3653 if (cmsg_arg == NULL)
3654 ncmsgs = 0;
3655 else {
3656 if ((cmsg_fast = PySequence_Fast(cmsg_arg,
3657 "sendmsg() argument 2 must be an "
3658 "iterable")) == NULL)
3659 goto finally;
3660 ncmsgs = PySequence_Fast_GET_SIZE(cmsg_fast);
3661 }
3662
3663#ifndef CMSG_SPACE
3664 if (ncmsgs > 1) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003665 PyErr_SetString(PyExc_OSError,
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003666 "sending multiple control messages is not supported "
3667 "on this system");
3668 goto finally;
3669 }
3670#endif
3671 /* Save level, type and Py_buffer for each control message,
3672 and calculate total size. */
3673 if (ncmsgs > 0 && (cmsgs = PyMem_New(struct cmsginfo, ncmsgs)) == NULL) {
3674 PyErr_NoMemory();
3675 goto finally;
3676 }
3677 controllen = controllen_last = 0;
3678 while (ncmsgbufs < ncmsgs) {
3679 size_t bufsize, space;
3680
3681 if (!PyArg_Parse(PySequence_Fast_GET_ITEM(cmsg_fast, ncmsgbufs),
3682 "(iiy*):[sendmsg() ancillary data items]",
3683 &cmsgs[ncmsgbufs].level,
3684 &cmsgs[ncmsgbufs].type,
3685 &cmsgs[ncmsgbufs].data))
3686 goto finally;
3687 bufsize = cmsgs[ncmsgbufs++].data.len;
3688
3689#ifdef CMSG_SPACE
3690 if (!get_CMSG_SPACE(bufsize, &space)) {
3691#else
3692 if (!get_CMSG_LEN(bufsize, &space)) {
3693#endif
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003694 PyErr_SetString(PyExc_OSError, "ancillary data item too large");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003695 goto finally;
3696 }
3697 controllen += space;
3698 if (controllen > SOCKLEN_T_LIMIT || controllen < controllen_last) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003699 PyErr_SetString(PyExc_OSError, "too much ancillary data");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003700 goto finally;
3701 }
3702 controllen_last = controllen;
3703 }
3704
3705 /* Construct ancillary data block from control message info. */
3706 if (ncmsgbufs > 0) {
3707 struct cmsghdr *cmsgh = NULL;
3708
3709 if ((msg.msg_control = controlbuf =
3710 PyMem_Malloc(controllen)) == NULL) {
3711 PyErr_NoMemory();
3712 goto finally;
3713 }
3714 msg.msg_controllen = controllen;
3715
3716 /* Need to zero out the buffer as a workaround for glibc's
3717 CMSG_NXTHDR() implementation. After getting the pointer to
3718 the next header, it checks its (uninitialized) cmsg_len
3719 member to see if the "message" fits in the buffer, and
3720 returns NULL if it doesn't. Zero-filling the buffer
Terry Jan Reedy0f847642013-03-11 18:34:00 -04003721 ensures that this doesn't happen. */
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003722 memset(controlbuf, 0, controllen);
3723
3724 for (i = 0; i < ncmsgbufs; i++) {
3725 size_t msg_len, data_len = cmsgs[i].data.len;
3726 int enough_space = 0;
3727
3728 cmsgh = (i == 0) ? CMSG_FIRSTHDR(&msg) : CMSG_NXTHDR(&msg, cmsgh);
3729 if (cmsgh == NULL) {
3730 PyErr_Format(PyExc_RuntimeError,
3731 "unexpected NULL result from %s()",
3732 (i == 0) ? "CMSG_FIRSTHDR" : "CMSG_NXTHDR");
3733 goto finally;
3734 }
3735 if (!get_CMSG_LEN(data_len, &msg_len)) {
3736 PyErr_SetString(PyExc_RuntimeError,
3737 "item size out of range for CMSG_LEN()");
3738 goto finally;
3739 }
3740 if (cmsg_min_space(&msg, cmsgh, msg_len)) {
3741 size_t space;
3742
3743 cmsgh->cmsg_len = msg_len;
3744 if (get_cmsg_data_space(&msg, cmsgh, &space))
3745 enough_space = (space >= data_len);
3746 }
3747 if (!enough_space) {
3748 PyErr_SetString(PyExc_RuntimeError,
3749 "ancillary data does not fit in calculated "
3750 "space");
3751 goto finally;
3752 }
3753 cmsgh->cmsg_level = cmsgs[i].level;
3754 cmsgh->cmsg_type = cmsgs[i].type;
3755 memcpy(CMSG_DATA(cmsgh), cmsgs[i].data.buf, data_len);
3756 }
3757 }
3758
3759 /* Make the system call. */
3760 if (!IS_SELECTABLE(s)) {
3761 select_error();
3762 goto finally;
3763 }
3764
3765 BEGIN_SELECT_LOOP(s)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003766 do {
Victor Stinner391fa712015-03-31 13:15:31 +02003767 timeout = internal_select(s, 1, interval);
Victor Stinnerb7df3142015-03-27 22:59:32 +01003768
3769 if (!timeout) {
3770 Py_BEGIN_ALLOW_THREADS;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003771 bytes_sent = sendmsg(s->sock_fd, &msg, flags);
Victor Stinnerb7df3142015-03-27 22:59:32 +01003772 Py_END_ALLOW_THREADS;
3773 }
3774
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003775 if (timeout == 1) {
3776 PyErr_SetString(socket_timeout, "timed out");
3777 goto finally;
3778 }
3779 } while (bytes_sent < 0 && errno == EINTR &&
3780 !(async_err = PyErr_CheckSignals()));
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003781 END_SELECT_LOOP(s)
3782
3783 if (bytes_sent < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003784 if (!async_err)
3785 s->errorhandler();
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003786 goto finally;
3787 }
3788 retval = PyLong_FromSsize_t(bytes_sent);
3789
3790finally:
3791 PyMem_Free(controlbuf);
3792 for (i = 0; i < ncmsgbufs; i++)
3793 PyBuffer_Release(&cmsgs[i].data);
3794 PyMem_Free(cmsgs);
3795 Py_XDECREF(cmsg_fast);
3796 for (i = 0; i < ndatabufs; i++)
3797 PyBuffer_Release(&databufs[i]);
3798 PyMem_Free(databufs);
3799 PyMem_Free(iovs);
3800 Py_XDECREF(data_fast);
3801 return retval;
3802}
3803
3804PyDoc_STRVAR(sendmsg_doc,
3805"sendmsg(buffers[, ancdata[, flags[, address]]]) -> count\n\
3806\n\
3807Send normal and ancillary data to the socket, gathering the\n\
3808non-ancillary data from a series of buffers and concatenating it into\n\
3809a single message. The buffers argument specifies the non-ancillary\n\
Serhiy Storchakab757c832014-12-05 22:25:22 +02003810data as an iterable of bytes-like objects (e.g. bytes objects).\n\
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003811The ancdata argument specifies the ancillary data (control messages)\n\
3812as an iterable of zero or more tuples (cmsg_level, cmsg_type,\n\
3813cmsg_data), where cmsg_level and cmsg_type are integers specifying the\n\
3814protocol level and protocol-specific type respectively, and cmsg_data\n\
Serhiy Storchakab757c832014-12-05 22:25:22 +02003815is a bytes-like object holding the associated data. The flags\n\
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003816argument defaults to 0 and has the same meaning as for send(). If\n\
3817address is supplied and not None, it sets a destination address for\n\
3818the message. The return value is the number of bytes of non-ancillary\n\
3819data sent.");
3820#endif /* CMSG_LEN */
3821
3822
Guido van Rossum30a685f1991-06-27 15:51:29 +00003823/* s.shutdown(how) method */
3824
Guido van Rossum73624e91994-10-10 17:59:00 +00003825static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003826sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003827{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003828 int how;
3829 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00003830
Serhiy Storchaka78980432013-01-15 01:12:17 +02003831 how = _PyLong_AsInt(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003832 if (how == -1 && PyErr_Occurred())
3833 return NULL;
3834 Py_BEGIN_ALLOW_THREADS
3835 res = shutdown(s->sock_fd, how);
3836 Py_END_ALLOW_THREADS
3837 if (res < 0)
3838 return s->errorhandler();
3839 Py_INCREF(Py_None);
3840 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003841}
3842
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003843PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003844"shutdown(flag)\n\
3845\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00003846Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
3847of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003848
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00003849#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Christian Heimesfaf2f632008-01-06 16:59:19 +00003850static PyObject*
3851sock_ioctl(PySocketSockObject *s, PyObject *arg)
3852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003853 unsigned long cmd = SIO_RCVALL;
3854 PyObject *argO;
3855 DWORD recv;
Christian Heimesfaf2f632008-01-06 16:59:19 +00003856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003857 if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO))
3858 return NULL;
Christian Heimesfaf2f632008-01-06 16:59:19 +00003859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003860 switch (cmd) {
3861 case SIO_RCVALL: {
3862 unsigned int option = RCVALL_ON;
3863 if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option))
3864 return NULL;
3865 if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option),
3866 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
3867 return set_error();
3868 }
3869 return PyLong_FromUnsignedLong(recv); }
3870 case SIO_KEEPALIVE_VALS: {
3871 struct tcp_keepalive ka;
3872 if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd,
3873 &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval))
3874 return NULL;
3875 if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka),
3876 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
3877 return set_error();
3878 }
3879 return PyLong_FromUnsignedLong(recv); }
3880 default:
3881 PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd);
3882 return NULL;
3883 }
Christian Heimesfaf2f632008-01-06 16:59:19 +00003884}
3885PyDoc_STRVAR(sock_ioctl_doc,
3886"ioctl(cmd, option) -> long\n\
3887\n\
Kristján Valur Jónsson847ec752009-09-27 21:10:38 +00003888Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\
3889SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\
3890SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval).");
Kristján Valur Jónsson10f383a2012-04-07 11:23:31 +00003891#endif
3892
3893#if defined(MS_WINDOWS)
3894static PyObject*
3895sock_share(PySocketSockObject *s, PyObject *arg)
3896{
3897 WSAPROTOCOL_INFO info;
3898 DWORD processId;
3899 int result;
3900
3901 if (!PyArg_ParseTuple(arg, "I", &processId))
3902 return NULL;
3903
3904 Py_BEGIN_ALLOW_THREADS
3905 result = WSADuplicateSocket(s->sock_fd, processId, &info);
3906 Py_END_ALLOW_THREADS
3907 if (result == SOCKET_ERROR)
3908 return set_error();
3909 return PyBytes_FromStringAndSize((const char*)&info, sizeof(info));
3910}
3911PyDoc_STRVAR(sock_share_doc,
3912"share(process_id) -> bytes\n\
3913\n\
3914Share the socket with another process. The target process id\n\
3915must be provided and the resulting bytes object passed to the target\n\
3916process. There the shared socket can be instantiated by calling\n\
3917socket.fromshare().");
3918
Christian Heimesfaf2f632008-01-06 16:59:19 +00003919
3920#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00003921
3922/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003923
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003924static PyMethodDef sock_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003925 {"_accept", (PyCFunction)sock_accept, METH_NOARGS,
3926 accept_doc},
3927 {"bind", (PyCFunction)sock_bind, METH_O,
3928 bind_doc},
3929 {"close", (PyCFunction)sock_close, METH_NOARGS,
3930 close_doc},
3931 {"connect", (PyCFunction)sock_connect, METH_O,
3932 connect_doc},
3933 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
3934 connect_ex_doc},
Antoine Pitrou6e451df2010-08-09 20:39:54 +00003935 {"detach", (PyCFunction)sock_detach, METH_NOARGS,
3936 detach_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003937 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
3938 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003939#ifdef HAVE_GETPEERNAME
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003940 {"getpeername", (PyCFunction)sock_getpeername,
3941 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00003942#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003943 {"getsockname", (PyCFunction)sock_getsockname,
3944 METH_NOARGS, getsockname_doc},
3945 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
3946 getsockopt_doc},
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00003947#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003948 {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS,
3949 sock_ioctl_doc},
Christian Heimesfaf2f632008-01-06 16:59:19 +00003950#endif
Kristján Valur Jónsson10f383a2012-04-07 11:23:31 +00003951#if defined(MS_WINDOWS)
3952 {"share", (PyCFunction)sock_share, METH_VARARGS,
3953 sock_share_doc},
3954#endif
Charles-François Natali644b8f52014-05-22 19:45:39 +01003955 {"listen", (PyCFunction)sock_listen, METH_VARARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003956 listen_doc},
3957 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
3958 recv_doc},
3959 {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
3960 recv_into_doc},
3961 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
3962 recvfrom_doc},
3963 {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
3964 recvfrom_into_doc},
3965 {"send", (PyCFunction)sock_send, METH_VARARGS,
3966 send_doc},
3967 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
3968 sendall_doc},
3969 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
3970 sendto_doc},
3971 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
3972 setblocking_doc},
3973 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
3974 settimeout_doc},
3975 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
3976 gettimeout_doc},
3977 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
3978 setsockopt_doc},
3979 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
3980 shutdown_doc},
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003981#ifdef CMSG_LEN
3982 {"recvmsg", (PyCFunction)sock_recvmsg, METH_VARARGS,
3983 recvmsg_doc},
3984 {"recvmsg_into", (PyCFunction)sock_recvmsg_into, METH_VARARGS,
3985 recvmsg_into_doc,},
3986 {"sendmsg", (PyCFunction)sock_sendmsg, METH_VARARGS,
3987 sendmsg_doc},
3988#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003989 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003990};
3991
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003992/* SockObject members */
3993static PyMemberDef sock_memberlist[] = {
3994 {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},
3995 {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},
3996 {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003997 {0},
3998};
Guido van Rossum30a685f1991-06-27 15:51:29 +00003999
Victor Stinner71694d52015-03-28 01:18:54 +01004000static PyGetSetDef sock_getsetlist[] = {
4001 {"timeout", (getter)sock_gettimeout, NULL, PyDoc_STR("the socket timeout")},
4002 {NULL} /* sentinel */
4003};
4004
Guido van Rossum73624e91994-10-10 17:59:00 +00004005/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00004006 First close the file description. */
4007
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004008static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004009sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004010{
Antoine Pitroue033e062010-10-29 10:38:18 +00004011 if (s->sock_fd != -1) {
4012 PyObject *exc, *val, *tb;
4013 Py_ssize_t old_refcount = Py_REFCNT(s);
4014 ++Py_REFCNT(s);
4015 PyErr_Fetch(&exc, &val, &tb);
4016 if (PyErr_WarnFormat(PyExc_ResourceWarning, 1,
4017 "unclosed %R", s))
4018 /* Spurious errors can appear at shutdown */
4019 if (PyErr_ExceptionMatches(PyExc_Warning))
4020 PyErr_WriteUnraisable((PyObject *) s);
4021 PyErr_Restore(exc, val, tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004022 (void) SOCKETCLOSE(s->sock_fd);
Antoine Pitroue033e062010-10-29 10:38:18 +00004023 Py_REFCNT(s) = old_refcount;
4024 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004025 Py_TYPE(s)->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004026}
4027
Guido van Rossum30a685f1991-06-27 15:51:29 +00004028
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004029static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004030sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004031{
Victor Stinnere254e532014-07-26 14:36:55 +02004032 long sock_fd;
4033 /* On Windows, this test is needed because SOCKET_T is unsigned */
4034 if (s->sock_fd == INVALID_SOCKET) {
4035 sock_fd = -1;
4036 }
Fred Drakea04eaad2000-06-30 02:46:07 +00004037#if SIZEOF_SOCKET_T > SIZEOF_LONG
Victor Stinnere254e532014-07-26 14:36:55 +02004038 else if (s->sock_fd > LONG_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004039 /* this can occur on Win64, and actually there is a special
4040 ugly printf formatter for decimal pointer length integer
4041 printing, only bother if necessary*/
4042 PyErr_SetString(PyExc_OverflowError,
4043 "no printf formatter to display "
4044 "the socket descriptor in decimal");
4045 return NULL;
4046 }
Fred Drakea04eaad2000-06-30 02:46:07 +00004047#endif
Victor Stinnere254e532014-07-26 14:36:55 +02004048 else
4049 sock_fd = (long)s->sock_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004050 return PyUnicode_FromFormat(
4051 "<socket object, fd=%ld, family=%d, type=%d, proto=%d>",
Victor Stinnere254e532014-07-26 14:36:55 +02004052 sock_fd, s->sock_family,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004053 s->sock_type,
4054 s->sock_proto);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004055}
4056
4057
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004058/* Create a new, uninitialized socket object. */
4059
4060static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004061sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 PyObject *new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 new = type->tp_alloc(type, 0);
4066 if (new != NULL) {
4067 ((PySocketSockObject *)new)->sock_fd = -1;
Victor Stinner71694d52015-03-28 01:18:54 +01004068 ((PySocketSockObject *)new)->sock_timeout = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004069 ((PySocketSockObject *)new)->errorhandler = &set_error;
4070 }
4071 return new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004072}
4073
4074
4075/* Initialize a new socket object. */
4076
Victor Stinnerdaf45552013-08-28 00:53:59 +02004077#ifdef SOCK_CLOEXEC
4078/* socket() and socketpair() fail with EINVAL on Linux kernel older
4079 * than 2.6.27 if SOCK_CLOEXEC flag is set in the socket type. */
4080static int sock_cloexec_works = -1;
4081#endif
4082
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004083/*ARGSUSED*/
4084static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00004085sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004086{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004087 PySocketSockObject *s = (PySocketSockObject *)self;
4088 PyObject *fdobj = NULL;
4089 SOCKET_T fd = INVALID_SOCKET;
4090 int family = AF_INET, type = SOCK_STREAM, proto = 0;
4091 static char *keywords[] = {"family", "type", "proto", "fileno", 0};
Victor Stinnerdaf45552013-08-28 00:53:59 +02004092#ifndef MS_WINDOWS
4093#ifdef SOCK_CLOEXEC
4094 int *atomic_flag_works = &sock_cloexec_works;
4095#else
4096 int *atomic_flag_works = NULL;
4097#endif
4098#endif
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004100 if (!PyArg_ParseTupleAndKeywords(args, kwds,
4101 "|iiiO:socket", keywords,
4102 &family, &type, &proto, &fdobj))
4103 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004105 if (fdobj != NULL && fdobj != Py_None) {
Kristján Valur Jónsson10f383a2012-04-07 11:23:31 +00004106#ifdef MS_WINDOWS
4107 /* recreate a socket that was duplicated */
4108 if (PyBytes_Check(fdobj)) {
4109 WSAPROTOCOL_INFO info;
4110 if (PyBytes_GET_SIZE(fdobj) != sizeof(info)) {
4111 PyErr_Format(PyExc_ValueError,
4112 "socket descriptor string has wrong size, "
4113 "should be %zu bytes.", sizeof(info));
4114 return -1;
4115 }
4116 memcpy(&info, PyBytes_AS_STRING(fdobj), sizeof(info));
4117 Py_BEGIN_ALLOW_THREADS
4118 fd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
4119 FROM_PROTOCOL_INFO, &info, 0, WSA_FLAG_OVERLAPPED);
4120 Py_END_ALLOW_THREADS
4121 if (fd == INVALID_SOCKET) {
4122 set_error();
4123 return -1;
4124 }
4125 family = info.iAddressFamily;
4126 type = info.iSocketType;
4127 proto = info.iProtocol;
4128 }
4129 else
4130#endif
4131 {
4132 fd = PyLong_AsSocket_t(fdobj);
4133 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
4134 return -1;
4135 if (fd == INVALID_SOCKET) {
4136 PyErr_SetString(PyExc_ValueError,
4137 "can't use invalid socket value");
4138 return -1;
4139 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004140 }
4141 }
4142 else {
Victor Stinnerdaf45552013-08-28 00:53:59 +02004143#ifdef MS_WINDOWS
4144 /* Windows implementation */
4145#ifndef WSA_FLAG_NO_HANDLE_INHERIT
4146#define WSA_FLAG_NO_HANDLE_INHERIT 0x80
4147#endif
4148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004149 Py_BEGIN_ALLOW_THREADS
Victor Stinnerdaf45552013-08-28 00:53:59 +02004150 if (support_wsa_no_inherit) {
4151 fd = WSASocket(family, type, proto,
4152 NULL, 0,
4153 WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT);
4154 if (fd == INVALID_SOCKET) {
4155 /* Windows 7 or Windows 2008 R2 without SP1 or the hotfix */
4156 support_wsa_no_inherit = 0;
4157 fd = socket(family, type, proto);
4158 }
4159 }
4160 else {
4161 fd = socket(family, type, proto);
4162 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004163 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00004164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 if (fd == INVALID_SOCKET) {
4166 set_error();
4167 return -1;
4168 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02004169
4170 if (!support_wsa_no_inherit) {
4171 if (!SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0)) {
4172 closesocket(fd);
4173 PyErr_SetFromWindowsErr(0);
4174 return -1;
4175 }
4176 }
4177#else
4178 /* UNIX */
4179 Py_BEGIN_ALLOW_THREADS
4180#ifdef SOCK_CLOEXEC
4181 if (sock_cloexec_works != 0) {
4182 fd = socket(family, type | SOCK_CLOEXEC, proto);
4183 if (sock_cloexec_works == -1) {
4184 if (fd >= 0) {
4185 sock_cloexec_works = 1;
4186 }
4187 else if (errno == EINVAL) {
4188 /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */
4189 sock_cloexec_works = 0;
4190 fd = socket(family, type, proto);
4191 }
4192 }
4193 }
4194 else
4195#endif
4196 {
4197 fd = socket(family, type, proto);
4198 }
4199 Py_END_ALLOW_THREADS
4200
4201 if (fd == INVALID_SOCKET) {
4202 set_error();
4203 return -1;
4204 }
4205
4206 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
4207 SOCKETCLOSE(fd);
4208 return -1;
4209 }
4210#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004211 }
4212 init_sockobject(s, fd, family, type, proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00004213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004215
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004216}
4217
4218
Guido van Rossumb6775db1994-08-01 11:34:53 +00004219/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00004220
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004221static PyTypeObject sock_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004222 PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
4223 "_socket.socket", /* tp_name */
4224 sizeof(PySocketSockObject), /* tp_basicsize */
4225 0, /* tp_itemsize */
4226 (destructor)sock_dealloc, /* tp_dealloc */
4227 0, /* tp_print */
4228 0, /* tp_getattr */
4229 0, /* tp_setattr */
4230 0, /* tp_reserved */
4231 (reprfunc)sock_repr, /* tp_repr */
4232 0, /* tp_as_number */
4233 0, /* tp_as_sequence */
4234 0, /* tp_as_mapping */
4235 0, /* tp_hash */
4236 0, /* tp_call */
4237 0, /* tp_str */
4238 PyObject_GenericGetAttr, /* tp_getattro */
4239 0, /* tp_setattro */
4240 0, /* tp_as_buffer */
4241 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4242 sock_doc, /* tp_doc */
4243 0, /* tp_traverse */
4244 0, /* tp_clear */
4245 0, /* tp_richcompare */
4246 0, /* tp_weaklistoffset */
4247 0, /* tp_iter */
4248 0, /* tp_iternext */
4249 sock_methods, /* tp_methods */
4250 sock_memberlist, /* tp_members */
Victor Stinner71694d52015-03-28 01:18:54 +01004251 sock_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004252 0, /* tp_base */
4253 0, /* tp_dict */
4254 0, /* tp_descr_get */
4255 0, /* tp_descr_set */
4256 0, /* tp_dictoffset */
4257 sock_initobj, /* tp_init */
4258 PyType_GenericAlloc, /* tp_alloc */
4259 sock_new, /* tp_new */
4260 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004261};
4262
Guido van Rossum30a685f1991-06-27 15:51:29 +00004263
Guido van Rossum81194471991-07-27 21:42:02 +00004264/* Python interface to gethostname(). */
4265
4266/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00004267static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004268socket_gethostname(PyObject *self, PyObject *unused)
Guido van Rossum81194471991-07-27 21:42:02 +00004269{
Martin v. Löwis72f48422010-10-29 18:20:08 +00004270#ifdef MS_WINDOWS
4271 /* Don't use winsock's gethostname, as this returns the ANSI
4272 version of the hostname, whereas we need a Unicode string.
4273 Otherwise, gethostname apparently also returns the DNS name. */
Hirokazu Yamamoto09fff7a2010-11-05 17:24:13 +00004274 wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1];
Victor Stinner63941882011-09-29 00:42:28 +02004275 DWORD size = Py_ARRAY_LENGTH(buf);
Victor Stinner74168972011-11-17 01:11:36 +01004276 wchar_t *name;
Hirokazu Yamamoto09fff7a2010-11-05 17:24:13 +00004277 PyObject *result;
Victor Stinner74168972011-11-17 01:11:36 +01004278
4279 if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size))
Victor Stinner9d3b93b2011-11-22 02:27:30 +01004280 return PyUnicode_FromWideChar(buf, size);
Victor Stinner74168972011-11-17 01:11:36 +01004281
4282 if (GetLastError() != ERROR_MORE_DATA)
4283 return PyErr_SetFromWindowsErr(0);
4284
4285 if (size == 0)
4286 return PyUnicode_New(0, 0);
4287
4288 /* MSDN says ERROR_MORE_DATA may occur because DNS allows longer
4289 names */
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02004290 name = PyMem_New(wchar_t, size);
4291 if (!name) {
4292 PyErr_NoMemory();
Victor Stinner74168972011-11-17 01:11:36 +01004293 return NULL;
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02004294 }
Victor Stinner74168972011-11-17 01:11:36 +01004295 if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname,
4296 name,
4297 &size))
4298 {
4299 PyMem_Free(name);
4300 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis72f48422010-10-29 18:20:08 +00004301 }
Victor Stinner74168972011-11-17 01:11:36 +01004302
4303 result = PyUnicode_FromWideChar(name, size);
4304 PyMem_Free(name);
4305 return result;
Martin v. Löwis72f48422010-10-29 18:20:08 +00004306#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004307 char buf[1024];
4308 int res;
4309 Py_BEGIN_ALLOW_THREADS
4310 res = gethostname(buf, (int) sizeof buf - 1);
4311 Py_END_ALLOW_THREADS
4312 if (res < 0)
4313 return set_error();
4314 buf[sizeof buf - 1] = '\0';
Victor Stinnera534fc42013-06-03 22:07:27 +02004315 return PyUnicode_DecodeFSDefault(buf);
Martin v. Löwis72f48422010-10-29 18:20:08 +00004316#endif
Guido van Rossum81194471991-07-27 21:42:02 +00004317}
Guido van Rossumff4949e1992-08-05 19:58:53 +00004318
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004319PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004320"gethostname() -> string\n\
4321\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004322Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004323
Antoine Pitrou061cfb52011-02-28 22:25:22 +00004324#ifdef HAVE_SETHOSTNAME
4325PyDoc_STRVAR(sethostname_doc,
4326"sethostname(name)\n\n\
4327Sets the hostname to name.");
4328
4329static PyObject *
4330socket_sethostname(PyObject *self, PyObject *args)
4331{
4332 PyObject *hnobj;
4333 Py_buffer buf;
4334 int res, flag = 0;
4335
Christian Heimesd2774c72013-06-19 02:06:29 +02004336#ifdef _AIX
4337/* issue #18259, not declared in any useful header file */
4338extern int sethostname(const char *, size_t);
4339#endif
4340
Antoine Pitrou061cfb52011-02-28 22:25:22 +00004341 if (!PyArg_ParseTuple(args, "S:sethostname", &hnobj)) {
4342 PyErr_Clear();
4343 if (!PyArg_ParseTuple(args, "O&:sethostname",
4344 PyUnicode_FSConverter, &hnobj))
4345 return NULL;
4346 flag = 1;
4347 }
4348 res = PyObject_GetBuffer(hnobj, &buf, PyBUF_SIMPLE);
4349 if (!res) {
4350 res = sethostname(buf.buf, buf.len);
4351 PyBuffer_Release(&buf);
4352 }
4353 if (flag)
4354 Py_DECREF(hnobj);
4355 if (res)
4356 return set_error();
4357 Py_RETURN_NONE;
4358}
4359#endif
Guido van Rossumff4949e1992-08-05 19:58:53 +00004360
Guido van Rossum30a685f1991-06-27 15:51:29 +00004361/* Python interface to gethostbyname(name). */
4362
4363/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00004364static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004365socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00004366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004367 char *name;
4368 sock_addr_t addrbuf;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004369 PyObject *ret = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004370
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004371 if (!PyArg_ParseTuple(args, "et:gethostbyname", "idna", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372 return NULL;
4373 if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004374 goto finally;
4375 ret = makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
4376finally:
4377 PyMem_Free(name);
4378 return ret;
Guido van Rossum30a685f1991-06-27 15:51:29 +00004379}
4380
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004381PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004382"gethostbyname(host) -> address\n\
4383\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004384Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004385
4386
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004387/* Convenience function common to gethostbyname_ex and gethostbyaddr */
4388
4389static PyObject *
Victor Stinner1a62a682014-08-17 19:33:28 +02004390gethost_common(struct hostent *h, struct sockaddr *addr, size_t alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004392 char **pch;
4393 PyObject *rtn_tuple = (PyObject *)NULL;
4394 PyObject *name_list = (PyObject *)NULL;
4395 PyObject *addr_list = (PyObject *)NULL;
4396 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004398 if (h == NULL) {
4399 /* Let's get real error message to return */
4400 set_herror(h_errno);
4401 return NULL;
4402 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 if (h->h_addrtype != af) {
4405 /* Let's get real error message to return */
Victor Stinner1f33f2b2011-12-17 04:45:09 +01004406 errno = EAFNOSUPPORT;
4407 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 return NULL;
4409 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004411 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00004412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004413 case AF_INET:
4414 if (alen < sizeof(struct sockaddr_in))
4415 return NULL;
4416 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004417
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00004418#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004419 case AF_INET6:
4420 if (alen < sizeof(struct sockaddr_in6))
4421 return NULL;
4422 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004423#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00004424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004427 if ((name_list = PyList_New(0)) == NULL)
4428 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004430 if ((addr_list = PyList_New(0)) == NULL)
4431 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 /* SF #1511317: h_aliases can be NULL */
4434 if (h->h_aliases) {
4435 for (pch = h->h_aliases; *pch != NULL; pch++) {
4436 int status;
4437 tmp = PyUnicode_FromString(*pch);
4438 if (tmp == NULL)
4439 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004441 status = PyList_Append(name_list, tmp);
4442 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00004443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 if (status)
4445 goto err;
4446 }
4447 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004449 for (pch = h->h_addr_list; *pch != NULL; pch++) {
4450 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00004453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 case AF_INET:
4455 {
4456 struct sockaddr_in sin;
4457 memset(&sin, 0, sizeof(sin));
4458 sin.sin_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004459#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 sin.sin_len = sizeof(sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004461#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
4463 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00004464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004465 if (pch == h->h_addr_list && alen >= sizeof(sin))
4466 memcpy((char *) addr, &sin, sizeof(sin));
4467 break;
4468 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004469
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00004470#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 case AF_INET6:
4472 {
4473 struct sockaddr_in6 sin6;
4474 memset(&sin6, 0, sizeof(sin6));
4475 sin6.sin6_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004476#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 sin6.sin6_len = sizeof(sin6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004478#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004479 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
4480 tmp = makeipaddr((struct sockaddr *)&sin6,
4481 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00004482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 if (pch == h->h_addr_list && alen >= sizeof(sin6))
4484 memcpy((char *) addr, &sin6, sizeof(sin6));
4485 break;
4486 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004487#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00004488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 default: /* can't happen */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004490 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 "unsupported address family");
4492 return NULL;
4493 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004495 if (tmp == NULL)
4496 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 status = PyList_Append(addr_list, tmp);
4499 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00004500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 if (status)
4502 goto err;
4503 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00004506
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004507 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508 Py_XDECREF(name_list);
4509 Py_XDECREF(addr_list);
4510 return rtn_tuple;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004511}
4512
4513
4514/* Python interface to gethostbyname_ex(name). */
4515
4516/*ARGSUSED*/
4517static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004518socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004520 char *name;
4521 struct hostent *h;
Charles-François Natali8b759652011-12-23 16:44:51 +01004522 sock_addr_t addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004523 struct sockaddr *sa;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004524 PyObject *ret = NULL;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004525#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004526 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004527#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004528 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004529#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 char buf[16384];
4531 int buf_len = (sizeof buf) - 1;
4532 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004533#endif
Ross Lagerwall8c159762012-03-06 21:36:18 +02004534#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004535 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004536#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004537#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004538
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004539 if (!PyArg_ParseTuple(args, "et:gethostbyname_ex", "idna", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004540 return NULL;
Charles-François Natali8b759652011-12-23 16:44:51 +01004541 if (setipaddr(name, SAS2SA(&addr), sizeof(addr), AF_INET) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004542 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 Py_BEGIN_ALLOW_THREADS
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004544#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004545#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Ross Lagerwall8c159762012-03-06 21:36:18 +02004546 gethostbyname_r(name, &hp_allocated, buf, buf_len,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004547 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004548#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004549 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004550#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 memset((void *) &data, '\0', sizeof(data));
4552 result = gethostbyname_r(name, &hp_allocated, &data);
4553 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004554#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004555#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00004556#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004558#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004560#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004561 Py_END_ALLOW_THREADS
4562 /* Some C libraries would require addr.__ss_family instead of
4563 addr.ss_family.
4564 Therefore, we cast the sockaddr_storage into sockaddr to
4565 access sa_family. */
Charles-François Natali8b759652011-12-23 16:44:51 +01004566 sa = SAS2SA(&addr);
4567 ret = gethost_common(h, SAS2SA(&addr), sizeof(addr),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004568 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00004569#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004570 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00004571#endif
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004572finally:
4573 PyMem_Free(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004574 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004575}
4576
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004577PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004578"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
4579\n\
4580Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004581for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004582
4583
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004584/* Python interface to gethostbyaddr(IP). */
4585
4586/*ARGSUSED*/
4587static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004588socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004589{
Charles-François Natali8b759652011-12-23 16:44:51 +01004590 sock_addr_t addr;
4591 struct sockaddr *sa = SAS2SA(&addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 char *ip_num;
4593 struct hostent *h;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004594 PyObject *ret = NULL;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004595#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004597#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004598 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004599#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 /* glibcs up to 2.10 assume that the buf argument to
4601 gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc
4602 does not ensure. The attribute below instructs the compiler
4603 to maintain this alignment. */
4604 char buf[16384] Py_ALIGNED(8);
4605 int buf_len = (sizeof buf) - 1;
4606 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004607#endif
Ross Lagerwall8c159762012-03-06 21:36:18 +02004608#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004609 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004610#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004611#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004612 char *ap;
4613 int al;
4614 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004615
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004616 if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004617 return NULL;
4618 af = AF_UNSPEC;
4619 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004620 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004621 af = sa->sa_family;
4622 ap = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00004623 /* al = 0; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004624 switch (af) {
4625 case AF_INET:
4626 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
4627 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
4628 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00004629#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004630 case AF_INET6:
4631 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
4632 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
4633 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004634#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004635 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004636 PyErr_SetString(PyExc_OSError, "unsupported address family");
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004637 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004638 }
4639 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004640#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004641#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Ross Lagerwall8c159762012-03-06 21:36:18 +02004642 gethostbyaddr_r(ap, al, af,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004643 &hp_allocated, buf, buf_len,
4644 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004645#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004646 h = gethostbyaddr_r(ap, al, af,
4647 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004648#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004649 memset((void *) &data, '\0', sizeof(data));
4650 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
4651 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004652#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004653#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00004654#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004655 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004656#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004657 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004658#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004659 Py_END_ALLOW_THREADS
Charles-François Natali8b759652011-12-23 16:44:51 +01004660 ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00004661#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004662 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00004663#endif
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004664finally:
4665 PyMem_Free(ip_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004666 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004667}
4668
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004669PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004670"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
4671\n\
4672Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004673for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004674
Guido van Rossum30a685f1991-06-27 15:51:29 +00004675
4676/* Python interface to getservbyname(name).
4677 This only returns the port number, since the other info is already
4678 known or not useful (like the list of aliases). */
4679
4680/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00004681static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004682socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00004683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004684 char *name, *proto=NULL;
4685 struct servent *sp;
4686 if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
4687 return NULL;
4688 Py_BEGIN_ALLOW_THREADS
4689 sp = getservbyname(name, proto);
4690 Py_END_ALLOW_THREADS
4691 if (sp == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004692 PyErr_SetString(PyExc_OSError, "service/proto not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004693 return NULL;
4694 }
4695 return PyLong_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00004696}
4697
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004698PyDoc_STRVAR(getservbyname_doc,
Barry Warsaw11b91a02004-06-28 00:50:43 +00004699"getservbyname(servicename[, protocolname]) -> integer\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00004700\n\
4701Return a port number from a service name and protocol name.\n\
Barry Warsaw11b91a02004-06-28 00:50:43 +00004702The optional protocol name, if given, should be 'tcp' or 'udp',\n\
4703otherwise any protocol will match.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004704
Guido van Rossum30a685f1991-06-27 15:51:29 +00004705
Barry Warsaw11b91a02004-06-28 00:50:43 +00004706/* Python interface to getservbyport(port).
4707 This only returns the service name, since the other info is already
4708 known or not useful (like the list of aliases). */
4709
4710/*ARGSUSED*/
4711static PyObject *
4712socket_getservbyport(PyObject *self, PyObject *args)
4713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 int port;
4715 char *proto=NULL;
4716 struct servent *sp;
4717 if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto))
4718 return NULL;
4719 if (port < 0 || port > 0xffff) {
4720 PyErr_SetString(
4721 PyExc_OverflowError,
4722 "getservbyport: port must be 0-65535.");
4723 return NULL;
4724 }
4725 Py_BEGIN_ALLOW_THREADS
4726 sp = getservbyport(htons((short)port), proto);
4727 Py_END_ALLOW_THREADS
4728 if (sp == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004729 PyErr_SetString(PyExc_OSError, "port/proto not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004730 return NULL;
4731 }
4732 return PyUnicode_FromString(sp->s_name);
Barry Warsaw11b91a02004-06-28 00:50:43 +00004733}
4734
4735PyDoc_STRVAR(getservbyport_doc,
4736"getservbyport(port[, protocolname]) -> string\n\
4737\n\
4738Return the service name from a port number and protocol name.\n\
4739The optional protocol name, if given, should be 'tcp' or 'udp',\n\
4740otherwise any protocol will match.");
4741
Guido van Rossum3901d851996-12-19 16:35:04 +00004742/* Python interface to getprotobyname(name).
4743 This only returns the protocol number, since the other info is
4744 already known or not useful (like the list of aliases). */
4745
4746/*ARGSUSED*/
4747static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004748socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00004749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004750 char *name;
4751 struct protoent *sp;
4752 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
4753 return NULL;
4754 Py_BEGIN_ALLOW_THREADS
4755 sp = getprotobyname(name);
4756 Py_END_ALLOW_THREADS
4757 if (sp == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004758 PyErr_SetString(PyExc_OSError, "protocol not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004759 return NULL;
4760 }
4761 return PyLong_FromLong((long) sp->p_proto);
Guido van Rossum3901d851996-12-19 16:35:04 +00004762}
4763
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004764PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004765"getprotobyname(name) -> integer\n\
4766\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004767Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004768
Guido van Rossum3901d851996-12-19 16:35:04 +00004769
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004770#ifndef NO_DUP
4771/* dup() function for socket fds */
4772
4773static PyObject *
4774socket_dup(PyObject *self, PyObject *fdobj)
4775{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004776 SOCKET_T fd, newfd;
4777 PyObject *newfdobj;
Victor Stinnerdaf45552013-08-28 00:53:59 +02004778#ifdef MS_WINDOWS
4779 WSAPROTOCOL_INFO info;
4780#endif
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004782 fd = PyLong_AsSocket_t(fdobj);
4783 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
4784 return NULL;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004785
Victor Stinnerdaf45552013-08-28 00:53:59 +02004786#ifdef MS_WINDOWS
4787 if (WSADuplicateSocket(fd, GetCurrentProcessId(), &info))
4788 return set_error();
4789
4790 newfd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
4791 FROM_PROTOCOL_INFO,
4792 &info, 0, WSA_FLAG_OVERLAPPED);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004793 if (newfd == INVALID_SOCKET)
4794 return set_error();
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004795
Victor Stinnerdaf45552013-08-28 00:53:59 +02004796 if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) {
4797 closesocket(newfd);
4798 PyErr_SetFromWindowsErr(0);
4799 return NULL;
4800 }
4801#else
4802 /* On UNIX, dup can be used to duplicate the file descriptor of a socket */
4803 newfd = _Py_dup(fd);
4804 if (newfd == INVALID_SOCKET)
Victor Stinnere990c6e2013-11-16 00:18:58 +01004805 return NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02004806#endif
4807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004808 newfdobj = PyLong_FromSocket_t(newfd);
4809 if (newfdobj == NULL)
4810 SOCKETCLOSE(newfd);
4811 return newfdobj;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004812}
4813
4814PyDoc_STRVAR(dup_doc,
4815"dup(integer) -> integer\n\
4816\n\
4817Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\
4818sockets; on some platforms os.dup() won't work for socket file descriptors.");
4819#endif
4820
4821
Dave Cole331708b2004-08-09 04:51:41 +00004822#ifdef HAVE_SOCKETPAIR
4823/* Create a pair of sockets using the socketpair() function.
Dave Cole07fda7e2004-08-23 05:16:23 +00004824 Arguments as for socket() except the default family is AF_UNIX if
Dave Colee8bbfe42004-08-26 00:51:16 +00004825 defined on the platform; otherwise, the default is AF_INET. */
Dave Cole331708b2004-08-09 04:51:41 +00004826
4827/*ARGSUSED*/
4828static PyObject *
4829socket_socketpair(PyObject *self, PyObject *args)
4830{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004831 PySocketSockObject *s0 = NULL, *s1 = NULL;
4832 SOCKET_T sv[2];
4833 int family, type = SOCK_STREAM, proto = 0;
4834 PyObject *res = NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02004835#ifdef SOCK_CLOEXEC
4836 int *atomic_flag_works = &sock_cloexec_works;
4837#else
4838 int *atomic_flag_works = NULL;
4839#endif
4840 int ret;
Dave Cole331708b2004-08-09 04:51:41 +00004841
4842#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004843 family = AF_UNIX;
Dave Cole331708b2004-08-09 04:51:41 +00004844#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004845 family = AF_INET;
Dave Cole331708b2004-08-09 04:51:41 +00004846#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004847 if (!PyArg_ParseTuple(args, "|iii:socketpair",
4848 &family, &type, &proto))
4849 return NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02004850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004851 /* Create a pair of socket fds */
Victor Stinnerdaf45552013-08-28 00:53:59 +02004852 Py_BEGIN_ALLOW_THREADS
4853#ifdef SOCK_CLOEXEC
4854 if (sock_cloexec_works != 0) {
4855 ret = socketpair(family, type | SOCK_CLOEXEC, proto, sv);
4856 if (sock_cloexec_works == -1) {
4857 if (ret >= 0) {
4858 sock_cloexec_works = 1;
4859 }
4860 else if (errno == EINVAL) {
4861 /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */
4862 sock_cloexec_works = 0;
4863 ret = socketpair(family, type, proto, sv);
4864 }
4865 }
4866 }
4867 else
4868#endif
4869 {
4870 ret = socketpair(family, type, proto, sv);
4871 }
4872 Py_END_ALLOW_THREADS
4873
4874 if (ret < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004875 return set_error();
Victor Stinnerdaf45552013-08-28 00:53:59 +02004876
4877 if (_Py_set_inheritable(sv[0], 0, atomic_flag_works) < 0)
4878 goto finally;
4879 if (_Py_set_inheritable(sv[1], 0, atomic_flag_works) < 0)
4880 goto finally;
4881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004882 s0 = new_sockobject(sv[0], family, type, proto);
4883 if (s0 == NULL)
4884 goto finally;
4885 s1 = new_sockobject(sv[1], family, type, proto);
4886 if (s1 == NULL)
4887 goto finally;
4888 res = PyTuple_Pack(2, s0, s1);
Dave Cole331708b2004-08-09 04:51:41 +00004889
4890finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004891 if (res == NULL) {
4892 if (s0 == NULL)
4893 SOCKETCLOSE(sv[0]);
4894 if (s1 == NULL)
4895 SOCKETCLOSE(sv[1]);
4896 }
4897 Py_XDECREF(s0);
4898 Py_XDECREF(s1);
4899 return res;
Dave Cole331708b2004-08-09 04:51:41 +00004900}
4901
4902PyDoc_STRVAR(socketpair_doc,
Victor Stinnerdaf45552013-08-28 00:53:59 +02004903"socketpair([family[, type [, proto]]]) -> (socket object, socket object)\n\
Dave Cole331708b2004-08-09 04:51:41 +00004904\n\
4905Create a pair of socket objects from the sockets returned by the platform\n\
4906socketpair() function.\n\
Dave Cole07fda7e2004-08-23 05:16:23 +00004907The arguments are the same as for socket() except the default family is\n\
Dave Colee8bbfe42004-08-26 00:51:16 +00004908AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
Dave Cole331708b2004-08-09 04:51:41 +00004909
4910#endif /* HAVE_SOCKETPAIR */
4911
4912
Guido van Rossum006bf911996-06-12 04:04:55 +00004913static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004914socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00004915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004916 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00004917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004918 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
4919 return NULL;
4920 }
4921 if (x1 < 0) {
4922 PyErr_SetString(PyExc_OverflowError,
4923 "can't convert negative number to unsigned long");
4924 return NULL;
4925 }
4926 x2 = (unsigned int)ntohs((unsigned short)x1);
4927 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00004928}
4929
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004930PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004931"ntohs(integer) -> integer\n\
4932\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004933Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004934
4935
Guido van Rossum006bf911996-06-12 04:04:55 +00004936static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004937socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00004938{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004939 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00004940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004941 if (PyLong_Check(arg)) {
4942 x = PyLong_AsUnsignedLong(arg);
4943 if (x == (unsigned long) -1 && PyErr_Occurred())
4944 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004945#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004946 {
4947 unsigned long y;
4948 /* only want the trailing 32 bits */
4949 y = x & 0xFFFFFFFFUL;
4950 if (y ^ x)
4951 return PyErr_Format(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03004952 "int larger than 32 bits");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004953 x = y;
4954 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004955#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004956 }
4957 else
4958 return PyErr_Format(PyExc_TypeError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03004959 "expected int, %s found",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004960 Py_TYPE(arg)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004961 return PyLong_FromUnsignedLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00004962}
4963
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004964PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004965"ntohl(integer) -> integer\n\
4966\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004967Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004968
4969
Guido van Rossum006bf911996-06-12 04:04:55 +00004970static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004971socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00004972{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004973 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00004974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004975 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
4976 return NULL;
4977 }
4978 if (x1 < 0) {
4979 PyErr_SetString(PyExc_OverflowError,
4980 "can't convert negative number to unsigned long");
4981 return NULL;
4982 }
4983 x2 = (unsigned int)htons((unsigned short)x1);
4984 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00004985}
4986
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004987PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004988"htons(integer) -> integer\n\
4989\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004990Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004991
4992
Guido van Rossum006bf911996-06-12 04:04:55 +00004993static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004994socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00004995{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004996 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00004997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004998 if (PyLong_Check(arg)) {
4999 x = PyLong_AsUnsignedLong(arg);
5000 if (x == (unsigned long) -1 && PyErr_Occurred())
5001 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00005002#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005003 {
5004 unsigned long y;
5005 /* only want the trailing 32 bits */
5006 y = x & 0xFFFFFFFFUL;
5007 if (y ^ x)
5008 return PyErr_Format(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03005009 "int larger than 32 bits");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005010 x = y;
5011 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00005012#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005013 }
5014 else
5015 return PyErr_Format(PyExc_TypeError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03005016 "expected int, %s found",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005017 Py_TYPE(arg)->tp_name);
5018 return PyLong_FromUnsignedLong(htonl((unsigned long)x));
Guido van Rossum006bf911996-06-12 04:04:55 +00005019}
5020
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005021PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00005022"htonl(integer) -> integer\n\
5023\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005024Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00005025
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005026/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005027
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005028PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum7d0a8262007-05-21 23:13:11 +00005029"inet_aton(string) -> bytes giving packed 32-bit IP representation\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005030\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00005031Convert 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 +00005032binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005033
5034static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005035socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005036{
Neal Norwitz88f115b2003-02-13 02:15:42 +00005037#ifdef HAVE_INET_ATON
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005038 struct in_addr buf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00005039#endif
5040
5041#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
Benjamin Petersonf91df042009-02-13 02:50:59 +00005042#if (SIZEOF_INT != 4)
5043#error "Not sure if in_addr_t exists and int is not 32-bits."
5044#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005045 /* Have to use inet_addr() instead */
5046 unsigned int packed_addr;
Tim Peters1df9fdd2003-02-13 03:13:40 +00005047#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005048 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00005049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005050 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
5051 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00005052
Tim Peters1df9fdd2003-02-13 03:13:40 +00005053
5054#ifdef HAVE_INET_ATON
Thomas Wouters477c8d52006-05-27 19:21:47 +00005055
5056#ifdef USE_INET_ATON_WEAKLINK
5057 if (inet_aton != NULL) {
5058#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005059 if (inet_aton(ip_addr, &buf))
5060 return PyBytes_FromStringAndSize((char *)(&buf),
5061 sizeof(buf));
Guido van Rossumad05cdf2003-02-12 23:08:22 +00005062
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005063 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005064 "illegal IP address string passed to inet_aton");
5065 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00005066
Thomas Wouters477c8d52006-05-27 19:21:47 +00005067#ifdef USE_INET_ATON_WEAKLINK
5068 } else {
5069#endif
5070
5071#endif
5072
5073#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
5074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005075 /* special-case this address as inet_addr might return INADDR_NONE
5076 * for this */
5077 if (strcmp(ip_addr, "255.255.255.255") == 0) {
Charles-François Natali24aa0412013-08-31 14:48:25 +02005078 packed_addr = INADDR_BROADCAST;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005079 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00005080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005081 packed_addr = inet_addr(ip_addr);
Guido van Rossumc6a164b1999-08-20 19:11:27 +00005082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005083 if (packed_addr == INADDR_NONE) { /* invalid address */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005084 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005085 "illegal IP address string passed to inet_aton");
5086 return NULL;
5087 }
5088 }
5089 return PyBytes_FromStringAndSize((char *) &packed_addr,
5090 sizeof(packed_addr));
Thomas Wouters477c8d52006-05-27 19:21:47 +00005091
5092#ifdef USE_INET_ATON_WEAKLINK
5093 }
5094#endif
5095
Guido van Rossumad05cdf2003-02-12 23:08:22 +00005096#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005097}
5098
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005099PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00005100"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005101\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005102Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005103
5104static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005105socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005106{
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005107 Py_buffer packed_ip;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005108 struct in_addr packed_addr;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005109
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005110 if (!PyArg_ParseTuple(args, "y*:inet_ntoa", &packed_ip)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005111 return NULL;
5112 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00005113
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005114 if (packed_ip.len != sizeof(packed_addr)) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005115 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005116 "packed IP wrong length for inet_ntoa");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005117 PyBuffer_Release(&packed_ip);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005118 return NULL;
5119 }
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005120
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005121 memcpy(&packed_addr, packed_ip.buf, packed_ip.len);
5122 PyBuffer_Release(&packed_ip);
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005124 return PyUnicode_FromString(inet_ntoa(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00005125}
Guido van Rossum82a5c661998-07-07 20:45:43 +00005126
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005127#if defined(HAVE_INET_PTON) || defined(MS_WINDOWS)
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005128
5129PyDoc_STRVAR(inet_pton_doc,
5130"inet_pton(af, ip) -> packed IP address string\n\
5131\n\
5132Convert an IP address from string format to a packed string suitable\n\
5133for use with low-level network functions.");
5134
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005135#endif
5136
5137#ifdef HAVE_INET_PTON
5138
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005139static PyObject *
5140socket_inet_pton(PyObject *self, PyObject *args)
5141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005142 int af;
5143 char* ip;
5144 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005145#ifdef ENABLE_IPV6
Victor Stinner640c35c2013-06-04 23:14:37 +02005146 char packed[Py_MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005147#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005148 char packed[sizeof(struct in_addr)];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005149#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005150 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
5151 return NULL;
5152 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005153
Martin v. Löwis04697e82004-06-02 12:35:29 +00005154#if !defined(ENABLE_IPV6) && defined(AF_INET6)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005155 if(af == AF_INET6) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005156 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005157 "can't use AF_INET6, IPv6 is disabled");
5158 return NULL;
5159 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00005160#endif
Martin v. Löwis10649092003-08-05 06:25:06 +00005161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005162 retval = inet_pton(af, ip, packed);
5163 if (retval < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005164 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005165 return NULL;
5166 } else if (retval == 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005167 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005168 "illegal IP address string passed to inet_pton");
5169 return NULL;
5170 } else if (af == AF_INET) {
5171 return PyBytes_FromStringAndSize(packed,
5172 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005173#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005174 } else if (af == AF_INET6) {
5175 return PyBytes_FromStringAndSize(packed,
5176 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005177#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005178 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005179 PyErr_SetString(PyExc_OSError, "unknown address family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005180 return NULL;
5181 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005182}
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005183#elif defined(MS_WINDOWS)
5184
5185static PyObject *
5186socket_inet_pton(PyObject *self, PyObject *args)
5187{
5188 int af;
5189 char* ip;
5190 struct sockaddr_in6 addr;
5191 INT ret, size;
5192
5193 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
5194 return NULL;
5195 }
5196
Victor Stinnere990c6e2013-11-16 00:18:58 +01005197 size = sizeof(addr);
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005198 ret = WSAStringToAddressA(ip, af, NULL, (LPSOCKADDR)&addr, &size);
5199
5200 if (ret) {
5201 PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
5202 return NULL;
5203 } else if(af == AF_INET) {
5204 struct sockaddr_in *addr4 = (struct sockaddr_in*)&addr;
Victor Stinnere990c6e2013-11-16 00:18:58 +01005205 return PyBytes_FromStringAndSize((const char *)&(addr4->sin_addr),
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005206 sizeof(addr4->sin_addr));
5207 } else if (af == AF_INET6) {
Victor Stinnere990c6e2013-11-16 00:18:58 +01005208 return PyBytes_FromStringAndSize((const char *)&(addr.sin6_addr),
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005209 sizeof(addr.sin6_addr));
5210 } else {
5211 PyErr_SetString(PyExc_OSError, "unknown address family");
5212 return NULL;
5213 }
5214}
5215
5216#endif
5217
5218#if defined(HAVE_INET_PTON) || defined(MS_WINDOWS)
Thomas Wouters477c8d52006-05-27 19:21:47 +00005219
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005220PyDoc_STRVAR(inet_ntop_doc,
5221"inet_ntop(af, packed_ip) -> string formatted IP address\n\
5222\n\
5223Convert a packed IP address of the given family to string format.");
5224
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005225#endif
5226
5227
5228#ifdef HAVE_INET_PTON
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005229static PyObject *
5230socket_inet_ntop(PyObject *self, PyObject *args)
5231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005232 int af;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005233 Py_buffer packed_ip;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005234 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005235#ifdef ENABLE_IPV6
Victor Stinner640c35c2013-06-04 23:14:37 +02005236 char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005237#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005238 char ip[INET_ADDRSTRLEN + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005239#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00005240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005241 /* Guarantee NUL-termination for PyUnicode_FromString() below */
5242 memset((void *) &ip[0], '\0', sizeof(ip));
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005243
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005244 if (!PyArg_ParseTuple(args, "iy*:inet_ntop", &af, &packed_ip)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005245 return NULL;
5246 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005248 if (af == AF_INET) {
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005249 if (packed_ip.len != sizeof(struct in_addr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005250 PyErr_SetString(PyExc_ValueError,
5251 "invalid length of packed IP address string");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005252 PyBuffer_Release(&packed_ip);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005253 return NULL;
5254 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005255#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005256 } else if (af == AF_INET6) {
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005257 if (packed_ip.len != sizeof(struct in6_addr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005258 PyErr_SetString(PyExc_ValueError,
5259 "invalid length of packed IP address string");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005260 PyBuffer_Release(&packed_ip);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005261 return NULL;
5262 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005263#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005264 } else {
5265 PyErr_Format(PyExc_ValueError,
5266 "unknown address family %d", af);
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005267 PyBuffer_Release(&packed_ip);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005268 return NULL;
5269 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005270
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005271 retval = inet_ntop(af, packed_ip.buf, ip, sizeof(ip));
5272 PyBuffer_Release(&packed_ip);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005273 if (!retval) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005274 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005275 return NULL;
5276 } else {
5277 return PyUnicode_FromString(retval);
5278 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005280 /* NOTREACHED */
5281 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
5282 return NULL;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005283}
5284
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005285#elif defined(MS_WINDOWS)
5286
5287static PyObject *
5288socket_inet_ntop(PyObject *self, PyObject *args)
5289{
5290 int af;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005291 Py_buffer packed_ip;
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005292 struct sockaddr_in6 addr;
5293 DWORD addrlen, ret, retlen;
Jason R. Coombs8ec784c2013-11-10 13:43:22 -05005294#ifdef ENABLE_IPV6
5295 char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
5296#else
5297 char ip[INET_ADDRSTRLEN + 1];
5298#endif
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005299
5300 /* Guarantee NUL-termination for PyUnicode_FromString() below */
5301 memset((void *) &ip[0], '\0', sizeof(ip));
5302
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005303 if (!PyArg_ParseTuple(args, "iy*:inet_ntop", &af, &packed_ip)) {
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005304 return NULL;
5305 }
5306
5307 if (af == AF_INET) {
5308 struct sockaddr_in * addr4 = (struct sockaddr_in *)&addr;
5309
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005310 if (packed_ip.len != sizeof(struct in_addr)) {
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005311 PyErr_SetString(PyExc_ValueError,
5312 "invalid length of packed IP address string");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005313 PyBuffer_Release(&packed_ip);
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005314 return NULL;
5315 }
5316 memset(addr4, 0, sizeof(struct sockaddr_in));
5317 addr4->sin_family = AF_INET;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005318 memcpy(&(addr4->sin_addr), packed_ip.buf, sizeof(addr4->sin_addr));
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005319 addrlen = sizeof(struct sockaddr_in);
5320 } else if (af == AF_INET6) {
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005321 if (packed_ip.len != sizeof(struct in6_addr)) {
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005322 PyErr_SetString(PyExc_ValueError,
5323 "invalid length of packed IP address string");
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005324 PyBuffer_Release(&packed_ip);
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005325 return NULL;
5326 }
5327
5328 memset(&addr, 0, sizeof(addr));
5329 addr.sin6_family = AF_INET6;
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005330 memcpy(&(addr.sin6_addr), packed_ip.buf, sizeof(addr.sin6_addr));
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005331 addrlen = sizeof(addr);
5332 } else {
5333 PyErr_Format(PyExc_ValueError,
5334 "unknown address family %d", af);
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005335 PyBuffer_Release(&packed_ip);
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005336 return NULL;
5337 }
Serhiy Storchaka8490f5a2015-03-20 09:00:36 +02005338 PyBuffer_Release(&packed_ip);
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005339
5340 retlen = sizeof(ip);
Victor Stinnere990c6e2013-11-16 00:18:58 +01005341 ret = WSAAddressToStringA((struct sockaddr*)&addr, addrlen, NULL,
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005342 ip, &retlen);
5343
5344 if (ret) {
5345 PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
5346 return NULL;
5347 } else {
5348 return PyUnicode_FromString(ip);
5349 }
5350}
5351
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005352#endif /* HAVE_INET_PTON */
5353
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005354/* Python interface to getaddrinfo(host, port). */
5355
5356/*ARGSUSED*/
5357static PyObject *
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005358socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005359{
Victor Stinner77af1722011-05-26 14:05:59 +02005360 static char* kwnames[] = {"host", "port", "family", "type", "proto",
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005361 "flags", 0};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005362 struct addrinfo hints, *res;
5363 struct addrinfo *res0 = NULL;
5364 PyObject *hobj = NULL;
5365 PyObject *pobj = (PyObject *)NULL;
5366 char pbuf[30];
5367 char *hptr, *pptr;
5368 int family, socktype, protocol, flags;
5369 int error;
5370 PyObject *all = (PyObject *)NULL;
5371 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005372
Georg Brandl6083a4b2013-10-14 06:51:46 +02005373 socktype = protocol = flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005374 family = AF_UNSPEC;
Victor Stinner77af1722011-05-26 14:05:59 +02005375 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iiii:getaddrinfo",
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005376 kwnames, &hobj, &pobj, &family, &socktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005377 &protocol, &flags)) {
5378 return NULL;
5379 }
5380 if (hobj == Py_None) {
5381 hptr = NULL;
5382 } else if (PyUnicode_Check(hobj)) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005383 _Py_IDENTIFIER(encode);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005384
5385 idna = _PyObject_CallMethodId(hobj, &PyId_encode, "s", "idna");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005386 if (!idna)
5387 return NULL;
5388 assert(PyBytes_Check(idna));
5389 hptr = PyBytes_AS_STRING(idna);
5390 } else if (PyBytes_Check(hobj)) {
5391 hptr = PyBytes_AsString(hobj);
5392 } else {
5393 PyErr_SetString(PyExc_TypeError,
5394 "getaddrinfo() argument 1 must be string or None");
5395 return NULL;
5396 }
5397 if (PyLong_CheckExact(pobj)) {
5398 long value = PyLong_AsLong(pobj);
5399 if (value == -1 && PyErr_Occurred())
5400 goto err;
5401 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value);
5402 pptr = pbuf;
5403 } else if (PyUnicode_Check(pobj)) {
5404 pptr = _PyUnicode_AsString(pobj);
Alexander Belopolskye239d232010-12-08 23:31:48 +00005405 if (pptr == NULL)
5406 goto err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005407 } else if (PyBytes_Check(pobj)) {
Alexander Belopolskye239d232010-12-08 23:31:48 +00005408 pptr = PyBytes_AS_STRING(pobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005409 } else if (pobj == Py_None) {
5410 pptr = (char *)NULL;
5411 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005412 PyErr_SetString(PyExc_OSError, "Int or String expected");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005413 goto err;
5414 }
Ronald Oussorena822d3662013-06-10 10:36:28 +02005415#if defined(__APPLE__) && defined(AI_NUMERICSERV)
Ronald Oussoren27a4ac52013-05-24 13:47:37 +02005416 if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) {
5417 /* On OSX upto at least OSX 10.8 getaddrinfo crashes
Christian Heimesaf01f662013-12-21 16:19:10 +01005418 * if AI_NUMERICSERV is set and the servname is NULL or "0".
5419 * This workaround avoids a segfault in libsystem.
5420 */
Ronald Oussoren27a4ac52013-05-24 13:47:37 +02005421 pptr = "00";
5422 }
5423#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005424 memset(&hints, 0, sizeof(hints));
5425 hints.ai_family = family;
5426 hints.ai_socktype = socktype;
5427 hints.ai_protocol = protocol;
5428 hints.ai_flags = flags;
5429 Py_BEGIN_ALLOW_THREADS
5430 ACQUIRE_GETADDRINFO_LOCK
5431 error = getaddrinfo(hptr, pptr, &hints, &res0);
5432 Py_END_ALLOW_THREADS
5433 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
5434 if (error) {
5435 set_gaierror(error);
5436 goto err;
5437 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005439 if ((all = PyList_New(0)) == NULL)
5440 goto err;
5441 for (res = res0; res; res = res->ai_next) {
5442 PyObject *single;
5443 PyObject *addr =
5444 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
5445 if (addr == NULL)
5446 goto err;
5447 single = Py_BuildValue("iiisO", res->ai_family,
5448 res->ai_socktype, res->ai_protocol,
5449 res->ai_canonname ? res->ai_canonname : "",
5450 addr);
5451 Py_DECREF(addr);
5452 if (single == NULL)
5453 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005455 if (PyList_Append(all, single))
5456 goto err;
5457 Py_XDECREF(single);
5458 }
5459 Py_XDECREF(idna);
5460 if (res0)
5461 freeaddrinfo(res0);
5462 return all;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005463 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005464 Py_XDECREF(all);
5465 Py_XDECREF(idna);
5466 if (res0)
5467 freeaddrinfo(res0);
5468 return (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005469}
5470
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005471PyDoc_STRVAR(getaddrinfo_doc,
Berker Peksaga6ec5ee2014-06-30 11:28:40 +03005472"getaddrinfo(host, port [, family, type, proto, flags])\n\
5473 -> list of (family, type, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005474\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005475Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005476
5477/* Python interface to getnameinfo(sa, flags). */
5478
5479/*ARGSUSED*/
5480static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005481socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005483 PyObject *sa = (PyObject *)NULL;
5484 int flags;
5485 char *hostp;
Charles-François Natali366999a2012-01-02 15:47:29 +01005486 int port;
5487 unsigned int flowinfo, scope_id;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005488 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
5489 struct addrinfo hints, *res = NULL;
5490 int error;
5491 PyObject *ret = (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005493 flags = flowinfo = scope_id = 0;
5494 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
5495 return NULL;
5496 if (!PyTuple_Check(sa)) {
5497 PyErr_SetString(PyExc_TypeError,
5498 "getnameinfo() argument 1 must be a tuple");
5499 return NULL;
5500 }
Charles-François Natali366999a2012-01-02 15:47:29 +01005501 if (!PyArg_ParseTuple(sa, "si|II",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005502 &hostp, &port, &flowinfo, &scope_id))
5503 return NULL;
Charles-François Natali9b0c0062012-06-23 10:17:05 +02005504 if (flowinfo > 0xfffff) {
Charles-François Natali366999a2012-01-02 15:47:29 +01005505 PyErr_SetString(PyExc_OverflowError,
5506 "getsockaddrarg: flowinfo must be 0-1048575.");
5507 return NULL;
5508 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005509 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
5510 memset(&hints, 0, sizeof(hints));
5511 hints.ai_family = AF_UNSPEC;
5512 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Martin v. Löwis112c0f32010-08-25 07:38:15 +00005513 hints.ai_flags = AI_NUMERICHOST; /* don't do any name resolution */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005514 Py_BEGIN_ALLOW_THREADS
5515 ACQUIRE_GETADDRINFO_LOCK
5516 error = getaddrinfo(hostp, pbuf, &hints, &res);
5517 Py_END_ALLOW_THREADS
5518 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
5519 if (error) {
5520 set_gaierror(error);
5521 goto fail;
5522 }
5523 if (res->ai_next) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005524 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005525 "sockaddr resolved to multiple addresses");
5526 goto fail;
5527 }
5528 switch (res->ai_family) {
5529 case AF_INET:
5530 {
5531 if (PyTuple_GET_SIZE(sa) != 2) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005532 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005533 "IPv4 sockaddr must be 2 tuple");
5534 goto fail;
5535 }
5536 break;
5537 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00005538#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005539 case AF_INET6:
5540 {
5541 struct sockaddr_in6 *sin6;
5542 sin6 = (struct sockaddr_in6 *)res->ai_addr;
Charles-François Natali366999a2012-01-02 15:47:29 +01005543 sin6->sin6_flowinfo = htonl(flowinfo);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005544 sin6->sin6_scope_id = scope_id;
5545 break;
5546 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005547#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005548 }
Antoine Pitrou19467d22010-08-17 19:33:30 +00005549 error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005550 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
5551 if (error) {
5552 set_gaierror(error);
5553 goto fail;
5554 }
5555 ret = Py_BuildValue("ss", hbuf, pbuf);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005556
5557fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005558 if (res)
5559 freeaddrinfo(res);
5560 return ret;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005561}
5562
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005563PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005564"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005565\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005566Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005567
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005568
5569/* Python API to getting and setting the default timeout value. */
5570
5571static PyObject *
5572socket_getdefaulttimeout(PyObject *self)
5573{
Victor Stinner71694d52015-03-28 01:18:54 +01005574 if (defaulttimeout < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005575 Py_INCREF(Py_None);
5576 return Py_None;
5577 }
Victor Stinner71694d52015-03-28 01:18:54 +01005578 else {
5579 double seconds = _PyTime_AsSecondsDouble(defaulttimeout);
5580 return PyFloat_FromDouble(seconds);
5581 }
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005582}
5583
5584PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005585"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005586\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03005587Returns the default timeout in seconds (float) for new socket objects.\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005588A value of None indicates that new socket objects have no timeout.\n\
5589When the socket module is first imported, the default is None.");
5590
5591static PyObject *
5592socket_setdefaulttimeout(PyObject *self, PyObject *arg)
5593{
Victor Stinner71694d52015-03-28 01:18:54 +01005594 _PyTime_t timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005595
Victor Stinner71694d52015-03-28 01:18:54 +01005596 if (socket_parse_timeout(&timeout, arg) < 0)
5597 return NULL;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005599 defaulttimeout = timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005601 Py_INCREF(Py_None);
5602 return Py_None;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005603}
5604
5605PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005606"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005607\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03005608Set the default timeout in seconds (float) for new socket objects.\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005609A value of None indicates that new socket objects have no timeout.\n\
5610When the socket module is first imported, the default is None.");
5611
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005612#ifdef HAVE_IF_NAMEINDEX
5613/* Python API for getting interface indices and names */
5614
5615static PyObject *
5616socket_if_nameindex(PyObject *self, PyObject *arg)
5617{
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005618 PyObject *list;
Charles-François Natali60713592011-05-20 16:55:06 +02005619 int i;
5620 struct if_nameindex *ni;
Victor Stinner77af1722011-05-26 14:05:59 +02005621
Charles-François Natali60713592011-05-20 16:55:06 +02005622 ni = if_nameindex();
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005623 if (ni == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005624 PyErr_SetFromErrno(PyExc_OSError);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005625 return NULL;
5626 }
5627
5628 list = PyList_New(0);
5629 if (list == NULL) {
5630 if_freenameindex(ni);
5631 return NULL;
5632 }
5633
Charles-François Natali60713592011-05-20 16:55:06 +02005634 for (i = 0; ni[i].if_index != 0 && i < INT_MAX; i++) {
5635 PyObject *ni_tuple = Py_BuildValue("IO&",
5636 ni[i].if_index, PyUnicode_DecodeFSDefault, ni[i].if_name);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005637
5638 if (ni_tuple == NULL || PyList_Append(list, ni_tuple) == -1) {
5639 Py_XDECREF(ni_tuple);
Charles-François Natali60713592011-05-20 16:55:06 +02005640 Py_DECREF(list);
5641 if_freenameindex(ni);
5642 return NULL;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005643 }
5644 Py_DECREF(ni_tuple);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005645 }
5646
5647 if_freenameindex(ni);
5648 return list;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005649}
5650
5651PyDoc_STRVAR(if_nameindex_doc,
5652"if_nameindex()\n\
5653\n\
5654Returns a list of network interface information (index, name) tuples.");
5655
Charles-François Natali60713592011-05-20 16:55:06 +02005656static PyObject *
5657socket_if_nametoindex(PyObject *self, PyObject *args)
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005658{
Charles-François Natali60713592011-05-20 16:55:06 +02005659 PyObject *oname;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005660 unsigned long index;
5661
Charles-François Natali60713592011-05-20 16:55:06 +02005662 if (!PyArg_ParseTuple(args, "O&:if_nametoindex",
5663 PyUnicode_FSConverter, &oname))
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005664 return NULL;
5665
Charles-François Natali60713592011-05-20 16:55:06 +02005666 index = if_nametoindex(PyBytes_AS_STRING(oname));
5667 Py_DECREF(oname);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005668 if (index == 0) {
Charles-François Natali60713592011-05-20 16:55:06 +02005669 /* if_nametoindex() doesn't set errno */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005670 PyErr_SetString(PyExc_OSError, "no interface with this name");
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005671 return NULL;
5672 }
5673
5674 return PyLong_FromUnsignedLong(index);
5675}
5676
5677PyDoc_STRVAR(if_nametoindex_doc,
5678"if_nametoindex(if_name)\n\
5679\n\
5680Returns the interface index corresponding to the interface name if_name.");
5681
Charles-François Natali60713592011-05-20 16:55:06 +02005682static PyObject *
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005683socket_if_indextoname(PyObject *self, PyObject *arg)
5684{
Charles-François Natali60713592011-05-20 16:55:06 +02005685 unsigned long index;
5686 char name[IF_NAMESIZE + 1];
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005687
Charles-François Natali60713592011-05-20 16:55:06 +02005688 index = PyLong_AsUnsignedLong(arg);
5689 if (index == (unsigned long) -1)
5690 return NULL;
5691
5692 if (if_indextoname(index, name) == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005693 PyErr_SetFromErrno(PyExc_OSError);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005694 return NULL;
5695 }
5696
Charles-François Natali60713592011-05-20 16:55:06 +02005697 return PyUnicode_DecodeFSDefault(name);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005698}
5699
5700PyDoc_STRVAR(if_indextoname_doc,
5701"if_indextoname(if_index)\n\
5702\n\
5703Returns the interface name corresponding to the interface index if_index.");
5704
5705#endif /* HAVE_IF_NAMEINDEX */
5706
5707
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005708#ifdef CMSG_LEN
5709/* Python interface to CMSG_LEN(length). */
5710
5711static PyObject *
5712socket_CMSG_LEN(PyObject *self, PyObject *args)
5713{
5714 Py_ssize_t length;
5715 size_t result;
5716
5717 if (!PyArg_ParseTuple(args, "n:CMSG_LEN", &length))
5718 return NULL;
5719 if (length < 0 || !get_CMSG_LEN(length, &result)) {
5720 PyErr_Format(PyExc_OverflowError, "CMSG_LEN() argument out of range");
5721 return NULL;
5722 }
5723 return PyLong_FromSize_t(result);
5724}
5725
5726PyDoc_STRVAR(CMSG_LEN_doc,
5727"CMSG_LEN(length) -> control message length\n\
5728\n\
5729Return the total length, without trailing padding, of an ancillary\n\
5730data item with associated data of the given length. This value can\n\
5731often be used as the buffer size for recvmsg() to receive a single\n\
5732item of ancillary data, but RFC 3542 requires portable applications to\n\
5733use CMSG_SPACE() and thus include space for padding, even when the\n\
5734item will be the last in the buffer. Raises OverflowError if length\n\
5735is outside the permissible range of values.");
5736
5737
5738#ifdef CMSG_SPACE
5739/* Python interface to CMSG_SPACE(length). */
5740
5741static PyObject *
5742socket_CMSG_SPACE(PyObject *self, PyObject *args)
5743{
5744 Py_ssize_t length;
5745 size_t result;
5746
5747 if (!PyArg_ParseTuple(args, "n:CMSG_SPACE", &length))
5748 return NULL;
5749 if (length < 0 || !get_CMSG_SPACE(length, &result)) {
5750 PyErr_SetString(PyExc_OverflowError,
5751 "CMSG_SPACE() argument out of range");
5752 return NULL;
5753 }
5754 return PyLong_FromSize_t(result);
5755}
5756
5757PyDoc_STRVAR(CMSG_SPACE_doc,
5758"CMSG_SPACE(length) -> buffer size\n\
5759\n\
5760Return the buffer size needed for recvmsg() to receive an ancillary\n\
5761data item with associated data of the given length, along with any\n\
5762trailing padding. The buffer space needed to receive multiple items\n\
5763is the sum of the CMSG_SPACE() values for their associated data\n\
5764lengths. Raises OverflowError if length is outside the permissible\n\
5765range of values.");
5766#endif /* CMSG_SPACE */
5767#endif /* CMSG_LEN */
5768
5769
Guido van Rossum30a685f1991-06-27 15:51:29 +00005770/* List of functions exported by this module. */
5771
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005772static PyMethodDef socket_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005773 {"gethostbyname", socket_gethostbyname,
5774 METH_VARARGS, gethostbyname_doc},
5775 {"gethostbyname_ex", socket_gethostbyname_ex,
5776 METH_VARARGS, ghbn_ex_doc},
5777 {"gethostbyaddr", socket_gethostbyaddr,
5778 METH_VARARGS, gethostbyaddr_doc},
5779 {"gethostname", socket_gethostname,
5780 METH_NOARGS, gethostname_doc},
Antoine Pitrou061cfb52011-02-28 22:25:22 +00005781#ifdef HAVE_SETHOSTNAME
5782 {"sethostname", socket_sethostname,
5783 METH_VARARGS, sethostname_doc},
5784#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005785 {"getservbyname", socket_getservbyname,
5786 METH_VARARGS, getservbyname_doc},
5787 {"getservbyport", socket_getservbyport,
5788 METH_VARARGS, getservbyport_doc},
5789 {"getprotobyname", socket_getprotobyname,
5790 METH_VARARGS, getprotobyname_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005791#ifndef NO_DUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005792 {"dup", socket_dup,
5793 METH_O, dup_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005794#endif
Dave Cole331708b2004-08-09 04:51:41 +00005795#ifdef HAVE_SOCKETPAIR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005796 {"socketpair", socket_socketpair,
5797 METH_VARARGS, socketpair_doc},
Dave Cole331708b2004-08-09 04:51:41 +00005798#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005799 {"ntohs", socket_ntohs,
5800 METH_VARARGS, ntohs_doc},
5801 {"ntohl", socket_ntohl,
5802 METH_O, ntohl_doc},
5803 {"htons", socket_htons,
5804 METH_VARARGS, htons_doc},
5805 {"htonl", socket_htonl,
5806 METH_O, htonl_doc},
5807 {"inet_aton", socket_inet_aton,
5808 METH_VARARGS, inet_aton_doc},
5809 {"inet_ntoa", socket_inet_ntoa,
5810 METH_VARARGS, inet_ntoa_doc},
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005811#if defined(HAVE_INET_PTON) || defined(MS_WINDOWS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005812 {"inet_pton", socket_inet_pton,
5813 METH_VARARGS, inet_pton_doc},
5814 {"inet_ntop", socket_inet_ntop,
5815 METH_VARARGS, inet_ntop_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005816#endif
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005817 {"getaddrinfo", (PyCFunction)socket_getaddrinfo,
5818 METH_VARARGS | METH_KEYWORDS, getaddrinfo_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005819 {"getnameinfo", socket_getnameinfo,
5820 METH_VARARGS, getnameinfo_doc},
5821 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
5822 METH_NOARGS, getdefaulttimeout_doc},
5823 {"setdefaulttimeout", socket_setdefaulttimeout,
5824 METH_O, setdefaulttimeout_doc},
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005825#ifdef HAVE_IF_NAMEINDEX
5826 {"if_nameindex", socket_if_nameindex,
5827 METH_NOARGS, if_nameindex_doc},
5828 {"if_nametoindex", socket_if_nametoindex,
Charles-François Natali60713592011-05-20 16:55:06 +02005829 METH_VARARGS, if_nametoindex_doc},
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005830 {"if_indextoname", socket_if_indextoname,
5831 METH_O, if_indextoname_doc},
5832#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005833#ifdef CMSG_LEN
5834 {"CMSG_LEN", socket_CMSG_LEN,
5835 METH_VARARGS, CMSG_LEN_doc},
5836#ifdef CMSG_SPACE
5837 {"CMSG_SPACE", socket_CMSG_SPACE,
5838 METH_VARARGS, CMSG_SPACE_doc},
5839#endif
5840#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005841 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005842};
5843
Guido van Rossum30a685f1991-06-27 15:51:29 +00005844
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005845#ifdef MS_WINDOWS
5846#define OS_INIT_DEFINED
5847
5848/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00005849
5850static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005851os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00005852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005853 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00005854}
5855
5856static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005857os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00005858{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005859 WSADATA WSAData;
5860 int ret;
5861 ret = WSAStartup(0x0101, &WSAData);
5862 switch (ret) {
5863 case 0: /* No error */
5864 Py_AtExit(os_cleanup);
5865 return 1; /* Success */
5866 case WSASYSNOTREADY:
5867 PyErr_SetString(PyExc_ImportError,
5868 "WSAStartup failed: network not ready");
5869 break;
5870 case WSAVERNOTSUPPORTED:
5871 case WSAEINVAL:
5872 PyErr_SetString(
5873 PyExc_ImportError,
5874 "WSAStartup failed: requested version not supported");
5875 break;
5876 default:
5877 PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret);
5878 break;
5879 }
5880 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00005881}
5882
Guido van Rossum8d665e61996-06-26 18:22:49 +00005883#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00005884
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005885
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005886
5887#ifndef OS_INIT_DEFINED
5888static int
5889os_init(void)
5890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005891 return 1; /* Success */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005892}
5893#endif
5894
5895
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00005896/* C API table - always add new things to the end for binary
5897 compatibility. */
5898static
5899PySocketModule_APIObject PySocketModuleAPI =
5900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005901 &sock_type,
Antoine Pitrouc4df7842010-12-03 19:59:41 +00005902 NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005903 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00005904};
5905
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005906
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005907/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005908
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005909 This module is actually called "_socket", and there's a wrapper
Guido van Rossum7d0a8262007-05-21 23:13:11 +00005910 "socket.py" which implements some additional functionality.
5911 The import of "_socket" may fail with an ImportError exception if
5912 os-specific initialization fails. On Windows, this does WINSOCK
Ezio Melotti13925002011-03-16 11:05:33 +02005913 initialization. When WINSOCK is initialized successfully, a call to
Guido van Rossum7d0a8262007-05-21 23:13:11 +00005914 WSACleanup() is scheduled to be made at exit time.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005915*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005916
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005917PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005918"Implementation module for socket operations.\n\
5919\n\
5920See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00005921
Martin v. Löwis1a214512008-06-11 05:26:20 +00005922static struct PyModuleDef socketmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005923 PyModuleDef_HEAD_INIT,
5924 PySocket_MODULE_NAME,
5925 socket_doc,
5926 -1,
5927 socket_methods,
5928 NULL,
5929 NULL,
5930 NULL,
5931 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00005932};
5933
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005934PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00005935PyInit__socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005937 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00005938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005939 if (!os_init())
5940 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005941
Victor Stinnerdaf45552013-08-28 00:53:59 +02005942#ifdef MS_WINDOWS
5943 if (support_wsa_no_inherit == -1) {
Steve Dower65e4cb12014-11-22 12:54:57 -08005944#if defined(_MSC_VER) && _MSC_VER >= 1800
5945 support_wsa_no_inherit = IsWindows7SP1OrGreater();
5946#else
Victor Stinnerdaf45552013-08-28 00:53:59 +02005947 DWORD version = GetVersion();
5948 DWORD major = (DWORD)LOBYTE(LOWORD(version));
5949 DWORD minor = (DWORD)HIBYTE(LOWORD(version));
5950 /* need Windows 7 SP1, 2008 R2 SP1 or later */
Steve Dower65e4cb12014-11-22 12:54:57 -08005951 support_wsa_no_inherit = major > 6 || (major == 6 && minor >= 1);
5952#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02005953 }
5954#endif
5955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005956 Py_TYPE(&sock_type) = &PyType_Type;
5957 m = PyModule_Create(&socketmodule);
5958 if (m == NULL)
5959 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005960
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005961 Py_INCREF(PyExc_OSError);
5962 PySocketModuleAPI.error = PyExc_OSError;
5963 Py_INCREF(PyExc_OSError);
5964 PyModule_AddObject(m, "error", PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005965 socket_herror = PyErr_NewException("socket.herror",
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005966 PyExc_OSError, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005967 if (socket_herror == NULL)
5968 return NULL;
5969 Py_INCREF(socket_herror);
5970 PyModule_AddObject(m, "herror", socket_herror);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005971 socket_gaierror = PyErr_NewException("socket.gaierror", PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005972 NULL);
5973 if (socket_gaierror == NULL)
5974 return NULL;
5975 Py_INCREF(socket_gaierror);
5976 PyModule_AddObject(m, "gaierror", socket_gaierror);
5977 socket_timeout = PyErr_NewException("socket.timeout",
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005978 PyExc_OSError, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005979 if (socket_timeout == NULL)
5980 return NULL;
Antoine Pitrouc4df7842010-12-03 19:59:41 +00005981 PySocketModuleAPI.timeout_error = socket_timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005982 Py_INCREF(socket_timeout);
5983 PyModule_AddObject(m, "timeout", socket_timeout);
5984 Py_INCREF((PyObject *)&sock_type);
5985 if (PyModule_AddObject(m, "SocketType",
5986 (PyObject *)&sock_type) != 0)
5987 return NULL;
5988 Py_INCREF((PyObject *)&sock_type);
5989 if (PyModule_AddObject(m, "socket",
5990 (PyObject *)&sock_type) != 0)
5991 return NULL;
Guido van Rossum09be4091999-08-09 14:40:40 +00005992
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005993#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005994 has_ipv6 = Py_True;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005995#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005996 has_ipv6 = Py_False;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005997#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005998 Py_INCREF(has_ipv6);
5999 PyModule_AddObject(m, "has_ipv6", has_ipv6);
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00006000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006001 /* Export C API */
6002 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
6003 PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL)
6004 ) != 0)
6005 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00006006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006007 /* Address families (we only support AF_INET and AF_UNIX) */
Guido van Rossum09be4091999-08-09 14:40:40 +00006008#ifdef AF_UNSPEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006009 PyModule_AddIntMacro(m, AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00006010#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006011 PyModule_AddIntMacro(m, AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006012#ifdef AF_INET6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006013 PyModule_AddIntMacro(m, AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006014#endif /* AF_INET6 */
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00006015#if defined(AF_UNIX)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006016 PyModule_AddIntMacro(m, AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006017#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00006018#ifdef AF_AX25
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006019 /* Amateur Radio AX.25 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006020 PyModule_AddIntMacro(m, AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00006021#endif
6022#ifdef AF_IPX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006023 PyModule_AddIntMacro(m, AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00006024#endif
6025#ifdef AF_APPLETALK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006026 /* Appletalk DDP */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006027 PyModule_AddIntMacro(m, AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00006028#endif
6029#ifdef AF_NETROM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006030 /* Amateur radio NetROM */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006031 PyModule_AddIntMacro(m, AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00006032#endif
6033#ifdef AF_BRIDGE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006034 /* Multiprotocol bridge */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006035 PyModule_AddIntMacro(m, AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00006036#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006037#ifdef AF_ATMPVC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006038 /* ATM PVCs */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006039 PyModule_AddIntMacro(m, AF_ATMPVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006040#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006041#ifdef AF_AAL5
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006042 /* Reserved for Werner's ATM */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006043 PyModule_AddIntMacro(m, AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00006044#endif
6045#ifdef AF_X25
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006046 /* Reserved for X.25 project */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006047 PyModule_AddIntMacro(m, AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00006048#endif
6049#ifdef AF_INET6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006050 PyModule_AddIntMacro(m, AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00006051#endif
6052#ifdef AF_ROSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006053 /* Amateur Radio X.25 PLP */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006054 PyModule_AddIntMacro(m, AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00006055#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006056#ifdef AF_DECnet
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006057 /* Reserved for DECnet project */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006058 PyModule_AddIntMacro(m, AF_DECnet);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006059#endif
6060#ifdef AF_NETBEUI
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006061 /* Reserved for 802.2LLC project */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006062 PyModule_AddIntMacro(m, AF_NETBEUI);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006063#endif
6064#ifdef AF_SECURITY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006065 /* Security callback pseudo AF */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006066 PyModule_AddIntMacro(m, AF_SECURITY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006067#endif
6068#ifdef AF_KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006069 /* PF_KEY key management API */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006070 PyModule_AddIntMacro(m, AF_KEY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006071#endif
6072#ifdef AF_NETLINK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006073 /* */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006074 PyModule_AddIntMacro(m, AF_NETLINK);
6075 PyModule_AddIntMacro(m, NETLINK_ROUTE);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006076#ifdef NETLINK_SKIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006077 PyModule_AddIntMacro(m, NETLINK_SKIP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006078#endif
6079#ifdef NETLINK_W1
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006080 PyModule_AddIntMacro(m, NETLINK_W1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006081#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006082 PyModule_AddIntMacro(m, NETLINK_USERSOCK);
6083 PyModule_AddIntMacro(m, NETLINK_FIREWALL);
Guido van Rossum668a94a2006-02-21 01:07:27 +00006084#ifdef NETLINK_TCPDIAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006085 PyModule_AddIntMacro(m, NETLINK_TCPDIAG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00006086#endif
6087#ifdef NETLINK_NFLOG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006088 PyModule_AddIntMacro(m, NETLINK_NFLOG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00006089#endif
Neal Norwitz65851662006-01-16 04:31:40 +00006090#ifdef NETLINK_XFRM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006091 PyModule_AddIntMacro(m, NETLINK_XFRM);
Neal Norwitz65851662006-01-16 04:31:40 +00006092#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006093#ifdef NETLINK_ARPD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006094 PyModule_AddIntMacro(m, NETLINK_ARPD);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006095#endif
6096#ifdef NETLINK_ROUTE6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006097 PyModule_AddIntMacro(m, NETLINK_ROUTE6);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006098#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006099 PyModule_AddIntMacro(m, NETLINK_IP6_FW);
Thomas Wouterscf297e42007-02-23 15:07:44 +00006100#ifdef NETLINK_DNRTMSG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006101 PyModule_AddIntMacro(m, NETLINK_DNRTMSG);
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00006102#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006103#ifdef NETLINK_TAPBASE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006104 PyModule_AddIntMacro(m, NETLINK_TAPBASE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006105#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006106#endif /* AF_NETLINK */
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006107#ifdef AF_ROUTE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006108 /* Alias to emulate 4.4BSD */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006109 PyModule_AddIntMacro(m, AF_ROUTE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006110#endif
Giampaolo Rodola'80e1c432013-05-21 21:02:04 +02006111#ifdef AF_LINK
6112 PyModule_AddIntMacro(m, AF_LINK);
6113#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006114#ifdef AF_ASH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006115 /* Ash */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006116 PyModule_AddIntMacro(m, AF_ASH);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006117#endif
6118#ifdef AF_ECONET
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006119 /* Acorn Econet */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006120 PyModule_AddIntMacro(m, AF_ECONET);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006121#endif
6122#ifdef AF_ATMSVC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006123 /* ATM SVCs */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006124 PyModule_AddIntMacro(m, AF_ATMSVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006125#endif
6126#ifdef AF_SNA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006127 /* Linux SNA Project (nutters!) */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006128 PyModule_AddIntMacro(m, AF_SNA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006129#endif
6130#ifdef AF_IRDA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006131 /* IRDA sockets */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006132 PyModule_AddIntMacro(m, AF_IRDA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006133#endif
6134#ifdef AF_PPPOX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006135 /* PPPoX sockets */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006136 PyModule_AddIntMacro(m, AF_PPPOX);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006137#endif
6138#ifdef AF_WANPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006139 /* Wanpipe API Sockets */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006140 PyModule_AddIntMacro(m, AF_WANPIPE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006141#endif
6142#ifdef AF_LLC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006143 /* Linux LLC */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006144 PyModule_AddIntMacro(m, AF_LLC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00006145#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00006146
Hye-Shik Chang81268602004-02-02 06:05:24 +00006147#ifdef USE_BLUETOOTH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006148 PyModule_AddIntMacro(m, AF_BLUETOOTH);
6149 PyModule_AddIntMacro(m, BTPROTO_L2CAP);
6150 PyModule_AddIntMacro(m, BTPROTO_HCI);
6151 PyModule_AddIntMacro(m, SOL_HCI);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00006152#if !defined(__NetBSD__) && !defined(__DragonFly__)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006153 PyModule_AddIntMacro(m, HCI_FILTER);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00006154#endif
Hye-Shik Chang81268602004-02-02 06:05:24 +00006155#if !defined(__FreeBSD__)
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00006156#if !defined(__NetBSD__) && !defined(__DragonFly__)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006157 PyModule_AddIntMacro(m, HCI_TIME_STAMP);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00006158#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006159 PyModule_AddIntMacro(m, HCI_DATA_DIR);
6160 PyModule_AddIntMacro(m, BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00006161#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006162 PyModule_AddIntMacro(m, BTPROTO_RFCOMM);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006163 PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
6164 PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
Martin v. Löwis12af0482004-01-31 12:34:17 +00006165#endif
6166
Charles-François Natali47413c12011-10-06 19:47:44 +02006167#ifdef AF_CAN
6168 /* Controller Area Network */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006169 PyModule_AddIntMacro(m, AF_CAN);
Charles-François Natali47413c12011-10-06 19:47:44 +02006170#endif
6171#ifdef PF_CAN
6172 /* Controller Area Network */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006173 PyModule_AddIntMacro(m, PF_CAN);
Charles-François Natali47413c12011-10-06 19:47:44 +02006174#endif
6175
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006176/* Reliable Datagram Sockets */
6177#ifdef AF_RDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006178 PyModule_AddIntMacro(m, AF_RDS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006179#endif
6180#ifdef PF_RDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006181 PyModule_AddIntMacro(m, PF_RDS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006182#endif
6183
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006184/* Kernel event messages */
6185#ifdef PF_SYSTEM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006186 PyModule_AddIntMacro(m, PF_SYSTEM);
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006187#endif
6188#ifdef AF_SYSTEM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006189 PyModule_AddIntMacro(m, AF_SYSTEM);
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006190#endif
6191
Antoine Pitroub156a462010-10-27 20:13:57 +00006192#ifdef AF_PACKET
6193 PyModule_AddIntMacro(m, AF_PACKET);
6194#endif
6195#ifdef PF_PACKET
6196 PyModule_AddIntMacro(m, PF_PACKET);
6197#endif
6198#ifdef PACKET_HOST
6199 PyModule_AddIntMacro(m, PACKET_HOST);
6200#endif
6201#ifdef PACKET_BROADCAST
6202 PyModule_AddIntMacro(m, PACKET_BROADCAST);
6203#endif
6204#ifdef PACKET_MULTICAST
6205 PyModule_AddIntMacro(m, PACKET_MULTICAST);
6206#endif
6207#ifdef PACKET_OTHERHOST
6208 PyModule_AddIntMacro(m, PACKET_OTHERHOST);
6209#endif
6210#ifdef PACKET_OUTGOING
6211 PyModule_AddIntMacro(m, PACKET_OUTGOING);
6212#endif
6213#ifdef PACKET_LOOPBACK
6214 PyModule_AddIntMacro(m, PACKET_LOOPBACK);
6215#endif
6216#ifdef PACKET_FASTROUTE
6217 PyModule_AddIntMacro(m, PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00006218#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006219
Christian Heimes043d6f62008-01-07 17:19:16 +00006220#ifdef HAVE_LINUX_TIPC_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006221 PyModule_AddIntMacro(m, AF_TIPC);
Christian Heimes043d6f62008-01-07 17:19:16 +00006222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006223 /* for addresses */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006224 PyModule_AddIntMacro(m, TIPC_ADDR_NAMESEQ);
6225 PyModule_AddIntMacro(m, TIPC_ADDR_NAME);
6226 PyModule_AddIntMacro(m, TIPC_ADDR_ID);
Christian Heimes043d6f62008-01-07 17:19:16 +00006227
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006228 PyModule_AddIntMacro(m, TIPC_ZONE_SCOPE);
6229 PyModule_AddIntMacro(m, TIPC_CLUSTER_SCOPE);
6230 PyModule_AddIntMacro(m, TIPC_NODE_SCOPE);
Christian Heimes043d6f62008-01-07 17:19:16 +00006231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006232 /* for setsockopt() */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006233 PyModule_AddIntMacro(m, SOL_TIPC);
6234 PyModule_AddIntMacro(m, TIPC_IMPORTANCE);
6235 PyModule_AddIntMacro(m, TIPC_SRC_DROPPABLE);
6236 PyModule_AddIntMacro(m, TIPC_DEST_DROPPABLE);
6237 PyModule_AddIntMacro(m, TIPC_CONN_TIMEOUT);
Christian Heimes043d6f62008-01-07 17:19:16 +00006238
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006239 PyModule_AddIntMacro(m, TIPC_LOW_IMPORTANCE);
6240 PyModule_AddIntMacro(m, TIPC_MEDIUM_IMPORTANCE);
6241 PyModule_AddIntMacro(m, TIPC_HIGH_IMPORTANCE);
6242 PyModule_AddIntMacro(m, TIPC_CRITICAL_IMPORTANCE);
Christian Heimes043d6f62008-01-07 17:19:16 +00006243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006244 /* for subscriptions */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006245 PyModule_AddIntMacro(m, TIPC_SUB_PORTS);
6246 PyModule_AddIntMacro(m, TIPC_SUB_SERVICE);
Christian Heimes25bb7832008-01-11 16:17:00 +00006247#ifdef TIPC_SUB_CANCEL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006248 /* doesn't seem to be available everywhere */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006249 PyModule_AddIntMacro(m, TIPC_SUB_CANCEL);
Christian Heimes25bb7832008-01-11 16:17:00 +00006250#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006251 PyModule_AddIntMacro(m, TIPC_WAIT_FOREVER);
6252 PyModule_AddIntMacro(m, TIPC_PUBLISHED);
6253 PyModule_AddIntMacro(m, TIPC_WITHDRAWN);
6254 PyModule_AddIntMacro(m, TIPC_SUBSCR_TIMEOUT);
6255 PyModule_AddIntMacro(m, TIPC_CFG_SRV);
6256 PyModule_AddIntMacro(m, TIPC_TOP_SRV);
Christian Heimes043d6f62008-01-07 17:19:16 +00006257#endif
6258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006259 /* Socket types */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006260 PyModule_AddIntMacro(m, SOCK_STREAM);
6261 PyModule_AddIntMacro(m, SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00006262/* We have incomplete socket support. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006263 PyModule_AddIntMacro(m, SOCK_RAW);
6264 PyModule_AddIntMacro(m, SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00006265#if defined(SOCK_RDM)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006266 PyModule_AddIntMacro(m, SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00006267#endif
Antoine Pitroub1c54962010-10-14 15:05:38 +00006268#ifdef SOCK_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006269 PyModule_AddIntMacro(m, SOCK_CLOEXEC);
Antoine Pitroub1c54962010-10-14 15:05:38 +00006270#endif
6271#ifdef SOCK_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006272 PyModule_AddIntMacro(m, SOCK_NONBLOCK);
Antoine Pitroub1c54962010-10-14 15:05:38 +00006273#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006275#ifdef SO_DEBUG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006276 PyModule_AddIntMacro(m, SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006277#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006278#ifdef SO_ACCEPTCONN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006279 PyModule_AddIntMacro(m, SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006280#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006281#ifdef SO_REUSEADDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006282 PyModule_AddIntMacro(m, SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006283#endif
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00006284#ifdef SO_EXCLUSIVEADDRUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006285 PyModule_AddIntMacro(m, SO_EXCLUSIVEADDRUSE);
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00006286#endif
6287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006288#ifdef SO_KEEPALIVE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006289 PyModule_AddIntMacro(m, SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006290#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006291#ifdef SO_DONTROUTE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006292 PyModule_AddIntMacro(m, SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006293#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006294#ifdef SO_BROADCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006295 PyModule_AddIntMacro(m, SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006296#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006297#ifdef SO_USELOOPBACK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006298 PyModule_AddIntMacro(m, SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006299#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006300#ifdef SO_LINGER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006301 PyModule_AddIntMacro(m, SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006302#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006303#ifdef SO_OOBINLINE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006304 PyModule_AddIntMacro(m, SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006305#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006306#ifdef SO_REUSEPORT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006307 PyModule_AddIntMacro(m, SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006308#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006309#ifdef SO_SNDBUF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006310 PyModule_AddIntMacro(m, SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006311#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006312#ifdef SO_RCVBUF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006313 PyModule_AddIntMacro(m, SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006314#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006315#ifdef SO_SNDLOWAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006316 PyModule_AddIntMacro(m, SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006317#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006318#ifdef SO_RCVLOWAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006319 PyModule_AddIntMacro(m, SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006320#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006321#ifdef SO_SNDTIMEO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006322 PyModule_AddIntMacro(m, SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006323#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006324#ifdef SO_RCVTIMEO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006325 PyModule_AddIntMacro(m, SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006326#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006327#ifdef SO_ERROR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006328 PyModule_AddIntMacro(m, SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006329#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006330#ifdef SO_TYPE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006331 PyModule_AddIntMacro(m, SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006332#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006333#ifdef SO_SETFIB
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006334 PyModule_AddIntMacro(m, SO_SETFIB);
Larry Hastingsf0f37952010-04-02 11:47:10 +00006335#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006336#ifdef SO_PASSCRED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006337 PyModule_AddIntMacro(m, SO_PASSCRED);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006338#endif
6339#ifdef SO_PEERCRED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006340 PyModule_AddIntMacro(m, SO_PEERCRED);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006341#endif
6342#ifdef LOCAL_PEERCRED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006343 PyModule_AddIntMacro(m, LOCAL_PEERCRED);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006344#endif
Victor Stinnercb98bed2011-10-25 13:45:48 +02006345#ifdef SO_BINDTODEVICE
6346 PyModule_AddIntMacro(m, SO_BINDTODEVICE);
6347#endif
Benjamin Peterson1314ef72013-11-27 09:18:54 -06006348#ifdef SO_PRIORITY
6349 PyModule_AddIntMacro(m, SO_PRIORITY);
6350#endif
Charles-François Nataliea07eb92014-09-18 23:18:46 +01006351#ifdef SO_MARK
6352 PyModule_AddIntMacro(m, SO_MARK);
6353#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006355 /* Maximum number of connections for "listen" */
6356#ifdef SOMAXCONN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006357 PyModule_AddIntMacro(m, SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006358#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006359 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006360#endif
6361
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006362 /* Ancilliary message types */
6363#ifdef SCM_RIGHTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006364 PyModule_AddIntMacro(m, SCM_RIGHTS);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006365#endif
6366#ifdef SCM_CREDENTIALS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006367 PyModule_AddIntMacro(m, SCM_CREDENTIALS);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006368#endif
6369#ifdef SCM_CREDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006370 PyModule_AddIntMacro(m, SCM_CREDS);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006371#endif
6372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006373 /* Flags for send, recv */
6374#ifdef MSG_OOB
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006375 PyModule_AddIntMacro(m, MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006376#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006377#ifdef MSG_PEEK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006378 PyModule_AddIntMacro(m, MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006379#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006380#ifdef MSG_DONTROUTE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006381 PyModule_AddIntMacro(m, MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006382#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006383#ifdef MSG_DONTWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006384 PyModule_AddIntMacro(m, MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00006385#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006386#ifdef MSG_EOR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006387 PyModule_AddIntMacro(m, MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006388#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006389#ifdef MSG_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006390 PyModule_AddIntMacro(m, MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006391#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006392#ifdef MSG_CTRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006393 PyModule_AddIntMacro(m, MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006394#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006395#ifdef MSG_WAITALL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006396 PyModule_AddIntMacro(m, MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006397#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006398#ifdef MSG_BTAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006399 PyModule_AddIntMacro(m, MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006400#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006401#ifdef MSG_ETAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006402 PyModule_AddIntMacro(m, MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006403#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006404#ifdef MSG_NOSIGNAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006405 PyModule_AddIntMacro(m, MSG_NOSIGNAL);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006406#endif
6407#ifdef MSG_NOTIFICATION
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006408 PyModule_AddIntMacro(m, MSG_NOTIFICATION);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006409#endif
6410#ifdef MSG_CMSG_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006411 PyModule_AddIntMacro(m, MSG_CMSG_CLOEXEC);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006412#endif
6413#ifdef MSG_ERRQUEUE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006414 PyModule_AddIntMacro(m, MSG_ERRQUEUE);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006415#endif
6416#ifdef MSG_CONFIRM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006417 PyModule_AddIntMacro(m, MSG_CONFIRM);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006418#endif
6419#ifdef MSG_MORE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006420 PyModule_AddIntMacro(m, MSG_MORE);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006421#endif
6422#ifdef MSG_EOF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006423 PyModule_AddIntMacro(m, MSG_EOF);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006424#endif
6425#ifdef MSG_BCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006426 PyModule_AddIntMacro(m, MSG_BCAST);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006427#endif
6428#ifdef MSG_MCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006429 PyModule_AddIntMacro(m, MSG_MCAST);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006430#endif
Benjamin Peterson57b66722012-12-12 22:24:47 -05006431#ifdef MSG_FASTOPEN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006432 PyModule_AddIntMacro(m, MSG_FASTOPEN);
Benjamin Peterson57b66722012-12-12 22:24:47 -05006433#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006435 /* Protocol level and numbers, usable for [gs]etsockopt */
6436#ifdef SOL_SOCKET
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006437 PyModule_AddIntMacro(m, SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006438#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006439#ifdef SOL_IP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006440 PyModule_AddIntMacro(m, SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006441#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006442 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00006443#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006444#ifdef SOL_IPX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006445 PyModule_AddIntMacro(m, SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00006446#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006447#ifdef SOL_AX25
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006448 PyModule_AddIntMacro(m, SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00006449#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006450#ifdef SOL_ATALK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006451 PyModule_AddIntMacro(m, SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00006452#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006453#ifdef SOL_NETROM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006454 PyModule_AddIntMacro(m, SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00006455#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006456#ifdef SOL_ROSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006457 PyModule_AddIntMacro(m, SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00006458#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006459#ifdef SOL_TCP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006460 PyModule_AddIntMacro(m, SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006461#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006462 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00006463#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006464#ifdef SOL_UDP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006465 PyModule_AddIntMacro(m, SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006466#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006467 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00006468#endif
Charles-François Natali47413c12011-10-06 19:47:44 +02006469#ifdef SOL_CAN_BASE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006470 PyModule_AddIntMacro(m, SOL_CAN_BASE);
Charles-François Natali47413c12011-10-06 19:47:44 +02006471#endif
6472#ifdef SOL_CAN_RAW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006473 PyModule_AddIntMacro(m, SOL_CAN_RAW);
6474 PyModule_AddIntMacro(m, CAN_RAW);
Charles-François Natali47413c12011-10-06 19:47:44 +02006475#endif
6476#ifdef HAVE_LINUX_CAN_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006477 PyModule_AddIntMacro(m, CAN_EFF_FLAG);
6478 PyModule_AddIntMacro(m, CAN_RTR_FLAG);
6479 PyModule_AddIntMacro(m, CAN_ERR_FLAG);
Charles-François Natali47413c12011-10-06 19:47:44 +02006480
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006481 PyModule_AddIntMacro(m, CAN_SFF_MASK);
6482 PyModule_AddIntMacro(m, CAN_EFF_MASK);
6483 PyModule_AddIntMacro(m, CAN_ERR_MASK);
Charles-François Natali47413c12011-10-06 19:47:44 +02006484#endif
6485#ifdef HAVE_LINUX_CAN_RAW_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006486 PyModule_AddIntMacro(m, CAN_RAW_FILTER);
6487 PyModule_AddIntMacro(m, CAN_RAW_ERR_FILTER);
6488 PyModule_AddIntMacro(m, CAN_RAW_LOOPBACK);
6489 PyModule_AddIntMacro(m, CAN_RAW_RECV_OWN_MSGS);
Charles-François Natali47413c12011-10-06 19:47:44 +02006490#endif
Charles-François Natali773e42d2013-02-05 19:42:01 +01006491#ifdef HAVE_LINUX_CAN_BCM_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006492 PyModule_AddIntMacro(m, CAN_BCM);
Charles-François Natali773e42d2013-02-05 19:42:01 +01006493 PyModule_AddIntConstant(m, "CAN_BCM_TX_SETUP", TX_SETUP);
6494 PyModule_AddIntConstant(m, "CAN_BCM_TX_DELETE", TX_DELETE);
6495 PyModule_AddIntConstant(m, "CAN_BCM_TX_READ", TX_READ);
6496 PyModule_AddIntConstant(m, "CAN_BCM_TX_SEND", TX_SEND);
6497 PyModule_AddIntConstant(m, "CAN_BCM_RX_SETUP", RX_SETUP);
6498 PyModule_AddIntConstant(m, "CAN_BCM_RX_DELETE", RX_DELETE);
6499 PyModule_AddIntConstant(m, "CAN_BCM_RX_READ", RX_READ);
6500 PyModule_AddIntConstant(m, "CAN_BCM_TX_STATUS", TX_STATUS);
6501 PyModule_AddIntConstant(m, "CAN_BCM_TX_EXPIRED", TX_EXPIRED);
6502 PyModule_AddIntConstant(m, "CAN_BCM_RX_STATUS", RX_STATUS);
6503 PyModule_AddIntConstant(m, "CAN_BCM_RX_TIMEOUT", RX_TIMEOUT);
6504 PyModule_AddIntConstant(m, "CAN_BCM_RX_CHANGED", RX_CHANGED);
6505#endif
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006506#ifdef SOL_RDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006507 PyModule_AddIntMacro(m, SOL_RDS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006508#endif
6509#ifdef RDS_CANCEL_SENT_TO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006510 PyModule_AddIntMacro(m, RDS_CANCEL_SENT_TO);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006511#endif
6512#ifdef RDS_GET_MR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006513 PyModule_AddIntMacro(m, RDS_GET_MR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006514#endif
6515#ifdef RDS_FREE_MR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006516 PyModule_AddIntMacro(m, RDS_FREE_MR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006517#endif
6518#ifdef RDS_RECVERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006519 PyModule_AddIntMacro(m, RDS_RECVERR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006520#endif
6521#ifdef RDS_CONG_MONITOR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006522 PyModule_AddIntMacro(m, RDS_CONG_MONITOR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006523#endif
6524#ifdef RDS_GET_MR_FOR_DEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006525 PyModule_AddIntMacro(m, RDS_GET_MR_FOR_DEST);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006526#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006527#ifdef IPPROTO_IP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006528 PyModule_AddIntMacro(m, IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00006529#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006530 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006531#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006532#ifdef IPPROTO_HOPOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006533 PyModule_AddIntMacro(m, IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006534#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006535#ifdef IPPROTO_ICMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006536 PyModule_AddIntMacro(m, IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00006537#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006538 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006539#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006540#ifdef IPPROTO_IGMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006541 PyModule_AddIntMacro(m, IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006542#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006543#ifdef IPPROTO_GGP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006544 PyModule_AddIntMacro(m, IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006545#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006546#ifdef IPPROTO_IPV4
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006547 PyModule_AddIntMacro(m, IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006548#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006549#ifdef IPPROTO_IPV6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006550 PyModule_AddIntMacro(m, IPPROTO_IPV6);
Martin v. Löwisa0f17342003-10-03 13:56:20 +00006551#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006552#ifdef IPPROTO_IPIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006553 PyModule_AddIntMacro(m, IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006554#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006555#ifdef IPPROTO_TCP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006556 PyModule_AddIntMacro(m, IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00006557#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006558 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006559#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006560#ifdef IPPROTO_EGP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006561 PyModule_AddIntMacro(m, IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006562#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006563#ifdef IPPROTO_PUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006564 PyModule_AddIntMacro(m, IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006565#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006566#ifdef IPPROTO_UDP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006567 PyModule_AddIntMacro(m, IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00006568#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006569 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006570#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006571#ifdef IPPROTO_IDP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006572 PyModule_AddIntMacro(m, IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006573#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006574#ifdef IPPROTO_HELLO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006575 PyModule_AddIntMacro(m, IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006576#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006577#ifdef IPPROTO_ND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006578 PyModule_AddIntMacro(m, IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006579#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006580#ifdef IPPROTO_TP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006581 PyModule_AddIntMacro(m, IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006582#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006583#ifdef IPPROTO_IPV6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006584 PyModule_AddIntMacro(m, IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006585#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006586#ifdef IPPROTO_ROUTING
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006587 PyModule_AddIntMacro(m, IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006588#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006589#ifdef IPPROTO_FRAGMENT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006590 PyModule_AddIntMacro(m, IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006591#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006592#ifdef IPPROTO_RSVP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006593 PyModule_AddIntMacro(m, IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006594#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006595#ifdef IPPROTO_GRE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006596 PyModule_AddIntMacro(m, IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006597#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006598#ifdef IPPROTO_ESP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006599 PyModule_AddIntMacro(m, IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006600#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006601#ifdef IPPROTO_AH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006602 PyModule_AddIntMacro(m, IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006603#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006604#ifdef IPPROTO_MOBILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006605 PyModule_AddIntMacro(m, IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006606#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006607#ifdef IPPROTO_ICMPV6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006608 PyModule_AddIntMacro(m, IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006609#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006610#ifdef IPPROTO_NONE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006611 PyModule_AddIntMacro(m, IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006612#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006613#ifdef IPPROTO_DSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006614 PyModule_AddIntMacro(m, IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006615#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006616#ifdef IPPROTO_XTP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006617 PyModule_AddIntMacro(m, IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006618#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006619#ifdef IPPROTO_EON
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006620 PyModule_AddIntMacro(m, IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006621#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006622#ifdef IPPROTO_PIM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006623 PyModule_AddIntMacro(m, IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006624#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006625#ifdef IPPROTO_IPCOMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006626 PyModule_AddIntMacro(m, IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006627#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006628#ifdef IPPROTO_VRRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006629 PyModule_AddIntMacro(m, IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006630#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006631#ifdef IPPROTO_SCTP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006632 PyModule_AddIntMacro(m, IPPROTO_SCTP);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006633#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006634#ifdef IPPROTO_BIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006635 PyModule_AddIntMacro(m, IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006636#endif
6637/**/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006638#ifdef IPPROTO_RAW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006639 PyModule_AddIntMacro(m, IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00006640#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006641 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006642#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006643#ifdef IPPROTO_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006644 PyModule_AddIntMacro(m, IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006645#endif
6646
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006647#ifdef SYSPROTO_CONTROL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006648 PyModule_AddIntMacro(m, SYSPROTO_CONTROL);
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006649#endif
6650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006651 /* Some port configuration */
6652#ifdef IPPORT_RESERVED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006653 PyModule_AddIntMacro(m, IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006654#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006655 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006656#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006657#ifdef IPPORT_USERRESERVED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006658 PyModule_AddIntMacro(m, IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006659#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006660 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006661#endif
6662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006663 /* Some reserved IP v.4 addresses */
6664#ifdef INADDR_ANY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006665 PyModule_AddIntMacro(m, INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006666#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006667 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006668#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006669#ifdef INADDR_BROADCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006670 PyModule_AddIntMacro(m, INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006671#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006672 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006673#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006674#ifdef INADDR_LOOPBACK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006675 PyModule_AddIntMacro(m, INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006676#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006677 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006678#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006679#ifdef INADDR_UNSPEC_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006680 PyModule_AddIntMacro(m, INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006681#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006682 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006683#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006684#ifdef INADDR_ALLHOSTS_GROUP
6685 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
6686 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006687#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006688 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006689#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006690#ifdef INADDR_MAX_LOCAL_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006691 PyModule_AddIntMacro(m, INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006692#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006693 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006694#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006695#ifdef INADDR_NONE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006696 PyModule_AddIntMacro(m, INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006697#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006698 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006699#endif
6700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006701 /* IPv4 [gs]etsockopt options */
6702#ifdef IP_OPTIONS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006703 PyModule_AddIntMacro(m, IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006704#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006705#ifdef IP_HDRINCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006706 PyModule_AddIntMacro(m, IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006707#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006708#ifdef IP_TOS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006709 PyModule_AddIntMacro(m, IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006710#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006711#ifdef IP_TTL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006712 PyModule_AddIntMacro(m, IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006713#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006714#ifdef IP_RECVOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006715 PyModule_AddIntMacro(m, IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006716#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006717#ifdef IP_RECVRETOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006718 PyModule_AddIntMacro(m, IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006719#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006720#ifdef IP_RECVDSTADDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006721 PyModule_AddIntMacro(m, IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006722#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006723#ifdef IP_RETOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006724 PyModule_AddIntMacro(m, IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006725#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006726#ifdef IP_MULTICAST_IF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006727 PyModule_AddIntMacro(m, IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006728#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006729#ifdef IP_MULTICAST_TTL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006730 PyModule_AddIntMacro(m, IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006731#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006732#ifdef IP_MULTICAST_LOOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006733 PyModule_AddIntMacro(m, IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006734#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006735#ifdef IP_ADD_MEMBERSHIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006736 PyModule_AddIntMacro(m, IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006737#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006738#ifdef IP_DROP_MEMBERSHIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006739 PyModule_AddIntMacro(m, IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006740#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006741#ifdef IP_DEFAULT_MULTICAST_TTL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006742 PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00006743#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006744#ifdef IP_DEFAULT_MULTICAST_LOOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006745 PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006746#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006747#ifdef IP_MAX_MEMBERSHIPS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006748 PyModule_AddIntMacro(m, IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00006749#endif
Charles-François Natali564a42c2011-12-17 14:59:56 +01006750#ifdef IP_TRANSPARENT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006751 PyModule_AddIntMacro(m, IP_TRANSPARENT);
Charles-François Natali564a42c2011-12-17 14:59:56 +01006752#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006754 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
6755#ifdef IPV6_JOIN_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006756 PyModule_AddIntMacro(m, IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006757#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006758#ifdef IPV6_LEAVE_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006759 PyModule_AddIntMacro(m, IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006760#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006761#ifdef IPV6_MULTICAST_HOPS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006762 PyModule_AddIntMacro(m, IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006763#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006764#ifdef IPV6_MULTICAST_IF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006765 PyModule_AddIntMacro(m, IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006766#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006767#ifdef IPV6_MULTICAST_LOOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006768 PyModule_AddIntMacro(m, IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006769#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006770#ifdef IPV6_UNICAST_HOPS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006771 PyModule_AddIntMacro(m, IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006772#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006773 /* Additional IPV6 socket options, defined in RFC 3493 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00006774#ifdef IPV6_V6ONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006775 PyModule_AddIntMacro(m, IPV6_V6ONLY);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006776#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006777 /* Advanced IPV6 socket options, from RFC 3542 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00006778#ifdef IPV6_CHECKSUM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006779 PyModule_AddIntMacro(m, IPV6_CHECKSUM);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006780#endif
6781#ifdef IPV6_DONTFRAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006782 PyModule_AddIntMacro(m, IPV6_DONTFRAG);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006783#endif
6784#ifdef IPV6_DSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006785 PyModule_AddIntMacro(m, IPV6_DSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006786#endif
6787#ifdef IPV6_HOPLIMIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006788 PyModule_AddIntMacro(m, IPV6_HOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006789#endif
6790#ifdef IPV6_HOPOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006791 PyModule_AddIntMacro(m, IPV6_HOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006792#endif
6793#ifdef IPV6_NEXTHOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006794 PyModule_AddIntMacro(m, IPV6_NEXTHOP);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006795#endif
6796#ifdef IPV6_PATHMTU
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006797 PyModule_AddIntMacro(m, IPV6_PATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006798#endif
6799#ifdef IPV6_PKTINFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006800 PyModule_AddIntMacro(m, IPV6_PKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006801#endif
6802#ifdef IPV6_RECVDSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006803 PyModule_AddIntMacro(m, IPV6_RECVDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006804#endif
6805#ifdef IPV6_RECVHOPLIMIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006806 PyModule_AddIntMacro(m, IPV6_RECVHOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006807#endif
6808#ifdef IPV6_RECVHOPOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006809 PyModule_AddIntMacro(m, IPV6_RECVHOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006810#endif
6811#ifdef IPV6_RECVPKTINFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006812 PyModule_AddIntMacro(m, IPV6_RECVPKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006813#endif
6814#ifdef IPV6_RECVRTHDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006815 PyModule_AddIntMacro(m, IPV6_RECVRTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006816#endif
6817#ifdef IPV6_RECVTCLASS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006818 PyModule_AddIntMacro(m, IPV6_RECVTCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006819#endif
6820#ifdef IPV6_RTHDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006821 PyModule_AddIntMacro(m, IPV6_RTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006822#endif
6823#ifdef IPV6_RTHDRDSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006824 PyModule_AddIntMacro(m, IPV6_RTHDRDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006825#endif
6826#ifdef IPV6_RTHDR_TYPE_0
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006827 PyModule_AddIntMacro(m, IPV6_RTHDR_TYPE_0);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006828#endif
6829#ifdef IPV6_RECVPATHMTU
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006830 PyModule_AddIntMacro(m, IPV6_RECVPATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006831#endif
6832#ifdef IPV6_TCLASS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006833 PyModule_AddIntMacro(m, IPV6_TCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006834#endif
6835#ifdef IPV6_USE_MIN_MTU
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006836 PyModule_AddIntMacro(m, IPV6_USE_MIN_MTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006837#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006839 /* TCP options */
6840#ifdef TCP_NODELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006841 PyModule_AddIntMacro(m, TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00006842#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006843#ifdef TCP_MAXSEG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006844 PyModule_AddIntMacro(m, TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00006845#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006846#ifdef TCP_CORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006847 PyModule_AddIntMacro(m, TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006848#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006849#ifdef TCP_KEEPIDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006850 PyModule_AddIntMacro(m, TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006851#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006852#ifdef TCP_KEEPINTVL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006853 PyModule_AddIntMacro(m, TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006854#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006855#ifdef TCP_KEEPCNT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006856 PyModule_AddIntMacro(m, TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006857#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006858#ifdef TCP_SYNCNT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006859 PyModule_AddIntMacro(m, TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006860#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006861#ifdef TCP_LINGER2
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006862 PyModule_AddIntMacro(m, TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006863#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006864#ifdef TCP_DEFER_ACCEPT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006865 PyModule_AddIntMacro(m, TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006866#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006867#ifdef TCP_WINDOW_CLAMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006868 PyModule_AddIntMacro(m, TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006869#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006870#ifdef TCP_INFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006871 PyModule_AddIntMacro(m, TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006872#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006873#ifdef TCP_QUICKACK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006874 PyModule_AddIntMacro(m, TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006875#endif
Benjamin Peterson57b66722012-12-12 22:24:47 -05006876#ifdef TCP_FASTOPEN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006877 PyModule_AddIntMacro(m, TCP_FASTOPEN);
Benjamin Peterson57b66722012-12-12 22:24:47 -05006878#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006880 /* IPX options */
6881#ifdef IPX_TYPE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006882 PyModule_AddIntMacro(m, IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00006883#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00006884
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006885/* Reliable Datagram Sockets */
6886#ifdef RDS_CMSG_RDMA_ARGS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006887 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_ARGS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006888#endif
6889#ifdef RDS_CMSG_RDMA_DEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006890 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_DEST);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006891#endif
6892#ifdef RDS_CMSG_RDMA_MAP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006893 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_MAP);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006894#endif
6895#ifdef RDS_CMSG_RDMA_STATUS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006896 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_STATUS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006897#endif
6898#ifdef RDS_CMSG_RDMA_UPDATE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006899 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_UPDATE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006900#endif
6901#ifdef RDS_RDMA_READWRITE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006902 PyModule_AddIntMacro(m, RDS_RDMA_READWRITE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006903#endif
6904#ifdef RDS_RDMA_FENCE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006905 PyModule_AddIntMacro(m, RDS_RDMA_FENCE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006906#endif
6907#ifdef RDS_RDMA_INVALIDATE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006908 PyModule_AddIntMacro(m, RDS_RDMA_INVALIDATE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006909#endif
6910#ifdef RDS_RDMA_USE_ONCE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006911 PyModule_AddIntMacro(m, RDS_RDMA_USE_ONCE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006912#endif
6913#ifdef RDS_RDMA_DONTWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006914 PyModule_AddIntMacro(m, RDS_RDMA_DONTWAIT);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006915#endif
6916#ifdef RDS_RDMA_NOTIFY_ME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006917 PyModule_AddIntMacro(m, RDS_RDMA_NOTIFY_ME);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006918#endif
6919#ifdef RDS_RDMA_SILENT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006920 PyModule_AddIntMacro(m, RDS_RDMA_SILENT);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006921#endif
6922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006923 /* get{addr,name}info parameters */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006924#ifdef EAI_ADDRFAMILY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006925 PyModule_AddIntMacro(m, EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006926#endif
6927#ifdef EAI_AGAIN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006928 PyModule_AddIntMacro(m, EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006929#endif
6930#ifdef EAI_BADFLAGS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006931 PyModule_AddIntMacro(m, EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006932#endif
6933#ifdef EAI_FAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006934 PyModule_AddIntMacro(m, EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006935#endif
6936#ifdef EAI_FAMILY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006937 PyModule_AddIntMacro(m, EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006938#endif
6939#ifdef EAI_MEMORY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006940 PyModule_AddIntMacro(m, EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006941#endif
6942#ifdef EAI_NODATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006943 PyModule_AddIntMacro(m, EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006944#endif
6945#ifdef EAI_NONAME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006946 PyModule_AddIntMacro(m, EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006947#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00006948#ifdef EAI_OVERFLOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006949 PyModule_AddIntMacro(m, EAI_OVERFLOW);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006950#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006951#ifdef EAI_SERVICE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006952 PyModule_AddIntMacro(m, EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006953#endif
6954#ifdef EAI_SOCKTYPE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006955 PyModule_AddIntMacro(m, EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006956#endif
6957#ifdef EAI_SYSTEM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006958 PyModule_AddIntMacro(m, EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006959#endif
6960#ifdef EAI_BADHINTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006961 PyModule_AddIntMacro(m, EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006962#endif
6963#ifdef EAI_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006964 PyModule_AddIntMacro(m, EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006965#endif
6966#ifdef EAI_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006967 PyModule_AddIntMacro(m, EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006968#endif
6969#ifdef AI_PASSIVE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006970 PyModule_AddIntMacro(m, AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006971#endif
6972#ifdef AI_CANONNAME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006973 PyModule_AddIntMacro(m, AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006974#endif
6975#ifdef AI_NUMERICHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006976 PyModule_AddIntMacro(m, AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006977#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00006978#ifdef AI_NUMERICSERV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006979 PyModule_AddIntMacro(m, AI_NUMERICSERV);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006980#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006981#ifdef AI_MASK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006982 PyModule_AddIntMacro(m, AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006983#endif
6984#ifdef AI_ALL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006985 PyModule_AddIntMacro(m, AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006986#endif
6987#ifdef AI_V4MAPPED_CFG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006988 PyModule_AddIntMacro(m, AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006989#endif
6990#ifdef AI_ADDRCONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006991 PyModule_AddIntMacro(m, AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006992#endif
6993#ifdef AI_V4MAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006994 PyModule_AddIntMacro(m, AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006995#endif
6996#ifdef AI_DEFAULT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006997 PyModule_AddIntMacro(m, AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006998#endif
6999#ifdef NI_MAXHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007000 PyModule_AddIntMacro(m, NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007001#endif
7002#ifdef NI_MAXSERV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007003 PyModule_AddIntMacro(m, NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007004#endif
7005#ifdef NI_NOFQDN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007006 PyModule_AddIntMacro(m, NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007007#endif
7008#ifdef NI_NUMERICHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007009 PyModule_AddIntMacro(m, NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007010#endif
7011#ifdef NI_NAMEREQD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007012 PyModule_AddIntMacro(m, NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007013#endif
7014#ifdef NI_NUMERICSERV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007015 PyModule_AddIntMacro(m, NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007016#endif
7017#ifdef NI_DGRAM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007018 PyModule_AddIntMacro(m, NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00007019#endif
7020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007021 /* shutdown() parameters */
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007022#ifdef SHUT_RD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007023 PyModule_AddIntMacro(m, SHUT_RD);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007024#elif defined(SD_RECEIVE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007025 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007026#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007027 PyModule_AddIntConstant(m, "SHUT_RD", 0);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007028#endif
7029#ifdef SHUT_WR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007030 PyModule_AddIntMacro(m, SHUT_WR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007031#elif defined(SD_SEND)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007032 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007033#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007034 PyModule_AddIntConstant(m, "SHUT_WR", 1);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007035#endif
7036#ifdef SHUT_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007037 PyModule_AddIntMacro(m, SHUT_RDWR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007038#elif defined(SD_BOTH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007039 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007040#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007041 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00007042#endif
7043
Christian Heimesfaf2f632008-01-06 16:59:19 +00007044#ifdef SIO_RCVALL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007045 {
7046 DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS};
7047 const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"};
7048 int i;
Victor Stinner63941882011-09-29 00:42:28 +02007049 for(i = 0; i<Py_ARRAY_LENGTH(codes); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007050 PyObject *tmp;
7051 tmp = PyLong_FromUnsignedLong(codes[i]);
7052 if (tmp == NULL)
7053 return NULL;
7054 PyModule_AddObject(m, names[i], tmp);
7055 }
7056 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007057 PyModule_AddIntMacro(m, RCVALL_OFF);
7058 PyModule_AddIntMacro(m, RCVALL_ON);
7059 PyModule_AddIntMacro(m, RCVALL_SOCKETLEVELONLY);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00007060#ifdef RCVALL_IPLEVEL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007061 PyModule_AddIntMacro(m, RCVALL_IPLEVEL);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00007062#endif
7063#ifdef RCVALL_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02007064 PyModule_AddIntMacro(m, RCVALL_MAX);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00007065#endif
Christian Heimesfaf2f632008-01-06 16:59:19 +00007066#endif /* _MSTCPIP_ */
7067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007068 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00007069#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007070 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00007071#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007072 return m;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00007073}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007074
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007075
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007076#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +00007077#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007078
7079/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00007080/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007081
Guido van Rossum3eede5a2002-06-07 02:08:35 +00007082int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007083inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007085 if (af == AF_INET) {
Benjamin Petersonf91df042009-02-13 02:50:59 +00007086#if (SIZEOF_INT != 4)
7087#error "Not sure if in_addr_t exists and int is not 32-bits."
7088#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007089 unsigned int packed_addr;
7090 packed_addr = inet_addr(src);
7091 if (packed_addr == INADDR_NONE)
7092 return 0;
7093 memcpy(dst, &packed_addr, 4);
7094 return 1;
7095 }
7096 /* Should set errno to EAFNOSUPPORT */
7097 return -1;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007098}
7099
Martin v. Löwisc925b1532001-07-21 09:42:15 +00007100const char *
7101inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007103 if (af == AF_INET) {
7104 struct in_addr packed_addr;
7105 if (size < 16)
7106 /* Should set errno to ENOSPC. */
7107 return NULL;
7108 memcpy(&packed_addr, src, sizeof(packed_addr));
7109 return strncpy(dst, inet_ntoa(packed_addr), size);
7110 }
7111 /* Should set errno to EAFNOSUPPORT */
7112 return NULL;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007113}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00007114
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00007115#endif
Christian Heimesb6150692007-11-15 23:37:07 +00007116#endif