blob: a1e5da8bc0e3dd4a200b9e7370ba7dc69e40257b [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
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000036- socket.getaddrinfo(host, port [, family, socktype, proto, flags])
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 --> List of (family, socktype, proto, canonname, sockaddr)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000038- socket.getnameinfo(sockaddr, flags) --> (host, port)
Guido van Rossum27e177d1995-03-16 15:43:47 +000039- socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
Guido van Rossum47dfa4a2003-04-25 05:48:32 +000040- socket.has_ipv6: boolean value indicating if IPv6 is supported
Guido van Rossum5c9eb211999-08-20 18:21:51 +000041- socket.inet_aton(IP address) -> 32-bit packed IP representation
42- socket.inet_ntoa(packed IP) -> IP address string
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +000043- socket.getdefaulttimeout() -> None | float
44- socket.setdefaulttimeout(None | float)
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\
124listen(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
Jeremy Hylton22308652001-02-02 03:23:09 +0000287#endif
288
Skip Montanaro7befb992004-02-10 16:50:21 +0000289#include <stddef.h>
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000290
291#ifndef offsetof
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292# define offsetof(type, member) ((size_t)(&((type *)0)->member))
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000293#endif
294
Neal Norwitz39d22e52002-11-02 19:55:21 +0000295#ifndef O_NONBLOCK
296# define O_NONBLOCK O_NDELAY
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000297#endif
298
Trent Micka708d6e2004-09-07 17:48:26 +0000299/* include Python's addrinfo.h unless it causes trouble */
300#if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE)
301 /* Do not include addinfo.h on some newer IRIX versions.
302 * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21,
303 * for example, but not by 6.5.10.
304 */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000305#elif defined(_MSC_VER) && _MSC_VER>1201
Trent Micka708d6e2004-09-07 17:48:26 +0000306 /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and
307 * EAI_* constants are defined in (the already included) ws2tcpip.h.
308 */
309#else
310# include "addrinfo.h"
311#endif
Jason Tishlerc246cb72004-08-09 13:25:59 +0000312
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000313#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +0000314#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000315int inet_pton(int af, const char *src, void *dst);
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000316const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000317#endif
Christian Heimesb6150692007-11-15 23:37:07 +0000318#endif
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000319
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000320#ifdef __APPLE__
321/* On OS X, getaddrinfo returns no error indication of lookup
322 failure, so we must use the emulation instead of the libinfo
323 implementation. Unfortunately, performing an autoconf test
324 for this bug would require DNS access for the machine performing
325 the configuration, which is not acceptable. Therefore, we
326 determine the bug just by checking for __APPLE__. If this bug
327 gets ever fixed, perhaps checking for sys/version.h would be
328 appropriate, which is 10/0 on the system with the bug. */
Jack Jansen84262fb2002-07-02 14:40:42 +0000329#ifndef HAVE_GETNAMEINFO
330/* This bug seems to be fixed in Jaguar. Ths easiest way I could
331 Find to check for Jaguar is that it has getnameinfo(), which
332 older releases don't have */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000333#undef HAVE_GETADDRINFO
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000334#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000335
336#ifdef HAVE_INET_ATON
337#define USE_INET_ATON_WEAKLINK
338#endif
339
Jack Jansen84262fb2002-07-02 14:40:42 +0000340#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000341
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000342/* I know this is a bad practice, but it is the easiest... */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000343#if !defined(HAVE_GETADDRINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000344/* avoid clashes with the C library definition of the symbol. */
345#define getaddrinfo fake_getaddrinfo
346#define gai_strerror fake_gai_strerror
347#define freeaddrinfo fake_freeaddrinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000348#include "getaddrinfo.c"
349#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000350#if !defined(HAVE_GETNAMEINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000351#define getnameinfo fake_getnameinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000352#include "getnameinfo.c"
353#endif
354
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000355#ifdef MS_WINDOWS
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000356#define SOCKETCLOSE closesocket
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000357#endif
358
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000359#ifdef MS_WIN32
Kristján Valur Jónsson310052c2012-03-31 13:35:00 +0000360#undef EAFNOSUPPORT
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000361#define EAFNOSUPPORT WSAEAFNOSUPPORT
362#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000363#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000364
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000365#ifndef SOCKETCLOSE
366#define SOCKETCLOSE close
367#endif
368
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +0000369#if (defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)) && !defined(__NetBSD__) && !defined(__DragonFly__)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000370#define USE_BLUETOOTH 1
371#if defined(__FreeBSD__)
372#define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP
373#define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM
Thomas Wouterscf297e42007-02-23 15:07:44 +0000374#define BTPROTO_HCI BLUETOOTH_PROTO_HCI
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000375#define SOL_HCI SOL_HCI_RAW
376#define HCI_FILTER SO_HCI_RAW_FILTER
Hye-Shik Chang81268602004-02-02 06:05:24 +0000377#define sockaddr_l2 sockaddr_l2cap
378#define sockaddr_rc sockaddr_rfcomm
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000379#define hci_dev hci_node
Hye-Shik Chang81268602004-02-02 06:05:24 +0000380#define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb)
381#define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000382#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +0000383#elif defined(__NetBSD__) || defined(__DragonFly__)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000384#define sockaddr_l2 sockaddr_bt
385#define sockaddr_rc sockaddr_bt
Thomas Wouterscf297e42007-02-23 15:07:44 +0000386#define sockaddr_hci sockaddr_bt
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000387#define sockaddr_sco sockaddr_bt
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +0000388#define SOL_HCI BTPROTO_HCI
389#define HCI_DATA_DIR SO_HCI_DIRECTION
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000390#define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb)
391#define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000392#define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000393#define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000394#else
Hye-Shik Chang81268602004-02-02 06:05:24 +0000395#define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)
396#define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000397#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000398#define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)
399#endif
400#endif
401
Charles-François Natali8b759652011-12-23 16:44:51 +0100402/* Convert "sock_addr_t *" to "struct sockaddr *". */
403#define SAS2SA(x) (&((x)->sa))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000404
Martin v. Löwise9416172003-05-03 10:12:45 +0000405/*
406 * Constants for getnameinfo()
407 */
408#if !defined(NI_MAXHOST)
409#define NI_MAXHOST 1025
410#endif
411#if !defined(NI_MAXSERV)
412#define NI_MAXSERV 32
413#endif
414
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000415#ifndef INVALID_SOCKET /* MS defines this */
416#define INVALID_SOCKET (-1)
417#endif
418
Charles-François Natali0cc86852013-09-13 19:53:08 +0200419#ifndef INADDR_NONE
420#define INADDR_NONE (-1)
421#endif
422
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000423/* XXX There's a problem here: *static* functions are not supposed to have
424 a Py prefix (or use CapitalizedWords). Later... */
425
Guido van Rossum30a685f1991-06-27 15:51:29 +0000426/* Global variable holding the exception type for errors detected
427 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000428static PyObject *socket_herror;
429static PyObject *socket_gaierror;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000430static PyObject *socket_timeout;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000431
Tim Peters643a7fc2002-02-17 04:13:21 +0000432/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000433 The sock_type variable contains pointers to various functions,
434 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000435 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000436static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000437
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000438#if defined(HAVE_POLL_H)
439#include <poll.h>
440#elif defined(HAVE_SYS_POLL_H)
441#include <sys/poll.h>
442#endif
443
Nick Coghlan96fe56a2011-08-22 11:55:57 +1000444/* Largest value to try to store in a socklen_t (used when handling
445 ancillary data). POSIX requires socklen_t to hold at least
446 (2**31)-1 and recommends against storing larger values, but
447 socklen_t was originally int in the BSD interface, so to be on the
448 safe side we use the smaller of (2**31)-1 and INT_MAX. */
449#if INT_MAX > 0x7fffffff
450#define SOCKLEN_T_LIMIT 0x7fffffff
451#else
452#define SOCKLEN_T_LIMIT INT_MAX
453#endif
454
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200455#ifdef HAVE_POLL
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000456/* Instead of select(), we'll use poll() since poll() works on any fd. */
457#define IS_SELECTABLE(s) 1
458/* Can we call select() with this socket without a buffer overrun? */
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000459#else
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200460/* If there's no timeout left, we don't have to call select, so it's a safe,
461 * little white lie. */
462#define IS_SELECTABLE(s) (_PyIsSelectable_fd((s)->sock_fd) || (s)->sock_timeout <= 0.0)
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000463#endif
Neal Norwitz082b2df2006-02-07 07:04:46 +0000464
465static PyObject*
466select_error(void)
467{
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200468 PyErr_SetString(PyExc_OSError, "unable to select on socket");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 return NULL;
Neal Norwitz082b2df2006-02-07 07:04:46 +0000470}
471
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000472#ifdef MS_WINDOWS
Antoine Pitrouc8f01962010-09-28 22:03:27 +0000473#ifndef WSAEAGAIN
474#define WSAEAGAIN WSAEWOULDBLOCK
475#endif
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000476#define CHECK_ERRNO(expected) \
477 (WSAGetLastError() == WSA ## expected)
478#else
479#define CHECK_ERRNO(expected) \
480 (errno == expected)
481#endif
482
Victor Stinnerdaf45552013-08-28 00:53:59 +0200483#ifdef MS_WINDOWS
484/* Does WSASocket() support the WSA_FLAG_NO_HANDLE_INHERIT flag? */
485static int support_wsa_no_inherit = -1;
486#endif
487
Guido van Rossum30a685f1991-06-27 15:51:29 +0000488/* Convenience function to raise an error according to errno
489 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000490
Guido van Rossum73624e91994-10-10 17:59:00 +0000491static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000492set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000493{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000494#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 int err_no = WSAGetLastError();
496 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
497 recognizes the error codes used by both GetLastError() and
498 WSAGetLastError */
499 if (err_no)
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200500 return PyErr_SetExcFromWindowsErr(PyExc_OSError, err_no);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000501#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000502
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200503 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000504}
505
Guido van Rossum30a685f1991-06-27 15:51:29 +0000506
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000507static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000508set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000511
512#ifdef HAVE_HSTRERROR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000514#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 v = Py_BuildValue("(is)", h_error, "host not found");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000516#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 if (v != NULL) {
518 PyErr_SetObject(socket_herror, v);
519 Py_DECREF(v);
520 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000523}
524
525
526static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000527set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000530
Martin v. Löwis272cb402002-03-01 08:31:07 +0000531#ifdef EAI_SYSTEM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 /* EAI_SYSTEM is not available on Windows XP. */
533 if (error == EAI_SYSTEM)
534 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000535#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000536
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000537#ifdef HAVE_GAI_STRERROR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000539#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000541#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 if (v != NULL) {
543 PyErr_SetObject(socket_gaierror, v);
544 Py_DECREF(v);
545 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000548}
549
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000550/* Function to perform the setting of socket blocking mode
551 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000552static int
553internal_setblocking(PySocketSockObject *s, int block)
554{
Victor Stinner9a954832013-12-04 00:41:24 +0100555#if !defined(MS_WINDOWS) \
Christian Heimesaf01f662013-12-21 16:19:10 +0100556 && !((defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)))
Victor Stinner9a954832013-12-04 00:41:24 +0100557 int delay_flag, new_delay_flag;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000558#endif
Antoine Pitroub1c54962010-10-14 15:05:38 +0000559#ifdef SOCK_NONBLOCK
560 if (block)
561 s->sock_type &= (~SOCK_NONBLOCK);
562 else
563 s->sock_type |= SOCK_NONBLOCK;
564#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +0000565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 Py_BEGIN_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000567#ifndef MS_WINDOWS
Christian Heimesaf01f662013-12-21 16:19:10 +0100568#if (defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 block = !block;
570 ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);
Victor Stinner9a954832013-12-04 00:41:24 +0100571#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
573 if (block)
Victor Stinner9a954832013-12-04 00:41:24 +0100574 new_delay_flag = delay_flag & (~O_NONBLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 else
Victor Stinner9a954832013-12-04 00:41:24 +0100576 new_delay_flag = delay_flag | O_NONBLOCK;
577 if (new_delay_flag != delay_flag)
578 fcntl(s->sock_fd, F_SETFL, new_delay_flag);
579#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +0000580#else /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 block = !block;
582 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000583#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 /* Since these don't return anything */
587 return 1;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000588}
589
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000590/* Do a select()/poll() on the socket, if necessary (sock_timeout > 0).
Guido van Rossum11ba0942002-06-13 15:07:44 +0000591 The argument writing indicates the direction.
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000592 This does not raise an exception; we'll let our caller do that
593 after they've reacquired the interpreter lock.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000594 Returns 1 on timeout, -1 on error, 0 otherwise. */
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000595static int
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000596internal_select_ex(PySocketSockObject *s, int writing, double interval)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000597{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 int n;
Guido van Rossum11ba0942002-06-13 15:07:44 +0000599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 /* Nothing to do unless we're in timeout mode (not non-blocking) */
601 if (s->sock_timeout <= 0.0)
602 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 /* Guard against closed socket */
605 if (s->sock_fd < 0)
606 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000607
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000608 /* Handling this condition here simplifies the select loops */
609 if (interval < 0.0)
610 return 1;
611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 /* Prefer poll, if available, since you can poll() any fd
613 * which can't be done with select(). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000614#ifdef HAVE_POLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 {
616 struct pollfd pollfd;
617 int timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 pollfd.fd = s->sock_fd;
620 pollfd.events = writing ? POLLOUT : POLLIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 /* s->sock_timeout is in seconds, timeout in ms */
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000623 timeout = (int)(interval * 1000 + 0.5);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 n = poll(&pollfd, 1, timeout);
625 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000626#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 {
628 /* Construct the arguments to select */
629 fd_set fds;
630 struct timeval tv;
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000631 tv.tv_sec = (int)interval;
632 tv.tv_usec = (int)((interval - tv.tv_sec) * 1e6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 FD_ZERO(&fds);
634 FD_SET(s->sock_fd, &fds);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 /* See if the socket is ready */
637 if (writing)
Antoine Pitrou19467d22010-08-17 19:33:30 +0000638 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
639 NULL, &fds, NULL, &tv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 else
Antoine Pitrou19467d22010-08-17 19:33:30 +0000641 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
642 &fds, NULL, NULL, &tv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000644#endif
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 if (n < 0)
647 return -1;
648 if (n == 0)
649 return 1;
650 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000651}
652
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000653static int
654internal_select(PySocketSockObject *s, int writing)
655{
656 return internal_select_ex(s, writing, s->sock_timeout);
657}
658
659/*
660 Two macros for automatic retry of select() in case of false positives
661 (for example, select() could indicate a socket is ready for reading
662 but the data then discarded by the OS because of a wrong checksum).
663 Here is an example of use:
664
665 BEGIN_SELECT_LOOP(s)
666 Py_BEGIN_ALLOW_THREADS
667 timeout = internal_select_ex(s, 0, interval);
668 if (!timeout)
669 outlen = recv(s->sock_fd, cbuf, len, flags);
670 Py_END_ALLOW_THREADS
671 if (timeout == 1) {
672 PyErr_SetString(socket_timeout, "timed out");
673 return -1;
674 }
675 END_SELECT_LOOP(s)
676*/
677
678#define BEGIN_SELECT_LOOP(s) \
679 { \
680 _PyTime_timeval now, deadline = {0, 0}; \
681 double interval = s->sock_timeout; \
682 int has_timeout = s->sock_timeout > 0.0; \
683 if (has_timeout) { \
684 _PyTime_gettimeofday(&now); \
685 deadline = now; \
686 _PyTime_ADD_SECONDS(deadline, s->sock_timeout); \
687 } \
688 while (1) { \
689 errno = 0; \
690
691#define END_SELECT_LOOP(s) \
692 if (!has_timeout || \
693 (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \
694 break; \
695 _PyTime_gettimeofday(&now); \
696 interval = _PyTime_INTERVAL(now, deadline); \
697 } \
698 } \
699
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000700/* Initialize a new socket object. */
701
Tim Petersa12b4cf2002-07-18 22:38:44 +0000702static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000703
Martin v. Löwis1a214512008-06-11 05:26:20 +0000704static void
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000705init_sockobject(PySocketSockObject *s,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 SOCKET_T fd, int family, int type, int proto)
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 s->sock_fd = fd;
709 s->sock_family = family;
710 s->sock_type = type;
711 s->sock_proto = proto;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 s->errorhandler = &set_error;
Antoine Pitroub1c54962010-10-14 15:05:38 +0000714#ifdef SOCK_NONBLOCK
715 if (type & SOCK_NONBLOCK)
716 s->sock_timeout = 0.0;
717 else
718#endif
719 {
720 s->sock_timeout = defaulttimeout;
721 if (defaulttimeout >= 0.0)
722 internal_setblocking(s, 0);
723 }
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000724
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000725}
726
727
Guido van Rossum30a685f1991-06-27 15:51:29 +0000728/* Create a new socket object.
729 This just creates the object and initializes it.
730 If the creation fails, return NULL and set an exception (implicit
731 in NEWOBJ()). */
732
Guido van Rossum73624e91994-10-10 17:59:00 +0000733static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000734new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 PySocketSockObject *s;
737 s = (PySocketSockObject *)
738 PyType_GenericNew(&sock_type, NULL, NULL);
739 if (s != NULL)
740 init_sockobject(s, fd, family, type, proto);
741 return s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000742}
743
Guido van Rossum30a685f1991-06-27 15:51:29 +0000744
Guido van Rossum48a680c2001-03-02 06:34:14 +0000745/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000746 thread to be in gethostbyname or getaddrinfo */
747#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Matthias Klosed182a6c2012-08-14 17:29:04 +0200748static PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000749#endif
750
751
Guido van Rossum30a685f1991-06-27 15:51:29 +0000752/* Convert a string specifying a host name or one of a few symbolic
753 names to a numeric IP address. This usually calls gethostbyname()
754 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000755 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000756 an error occurred; then an exception is raised. */
757
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000758static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000759setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 struct addrinfo hints, *res;
762 int error;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
765 if (name[0] == '\0') {
766 int siz;
767 memset(&hints, 0, sizeof(hints));
768 hints.ai_family = af;
769 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
770 hints.ai_flags = AI_PASSIVE;
771 Py_BEGIN_ALLOW_THREADS
772 ACQUIRE_GETADDRINFO_LOCK
773 error = getaddrinfo(NULL, "0", &hints, &res);
774 Py_END_ALLOW_THREADS
775 /* We assume that those thread-unsafe getaddrinfo() versions
776 *are* safe regarding their return value, ie. that a
777 subsequent call to getaddrinfo() does not destroy the
778 outcome of the first call. */
779 RELEASE_GETADDRINFO_LOCK
780 if (error) {
781 set_gaierror(error);
782 return -1;
783 }
784 switch (res->ai_family) {
785 case AF_INET:
786 siz = 4;
787 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000788#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 case AF_INET6:
790 siz = 16;
791 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000792#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 default:
794 freeaddrinfo(res);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200795 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 "unsupported address family");
797 return -1;
798 }
799 if (res->ai_next) {
800 freeaddrinfo(res);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200801 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 "wildcard resolved to multiple address");
803 return -1;
804 }
805 if (res->ai_addrlen < addr_ret_size)
806 addr_ret_size = res->ai_addrlen;
807 memcpy(addr_ret, res->ai_addr, addr_ret_size);
808 freeaddrinfo(res);
809 return siz;
810 }
Charles-François Natali0cc86852013-09-13 19:53:08 +0200811 /* special-case broadcast - inet_addr() below can return INADDR_NONE for
Victor Stinnere990c6e2013-11-16 00:18:58 +0100812 * this */
Charles-François Natali0cc86852013-09-13 19:53:08 +0200813 if (strcmp(name, "255.255.255.255") == 0 ||
814 strcmp(name, "<broadcast>") == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 struct sockaddr_in *sin;
816 if (af != AF_INET && af != AF_UNSPEC) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200817 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 "address family mismatched");
819 return -1;
820 }
821 sin = (struct sockaddr_in *)addr_ret;
822 memset((void *) sin, '\0', sizeof(*sin));
823 sin->sin_family = AF_INET;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000824#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 sin->sin_len = sizeof(*sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000826#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 sin->sin_addr.s_addr = INADDR_BROADCAST;
828 return sizeof(sin->sin_addr);
829 }
Charles-François Natali0cc86852013-09-13 19:53:08 +0200830
831 /* avoid a name resolution in case of numeric address */
832#ifdef HAVE_INET_PTON
833 /* check for an IPv4 address */
834 if (af == AF_UNSPEC || af == AF_INET) {
835 struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret;
836 memset(sin, 0, sizeof(*sin));
837 if (inet_pton(AF_INET, name, &sin->sin_addr) > 0) {
838 sin->sin_family = AF_INET;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000839#ifdef HAVE_SOCKADDR_SA_LEN
Charles-François Natali0cc86852013-09-13 19:53:08 +0200840 sin->sin_len = sizeof(*sin);
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000841#endif
Charles-François Natali0cc86852013-09-13 19:53:08 +0200842 return 4;
843 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 }
Charles-François Natali0cc86852013-09-13 19:53:08 +0200845#ifdef ENABLE_IPV6
846 /* check for an IPv6 address - if the address contains a scope ID, we
847 * fallback to getaddrinfo(), which can handle translation from interface
848 * name to interface index */
849 if ((af == AF_UNSPEC || af == AF_INET6) && !strchr(name, '%')) {
850 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)addr_ret;
851 memset(sin, 0, sizeof(*sin));
852 if (inet_pton(AF_INET6, name, &sin->sin6_addr) > 0) {
853 sin->sin6_family = AF_INET6;
854#ifdef HAVE_SOCKADDR_SA_LEN
855 sin->sin6_len = sizeof(*sin);
856#endif
857 return 16;
858 }
859 }
860#endif /* ENABLE_IPV6 */
861#else /* HAVE_INET_PTON */
862 /* check for an IPv4 address */
863 if (af == AF_INET || af == AF_UNSPEC) {
864 struct sockaddr_in *sin = (struct sockaddr_in *)addr_ret;
865 memset(sin, 0, sizeof(*sin));
866 if ((sin->sin_addr.s_addr = inet_addr(name)) != INADDR_NONE) {
867 sin->sin_family = AF_INET;
868#ifdef HAVE_SOCKADDR_SA_LEN
869 sin->sin_len = sizeof(*sin);
870#endif
871 return 4;
872 }
Victor Stinnere990c6e2013-11-16 00:18:58 +0100873 }
Charles-François Natali0cc86852013-09-13 19:53:08 +0200874#endif /* HAVE_INET_PTON */
875
876 /* perform a name resolution */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 memset(&hints, 0, sizeof(hints));
878 hints.ai_family = af;
879 Py_BEGIN_ALLOW_THREADS
880 ACQUIRE_GETADDRINFO_LOCK
881 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000882#if defined(__digital__) && defined(__unix__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 if (error == EAI_NONAME && af == AF_UNSPEC) {
884 /* On Tru64 V5.1, numeric-to-addr conversion fails
885 if no address family is given. Assume IPv4 for now.*/
886 hints.ai_family = AF_INET;
887 error = getaddrinfo(name, NULL, &hints, &res);
888 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000889#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 Py_END_ALLOW_THREADS
891 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
892 if (error) {
893 set_gaierror(error);
894 return -1;
895 }
896 if (res->ai_addrlen < addr_ret_size)
897 addr_ret_size = res->ai_addrlen;
898 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
899 freeaddrinfo(res);
900 switch (addr_ret->sa_family) {
901 case AF_INET:
902 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000903#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 case AF_INET6:
905 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000906#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200908 PyErr_SetString(PyExc_OSError, "unknown address family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 return -1;
910 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000911}
912
Guido van Rossum30a685f1991-06-27 15:51:29 +0000913
Guido van Rossum30a685f1991-06-27 15:51:29 +0000914/* Create a string object representing an IP address.
915 This is always a string of the form 'dd.dd.dd.dd' (with variable
916 size numbers). */
917
Guido van Rossum73624e91994-10-10 17:59:00 +0000918static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000919makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000920{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 char buf[NI_MAXHOST];
922 int error;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
925 NI_NUMERICHOST);
926 if (error) {
927 set_gaierror(error);
928 return NULL;
929 }
930 return PyUnicode_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000931}
932
933
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000934#ifdef USE_BLUETOOTH
935/* Convert a string representation of a Bluetooth address into a numeric
936 address. Returns the length (6), or raises an exception and returns -1 if
937 an error occurred. */
938
939static int
940setbdaddr(char *name, bdaddr_t *bdaddr)
941{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 unsigned int b0, b1, b2, b3, b4, b5;
943 char ch;
944 int n;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
947 &b5, &b4, &b3, &b2, &b1, &b0, &ch);
948 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
949 bdaddr->b[0] = b0;
950 bdaddr->b[1] = b1;
951 bdaddr->b[2] = b2;
952 bdaddr->b[3] = b3;
953 bdaddr->b[4] = b4;
954 bdaddr->b[5] = b5;
955 return 6;
956 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200957 PyErr_SetString(PyExc_OSError, "bad bluetooth address");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 return -1;
959 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000960}
961
962/* Create a string representation of the Bluetooth address. This is always a
963 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
964 value (zero padded if necessary). */
965
966static PyObject *
967makebdaddr(bdaddr_t *bdaddr)
968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 char buf[(6 * 2) + 5 + 1];
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
972 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
973 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
974 return PyUnicode_FromString(buf);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000975}
976#endif
977
978
Guido van Rossum30a685f1991-06-27 15:51:29 +0000979/* Create an object representing the given socket address,
980 suitable for passing it back to bind(), connect() etc.
981 The family field of the sockaddr structure is inspected
982 to determine what kind of address it really is. */
983
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000984/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000985static PyObject *
Antoine Pitrou19467d22010-08-17 19:33:30 +0000986makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000987{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 if (addrlen == 0) {
989 /* No address -- may be recvfrom() from known socket */
990 Py_INCREF(Py_None);
991 return Py_None;
992 }
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 switch (addr->sa_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +0000995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 case AF_INET:
997 {
998 struct sockaddr_in *a;
999 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
1000 PyObject *ret = NULL;
1001 if (addrobj) {
1002 a = (struct sockaddr_in *)addr;
1003 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
1004 Py_DECREF(addrobj);
1005 }
1006 return ret;
1007 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00001008
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001009#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 case AF_UNIX:
1011 {
1012 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001013#ifdef linux
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 if (a->sun_path[0] == 0) { /* Linux abstract namespace */
1015 addrlen -= offsetof(struct sockaddr_un, sun_path);
Charles-François Natalib10c71d2013-05-21 10:45:46 +02001016 return PyBytes_FromStringAndSize(a->sun_path, addrlen);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 }
1018 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001019#endif /* linux */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 {
1021 /* regular NULL-terminated string */
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001022 return PyUnicode_DecodeFSDefault(a->sun_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 }
1024 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001025#endif /* AF_UNIX */
1026
Martin v. Löwis11017b12006-01-14 18:12:57 +00001027#if defined(AF_NETLINK)
1028 case AF_NETLINK:
1029 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
1031 return Py_BuildValue("II", a->nl_pid, a->nl_groups);
Martin v. Löwis11017b12006-01-14 18:12:57 +00001032 }
1033#endif /* AF_NETLINK */
1034
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001035#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 case AF_INET6:
1037 {
1038 struct sockaddr_in6 *a;
1039 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
1040 PyObject *ret = NULL;
1041 if (addrobj) {
1042 a = (struct sockaddr_in6 *)addr;
Charles-François Natali366999a2012-01-02 15:47:29 +01001043 ret = Py_BuildValue("OiII",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 addrobj,
1045 ntohs(a->sin6_port),
Charles-François Natali366999a2012-01-02 15:47:29 +01001046 ntohl(a->sin6_flowinfo),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 a->sin6_scope_id);
1048 Py_DECREF(addrobj);
1049 }
1050 return ret;
1051 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001052#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001053
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001054#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 case AF_BLUETOOTH:
1056 switch (proto) {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 case BTPROTO_L2CAP:
1059 {
1060 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
1061 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
1062 PyObject *ret = NULL;
1063 if (addrobj) {
1064 ret = Py_BuildValue("Oi",
1065 addrobj,
1066 _BT_L2_MEMB(a, psm));
1067 Py_DECREF(addrobj);
1068 }
1069 return ret;
1070 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 case BTPROTO_RFCOMM:
1073 {
1074 struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
1075 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
1076 PyObject *ret = NULL;
1077 if (addrobj) {
1078 ret = Py_BuildValue("Oi",
1079 addrobj,
1080 _BT_RC_MEMB(a, channel));
1081 Py_DECREF(addrobj);
1082 }
1083 return ret;
1084 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 case BTPROTO_HCI:
1087 {
1088 struct sockaddr_hci *a = (struct sockaddr_hci *) addr;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001089#if defined(__NetBSD__) || defined(__DragonFly__)
1090 return makebdaddr(&_BT_HCI_MEMB(a, bdaddr));
1091#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 PyObject *ret = NULL;
1093 ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
1094 return ret;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001095#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 }
Thomas Wouterscf297e42007-02-23 15:07:44 +00001097
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001098#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 case BTPROTO_SCO:
1100 {
1101 struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
1102 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
1103 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001104#endif
1105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 default:
1107 PyErr_SetString(PyExc_ValueError,
1108 "Unknown Bluetooth protocol");
1109 return NULL;
1110 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001111#endif
1112
Antoine Pitroub156a462010-10-27 20:13:57 +00001113#if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 case AF_PACKET:
1115 {
1116 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
1117 char *ifname = "";
1118 struct ifreq ifr;
1119 /* need to look up interface name give index */
1120 if (a->sll_ifindex) {
1121 ifr.ifr_ifindex = a->sll_ifindex;
1122 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
1123 ifname = ifr.ifr_name;
1124 }
1125 return Py_BuildValue("shbhy#",
1126 ifname,
1127 ntohs(a->sll_protocol),
1128 a->sll_pkttype,
1129 a->sll_hatype,
1130 a->sll_addr,
1131 a->sll_halen);
1132 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001133#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001134
Christian Heimes043d6f62008-01-07 17:19:16 +00001135#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 case AF_TIPC:
1137 {
1138 struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr;
1139 if (a->addrtype == TIPC_ADDR_NAMESEQ) {
1140 return Py_BuildValue("IIIII",
1141 a->addrtype,
1142 a->addr.nameseq.type,
1143 a->addr.nameseq.lower,
1144 a->addr.nameseq.upper,
1145 a->scope);
1146 } else if (a->addrtype == TIPC_ADDR_NAME) {
1147 return Py_BuildValue("IIIII",
1148 a->addrtype,
1149 a->addr.name.name.type,
1150 a->addr.name.name.instance,
1151 a->addr.name.name.instance,
1152 a->scope);
1153 } else if (a->addrtype == TIPC_ADDR_ID) {
1154 return Py_BuildValue("IIIII",
1155 a->addrtype,
1156 a->addr.id.node,
1157 a->addr.id.ref,
1158 0,
1159 a->scope);
1160 } else {
1161 PyErr_SetString(PyExc_ValueError,
1162 "Invalid address type");
1163 return NULL;
1164 }
1165 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001166#endif
1167
Charles-François Natalib4062e82014-02-08 22:54:11 +01001168#ifdef HAVE_LINUX_CAN_H
Charles-François Natali47413c12011-10-06 19:47:44 +02001169 case AF_CAN:
1170 {
1171 struct sockaddr_can *a = (struct sockaddr_can *)addr;
1172 char *ifname = "";
1173 struct ifreq ifr;
1174 /* need to look up interface name given index */
1175 if (a->can_ifindex) {
1176 ifr.ifr_ifindex = a->can_ifindex;
1177 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
1178 ifname = ifr.ifr_name;
1179 }
1180
1181 return Py_BuildValue("O&h", PyUnicode_DecodeFSDefault,
1182 ifname,
1183 a->can_family);
1184 }
1185#endif
1186
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001187#ifdef PF_SYSTEM
1188 case PF_SYSTEM:
1189 switch(proto) {
1190#ifdef SYSPROTO_CONTROL
1191 case SYSPROTO_CONTROL:
1192 {
1193 struct sockaddr_ctl *a = (struct sockaddr_ctl *)addr;
1194 return Py_BuildValue("(II)", a->sc_id, a->sc_unit);
1195 }
1196#endif
1197 default:
1198 PyErr_SetString(PyExc_ValueError,
1199 "Invalid address type");
1200 return 0;
1201 }
1202#endif
1203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 default:
1207 /* If we don't know the address family, don't raise an
1208 exception -- return it as an (int, bytes) tuple. */
1209 return Py_BuildValue("iy#",
1210 addr->sa_family,
1211 addr->sa_data,
1212 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001215}
1216
Guido van Rossum30a685f1991-06-27 15:51:29 +00001217
1218/* Parse a socket address argument according to the socket object's
1219 address family. Return 1 if the address was in the proper format,
1220 0 of not. The address is returned through addr_ret, its length
1221 through len_ret. */
1222
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001223static int
Guido van Rossum48a680c2001-03-02 06:34:14 +00001224getsockaddrarg(PySocketSockObject *s, PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 struct sockaddr *addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 switch (s->sock_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +00001228
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001229#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 case AF_UNIX:
1231 {
1232 struct sockaddr_un* addr;
1233 char *path;
1234 int len;
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001235 int retval = 0;
1236
1237 /* PEP 383. Not using PyUnicode_FSConverter since we need to
1238 allow embedded nulls on Linux. */
1239 if (PyUnicode_Check(args)) {
1240 if ((args = PyUnicode_EncodeFSDefault(args)) == NULL)
1241 return 0;
1242 }
1243 else
1244 Py_INCREF(args);
1245 if (!PyArg_Parse(args, "y#", &path, &len))
1246 goto unix_out;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 addr = (struct sockaddr_un*)addr_ret;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001249#ifdef linux
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 if (len > 0 && path[0] == 0) {
1251 /* Linux abstract namespace extension */
1252 if (len > sizeof addr->sun_path) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001253 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 "AF_UNIX path too long");
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001255 goto unix_out;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 }
1257 }
1258 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001259#endif /* linux */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 {
1261 /* regular NULL-terminated string */
1262 if (len >= sizeof addr->sun_path) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001263 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 "AF_UNIX path too long");
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001265 goto unix_out;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 }
1267 addr->sun_path[len] = 0;
1268 }
1269 addr->sun_family = s->sock_family;
1270 memcpy(addr->sun_path, path, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 *len_ret = len + offsetof(struct sockaddr_un, sun_path);
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001272 retval = 1;
1273 unix_out:
1274 Py_DECREF(args);
1275 return retval;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001277#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001278
Martin v. Löwis11017b12006-01-14 18:12:57 +00001279#if defined(AF_NETLINK)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 case AF_NETLINK:
1281 {
1282 struct sockaddr_nl* addr;
1283 int pid, groups;
1284 addr = (struct sockaddr_nl *)addr_ret;
1285 if (!PyTuple_Check(args)) {
1286 PyErr_Format(
1287 PyExc_TypeError,
1288 "getsockaddrarg: "
1289 "AF_NETLINK address must be tuple, not %.500s",
1290 Py_TYPE(args)->tp_name);
1291 return 0;
1292 }
1293 if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
1294 return 0;
1295 addr->nl_family = AF_NETLINK;
1296 addr->nl_pid = pid;
1297 addr->nl_groups = groups;
1298 *len_ret = sizeof(*addr);
1299 return 1;
1300 }
Martin v. Löwis11017b12006-01-14 18:12:57 +00001301#endif
1302
Charles-François Natali10b8cf42011-11-10 19:21:37 +01001303#ifdef AF_RDS
1304 case AF_RDS:
1305 /* RDS sockets use sockaddr_in: fall-through */
1306#endif
1307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 case AF_INET:
1309 {
1310 struct sockaddr_in* addr;
1311 char *host;
1312 int port, result;
1313 if (!PyTuple_Check(args)) {
1314 PyErr_Format(
1315 PyExc_TypeError,
1316 "getsockaddrarg: "
1317 "AF_INET address must be tuple, not %.500s",
1318 Py_TYPE(args)->tp_name);
1319 return 0;
1320 }
1321 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
1322 "idna", &host, &port))
1323 return 0;
1324 addr=(struct sockaddr_in*)addr_ret;
1325 result = setipaddr(host, (struct sockaddr *)addr,
1326 sizeof(*addr), AF_INET);
1327 PyMem_Free(host);
1328 if (result < 0)
1329 return 0;
1330 if (port < 0 || port > 0xffff) {
1331 PyErr_SetString(
1332 PyExc_OverflowError,
1333 "getsockaddrarg: port must be 0-65535.");
1334 return 0;
1335 }
1336 addr->sin_family = AF_INET;
1337 addr->sin_port = htons((short)port);
1338 *len_ret = sizeof *addr;
1339 return 1;
1340 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001341
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001342#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 case AF_INET6:
1344 {
1345 struct sockaddr_in6* addr;
1346 char *host;
Charles-François Natali366999a2012-01-02 15:47:29 +01001347 int port, result;
1348 unsigned int flowinfo, scope_id;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 flowinfo = scope_id = 0;
1350 if (!PyTuple_Check(args)) {
1351 PyErr_Format(
1352 PyExc_TypeError,
1353 "getsockaddrarg: "
1354 "AF_INET6 address must be tuple, not %.500s",
1355 Py_TYPE(args)->tp_name);
1356 return 0;
1357 }
Charles-François Natali366999a2012-01-02 15:47:29 +01001358 if (!PyArg_ParseTuple(args, "eti|II",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 "idna", &host, &port, &flowinfo,
1360 &scope_id)) {
1361 return 0;
1362 }
1363 addr = (struct sockaddr_in6*)addr_ret;
1364 result = setipaddr(host, (struct sockaddr *)addr,
1365 sizeof(*addr), AF_INET6);
1366 PyMem_Free(host);
1367 if (result < 0)
1368 return 0;
1369 if (port < 0 || port > 0xffff) {
1370 PyErr_SetString(
1371 PyExc_OverflowError,
1372 "getsockaddrarg: port must be 0-65535.");
1373 return 0;
1374 }
Charles-François Natali9b0c0062012-06-23 10:17:05 +02001375 if (flowinfo > 0xfffff) {
Charles-François Natali366999a2012-01-02 15:47:29 +01001376 PyErr_SetString(
1377 PyExc_OverflowError,
1378 "getsockaddrarg: flowinfo must be 0-1048575.");
1379 return 0;
1380 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 addr->sin6_family = s->sock_family;
1382 addr->sin6_port = htons((short)port);
Charles-François Natali366999a2012-01-02 15:47:29 +01001383 addr->sin6_flowinfo = htonl(flowinfo);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 addr->sin6_scope_id = scope_id;
1385 *len_ret = sizeof *addr;
1386 return 1;
1387 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001388#endif
1389
Hye-Shik Chang81268602004-02-02 06:05:24 +00001390#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 case AF_BLUETOOTH:
1392 {
1393 switch (s->sock_proto) {
1394 case BTPROTO_L2CAP:
1395 {
1396 struct sockaddr_l2 *addr;
1397 char *straddr;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 addr = (struct sockaddr_l2 *)addr_ret;
1400 memset(addr, 0, sizeof(struct sockaddr_l2));
1401 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
1402 if (!PyArg_ParseTuple(args, "si", &straddr,
1403 &_BT_L2_MEMB(addr, psm))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001404 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 "wrong format");
1406 return 0;
1407 }
1408 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
1409 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 *len_ret = sizeof *addr;
1412 return 1;
1413 }
1414 case BTPROTO_RFCOMM:
1415 {
1416 struct sockaddr_rc *addr;
1417 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 addr = (struct sockaddr_rc *)addr_ret;
1420 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
1421 if (!PyArg_ParseTuple(args, "si", &straddr,
1422 &_BT_RC_MEMB(addr, channel))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001423 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 "wrong format");
1425 return 0;
1426 }
1427 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
1428 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 *len_ret = sizeof *addr;
1431 return 1;
1432 }
1433 case BTPROTO_HCI:
1434 {
1435 struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001436#if defined(__NetBSD__) || defined(__DragonFly__)
Alexander Belopolskye239d232010-12-08 23:31:48 +00001437 char *straddr = PyBytes_AS_STRING(args);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001438
Alexander Belopolskye239d232010-12-08 23:31:48 +00001439 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001440 if (straddr == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001441 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001442 "wrong format");
1443 return 0;
1444 }
1445 if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0)
1446 return 0;
1447#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
1449 if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001450 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 "wrong format");
1452 return 0;
1453 }
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001454#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 *len_ret = sizeof *addr;
1456 return 1;
1457 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001458#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 case BTPROTO_SCO:
1460 {
1461 struct sockaddr_sco *addr;
1462 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 addr = (struct sockaddr_sco *)addr_ret;
1465 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
1466 if (!PyBytes_Check(args)) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001467 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 "wrong format");
1469 return 0;
1470 }
1471 straddr = PyBytes_AS_STRING(args);
1472 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
1473 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 *len_ret = sizeof *addr;
1476 return 1;
1477 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001478#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001480 PyErr_SetString(PyExc_OSError, "getsockaddrarg: unknown Bluetooth protocol");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 return 0;
1482 }
1483 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001484#endif
1485
Antoine Pitroub156a462010-10-27 20:13:57 +00001486#if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 case AF_PACKET:
1488 {
1489 struct sockaddr_ll* addr;
1490 struct ifreq ifr;
1491 char *interfaceName;
1492 int protoNumber;
1493 int hatype = 0;
1494 int pkttype = 0;
1495 char *haddr = NULL;
1496 unsigned int halen = 0;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 if (!PyTuple_Check(args)) {
1499 PyErr_Format(
1500 PyExc_TypeError,
1501 "getsockaddrarg: "
1502 "AF_PACKET address must be tuple, not %.500s",
1503 Py_TYPE(args)->tp_name);
1504 return 0;
1505 }
1506 if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName,
1507 &protoNumber, &pkttype, &hatype,
1508 &haddr, &halen))
1509 return 0;
1510 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1511 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
1512 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
1513 s->errorhandler();
1514 return 0;
1515 }
1516 if (halen > 8) {
1517 PyErr_SetString(PyExc_ValueError,
1518 "Hardware address must be 8 bytes or less");
1519 return 0;
1520 }
1521 if (protoNumber < 0 || protoNumber > 0xffff) {
1522 PyErr_SetString(
1523 PyExc_OverflowError,
1524 "getsockaddrarg: protoNumber must be 0-65535.");
1525 return 0;
1526 }
1527 addr = (struct sockaddr_ll*)addr_ret;
1528 addr->sll_family = AF_PACKET;
1529 addr->sll_protocol = htons((short)protoNumber);
1530 addr->sll_ifindex = ifr.ifr_ifindex;
1531 addr->sll_pkttype = pkttype;
1532 addr->sll_hatype = hatype;
1533 if (halen != 0) {
1534 memcpy(&addr->sll_addr, haddr, halen);
1535 }
1536 addr->sll_halen = halen;
1537 *len_ret = sizeof *addr;
1538 return 1;
1539 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001540#endif
1541
Christian Heimes043d6f62008-01-07 17:19:16 +00001542#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 case AF_TIPC:
1544 {
1545 unsigned int atype, v1, v2, v3;
1546 unsigned int scope = TIPC_CLUSTER_SCOPE;
1547 struct sockaddr_tipc *addr;
Christian Heimes043d6f62008-01-07 17:19:16 +00001548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 if (!PyTuple_Check(args)) {
1550 PyErr_Format(
1551 PyExc_TypeError,
1552 "getsockaddrarg: "
1553 "AF_TIPC address must be tuple, not %.500s",
1554 Py_TYPE(args)->tp_name);
1555 return 0;
1556 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 if (!PyArg_ParseTuple(args,
1559 "IIII|I;Invalid TIPC address format",
1560 &atype, &v1, &v2, &v3, &scope))
1561 return 0;
Christian Heimes043d6f62008-01-07 17:19:16 +00001562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 addr = (struct sockaddr_tipc *) addr_ret;
1564 memset(addr, 0, sizeof(struct sockaddr_tipc));
Christian Heimes043d6f62008-01-07 17:19:16 +00001565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 addr->family = AF_TIPC;
1567 addr->scope = scope;
1568 addr->addrtype = atype;
Christian Heimes043d6f62008-01-07 17:19:16 +00001569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 if (atype == TIPC_ADDR_NAMESEQ) {
1571 addr->addr.nameseq.type = v1;
1572 addr->addr.nameseq.lower = v2;
1573 addr->addr.nameseq.upper = v3;
1574 } else if (atype == TIPC_ADDR_NAME) {
1575 addr->addr.name.name.type = v1;
1576 addr->addr.name.name.instance = v2;
1577 } else if (atype == TIPC_ADDR_ID) {
1578 addr->addr.id.node = v1;
1579 addr->addr.id.ref = v2;
1580 } else {
1581 /* Shouldn't happen */
1582 PyErr_SetString(PyExc_TypeError, "Invalid address type");
1583 return 0;
1584 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 *len_ret = sizeof(*addr);
Christian Heimes043d6f62008-01-07 17:19:16 +00001587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 return 1;
1589 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001590#endif
1591
Charles-François Natalib4062e82014-02-08 22:54:11 +01001592#ifdef HAVE_LINUX_CAN_H
Charles-François Natali47413c12011-10-06 19:47:44 +02001593 case AF_CAN:
1594 switch (s->sock_proto) {
1595 case CAN_RAW:
Charles-François Natali773e42d2013-02-05 19:42:01 +01001596 /* fall-through */
1597 case CAN_BCM:
Charles-François Natali47413c12011-10-06 19:47:44 +02001598 {
1599 struct sockaddr_can *addr;
1600 PyObject *interfaceName;
1601 struct ifreq ifr;
Charles-François Natali47413c12011-10-06 19:47:44 +02001602 Py_ssize_t len;
1603
Benjamin Peterson18b71912013-05-16 15:29:44 -05001604 addr = (struct sockaddr_can *)addr_ret;
1605
Charles-François Natali47413c12011-10-06 19:47:44 +02001606 if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter,
1607 &interfaceName))
1608 return 0;
1609
1610 len = PyBytes_GET_SIZE(interfaceName);
1611
1612 if (len == 0) {
1613 ifr.ifr_ifindex = 0;
1614 } else if (len < sizeof(ifr.ifr_name)) {
Christian Heimes15b68852012-09-10 01:25:50 +02001615 strncpy(ifr.ifr_name, PyBytes_AS_STRING(interfaceName), sizeof(ifr.ifr_name));
1616 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Charles-François Natali47413c12011-10-06 19:47:44 +02001617 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
1618 s->errorhandler();
1619 Py_DECREF(interfaceName);
1620 return 0;
1621 }
1622 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001623 PyErr_SetString(PyExc_OSError,
Charles-François Natali47413c12011-10-06 19:47:44 +02001624 "AF_CAN interface name too long");
1625 Py_DECREF(interfaceName);
1626 return 0;
1627 }
1628
1629 addr->can_family = AF_CAN;
1630 addr->can_ifindex = ifr.ifr_ifindex;
1631
1632 *len_ret = sizeof(*addr);
1633 Py_DECREF(interfaceName);
1634 return 1;
1635 }
1636 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001637 PyErr_SetString(PyExc_OSError,
Charles-François Natali47413c12011-10-06 19:47:44 +02001638 "getsockaddrarg: unsupported CAN protocol");
1639 return 0;
1640 }
1641#endif
Victor Stinnera534fc42013-06-03 22:07:27 +02001642
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001643#ifdef PF_SYSTEM
1644 case PF_SYSTEM:
1645 switch (s->sock_proto) {
1646#ifdef SYSPROTO_CONTROL
1647 case SYSPROTO_CONTROL:
1648 {
1649 struct sockaddr_ctl *addr;
Victor Stinnera534fc42013-06-03 22:07:27 +02001650
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001651 addr = (struct sockaddr_ctl *)addr_ret;
1652 addr->sc_family = AF_SYSTEM;
Victor Stinnera534fc42013-06-03 22:07:27 +02001653 addr->ss_sysaddr = AF_SYS_CONTROL;
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001654
1655 if (PyUnicode_Check(args)) {
1656 struct ctl_info info;
1657 PyObject *ctl_name;
1658
1659 if (!PyArg_Parse(args, "O&",
1660 PyUnicode_FSConverter, &ctl_name)) {
1661 return 0;
1662 }
1663
1664 if (PyBytes_GET_SIZE(ctl_name) > sizeof(info.ctl_name)) {
1665 PyErr_SetString(PyExc_ValueError,
1666 "provided string is too long");
1667 Py_DECREF(ctl_name);
1668 return 0;
1669 }
1670 strncpy(info.ctl_name, PyBytes_AS_STRING(ctl_name),
1671 sizeof(info.ctl_name));
1672 Py_DECREF(ctl_name);
1673
1674 if (ioctl(s->sock_fd, CTLIOCGINFO, &info)) {
1675 PyErr_SetString(PyExc_OSError,
1676 "cannot find kernel control with provided name");
1677 return 0;
1678 }
Victor Stinnera534fc42013-06-03 22:07:27 +02001679
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001680 addr->sc_id = info.ctl_id;
1681 addr->sc_unit = 0;
1682 } else if (!PyArg_ParseTuple(args, "II",
1683 &(addr->sc_id), &(addr->sc_unit))) {
1684 PyErr_SetString(PyExc_TypeError, "getsockaddrarg: "
1685 "expected str or tuple of two ints");
Victor Stinnera534fc42013-06-03 22:07:27 +02001686
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001687 return 0;
1688 }
Victor Stinnera534fc42013-06-03 22:07:27 +02001689
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001690 *len_ret = sizeof(*addr);
1691 return 1;
1692 }
1693#endif
1694 default:
1695 PyErr_SetString(PyExc_OSError,
1696 "getsockaddrarg: unsupported PF_SYSTEM protocol");
1697 return 0;
1698 }
1699#endif
Charles-François Natali47413c12011-10-06 19:47:44 +02001700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001704 PyErr_SetString(PyExc_OSError, "getsockaddrarg: bad family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 return 0;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001708}
1709
Guido van Rossum30a685f1991-06-27 15:51:29 +00001710
Guido van Rossum48a680c2001-03-02 06:34:14 +00001711/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00001712 Return 1 if the family is known, 0 otherwise. The length is returned
1713 through len_ret. */
1714
1715static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00001716getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 switch (s->sock_family) {
Guido van Rossum710e1df1992-06-12 10:39:36 +00001719
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001720#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 case AF_UNIX:
1722 {
1723 *len_ret = sizeof (struct sockaddr_un);
1724 return 1;
1725 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001726#endif /* AF_UNIX */
Martin v. Löwis11017b12006-01-14 18:12:57 +00001727#if defined(AF_NETLINK)
1728 case AF_NETLINK:
1729 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 *len_ret = sizeof (struct sockaddr_nl);
1731 return 1;
Martin v. Löwis11017b12006-01-14 18:12:57 +00001732 }
1733#endif
Guido van Rossum710e1df1992-06-12 10:39:36 +00001734
Charles-François Natali10b8cf42011-11-10 19:21:37 +01001735#ifdef AF_RDS
1736 case AF_RDS:
1737 /* RDS sockets use sockaddr_in: fall-through */
1738#endif
1739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 case AF_INET:
1741 {
1742 *len_ret = sizeof (struct sockaddr_in);
1743 return 1;
1744 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00001745
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001746#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 case AF_INET6:
1748 {
1749 *len_ret = sizeof (struct sockaddr_in6);
1750 return 1;
1751 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001752#endif
1753
Hye-Shik Chang81268602004-02-02 06:05:24 +00001754#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 case AF_BLUETOOTH:
1756 {
1757 switch(s->sock_proto)
1758 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 case BTPROTO_L2CAP:
1761 *len_ret = sizeof (struct sockaddr_l2);
1762 return 1;
1763 case BTPROTO_RFCOMM:
1764 *len_ret = sizeof (struct sockaddr_rc);
1765 return 1;
1766 case BTPROTO_HCI:
1767 *len_ret = sizeof (struct sockaddr_hci);
1768 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001769#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 case BTPROTO_SCO:
1771 *len_ret = sizeof (struct sockaddr_sco);
1772 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001773#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001775 PyErr_SetString(PyExc_OSError, "getsockaddrlen: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 "unknown BT protocol");
1777 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 }
1780 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001781#endif
1782
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001783#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 case AF_PACKET:
1785 {
1786 *len_ret = sizeof (struct sockaddr_ll);
1787 return 1;
1788 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001789#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001790
Christian Heimes043d6f62008-01-07 17:19:16 +00001791#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 case AF_TIPC:
1793 {
1794 *len_ret = sizeof (struct sockaddr_tipc);
1795 return 1;
1796 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001797#endif
1798
Charles-François Natalib4062e82014-02-08 22:54:11 +01001799#ifdef HAVE_LINUX_CAN_H
Charles-François Natali47413c12011-10-06 19:47:44 +02001800 case AF_CAN:
1801 {
1802 *len_ret = sizeof (struct sockaddr_can);
1803 return 1;
1804 }
1805#endif
Victor Stinnera534fc42013-06-03 22:07:27 +02001806
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001807#ifdef PF_SYSTEM
1808 case PF_SYSTEM:
1809 switch(s->sock_proto) {
1810#ifdef SYSPROTO_CONTROL
1811 case SYSPROTO_CONTROL:
1812 *len_ret = sizeof (struct sockaddr_ctl);
1813 return 1;
1814#endif
1815 default:
1816 PyErr_SetString(PyExc_OSError, "getsockaddrlen: "
1817 "unknown PF_SYSTEM protocol");
1818 return 0;
1819 }
1820#endif
Charles-François Natali47413c12011-10-06 19:47:44 +02001821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 /* More cases here... */
Guido van Rossum710e1df1992-06-12 10:39:36 +00001823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001825 PyErr_SetString(PyExc_OSError, "getsockaddrlen: bad family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 return 0;
Guido van Rossum710e1df1992-06-12 10:39:36 +00001827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00001829}
1830
1831
Nick Coghlan96fe56a2011-08-22 11:55:57 +10001832/* Support functions for the sendmsg() and recvmsg[_into]() methods.
1833 Currently, these methods are only compiled if the RFC 2292/3542
1834 CMSG_LEN() macro is available. Older systems seem to have used
1835 sizeof(struct cmsghdr) + (length) where CMSG_LEN() is used now, so
1836 it may be possible to define CMSG_LEN() that way if it's not
1837 provided. Some architectures might need extra padding after the
1838 cmsghdr, however, and CMSG_LEN() would have to take account of
1839 this. */
1840#ifdef CMSG_LEN
1841/* If length is in range, set *result to CMSG_LEN(length) and return
1842 true; otherwise, return false. */
1843static int
1844get_CMSG_LEN(size_t length, size_t *result)
1845{
1846 size_t tmp;
1847
1848 if (length > (SOCKLEN_T_LIMIT - CMSG_LEN(0)))
1849 return 0;
1850 tmp = CMSG_LEN(length);
1851 if (tmp > SOCKLEN_T_LIMIT || tmp < length)
1852 return 0;
1853 *result = tmp;
1854 return 1;
1855}
1856
1857#ifdef CMSG_SPACE
1858/* If length is in range, set *result to CMSG_SPACE(length) and return
1859 true; otherwise, return false. */
1860static int
1861get_CMSG_SPACE(size_t length, size_t *result)
1862{
1863 size_t tmp;
1864
1865 /* Use CMSG_SPACE(1) here in order to take account of the padding
1866 necessary before *and* after the data. */
1867 if (length > (SOCKLEN_T_LIMIT - CMSG_SPACE(1)))
1868 return 0;
1869 tmp = CMSG_SPACE(length);
1870 if (tmp > SOCKLEN_T_LIMIT || tmp < length)
1871 return 0;
1872 *result = tmp;
1873 return 1;
1874}
1875#endif
1876
1877/* Return true iff msg->msg_controllen is valid, cmsgh is a valid
1878 pointer in msg->msg_control with at least "space" bytes after it,
1879 and its cmsg_len member inside the buffer. */
1880static int
1881cmsg_min_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t space)
1882{
1883 size_t cmsg_offset;
1884 static const size_t cmsg_len_end = (offsetof(struct cmsghdr, cmsg_len) +
1885 sizeof(cmsgh->cmsg_len));
1886
Charles-François Natali466517d2011-08-28 18:23:43 +02001887 /* Note that POSIX allows msg_controllen to be of signed type. */
Brett Cannonb05cbe62014-01-07 17:01:01 -05001888 if (cmsgh == NULL || msg->msg_control == NULL)
Nick Coghlan96fe56a2011-08-22 11:55:57 +10001889 return 0;
Brett Cannonb05cbe62014-01-07 17:01:01 -05001890 /* Note that POSIX allows msg_controllen to be of a signed type. This is
1891 annoying under OS X as it's unsigned there and so it triggers a
1892 tautological comparison warning under Clang when compared against 0.
1893 Since the check is valid on other platforms, silence the warning under
1894 Clang. */
1895 #ifdef __clang__
1896 #pragma clang diagnostic push
1897 #pragma clang diagnostic ignored "-Wtautological-compare"
1898 #endif
1899 if (msg->msg_controllen < 0)
1900 return 0;
1901 #ifdef __clang__
1902 #pragma clang diagnostic pop
1903 #endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10001904 if (space < cmsg_len_end)
1905 space = cmsg_len_end;
1906 cmsg_offset = (char *)cmsgh - (char *)msg->msg_control;
1907 return (cmsg_offset <= (size_t)-1 - space &&
1908 cmsg_offset + space <= msg->msg_controllen);
1909}
1910
1911/* If pointer CMSG_DATA(cmsgh) is in buffer msg->msg_control, set
1912 *space to number of bytes following it in the buffer and return
1913 true; otherwise, return false. Assumes cmsgh, msg->msg_control and
1914 msg->msg_controllen are valid. */
1915static int
1916get_cmsg_data_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *space)
1917{
1918 size_t data_offset;
1919 char *data_ptr;
1920
1921 if ((data_ptr = (char *)CMSG_DATA(cmsgh)) == NULL)
1922 return 0;
1923 data_offset = data_ptr - (char *)msg->msg_control;
1924 if (data_offset > msg->msg_controllen)
1925 return 0;
1926 *space = msg->msg_controllen - data_offset;
1927 return 1;
1928}
1929
1930/* If cmsgh is invalid or not contained in the buffer pointed to by
1931 msg->msg_control, return -1. If cmsgh is valid and its associated
1932 data is entirely contained in the buffer, set *data_len to the
1933 length of the associated data and return 0. If only part of the
1934 associated data is contained in the buffer but cmsgh is otherwise
1935 valid, set *data_len to the length contained in the buffer and
1936 return 1. */
1937static int
1938get_cmsg_data_len(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *data_len)
1939{
1940 size_t space, cmsg_data_len;
1941
1942 if (!cmsg_min_space(msg, cmsgh, CMSG_LEN(0)) ||
1943 cmsgh->cmsg_len < CMSG_LEN(0))
1944 return -1;
1945 cmsg_data_len = cmsgh->cmsg_len - CMSG_LEN(0);
1946 if (!get_cmsg_data_space(msg, cmsgh, &space))
1947 return -1;
1948 if (space >= cmsg_data_len) {
1949 *data_len = cmsg_data_len;
1950 return 0;
1951 }
1952 *data_len = space;
1953 return 1;
1954}
1955#endif /* CMSG_LEN */
1956
1957
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001958/* s._accept() -> (fd, address) */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001959
Guido van Rossum73624e91994-10-10 17:59:00 +00001960static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001961sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 sock_addr_t addrbuf;
1964 SOCKET_T newfd = INVALID_SOCKET;
1965 socklen_t addrlen;
1966 PyObject *sock = NULL;
1967 PyObject *addr = NULL;
1968 PyObject *res = NULL;
1969 int timeout;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001970#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
1971 /* accept4() is available on Linux 2.6.28+ and glibc 2.10 */
1972 static int accept4_works = -1;
1973#endif
1974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 if (!getsockaddrlen(s, &addrlen))
1976 return NULL;
1977 memset(&addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 if (!IS_SELECTABLE(s))
1980 return select_error();
Neal Norwitz082b2df2006-02-07 07:04:46 +00001981
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00001982 BEGIN_SELECT_LOOP(s)
Victor Stinnerdaf45552013-08-28 00:53:59 +02001983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00001985 timeout = internal_select_ex(s, 0, interval);
Antoine Pitroub1c54962010-10-14 15:05:38 +00001986 if (!timeout) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001987#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
1988 if (accept4_works != 0) {
1989 newfd = accept4(s->sock_fd, SAS2SA(&addrbuf), &addrlen,
1990 SOCK_CLOEXEC);
1991 if (newfd == INVALID_SOCKET && accept4_works == -1) {
1992 /* On Linux older than 2.6.28, accept4() fails with ENOSYS */
1993 accept4_works = (errno != ENOSYS);
1994 }
1995 }
1996 if (accept4_works == 0)
1997 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
1998#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Victor Stinnerdaf45552013-08-28 00:53:59 +02002000#endif
Antoine Pitroub1c54962010-10-14 15:05:38 +00002001 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 if (timeout == 1) {
2005 PyErr_SetString(socket_timeout, "timed out");
2006 return NULL;
2007 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002008 END_SELECT_LOOP(s)
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 if (newfd == INVALID_SOCKET)
2011 return s->errorhandler();
Barry Warsaw752300b1997-01-03 17:18:10 +00002012
Victor Stinnerdaf45552013-08-28 00:53:59 +02002013#ifdef MS_WINDOWS
2014 if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) {
2015 PyErr_SetFromWindowsErr(0);
2016 SOCKETCLOSE(newfd);
2017 goto finally;
2018 }
2019#else
2020
2021#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
2022 if (!accept4_works)
2023#endif
2024 {
2025 if (_Py_set_inheritable(newfd, 0, NULL) < 0) {
2026 SOCKETCLOSE(newfd);
2027 goto finally;
2028 }
2029 }
2030#endif
2031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 sock = PyLong_FromSocket_t(newfd);
2033 if (sock == NULL) {
2034 SOCKETCLOSE(newfd);
2035 goto finally;
2036 }
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
2039 addrlen, s->sock_proto);
2040 if (addr == NULL)
2041 goto finally;
Barry Warsaw752300b1997-01-03 17:18:10 +00002042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 res = PyTuple_Pack(2, sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00002044
Guido van Rossum67f7a382002-06-06 21:08:16 +00002045finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 Py_XDECREF(sock);
2047 Py_XDECREF(addr);
2048 return res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002049}
2050
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002051PyDoc_STRVAR(accept_doc,
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002052"_accept() -> (integer, address info)\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002053\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002054Wait for an incoming connection. Return a new socket file descriptor\n\
2055representing the connection, and the address of the client.\n\
2056For IP sockets, the address info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002057
Guido van Rossum11ba0942002-06-13 15:07:44 +00002058/* s.setblocking(flag) method. Argument:
2059 False -- non-blocking mode; same as settimeout(0)
2060 True -- blocking mode; same as settimeout(None)
2061*/
Guido van Rossume4485b01994-09-07 14:32:49 +00002062
Guido van Rossum73624e91994-10-10 17:59:00 +00002063static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002064sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00002065{
Serhiy Storchaka78980432013-01-15 01:12:17 +02002066 long block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 block = PyLong_AsLong(arg);
2069 if (block == -1 && PyErr_Occurred())
2070 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 s->sock_timeout = block ? -1.0 : 0.0;
2073 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00002074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 Py_INCREF(Py_None);
2076 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00002077}
Guido van Rossume4485b01994-09-07 14:32:49 +00002078
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002079PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002080"setblocking(flag)\n\
2081\n\
2082Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00002083setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002084setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002085
Guido van Rossum11ba0942002-06-13 15:07:44 +00002086/* s.settimeout(timeout) method. Argument:
2087 None -- no timeout, blocking mode; same as setblocking(True)
2088 0.0 -- non-blocking mode; same as setblocking(False)
2089 > 0 -- timeout mode; operations time out after timeout seconds
2090 < 0 -- illegal; raises an exception
2091*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00002092static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002093sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00002094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 double timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 if (arg == Py_None)
2098 timeout = -1.0;
2099 else {
2100 timeout = PyFloat_AsDouble(arg);
2101 if (timeout < 0.0) {
2102 if (!PyErr_Occurred())
2103 PyErr_SetString(PyExc_ValueError,
2104 "Timeout value out of range");
2105 return NULL;
2106 }
2107 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 s->sock_timeout = timeout;
2110 internal_setblocking(s, timeout < 0.0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 Py_INCREF(Py_None);
2113 return Py_None;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002114}
2115
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002116PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002117"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00002118\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00002119Set a timeout on socket operations. 'timeout' can be a float,\n\
2120giving in seconds, or None. Setting a timeout of None disables\n\
2121the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002122Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00002123
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002124/* s.gettimeout() method.
2125 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00002126static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002127sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00002128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 if (s->sock_timeout < 0.0) {
2130 Py_INCREF(Py_None);
2131 return Py_None;
2132 }
2133 else
2134 return PyFloat_FromDouble(s->sock_timeout);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002135}
2136
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002137PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002138"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00002139\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03002140Returns the timeout in seconds (float) associated with socket \n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00002141operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002142operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00002143
Guido van Rossumaee08791992-09-08 09:05:33 +00002144/* s.setsockopt() method.
2145 With an integer third argument, sets an integer option.
2146 With a string third argument, sets an option from a buffer;
2147 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002148
Guido van Rossum73624e91994-10-10 17:59:00 +00002149static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002150sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 int level;
2153 int optname;
2154 int res;
2155 char *buf;
2156 int buflen;
2157 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 if (PyArg_ParseTuple(args, "iii:setsockopt",
2160 &level, &optname, &flag)) {
2161 buf = (char *) &flag;
2162 buflen = sizeof flag;
2163 }
2164 else {
2165 PyErr_Clear();
2166 if (!PyArg_ParseTuple(args, "iiy#:setsockopt",
2167 &level, &optname, &buf, &buflen))
2168 return NULL;
2169 }
2170 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
2171 if (res < 0)
2172 return s->errorhandler();
2173 Py_INCREF(Py_None);
2174 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002175}
2176
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002177PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002178"setsockopt(level, option, value)\n\
2179\n\
2180Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002181The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002182
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002183
Guido van Rossumaee08791992-09-08 09:05:33 +00002184/* s.getsockopt() method.
2185 With two arguments, retrieves an integer option.
2186 With a third integer argument, retrieves a string buffer of that size;
2187 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002188
Guido van Rossum73624e91994-10-10 17:59:00 +00002189static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002190sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 int level;
2193 int optname;
2194 int res;
2195 PyObject *buf;
2196 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
2199 &level, &optname, &buflen))
2200 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 if (buflen == 0) {
2203 int flag = 0;
2204 socklen_t flagsize = sizeof flag;
2205 res = getsockopt(s->sock_fd, level, optname,
2206 (void *)&flag, &flagsize);
2207 if (res < 0)
2208 return s->errorhandler();
2209 return PyLong_FromLong(flag);
2210 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 if (buflen <= 0 || buflen > 1024) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002212 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 "getsockopt buflen out of range");
2214 return NULL;
2215 }
2216 buf = PyBytes_FromStringAndSize((char *)NULL, buflen);
2217 if (buf == NULL)
2218 return NULL;
2219 res = getsockopt(s->sock_fd, level, optname,
2220 (void *)PyBytes_AS_STRING(buf), &buflen);
2221 if (res < 0) {
2222 Py_DECREF(buf);
2223 return s->errorhandler();
2224 }
2225 _PyBytes_Resize(&buf, buflen);
2226 return buf;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002227}
2228
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002229PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002230"getsockopt(level, option[, buffersize]) -> value\n\
2231\n\
2232Get a socket option. See the Unix manual for level and option.\n\
2233If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002234string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002235
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002236
Fred Drake728819a2000-07-01 03:40:12 +00002237/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002238
Guido van Rossum73624e91994-10-10 17:59:00 +00002239static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002240sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 sock_addr_t addrbuf;
2243 int addrlen;
2244 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2247 return NULL;
2248 Py_BEGIN_ALLOW_THREADS
2249 res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);
2250 Py_END_ALLOW_THREADS
2251 if (res < 0)
2252 return s->errorhandler();
2253 Py_INCREF(Py_None);
2254 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002255}
2256
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002257PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002258"bind(address)\n\
2259\n\
2260Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00002261pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002262sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002263
Guido van Rossum30a685f1991-06-27 15:51:29 +00002264
2265/* s.close() method.
2266 Set the file descriptor to -1 so operations tried subsequently
2267 will surely fail. */
2268
Guido van Rossum73624e91994-10-10 17:59:00 +00002269static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002270sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 if ((fd = s->sock_fd) != -1) {
2275 s->sock_fd = -1;
2276 Py_BEGIN_ALLOW_THREADS
2277 (void) SOCKETCLOSE(fd);
2278 Py_END_ALLOW_THREADS
2279 }
2280 Py_INCREF(Py_None);
2281 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002282}
2283
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002284PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002285"close()\n\
2286\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002287Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002288
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002289static PyObject *
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002290sock_detach(PySocketSockObject *s)
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002291{
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002292 SOCKET_T fd = s->sock_fd;
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002293 s->sock_fd = -1;
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002294 return PyLong_FromSocket_t(fd);
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002295}
2296
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002297PyDoc_STRVAR(detach_doc,
2298"detach()\n\
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002299\n\
Guido van Rossum638aebd2013-12-04 11:50:09 -08002300Close the socket object without closing the underlying file descriptor.\n\
2301The object cannot be used after this call, but the file descriptor\n\
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002302can be reused for other purposes. The file descriptor is returned.");
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002303
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002304static int
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002305internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 int *timeoutp)
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 int res, timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 timeout = 0;
2311 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002312
2313#ifdef MS_WINDOWS
2314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 if (s->sock_timeout > 0.0) {
2316 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK &&
2317 IS_SELECTABLE(s)) {
2318 /* This is a mess. Best solution: trust select */
2319 fd_set fds;
2320 fd_set fds_exc;
2321 struct timeval tv;
2322 tv.tv_sec = (int)s->sock_timeout;
2323 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
2324 FD_ZERO(&fds);
2325 FD_SET(s->sock_fd, &fds);
2326 FD_ZERO(&fds_exc);
2327 FD_SET(s->sock_fd, &fds_exc);
Antoine Pitrou19467d22010-08-17 19:33:30 +00002328 res = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
2329 NULL, &fds, &fds_exc, &tv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 if (res == 0) {
2331 res = WSAEWOULDBLOCK;
2332 timeout = 1;
2333 } else if (res > 0) {
2334 if (FD_ISSET(s->sock_fd, &fds))
2335 /* The socket is in the writable set - this
2336 means connected */
2337 res = 0;
2338 else {
2339 /* As per MS docs, we need to call getsockopt()
2340 to get the underlying error */
2341 int res_size = sizeof res;
2342 /* It must be in the exception set */
2343 assert(FD_ISSET(s->sock_fd, &fds_exc));
2344 if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
2345 (char *)&res, &res_size))
2346 /* getsockopt also clears WSAGetLastError,
2347 so reset it back. */
2348 WSASetLastError(res);
2349 else
2350 res = WSAGetLastError();
2351 }
2352 }
2353 /* else if (res < 0) an error occurred */
2354 }
2355 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 if (res < 0)
2358 res = WSAGetLastError();
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002359
2360#else
2361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 if (s->sock_timeout > 0.0) {
2363 if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) {
2364 timeout = internal_select(s, 1);
2365 if (timeout == 0) {
2366 /* Bug #1019808: in case of an EINPROGRESS,
2367 use getsockopt(SO_ERROR) to get the real
2368 error. */
2369 socklen_t res_size = sizeof res;
2370 (void)getsockopt(s->sock_fd, SOL_SOCKET,
2371 SO_ERROR, &res, &res_size);
2372 if (res == EISCONN)
2373 res = 0;
2374 errno = res;
2375 }
2376 else if (timeout == -1) {
2377 res = errno; /* had error */
2378 }
2379 else
2380 res = EWOULDBLOCK; /* timed out */
2381 }
2382 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 if (res < 0)
2385 res = errno;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002386
2387#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 *timeoutp = timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 return res;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002391}
Guido van Rossum30a685f1991-06-27 15:51:29 +00002392
Fred Drake728819a2000-07-01 03:40:12 +00002393/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002394
Guido van Rossum73624e91994-10-10 17:59:00 +00002395static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002396sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 sock_addr_t addrbuf;
2399 int addrlen;
2400 int res;
2401 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2404 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 Py_BEGIN_ALLOW_THREADS
2407 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
2408 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 if (timeout == 1) {
2411 PyErr_SetString(socket_timeout, "timed out");
2412 return NULL;
2413 }
2414 if (res != 0)
2415 return s->errorhandler();
2416 Py_INCREF(Py_None);
2417 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002418}
2419
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002420PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002421"connect(address)\n\
2422\n\
2423Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002424is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002425
Guido van Rossum30a685f1991-06-27 15:51:29 +00002426
Fred Drake728819a2000-07-01 03:40:12 +00002427/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002428
2429static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002430sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 sock_addr_t addrbuf;
2433 int addrlen;
2434 int res;
2435 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2438 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 Py_BEGIN_ALLOW_THREADS
2441 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
2442 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 /* Signals are not errors (though they may raise exceptions). Adapted
2445 from PyErr_SetFromErrnoWithFilenameObject(). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002446#ifdef EINTR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 if (res == EINTR && PyErr_CheckSignals())
2448 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002449#endif
2450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 return PyLong_FromLong((long) res);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002452}
2453
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002454PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002455"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002456\n\
2457This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002458instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002459
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002460
Guido van Rossumed233a51992-06-23 09:07:03 +00002461/* s.fileno() method */
2462
Guido van Rossum73624e91994-10-10 17:59:00 +00002463static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002464sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00002465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 return PyLong_FromSocket_t(s->sock_fd);
Guido van Rossumed233a51992-06-23 09:07:03 +00002467}
2468
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002469PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002470"fileno() -> integer\n\
2471\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002472Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002473
Guido van Rossumed233a51992-06-23 09:07:03 +00002474
Guido van Rossumc89705d1992-11-26 08:54:07 +00002475/* s.getsockname() method */
2476
Guido van Rossum73624e91994-10-10 17:59:00 +00002477static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002478sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 sock_addr_t addrbuf;
2481 int res;
2482 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 if (!getsockaddrlen(s, &addrlen))
2485 return NULL;
2486 memset(&addrbuf, 0, addrlen);
2487 Py_BEGIN_ALLOW_THREADS
2488 res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2489 Py_END_ALLOW_THREADS
2490 if (res < 0)
2491 return s->errorhandler();
2492 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2493 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002494}
2495
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002496PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002497"getsockname() -> address info\n\
2498\n\
2499Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002500info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002501
Guido van Rossumc89705d1992-11-26 08:54:07 +00002502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002504/* s.getpeername() method */
2505
Guido van Rossum73624e91994-10-10 17:59:00 +00002506static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002507sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 sock_addr_t addrbuf;
2510 int res;
2511 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 if (!getsockaddrlen(s, &addrlen))
2514 return NULL;
2515 memset(&addrbuf, 0, addrlen);
2516 Py_BEGIN_ALLOW_THREADS
2517 res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2518 Py_END_ALLOW_THREADS
2519 if (res < 0)
2520 return s->errorhandler();
2521 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2522 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002523}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002524
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002525PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002526"getpeername() -> address info\n\
2527\n\
2528Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002529info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002530
Guido van Rossumb6775db1994-08-01 11:34:53 +00002531#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002532
2533
Guido van Rossum30a685f1991-06-27 15:51:29 +00002534/* s.listen(n) method */
2535
Guido van Rossum73624e91994-10-10 17:59:00 +00002536static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002537sock_listen(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002539 int backlog;
2540 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002541
Serhiy Storchaka78980432013-01-15 01:12:17 +02002542 backlog = _PyLong_AsInt(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 if (backlog == -1 && PyErr_Occurred())
2544 return NULL;
2545 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou1be815a2011-05-10 19:16:29 +02002546 /* To avoid problems on systems that don't allow a negative backlog
2547 * (which doesn't make sense anyway) we force a minimum value of 0. */
2548 if (backlog < 0)
2549 backlog = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 res = listen(s->sock_fd, backlog);
2551 Py_END_ALLOW_THREADS
2552 if (res < 0)
2553 return s->errorhandler();
2554 Py_INCREF(Py_None);
2555 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002556}
2557
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002558PyDoc_STRVAR(listen_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002559"listen(backlog)\n\
2560\n\
2561Enable a server to accept connections. The backlog argument must be at\n\
Antoine Pitrou1be815a2011-05-10 19:16:29 +02002562least 0 (if it is lower, it is set to 0); it specifies the number of\n\
2563unaccepted connections that the system will allow before refusing new\n\
2564connections.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002565
2566
Thomas Wouters477c8d52006-05-27 19:21:47 +00002567/*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002568 * This is the guts of the recv() and recv_into() methods, which reads into a
Thomas Wouters902d6eb2007-01-09 23:18:33 +00002569 * char buffer. If you have any inc/dec ref to do to the objects that contain
Thomas Wouters477c8d52006-05-27 19:21:47 +00002570 * the buffer, do it in the caller. This function returns the number of bytes
Ezio Melotti13925002011-03-16 11:05:33 +02002571 * successfully read. If there was an error, it returns -1. Note that it is
Thomas Wouters477c8d52006-05-27 19:21:47 +00002572 * also possible that we return a number of bytes smaller than the request
2573 * bytes.
2574 */
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002575
Antoine Pitrou19467d22010-08-17 19:33:30 +00002576static Py_ssize_t
2577sock_recv_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002578{
Antoine Pitrou19467d22010-08-17 19:33:30 +00002579 Py_ssize_t outlen = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 int timeout;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 if (!IS_SELECTABLE(s)) {
2583 select_error();
2584 return -1;
2585 }
2586 if (len == 0) {
2587 /* If 0 bytes were requested, do nothing. */
2588 return 0;
2589 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002590
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002591 BEGIN_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002592 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002593 timeout = internal_select_ex(s, 0, interval);
Victor Stinner9a644b22013-06-24 23:47:41 +02002594 if (!timeout) {
Victor Stinner14b9b112013-06-25 00:37:25 +02002595#ifdef MS_WINDOWS
Victor Stinner9a644b22013-06-24 23:47:41 +02002596 if (len > INT_MAX)
2597 len = INT_MAX;
2598 outlen = recv(s->sock_fd, cbuf, (int)len, flags);
2599#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002600 outlen = recv(s->sock_fd, cbuf, len, flags);
Victor Stinner9a644b22013-06-24 23:47:41 +02002601#endif
2602 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 if (timeout == 1) {
2606 PyErr_SetString(socket_timeout, "timed out");
2607 return -1;
2608 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002609 END_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002610 if (outlen < 0) {
2611 /* Note: the call to errorhandler() ALWAYS indirectly returned
2612 NULL, so ignore its return value */
2613 s->errorhandler();
2614 return -1;
2615 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 return outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002617}
2618
Guido van Rossum48a680c2001-03-02 06:34:14 +00002619
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002620/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002621
Guido van Rossum73624e91994-10-10 17:59:00 +00002622static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002623sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002624{
Antoine Pitrou19467d22010-08-17 19:33:30 +00002625 Py_ssize_t recvlen, outlen;
2626 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 PyObject *buf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002628
Antoine Pitrou19467d22010-08-17 19:33:30 +00002629 if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002630 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 if (recvlen < 0) {
2633 PyErr_SetString(PyExc_ValueError,
2634 "negative buffersize in recv");
2635 return NULL;
2636 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 /* Allocate a new string. */
2639 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2640 if (buf == NULL)
2641 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643 /* Call the guts */
2644 outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags);
2645 if (outlen < 0) {
2646 /* An error occurred, release the string and return an
2647 error. */
2648 Py_DECREF(buf);
2649 return NULL;
2650 }
2651 if (outlen != recvlen) {
2652 /* We did not read as many bytes as we anticipated, resize the
2653 string if possible and be successful. */
2654 _PyBytes_Resize(&buf, outlen);
2655 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657 return buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002658}
2659
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002660PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002661"recv(buffersize[, flags]) -> data\n\
2662\n\
2663Receive up to buffersize bytes from the socket. For the optional flags\n\
2664argument, see the Unix manual. When no data is available, block until\n\
2665at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002666the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002667
Guido van Rossum30a685f1991-06-27 15:51:29 +00002668
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002669/* s.recv_into(buffer, [nbytes [,flags]]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002670
Thomas Wouters477c8d52006-05-27 19:21:47 +00002671static PyObject*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002672sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00002675
Antoine Pitrou19467d22010-08-17 19:33:30 +00002676 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 Py_buffer pbuf;
2678 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002679 Py_ssize_t buflen, readlen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 /* Get the buffer's memory */
Antoine Pitrou19467d22010-08-17 19:33:30 +00002682 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 &pbuf, &recvlen, &flags))
2684 return NULL;
2685 buf = pbuf.buf;
2686 buflen = pbuf.len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 if (recvlen < 0) {
2689 PyBuffer_Release(&pbuf);
2690 PyErr_SetString(PyExc_ValueError,
2691 "negative buffersize in recv_into");
2692 return NULL;
2693 }
2694 if (recvlen == 0) {
2695 /* If nbytes was not specified, use the buffer's length */
2696 recvlen = buflen;
2697 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002699 /* Check if the buffer is large enough */
2700 if (buflen < recvlen) {
2701 PyBuffer_Release(&pbuf);
2702 PyErr_SetString(PyExc_ValueError,
2703 "buffer too small for requested bytes");
2704 return NULL;
2705 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 /* Call the guts */
2708 readlen = sock_recv_guts(s, buf, recvlen, flags);
2709 if (readlen < 0) {
2710 /* Return an error. */
2711 PyBuffer_Release(&pbuf);
2712 return NULL;
2713 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 PyBuffer_Release(&pbuf);
2716 /* Return the number of bytes read. Note that we do not do anything
2717 special here in the case that readlen < recvlen. */
2718 return PyLong_FromSsize_t(readlen);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002719}
2720
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002721PyDoc_STRVAR(recv_into_doc,
2722"recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002723\n\
2724A version of recv() that stores its data into a buffer rather than creating \n\
2725a new string. Receive up to buffersize bytes from the socket. If buffersize \n\
2726is not specified (or 0), receive up to the size available in the given buffer.\n\
2727\n\
2728See recv() for documentation about the flags.");
2729
2730
2731/*
Christian Heimes99170a52007-12-19 02:07:34 +00002732 * This is the guts of the recvfrom() and recvfrom_into() methods, which reads
2733 * into a char buffer. If you have any inc/def ref to do to the objects that
2734 * contain the buffer, do it in the caller. This function returns the number
Ezio Melotti13925002011-03-16 11:05:33 +02002735 * of bytes successfully read. If there was an error, it returns -1. Note
Christian Heimes99170a52007-12-19 02:07:34 +00002736 * that it is also possible that we return a number of bytes smaller than the
2737 * request bytes.
Thomas Wouters477c8d52006-05-27 19:21:47 +00002738 *
2739 * 'addr' is a return value for the address object. Note that you must decref
2740 * it yourself.
2741 */
Antoine Pitrou19467d22010-08-17 19:33:30 +00002742static Py_ssize_t
2743sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 PyObject** addr)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 sock_addr_t addrbuf;
2747 int timeout;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002748 Py_ssize_t n = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 *addr = NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 if (!getsockaddrlen(s, &addrlen))
2754 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 if (!IS_SELECTABLE(s)) {
2757 select_error();
2758 return -1;
2759 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002760
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002761 BEGIN_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 Py_BEGIN_ALLOW_THREADS
2763 memset(&addrbuf, 0, addrlen);
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002764 timeout = internal_select_ex(s, 0, interval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 if (!timeout) {
Victor Stinnere990c6e2013-11-16 00:18:58 +01002766#ifdef MS_WINDOWS
Victor Stinner9a644b22013-06-24 23:47:41 +02002767 if (len > INT_MAX)
2768 len = INT_MAX;
2769 n = recvfrom(s->sock_fd, cbuf, (int)len, flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002770 (void *) &addrbuf, &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002771#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 n = recvfrom(s->sock_fd, cbuf, len, flags,
2773 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002774#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 }
2776 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002778 if (timeout == 1) {
2779 PyErr_SetString(socket_timeout, "timed out");
2780 return -1;
2781 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002782 END_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 if (n < 0) {
2784 s->errorhandler();
2785 return -1;
2786 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
2789 addrlen, s->sock_proto)))
2790 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 return n;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002793}
2794
2795/* s.recvfrom(nbytes [,flags]) method */
2796
2797static PyObject *
2798sock_recvfrom(PySocketSockObject *s, PyObject *args)
2799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 PyObject *buf = NULL;
2801 PyObject *addr = NULL;
2802 PyObject *ret = NULL;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002803 int flags = 0;
2804 Py_ssize_t recvlen, outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002805
Antoine Pitrou19467d22010-08-17 19:33:30 +00002806 if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 if (recvlen < 0) {
2810 PyErr_SetString(PyExc_ValueError,
2811 "negative buffersize in recvfrom");
2812 return NULL;
2813 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2816 if (buf == NULL)
2817 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf),
2820 recvlen, flags, &addr);
2821 if (outlen < 0) {
2822 goto finally;
2823 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002825 if (outlen != recvlen) {
2826 /* We did not read as many bytes as we anticipated, resize the
Ezio Melotti13925002011-03-16 11:05:33 +02002827 string if possible and be successful. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 if (_PyBytes_Resize(&buf, outlen) < 0)
Ezio Melotti13925002011-03-16 11:05:33 +02002829 /* Oopsy, not so successful after all. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 goto finally;
2831 }
Barry Warsaw752300b1997-01-03 17:18:10 +00002832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002834
2835finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 Py_XDECREF(buf);
2837 Py_XDECREF(addr);
2838 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002839}
2840
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002841PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002842"recvfrom(buffersize[, flags]) -> (data, address info)\n\
2843\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002844Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002845
Thomas Wouters477c8d52006-05-27 19:21:47 +00002846
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002847/* s.recvfrom_into(buffer[, nbytes [,flags]]) method */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002848
2849static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002850sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00002853
Antoine Pitrou19467d22010-08-17 19:33:30 +00002854 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 Py_buffer pbuf;
2856 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002857 Py_ssize_t readlen, buflen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 PyObject *addr = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002860
Antoine Pitrou19467d22010-08-17 19:33:30 +00002861 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 kwlist, &pbuf,
2863 &recvlen, &flags))
2864 return NULL;
2865 buf = pbuf.buf;
2866 buflen = pbuf.len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 if (recvlen < 0) {
2869 PyBuffer_Release(&pbuf);
2870 PyErr_SetString(PyExc_ValueError,
2871 "negative buffersize in recvfrom_into");
2872 return NULL;
2873 }
2874 if (recvlen == 0) {
2875 /* If nbytes was not specified, use the buffer's length */
2876 recvlen = buflen;
Benjamin Petersonc6b37e22014-01-13 23:14:42 -05002877 } else if (recvlen > buflen) {
2878 PyBuffer_Release(&pbuf);
2879 PyErr_SetString(PyExc_ValueError,
2880 "nbytes is greater than the length of the buffer");
2881 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
2885 if (readlen < 0) {
2886 PyBuffer_Release(&pbuf);
2887 /* Return an error */
2888 Py_XDECREF(addr);
2889 return NULL;
2890 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 PyBuffer_Release(&pbuf);
2893 /* Return the number of bytes read and the address. Note that we do
2894 not do anything special here in the case that readlen < recvlen. */
Antoine Pitrou19467d22010-08-17 19:33:30 +00002895 return Py_BuildValue("nN", readlen, addr);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002896}
2897
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002898PyDoc_STRVAR(recvfrom_into_doc,
2899"recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002900\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002901Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002902
2903
Nick Coghlan96fe56a2011-08-22 11:55:57 +10002904/* The sendmsg() and recvmsg[_into]() methods require a working
2905 CMSG_LEN(). See the comment near get_CMSG_LEN(). */
2906#ifdef CMSG_LEN
2907/*
2908 * Call recvmsg() with the supplied iovec structures, flags, and
2909 * ancillary data buffer size (controllen). Returns the tuple return
2910 * value for recvmsg() or recvmsg_into(), with the first item provided
2911 * by the supplied makeval() function. makeval() will be called with
2912 * the length read and makeval_data as arguments, and must return a
2913 * new reference (which will be decrefed if there is a subsequent
2914 * error). On error, closes any file descriptors received via
2915 * SCM_RIGHTS.
2916 */
2917static PyObject *
2918sock_recvmsg_guts(PySocketSockObject *s, struct iovec *iov, int iovlen,
2919 int flags, Py_ssize_t controllen,
2920 PyObject *(*makeval)(ssize_t, void *), void *makeval_data)
2921{
2922 ssize_t bytes_received = -1;
2923 int timeout;
2924 sock_addr_t addrbuf;
2925 socklen_t addrbuflen;
Charles-François Natalie9e95ae2011-08-24 21:40:53 +02002926 struct msghdr msg = {0};
Nick Coghlan96fe56a2011-08-22 11:55:57 +10002927 PyObject *cmsg_list = NULL, *retval = NULL;
2928 void *controlbuf = NULL;
2929 struct cmsghdr *cmsgh;
2930 size_t cmsgdatalen = 0;
2931 int cmsg_status;
2932
2933 /* XXX: POSIX says that msg_name and msg_namelen "shall be
2934 ignored" when the socket is connected (Linux fills them in
2935 anyway for AF_UNIX sockets at least). Normally msg_namelen
2936 seems to be set to 0 if there's no address, but try to
2937 initialize msg_name to something that won't be mistaken for a
2938 real address if that doesn't happen. */
2939 if (!getsockaddrlen(s, &addrbuflen))
2940 return NULL;
2941 memset(&addrbuf, 0, addrbuflen);
2942 SAS2SA(&addrbuf)->sa_family = AF_UNSPEC;
2943
2944 if (controllen < 0 || controllen > SOCKLEN_T_LIMIT) {
2945 PyErr_SetString(PyExc_ValueError,
2946 "invalid ancillary data buffer length");
2947 return NULL;
2948 }
2949 if (controllen > 0 && (controlbuf = PyMem_Malloc(controllen)) == NULL)
2950 return PyErr_NoMemory();
2951
2952 /* Make the system call. */
2953 if (!IS_SELECTABLE(s)) {
2954 select_error();
2955 goto finally;
2956 }
2957
2958 BEGIN_SELECT_LOOP(s)
2959 Py_BEGIN_ALLOW_THREADS;
Nick Coghlan96fe56a2011-08-22 11:55:57 +10002960 msg.msg_name = SAS2SA(&addrbuf);
2961 msg.msg_namelen = addrbuflen;
2962 msg.msg_iov = iov;
2963 msg.msg_iovlen = iovlen;
2964 msg.msg_control = controlbuf;
2965 msg.msg_controllen = controllen;
2966 timeout = internal_select_ex(s, 0, interval);
2967 if (!timeout)
2968 bytes_received = recvmsg(s->sock_fd, &msg, flags);
2969 Py_END_ALLOW_THREADS;
2970 if (timeout == 1) {
2971 PyErr_SetString(socket_timeout, "timed out");
2972 goto finally;
2973 }
2974 END_SELECT_LOOP(s)
2975
2976 if (bytes_received < 0) {
2977 s->errorhandler();
2978 goto finally;
2979 }
2980
2981 /* Make list of (level, type, data) tuples from control messages. */
2982 if ((cmsg_list = PyList_New(0)) == NULL)
2983 goto err_closefds;
2984 /* Check for empty ancillary data as old CMSG_FIRSTHDR()
2985 implementations didn't do so. */
2986 for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL);
2987 cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) {
2988 PyObject *bytes, *tuple;
2989 int tmp;
2990
2991 cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen);
2992 if (cmsg_status != 0) {
2993 if (PyErr_WarnEx(PyExc_RuntimeWarning,
2994 "received malformed or improperly-truncated "
2995 "ancillary data", 1) == -1)
2996 goto err_closefds;
2997 }
2998 if (cmsg_status < 0)
2999 break;
3000 if (cmsgdatalen > PY_SSIZE_T_MAX) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003001 PyErr_SetString(PyExc_OSError, "control message too long");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003002 goto err_closefds;
3003 }
3004
3005 bytes = PyBytes_FromStringAndSize((char *)CMSG_DATA(cmsgh),
3006 cmsgdatalen);
3007 tuple = Py_BuildValue("iiN", (int)cmsgh->cmsg_level,
3008 (int)cmsgh->cmsg_type, bytes);
3009 if (tuple == NULL)
3010 goto err_closefds;
3011 tmp = PyList_Append(cmsg_list, tuple);
3012 Py_DECREF(tuple);
3013 if (tmp != 0)
3014 goto err_closefds;
3015
3016 if (cmsg_status != 0)
3017 break;
3018 }
3019
3020 retval = Py_BuildValue("NOiN",
3021 (*makeval)(bytes_received, makeval_data),
3022 cmsg_list,
3023 (int)msg.msg_flags,
3024 makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
3025 ((msg.msg_namelen > addrbuflen) ?
3026 addrbuflen : msg.msg_namelen),
3027 s->sock_proto));
3028 if (retval == NULL)
3029 goto err_closefds;
3030
3031finally:
3032 Py_XDECREF(cmsg_list);
3033 PyMem_Free(controlbuf);
3034 return retval;
3035
3036err_closefds:
3037#ifdef SCM_RIGHTS
3038 /* Close all descriptors coming from SCM_RIGHTS, so they don't leak. */
3039 for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL);
3040 cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) {
3041 cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen);
3042 if (cmsg_status < 0)
3043 break;
3044 if (cmsgh->cmsg_level == SOL_SOCKET &&
3045 cmsgh->cmsg_type == SCM_RIGHTS) {
3046 size_t numfds;
3047 int *fdp;
3048
3049 numfds = cmsgdatalen / sizeof(int);
3050 fdp = (int *)CMSG_DATA(cmsgh);
3051 while (numfds-- > 0)
3052 close(*fdp++);
3053 }
3054 if (cmsg_status != 0)
3055 break;
3056 }
3057#endif /* SCM_RIGHTS */
3058 goto finally;
3059}
3060
3061
3062static PyObject *
3063makeval_recvmsg(ssize_t received, void *data)
3064{
3065 PyObject **buf = data;
3066
3067 if (received < PyBytes_GET_SIZE(*buf))
3068 _PyBytes_Resize(buf, received);
3069 Py_XINCREF(*buf);
3070 return *buf;
3071}
3072
3073/* s.recvmsg(bufsize[, ancbufsize[, flags]]) method */
3074
3075static PyObject *
3076sock_recvmsg(PySocketSockObject *s, PyObject *args)
3077{
3078 Py_ssize_t bufsize, ancbufsize = 0;
3079 int flags = 0;
3080 struct iovec iov;
3081 PyObject *buf = NULL, *retval = NULL;
3082
3083 if (!PyArg_ParseTuple(args, "n|ni:recvmsg", &bufsize, &ancbufsize, &flags))
3084 return NULL;
3085
3086 if (bufsize < 0) {
3087 PyErr_SetString(PyExc_ValueError, "negative buffer size in recvmsg()");
3088 return NULL;
3089 }
3090 if ((buf = PyBytes_FromStringAndSize(NULL, bufsize)) == NULL)
3091 return NULL;
3092 iov.iov_base = PyBytes_AS_STRING(buf);
3093 iov.iov_len = bufsize;
3094
3095 /* Note that we're passing a pointer to *our pointer* to the bytes
3096 object here (&buf); makeval_recvmsg() may incref the object, or
3097 deallocate it and set our pointer to NULL. */
3098 retval = sock_recvmsg_guts(s, &iov, 1, flags, ancbufsize,
3099 &makeval_recvmsg, &buf);
3100 Py_XDECREF(buf);
3101 return retval;
3102}
3103
3104PyDoc_STRVAR(recvmsg_doc,
3105"recvmsg(bufsize[, ancbufsize[, flags]]) -> (data, ancdata, msg_flags, address)\n\
3106\n\
3107Receive normal data (up to bufsize bytes) and ancillary data from the\n\
3108socket. The ancbufsize argument sets the size in bytes of the\n\
3109internal buffer used to receive the ancillary data; it defaults to 0,\n\
3110meaning that no ancillary data will be received. Appropriate buffer\n\
3111sizes for ancillary data can be calculated using CMSG_SPACE() or\n\
3112CMSG_LEN(), and items which do not fit into the buffer might be\n\
3113truncated or discarded. The flags argument defaults to 0 and has the\n\
3114same meaning as for recv().\n\
3115\n\
3116The return value is a 4-tuple: (data, ancdata, msg_flags, address).\n\
3117The data item is a bytes object holding the non-ancillary data\n\
3118received. The ancdata item is a list of zero or more tuples\n\
3119(cmsg_level, cmsg_type, cmsg_data) representing the ancillary data\n\
3120(control messages) received: cmsg_level and cmsg_type are integers\n\
3121specifying the protocol level and protocol-specific type respectively,\n\
3122and cmsg_data is a bytes object holding the associated data. The\n\
3123msg_flags item is the bitwise OR of various flags indicating\n\
3124conditions on the received message; see your system documentation for\n\
3125details. If the receiving socket is unconnected, address is the\n\
3126address of the sending socket, if available; otherwise, its value is\n\
3127unspecified.\n\
3128\n\
3129If recvmsg() raises an exception after the system call returns, it\n\
3130will first attempt to close any file descriptors received via the\n\
3131SCM_RIGHTS mechanism.");
3132
3133
3134static PyObject *
3135makeval_recvmsg_into(ssize_t received, void *data)
3136{
3137 return PyLong_FromSsize_t(received);
3138}
3139
3140/* s.recvmsg_into(buffers[, ancbufsize[, flags]]) method */
3141
3142static PyObject *
3143sock_recvmsg_into(PySocketSockObject *s, PyObject *args)
3144{
3145 Py_ssize_t ancbufsize = 0;
3146 int flags = 0;
3147 struct iovec *iovs = NULL;
3148 Py_ssize_t i, nitems, nbufs = 0;
3149 Py_buffer *bufs = NULL;
3150 PyObject *buffers_arg, *fast, *retval = NULL;
3151
3152 if (!PyArg_ParseTuple(args, "O|ni:recvmsg_into",
3153 &buffers_arg, &ancbufsize, &flags))
3154 return NULL;
3155
3156 if ((fast = PySequence_Fast(buffers_arg,
3157 "recvmsg_into() argument 1 must be an "
3158 "iterable")) == NULL)
3159 return NULL;
3160 nitems = PySequence_Fast_GET_SIZE(fast);
3161 if (nitems > INT_MAX) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003162 PyErr_SetString(PyExc_OSError, "recvmsg_into() argument 1 is too long");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003163 goto finally;
3164 }
3165
3166 /* Fill in an iovec for each item, and save the Py_buffer
3167 structs to release afterwards. */
3168 if (nitems > 0 && ((iovs = PyMem_New(struct iovec, nitems)) == NULL ||
3169 (bufs = PyMem_New(Py_buffer, nitems)) == NULL)) {
3170 PyErr_NoMemory();
3171 goto finally;
3172 }
3173 for (; nbufs < nitems; nbufs++) {
3174 if (!PyArg_Parse(PySequence_Fast_GET_ITEM(fast, nbufs),
3175 "w*;recvmsg_into() argument 1 must be an iterable "
3176 "of single-segment read-write buffers",
3177 &bufs[nbufs]))
3178 goto finally;
3179 iovs[nbufs].iov_base = bufs[nbufs].buf;
3180 iovs[nbufs].iov_len = bufs[nbufs].len;
3181 }
3182
3183 retval = sock_recvmsg_guts(s, iovs, nitems, flags, ancbufsize,
3184 &makeval_recvmsg_into, NULL);
3185finally:
3186 for (i = 0; i < nbufs; i++)
3187 PyBuffer_Release(&bufs[i]);
3188 PyMem_Free(bufs);
3189 PyMem_Free(iovs);
3190 Py_DECREF(fast);
3191 return retval;
3192}
3193
3194PyDoc_STRVAR(recvmsg_into_doc,
3195"recvmsg_into(buffers[, ancbufsize[, flags]]) -> (nbytes, ancdata, msg_flags, address)\n\
3196\n\
3197Receive normal data and ancillary data from the socket, scattering the\n\
3198non-ancillary data into a series of buffers. The buffers argument\n\
3199must be an iterable of objects that export writable buffers\n\
3200(e.g. bytearray objects); these will be filled with successive chunks\n\
3201of the non-ancillary data until it has all been written or there are\n\
3202no more buffers. The ancbufsize argument sets the size in bytes of\n\
3203the internal buffer used to receive the ancillary data; it defaults to\n\
32040, meaning that no ancillary data will be received. Appropriate\n\
3205buffer sizes for ancillary data can be calculated using CMSG_SPACE()\n\
3206or CMSG_LEN(), and items which do not fit into the buffer might be\n\
3207truncated or discarded. The flags argument defaults to 0 and has the\n\
3208same meaning as for recv().\n\
3209\n\
3210The return value is a 4-tuple: (nbytes, ancdata, msg_flags, address).\n\
3211The nbytes item is the total number of bytes of non-ancillary data\n\
3212written into the buffers. The ancdata item is a list of zero or more\n\
3213tuples (cmsg_level, cmsg_type, cmsg_data) representing the ancillary\n\
3214data (control messages) received: cmsg_level and cmsg_type are\n\
3215integers specifying the protocol level and protocol-specific type\n\
3216respectively, and cmsg_data is a bytes object holding the associated\n\
3217data. The msg_flags item is the bitwise OR of various flags\n\
3218indicating conditions on the received message; see your system\n\
3219documentation for details. If the receiving socket is unconnected,\n\
3220address is the address of the sending socket, if available; otherwise,\n\
3221its value is unspecified.\n\
3222\n\
3223If recvmsg_into() raises an exception after the system call returns,\n\
3224it will first attempt to close any file descriptors received via the\n\
3225SCM_RIGHTS mechanism.");
3226#endif /* CMSG_LEN */
3227
3228
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00003229/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00003230
Guido van Rossum73624e91994-10-10 17:59:00 +00003231static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003232sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00003233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003234 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003235 Py_ssize_t len, n = -1;
3236 int flags = 0, timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003237 Py_buffer pbuf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003239 if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags))
3240 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 if (!IS_SELECTABLE(s)) {
3243 PyBuffer_Release(&pbuf);
3244 return select_error();
3245 }
3246 buf = pbuf.buf;
3247 len = pbuf.len;
Neal Norwitz082b2df2006-02-07 07:04:46 +00003248
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003249 BEGIN_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003251 timeout = internal_select_ex(s, 1, interval);
Victor Stinner9a644b22013-06-24 23:47:41 +02003252 if (!timeout) {
Christian Heimesaf01f662013-12-21 16:19:10 +01003253#ifdef MS_WINDOWS
Victor Stinner9a644b22013-06-24 23:47:41 +02003254 if (len > INT_MAX)
3255 len = INT_MAX;
3256 n = send(s->sock_fd, buf, (int)len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003257#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003259#endif
Victor Stinner9a644b22013-06-24 23:47:41 +02003260 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003262 if (timeout == 1) {
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003263 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 PyErr_SetString(socket_timeout, "timed out");
3265 return NULL;
3266 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003267 END_SELECT_LOOP(s)
3268
3269 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 if (n < 0)
3271 return s->errorhandler();
Antoine Pitrou19467d22010-08-17 19:33:30 +00003272 return PyLong_FromSsize_t(n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00003273}
3274
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003275PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003276"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003277\n\
3278Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003279argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003280sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003281
3282
3283/* s.sendall(data [,flags]) method */
3284
3285static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003286sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003289 Py_ssize_t len, n = -1;
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003290 int flags = 0, timeout, saved_errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 Py_buffer pbuf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags))
3294 return NULL;
3295 buf = pbuf.buf;
3296 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 if (!IS_SELECTABLE(s)) {
3299 PyBuffer_Release(&pbuf);
3300 return select_error();
3301 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00003302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 do {
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003304 Py_BEGIN_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003305 timeout = internal_select(s, 1);
3306 n = -1;
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003307 if (!timeout) {
Christian Heimesaf01f662013-12-21 16:19:10 +01003308#ifdef MS_WINDOWS
Victor Stinner9a644b22013-06-24 23:47:41 +02003309 if (len > INT_MAX)
3310 len = INT_MAX;
3311 n = send(s->sock_fd, buf, (int)len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003312#else
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003313 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003314#endif
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003315 }
3316 Py_END_ALLOW_THREADS
3317 if (timeout == 1) {
3318 PyBuffer_Release(&pbuf);
3319 PyErr_SetString(socket_timeout, "timed out");
3320 return NULL;
3321 }
3322 /* PyErr_CheckSignals() might change errno */
3323 saved_errno = errno;
3324 /* We must run our signal handlers before looping again.
3325 send() can return a successful partial write when it is
3326 interrupted, so we can't restrict ourselves to EINTR. */
3327 if (PyErr_CheckSignals()) {
3328 PyBuffer_Release(&pbuf);
3329 return NULL;
3330 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003331 if (n < 0) {
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003332 /* If interrupted, try again */
3333 if (saved_errno == EINTR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003334 continue;
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003335 else
3336 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003337 }
3338 buf += n;
3339 len -= n;
3340 } while (len > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003341 PyBuffer_Release(&pbuf);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 if (n < 0)
3344 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00003345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 Py_INCREF(Py_None);
3347 return Py_None;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003348}
3349
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003350PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003351"sendall(data[, flags])\n\
3352\n\
3353Send a data string to the socket. For the optional flags\n\
3354argument, see the Unix manual. This calls send() repeatedly\n\
3355until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003356to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003357
Guido van Rossum30a685f1991-06-27 15:51:29 +00003358
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00003359/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00003360
Guido van Rossum73624e91994-10-10 17:59:00 +00003361static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003362sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 Py_buffer pbuf;
3365 PyObject *addro;
3366 char *buf;
Antoine Pitrou5e981412011-03-17 22:38:37 +01003367 Py_ssize_t len, arglen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003368 sock_addr_t addrbuf;
3369 int addrlen, n = -1, flags, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 flags = 0;
Antoine Pitrou5e981412011-03-17 22:38:37 +01003372 arglen = PyTuple_Size(args);
3373 switch (arglen) {
3374 case 2:
3375 PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro);
3376 break;
3377 case 3:
3378 PyArg_ParseTuple(args, "y*iO:sendto",
3379 &pbuf, &flags, &addro);
3380 break;
3381 default:
3382 PyErr_Format(PyExc_TypeError,
3383 "sendto() takes 2 or 3 arguments (%d given)",
3384 arglen);
Victor Stinner77af1722011-05-26 14:05:59 +02003385 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003386 }
Antoine Pitrou5e981412011-03-17 22:38:37 +01003387 if (PyErr_Occurred())
3388 return NULL;
3389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 buf = pbuf.buf;
3391 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003393 if (!IS_SELECTABLE(s)) {
3394 PyBuffer_Release(&pbuf);
3395 return select_error();
3396 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00003397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003398 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) {
3399 PyBuffer_Release(&pbuf);
3400 return NULL;
3401 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003402
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003403 BEGIN_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003405 timeout = internal_select_ex(s, 1, interval);
Victor Stinner9a644b22013-06-24 23:47:41 +02003406 if (!timeout) {
Victor Stinner14b9b112013-06-25 00:37:25 +02003407#ifdef MS_WINDOWS
Victor Stinner9a644b22013-06-24 23:47:41 +02003408 if (len > INT_MAX)
3409 len = INT_MAX;
3410 n = sendto(s->sock_fd, buf, (int)len, flags,
3411 SAS2SA(&addrbuf), addrlen);
3412#else
3413 n = sendto(s->sock_fd, buf, len, flags,
3414 SAS2SA(&addrbuf), addrlen);
3415#endif
3416 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00003418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 if (timeout == 1) {
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003420 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003421 PyErr_SetString(socket_timeout, "timed out");
3422 return NULL;
3423 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003424 END_SELECT_LOOP(s)
3425 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 if (n < 0)
3427 return s->errorhandler();
Antoine Pitrou19467d22010-08-17 19:33:30 +00003428 return PyLong_FromSsize_t(n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003429}
3430
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003431PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003432"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003433\n\
3434Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003435For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003436
Guido van Rossum30a685f1991-06-27 15:51:29 +00003437
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003438/* The sendmsg() and recvmsg[_into]() methods require a working
3439 CMSG_LEN(). See the comment near get_CMSG_LEN(). */
3440#ifdef CMSG_LEN
3441/* s.sendmsg(buffers[, ancdata[, flags[, address]]]) method */
3442
3443static PyObject *
3444sock_sendmsg(PySocketSockObject *s, PyObject *args)
3445{
3446 Py_ssize_t i, ndataparts, ndatabufs = 0, ncmsgs, ncmsgbufs = 0;
3447 Py_buffer *databufs = NULL;
3448 struct iovec *iovs = NULL;
3449 sock_addr_t addrbuf;
Charles-François Natalie9e95ae2011-08-24 21:40:53 +02003450 struct msghdr msg = {0};
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003451 struct cmsginfo {
3452 int level;
3453 int type;
3454 Py_buffer data;
3455 } *cmsgs = NULL;
3456 void *controlbuf = NULL;
3457 size_t controllen, controllen_last;
3458 ssize_t bytes_sent = -1;
3459 int addrlen, timeout, flags = 0;
3460 PyObject *data_arg, *cmsg_arg = NULL, *addr_arg = NULL, *data_fast = NULL,
3461 *cmsg_fast = NULL, *retval = NULL;
3462
3463 if (!PyArg_ParseTuple(args, "O|OiO:sendmsg",
3464 &data_arg, &cmsg_arg, &flags, &addr_arg))
3465 return NULL;
3466
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003467 /* Parse destination address. */
3468 if (addr_arg != NULL && addr_arg != Py_None) {
3469 if (!getsockaddrarg(s, addr_arg, SAS2SA(&addrbuf), &addrlen))
3470 goto finally;
3471 msg.msg_name = &addrbuf;
3472 msg.msg_namelen = addrlen;
3473 }
3474
3475 /* Fill in an iovec for each message part, and save the Py_buffer
3476 structs to release afterwards. */
3477 if ((data_fast = PySequence_Fast(data_arg,
3478 "sendmsg() argument 1 must be an "
3479 "iterable")) == NULL)
3480 goto finally;
3481 ndataparts = PySequence_Fast_GET_SIZE(data_fast);
3482 if (ndataparts > INT_MAX) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003483 PyErr_SetString(PyExc_OSError, "sendmsg() argument 1 is too long");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003484 goto finally;
3485 }
3486 msg.msg_iovlen = ndataparts;
3487 if (ndataparts > 0 &&
3488 ((msg.msg_iov = iovs = PyMem_New(struct iovec, ndataparts)) == NULL ||
3489 (databufs = PyMem_New(Py_buffer, ndataparts)) == NULL)) {
3490 PyErr_NoMemory();
3491 goto finally;
3492 }
3493 for (; ndatabufs < ndataparts; ndatabufs++) {
3494 if (!PyArg_Parse(PySequence_Fast_GET_ITEM(data_fast, ndatabufs),
3495 "y*;sendmsg() argument 1 must be an iterable of "
3496 "buffer-compatible objects",
3497 &databufs[ndatabufs]))
3498 goto finally;
3499 iovs[ndatabufs].iov_base = databufs[ndatabufs].buf;
3500 iovs[ndatabufs].iov_len = databufs[ndatabufs].len;
3501 }
3502
3503 if (cmsg_arg == NULL)
3504 ncmsgs = 0;
3505 else {
3506 if ((cmsg_fast = PySequence_Fast(cmsg_arg,
3507 "sendmsg() argument 2 must be an "
3508 "iterable")) == NULL)
3509 goto finally;
3510 ncmsgs = PySequence_Fast_GET_SIZE(cmsg_fast);
3511 }
3512
3513#ifndef CMSG_SPACE
3514 if (ncmsgs > 1) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003515 PyErr_SetString(PyExc_OSError,
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003516 "sending multiple control messages is not supported "
3517 "on this system");
3518 goto finally;
3519 }
3520#endif
3521 /* Save level, type and Py_buffer for each control message,
3522 and calculate total size. */
3523 if (ncmsgs > 0 && (cmsgs = PyMem_New(struct cmsginfo, ncmsgs)) == NULL) {
3524 PyErr_NoMemory();
3525 goto finally;
3526 }
3527 controllen = controllen_last = 0;
3528 while (ncmsgbufs < ncmsgs) {
3529 size_t bufsize, space;
3530
3531 if (!PyArg_Parse(PySequence_Fast_GET_ITEM(cmsg_fast, ncmsgbufs),
3532 "(iiy*):[sendmsg() ancillary data items]",
3533 &cmsgs[ncmsgbufs].level,
3534 &cmsgs[ncmsgbufs].type,
3535 &cmsgs[ncmsgbufs].data))
3536 goto finally;
3537 bufsize = cmsgs[ncmsgbufs++].data.len;
3538
3539#ifdef CMSG_SPACE
3540 if (!get_CMSG_SPACE(bufsize, &space)) {
3541#else
3542 if (!get_CMSG_LEN(bufsize, &space)) {
3543#endif
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003544 PyErr_SetString(PyExc_OSError, "ancillary data item too large");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003545 goto finally;
3546 }
3547 controllen += space;
3548 if (controllen > SOCKLEN_T_LIMIT || controllen < controllen_last) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003549 PyErr_SetString(PyExc_OSError, "too much ancillary data");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003550 goto finally;
3551 }
3552 controllen_last = controllen;
3553 }
3554
3555 /* Construct ancillary data block from control message info. */
3556 if (ncmsgbufs > 0) {
3557 struct cmsghdr *cmsgh = NULL;
3558
3559 if ((msg.msg_control = controlbuf =
3560 PyMem_Malloc(controllen)) == NULL) {
3561 PyErr_NoMemory();
3562 goto finally;
3563 }
3564 msg.msg_controllen = controllen;
3565
3566 /* Need to zero out the buffer as a workaround for glibc's
3567 CMSG_NXTHDR() implementation. After getting the pointer to
3568 the next header, it checks its (uninitialized) cmsg_len
3569 member to see if the "message" fits in the buffer, and
3570 returns NULL if it doesn't. Zero-filling the buffer
Terry Jan Reedy0f847642013-03-11 18:34:00 -04003571 ensures that this doesn't happen. */
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003572 memset(controlbuf, 0, controllen);
3573
3574 for (i = 0; i < ncmsgbufs; i++) {
3575 size_t msg_len, data_len = cmsgs[i].data.len;
3576 int enough_space = 0;
3577
3578 cmsgh = (i == 0) ? CMSG_FIRSTHDR(&msg) : CMSG_NXTHDR(&msg, cmsgh);
3579 if (cmsgh == NULL) {
3580 PyErr_Format(PyExc_RuntimeError,
3581 "unexpected NULL result from %s()",
3582 (i == 0) ? "CMSG_FIRSTHDR" : "CMSG_NXTHDR");
3583 goto finally;
3584 }
3585 if (!get_CMSG_LEN(data_len, &msg_len)) {
3586 PyErr_SetString(PyExc_RuntimeError,
3587 "item size out of range for CMSG_LEN()");
3588 goto finally;
3589 }
3590 if (cmsg_min_space(&msg, cmsgh, msg_len)) {
3591 size_t space;
3592
3593 cmsgh->cmsg_len = msg_len;
3594 if (get_cmsg_data_space(&msg, cmsgh, &space))
3595 enough_space = (space >= data_len);
3596 }
3597 if (!enough_space) {
3598 PyErr_SetString(PyExc_RuntimeError,
3599 "ancillary data does not fit in calculated "
3600 "space");
3601 goto finally;
3602 }
3603 cmsgh->cmsg_level = cmsgs[i].level;
3604 cmsgh->cmsg_type = cmsgs[i].type;
3605 memcpy(CMSG_DATA(cmsgh), cmsgs[i].data.buf, data_len);
3606 }
3607 }
3608
3609 /* Make the system call. */
3610 if (!IS_SELECTABLE(s)) {
3611 select_error();
3612 goto finally;
3613 }
3614
3615 BEGIN_SELECT_LOOP(s)
3616 Py_BEGIN_ALLOW_THREADS;
3617 timeout = internal_select_ex(s, 1, interval);
3618 if (!timeout)
3619 bytes_sent = sendmsg(s->sock_fd, &msg, flags);
3620 Py_END_ALLOW_THREADS;
3621 if (timeout == 1) {
3622 PyErr_SetString(socket_timeout, "timed out");
3623 goto finally;
3624 }
3625 END_SELECT_LOOP(s)
3626
3627 if (bytes_sent < 0) {
3628 s->errorhandler();
3629 goto finally;
3630 }
3631 retval = PyLong_FromSsize_t(bytes_sent);
3632
3633finally:
3634 PyMem_Free(controlbuf);
3635 for (i = 0; i < ncmsgbufs; i++)
3636 PyBuffer_Release(&cmsgs[i].data);
3637 PyMem_Free(cmsgs);
3638 Py_XDECREF(cmsg_fast);
3639 for (i = 0; i < ndatabufs; i++)
3640 PyBuffer_Release(&databufs[i]);
3641 PyMem_Free(databufs);
3642 PyMem_Free(iovs);
3643 Py_XDECREF(data_fast);
3644 return retval;
3645}
3646
3647PyDoc_STRVAR(sendmsg_doc,
3648"sendmsg(buffers[, ancdata[, flags[, address]]]) -> count\n\
3649\n\
3650Send normal and ancillary data to the socket, gathering the\n\
3651non-ancillary data from a series of buffers and concatenating it into\n\
3652a single message. The buffers argument specifies the non-ancillary\n\
3653data as an iterable of buffer-compatible objects (e.g. bytes objects).\n\
3654The ancdata argument specifies the ancillary data (control messages)\n\
3655as an iterable of zero or more tuples (cmsg_level, cmsg_type,\n\
3656cmsg_data), where cmsg_level and cmsg_type are integers specifying the\n\
3657protocol level and protocol-specific type respectively, and cmsg_data\n\
3658is a buffer-compatible object holding the associated data. The flags\n\
3659argument defaults to 0 and has the same meaning as for send(). If\n\
3660address is supplied and not None, it sets a destination address for\n\
3661the message. The return value is the number of bytes of non-ancillary\n\
3662data sent.");
3663#endif /* CMSG_LEN */
3664
3665
Guido van Rossum30a685f1991-06-27 15:51:29 +00003666/* s.shutdown(how) method */
3667
Guido van Rossum73624e91994-10-10 17:59:00 +00003668static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003669sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003670{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003671 int how;
3672 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00003673
Serhiy Storchaka78980432013-01-15 01:12:17 +02003674 how = _PyLong_AsInt(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 if (how == -1 && PyErr_Occurred())
3676 return NULL;
3677 Py_BEGIN_ALLOW_THREADS
3678 res = shutdown(s->sock_fd, how);
3679 Py_END_ALLOW_THREADS
3680 if (res < 0)
3681 return s->errorhandler();
3682 Py_INCREF(Py_None);
3683 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003684}
3685
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003686PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003687"shutdown(flag)\n\
3688\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00003689Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
3690of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003691
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00003692#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Christian Heimesfaf2f632008-01-06 16:59:19 +00003693static PyObject*
3694sock_ioctl(PySocketSockObject *s, PyObject *arg)
3695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 unsigned long cmd = SIO_RCVALL;
3697 PyObject *argO;
3698 DWORD recv;
Christian Heimesfaf2f632008-01-06 16:59:19 +00003699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003700 if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO))
3701 return NULL;
Christian Heimesfaf2f632008-01-06 16:59:19 +00003702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003703 switch (cmd) {
3704 case SIO_RCVALL: {
3705 unsigned int option = RCVALL_ON;
3706 if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option))
3707 return NULL;
3708 if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option),
3709 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
3710 return set_error();
3711 }
3712 return PyLong_FromUnsignedLong(recv); }
3713 case SIO_KEEPALIVE_VALS: {
3714 struct tcp_keepalive ka;
3715 if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd,
3716 &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval))
3717 return NULL;
3718 if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka),
3719 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
3720 return set_error();
3721 }
3722 return PyLong_FromUnsignedLong(recv); }
3723 default:
3724 PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd);
3725 return NULL;
3726 }
Christian Heimesfaf2f632008-01-06 16:59:19 +00003727}
3728PyDoc_STRVAR(sock_ioctl_doc,
3729"ioctl(cmd, option) -> long\n\
3730\n\
Kristján Valur Jónsson847ec752009-09-27 21:10:38 +00003731Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\
3732SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\
3733SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval).");
Kristján Valur Jónsson10f383a2012-04-07 11:23:31 +00003734#endif
3735
3736#if defined(MS_WINDOWS)
3737static PyObject*
3738sock_share(PySocketSockObject *s, PyObject *arg)
3739{
3740 WSAPROTOCOL_INFO info;
3741 DWORD processId;
3742 int result;
3743
3744 if (!PyArg_ParseTuple(arg, "I", &processId))
3745 return NULL;
3746
3747 Py_BEGIN_ALLOW_THREADS
3748 result = WSADuplicateSocket(s->sock_fd, processId, &info);
3749 Py_END_ALLOW_THREADS
3750 if (result == SOCKET_ERROR)
3751 return set_error();
3752 return PyBytes_FromStringAndSize((const char*)&info, sizeof(info));
3753}
3754PyDoc_STRVAR(sock_share_doc,
3755"share(process_id) -> bytes\n\
3756\n\
3757Share the socket with another process. The target process id\n\
3758must be provided and the resulting bytes object passed to the target\n\
3759process. There the shared socket can be instantiated by calling\n\
3760socket.fromshare().");
3761
Christian Heimesfaf2f632008-01-06 16:59:19 +00003762
3763#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00003764
3765/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003766
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003767static PyMethodDef sock_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003768 {"_accept", (PyCFunction)sock_accept, METH_NOARGS,
3769 accept_doc},
3770 {"bind", (PyCFunction)sock_bind, METH_O,
3771 bind_doc},
3772 {"close", (PyCFunction)sock_close, METH_NOARGS,
3773 close_doc},
3774 {"connect", (PyCFunction)sock_connect, METH_O,
3775 connect_doc},
3776 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
3777 connect_ex_doc},
Antoine Pitrou6e451df2010-08-09 20:39:54 +00003778 {"detach", (PyCFunction)sock_detach, METH_NOARGS,
3779 detach_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003780 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
3781 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003782#ifdef HAVE_GETPEERNAME
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 {"getpeername", (PyCFunction)sock_getpeername,
3784 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00003785#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003786 {"getsockname", (PyCFunction)sock_getsockname,
3787 METH_NOARGS, getsockname_doc},
3788 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
3789 getsockopt_doc},
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00003790#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS,
3792 sock_ioctl_doc},
Christian Heimesfaf2f632008-01-06 16:59:19 +00003793#endif
Kristján Valur Jónsson10f383a2012-04-07 11:23:31 +00003794#if defined(MS_WINDOWS)
3795 {"share", (PyCFunction)sock_share, METH_VARARGS,
3796 sock_share_doc},
3797#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003798 {"listen", (PyCFunction)sock_listen, METH_O,
3799 listen_doc},
3800 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
3801 recv_doc},
3802 {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
3803 recv_into_doc},
3804 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
3805 recvfrom_doc},
3806 {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
3807 recvfrom_into_doc},
3808 {"send", (PyCFunction)sock_send, METH_VARARGS,
3809 send_doc},
3810 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
3811 sendall_doc},
3812 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
3813 sendto_doc},
3814 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
3815 setblocking_doc},
3816 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
3817 settimeout_doc},
3818 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
3819 gettimeout_doc},
3820 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
3821 setsockopt_doc},
3822 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
3823 shutdown_doc},
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003824#ifdef CMSG_LEN
3825 {"recvmsg", (PyCFunction)sock_recvmsg, METH_VARARGS,
3826 recvmsg_doc},
3827 {"recvmsg_into", (PyCFunction)sock_recvmsg_into, METH_VARARGS,
3828 recvmsg_into_doc,},
3829 {"sendmsg", (PyCFunction)sock_sendmsg, METH_VARARGS,
3830 sendmsg_doc},
3831#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003832 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003833};
3834
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003835/* SockObject members */
3836static PyMemberDef sock_memberlist[] = {
3837 {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},
3838 {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},
3839 {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},
3840 {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"},
3841 {0},
3842};
Guido van Rossum30a685f1991-06-27 15:51:29 +00003843
Guido van Rossum73624e91994-10-10 17:59:00 +00003844/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00003845 First close the file description. */
3846
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003847static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003848sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003849{
Antoine Pitroue033e062010-10-29 10:38:18 +00003850 if (s->sock_fd != -1) {
3851 PyObject *exc, *val, *tb;
3852 Py_ssize_t old_refcount = Py_REFCNT(s);
3853 ++Py_REFCNT(s);
3854 PyErr_Fetch(&exc, &val, &tb);
3855 if (PyErr_WarnFormat(PyExc_ResourceWarning, 1,
3856 "unclosed %R", s))
3857 /* Spurious errors can appear at shutdown */
3858 if (PyErr_ExceptionMatches(PyExc_Warning))
3859 PyErr_WriteUnraisable((PyObject *) s);
3860 PyErr_Restore(exc, val, tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003861 (void) SOCKETCLOSE(s->sock_fd);
Antoine Pitroue033e062010-10-29 10:38:18 +00003862 Py_REFCNT(s) = old_refcount;
3863 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003864 Py_TYPE(s)->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003865}
3866
Guido van Rossum30a685f1991-06-27 15:51:29 +00003867
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003868static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003869sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003870{
Fred Drakea04eaad2000-06-30 02:46:07 +00003871#if SIZEOF_SOCKET_T > SIZEOF_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003872 if (s->sock_fd > LONG_MAX) {
3873 /* this can occur on Win64, and actually there is a special
3874 ugly printf formatter for decimal pointer length integer
3875 printing, only bother if necessary*/
3876 PyErr_SetString(PyExc_OverflowError,
3877 "no printf formatter to display "
3878 "the socket descriptor in decimal");
3879 return NULL;
3880 }
Fred Drakea04eaad2000-06-30 02:46:07 +00003881#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003882 return PyUnicode_FromFormat(
3883 "<socket object, fd=%ld, family=%d, type=%d, proto=%d>",
3884 (long)s->sock_fd, s->sock_family,
3885 s->sock_type,
3886 s->sock_proto);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003887}
3888
3889
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003890/* Create a new, uninitialized socket object. */
3891
3892static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003893sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003895 PyObject *new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003897 new = type->tp_alloc(type, 0);
3898 if (new != NULL) {
3899 ((PySocketSockObject *)new)->sock_fd = -1;
3900 ((PySocketSockObject *)new)->sock_timeout = -1.0;
3901 ((PySocketSockObject *)new)->errorhandler = &set_error;
3902 }
3903 return new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003904}
3905
3906
3907/* Initialize a new socket object. */
3908
Victor Stinnerdaf45552013-08-28 00:53:59 +02003909#ifdef SOCK_CLOEXEC
3910/* socket() and socketpair() fail with EINVAL on Linux kernel older
3911 * than 2.6.27 if SOCK_CLOEXEC flag is set in the socket type. */
3912static int sock_cloexec_works = -1;
3913#endif
3914
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003915/*ARGSUSED*/
3916static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00003917sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003918{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003919 PySocketSockObject *s = (PySocketSockObject *)self;
3920 PyObject *fdobj = NULL;
3921 SOCKET_T fd = INVALID_SOCKET;
3922 int family = AF_INET, type = SOCK_STREAM, proto = 0;
3923 static char *keywords[] = {"family", "type", "proto", "fileno", 0};
Victor Stinnerdaf45552013-08-28 00:53:59 +02003924#ifndef MS_WINDOWS
3925#ifdef SOCK_CLOEXEC
3926 int *atomic_flag_works = &sock_cloexec_works;
3927#else
3928 int *atomic_flag_works = NULL;
3929#endif
3930#endif
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003932 if (!PyArg_ParseTupleAndKeywords(args, kwds,
3933 "|iiiO:socket", keywords,
3934 &family, &type, &proto, &fdobj))
3935 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003937 if (fdobj != NULL && fdobj != Py_None) {
Kristján Valur Jónsson10f383a2012-04-07 11:23:31 +00003938#ifdef MS_WINDOWS
3939 /* recreate a socket that was duplicated */
3940 if (PyBytes_Check(fdobj)) {
3941 WSAPROTOCOL_INFO info;
3942 if (PyBytes_GET_SIZE(fdobj) != sizeof(info)) {
3943 PyErr_Format(PyExc_ValueError,
3944 "socket descriptor string has wrong size, "
3945 "should be %zu bytes.", sizeof(info));
3946 return -1;
3947 }
3948 memcpy(&info, PyBytes_AS_STRING(fdobj), sizeof(info));
3949 Py_BEGIN_ALLOW_THREADS
3950 fd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
3951 FROM_PROTOCOL_INFO, &info, 0, WSA_FLAG_OVERLAPPED);
3952 Py_END_ALLOW_THREADS
3953 if (fd == INVALID_SOCKET) {
3954 set_error();
3955 return -1;
3956 }
3957 family = info.iAddressFamily;
3958 type = info.iSocketType;
3959 proto = info.iProtocol;
3960 }
3961 else
3962#endif
3963 {
3964 fd = PyLong_AsSocket_t(fdobj);
3965 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
3966 return -1;
3967 if (fd == INVALID_SOCKET) {
3968 PyErr_SetString(PyExc_ValueError,
3969 "can't use invalid socket value");
3970 return -1;
3971 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003972 }
3973 }
3974 else {
Victor Stinnerdaf45552013-08-28 00:53:59 +02003975#ifdef MS_WINDOWS
3976 /* Windows implementation */
3977#ifndef WSA_FLAG_NO_HANDLE_INHERIT
3978#define WSA_FLAG_NO_HANDLE_INHERIT 0x80
3979#endif
3980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 Py_BEGIN_ALLOW_THREADS
Victor Stinnerdaf45552013-08-28 00:53:59 +02003982 if (support_wsa_no_inherit) {
3983 fd = WSASocket(family, type, proto,
3984 NULL, 0,
3985 WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT);
3986 if (fd == INVALID_SOCKET) {
3987 /* Windows 7 or Windows 2008 R2 without SP1 or the hotfix */
3988 support_wsa_no_inherit = 0;
3989 fd = socket(family, type, proto);
3990 }
3991 }
3992 else {
3993 fd = socket(family, type, proto);
3994 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003995 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00003996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003997 if (fd == INVALID_SOCKET) {
3998 set_error();
3999 return -1;
4000 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02004001
4002 if (!support_wsa_no_inherit) {
4003 if (!SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0)) {
4004 closesocket(fd);
4005 PyErr_SetFromWindowsErr(0);
4006 return -1;
4007 }
4008 }
4009#else
4010 /* UNIX */
4011 Py_BEGIN_ALLOW_THREADS
4012#ifdef SOCK_CLOEXEC
4013 if (sock_cloexec_works != 0) {
4014 fd = socket(family, type | SOCK_CLOEXEC, proto);
4015 if (sock_cloexec_works == -1) {
4016 if (fd >= 0) {
4017 sock_cloexec_works = 1;
4018 }
4019 else if (errno == EINVAL) {
4020 /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */
4021 sock_cloexec_works = 0;
4022 fd = socket(family, type, proto);
4023 }
4024 }
4025 }
4026 else
4027#endif
4028 {
4029 fd = socket(family, type, proto);
4030 }
4031 Py_END_ALLOW_THREADS
4032
4033 if (fd == INVALID_SOCKET) {
4034 set_error();
4035 return -1;
4036 }
4037
4038 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
4039 SOCKETCLOSE(fd);
4040 return -1;
4041 }
4042#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 }
4044 init_sockobject(s, fd, family, type, proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00004045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004046 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004047
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004048}
4049
4050
Guido van Rossumb6775db1994-08-01 11:34:53 +00004051/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00004052
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004053static PyTypeObject sock_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
4055 "_socket.socket", /* tp_name */
4056 sizeof(PySocketSockObject), /* tp_basicsize */
4057 0, /* tp_itemsize */
4058 (destructor)sock_dealloc, /* tp_dealloc */
4059 0, /* tp_print */
4060 0, /* tp_getattr */
4061 0, /* tp_setattr */
4062 0, /* tp_reserved */
4063 (reprfunc)sock_repr, /* tp_repr */
4064 0, /* tp_as_number */
4065 0, /* tp_as_sequence */
4066 0, /* tp_as_mapping */
4067 0, /* tp_hash */
4068 0, /* tp_call */
4069 0, /* tp_str */
4070 PyObject_GenericGetAttr, /* tp_getattro */
4071 0, /* tp_setattro */
4072 0, /* tp_as_buffer */
4073 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4074 sock_doc, /* tp_doc */
4075 0, /* tp_traverse */
4076 0, /* tp_clear */
4077 0, /* tp_richcompare */
4078 0, /* tp_weaklistoffset */
4079 0, /* tp_iter */
4080 0, /* tp_iternext */
4081 sock_methods, /* tp_methods */
4082 sock_memberlist, /* tp_members */
4083 0, /* tp_getset */
4084 0, /* tp_base */
4085 0, /* tp_dict */
4086 0, /* tp_descr_get */
4087 0, /* tp_descr_set */
4088 0, /* tp_dictoffset */
4089 sock_initobj, /* tp_init */
4090 PyType_GenericAlloc, /* tp_alloc */
4091 sock_new, /* tp_new */
4092 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004093};
4094
Guido van Rossum30a685f1991-06-27 15:51:29 +00004095
Guido van Rossum81194471991-07-27 21:42:02 +00004096/* Python interface to gethostname(). */
4097
4098/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00004099static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004100socket_gethostname(PyObject *self, PyObject *unused)
Guido van Rossum81194471991-07-27 21:42:02 +00004101{
Martin v. Löwis72f48422010-10-29 18:20:08 +00004102#ifdef MS_WINDOWS
4103 /* Don't use winsock's gethostname, as this returns the ANSI
4104 version of the hostname, whereas we need a Unicode string.
4105 Otherwise, gethostname apparently also returns the DNS name. */
Hirokazu Yamamoto09fff7a2010-11-05 17:24:13 +00004106 wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1];
Victor Stinner63941882011-09-29 00:42:28 +02004107 DWORD size = Py_ARRAY_LENGTH(buf);
Victor Stinner74168972011-11-17 01:11:36 +01004108 wchar_t *name;
Hirokazu Yamamoto09fff7a2010-11-05 17:24:13 +00004109 PyObject *result;
Victor Stinner74168972011-11-17 01:11:36 +01004110
4111 if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size))
Victor Stinner9d3b93b2011-11-22 02:27:30 +01004112 return PyUnicode_FromWideChar(buf, size);
Victor Stinner74168972011-11-17 01:11:36 +01004113
4114 if (GetLastError() != ERROR_MORE_DATA)
4115 return PyErr_SetFromWindowsErr(0);
4116
4117 if (size == 0)
4118 return PyUnicode_New(0, 0);
4119
4120 /* MSDN says ERROR_MORE_DATA may occur because DNS allows longer
4121 names */
4122 name = PyMem_Malloc(size * sizeof(wchar_t));
4123 if (!name)
4124 return NULL;
4125 if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname,
4126 name,
4127 &size))
4128 {
4129 PyMem_Free(name);
4130 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis72f48422010-10-29 18:20:08 +00004131 }
Victor Stinner74168972011-11-17 01:11:36 +01004132
4133 result = PyUnicode_FromWideChar(name, size);
4134 PyMem_Free(name);
4135 return result;
Martin v. Löwis72f48422010-10-29 18:20:08 +00004136#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004137 char buf[1024];
4138 int res;
4139 Py_BEGIN_ALLOW_THREADS
4140 res = gethostname(buf, (int) sizeof buf - 1);
4141 Py_END_ALLOW_THREADS
4142 if (res < 0)
4143 return set_error();
4144 buf[sizeof buf - 1] = '\0';
Victor Stinnera534fc42013-06-03 22:07:27 +02004145 return PyUnicode_DecodeFSDefault(buf);
Martin v. Löwis72f48422010-10-29 18:20:08 +00004146#endif
Guido van Rossum81194471991-07-27 21:42:02 +00004147}
Guido van Rossumff4949e1992-08-05 19:58:53 +00004148
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004149PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004150"gethostname() -> string\n\
4151\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004152Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004153
Antoine Pitrou061cfb52011-02-28 22:25:22 +00004154#ifdef HAVE_SETHOSTNAME
4155PyDoc_STRVAR(sethostname_doc,
4156"sethostname(name)\n\n\
4157Sets the hostname to name.");
4158
4159static PyObject *
4160socket_sethostname(PyObject *self, PyObject *args)
4161{
4162 PyObject *hnobj;
4163 Py_buffer buf;
4164 int res, flag = 0;
4165
Christian Heimesd2774c72013-06-19 02:06:29 +02004166#ifdef _AIX
4167/* issue #18259, not declared in any useful header file */
4168extern int sethostname(const char *, size_t);
4169#endif
4170
Antoine Pitrou061cfb52011-02-28 22:25:22 +00004171 if (!PyArg_ParseTuple(args, "S:sethostname", &hnobj)) {
4172 PyErr_Clear();
4173 if (!PyArg_ParseTuple(args, "O&:sethostname",
4174 PyUnicode_FSConverter, &hnobj))
4175 return NULL;
4176 flag = 1;
4177 }
4178 res = PyObject_GetBuffer(hnobj, &buf, PyBUF_SIMPLE);
4179 if (!res) {
4180 res = sethostname(buf.buf, buf.len);
4181 PyBuffer_Release(&buf);
4182 }
4183 if (flag)
4184 Py_DECREF(hnobj);
4185 if (res)
4186 return set_error();
4187 Py_RETURN_NONE;
4188}
4189#endif
Guido van Rossumff4949e1992-08-05 19:58:53 +00004190
Guido van Rossum30a685f1991-06-27 15:51:29 +00004191/* Python interface to gethostbyname(name). */
4192
4193/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00004194static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004195socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00004196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004197 char *name;
4198 sock_addr_t addrbuf;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004199 PyObject *ret = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004200
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004201 if (!PyArg_ParseTuple(args, "et:gethostbyname", "idna", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004202 return NULL;
4203 if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004204 goto finally;
4205 ret = makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
4206finally:
4207 PyMem_Free(name);
4208 return ret;
Guido van Rossum30a685f1991-06-27 15:51:29 +00004209}
4210
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004211PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004212"gethostbyname(host) -> address\n\
4213\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004214Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004215
4216
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004217/* Convenience function common to gethostbyname_ex and gethostbyaddr */
4218
4219static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004220gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004222 char **pch;
4223 PyObject *rtn_tuple = (PyObject *)NULL;
4224 PyObject *name_list = (PyObject *)NULL;
4225 PyObject *addr_list = (PyObject *)NULL;
4226 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004228 if (h == NULL) {
4229 /* Let's get real error message to return */
4230 set_herror(h_errno);
4231 return NULL;
4232 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004234 if (h->h_addrtype != af) {
4235 /* Let's get real error message to return */
Victor Stinner1f33f2b2011-12-17 04:45:09 +01004236 errno = EAFNOSUPPORT;
4237 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004238 return NULL;
4239 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004241 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00004242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004243 case AF_INET:
4244 if (alen < sizeof(struct sockaddr_in))
4245 return NULL;
4246 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004247
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00004248#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004249 case AF_INET6:
4250 if (alen < sizeof(struct sockaddr_in6))
4251 return NULL;
4252 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004253#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00004254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004255 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004257 if ((name_list = PyList_New(0)) == NULL)
4258 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004260 if ((addr_list = PyList_New(0)) == NULL)
4261 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004263 /* SF #1511317: h_aliases can be NULL */
4264 if (h->h_aliases) {
4265 for (pch = h->h_aliases; *pch != NULL; pch++) {
4266 int status;
4267 tmp = PyUnicode_FromString(*pch);
4268 if (tmp == NULL)
4269 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004271 status = PyList_Append(name_list, tmp);
4272 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00004273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004274 if (status)
4275 goto err;
4276 }
4277 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004279 for (pch = h->h_addr_list; *pch != NULL; pch++) {
4280 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004282 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00004283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004284 case AF_INET:
4285 {
4286 struct sockaddr_in sin;
4287 memset(&sin, 0, sizeof(sin));
4288 sin.sin_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004289#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004290 sin.sin_len = sizeof(sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004291#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004292 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
4293 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00004294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004295 if (pch == h->h_addr_list && alen >= sizeof(sin))
4296 memcpy((char *) addr, &sin, sizeof(sin));
4297 break;
4298 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004299
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00004300#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004301 case AF_INET6:
4302 {
4303 struct sockaddr_in6 sin6;
4304 memset(&sin6, 0, sizeof(sin6));
4305 sin6.sin6_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004306#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004307 sin6.sin6_len = sizeof(sin6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004308#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004309 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
4310 tmp = makeipaddr((struct sockaddr *)&sin6,
4311 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00004312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004313 if (pch == h->h_addr_list && alen >= sizeof(sin6))
4314 memcpy((char *) addr, &sin6, sizeof(sin6));
4315 break;
4316 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004317#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00004318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004319 default: /* can't happen */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004320 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004321 "unsupported address family");
4322 return NULL;
4323 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 if (tmp == NULL)
4326 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004328 status = PyList_Append(addr_list, tmp);
4329 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00004330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004331 if (status)
4332 goto err;
4333 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004335 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00004336
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004337 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004338 Py_XDECREF(name_list);
4339 Py_XDECREF(addr_list);
4340 return rtn_tuple;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004341}
4342
4343
4344/* Python interface to gethostbyname_ex(name). */
4345
4346/*ARGSUSED*/
4347static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004348socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350 char *name;
4351 struct hostent *h;
Charles-François Natali8b759652011-12-23 16:44:51 +01004352 sock_addr_t addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 struct sockaddr *sa;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004354 PyObject *ret = NULL;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004355#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004356 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004357#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004359#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004360 char buf[16384];
4361 int buf_len = (sizeof buf) - 1;
4362 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004363#endif
Ross Lagerwall8c159762012-03-06 21:36:18 +02004364#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004365 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004366#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004367#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004368
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004369 if (!PyArg_ParseTuple(args, "et:gethostbyname_ex", "idna", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004370 return NULL;
Charles-François Natali8b759652011-12-23 16:44:51 +01004371 if (setipaddr(name, SAS2SA(&addr), sizeof(addr), AF_INET) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004372 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004373 Py_BEGIN_ALLOW_THREADS
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004374#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004375#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Ross Lagerwall8c159762012-03-06 21:36:18 +02004376 gethostbyname_r(name, &hp_allocated, buf, buf_len,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004378#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004379 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004380#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004381 memset((void *) &data, '\0', sizeof(data));
4382 result = gethostbyname_r(name, &hp_allocated, &data);
4383 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004384#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004385#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00004386#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004387 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004388#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004389 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004390#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004391 Py_END_ALLOW_THREADS
4392 /* Some C libraries would require addr.__ss_family instead of
4393 addr.ss_family.
4394 Therefore, we cast the sockaddr_storage into sockaddr to
4395 access sa_family. */
Charles-François Natali8b759652011-12-23 16:44:51 +01004396 sa = SAS2SA(&addr);
4397 ret = gethost_common(h, SAS2SA(&addr), sizeof(addr),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004398 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00004399#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004400 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00004401#endif
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004402finally:
4403 PyMem_Free(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004405}
4406
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004407PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004408"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
4409\n\
4410Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004411for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004412
4413
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004414/* Python interface to gethostbyaddr(IP). */
4415
4416/*ARGSUSED*/
4417static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004418socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004419{
Charles-François Natali8b759652011-12-23 16:44:51 +01004420 sock_addr_t addr;
4421 struct sockaddr *sa = SAS2SA(&addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 char *ip_num;
4423 struct hostent *h;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004424 PyObject *ret = NULL;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004425#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004426 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004427#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004429#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004430 /* glibcs up to 2.10 assume that the buf argument to
4431 gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc
4432 does not ensure. The attribute below instructs the compiler
4433 to maintain this alignment. */
4434 char buf[16384] Py_ALIGNED(8);
4435 int buf_len = (sizeof buf) - 1;
4436 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004437#endif
Ross Lagerwall8c159762012-03-06 21:36:18 +02004438#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004440#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004441#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004442 char *ap;
4443 int al;
4444 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004445
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004446 if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 return NULL;
4448 af = AF_UNSPEC;
4449 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004450 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004451 af = sa->sa_family;
4452 ap = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00004453 /* al = 0; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 switch (af) {
4455 case AF_INET:
4456 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
4457 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
4458 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00004459#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 case AF_INET6:
4461 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
4462 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
4463 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004464#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004465 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004466 PyErr_SetString(PyExc_OSError, "unsupported address family");
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004467 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004468 }
4469 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004470#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004471#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Ross Lagerwall8c159762012-03-06 21:36:18 +02004472 gethostbyaddr_r(ap, al, af,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004473 &hp_allocated, buf, buf_len,
4474 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004475#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004476 h = gethostbyaddr_r(ap, al, af,
4477 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004478#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004479 memset((void *) &data, '\0', sizeof(data));
4480 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
4481 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004482#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004483#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00004484#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004486#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004488#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 Py_END_ALLOW_THREADS
Charles-François Natali8b759652011-12-23 16:44:51 +01004490 ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00004491#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00004493#endif
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004494finally:
4495 PyMem_Free(ip_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004497}
4498
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004499PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004500"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
4501\n\
4502Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004503for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004504
Guido van Rossum30a685f1991-06-27 15:51:29 +00004505
4506/* Python interface to getservbyname(name).
4507 This only returns the port number, since the other info is already
4508 known or not useful (like the list of aliases). */
4509
4510/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00004511static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004512socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00004513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004514 char *name, *proto=NULL;
4515 struct servent *sp;
4516 if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
4517 return NULL;
4518 Py_BEGIN_ALLOW_THREADS
4519 sp = getservbyname(name, proto);
4520 Py_END_ALLOW_THREADS
4521 if (sp == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004522 PyErr_SetString(PyExc_OSError, "service/proto not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004523 return NULL;
4524 }
4525 return PyLong_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00004526}
4527
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004528PyDoc_STRVAR(getservbyname_doc,
Barry Warsaw11b91a02004-06-28 00:50:43 +00004529"getservbyname(servicename[, protocolname]) -> integer\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00004530\n\
4531Return a port number from a service name and protocol name.\n\
Barry Warsaw11b91a02004-06-28 00:50:43 +00004532The optional protocol name, if given, should be 'tcp' or 'udp',\n\
4533otherwise any protocol will match.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004534
Guido van Rossum30a685f1991-06-27 15:51:29 +00004535
Barry Warsaw11b91a02004-06-28 00:50:43 +00004536/* Python interface to getservbyport(port).
4537 This only returns the service name, since the other info is already
4538 known or not useful (like the list of aliases). */
4539
4540/*ARGSUSED*/
4541static PyObject *
4542socket_getservbyport(PyObject *self, PyObject *args)
4543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004544 int port;
4545 char *proto=NULL;
4546 struct servent *sp;
4547 if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto))
4548 return NULL;
4549 if (port < 0 || port > 0xffff) {
4550 PyErr_SetString(
4551 PyExc_OverflowError,
4552 "getservbyport: port must be 0-65535.");
4553 return NULL;
4554 }
4555 Py_BEGIN_ALLOW_THREADS
4556 sp = getservbyport(htons((short)port), proto);
4557 Py_END_ALLOW_THREADS
4558 if (sp == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004559 PyErr_SetString(PyExc_OSError, "port/proto not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004560 return NULL;
4561 }
4562 return PyUnicode_FromString(sp->s_name);
Barry Warsaw11b91a02004-06-28 00:50:43 +00004563}
4564
4565PyDoc_STRVAR(getservbyport_doc,
4566"getservbyport(port[, protocolname]) -> string\n\
4567\n\
4568Return the service name from a port number and protocol name.\n\
4569The optional protocol name, if given, should be 'tcp' or 'udp',\n\
4570otherwise any protocol will match.");
4571
Guido van Rossum3901d851996-12-19 16:35:04 +00004572/* Python interface to getprotobyname(name).
4573 This only returns the protocol number, since the other info is
4574 already known or not useful (like the list of aliases). */
4575
4576/*ARGSUSED*/
4577static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004578socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00004579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004580 char *name;
4581 struct protoent *sp;
4582 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
4583 return NULL;
4584 Py_BEGIN_ALLOW_THREADS
4585 sp = getprotobyname(name);
4586 Py_END_ALLOW_THREADS
4587 if (sp == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004588 PyErr_SetString(PyExc_OSError, "protocol not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 return NULL;
4590 }
4591 return PyLong_FromLong((long) sp->p_proto);
Guido van Rossum3901d851996-12-19 16:35:04 +00004592}
4593
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004594PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004595"getprotobyname(name) -> integer\n\
4596\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004597Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004598
Guido van Rossum3901d851996-12-19 16:35:04 +00004599
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004600#ifndef NO_DUP
4601/* dup() function for socket fds */
4602
4603static PyObject *
4604socket_dup(PyObject *self, PyObject *fdobj)
4605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 SOCKET_T fd, newfd;
4607 PyObject *newfdobj;
Victor Stinnerdaf45552013-08-28 00:53:59 +02004608#ifdef MS_WINDOWS
4609 WSAPROTOCOL_INFO info;
4610#endif
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004612 fd = PyLong_AsSocket_t(fdobj);
4613 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
4614 return NULL;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004615
Victor Stinnerdaf45552013-08-28 00:53:59 +02004616#ifdef MS_WINDOWS
4617 if (WSADuplicateSocket(fd, GetCurrentProcessId(), &info))
4618 return set_error();
4619
4620 newfd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
4621 FROM_PROTOCOL_INFO,
4622 &info, 0, WSA_FLAG_OVERLAPPED);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004623 if (newfd == INVALID_SOCKET)
4624 return set_error();
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004625
Victor Stinnerdaf45552013-08-28 00:53:59 +02004626 if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) {
4627 closesocket(newfd);
4628 PyErr_SetFromWindowsErr(0);
4629 return NULL;
4630 }
4631#else
4632 /* On UNIX, dup can be used to duplicate the file descriptor of a socket */
4633 newfd = _Py_dup(fd);
4634 if (newfd == INVALID_SOCKET)
Victor Stinnere990c6e2013-11-16 00:18:58 +01004635 return NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02004636#endif
4637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004638 newfdobj = PyLong_FromSocket_t(newfd);
4639 if (newfdobj == NULL)
4640 SOCKETCLOSE(newfd);
4641 return newfdobj;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004642}
4643
4644PyDoc_STRVAR(dup_doc,
4645"dup(integer) -> integer\n\
4646\n\
4647Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\
4648sockets; on some platforms os.dup() won't work for socket file descriptors.");
4649#endif
4650
4651
Dave Cole331708b2004-08-09 04:51:41 +00004652#ifdef HAVE_SOCKETPAIR
4653/* Create a pair of sockets using the socketpair() function.
Dave Cole07fda7e2004-08-23 05:16:23 +00004654 Arguments as for socket() except the default family is AF_UNIX if
Dave Colee8bbfe42004-08-26 00:51:16 +00004655 defined on the platform; otherwise, the default is AF_INET. */
Dave Cole331708b2004-08-09 04:51:41 +00004656
4657/*ARGSUSED*/
4658static PyObject *
4659socket_socketpair(PyObject *self, PyObject *args)
4660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 PySocketSockObject *s0 = NULL, *s1 = NULL;
4662 SOCKET_T sv[2];
4663 int family, type = SOCK_STREAM, proto = 0;
4664 PyObject *res = NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02004665#ifdef SOCK_CLOEXEC
4666 int *atomic_flag_works = &sock_cloexec_works;
4667#else
4668 int *atomic_flag_works = NULL;
4669#endif
4670 int ret;
Dave Cole331708b2004-08-09 04:51:41 +00004671
4672#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004673 family = AF_UNIX;
Dave Cole331708b2004-08-09 04:51:41 +00004674#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004675 family = AF_INET;
Dave Cole331708b2004-08-09 04:51:41 +00004676#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004677 if (!PyArg_ParseTuple(args, "|iii:socketpair",
4678 &family, &type, &proto))
4679 return NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02004680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004681 /* Create a pair of socket fds */
Victor Stinnerdaf45552013-08-28 00:53:59 +02004682 Py_BEGIN_ALLOW_THREADS
4683#ifdef SOCK_CLOEXEC
4684 if (sock_cloexec_works != 0) {
4685 ret = socketpair(family, type | SOCK_CLOEXEC, proto, sv);
4686 if (sock_cloexec_works == -1) {
4687 if (ret >= 0) {
4688 sock_cloexec_works = 1;
4689 }
4690 else if (errno == EINVAL) {
4691 /* Linux older than 2.6.27 does not support SOCK_CLOEXEC */
4692 sock_cloexec_works = 0;
4693 ret = socketpair(family, type, proto, sv);
4694 }
4695 }
4696 }
4697 else
4698#endif
4699 {
4700 ret = socketpair(family, type, proto, sv);
4701 }
4702 Py_END_ALLOW_THREADS
4703
4704 if (ret < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004705 return set_error();
Victor Stinnerdaf45552013-08-28 00:53:59 +02004706
4707 if (_Py_set_inheritable(sv[0], 0, atomic_flag_works) < 0)
4708 goto finally;
4709 if (_Py_set_inheritable(sv[1], 0, atomic_flag_works) < 0)
4710 goto finally;
4711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004712 s0 = new_sockobject(sv[0], family, type, proto);
4713 if (s0 == NULL)
4714 goto finally;
4715 s1 = new_sockobject(sv[1], family, type, proto);
4716 if (s1 == NULL)
4717 goto finally;
4718 res = PyTuple_Pack(2, s0, s1);
Dave Cole331708b2004-08-09 04:51:41 +00004719
4720finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004721 if (res == NULL) {
4722 if (s0 == NULL)
4723 SOCKETCLOSE(sv[0]);
4724 if (s1 == NULL)
4725 SOCKETCLOSE(sv[1]);
4726 }
4727 Py_XDECREF(s0);
4728 Py_XDECREF(s1);
4729 return res;
Dave Cole331708b2004-08-09 04:51:41 +00004730}
4731
4732PyDoc_STRVAR(socketpair_doc,
Victor Stinnerdaf45552013-08-28 00:53:59 +02004733"socketpair([family[, type [, proto]]]) -> (socket object, socket object)\n\
Dave Cole331708b2004-08-09 04:51:41 +00004734\n\
4735Create a pair of socket objects from the sockets returned by the platform\n\
4736socketpair() function.\n\
Dave Cole07fda7e2004-08-23 05:16:23 +00004737The arguments are the same as for socket() except the default family is\n\
Dave Colee8bbfe42004-08-26 00:51:16 +00004738AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
Dave Cole331708b2004-08-09 04:51:41 +00004739
4740#endif /* HAVE_SOCKETPAIR */
4741
4742
Guido van Rossum006bf911996-06-12 04:04:55 +00004743static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004744socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00004745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004746 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00004747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004748 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
4749 return NULL;
4750 }
4751 if (x1 < 0) {
4752 PyErr_SetString(PyExc_OverflowError,
4753 "can't convert negative number to unsigned long");
4754 return NULL;
4755 }
4756 x2 = (unsigned int)ntohs((unsigned short)x1);
4757 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00004758}
4759
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004760PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004761"ntohs(integer) -> integer\n\
4762\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004763Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004764
4765
Guido van Rossum006bf911996-06-12 04:04:55 +00004766static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004767socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00004768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004769 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00004770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004771 if (PyLong_Check(arg)) {
4772 x = PyLong_AsUnsignedLong(arg);
4773 if (x == (unsigned long) -1 && PyErr_Occurred())
4774 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004775#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004776 {
4777 unsigned long y;
4778 /* only want the trailing 32 bits */
4779 y = x & 0xFFFFFFFFUL;
4780 if (y ^ x)
4781 return PyErr_Format(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03004782 "int larger than 32 bits");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004783 x = y;
4784 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004785#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004786 }
4787 else
4788 return PyErr_Format(PyExc_TypeError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03004789 "expected int, %s found",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004790 Py_TYPE(arg)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004791 return PyLong_FromUnsignedLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00004792}
4793
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004794PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004795"ntohl(integer) -> integer\n\
4796\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004797Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004798
4799
Guido van Rossum006bf911996-06-12 04:04:55 +00004800static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004801socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00004802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004803 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00004804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004805 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
4806 return NULL;
4807 }
4808 if (x1 < 0) {
4809 PyErr_SetString(PyExc_OverflowError,
4810 "can't convert negative number to unsigned long");
4811 return NULL;
4812 }
4813 x2 = (unsigned int)htons((unsigned short)x1);
4814 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00004815}
4816
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004817PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004818"htons(integer) -> integer\n\
4819\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004820Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004821
4822
Guido van Rossum006bf911996-06-12 04:04:55 +00004823static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004824socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00004825{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004826 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00004827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004828 if (PyLong_Check(arg)) {
4829 x = PyLong_AsUnsignedLong(arg);
4830 if (x == (unsigned long) -1 && PyErr_Occurred())
4831 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004832#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004833 {
4834 unsigned long y;
4835 /* only want the trailing 32 bits */
4836 y = x & 0xFFFFFFFFUL;
4837 if (y ^ x)
4838 return PyErr_Format(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03004839 "int larger than 32 bits");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004840 x = y;
4841 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004842#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004843 }
4844 else
4845 return PyErr_Format(PyExc_TypeError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03004846 "expected int, %s found",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004847 Py_TYPE(arg)->tp_name);
4848 return PyLong_FromUnsignedLong(htonl((unsigned long)x));
Guido van Rossum006bf911996-06-12 04:04:55 +00004849}
4850
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004851PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004852"htonl(integer) -> integer\n\
4853\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004854Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004855
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004856/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004857
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004858PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum7d0a8262007-05-21 23:13:11 +00004859"inet_aton(string) -> bytes giving packed 32-bit IP representation\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004860\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00004861Convert 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 +00004862binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004863
4864static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004865socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004866{
Neal Norwitz88f115b2003-02-13 02:15:42 +00004867#ifdef HAVE_INET_ATON
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004868 struct in_addr buf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00004869#endif
4870
4871#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
Benjamin Petersonf91df042009-02-13 02:50:59 +00004872#if (SIZEOF_INT != 4)
4873#error "Not sure if in_addr_t exists and int is not 32-bits."
4874#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004875 /* Have to use inet_addr() instead */
4876 unsigned int packed_addr;
Tim Peters1df9fdd2003-02-13 03:13:40 +00004877#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004878 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00004879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004880 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
4881 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00004882
Tim Peters1df9fdd2003-02-13 03:13:40 +00004883
4884#ifdef HAVE_INET_ATON
Thomas Wouters477c8d52006-05-27 19:21:47 +00004885
4886#ifdef USE_INET_ATON_WEAKLINK
4887 if (inet_aton != NULL) {
4888#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004889 if (inet_aton(ip_addr, &buf))
4890 return PyBytes_FromStringAndSize((char *)(&buf),
4891 sizeof(buf));
Guido van Rossumad05cdf2003-02-12 23:08:22 +00004892
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004893 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004894 "illegal IP address string passed to inet_aton");
4895 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00004896
Thomas Wouters477c8d52006-05-27 19:21:47 +00004897#ifdef USE_INET_ATON_WEAKLINK
4898 } else {
4899#endif
4900
4901#endif
4902
4903#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
4904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004905 /* special-case this address as inet_addr might return INADDR_NONE
4906 * for this */
4907 if (strcmp(ip_addr, "255.255.255.255") == 0) {
Charles-François Natali24aa0412013-08-31 14:48:25 +02004908 packed_addr = INADDR_BROADCAST;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004909 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00004910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004911 packed_addr = inet_addr(ip_addr);
Guido van Rossumc6a164b1999-08-20 19:11:27 +00004912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004913 if (packed_addr == INADDR_NONE) { /* invalid address */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004914 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004915 "illegal IP address string passed to inet_aton");
4916 return NULL;
4917 }
4918 }
4919 return PyBytes_FromStringAndSize((char *) &packed_addr,
4920 sizeof(packed_addr));
Thomas Wouters477c8d52006-05-27 19:21:47 +00004921
4922#ifdef USE_INET_ATON_WEAKLINK
4923 }
4924#endif
4925
Guido van Rossumad05cdf2003-02-12 23:08:22 +00004926#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004927}
4928
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004929PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00004930"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004931\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004932Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004933
4934static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004935socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004937 char *packed_str;
4938 int addr_len;
4939 struct in_addr packed_addr;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004941 if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) {
4942 return NULL;
4943 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00004944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004945 if (addr_len != sizeof(packed_addr)) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004946 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004947 "packed IP wrong length for inet_ntoa");
4948 return NULL;
4949 }
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004951 memcpy(&packed_addr, packed_str, addr_len);
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004953 return PyUnicode_FromString(inet_ntoa(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004954}
Guido van Rossum82a5c661998-07-07 20:45:43 +00004955
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09004956#if defined(HAVE_INET_PTON) || defined(MS_WINDOWS)
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004957
4958PyDoc_STRVAR(inet_pton_doc,
4959"inet_pton(af, ip) -> packed IP address string\n\
4960\n\
4961Convert an IP address from string format to a packed string suitable\n\
4962for use with low-level network functions.");
4963
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09004964#endif
4965
4966#ifdef HAVE_INET_PTON
4967
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004968static PyObject *
4969socket_inet_pton(PyObject *self, PyObject *args)
4970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004971 int af;
4972 char* ip;
4973 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00004974#ifdef ENABLE_IPV6
Victor Stinner640c35c2013-06-04 23:14:37 +02004975 char packed[Py_MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00004976#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004977 char packed[sizeof(struct in_addr)];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00004978#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004979 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
4980 return NULL;
4981 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004982
Martin v. Löwis04697e82004-06-02 12:35:29 +00004983#if !defined(ENABLE_IPV6) && defined(AF_INET6)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004984 if(af == AF_INET6) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004985 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004986 "can't use AF_INET6, IPv6 is disabled");
4987 return NULL;
4988 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004989#endif
Martin v. Löwis10649092003-08-05 06:25:06 +00004990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004991 retval = inet_pton(af, ip, packed);
4992 if (retval < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004993 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004994 return NULL;
4995 } else if (retval == 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004996 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004997 "illegal IP address string passed to inet_pton");
4998 return NULL;
4999 } else if (af == AF_INET) {
5000 return PyBytes_FromStringAndSize(packed,
5001 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005002#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005003 } else if (af == AF_INET6) {
5004 return PyBytes_FromStringAndSize(packed,
5005 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005006#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005007 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005008 PyErr_SetString(PyExc_OSError, "unknown address family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005009 return NULL;
5010 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005011}
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005012#elif defined(MS_WINDOWS)
5013
5014static PyObject *
5015socket_inet_pton(PyObject *self, PyObject *args)
5016{
5017 int af;
5018 char* ip;
5019 struct sockaddr_in6 addr;
5020 INT ret, size;
5021
5022 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
5023 return NULL;
5024 }
5025
Victor Stinnere990c6e2013-11-16 00:18:58 +01005026 size = sizeof(addr);
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005027 ret = WSAStringToAddressA(ip, af, NULL, (LPSOCKADDR)&addr, &size);
5028
5029 if (ret) {
5030 PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
5031 return NULL;
5032 } else if(af == AF_INET) {
5033 struct sockaddr_in *addr4 = (struct sockaddr_in*)&addr;
Victor Stinnere990c6e2013-11-16 00:18:58 +01005034 return PyBytes_FromStringAndSize((const char *)&(addr4->sin_addr),
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005035 sizeof(addr4->sin_addr));
5036 } else if (af == AF_INET6) {
Victor Stinnere990c6e2013-11-16 00:18:58 +01005037 return PyBytes_FromStringAndSize((const char *)&(addr.sin6_addr),
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005038 sizeof(addr.sin6_addr));
5039 } else {
5040 PyErr_SetString(PyExc_OSError, "unknown address family");
5041 return NULL;
5042 }
5043}
5044
5045#endif
5046
5047#if defined(HAVE_INET_PTON) || defined(MS_WINDOWS)
Thomas Wouters477c8d52006-05-27 19:21:47 +00005048
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005049PyDoc_STRVAR(inet_ntop_doc,
5050"inet_ntop(af, packed_ip) -> string formatted IP address\n\
5051\n\
5052Convert a packed IP address of the given family to string format.");
5053
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005054#endif
5055
5056
5057#ifdef HAVE_INET_PTON
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005058static PyObject *
5059socket_inet_ntop(PyObject *self, PyObject *args)
5060{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005061 int af;
5062 char* packed;
5063 int len;
5064 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005065#ifdef ENABLE_IPV6
Victor Stinner640c35c2013-06-04 23:14:37 +02005066 char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005067#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005068 char ip[INET_ADDRSTRLEN + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005069#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00005070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005071 /* Guarantee NUL-termination for PyUnicode_FromString() below */
5072 memset((void *) &ip[0], '\0', sizeof(ip));
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005074 if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) {
5075 return NULL;
5076 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005078 if (af == AF_INET) {
5079 if (len != sizeof(struct in_addr)) {
5080 PyErr_SetString(PyExc_ValueError,
5081 "invalid length of packed IP address string");
5082 return NULL;
5083 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005084#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005085 } else if (af == AF_INET6) {
5086 if (len != sizeof(struct in6_addr)) {
5087 PyErr_SetString(PyExc_ValueError,
5088 "invalid length of packed IP address string");
5089 return NULL;
5090 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00005091#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005092 } else {
5093 PyErr_Format(PyExc_ValueError,
5094 "unknown address family %d", af);
5095 return NULL;
5096 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005098 retval = inet_ntop(af, packed, ip, sizeof(ip));
5099 if (!retval) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005100 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005101 return NULL;
5102 } else {
5103 return PyUnicode_FromString(retval);
5104 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005106 /* NOTREACHED */
5107 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
5108 return NULL;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005109}
5110
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005111#elif defined(MS_WINDOWS)
5112
5113static PyObject *
5114socket_inet_ntop(PyObject *self, PyObject *args)
5115{
5116 int af;
5117 char* packed;
5118 int len;
5119 struct sockaddr_in6 addr;
5120 DWORD addrlen, ret, retlen;
Jason R. Coombs8ec784c2013-11-10 13:43:22 -05005121#ifdef ENABLE_IPV6
5122 char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
5123#else
5124 char ip[INET_ADDRSTRLEN + 1];
5125#endif
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005126
5127 /* Guarantee NUL-termination for PyUnicode_FromString() below */
5128 memset((void *) &ip[0], '\0', sizeof(ip));
5129
5130 if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) {
5131 return NULL;
5132 }
5133
5134 if (af == AF_INET) {
5135 struct sockaddr_in * addr4 = (struct sockaddr_in *)&addr;
5136
5137 if (len != sizeof(struct in_addr)) {
5138 PyErr_SetString(PyExc_ValueError,
5139 "invalid length of packed IP address string");
5140 return NULL;
5141 }
5142 memset(addr4, 0, sizeof(struct sockaddr_in));
5143 addr4->sin_family = AF_INET;
5144 memcpy(&(addr4->sin_addr), packed, sizeof(addr4->sin_addr));
5145 addrlen = sizeof(struct sockaddr_in);
5146 } else if (af == AF_INET6) {
5147 if (len != sizeof(struct in6_addr)) {
5148 PyErr_SetString(PyExc_ValueError,
5149 "invalid length of packed IP address string");
5150 return NULL;
5151 }
5152
5153 memset(&addr, 0, sizeof(addr));
5154 addr.sin6_family = AF_INET6;
5155 memcpy(&(addr.sin6_addr), packed, sizeof(addr.sin6_addr));
5156 addrlen = sizeof(addr);
5157 } else {
5158 PyErr_Format(PyExc_ValueError,
5159 "unknown address family %d", af);
5160 return NULL;
5161 }
5162
5163 retlen = sizeof(ip);
Victor Stinnere990c6e2013-11-16 00:18:58 +01005164 ret = WSAAddressToStringA((struct sockaddr*)&addr, addrlen, NULL,
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005165 ip, &retlen);
5166
5167 if (ret) {
5168 PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
5169 return NULL;
5170 } else {
5171 return PyUnicode_FromString(ip);
5172 }
5173}
5174
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005175#endif /* HAVE_INET_PTON */
5176
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005177/* Python interface to getaddrinfo(host, port). */
5178
5179/*ARGSUSED*/
5180static PyObject *
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005181socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005182{
Victor Stinner77af1722011-05-26 14:05:59 +02005183 static char* kwnames[] = {"host", "port", "family", "type", "proto",
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005184 "flags", 0};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005185 struct addrinfo hints, *res;
5186 struct addrinfo *res0 = NULL;
5187 PyObject *hobj = NULL;
5188 PyObject *pobj = (PyObject *)NULL;
5189 char pbuf[30];
5190 char *hptr, *pptr;
5191 int family, socktype, protocol, flags;
5192 int error;
5193 PyObject *all = (PyObject *)NULL;
5194 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005195
Georg Brandl6083a4b2013-10-14 06:51:46 +02005196 socktype = protocol = flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005197 family = AF_UNSPEC;
Victor Stinner77af1722011-05-26 14:05:59 +02005198 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iiii:getaddrinfo",
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005199 kwnames, &hobj, &pobj, &family, &socktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005200 &protocol, &flags)) {
5201 return NULL;
5202 }
5203 if (hobj == Py_None) {
5204 hptr = NULL;
5205 } else if (PyUnicode_Check(hobj)) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005206 _Py_IDENTIFIER(encode);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005207
5208 idna = _PyObject_CallMethodId(hobj, &PyId_encode, "s", "idna");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005209 if (!idna)
5210 return NULL;
5211 assert(PyBytes_Check(idna));
5212 hptr = PyBytes_AS_STRING(idna);
5213 } else if (PyBytes_Check(hobj)) {
5214 hptr = PyBytes_AsString(hobj);
5215 } else {
5216 PyErr_SetString(PyExc_TypeError,
5217 "getaddrinfo() argument 1 must be string or None");
5218 return NULL;
5219 }
5220 if (PyLong_CheckExact(pobj)) {
5221 long value = PyLong_AsLong(pobj);
5222 if (value == -1 && PyErr_Occurred())
5223 goto err;
5224 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value);
5225 pptr = pbuf;
5226 } else if (PyUnicode_Check(pobj)) {
5227 pptr = _PyUnicode_AsString(pobj);
Alexander Belopolskye239d232010-12-08 23:31:48 +00005228 if (pptr == NULL)
5229 goto err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005230 } else if (PyBytes_Check(pobj)) {
Alexander Belopolskye239d232010-12-08 23:31:48 +00005231 pptr = PyBytes_AS_STRING(pobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005232 } else if (pobj == Py_None) {
5233 pptr = (char *)NULL;
5234 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005235 PyErr_SetString(PyExc_OSError, "Int or String expected");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005236 goto err;
5237 }
Ronald Oussorena822d3662013-06-10 10:36:28 +02005238#if defined(__APPLE__) && defined(AI_NUMERICSERV)
Ronald Oussoren27a4ac52013-05-24 13:47:37 +02005239 if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) {
5240 /* On OSX upto at least OSX 10.8 getaddrinfo crashes
Christian Heimesaf01f662013-12-21 16:19:10 +01005241 * if AI_NUMERICSERV is set and the servname is NULL or "0".
5242 * This workaround avoids a segfault in libsystem.
5243 */
Ronald Oussoren27a4ac52013-05-24 13:47:37 +02005244 pptr = "00";
5245 }
5246#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005247 memset(&hints, 0, sizeof(hints));
5248 hints.ai_family = family;
5249 hints.ai_socktype = socktype;
5250 hints.ai_protocol = protocol;
5251 hints.ai_flags = flags;
5252 Py_BEGIN_ALLOW_THREADS
5253 ACQUIRE_GETADDRINFO_LOCK
5254 error = getaddrinfo(hptr, pptr, &hints, &res0);
5255 Py_END_ALLOW_THREADS
5256 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
5257 if (error) {
5258 set_gaierror(error);
5259 goto err;
5260 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005262 if ((all = PyList_New(0)) == NULL)
5263 goto err;
5264 for (res = res0; res; res = res->ai_next) {
5265 PyObject *single;
5266 PyObject *addr =
5267 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
5268 if (addr == NULL)
5269 goto err;
5270 single = Py_BuildValue("iiisO", res->ai_family,
5271 res->ai_socktype, res->ai_protocol,
5272 res->ai_canonname ? res->ai_canonname : "",
5273 addr);
5274 Py_DECREF(addr);
5275 if (single == NULL)
5276 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005278 if (PyList_Append(all, single))
5279 goto err;
5280 Py_XDECREF(single);
5281 }
5282 Py_XDECREF(idna);
5283 if (res0)
5284 freeaddrinfo(res0);
5285 return all;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005286 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005287 Py_XDECREF(all);
5288 Py_XDECREF(idna);
5289 if (res0)
5290 freeaddrinfo(res0);
5291 return (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005292}
5293
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005294PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005295"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
5296 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005297\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005298Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005299
5300/* Python interface to getnameinfo(sa, flags). */
5301
5302/*ARGSUSED*/
5303static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005304socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005306 PyObject *sa = (PyObject *)NULL;
5307 int flags;
5308 char *hostp;
Charles-François Natali366999a2012-01-02 15:47:29 +01005309 int port;
5310 unsigned int flowinfo, scope_id;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005311 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
5312 struct addrinfo hints, *res = NULL;
5313 int error;
5314 PyObject *ret = (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005316 flags = flowinfo = scope_id = 0;
5317 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
5318 return NULL;
5319 if (!PyTuple_Check(sa)) {
5320 PyErr_SetString(PyExc_TypeError,
5321 "getnameinfo() argument 1 must be a tuple");
5322 return NULL;
5323 }
Charles-François Natali366999a2012-01-02 15:47:29 +01005324 if (!PyArg_ParseTuple(sa, "si|II",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005325 &hostp, &port, &flowinfo, &scope_id))
5326 return NULL;
Charles-François Natali9b0c0062012-06-23 10:17:05 +02005327 if (flowinfo > 0xfffff) {
Charles-François Natali366999a2012-01-02 15:47:29 +01005328 PyErr_SetString(PyExc_OverflowError,
5329 "getsockaddrarg: flowinfo must be 0-1048575.");
5330 return NULL;
5331 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005332 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
5333 memset(&hints, 0, sizeof(hints));
5334 hints.ai_family = AF_UNSPEC;
5335 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Martin v. Löwis112c0f32010-08-25 07:38:15 +00005336 hints.ai_flags = AI_NUMERICHOST; /* don't do any name resolution */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005337 Py_BEGIN_ALLOW_THREADS
5338 ACQUIRE_GETADDRINFO_LOCK
5339 error = getaddrinfo(hostp, pbuf, &hints, &res);
5340 Py_END_ALLOW_THREADS
5341 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
5342 if (error) {
5343 set_gaierror(error);
5344 goto fail;
5345 }
5346 if (res->ai_next) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005347 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005348 "sockaddr resolved to multiple addresses");
5349 goto fail;
5350 }
5351 switch (res->ai_family) {
5352 case AF_INET:
5353 {
5354 if (PyTuple_GET_SIZE(sa) != 2) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005355 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005356 "IPv4 sockaddr must be 2 tuple");
5357 goto fail;
5358 }
5359 break;
5360 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00005361#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005362 case AF_INET6:
5363 {
5364 struct sockaddr_in6 *sin6;
5365 sin6 = (struct sockaddr_in6 *)res->ai_addr;
Charles-François Natali366999a2012-01-02 15:47:29 +01005366 sin6->sin6_flowinfo = htonl(flowinfo);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005367 sin6->sin6_scope_id = scope_id;
5368 break;
5369 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005370#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005371 }
Antoine Pitrou19467d22010-08-17 19:33:30 +00005372 error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005373 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
5374 if (error) {
5375 set_gaierror(error);
5376 goto fail;
5377 }
5378 ret = Py_BuildValue("ss", hbuf, pbuf);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005379
5380fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005381 if (res)
5382 freeaddrinfo(res);
5383 return ret;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005384}
5385
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005386PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005387"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005388\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005389Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005390
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005391
5392/* Python API to getting and setting the default timeout value. */
5393
5394static PyObject *
5395socket_getdefaulttimeout(PyObject *self)
5396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005397 if (defaulttimeout < 0.0) {
5398 Py_INCREF(Py_None);
5399 return Py_None;
5400 }
5401 else
5402 return PyFloat_FromDouble(defaulttimeout);
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005403}
5404
5405PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005406"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005407\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03005408Returns the default timeout in seconds (float) for new socket objects.\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005409A value of None indicates that new socket objects have no timeout.\n\
5410When the socket module is first imported, the default is None.");
5411
5412static PyObject *
5413socket_setdefaulttimeout(PyObject *self, PyObject *arg)
5414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005415 double timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005417 if (arg == Py_None)
5418 timeout = -1.0;
5419 else {
5420 timeout = PyFloat_AsDouble(arg);
5421 if (timeout < 0.0) {
5422 if (!PyErr_Occurred())
5423 PyErr_SetString(PyExc_ValueError,
5424 "Timeout value out of range");
5425 return NULL;
5426 }
5427 }
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005429 defaulttimeout = timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005431 Py_INCREF(Py_None);
5432 return Py_None;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005433}
5434
5435PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005436"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005437\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03005438Set the default timeout in seconds (float) for new socket objects.\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005439A value of None indicates that new socket objects have no timeout.\n\
5440When the socket module is first imported, the default is None.");
5441
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005442#ifdef HAVE_IF_NAMEINDEX
5443/* Python API for getting interface indices and names */
5444
5445static PyObject *
5446socket_if_nameindex(PyObject *self, PyObject *arg)
5447{
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005448 PyObject *list;
Charles-François Natali60713592011-05-20 16:55:06 +02005449 int i;
5450 struct if_nameindex *ni;
Victor Stinner77af1722011-05-26 14:05:59 +02005451
Charles-François Natali60713592011-05-20 16:55:06 +02005452 ni = if_nameindex();
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005453 if (ni == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005454 PyErr_SetFromErrno(PyExc_OSError);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005455 return NULL;
5456 }
5457
5458 list = PyList_New(0);
5459 if (list == NULL) {
5460 if_freenameindex(ni);
5461 return NULL;
5462 }
5463
Charles-François Natali60713592011-05-20 16:55:06 +02005464 for (i = 0; ni[i].if_index != 0 && i < INT_MAX; i++) {
5465 PyObject *ni_tuple = Py_BuildValue("IO&",
5466 ni[i].if_index, PyUnicode_DecodeFSDefault, ni[i].if_name);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005467
5468 if (ni_tuple == NULL || PyList_Append(list, ni_tuple) == -1) {
5469 Py_XDECREF(ni_tuple);
Charles-François Natali60713592011-05-20 16:55:06 +02005470 Py_DECREF(list);
5471 if_freenameindex(ni);
5472 return NULL;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005473 }
5474 Py_DECREF(ni_tuple);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005475 }
5476
5477 if_freenameindex(ni);
5478 return list;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005479}
5480
5481PyDoc_STRVAR(if_nameindex_doc,
5482"if_nameindex()\n\
5483\n\
5484Returns a list of network interface information (index, name) tuples.");
5485
Charles-François Natali60713592011-05-20 16:55:06 +02005486static PyObject *
5487socket_if_nametoindex(PyObject *self, PyObject *args)
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005488{
Charles-François Natali60713592011-05-20 16:55:06 +02005489 PyObject *oname;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005490 unsigned long index;
5491
Charles-François Natali60713592011-05-20 16:55:06 +02005492 if (!PyArg_ParseTuple(args, "O&:if_nametoindex",
5493 PyUnicode_FSConverter, &oname))
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005494 return NULL;
5495
Charles-François Natali60713592011-05-20 16:55:06 +02005496 index = if_nametoindex(PyBytes_AS_STRING(oname));
5497 Py_DECREF(oname);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005498 if (index == 0) {
Charles-François Natali60713592011-05-20 16:55:06 +02005499 /* if_nametoindex() doesn't set errno */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005500 PyErr_SetString(PyExc_OSError, "no interface with this name");
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005501 return NULL;
5502 }
5503
5504 return PyLong_FromUnsignedLong(index);
5505}
5506
5507PyDoc_STRVAR(if_nametoindex_doc,
5508"if_nametoindex(if_name)\n\
5509\n\
5510Returns the interface index corresponding to the interface name if_name.");
5511
Charles-François Natali60713592011-05-20 16:55:06 +02005512static PyObject *
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005513socket_if_indextoname(PyObject *self, PyObject *arg)
5514{
Charles-François Natali60713592011-05-20 16:55:06 +02005515 unsigned long index;
5516 char name[IF_NAMESIZE + 1];
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005517
Charles-François Natali60713592011-05-20 16:55:06 +02005518 index = PyLong_AsUnsignedLong(arg);
5519 if (index == (unsigned long) -1)
5520 return NULL;
5521
5522 if (if_indextoname(index, name) == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005523 PyErr_SetFromErrno(PyExc_OSError);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005524 return NULL;
5525 }
5526
Charles-François Natali60713592011-05-20 16:55:06 +02005527 return PyUnicode_DecodeFSDefault(name);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005528}
5529
5530PyDoc_STRVAR(if_indextoname_doc,
5531"if_indextoname(if_index)\n\
5532\n\
5533Returns the interface name corresponding to the interface index if_index.");
5534
5535#endif /* HAVE_IF_NAMEINDEX */
5536
5537
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005538#ifdef CMSG_LEN
5539/* Python interface to CMSG_LEN(length). */
5540
5541static PyObject *
5542socket_CMSG_LEN(PyObject *self, PyObject *args)
5543{
5544 Py_ssize_t length;
5545 size_t result;
5546
5547 if (!PyArg_ParseTuple(args, "n:CMSG_LEN", &length))
5548 return NULL;
5549 if (length < 0 || !get_CMSG_LEN(length, &result)) {
5550 PyErr_Format(PyExc_OverflowError, "CMSG_LEN() argument out of range");
5551 return NULL;
5552 }
5553 return PyLong_FromSize_t(result);
5554}
5555
5556PyDoc_STRVAR(CMSG_LEN_doc,
5557"CMSG_LEN(length) -> control message length\n\
5558\n\
5559Return the total length, without trailing padding, of an ancillary\n\
5560data item with associated data of the given length. This value can\n\
5561often be used as the buffer size for recvmsg() to receive a single\n\
5562item of ancillary data, but RFC 3542 requires portable applications to\n\
5563use CMSG_SPACE() and thus include space for padding, even when the\n\
5564item will be the last in the buffer. Raises OverflowError if length\n\
5565is outside the permissible range of values.");
5566
5567
5568#ifdef CMSG_SPACE
5569/* Python interface to CMSG_SPACE(length). */
5570
5571static PyObject *
5572socket_CMSG_SPACE(PyObject *self, PyObject *args)
5573{
5574 Py_ssize_t length;
5575 size_t result;
5576
5577 if (!PyArg_ParseTuple(args, "n:CMSG_SPACE", &length))
5578 return NULL;
5579 if (length < 0 || !get_CMSG_SPACE(length, &result)) {
5580 PyErr_SetString(PyExc_OverflowError,
5581 "CMSG_SPACE() argument out of range");
5582 return NULL;
5583 }
5584 return PyLong_FromSize_t(result);
5585}
5586
5587PyDoc_STRVAR(CMSG_SPACE_doc,
5588"CMSG_SPACE(length) -> buffer size\n\
5589\n\
5590Return the buffer size needed for recvmsg() to receive an ancillary\n\
5591data item with associated data of the given length, along with any\n\
5592trailing padding. The buffer space needed to receive multiple items\n\
5593is the sum of the CMSG_SPACE() values for their associated data\n\
5594lengths. Raises OverflowError if length is outside the permissible\n\
5595range of values.");
5596#endif /* CMSG_SPACE */
5597#endif /* CMSG_LEN */
5598
5599
Guido van Rossum30a685f1991-06-27 15:51:29 +00005600/* List of functions exported by this module. */
5601
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005602static PyMethodDef socket_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005603 {"gethostbyname", socket_gethostbyname,
5604 METH_VARARGS, gethostbyname_doc},
5605 {"gethostbyname_ex", socket_gethostbyname_ex,
5606 METH_VARARGS, ghbn_ex_doc},
5607 {"gethostbyaddr", socket_gethostbyaddr,
5608 METH_VARARGS, gethostbyaddr_doc},
5609 {"gethostname", socket_gethostname,
5610 METH_NOARGS, gethostname_doc},
Antoine Pitrou061cfb52011-02-28 22:25:22 +00005611#ifdef HAVE_SETHOSTNAME
5612 {"sethostname", socket_sethostname,
5613 METH_VARARGS, sethostname_doc},
5614#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005615 {"getservbyname", socket_getservbyname,
5616 METH_VARARGS, getservbyname_doc},
5617 {"getservbyport", socket_getservbyport,
5618 METH_VARARGS, getservbyport_doc},
5619 {"getprotobyname", socket_getprotobyname,
5620 METH_VARARGS, getprotobyname_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005621#ifndef NO_DUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005622 {"dup", socket_dup,
5623 METH_O, dup_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005624#endif
Dave Cole331708b2004-08-09 04:51:41 +00005625#ifdef HAVE_SOCKETPAIR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005626 {"socketpair", socket_socketpair,
5627 METH_VARARGS, socketpair_doc},
Dave Cole331708b2004-08-09 04:51:41 +00005628#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005629 {"ntohs", socket_ntohs,
5630 METH_VARARGS, ntohs_doc},
5631 {"ntohl", socket_ntohl,
5632 METH_O, ntohl_doc},
5633 {"htons", socket_htons,
5634 METH_VARARGS, htons_doc},
5635 {"htonl", socket_htonl,
5636 METH_O, htonl_doc},
5637 {"inet_aton", socket_inet_aton,
5638 METH_VARARGS, inet_aton_doc},
5639 {"inet_ntoa", socket_inet_ntoa,
5640 METH_VARARGS, inet_ntoa_doc},
Atsuo Ishimotoda0fc142012-07-16 15:16:54 +09005641#if defined(HAVE_INET_PTON) || defined(MS_WINDOWS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005642 {"inet_pton", socket_inet_pton,
5643 METH_VARARGS, inet_pton_doc},
5644 {"inet_ntop", socket_inet_ntop,
5645 METH_VARARGS, inet_ntop_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005646#endif
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005647 {"getaddrinfo", (PyCFunction)socket_getaddrinfo,
5648 METH_VARARGS | METH_KEYWORDS, getaddrinfo_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005649 {"getnameinfo", socket_getnameinfo,
5650 METH_VARARGS, getnameinfo_doc},
5651 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
5652 METH_NOARGS, getdefaulttimeout_doc},
5653 {"setdefaulttimeout", socket_setdefaulttimeout,
5654 METH_O, setdefaulttimeout_doc},
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005655#ifdef HAVE_IF_NAMEINDEX
5656 {"if_nameindex", socket_if_nameindex,
5657 METH_NOARGS, if_nameindex_doc},
5658 {"if_nametoindex", socket_if_nametoindex,
Charles-François Natali60713592011-05-20 16:55:06 +02005659 METH_VARARGS, if_nametoindex_doc},
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005660 {"if_indextoname", socket_if_indextoname,
5661 METH_O, if_indextoname_doc},
5662#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005663#ifdef CMSG_LEN
5664 {"CMSG_LEN", socket_CMSG_LEN,
5665 METH_VARARGS, CMSG_LEN_doc},
5666#ifdef CMSG_SPACE
5667 {"CMSG_SPACE", socket_CMSG_SPACE,
5668 METH_VARARGS, CMSG_SPACE_doc},
5669#endif
5670#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005671 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005672};
5673
Guido van Rossum30a685f1991-06-27 15:51:29 +00005674
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005675#ifdef MS_WINDOWS
5676#define OS_INIT_DEFINED
5677
5678/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00005679
5680static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005681os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00005682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005683 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00005684}
5685
5686static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005687os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00005688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005689 WSADATA WSAData;
5690 int ret;
5691 ret = WSAStartup(0x0101, &WSAData);
5692 switch (ret) {
5693 case 0: /* No error */
5694 Py_AtExit(os_cleanup);
5695 return 1; /* Success */
5696 case WSASYSNOTREADY:
5697 PyErr_SetString(PyExc_ImportError,
5698 "WSAStartup failed: network not ready");
5699 break;
5700 case WSAVERNOTSUPPORTED:
5701 case WSAEINVAL:
5702 PyErr_SetString(
5703 PyExc_ImportError,
5704 "WSAStartup failed: requested version not supported");
5705 break;
5706 default:
5707 PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret);
5708 break;
5709 }
5710 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00005711}
5712
Guido van Rossum8d665e61996-06-26 18:22:49 +00005713#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00005714
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005715
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005716
5717#ifndef OS_INIT_DEFINED
5718static int
5719os_init(void)
5720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005721 return 1; /* Success */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005722}
5723#endif
5724
5725
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00005726/* C API table - always add new things to the end for binary
5727 compatibility. */
5728static
5729PySocketModule_APIObject PySocketModuleAPI =
5730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005731 &sock_type,
Antoine Pitrouc4df7842010-12-03 19:59:41 +00005732 NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005733 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00005734};
5735
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005736
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005737/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005738
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005739 This module is actually called "_socket", and there's a wrapper
Guido van Rossum7d0a8262007-05-21 23:13:11 +00005740 "socket.py" which implements some additional functionality.
5741 The import of "_socket" may fail with an ImportError exception if
5742 os-specific initialization fails. On Windows, this does WINSOCK
Ezio Melotti13925002011-03-16 11:05:33 +02005743 initialization. When WINSOCK is initialized successfully, a call to
Guido van Rossum7d0a8262007-05-21 23:13:11 +00005744 WSACleanup() is scheduled to be made at exit time.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005745*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005746
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005747PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005748"Implementation module for socket operations.\n\
5749\n\
5750See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00005751
Martin v. Löwis1a214512008-06-11 05:26:20 +00005752static struct PyModuleDef socketmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005753 PyModuleDef_HEAD_INIT,
5754 PySocket_MODULE_NAME,
5755 socket_doc,
5756 -1,
5757 socket_methods,
5758 NULL,
5759 NULL,
5760 NULL,
5761 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00005762};
5763
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005764PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00005765PyInit__socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005767 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00005768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005769 if (!os_init())
5770 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005771
Victor Stinnerdaf45552013-08-28 00:53:59 +02005772#ifdef MS_WINDOWS
5773 if (support_wsa_no_inherit == -1) {
5774 DWORD version = GetVersion();
5775 DWORD major = (DWORD)LOBYTE(LOWORD(version));
5776 DWORD minor = (DWORD)HIBYTE(LOWORD(version));
5777 /* need Windows 7 SP1, 2008 R2 SP1 or later */
5778 support_wsa_no_inherit = (major >= 6 && minor >= 1);
5779 }
5780#endif
5781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005782 Py_TYPE(&sock_type) = &PyType_Type;
5783 m = PyModule_Create(&socketmodule);
5784 if (m == NULL)
5785 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005786
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005787 Py_INCREF(PyExc_OSError);
5788 PySocketModuleAPI.error = PyExc_OSError;
5789 Py_INCREF(PyExc_OSError);
5790 PyModule_AddObject(m, "error", PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005791 socket_herror = PyErr_NewException("socket.herror",
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005792 PyExc_OSError, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005793 if (socket_herror == NULL)
5794 return NULL;
5795 Py_INCREF(socket_herror);
5796 PyModule_AddObject(m, "herror", socket_herror);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005797 socket_gaierror = PyErr_NewException("socket.gaierror", PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005798 NULL);
5799 if (socket_gaierror == NULL)
5800 return NULL;
5801 Py_INCREF(socket_gaierror);
5802 PyModule_AddObject(m, "gaierror", socket_gaierror);
5803 socket_timeout = PyErr_NewException("socket.timeout",
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005804 PyExc_OSError, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005805 if (socket_timeout == NULL)
5806 return NULL;
Antoine Pitrouc4df7842010-12-03 19:59:41 +00005807 PySocketModuleAPI.timeout_error = socket_timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005808 Py_INCREF(socket_timeout);
5809 PyModule_AddObject(m, "timeout", socket_timeout);
5810 Py_INCREF((PyObject *)&sock_type);
5811 if (PyModule_AddObject(m, "SocketType",
5812 (PyObject *)&sock_type) != 0)
5813 return NULL;
5814 Py_INCREF((PyObject *)&sock_type);
5815 if (PyModule_AddObject(m, "socket",
5816 (PyObject *)&sock_type) != 0)
5817 return NULL;
Guido van Rossum09be4091999-08-09 14:40:40 +00005818
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005819#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005820 has_ipv6 = Py_True;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005821#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005822 has_ipv6 = Py_False;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005823#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005824 Py_INCREF(has_ipv6);
5825 PyModule_AddObject(m, "has_ipv6", has_ipv6);
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005827 /* Export C API */
5828 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
5829 PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL)
5830 ) != 0)
5831 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00005832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005833 /* Address families (we only support AF_INET and AF_UNIX) */
Guido van Rossum09be4091999-08-09 14:40:40 +00005834#ifdef AF_UNSPEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005835 PyModule_AddIntMacro(m, AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00005836#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005837 PyModule_AddIntMacro(m, AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005838#ifdef AF_INET6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005839 PyModule_AddIntMacro(m, AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005840#endif /* AF_INET6 */
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00005841#if defined(AF_UNIX)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005842 PyModule_AddIntMacro(m, AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005843#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00005844#ifdef AF_AX25
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005845 /* Amateur Radio AX.25 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005846 PyModule_AddIntMacro(m, AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00005847#endif
5848#ifdef AF_IPX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005849 PyModule_AddIntMacro(m, AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00005850#endif
5851#ifdef AF_APPLETALK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005852 /* Appletalk DDP */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005853 PyModule_AddIntMacro(m, AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00005854#endif
5855#ifdef AF_NETROM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005856 /* Amateur radio NetROM */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005857 PyModule_AddIntMacro(m, AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00005858#endif
5859#ifdef AF_BRIDGE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005860 /* Multiprotocol bridge */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005861 PyModule_AddIntMacro(m, AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00005862#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005863#ifdef AF_ATMPVC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005864 /* ATM PVCs */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005865 PyModule_AddIntMacro(m, AF_ATMPVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005866#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00005867#ifdef AF_AAL5
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005868 /* Reserved for Werner's ATM */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005869 PyModule_AddIntMacro(m, AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00005870#endif
5871#ifdef AF_X25
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005872 /* Reserved for X.25 project */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005873 PyModule_AddIntMacro(m, AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00005874#endif
5875#ifdef AF_INET6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005876 PyModule_AddIntMacro(m, AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00005877#endif
5878#ifdef AF_ROSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005879 /* Amateur Radio X.25 PLP */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005880 PyModule_AddIntMacro(m, AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00005881#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005882#ifdef AF_DECnet
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005883 /* Reserved for DECnet project */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005884 PyModule_AddIntMacro(m, AF_DECnet);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005885#endif
5886#ifdef AF_NETBEUI
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005887 /* Reserved for 802.2LLC project */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005888 PyModule_AddIntMacro(m, AF_NETBEUI);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005889#endif
5890#ifdef AF_SECURITY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005891 /* Security callback pseudo AF */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005892 PyModule_AddIntMacro(m, AF_SECURITY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005893#endif
5894#ifdef AF_KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005895 /* PF_KEY key management API */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005896 PyModule_AddIntMacro(m, AF_KEY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005897#endif
5898#ifdef AF_NETLINK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005899 /* */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005900 PyModule_AddIntMacro(m, AF_NETLINK);
5901 PyModule_AddIntMacro(m, NETLINK_ROUTE);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005902#ifdef NETLINK_SKIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005903 PyModule_AddIntMacro(m, NETLINK_SKIP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005904#endif
5905#ifdef NETLINK_W1
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005906 PyModule_AddIntMacro(m, NETLINK_W1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005907#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005908 PyModule_AddIntMacro(m, NETLINK_USERSOCK);
5909 PyModule_AddIntMacro(m, NETLINK_FIREWALL);
Guido van Rossum668a94a2006-02-21 01:07:27 +00005910#ifdef NETLINK_TCPDIAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005911 PyModule_AddIntMacro(m, NETLINK_TCPDIAG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00005912#endif
5913#ifdef NETLINK_NFLOG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005914 PyModule_AddIntMacro(m, NETLINK_NFLOG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00005915#endif
Neal Norwitz65851662006-01-16 04:31:40 +00005916#ifdef NETLINK_XFRM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005917 PyModule_AddIntMacro(m, NETLINK_XFRM);
Neal Norwitz65851662006-01-16 04:31:40 +00005918#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005919#ifdef NETLINK_ARPD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005920 PyModule_AddIntMacro(m, NETLINK_ARPD);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005921#endif
5922#ifdef NETLINK_ROUTE6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005923 PyModule_AddIntMacro(m, NETLINK_ROUTE6);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005924#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005925 PyModule_AddIntMacro(m, NETLINK_IP6_FW);
Thomas Wouterscf297e42007-02-23 15:07:44 +00005926#ifdef NETLINK_DNRTMSG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005927 PyModule_AddIntMacro(m, NETLINK_DNRTMSG);
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005928#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005929#ifdef NETLINK_TAPBASE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005930 PyModule_AddIntMacro(m, NETLINK_TAPBASE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005931#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005932#endif /* AF_NETLINK */
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005933#ifdef AF_ROUTE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005934 /* Alias to emulate 4.4BSD */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005935 PyModule_AddIntMacro(m, AF_ROUTE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005936#endif
Giampaolo Rodola'80e1c432013-05-21 21:02:04 +02005937#ifdef AF_LINK
5938 PyModule_AddIntMacro(m, AF_LINK);
5939#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005940#ifdef AF_ASH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005941 /* Ash */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005942 PyModule_AddIntMacro(m, AF_ASH);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005943#endif
5944#ifdef AF_ECONET
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005945 /* Acorn Econet */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005946 PyModule_AddIntMacro(m, AF_ECONET);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005947#endif
5948#ifdef AF_ATMSVC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005949 /* ATM SVCs */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005950 PyModule_AddIntMacro(m, AF_ATMSVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005951#endif
5952#ifdef AF_SNA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005953 /* Linux SNA Project (nutters!) */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005954 PyModule_AddIntMacro(m, AF_SNA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005955#endif
5956#ifdef AF_IRDA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005957 /* IRDA sockets */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005958 PyModule_AddIntMacro(m, AF_IRDA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005959#endif
5960#ifdef AF_PPPOX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005961 /* PPPoX sockets */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005962 PyModule_AddIntMacro(m, AF_PPPOX);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005963#endif
5964#ifdef AF_WANPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005965 /* Wanpipe API Sockets */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005966 PyModule_AddIntMacro(m, AF_WANPIPE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005967#endif
5968#ifdef AF_LLC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005969 /* Linux LLC */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005970 PyModule_AddIntMacro(m, AF_LLC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005971#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00005972
Hye-Shik Chang81268602004-02-02 06:05:24 +00005973#ifdef USE_BLUETOOTH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005974 PyModule_AddIntMacro(m, AF_BLUETOOTH);
5975 PyModule_AddIntMacro(m, BTPROTO_L2CAP);
5976 PyModule_AddIntMacro(m, BTPROTO_HCI);
5977 PyModule_AddIntMacro(m, SOL_HCI);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00005978#if !defined(__NetBSD__) && !defined(__DragonFly__)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005979 PyModule_AddIntMacro(m, HCI_FILTER);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00005980#endif
Hye-Shik Chang81268602004-02-02 06:05:24 +00005981#if !defined(__FreeBSD__)
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00005982#if !defined(__NetBSD__) && !defined(__DragonFly__)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005983 PyModule_AddIntMacro(m, HCI_TIME_STAMP);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00005984#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005985 PyModule_AddIntMacro(m, HCI_DATA_DIR);
5986 PyModule_AddIntMacro(m, BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00005987#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005988 PyModule_AddIntMacro(m, BTPROTO_RFCOMM);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005989 PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
5990 PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
Martin v. Löwis12af0482004-01-31 12:34:17 +00005991#endif
5992
Charles-François Natali47413c12011-10-06 19:47:44 +02005993#ifdef AF_CAN
5994 /* Controller Area Network */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005995 PyModule_AddIntMacro(m, AF_CAN);
Charles-François Natali47413c12011-10-06 19:47:44 +02005996#endif
5997#ifdef PF_CAN
5998 /* Controller Area Network */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005999 PyModule_AddIntMacro(m, PF_CAN);
Charles-François Natali47413c12011-10-06 19:47:44 +02006000#endif
6001
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006002/* Reliable Datagram Sockets */
6003#ifdef AF_RDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006004 PyModule_AddIntMacro(m, AF_RDS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006005#endif
6006#ifdef PF_RDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006007 PyModule_AddIntMacro(m, PF_RDS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006008#endif
6009
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006010/* Kernel event messages */
6011#ifdef PF_SYSTEM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006012 PyModule_AddIntMacro(m, PF_SYSTEM);
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006013#endif
6014#ifdef AF_SYSTEM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006015 PyModule_AddIntMacro(m, AF_SYSTEM);
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006016#endif
6017
Antoine Pitroub156a462010-10-27 20:13:57 +00006018#ifdef AF_PACKET
6019 PyModule_AddIntMacro(m, AF_PACKET);
6020#endif
6021#ifdef PF_PACKET
6022 PyModule_AddIntMacro(m, PF_PACKET);
6023#endif
6024#ifdef PACKET_HOST
6025 PyModule_AddIntMacro(m, PACKET_HOST);
6026#endif
6027#ifdef PACKET_BROADCAST
6028 PyModule_AddIntMacro(m, PACKET_BROADCAST);
6029#endif
6030#ifdef PACKET_MULTICAST
6031 PyModule_AddIntMacro(m, PACKET_MULTICAST);
6032#endif
6033#ifdef PACKET_OTHERHOST
6034 PyModule_AddIntMacro(m, PACKET_OTHERHOST);
6035#endif
6036#ifdef PACKET_OUTGOING
6037 PyModule_AddIntMacro(m, PACKET_OUTGOING);
6038#endif
6039#ifdef PACKET_LOOPBACK
6040 PyModule_AddIntMacro(m, PACKET_LOOPBACK);
6041#endif
6042#ifdef PACKET_FASTROUTE
6043 PyModule_AddIntMacro(m, PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00006044#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006045
Christian Heimes043d6f62008-01-07 17:19:16 +00006046#ifdef HAVE_LINUX_TIPC_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006047 PyModule_AddIntMacro(m, AF_TIPC);
Christian Heimes043d6f62008-01-07 17:19:16 +00006048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006049 /* for addresses */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006050 PyModule_AddIntMacro(m, TIPC_ADDR_NAMESEQ);
6051 PyModule_AddIntMacro(m, TIPC_ADDR_NAME);
6052 PyModule_AddIntMacro(m, TIPC_ADDR_ID);
Christian Heimes043d6f62008-01-07 17:19:16 +00006053
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006054 PyModule_AddIntMacro(m, TIPC_ZONE_SCOPE);
6055 PyModule_AddIntMacro(m, TIPC_CLUSTER_SCOPE);
6056 PyModule_AddIntMacro(m, TIPC_NODE_SCOPE);
Christian Heimes043d6f62008-01-07 17:19:16 +00006057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006058 /* for setsockopt() */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006059 PyModule_AddIntMacro(m, SOL_TIPC);
6060 PyModule_AddIntMacro(m, TIPC_IMPORTANCE);
6061 PyModule_AddIntMacro(m, TIPC_SRC_DROPPABLE);
6062 PyModule_AddIntMacro(m, TIPC_DEST_DROPPABLE);
6063 PyModule_AddIntMacro(m, TIPC_CONN_TIMEOUT);
Christian Heimes043d6f62008-01-07 17:19:16 +00006064
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006065 PyModule_AddIntMacro(m, TIPC_LOW_IMPORTANCE);
6066 PyModule_AddIntMacro(m, TIPC_MEDIUM_IMPORTANCE);
6067 PyModule_AddIntMacro(m, TIPC_HIGH_IMPORTANCE);
6068 PyModule_AddIntMacro(m, TIPC_CRITICAL_IMPORTANCE);
Christian Heimes043d6f62008-01-07 17:19:16 +00006069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006070 /* for subscriptions */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006071 PyModule_AddIntMacro(m, TIPC_SUB_PORTS);
6072 PyModule_AddIntMacro(m, TIPC_SUB_SERVICE);
Christian Heimes25bb7832008-01-11 16:17:00 +00006073#ifdef TIPC_SUB_CANCEL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006074 /* doesn't seem to be available everywhere */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006075 PyModule_AddIntMacro(m, TIPC_SUB_CANCEL);
Christian Heimes25bb7832008-01-11 16:17:00 +00006076#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006077 PyModule_AddIntMacro(m, TIPC_WAIT_FOREVER);
6078 PyModule_AddIntMacro(m, TIPC_PUBLISHED);
6079 PyModule_AddIntMacro(m, TIPC_WITHDRAWN);
6080 PyModule_AddIntMacro(m, TIPC_SUBSCR_TIMEOUT);
6081 PyModule_AddIntMacro(m, TIPC_CFG_SRV);
6082 PyModule_AddIntMacro(m, TIPC_TOP_SRV);
Christian Heimes043d6f62008-01-07 17:19:16 +00006083#endif
6084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006085 /* Socket types */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006086 PyModule_AddIntMacro(m, SOCK_STREAM);
6087 PyModule_AddIntMacro(m, SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00006088/* We have incomplete socket support. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006089 PyModule_AddIntMacro(m, SOCK_RAW);
6090 PyModule_AddIntMacro(m, SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00006091#if defined(SOCK_RDM)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006092 PyModule_AddIntMacro(m, SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00006093#endif
Antoine Pitroub1c54962010-10-14 15:05:38 +00006094#ifdef SOCK_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006095 PyModule_AddIntMacro(m, SOCK_CLOEXEC);
Antoine Pitroub1c54962010-10-14 15:05:38 +00006096#endif
6097#ifdef SOCK_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006098 PyModule_AddIntMacro(m, SOCK_NONBLOCK);
Antoine Pitroub1c54962010-10-14 15:05:38 +00006099#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006101#ifdef SO_DEBUG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006102 PyModule_AddIntMacro(m, SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006103#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006104#ifdef SO_ACCEPTCONN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006105 PyModule_AddIntMacro(m, SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006106#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006107#ifdef SO_REUSEADDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006108 PyModule_AddIntMacro(m, SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006109#endif
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00006110#ifdef SO_EXCLUSIVEADDRUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006111 PyModule_AddIntMacro(m, SO_EXCLUSIVEADDRUSE);
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00006112#endif
6113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006114#ifdef SO_KEEPALIVE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006115 PyModule_AddIntMacro(m, SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006116#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006117#ifdef SO_DONTROUTE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006118 PyModule_AddIntMacro(m, SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006119#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006120#ifdef SO_BROADCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006121 PyModule_AddIntMacro(m, SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006122#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006123#ifdef SO_USELOOPBACK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006124 PyModule_AddIntMacro(m, SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006125#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006126#ifdef SO_LINGER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006127 PyModule_AddIntMacro(m, SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006128#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006129#ifdef SO_OOBINLINE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006130 PyModule_AddIntMacro(m, SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006131#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006132#ifdef SO_REUSEPORT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006133 PyModule_AddIntMacro(m, SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006134#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006135#ifdef SO_SNDBUF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006136 PyModule_AddIntMacro(m, SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006137#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006138#ifdef SO_RCVBUF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006139 PyModule_AddIntMacro(m, SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006140#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006141#ifdef SO_SNDLOWAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006142 PyModule_AddIntMacro(m, SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006143#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006144#ifdef SO_RCVLOWAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006145 PyModule_AddIntMacro(m, SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006146#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006147#ifdef SO_SNDTIMEO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006148 PyModule_AddIntMacro(m, SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006149#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006150#ifdef SO_RCVTIMEO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006151 PyModule_AddIntMacro(m, SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006152#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006153#ifdef SO_ERROR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006154 PyModule_AddIntMacro(m, SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006155#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006156#ifdef SO_TYPE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006157 PyModule_AddIntMacro(m, SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006158#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006159#ifdef SO_SETFIB
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006160 PyModule_AddIntMacro(m, SO_SETFIB);
Larry Hastingsf0f37952010-04-02 11:47:10 +00006161#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006162#ifdef SO_PASSCRED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006163 PyModule_AddIntMacro(m, SO_PASSCRED);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006164#endif
6165#ifdef SO_PEERCRED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006166 PyModule_AddIntMacro(m, SO_PEERCRED);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006167#endif
6168#ifdef LOCAL_PEERCRED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006169 PyModule_AddIntMacro(m, LOCAL_PEERCRED);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006170#endif
Victor Stinnercb98bed2011-10-25 13:45:48 +02006171#ifdef SO_BINDTODEVICE
6172 PyModule_AddIntMacro(m, SO_BINDTODEVICE);
6173#endif
Benjamin Peterson1314ef72013-11-27 09:18:54 -06006174#ifdef SO_PRIORITY
6175 PyModule_AddIntMacro(m, SO_PRIORITY);
6176#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006178 /* Maximum number of connections for "listen" */
6179#ifdef SOMAXCONN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006180 PyModule_AddIntMacro(m, SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006181#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006182 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006183#endif
6184
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006185 /* Ancilliary message types */
6186#ifdef SCM_RIGHTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006187 PyModule_AddIntMacro(m, SCM_RIGHTS);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006188#endif
6189#ifdef SCM_CREDENTIALS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006190 PyModule_AddIntMacro(m, SCM_CREDENTIALS);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006191#endif
6192#ifdef SCM_CREDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006193 PyModule_AddIntMacro(m, SCM_CREDS);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006194#endif
6195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006196 /* Flags for send, recv */
6197#ifdef MSG_OOB
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006198 PyModule_AddIntMacro(m, MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006199#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006200#ifdef MSG_PEEK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006201 PyModule_AddIntMacro(m, MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006202#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006203#ifdef MSG_DONTROUTE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006204 PyModule_AddIntMacro(m, MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006205#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006206#ifdef MSG_DONTWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006207 PyModule_AddIntMacro(m, MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00006208#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006209#ifdef MSG_EOR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006210 PyModule_AddIntMacro(m, MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006211#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006212#ifdef MSG_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006213 PyModule_AddIntMacro(m, MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006214#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006215#ifdef MSG_CTRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006216 PyModule_AddIntMacro(m, MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006217#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006218#ifdef MSG_WAITALL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006219 PyModule_AddIntMacro(m, MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006220#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006221#ifdef MSG_BTAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006222 PyModule_AddIntMacro(m, MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006223#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006224#ifdef MSG_ETAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006225 PyModule_AddIntMacro(m, MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006226#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006227#ifdef MSG_NOSIGNAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006228 PyModule_AddIntMacro(m, MSG_NOSIGNAL);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006229#endif
6230#ifdef MSG_NOTIFICATION
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006231 PyModule_AddIntMacro(m, MSG_NOTIFICATION);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006232#endif
6233#ifdef MSG_CMSG_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006234 PyModule_AddIntMacro(m, MSG_CMSG_CLOEXEC);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006235#endif
6236#ifdef MSG_ERRQUEUE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006237 PyModule_AddIntMacro(m, MSG_ERRQUEUE);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006238#endif
6239#ifdef MSG_CONFIRM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006240 PyModule_AddIntMacro(m, MSG_CONFIRM);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006241#endif
6242#ifdef MSG_MORE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006243 PyModule_AddIntMacro(m, MSG_MORE);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006244#endif
6245#ifdef MSG_EOF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006246 PyModule_AddIntMacro(m, MSG_EOF);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006247#endif
6248#ifdef MSG_BCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006249 PyModule_AddIntMacro(m, MSG_BCAST);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006250#endif
6251#ifdef MSG_MCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006252 PyModule_AddIntMacro(m, MSG_MCAST);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006253#endif
Benjamin Peterson57b66722012-12-12 22:24:47 -05006254#ifdef MSG_FASTOPEN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006255 PyModule_AddIntMacro(m, MSG_FASTOPEN);
Benjamin Peterson57b66722012-12-12 22:24:47 -05006256#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006258 /* Protocol level and numbers, usable for [gs]etsockopt */
6259#ifdef SOL_SOCKET
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006260 PyModule_AddIntMacro(m, SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006261#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006262#ifdef SOL_IP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006263 PyModule_AddIntMacro(m, SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006264#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006265 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00006266#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006267#ifdef SOL_IPX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006268 PyModule_AddIntMacro(m, SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00006269#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006270#ifdef SOL_AX25
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006271 PyModule_AddIntMacro(m, SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00006272#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006273#ifdef SOL_ATALK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006274 PyModule_AddIntMacro(m, SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00006275#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006276#ifdef SOL_NETROM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006277 PyModule_AddIntMacro(m, SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00006278#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006279#ifdef SOL_ROSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006280 PyModule_AddIntMacro(m, SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00006281#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006282#ifdef SOL_TCP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006283 PyModule_AddIntMacro(m, SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006284#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006285 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00006286#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006287#ifdef SOL_UDP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006288 PyModule_AddIntMacro(m, SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006289#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006290 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00006291#endif
Charles-François Natali47413c12011-10-06 19:47:44 +02006292#ifdef SOL_CAN_BASE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006293 PyModule_AddIntMacro(m, SOL_CAN_BASE);
Charles-François Natali47413c12011-10-06 19:47:44 +02006294#endif
6295#ifdef SOL_CAN_RAW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006296 PyModule_AddIntMacro(m, SOL_CAN_RAW);
6297 PyModule_AddIntMacro(m, CAN_RAW);
Charles-François Natali47413c12011-10-06 19:47:44 +02006298#endif
6299#ifdef HAVE_LINUX_CAN_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006300 PyModule_AddIntMacro(m, CAN_EFF_FLAG);
6301 PyModule_AddIntMacro(m, CAN_RTR_FLAG);
6302 PyModule_AddIntMacro(m, CAN_ERR_FLAG);
Charles-François Natali47413c12011-10-06 19:47:44 +02006303
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006304 PyModule_AddIntMacro(m, CAN_SFF_MASK);
6305 PyModule_AddIntMacro(m, CAN_EFF_MASK);
6306 PyModule_AddIntMacro(m, CAN_ERR_MASK);
Charles-François Natali47413c12011-10-06 19:47:44 +02006307#endif
6308#ifdef HAVE_LINUX_CAN_RAW_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006309 PyModule_AddIntMacro(m, CAN_RAW_FILTER);
6310 PyModule_AddIntMacro(m, CAN_RAW_ERR_FILTER);
6311 PyModule_AddIntMacro(m, CAN_RAW_LOOPBACK);
6312 PyModule_AddIntMacro(m, CAN_RAW_RECV_OWN_MSGS);
Charles-François Natali47413c12011-10-06 19:47:44 +02006313#endif
Charles-François Natali773e42d2013-02-05 19:42:01 +01006314#ifdef HAVE_LINUX_CAN_BCM_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006315 PyModule_AddIntMacro(m, CAN_BCM);
Charles-François Natali773e42d2013-02-05 19:42:01 +01006316 PyModule_AddIntConstant(m, "CAN_BCM_TX_SETUP", TX_SETUP);
6317 PyModule_AddIntConstant(m, "CAN_BCM_TX_DELETE", TX_DELETE);
6318 PyModule_AddIntConstant(m, "CAN_BCM_TX_READ", TX_READ);
6319 PyModule_AddIntConstant(m, "CAN_BCM_TX_SEND", TX_SEND);
6320 PyModule_AddIntConstant(m, "CAN_BCM_RX_SETUP", RX_SETUP);
6321 PyModule_AddIntConstant(m, "CAN_BCM_RX_DELETE", RX_DELETE);
6322 PyModule_AddIntConstant(m, "CAN_BCM_RX_READ", RX_READ);
6323 PyModule_AddIntConstant(m, "CAN_BCM_TX_STATUS", TX_STATUS);
6324 PyModule_AddIntConstant(m, "CAN_BCM_TX_EXPIRED", TX_EXPIRED);
6325 PyModule_AddIntConstant(m, "CAN_BCM_RX_STATUS", RX_STATUS);
6326 PyModule_AddIntConstant(m, "CAN_BCM_RX_TIMEOUT", RX_TIMEOUT);
6327 PyModule_AddIntConstant(m, "CAN_BCM_RX_CHANGED", RX_CHANGED);
6328#endif
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006329#ifdef SOL_RDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006330 PyModule_AddIntMacro(m, SOL_RDS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006331#endif
6332#ifdef RDS_CANCEL_SENT_TO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006333 PyModule_AddIntMacro(m, RDS_CANCEL_SENT_TO);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006334#endif
6335#ifdef RDS_GET_MR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006336 PyModule_AddIntMacro(m, RDS_GET_MR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006337#endif
6338#ifdef RDS_FREE_MR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006339 PyModule_AddIntMacro(m, RDS_FREE_MR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006340#endif
6341#ifdef RDS_RECVERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006342 PyModule_AddIntMacro(m, RDS_RECVERR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006343#endif
6344#ifdef RDS_CONG_MONITOR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006345 PyModule_AddIntMacro(m, RDS_CONG_MONITOR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006346#endif
6347#ifdef RDS_GET_MR_FOR_DEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006348 PyModule_AddIntMacro(m, RDS_GET_MR_FOR_DEST);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006349#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006350#ifdef IPPROTO_IP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006351 PyModule_AddIntMacro(m, IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00006352#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006353 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006354#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006355#ifdef IPPROTO_HOPOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006356 PyModule_AddIntMacro(m, IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006357#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006358#ifdef IPPROTO_ICMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006359 PyModule_AddIntMacro(m, IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00006360#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006361 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006362#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006363#ifdef IPPROTO_IGMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006364 PyModule_AddIntMacro(m, IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006365#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006366#ifdef IPPROTO_GGP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006367 PyModule_AddIntMacro(m, IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006368#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006369#ifdef IPPROTO_IPV4
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006370 PyModule_AddIntMacro(m, IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006371#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006372#ifdef IPPROTO_IPV6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006373 PyModule_AddIntMacro(m, IPPROTO_IPV6);
Martin v. Löwisa0f17342003-10-03 13:56:20 +00006374#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006375#ifdef IPPROTO_IPIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006376 PyModule_AddIntMacro(m, IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006377#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006378#ifdef IPPROTO_TCP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006379 PyModule_AddIntMacro(m, IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00006380#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006381 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006382#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006383#ifdef IPPROTO_EGP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006384 PyModule_AddIntMacro(m, IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006385#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006386#ifdef IPPROTO_PUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006387 PyModule_AddIntMacro(m, IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006388#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006389#ifdef IPPROTO_UDP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006390 PyModule_AddIntMacro(m, IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00006391#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006392 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006393#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006394#ifdef IPPROTO_IDP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006395 PyModule_AddIntMacro(m, IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006396#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006397#ifdef IPPROTO_HELLO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006398 PyModule_AddIntMacro(m, IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006399#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006400#ifdef IPPROTO_ND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006401 PyModule_AddIntMacro(m, IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006402#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006403#ifdef IPPROTO_TP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006404 PyModule_AddIntMacro(m, IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006405#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006406#ifdef IPPROTO_IPV6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006407 PyModule_AddIntMacro(m, IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006408#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006409#ifdef IPPROTO_ROUTING
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006410 PyModule_AddIntMacro(m, IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006411#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006412#ifdef IPPROTO_FRAGMENT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006413 PyModule_AddIntMacro(m, IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006414#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006415#ifdef IPPROTO_RSVP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006416 PyModule_AddIntMacro(m, IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006417#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006418#ifdef IPPROTO_GRE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006419 PyModule_AddIntMacro(m, IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006420#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006421#ifdef IPPROTO_ESP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006422 PyModule_AddIntMacro(m, IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006423#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006424#ifdef IPPROTO_AH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006425 PyModule_AddIntMacro(m, IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006426#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006427#ifdef IPPROTO_MOBILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006428 PyModule_AddIntMacro(m, IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006429#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006430#ifdef IPPROTO_ICMPV6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006431 PyModule_AddIntMacro(m, IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006432#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006433#ifdef IPPROTO_NONE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006434 PyModule_AddIntMacro(m, IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006435#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006436#ifdef IPPROTO_DSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006437 PyModule_AddIntMacro(m, IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006438#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006439#ifdef IPPROTO_XTP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006440 PyModule_AddIntMacro(m, IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006441#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006442#ifdef IPPROTO_EON
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006443 PyModule_AddIntMacro(m, IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006444#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006445#ifdef IPPROTO_PIM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006446 PyModule_AddIntMacro(m, IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006447#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006448#ifdef IPPROTO_IPCOMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006449 PyModule_AddIntMacro(m, IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006450#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006451#ifdef IPPROTO_VRRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006452 PyModule_AddIntMacro(m, IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006453#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006454#ifdef IPPROTO_SCTP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006455 PyModule_AddIntMacro(m, IPPROTO_SCTP);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006456#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006457#ifdef IPPROTO_BIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006458 PyModule_AddIntMacro(m, IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006459#endif
6460/**/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006461#ifdef IPPROTO_RAW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006462 PyModule_AddIntMacro(m, IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00006463#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006464 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006465#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006466#ifdef IPPROTO_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006467 PyModule_AddIntMacro(m, IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006468#endif
6469
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006470#ifdef SYSPROTO_CONTROL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006471 PyModule_AddIntMacro(m, SYSPROTO_CONTROL);
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006472#endif
6473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006474 /* Some port configuration */
6475#ifdef IPPORT_RESERVED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006476 PyModule_AddIntMacro(m, IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006477#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006478 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006479#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006480#ifdef IPPORT_USERRESERVED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006481 PyModule_AddIntMacro(m, IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006482#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006483 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006484#endif
6485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006486 /* Some reserved IP v.4 addresses */
6487#ifdef INADDR_ANY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006488 PyModule_AddIntMacro(m, INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006489#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006490 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006491#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006492#ifdef INADDR_BROADCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006493 PyModule_AddIntMacro(m, INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006494#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006495 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006496#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006497#ifdef INADDR_LOOPBACK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006498 PyModule_AddIntMacro(m, INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006499#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006500 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006501#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006502#ifdef INADDR_UNSPEC_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006503 PyModule_AddIntMacro(m, INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006504#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006505 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006506#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006507#ifdef INADDR_ALLHOSTS_GROUP
6508 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
6509 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006510#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006511 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006512#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006513#ifdef INADDR_MAX_LOCAL_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006514 PyModule_AddIntMacro(m, INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006515#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006516 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006517#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006518#ifdef INADDR_NONE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006519 PyModule_AddIntMacro(m, INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006520#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006521 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006522#endif
6523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006524 /* IPv4 [gs]etsockopt options */
6525#ifdef IP_OPTIONS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006526 PyModule_AddIntMacro(m, IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006527#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006528#ifdef IP_HDRINCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006529 PyModule_AddIntMacro(m, IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006530#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006531#ifdef IP_TOS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006532 PyModule_AddIntMacro(m, IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006533#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006534#ifdef IP_TTL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006535 PyModule_AddIntMacro(m, IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006536#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006537#ifdef IP_RECVOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006538 PyModule_AddIntMacro(m, IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006539#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006540#ifdef IP_RECVRETOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006541 PyModule_AddIntMacro(m, IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006542#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006543#ifdef IP_RECVDSTADDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006544 PyModule_AddIntMacro(m, IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006545#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006546#ifdef IP_RETOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006547 PyModule_AddIntMacro(m, IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006548#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006549#ifdef IP_MULTICAST_IF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006550 PyModule_AddIntMacro(m, IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006551#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006552#ifdef IP_MULTICAST_TTL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006553 PyModule_AddIntMacro(m, IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006554#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006555#ifdef IP_MULTICAST_LOOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006556 PyModule_AddIntMacro(m, IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006557#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006558#ifdef IP_ADD_MEMBERSHIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006559 PyModule_AddIntMacro(m, IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006560#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006561#ifdef IP_DROP_MEMBERSHIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006562 PyModule_AddIntMacro(m, IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006563#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006564#ifdef IP_DEFAULT_MULTICAST_TTL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006565 PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00006566#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006567#ifdef IP_DEFAULT_MULTICAST_LOOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006568 PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006569#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006570#ifdef IP_MAX_MEMBERSHIPS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006571 PyModule_AddIntMacro(m, IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00006572#endif
Charles-François Natali564a42c2011-12-17 14:59:56 +01006573#ifdef IP_TRANSPARENT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006574 PyModule_AddIntMacro(m, IP_TRANSPARENT);
Charles-François Natali564a42c2011-12-17 14:59:56 +01006575#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006577 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
6578#ifdef IPV6_JOIN_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006579 PyModule_AddIntMacro(m, IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006580#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006581#ifdef IPV6_LEAVE_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006582 PyModule_AddIntMacro(m, IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006583#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006584#ifdef IPV6_MULTICAST_HOPS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006585 PyModule_AddIntMacro(m, IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006586#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006587#ifdef IPV6_MULTICAST_IF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006588 PyModule_AddIntMacro(m, IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006589#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006590#ifdef IPV6_MULTICAST_LOOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006591 PyModule_AddIntMacro(m, IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006592#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006593#ifdef IPV6_UNICAST_HOPS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006594 PyModule_AddIntMacro(m, IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006595#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006596 /* Additional IPV6 socket options, defined in RFC 3493 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00006597#ifdef IPV6_V6ONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006598 PyModule_AddIntMacro(m, IPV6_V6ONLY);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006599#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006600 /* Advanced IPV6 socket options, from RFC 3542 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00006601#ifdef IPV6_CHECKSUM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006602 PyModule_AddIntMacro(m, IPV6_CHECKSUM);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006603#endif
6604#ifdef IPV6_DONTFRAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006605 PyModule_AddIntMacro(m, IPV6_DONTFRAG);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006606#endif
6607#ifdef IPV6_DSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006608 PyModule_AddIntMacro(m, IPV6_DSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006609#endif
6610#ifdef IPV6_HOPLIMIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006611 PyModule_AddIntMacro(m, IPV6_HOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006612#endif
6613#ifdef IPV6_HOPOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006614 PyModule_AddIntMacro(m, IPV6_HOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006615#endif
6616#ifdef IPV6_NEXTHOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006617 PyModule_AddIntMacro(m, IPV6_NEXTHOP);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006618#endif
6619#ifdef IPV6_PATHMTU
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006620 PyModule_AddIntMacro(m, IPV6_PATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006621#endif
6622#ifdef IPV6_PKTINFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006623 PyModule_AddIntMacro(m, IPV6_PKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006624#endif
6625#ifdef IPV6_RECVDSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006626 PyModule_AddIntMacro(m, IPV6_RECVDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006627#endif
6628#ifdef IPV6_RECVHOPLIMIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006629 PyModule_AddIntMacro(m, IPV6_RECVHOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006630#endif
6631#ifdef IPV6_RECVHOPOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006632 PyModule_AddIntMacro(m, IPV6_RECVHOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006633#endif
6634#ifdef IPV6_RECVPKTINFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006635 PyModule_AddIntMacro(m, IPV6_RECVPKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006636#endif
6637#ifdef IPV6_RECVRTHDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006638 PyModule_AddIntMacro(m, IPV6_RECVRTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006639#endif
6640#ifdef IPV6_RECVTCLASS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006641 PyModule_AddIntMacro(m, IPV6_RECVTCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006642#endif
6643#ifdef IPV6_RTHDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006644 PyModule_AddIntMacro(m, IPV6_RTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006645#endif
6646#ifdef IPV6_RTHDRDSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006647 PyModule_AddIntMacro(m, IPV6_RTHDRDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006648#endif
6649#ifdef IPV6_RTHDR_TYPE_0
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006650 PyModule_AddIntMacro(m, IPV6_RTHDR_TYPE_0);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006651#endif
6652#ifdef IPV6_RECVPATHMTU
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006653 PyModule_AddIntMacro(m, IPV6_RECVPATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006654#endif
6655#ifdef IPV6_TCLASS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006656 PyModule_AddIntMacro(m, IPV6_TCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006657#endif
6658#ifdef IPV6_USE_MIN_MTU
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006659 PyModule_AddIntMacro(m, IPV6_USE_MIN_MTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006660#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006662 /* TCP options */
6663#ifdef TCP_NODELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006664 PyModule_AddIntMacro(m, TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00006665#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006666#ifdef TCP_MAXSEG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006667 PyModule_AddIntMacro(m, TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00006668#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006669#ifdef TCP_CORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006670 PyModule_AddIntMacro(m, TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006671#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006672#ifdef TCP_KEEPIDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006673 PyModule_AddIntMacro(m, TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006674#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006675#ifdef TCP_KEEPINTVL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006676 PyModule_AddIntMacro(m, TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006677#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006678#ifdef TCP_KEEPCNT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006679 PyModule_AddIntMacro(m, TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006680#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006681#ifdef TCP_SYNCNT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006682 PyModule_AddIntMacro(m, TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006683#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006684#ifdef TCP_LINGER2
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006685 PyModule_AddIntMacro(m, TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006686#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006687#ifdef TCP_DEFER_ACCEPT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006688 PyModule_AddIntMacro(m, TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006689#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006690#ifdef TCP_WINDOW_CLAMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006691 PyModule_AddIntMacro(m, TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006692#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006693#ifdef TCP_INFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006694 PyModule_AddIntMacro(m, TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006695#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006696#ifdef TCP_QUICKACK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006697 PyModule_AddIntMacro(m, TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006698#endif
Benjamin Peterson57b66722012-12-12 22:24:47 -05006699#ifdef TCP_FASTOPEN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006700 PyModule_AddIntMacro(m, TCP_FASTOPEN);
Benjamin Peterson57b66722012-12-12 22:24:47 -05006701#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006703 /* IPX options */
6704#ifdef IPX_TYPE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006705 PyModule_AddIntMacro(m, IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00006706#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00006707
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006708/* Reliable Datagram Sockets */
6709#ifdef RDS_CMSG_RDMA_ARGS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006710 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_ARGS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006711#endif
6712#ifdef RDS_CMSG_RDMA_DEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006713 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_DEST);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006714#endif
6715#ifdef RDS_CMSG_RDMA_MAP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006716 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_MAP);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006717#endif
6718#ifdef RDS_CMSG_RDMA_STATUS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006719 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_STATUS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006720#endif
6721#ifdef RDS_CMSG_RDMA_UPDATE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006722 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_UPDATE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006723#endif
6724#ifdef RDS_RDMA_READWRITE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006725 PyModule_AddIntMacro(m, RDS_RDMA_READWRITE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006726#endif
6727#ifdef RDS_RDMA_FENCE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006728 PyModule_AddIntMacro(m, RDS_RDMA_FENCE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006729#endif
6730#ifdef RDS_RDMA_INVALIDATE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006731 PyModule_AddIntMacro(m, RDS_RDMA_INVALIDATE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006732#endif
6733#ifdef RDS_RDMA_USE_ONCE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006734 PyModule_AddIntMacro(m, RDS_RDMA_USE_ONCE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006735#endif
6736#ifdef RDS_RDMA_DONTWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006737 PyModule_AddIntMacro(m, RDS_RDMA_DONTWAIT);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006738#endif
6739#ifdef RDS_RDMA_NOTIFY_ME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006740 PyModule_AddIntMacro(m, RDS_RDMA_NOTIFY_ME);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006741#endif
6742#ifdef RDS_RDMA_SILENT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006743 PyModule_AddIntMacro(m, RDS_RDMA_SILENT);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006744#endif
6745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006746 /* get{addr,name}info parameters */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006747#ifdef EAI_ADDRFAMILY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006748 PyModule_AddIntMacro(m, EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006749#endif
6750#ifdef EAI_AGAIN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006751 PyModule_AddIntMacro(m, EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006752#endif
6753#ifdef EAI_BADFLAGS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006754 PyModule_AddIntMacro(m, EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006755#endif
6756#ifdef EAI_FAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006757 PyModule_AddIntMacro(m, EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006758#endif
6759#ifdef EAI_FAMILY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006760 PyModule_AddIntMacro(m, EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006761#endif
6762#ifdef EAI_MEMORY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006763 PyModule_AddIntMacro(m, EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006764#endif
6765#ifdef EAI_NODATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006766 PyModule_AddIntMacro(m, EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006767#endif
6768#ifdef EAI_NONAME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006769 PyModule_AddIntMacro(m, EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006770#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00006771#ifdef EAI_OVERFLOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006772 PyModule_AddIntMacro(m, EAI_OVERFLOW);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006773#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006774#ifdef EAI_SERVICE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006775 PyModule_AddIntMacro(m, EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006776#endif
6777#ifdef EAI_SOCKTYPE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006778 PyModule_AddIntMacro(m, EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006779#endif
6780#ifdef EAI_SYSTEM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006781 PyModule_AddIntMacro(m, EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006782#endif
6783#ifdef EAI_BADHINTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006784 PyModule_AddIntMacro(m, EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006785#endif
6786#ifdef EAI_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006787 PyModule_AddIntMacro(m, EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006788#endif
6789#ifdef EAI_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006790 PyModule_AddIntMacro(m, EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006791#endif
6792#ifdef AI_PASSIVE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006793 PyModule_AddIntMacro(m, AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006794#endif
6795#ifdef AI_CANONNAME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006796 PyModule_AddIntMacro(m, AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006797#endif
6798#ifdef AI_NUMERICHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006799 PyModule_AddIntMacro(m, AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006800#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00006801#ifdef AI_NUMERICSERV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006802 PyModule_AddIntMacro(m, AI_NUMERICSERV);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006803#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006804#ifdef AI_MASK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006805 PyModule_AddIntMacro(m, AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006806#endif
6807#ifdef AI_ALL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006808 PyModule_AddIntMacro(m, AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006809#endif
6810#ifdef AI_V4MAPPED_CFG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006811 PyModule_AddIntMacro(m, AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006812#endif
6813#ifdef AI_ADDRCONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006814 PyModule_AddIntMacro(m, AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006815#endif
6816#ifdef AI_V4MAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006817 PyModule_AddIntMacro(m, AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006818#endif
6819#ifdef AI_DEFAULT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006820 PyModule_AddIntMacro(m, AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006821#endif
6822#ifdef NI_MAXHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006823 PyModule_AddIntMacro(m, NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006824#endif
6825#ifdef NI_MAXSERV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006826 PyModule_AddIntMacro(m, NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006827#endif
6828#ifdef NI_NOFQDN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006829 PyModule_AddIntMacro(m, NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006830#endif
6831#ifdef NI_NUMERICHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006832 PyModule_AddIntMacro(m, NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006833#endif
6834#ifdef NI_NAMEREQD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006835 PyModule_AddIntMacro(m, NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006836#endif
6837#ifdef NI_NUMERICSERV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006838 PyModule_AddIntMacro(m, NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006839#endif
6840#ifdef NI_DGRAM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006841 PyModule_AddIntMacro(m, NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006842#endif
6843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006844 /* shutdown() parameters */
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006845#ifdef SHUT_RD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006846 PyModule_AddIntMacro(m, SHUT_RD);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006847#elif defined(SD_RECEIVE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006848 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006849#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006850 PyModule_AddIntConstant(m, "SHUT_RD", 0);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006851#endif
6852#ifdef SHUT_WR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006853 PyModule_AddIntMacro(m, SHUT_WR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006854#elif defined(SD_SEND)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006855 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006856#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006857 PyModule_AddIntConstant(m, "SHUT_WR", 1);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006858#endif
6859#ifdef SHUT_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006860 PyModule_AddIntMacro(m, SHUT_RDWR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006861#elif defined(SD_BOTH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006862 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006863#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006864 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006865#endif
6866
Christian Heimesfaf2f632008-01-06 16:59:19 +00006867#ifdef SIO_RCVALL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006868 {
6869 DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS};
6870 const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"};
6871 int i;
Victor Stinner63941882011-09-29 00:42:28 +02006872 for(i = 0; i<Py_ARRAY_LENGTH(codes); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006873 PyObject *tmp;
6874 tmp = PyLong_FromUnsignedLong(codes[i]);
6875 if (tmp == NULL)
6876 return NULL;
6877 PyModule_AddObject(m, names[i], tmp);
6878 }
6879 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006880 PyModule_AddIntMacro(m, RCVALL_OFF);
6881 PyModule_AddIntMacro(m, RCVALL_ON);
6882 PyModule_AddIntMacro(m, RCVALL_SOCKETLEVELONLY);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00006883#ifdef RCVALL_IPLEVEL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006884 PyModule_AddIntMacro(m, RCVALL_IPLEVEL);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00006885#endif
6886#ifdef RCVALL_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006887 PyModule_AddIntMacro(m, RCVALL_MAX);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00006888#endif
Christian Heimesfaf2f632008-01-06 16:59:19 +00006889#endif /* _MSTCPIP_ */
6890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006891 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00006892#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006893 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00006894#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006895 return m;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00006896}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006897
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006898
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006899#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +00006900#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006901
6902/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00006903/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006904
Guido van Rossum3eede5a2002-06-07 02:08:35 +00006905int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006906inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006908 if (af == AF_INET) {
Benjamin Petersonf91df042009-02-13 02:50:59 +00006909#if (SIZEOF_INT != 4)
6910#error "Not sure if in_addr_t exists and int is not 32-bits."
6911#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006912 unsigned int packed_addr;
6913 packed_addr = inet_addr(src);
6914 if (packed_addr == INADDR_NONE)
6915 return 0;
6916 memcpy(dst, &packed_addr, 4);
6917 return 1;
6918 }
6919 /* Should set errno to EAFNOSUPPORT */
6920 return -1;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006921}
6922
Martin v. Löwisc925b1532001-07-21 09:42:15 +00006923const char *
6924inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006926 if (af == AF_INET) {
6927 struct in_addr packed_addr;
6928 if (size < 16)
6929 /* Should set errno to ENOSPC. */
6930 return NULL;
6931 memcpy(&packed_addr, src, sizeof(packed_addr));
6932 return strncpy(dst, inet_ntoa(packed_addr), size);
6933 }
6934 /* Should set errno to EAFNOSUPPORT */
6935 return NULL;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006936}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006937
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006938#endif
Christian Heimesb6150692007-11-15 23:37:07 +00006939#endif