blob: 46265ade891160bd8a4694d37f0194f7bd74c1b5 [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,
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000100"socket([family[, type[, proto]]]) -> socket object\n\
101\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\
107\n\
108A socket object represents one endpoint of a network connection.\n\
109\n\
110Methods of socket objects (keyword arguments not allowed):\n\
111\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000112_accept() -- accept connection, returning new socket fd and client address\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000113bind(addr) -- bind the socket to a local address\n\
114close() -- close the socket\n\
115connect(addr) -- connect the socket to a remote address\n\
116connect_ex(addr) -- connect, return an error code instead of an exception\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000117_dup() -- return a new socket fd duplicated from fileno()\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000118fileno() -- return underlying file descriptor\n\
119getpeername() -- return remote address [*]\n\
120getsockname() -- return local address\n\
121getsockopt(level, optname[, buflen]) -- get socket options\n\
122gettimeout() -- return timeout or None\n\
123listen(n) -- start listening for incoming connections\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000124recv(buflen[, flags]) -- receive data\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000125recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000126recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000127recvfrom_into(buffer[, nbytes, [, flags])\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000128 -- receive data and sender\'s address (into a buffer)\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000129sendall(data[, flags]) -- send all data\n\
130send(data[, flags]) -- send data, may not send all of it\n\
131sendto(data[, flags], addr) -- send data to a given address\n\
132setblocking(0 | 1) -- set or clear the blocking I/O flag\n\
133setsockopt(level, optname, value) -- set socket options\n\
134settimeout(None | float) -- set or clear the timeout\n\
135shutdown(how) -- shut down traffic in one or both directions\n\
Gregory P. Smith5ed2e772011-05-15 00:26:45 -0700136if_nameindex() -- return all network interface indices and names\n\
Gregory P. Smithb6471db2011-05-22 22:47:55 -0700137if_nametoindex(name) -- return the corresponding interface index\n\
138if_indextoname(index) -- return the corresponding interface name\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000139\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000140 [*] not available on all platforms!");
Guido van Rossum3baaa131999-03-22 21:44:51 +0000141
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000142/* XXX This is a terrible mess of platform-dependent preprocessor hacks.
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000143 I hope some day someone can clean this up please... */
144
Guido van Rossum9376b741999-09-15 22:01:40 +0000145/* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
146 script doesn't get this right, so we hardcode some platform checks below.
147 On the other hand, not all Linux versions agree, so there the settings
148 computed by the configure script are needed! */
149
150#ifndef linux
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000151# undef HAVE_GETHOSTBYNAME_R_3_ARG
152# undef HAVE_GETHOSTBYNAME_R_5_ARG
153# undef HAVE_GETHOSTBYNAME_R_6_ARG
Guido van Rossum9376b741999-09-15 22:01:40 +0000154#endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000155
Victor Stinner710d27e2011-08-23 10:57:32 +0200156#if defined(__OpenBSD__)
157# include <sys/uio.h>
158#endif
159
Guido van Rossum7a122991999-04-13 04:07:32 +0000160#ifndef WITH_THREAD
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000161# undef HAVE_GETHOSTBYNAME_R
Guido van Rossum7a122991999-04-13 04:07:32 +0000162#endif
163
Guido van Rossume7de2061999-03-24 17:24:33 +0000164#ifdef HAVE_GETHOSTBYNAME_R
Jesus Cea736e7fc2011-03-14 17:36:54 +0100165# if defined(_AIX)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000166# define HAVE_GETHOSTBYNAME_R_3_ARG
167# elif defined(__sun) || defined(__sgi)
168# define HAVE_GETHOSTBYNAME_R_5_ARG
169# elif defined(linux)
Guido van Rossum9376b741999-09-15 22:01:40 +0000170/* Rely on the configure script */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000171# else
172# undef HAVE_GETHOSTBYNAME_R
173# endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000174#endif
175
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000176#if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \
177 !defined(MS_WINDOWS)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000178# define USE_GETHOSTBYNAME_LOCK
Guido van Rossum3baaa131999-03-22 21:44:51 +0000179#endif
180
Hye-Shik Chang9ceebd52005-09-24 14:58:47 +0000181/* To use __FreeBSD_version */
182#ifdef HAVE_SYS_PARAM_H
183#include <sys/param.h>
184#endif
Just van Rossum1040d2c2003-05-09 07:53:18 +0000185/* On systems on which getaddrinfo() is believed to not be thread-safe,
Just van Rossum09aecd72003-05-09 08:03:44 +0000186 (this includes the getaddrinfo emulation) protect access with a lock. */
Hye-Shik Chang9ceebd52005-09-24 14:58:47 +0000187#if defined(WITH_THREAD) && (defined(__APPLE__) || \
188 (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000189 defined(__OpenBSD__) || defined(__NetBSD__) || \
190 defined(__VMS) || !defined(HAVE_GETADDRINFO))
Just van Rossum1040d2c2003-05-09 07:53:18 +0000191#define USE_GETADDRINFO_LOCK
192#endif
193
194#ifdef USE_GETADDRINFO_LOCK
195#define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1);
196#define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock);
197#else
198#define ACQUIRE_GETADDRINFO_LOCK
199#define RELEASE_GETADDRINFO_LOCK
200#endif
201
202#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000203# include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000204#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000205
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000206#if defined(PYCC_VACPP)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000207# include <types.h>
208# include <io.h>
209# include <sys/ioctl.h>
210# include <utils.h>
211# include <ctype.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000212#endif
213
Martin v. Löwis9e437302002-12-06 12:57:26 +0000214#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000215# include <ioctl.h>
216#endif
217
Martin v. Löwis9d6c6692012-02-03 17:44:58 +0100218#ifdef __APPLE__
219# include <sys/ioctl.h>
220#endif
221
222
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000223#if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000224/* make sure that the reentrant (gethostbyaddr_r etc)
225 functions are declared correctly if compiling with
226 MIPSPro 7.x in ANSI C mode (default) */
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000227
Thomas Wouters477c8d52006-05-27 19:21:47 +0000228/* XXX Using _SGIAPI is the wrong thing,
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000229 but I don't know what the right thing is. */
Trent Mick8ea5bdf2004-09-13 17:48:41 +0000230#undef _SGIAPI /* to avoid warning */
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000231#define _SGIAPI 1
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000232
Trent Mick8ea5bdf2004-09-13 17:48:41 +0000233#undef _XOPEN_SOURCE
234#include <sys/socket.h>
235#include <sys/types.h>
236#include <netinet/in.h>
237#ifdef _SS_ALIGNSIZE
238#define HAVE_GETADDRINFO 1
239#define HAVE_GETNAMEINFO 1
240#endif
241
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000242#define HAVE_INET_PTON
243#include <netdb.h>
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000244#endif
245
Thomas Wouters477c8d52006-05-27 19:21:47 +0000246/* Irix 6.5 fails to define this variable at all. This is needed
247 for both GCC and SGI's compiler. I'd say that the SGI headers
Georg Brandldbd83392006-02-20 09:42:33 +0000248 are just busted. Same thing for Solaris. */
249#if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN)
Anthony Baxterbab23cf2003-10-04 08:00:49 +0000250#define INET_ADDRSTRLEN 16
251#endif
252
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000253/* Generic includes */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000254#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000256#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000257
Gregory P. Smith3b1f2c32011-05-15 12:18:23 -0700258#ifdef HAVE_SYS_SOCKET_H
259#include <sys/socket.h>
260#endif
261
262#ifdef HAVE_NET_IF_H
263#include <net/if.h>
264#endif
265
Marc-André Lemburg976ade62002-02-16 18:47:07 +0000266/* Generic socket object definitions and includes */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000267#define PySocket_BUILDING_SOCKET
Marc-André Lemburgbb8b78b2002-02-16 18:44:52 +0000268#include "socketmodule.h"
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000269
270/* Addressing includes */
271
Guido van Rossum6f489d91996-06-28 20:15:15 +0000272#ifndef MS_WINDOWS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000273
274/* Non-MS WINDOWS includes */
275# include <netdb.h>
Nick Coghlan4ecf6cf2011-08-22 15:45:19 +1000276# include <unistd.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000277
Guido van Rossum9376b741999-09-15 22:01:40 +0000278/* Headers needed for inet_ntoa() and inet_addr() */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000279# include <arpa/inet.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000280
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000281# include <fcntl.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000282
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000283#else
Guido van Rossum48a680c2001-03-02 06:34:14 +0000284
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000285/* MS_WINDOWS includes */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000286# ifdef HAVE_FCNTL_H
287# include <fcntl.h>
288# endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000289
Jeremy Hylton22308652001-02-02 03:23:09 +0000290#endif
291
Skip Montanaro7befb992004-02-10 16:50:21 +0000292#include <stddef.h>
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000293
294#ifndef offsetof
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295# define offsetof(type, member) ((size_t)(&((type *)0)->member))
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000296#endif
297
Neal Norwitz39d22e52002-11-02 19:55:21 +0000298#ifndef O_NONBLOCK
299# define O_NONBLOCK O_NDELAY
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000300#endif
301
Trent Micka708d6e2004-09-07 17:48:26 +0000302/* include Python's addrinfo.h unless it causes trouble */
303#if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE)
304 /* Do not include addinfo.h on some newer IRIX versions.
305 * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21,
306 * for example, but not by 6.5.10.
307 */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000308#elif defined(_MSC_VER) && _MSC_VER>1201
Trent Micka708d6e2004-09-07 17:48:26 +0000309 /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and
310 * EAI_* constants are defined in (the already included) ws2tcpip.h.
311 */
312#else
313# include "addrinfo.h"
314#endif
Jason Tishlerc246cb72004-08-09 13:25:59 +0000315
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000316#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +0000317#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000318int inet_pton(int af, const char *src, void *dst);
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000319const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000320#endif
Christian Heimesb6150692007-11-15 23:37:07 +0000321#endif
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000322
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000323#ifdef __APPLE__
324/* On OS X, getaddrinfo returns no error indication of lookup
325 failure, so we must use the emulation instead of the libinfo
326 implementation. Unfortunately, performing an autoconf test
327 for this bug would require DNS access for the machine performing
328 the configuration, which is not acceptable. Therefore, we
329 determine the bug just by checking for __APPLE__. If this bug
330 gets ever fixed, perhaps checking for sys/version.h would be
331 appropriate, which is 10/0 on the system with the bug. */
Jack Jansen84262fb2002-07-02 14:40:42 +0000332#ifndef HAVE_GETNAMEINFO
333/* This bug seems to be fixed in Jaguar. Ths easiest way I could
334 Find to check for Jaguar is that it has getnameinfo(), which
335 older releases don't have */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000336#undef HAVE_GETADDRINFO
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000337#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000338
339#ifdef HAVE_INET_ATON
340#define USE_INET_ATON_WEAKLINK
341#endif
342
Jack Jansen84262fb2002-07-02 14:40:42 +0000343#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000344
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000345/* I know this is a bad practice, but it is the easiest... */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000346#if !defined(HAVE_GETADDRINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000347/* avoid clashes with the C library definition of the symbol. */
348#define getaddrinfo fake_getaddrinfo
349#define gai_strerror fake_gai_strerror
350#define freeaddrinfo fake_freeaddrinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000351#include "getaddrinfo.c"
352#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000353#if !defined(HAVE_GETNAMEINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000354#define getnameinfo fake_getnameinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000355#include "getnameinfo.c"
356#endif
357
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000358#ifdef MS_WINDOWS
359/* On Windows a socket is really a handle not an fd */
360static SOCKET
361dup_socket(SOCKET handle)
362{
Daniel Stutzbach6c765282010-09-03 12:38:33 +0000363 WSAPROTOCOL_INFO info;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000364
Daniel Stutzbach6c765282010-09-03 12:38:33 +0000365 if (WSADuplicateSocket(handle, GetCurrentProcessId(), &info))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 return INVALID_SOCKET;
Daniel Stutzbach6c765282010-09-03 12:38:33 +0000367
368 return WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
Antoine Pitrou9a54a262012-04-01 01:14:39 +0200369 FROM_PROTOCOL_INFO, &info, 0, WSA_FLAG_OVERLAPPED);
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000370}
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000371#define SOCKETCLOSE closesocket
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000372#else
373/* On Unix we can use dup to duplicate the file descriptor of a socket*/
374#define dup_socket(fd) dup(fd)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000375#endif
376
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000377#ifdef MS_WIN32
Kristján Valur Jónsson310052c2012-03-31 13:35:00 +0000378#undef EAFNOSUPPORT
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000379#define EAFNOSUPPORT WSAEAFNOSUPPORT
380#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000381#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000382
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000383#ifndef SOCKETCLOSE
384#define SOCKETCLOSE close
385#endif
386
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +0000387#if (defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)) && !defined(__NetBSD__) && !defined(__DragonFly__)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000388#define USE_BLUETOOTH 1
389#if defined(__FreeBSD__)
390#define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP
391#define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM
Thomas Wouterscf297e42007-02-23 15:07:44 +0000392#define BTPROTO_HCI BLUETOOTH_PROTO_HCI
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000393#define SOL_HCI SOL_HCI_RAW
394#define HCI_FILTER SO_HCI_RAW_FILTER
Hye-Shik Chang81268602004-02-02 06:05:24 +0000395#define sockaddr_l2 sockaddr_l2cap
396#define sockaddr_rc sockaddr_rfcomm
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000397#define hci_dev hci_node
Hye-Shik Chang81268602004-02-02 06:05:24 +0000398#define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb)
399#define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000400#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +0000401#elif defined(__NetBSD__) || defined(__DragonFly__)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000402#define sockaddr_l2 sockaddr_bt
403#define sockaddr_rc sockaddr_bt
Thomas Wouterscf297e42007-02-23 15:07:44 +0000404#define sockaddr_hci sockaddr_bt
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000405#define sockaddr_sco sockaddr_bt
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +0000406#define SOL_HCI BTPROTO_HCI
407#define HCI_DATA_DIR SO_HCI_DIRECTION
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000408#define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb)
409#define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000410#define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000411#define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000412#else
Hye-Shik Chang81268602004-02-02 06:05:24 +0000413#define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)
414#define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000415#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000416#define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)
417#endif
418#endif
419
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000420#ifdef __VMS
421/* TCP/IP Services for VMS uses a maximum send/recv buffer length */
422#define SEGMENT_SIZE (32 * 1024 -1)
423#endif
424
Charles-François Natali8b759652011-12-23 16:44:51 +0100425/* Convert "sock_addr_t *" to "struct sockaddr *". */
426#define SAS2SA(x) (&((x)->sa))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000427
Martin v. Löwise9416172003-05-03 10:12:45 +0000428/*
429 * Constants for getnameinfo()
430 */
431#if !defined(NI_MAXHOST)
432#define NI_MAXHOST 1025
433#endif
434#if !defined(NI_MAXSERV)
435#define NI_MAXSERV 32
436#endif
437
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000438#ifndef INVALID_SOCKET /* MS defines this */
439#define INVALID_SOCKET (-1)
440#endif
441
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000442/* XXX There's a problem here: *static* functions are not supposed to have
443 a Py prefix (or use CapitalizedWords). Later... */
444
Guido van Rossum30a685f1991-06-27 15:51:29 +0000445/* Global variable holding the exception type for errors detected
446 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000447static PyObject *socket_herror;
448static PyObject *socket_gaierror;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000449static PyObject *socket_timeout;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000450
Tim Peters643a7fc2002-02-17 04:13:21 +0000451/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000452 The sock_type variable contains pointers to various functions,
453 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000454 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000455static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000456
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000457#if defined(HAVE_POLL_H)
458#include <poll.h>
459#elif defined(HAVE_SYS_POLL_H)
460#include <sys/poll.h>
461#endif
462
Nick Coghlan96fe56a2011-08-22 11:55:57 +1000463/* Largest value to try to store in a socklen_t (used when handling
464 ancillary data). POSIX requires socklen_t to hold at least
465 (2**31)-1 and recommends against storing larger values, but
466 socklen_t was originally int in the BSD interface, so to be on the
467 safe side we use the smaller of (2**31)-1 and INT_MAX. */
468#if INT_MAX > 0x7fffffff
469#define SOCKLEN_T_LIMIT 0x7fffffff
470#else
471#define SOCKLEN_T_LIMIT INT_MAX
472#endif
473
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200474#ifdef HAVE_POLL
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000475/* Instead of select(), we'll use poll() since poll() works on any fd. */
476#define IS_SELECTABLE(s) 1
477/* Can we call select() with this socket without a buffer overrun? */
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000478#else
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200479/* If there's no timeout left, we don't have to call select, so it's a safe,
480 * little white lie. */
481#define IS_SELECTABLE(s) (_PyIsSelectable_fd((s)->sock_fd) || (s)->sock_timeout <= 0.0)
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000482#endif
Neal Norwitz082b2df2006-02-07 07:04:46 +0000483
484static PyObject*
485select_error(void)
486{
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200487 PyErr_SetString(PyExc_OSError, "unable to select on socket");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 return NULL;
Neal Norwitz082b2df2006-02-07 07:04:46 +0000489}
490
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000491#ifdef MS_WINDOWS
Antoine Pitrouc8f01962010-09-28 22:03:27 +0000492#ifndef WSAEAGAIN
493#define WSAEAGAIN WSAEWOULDBLOCK
494#endif
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000495#define CHECK_ERRNO(expected) \
496 (WSAGetLastError() == WSA ## expected)
497#else
498#define CHECK_ERRNO(expected) \
499 (errno == expected)
500#endif
501
Guido van Rossum30a685f1991-06-27 15:51:29 +0000502/* Convenience function to raise an error according to errno
503 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000504
Guido van Rossum73624e91994-10-10 17:59:00 +0000505static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000506set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000507{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000508#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 int err_no = WSAGetLastError();
510 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
511 recognizes the error codes used by both GetLastError() and
512 WSAGetLastError */
513 if (err_no)
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200514 return PyErr_SetExcFromWindowsErr(PyExc_OSError, err_no);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000515#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000516
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200517 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000518}
519
Guido van Rossum30a685f1991-06-27 15:51:29 +0000520
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000521static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000522set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000525
526#ifdef HAVE_HSTRERROR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000528#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 v = Py_BuildValue("(is)", h_error, "host not found");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000530#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 if (v != NULL) {
532 PyErr_SetObject(socket_herror, v);
533 Py_DECREF(v);
534 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000537}
538
539
540static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000541set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000544
Martin v. Löwis272cb402002-03-01 08:31:07 +0000545#ifdef EAI_SYSTEM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 /* EAI_SYSTEM is not available on Windows XP. */
547 if (error == EAI_SYSTEM)
548 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000549#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000550
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000551#ifdef HAVE_GAI_STRERROR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000553#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000555#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 if (v != NULL) {
557 PyErr_SetObject(socket_gaierror, v);
558 Py_DECREF(v);
559 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000562}
563
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000564#ifdef __VMS
565/* Function to send in segments */
566static int
567sendsegmented(int sock_fd, char *buf, int len, int flags)
568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 int n = 0;
570 int remaining = len;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 while (remaining > 0) {
573 unsigned int segment;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining);
576 n = send(sock_fd, buf, segment, flags);
577 if (n < 0) {
578 return n;
579 }
580 remaining -= segment;
581 buf += segment;
582 } /* end while */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 return len;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000585}
586#endif
587
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000588/* Function to perform the setting of socket blocking mode
589 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000590static int
591internal_setblocking(PySocketSockObject *s, int block)
592{
Guido van Rossum67f7a382002-06-06 21:08:16 +0000593#ifndef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 int delay_flag;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000595#endif
Antoine Pitroub1c54962010-10-14 15:05:38 +0000596#ifdef SOCK_NONBLOCK
597 if (block)
598 s->sock_type &= (~SOCK_NONBLOCK);
599 else
600 s->sock_type |= SOCK_NONBLOCK;
601#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +0000602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 Py_BEGIN_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000604#ifndef MS_WINDOWS
Jesus Cea14c81ab2012-10-05 02:11:36 +0200605#if defined(__VMS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 block = !block;
607 ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);
Jesus Cea14c81ab2012-10-05 02:11:36 +0200608#else /* !__VMS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
610 if (block)
611 delay_flag &= (~O_NONBLOCK);
612 else
613 delay_flag |= O_NONBLOCK;
614 fcntl(s->sock_fd, F_SETFL, delay_flag);
Jesus Cea14c81ab2012-10-05 02:11:36 +0200615#endif /* !__VMS */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000616#else /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 block = !block;
618 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000619#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 /* Since these don't return anything */
623 return 1;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000624}
625
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000626/* Do a select()/poll() on the socket, if necessary (sock_timeout > 0).
Guido van Rossum11ba0942002-06-13 15:07:44 +0000627 The argument writing indicates the direction.
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000628 This does not raise an exception; we'll let our caller do that
629 after they've reacquired the interpreter lock.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000630 Returns 1 on timeout, -1 on error, 0 otherwise. */
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000631static int
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000632internal_select_ex(PySocketSockObject *s, int writing, double interval)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 int n;
Guido van Rossum11ba0942002-06-13 15:07:44 +0000635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 /* Nothing to do unless we're in timeout mode (not non-blocking) */
637 if (s->sock_timeout <= 0.0)
638 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 /* Guard against closed socket */
641 if (s->sock_fd < 0)
642 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000643
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000644 /* Handling this condition here simplifies the select loops */
645 if (interval < 0.0)
646 return 1;
647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 /* Prefer poll, if available, since you can poll() any fd
649 * which can't be done with select(). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000650#ifdef HAVE_POLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 {
652 struct pollfd pollfd;
653 int timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 pollfd.fd = s->sock_fd;
656 pollfd.events = writing ? POLLOUT : POLLIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 /* s->sock_timeout is in seconds, timeout in ms */
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000659 timeout = (int)(interval * 1000 + 0.5);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 n = poll(&pollfd, 1, timeout);
661 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000662#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 {
664 /* Construct the arguments to select */
665 fd_set fds;
666 struct timeval tv;
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000667 tv.tv_sec = (int)interval;
668 tv.tv_usec = (int)((interval - tv.tv_sec) * 1e6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 FD_ZERO(&fds);
670 FD_SET(s->sock_fd, &fds);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 /* See if the socket is ready */
673 if (writing)
Antoine Pitrou19467d22010-08-17 19:33:30 +0000674 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
675 NULL, &fds, NULL, &tv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 else
Antoine Pitrou19467d22010-08-17 19:33:30 +0000677 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
678 &fds, NULL, NULL, &tv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000680#endif
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 if (n < 0)
683 return -1;
684 if (n == 0)
685 return 1;
686 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000687}
688
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000689static int
690internal_select(PySocketSockObject *s, int writing)
691{
692 return internal_select_ex(s, writing, s->sock_timeout);
693}
694
695/*
696 Two macros for automatic retry of select() in case of false positives
697 (for example, select() could indicate a socket is ready for reading
698 but the data then discarded by the OS because of a wrong checksum).
699 Here is an example of use:
700
701 BEGIN_SELECT_LOOP(s)
702 Py_BEGIN_ALLOW_THREADS
703 timeout = internal_select_ex(s, 0, interval);
704 if (!timeout)
705 outlen = recv(s->sock_fd, cbuf, len, flags);
706 Py_END_ALLOW_THREADS
707 if (timeout == 1) {
708 PyErr_SetString(socket_timeout, "timed out");
709 return -1;
710 }
711 END_SELECT_LOOP(s)
712*/
713
714#define BEGIN_SELECT_LOOP(s) \
715 { \
716 _PyTime_timeval now, deadline = {0, 0}; \
717 double interval = s->sock_timeout; \
718 int has_timeout = s->sock_timeout > 0.0; \
719 if (has_timeout) { \
720 _PyTime_gettimeofday(&now); \
721 deadline = now; \
722 _PyTime_ADD_SECONDS(deadline, s->sock_timeout); \
723 } \
724 while (1) { \
725 errno = 0; \
726
727#define END_SELECT_LOOP(s) \
728 if (!has_timeout || \
729 (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \
730 break; \
731 _PyTime_gettimeofday(&now); \
732 interval = _PyTime_INTERVAL(now, deadline); \
733 } \
734 } \
735
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000736/* Initialize a new socket object. */
737
Tim Petersa12b4cf2002-07-18 22:38:44 +0000738static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000739
Martin v. Löwis1a214512008-06-11 05:26:20 +0000740static void
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000741init_sockobject(PySocketSockObject *s,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 SOCKET_T fd, int family, int type, int proto)
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 s->sock_fd = fd;
745 s->sock_family = family;
746 s->sock_type = type;
747 s->sock_proto = proto;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 s->errorhandler = &set_error;
Antoine Pitroub1c54962010-10-14 15:05:38 +0000750#ifdef SOCK_NONBLOCK
751 if (type & SOCK_NONBLOCK)
752 s->sock_timeout = 0.0;
753 else
754#endif
755 {
756 s->sock_timeout = defaulttimeout;
757 if (defaulttimeout >= 0.0)
758 internal_setblocking(s, 0);
759 }
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000760
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000761}
762
763
Guido van Rossum30a685f1991-06-27 15:51:29 +0000764/* Create a new socket object.
765 This just creates the object and initializes it.
766 If the creation fails, return NULL and set an exception (implicit
767 in NEWOBJ()). */
768
Guido van Rossum73624e91994-10-10 17:59:00 +0000769static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000770new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 PySocketSockObject *s;
773 s = (PySocketSockObject *)
774 PyType_GenericNew(&sock_type, NULL, NULL);
775 if (s != NULL)
776 init_sockobject(s, fd, family, type, proto);
777 return s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000778}
779
Guido van Rossum30a685f1991-06-27 15:51:29 +0000780
Guido van Rossum48a680c2001-03-02 06:34:14 +0000781/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000782 thread to be in gethostbyname or getaddrinfo */
783#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Matthias Klosed182a6c2012-08-14 17:29:04 +0200784static PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000785#endif
786
787
Guido van Rossum30a685f1991-06-27 15:51:29 +0000788/* Convert a string specifying a host name or one of a few symbolic
789 names to a numeric IP address. This usually calls gethostbyname()
790 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000791 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000792 an error occurred; then an exception is raised. */
793
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000794static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000795setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000796{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 struct addrinfo hints, *res;
798 int error;
799 int d1, d2, d3, d4;
800 char ch;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
803 if (name[0] == '\0') {
804 int siz;
805 memset(&hints, 0, sizeof(hints));
806 hints.ai_family = af;
807 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
808 hints.ai_flags = AI_PASSIVE;
809 Py_BEGIN_ALLOW_THREADS
810 ACQUIRE_GETADDRINFO_LOCK
811 error = getaddrinfo(NULL, "0", &hints, &res);
812 Py_END_ALLOW_THREADS
813 /* We assume that those thread-unsafe getaddrinfo() versions
814 *are* safe regarding their return value, ie. that a
815 subsequent call to getaddrinfo() does not destroy the
816 outcome of the first call. */
817 RELEASE_GETADDRINFO_LOCK
818 if (error) {
819 set_gaierror(error);
820 return -1;
821 }
822 switch (res->ai_family) {
823 case AF_INET:
824 siz = 4;
825 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000826#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 case AF_INET6:
828 siz = 16;
829 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000830#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 default:
832 freeaddrinfo(res);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200833 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 "unsupported address family");
835 return -1;
836 }
837 if (res->ai_next) {
838 freeaddrinfo(res);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200839 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 "wildcard resolved to multiple address");
841 return -1;
842 }
843 if (res->ai_addrlen < addr_ret_size)
844 addr_ret_size = res->ai_addrlen;
845 memcpy(addr_ret, res->ai_addr, addr_ret_size);
846 freeaddrinfo(res);
847 return siz;
848 }
849 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
850 struct sockaddr_in *sin;
851 if (af != AF_INET && af != AF_UNSPEC) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200852 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 "address family mismatched");
854 return -1;
855 }
856 sin = (struct sockaddr_in *)addr_ret;
857 memset((void *) sin, '\0', sizeof(*sin));
858 sin->sin_family = AF_INET;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000859#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 sin->sin_len = sizeof(*sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000861#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 sin->sin_addr.s_addr = INADDR_BROADCAST;
863 return sizeof(sin->sin_addr);
864 }
865 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
866 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
867 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
868 struct sockaddr_in *sin;
869 sin = (struct sockaddr_in *)addr_ret;
870 sin->sin_addr.s_addr = htonl(
871 ((long) d1 << 24) | ((long) d2 << 16) |
872 ((long) d3 << 8) | ((long) d4 << 0));
873 sin->sin_family = AF_INET;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000874#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 sin->sin_len = sizeof(*sin);
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000876#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 return 4;
878 }
879 memset(&hints, 0, sizeof(hints));
880 hints.ai_family = af;
881 Py_BEGIN_ALLOW_THREADS
882 ACQUIRE_GETADDRINFO_LOCK
883 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000884#if defined(__digital__) && defined(__unix__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 if (error == EAI_NONAME && af == AF_UNSPEC) {
886 /* On Tru64 V5.1, numeric-to-addr conversion fails
887 if no address family is given. Assume IPv4 for now.*/
888 hints.ai_family = AF_INET;
889 error = getaddrinfo(name, NULL, &hints, &res);
890 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000891#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 Py_END_ALLOW_THREADS
893 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
894 if (error) {
895 set_gaierror(error);
896 return -1;
897 }
898 if (res->ai_addrlen < addr_ret_size)
899 addr_ret_size = res->ai_addrlen;
900 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
901 freeaddrinfo(res);
902 switch (addr_ret->sa_family) {
903 case AF_INET:
904 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000905#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 case AF_INET6:
907 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000908#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200910 PyErr_SetString(PyExc_OSError, "unknown address family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 return -1;
912 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000913}
914
Guido van Rossum30a685f1991-06-27 15:51:29 +0000915
Guido van Rossum30a685f1991-06-27 15:51:29 +0000916/* Create a string object representing an IP address.
917 This is always a string of the form 'dd.dd.dd.dd' (with variable
918 size numbers). */
919
Guido van Rossum73624e91994-10-10 17:59:00 +0000920static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000921makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000922{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 char buf[NI_MAXHOST];
924 int error;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
927 NI_NUMERICHOST);
928 if (error) {
929 set_gaierror(error);
930 return NULL;
931 }
932 return PyUnicode_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000933}
934
935
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000936#ifdef USE_BLUETOOTH
937/* Convert a string representation of a Bluetooth address into a numeric
938 address. Returns the length (6), or raises an exception and returns -1 if
939 an error occurred. */
940
941static int
942setbdaddr(char *name, bdaddr_t *bdaddr)
943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 unsigned int b0, b1, b2, b3, b4, b5;
945 char ch;
946 int n;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
949 &b5, &b4, &b3, &b2, &b1, &b0, &ch);
950 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
951 bdaddr->b[0] = b0;
952 bdaddr->b[1] = b1;
953 bdaddr->b[2] = b2;
954 bdaddr->b[3] = b3;
955 bdaddr->b[4] = b4;
956 bdaddr->b[5] = b5;
957 return 6;
958 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200959 PyErr_SetString(PyExc_OSError, "bad bluetooth address");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 return -1;
961 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000962}
963
964/* Create a string representation of the Bluetooth address. This is always a
965 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
966 value (zero padded if necessary). */
967
968static PyObject *
969makebdaddr(bdaddr_t *bdaddr)
970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 char buf[(6 * 2) + 5 + 1];
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
974 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
975 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
976 return PyUnicode_FromString(buf);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000977}
978#endif
979
980
Guido van Rossum30a685f1991-06-27 15:51:29 +0000981/* Create an object representing the given socket address,
982 suitable for passing it back to bind(), connect() etc.
983 The family field of the sockaddr structure is inspected
984 to determine what kind of address it really is. */
985
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000986/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000987static PyObject *
Antoine Pitrou19467d22010-08-17 19:33:30 +0000988makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 if (addrlen == 0) {
991 /* No address -- may be recvfrom() from known socket */
992 Py_INCREF(Py_None);
993 return Py_None;
994 }
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 switch (addr->sa_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +0000997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 case AF_INET:
999 {
1000 struct sockaddr_in *a;
1001 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
1002 PyObject *ret = NULL;
1003 if (addrobj) {
1004 a = (struct sockaddr_in *)addr;
1005 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
1006 Py_DECREF(addrobj);
1007 }
1008 return ret;
1009 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00001010
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001011#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 case AF_UNIX:
1013 {
1014 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001015#ifdef linux
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 if (a->sun_path[0] == 0) { /* Linux abstract namespace */
1017 addrlen -= offsetof(struct sockaddr_un, sun_path);
Charles-François Natalib10c71d2013-05-21 10:45:46 +02001018 return PyBytes_FromStringAndSize(a->sun_path, addrlen);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 }
1020 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001021#endif /* linux */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 {
1023 /* regular NULL-terminated string */
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001024 return PyUnicode_DecodeFSDefault(a->sun_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 }
1026 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001027#endif /* AF_UNIX */
1028
Martin v. Löwis11017b12006-01-14 18:12:57 +00001029#if defined(AF_NETLINK)
1030 case AF_NETLINK:
1031 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
1033 return Py_BuildValue("II", a->nl_pid, a->nl_groups);
Martin v. Löwis11017b12006-01-14 18:12:57 +00001034 }
1035#endif /* AF_NETLINK */
1036
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001037#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 case AF_INET6:
1039 {
1040 struct sockaddr_in6 *a;
1041 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
1042 PyObject *ret = NULL;
1043 if (addrobj) {
1044 a = (struct sockaddr_in6 *)addr;
Charles-François Natali366999a2012-01-02 15:47:29 +01001045 ret = Py_BuildValue("OiII",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 addrobj,
1047 ntohs(a->sin6_port),
Charles-François Natali366999a2012-01-02 15:47:29 +01001048 ntohl(a->sin6_flowinfo),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 a->sin6_scope_id);
1050 Py_DECREF(addrobj);
1051 }
1052 return ret;
1053 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001054#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001055
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001056#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 case AF_BLUETOOTH:
1058 switch (proto) {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 case BTPROTO_L2CAP:
1061 {
1062 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
1063 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
1064 PyObject *ret = NULL;
1065 if (addrobj) {
1066 ret = Py_BuildValue("Oi",
1067 addrobj,
1068 _BT_L2_MEMB(a, psm));
1069 Py_DECREF(addrobj);
1070 }
1071 return ret;
1072 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 case BTPROTO_RFCOMM:
1075 {
1076 struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
1077 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
1078 PyObject *ret = NULL;
1079 if (addrobj) {
1080 ret = Py_BuildValue("Oi",
1081 addrobj,
1082 _BT_RC_MEMB(a, channel));
1083 Py_DECREF(addrobj);
1084 }
1085 return ret;
1086 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 case BTPROTO_HCI:
1089 {
1090 struct sockaddr_hci *a = (struct sockaddr_hci *) addr;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001091#if defined(__NetBSD__) || defined(__DragonFly__)
1092 return makebdaddr(&_BT_HCI_MEMB(a, bdaddr));
1093#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 PyObject *ret = NULL;
1095 ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
1096 return ret;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001097#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 }
Thomas Wouterscf297e42007-02-23 15:07:44 +00001099
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001100#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 case BTPROTO_SCO:
1102 {
1103 struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
1104 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
1105 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001106#endif
1107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 default:
1109 PyErr_SetString(PyExc_ValueError,
1110 "Unknown Bluetooth protocol");
1111 return NULL;
1112 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001113#endif
1114
Antoine Pitroub156a462010-10-27 20:13:57 +00001115#if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 case AF_PACKET:
1117 {
1118 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
1119 char *ifname = "";
1120 struct ifreq ifr;
1121 /* need to look up interface name give index */
1122 if (a->sll_ifindex) {
1123 ifr.ifr_ifindex = a->sll_ifindex;
1124 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
1125 ifname = ifr.ifr_name;
1126 }
1127 return Py_BuildValue("shbhy#",
1128 ifname,
1129 ntohs(a->sll_protocol),
1130 a->sll_pkttype,
1131 a->sll_hatype,
1132 a->sll_addr,
1133 a->sll_halen);
1134 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001135#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001136
Christian Heimes043d6f62008-01-07 17:19:16 +00001137#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 case AF_TIPC:
1139 {
1140 struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr;
1141 if (a->addrtype == TIPC_ADDR_NAMESEQ) {
1142 return Py_BuildValue("IIIII",
1143 a->addrtype,
1144 a->addr.nameseq.type,
1145 a->addr.nameseq.lower,
1146 a->addr.nameseq.upper,
1147 a->scope);
1148 } else if (a->addrtype == TIPC_ADDR_NAME) {
1149 return Py_BuildValue("IIIII",
1150 a->addrtype,
1151 a->addr.name.name.type,
1152 a->addr.name.name.instance,
1153 a->addr.name.name.instance,
1154 a->scope);
1155 } else if (a->addrtype == TIPC_ADDR_ID) {
1156 return Py_BuildValue("IIIII",
1157 a->addrtype,
1158 a->addr.id.node,
1159 a->addr.id.ref,
1160 0,
1161 a->scope);
1162 } else {
1163 PyErr_SetString(PyExc_ValueError,
1164 "Invalid address type");
1165 return NULL;
1166 }
1167 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001168#endif
1169
Charles-François Natali30589c92011-10-07 22:47:08 +02001170#ifdef AF_CAN
Charles-François Natali47413c12011-10-06 19:47:44 +02001171 case AF_CAN:
1172 {
1173 struct sockaddr_can *a = (struct sockaddr_can *)addr;
1174 char *ifname = "";
1175 struct ifreq ifr;
1176 /* need to look up interface name given index */
1177 if (a->can_ifindex) {
1178 ifr.ifr_ifindex = a->can_ifindex;
1179 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
1180 ifname = ifr.ifr_name;
1181 }
1182
1183 return Py_BuildValue("O&h", PyUnicode_DecodeFSDefault,
1184 ifname,
1185 a->can_family);
1186 }
1187#endif
1188
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001189#ifdef PF_SYSTEM
1190 case PF_SYSTEM:
1191 switch(proto) {
1192#ifdef SYSPROTO_CONTROL
1193 case SYSPROTO_CONTROL:
1194 {
1195 struct sockaddr_ctl *a = (struct sockaddr_ctl *)addr;
1196 return Py_BuildValue("(II)", a->sc_id, a->sc_unit);
1197 }
1198#endif
1199 default:
1200 PyErr_SetString(PyExc_ValueError,
1201 "Invalid address type");
1202 return 0;
1203 }
1204#endif
1205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 default:
1209 /* If we don't know the address family, don't raise an
1210 exception -- return it as an (int, bytes) tuple. */
1211 return Py_BuildValue("iy#",
1212 addr->sa_family,
1213 addr->sa_data,
1214 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001217}
1218
Guido van Rossum30a685f1991-06-27 15:51:29 +00001219
1220/* Parse a socket address argument according to the socket object's
1221 address family. Return 1 if the address was in the proper format,
1222 0 of not. The address is returned through addr_ret, its length
1223 through len_ret. */
1224
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001225static int
Guido van Rossum48a680c2001-03-02 06:34:14 +00001226getsockaddrarg(PySocketSockObject *s, PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 struct sockaddr *addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 switch (s->sock_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +00001230
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001231#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 case AF_UNIX:
1233 {
1234 struct sockaddr_un* addr;
1235 char *path;
1236 int len;
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001237 int retval = 0;
1238
1239 /* PEP 383. Not using PyUnicode_FSConverter since we need to
1240 allow embedded nulls on Linux. */
1241 if (PyUnicode_Check(args)) {
1242 if ((args = PyUnicode_EncodeFSDefault(args)) == NULL)
1243 return 0;
1244 }
1245 else
1246 Py_INCREF(args);
1247 if (!PyArg_Parse(args, "y#", &path, &len))
1248 goto unix_out;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 addr = (struct sockaddr_un*)addr_ret;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001251#ifdef linux
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 if (len > 0 && path[0] == 0) {
1253 /* Linux abstract namespace extension */
1254 if (len > sizeof addr->sun_path) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001255 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 "AF_UNIX path too long");
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001257 goto unix_out;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 }
1259 }
1260 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001261#endif /* linux */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 {
1263 /* regular NULL-terminated string */
1264 if (len >= sizeof addr->sun_path) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001265 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 "AF_UNIX path too long");
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001267 goto unix_out;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 }
1269 addr->sun_path[len] = 0;
1270 }
1271 addr->sun_family = s->sock_family;
1272 memcpy(addr->sun_path, path, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 *len_ret = len + offsetof(struct sockaddr_un, sun_path);
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001274 retval = 1;
1275 unix_out:
1276 Py_DECREF(args);
1277 return retval;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001279#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001280
Martin v. Löwis11017b12006-01-14 18:12:57 +00001281#if defined(AF_NETLINK)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 case AF_NETLINK:
1283 {
1284 struct sockaddr_nl* addr;
1285 int pid, groups;
1286 addr = (struct sockaddr_nl *)addr_ret;
1287 if (!PyTuple_Check(args)) {
1288 PyErr_Format(
1289 PyExc_TypeError,
1290 "getsockaddrarg: "
1291 "AF_NETLINK address must be tuple, not %.500s",
1292 Py_TYPE(args)->tp_name);
1293 return 0;
1294 }
1295 if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
1296 return 0;
1297 addr->nl_family = AF_NETLINK;
1298 addr->nl_pid = pid;
1299 addr->nl_groups = groups;
1300 *len_ret = sizeof(*addr);
1301 return 1;
1302 }
Martin v. Löwis11017b12006-01-14 18:12:57 +00001303#endif
1304
Charles-François Natali10b8cf42011-11-10 19:21:37 +01001305#ifdef AF_RDS
1306 case AF_RDS:
1307 /* RDS sockets use sockaddr_in: fall-through */
1308#endif
1309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 case AF_INET:
1311 {
1312 struct sockaddr_in* addr;
1313 char *host;
1314 int port, result;
1315 if (!PyTuple_Check(args)) {
1316 PyErr_Format(
1317 PyExc_TypeError,
1318 "getsockaddrarg: "
1319 "AF_INET address must be tuple, not %.500s",
1320 Py_TYPE(args)->tp_name);
1321 return 0;
1322 }
1323 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
1324 "idna", &host, &port))
1325 return 0;
1326 addr=(struct sockaddr_in*)addr_ret;
1327 result = setipaddr(host, (struct sockaddr *)addr,
1328 sizeof(*addr), AF_INET);
1329 PyMem_Free(host);
1330 if (result < 0)
1331 return 0;
1332 if (port < 0 || port > 0xffff) {
1333 PyErr_SetString(
1334 PyExc_OverflowError,
1335 "getsockaddrarg: port must be 0-65535.");
1336 return 0;
1337 }
1338 addr->sin_family = AF_INET;
1339 addr->sin_port = htons((short)port);
1340 *len_ret = sizeof *addr;
1341 return 1;
1342 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001343
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001344#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 case AF_INET6:
1346 {
1347 struct sockaddr_in6* addr;
1348 char *host;
Charles-François Natali366999a2012-01-02 15:47:29 +01001349 int port, result;
1350 unsigned int flowinfo, scope_id;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 flowinfo = scope_id = 0;
1352 if (!PyTuple_Check(args)) {
1353 PyErr_Format(
1354 PyExc_TypeError,
1355 "getsockaddrarg: "
1356 "AF_INET6 address must be tuple, not %.500s",
1357 Py_TYPE(args)->tp_name);
1358 return 0;
1359 }
Charles-François Natali366999a2012-01-02 15:47:29 +01001360 if (!PyArg_ParseTuple(args, "eti|II",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 "idna", &host, &port, &flowinfo,
1362 &scope_id)) {
1363 return 0;
1364 }
1365 addr = (struct sockaddr_in6*)addr_ret;
1366 result = setipaddr(host, (struct sockaddr *)addr,
1367 sizeof(*addr), AF_INET6);
1368 PyMem_Free(host);
1369 if (result < 0)
1370 return 0;
1371 if (port < 0 || port > 0xffff) {
1372 PyErr_SetString(
1373 PyExc_OverflowError,
1374 "getsockaddrarg: port must be 0-65535.");
1375 return 0;
1376 }
Charles-François Natali9b0c0062012-06-23 10:17:05 +02001377 if (flowinfo > 0xfffff) {
Charles-François Natali366999a2012-01-02 15:47:29 +01001378 PyErr_SetString(
1379 PyExc_OverflowError,
1380 "getsockaddrarg: flowinfo must be 0-1048575.");
1381 return 0;
1382 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 addr->sin6_family = s->sock_family;
1384 addr->sin6_port = htons((short)port);
Charles-François Natali366999a2012-01-02 15:47:29 +01001385 addr->sin6_flowinfo = htonl(flowinfo);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 addr->sin6_scope_id = scope_id;
1387 *len_ret = sizeof *addr;
1388 return 1;
1389 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001390#endif
1391
Hye-Shik Chang81268602004-02-02 06:05:24 +00001392#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 case AF_BLUETOOTH:
1394 {
1395 switch (s->sock_proto) {
1396 case BTPROTO_L2CAP:
1397 {
1398 struct sockaddr_l2 *addr;
1399 char *straddr;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 addr = (struct sockaddr_l2 *)addr_ret;
1402 memset(addr, 0, sizeof(struct sockaddr_l2));
1403 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
1404 if (!PyArg_ParseTuple(args, "si", &straddr,
1405 &_BT_L2_MEMB(addr, psm))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001406 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 "wrong format");
1408 return 0;
1409 }
1410 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
1411 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 *len_ret = sizeof *addr;
1414 return 1;
1415 }
1416 case BTPROTO_RFCOMM:
1417 {
1418 struct sockaddr_rc *addr;
1419 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 addr = (struct sockaddr_rc *)addr_ret;
1422 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
1423 if (!PyArg_ParseTuple(args, "si", &straddr,
1424 &_BT_RC_MEMB(addr, channel))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001425 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 "wrong format");
1427 return 0;
1428 }
1429 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
1430 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 *len_ret = sizeof *addr;
1433 return 1;
1434 }
1435 case BTPROTO_HCI:
1436 {
1437 struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001438#if defined(__NetBSD__) || defined(__DragonFly__)
Alexander Belopolskye239d232010-12-08 23:31:48 +00001439 char *straddr = PyBytes_AS_STRING(args);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001440
Alexander Belopolskye239d232010-12-08 23:31:48 +00001441 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001442 if (straddr == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001443 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001444 "wrong format");
1445 return 0;
1446 }
1447 if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0)
1448 return 0;
1449#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
1451 if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001452 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 "wrong format");
1454 return 0;
1455 }
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001456#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 *len_ret = sizeof *addr;
1458 return 1;
1459 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001460#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 case BTPROTO_SCO:
1462 {
1463 struct sockaddr_sco *addr;
1464 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 addr = (struct sockaddr_sco *)addr_ret;
1467 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
1468 if (!PyBytes_Check(args)) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001469 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 "wrong format");
1471 return 0;
1472 }
1473 straddr = PyBytes_AS_STRING(args);
1474 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
1475 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 *len_ret = sizeof *addr;
1478 return 1;
1479 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001480#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001482 PyErr_SetString(PyExc_OSError, "getsockaddrarg: unknown Bluetooth protocol");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 return 0;
1484 }
1485 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001486#endif
1487
Antoine Pitroub156a462010-10-27 20:13:57 +00001488#if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 case AF_PACKET:
1490 {
1491 struct sockaddr_ll* addr;
1492 struct ifreq ifr;
1493 char *interfaceName;
1494 int protoNumber;
1495 int hatype = 0;
1496 int pkttype = 0;
1497 char *haddr = NULL;
1498 unsigned int halen = 0;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 if (!PyTuple_Check(args)) {
1501 PyErr_Format(
1502 PyExc_TypeError,
1503 "getsockaddrarg: "
1504 "AF_PACKET address must be tuple, not %.500s",
1505 Py_TYPE(args)->tp_name);
1506 return 0;
1507 }
1508 if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName,
1509 &protoNumber, &pkttype, &hatype,
1510 &haddr, &halen))
1511 return 0;
1512 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1513 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
1514 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
1515 s->errorhandler();
1516 return 0;
1517 }
1518 if (halen > 8) {
1519 PyErr_SetString(PyExc_ValueError,
1520 "Hardware address must be 8 bytes or less");
1521 return 0;
1522 }
1523 if (protoNumber < 0 || protoNumber > 0xffff) {
1524 PyErr_SetString(
1525 PyExc_OverflowError,
1526 "getsockaddrarg: protoNumber must be 0-65535.");
1527 return 0;
1528 }
1529 addr = (struct sockaddr_ll*)addr_ret;
1530 addr->sll_family = AF_PACKET;
1531 addr->sll_protocol = htons((short)protoNumber);
1532 addr->sll_ifindex = ifr.ifr_ifindex;
1533 addr->sll_pkttype = pkttype;
1534 addr->sll_hatype = hatype;
1535 if (halen != 0) {
1536 memcpy(&addr->sll_addr, haddr, halen);
1537 }
1538 addr->sll_halen = halen;
1539 *len_ret = sizeof *addr;
1540 return 1;
1541 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001542#endif
1543
Christian Heimes043d6f62008-01-07 17:19:16 +00001544#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 case AF_TIPC:
1546 {
1547 unsigned int atype, v1, v2, v3;
1548 unsigned int scope = TIPC_CLUSTER_SCOPE;
1549 struct sockaddr_tipc *addr;
Christian Heimes043d6f62008-01-07 17:19:16 +00001550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 if (!PyTuple_Check(args)) {
1552 PyErr_Format(
1553 PyExc_TypeError,
1554 "getsockaddrarg: "
1555 "AF_TIPC address must be tuple, not %.500s",
1556 Py_TYPE(args)->tp_name);
1557 return 0;
1558 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 if (!PyArg_ParseTuple(args,
1561 "IIII|I;Invalid TIPC address format",
1562 &atype, &v1, &v2, &v3, &scope))
1563 return 0;
Christian Heimes043d6f62008-01-07 17:19:16 +00001564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 addr = (struct sockaddr_tipc *) addr_ret;
1566 memset(addr, 0, sizeof(struct sockaddr_tipc));
Christian Heimes043d6f62008-01-07 17:19:16 +00001567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 addr->family = AF_TIPC;
1569 addr->scope = scope;
1570 addr->addrtype = atype;
Christian Heimes043d6f62008-01-07 17:19:16 +00001571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 if (atype == TIPC_ADDR_NAMESEQ) {
1573 addr->addr.nameseq.type = v1;
1574 addr->addr.nameseq.lower = v2;
1575 addr->addr.nameseq.upper = v3;
1576 } else if (atype == TIPC_ADDR_NAME) {
1577 addr->addr.name.name.type = v1;
1578 addr->addr.name.name.instance = v2;
1579 } else if (atype == TIPC_ADDR_ID) {
1580 addr->addr.id.node = v1;
1581 addr->addr.id.ref = v2;
1582 } else {
1583 /* Shouldn't happen */
1584 PyErr_SetString(PyExc_TypeError, "Invalid address type");
1585 return 0;
1586 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 *len_ret = sizeof(*addr);
Christian Heimes043d6f62008-01-07 17:19:16 +00001589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 return 1;
1591 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001592#endif
1593
Charles-François Natali30589c92011-10-07 22:47:08 +02001594#ifdef AF_CAN
Charles-François Natali47413c12011-10-06 19:47:44 +02001595 case AF_CAN:
1596 switch (s->sock_proto) {
1597 case CAN_RAW:
Charles-François Natali773e42d2013-02-05 19:42:01 +01001598 /* fall-through */
1599 case CAN_BCM:
Charles-François Natali47413c12011-10-06 19:47:44 +02001600 {
1601 struct sockaddr_can *addr;
1602 PyObject *interfaceName;
1603 struct ifreq ifr;
Charles-François Natali47413c12011-10-06 19:47:44 +02001604 Py_ssize_t len;
1605
Benjamin Peterson18b71912013-05-16 15:29:44 -05001606 addr = (struct sockaddr_can *)addr_ret;
1607
Charles-François Natali47413c12011-10-06 19:47:44 +02001608 if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter,
1609 &interfaceName))
1610 return 0;
1611
1612 len = PyBytes_GET_SIZE(interfaceName);
1613
1614 if (len == 0) {
1615 ifr.ifr_ifindex = 0;
1616 } else if (len < sizeof(ifr.ifr_name)) {
Christian Heimes15b68852012-09-10 01:25:50 +02001617 strncpy(ifr.ifr_name, PyBytes_AS_STRING(interfaceName), sizeof(ifr.ifr_name));
1618 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Charles-François Natali47413c12011-10-06 19:47:44 +02001619 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
1620 s->errorhandler();
1621 Py_DECREF(interfaceName);
1622 return 0;
1623 }
1624 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001625 PyErr_SetString(PyExc_OSError,
Charles-François Natali47413c12011-10-06 19:47:44 +02001626 "AF_CAN interface name too long");
1627 Py_DECREF(interfaceName);
1628 return 0;
1629 }
1630
1631 addr->can_family = AF_CAN;
1632 addr->can_ifindex = ifr.ifr_ifindex;
1633
1634 *len_ret = sizeof(*addr);
1635 Py_DECREF(interfaceName);
1636 return 1;
1637 }
1638 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001639 PyErr_SetString(PyExc_OSError,
Charles-François Natali47413c12011-10-06 19:47:44 +02001640 "getsockaddrarg: unsupported CAN protocol");
1641 return 0;
1642 }
1643#endif
Victor Stinnera534fc42013-06-03 22:07:27 +02001644
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001645#ifdef PF_SYSTEM
1646 case PF_SYSTEM:
1647 switch (s->sock_proto) {
1648#ifdef SYSPROTO_CONTROL
1649 case SYSPROTO_CONTROL:
1650 {
1651 struct sockaddr_ctl *addr;
Victor Stinnera534fc42013-06-03 22:07:27 +02001652
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001653 addr = (struct sockaddr_ctl *)addr_ret;
1654 addr->sc_family = AF_SYSTEM;
Victor Stinnera534fc42013-06-03 22:07:27 +02001655 addr->ss_sysaddr = AF_SYS_CONTROL;
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001656
1657 if (PyUnicode_Check(args)) {
1658 struct ctl_info info;
1659 PyObject *ctl_name;
1660
1661 if (!PyArg_Parse(args, "O&",
1662 PyUnicode_FSConverter, &ctl_name)) {
1663 return 0;
1664 }
1665
1666 if (PyBytes_GET_SIZE(ctl_name) > sizeof(info.ctl_name)) {
1667 PyErr_SetString(PyExc_ValueError,
1668 "provided string is too long");
1669 Py_DECREF(ctl_name);
1670 return 0;
1671 }
1672 strncpy(info.ctl_name, PyBytes_AS_STRING(ctl_name),
1673 sizeof(info.ctl_name));
1674 Py_DECREF(ctl_name);
1675
1676 if (ioctl(s->sock_fd, CTLIOCGINFO, &info)) {
1677 PyErr_SetString(PyExc_OSError,
1678 "cannot find kernel control with provided name");
1679 return 0;
1680 }
Victor Stinnera534fc42013-06-03 22:07:27 +02001681
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001682 addr->sc_id = info.ctl_id;
1683 addr->sc_unit = 0;
1684 } else if (!PyArg_ParseTuple(args, "II",
1685 &(addr->sc_id), &(addr->sc_unit))) {
1686 PyErr_SetString(PyExc_TypeError, "getsockaddrarg: "
1687 "expected str or tuple of two ints");
Victor Stinnera534fc42013-06-03 22:07:27 +02001688
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001689 return 0;
1690 }
Victor Stinnera534fc42013-06-03 22:07:27 +02001691
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001692 *len_ret = sizeof(*addr);
1693 return 1;
1694 }
1695#endif
1696 default:
1697 PyErr_SetString(PyExc_OSError,
1698 "getsockaddrarg: unsupported PF_SYSTEM protocol");
1699 return 0;
1700 }
1701#endif
Charles-François Natali47413c12011-10-06 19:47:44 +02001702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001706 PyErr_SetString(PyExc_OSError, "getsockaddrarg: bad family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 return 0;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001710}
1711
Guido van Rossum30a685f1991-06-27 15:51:29 +00001712
Guido van Rossum48a680c2001-03-02 06:34:14 +00001713/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00001714 Return 1 if the family is known, 0 otherwise. The length is returned
1715 through len_ret. */
1716
1717static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00001718getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 switch (s->sock_family) {
Guido van Rossum710e1df1992-06-12 10:39:36 +00001721
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001722#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 case AF_UNIX:
1724 {
1725 *len_ret = sizeof (struct sockaddr_un);
1726 return 1;
1727 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001728#endif /* AF_UNIX */
Martin v. Löwis11017b12006-01-14 18:12:57 +00001729#if defined(AF_NETLINK)
1730 case AF_NETLINK:
1731 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 *len_ret = sizeof (struct sockaddr_nl);
1733 return 1;
Martin v. Löwis11017b12006-01-14 18:12:57 +00001734 }
1735#endif
Guido van Rossum710e1df1992-06-12 10:39:36 +00001736
Charles-François Natali10b8cf42011-11-10 19:21:37 +01001737#ifdef AF_RDS
1738 case AF_RDS:
1739 /* RDS sockets use sockaddr_in: fall-through */
1740#endif
1741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 case AF_INET:
1743 {
1744 *len_ret = sizeof (struct sockaddr_in);
1745 return 1;
1746 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00001747
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001748#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 case AF_INET6:
1750 {
1751 *len_ret = sizeof (struct sockaddr_in6);
1752 return 1;
1753 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001754#endif
1755
Hye-Shik Chang81268602004-02-02 06:05:24 +00001756#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 case AF_BLUETOOTH:
1758 {
1759 switch(s->sock_proto)
1760 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 case BTPROTO_L2CAP:
1763 *len_ret = sizeof (struct sockaddr_l2);
1764 return 1;
1765 case BTPROTO_RFCOMM:
1766 *len_ret = sizeof (struct sockaddr_rc);
1767 return 1;
1768 case BTPROTO_HCI:
1769 *len_ret = sizeof (struct sockaddr_hci);
1770 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001771#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 case BTPROTO_SCO:
1773 *len_ret = sizeof (struct sockaddr_sco);
1774 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001775#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001777 PyErr_SetString(PyExc_OSError, "getsockaddrlen: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 "unknown BT protocol");
1779 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 }
1782 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001783#endif
1784
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001785#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 case AF_PACKET:
1787 {
1788 *len_ret = sizeof (struct sockaddr_ll);
1789 return 1;
1790 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001791#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001792
Christian Heimes043d6f62008-01-07 17:19:16 +00001793#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 case AF_TIPC:
1795 {
1796 *len_ret = sizeof (struct sockaddr_tipc);
1797 return 1;
1798 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001799#endif
1800
Charles-François Natali30589c92011-10-07 22:47:08 +02001801#ifdef AF_CAN
Charles-François Natali47413c12011-10-06 19:47:44 +02001802 case AF_CAN:
1803 {
1804 *len_ret = sizeof (struct sockaddr_can);
1805 return 1;
1806 }
1807#endif
Victor Stinnera534fc42013-06-03 22:07:27 +02001808
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001809#ifdef PF_SYSTEM
1810 case PF_SYSTEM:
1811 switch(s->sock_proto) {
1812#ifdef SYSPROTO_CONTROL
1813 case SYSPROTO_CONTROL:
1814 *len_ret = sizeof (struct sockaddr_ctl);
1815 return 1;
1816#endif
1817 default:
1818 PyErr_SetString(PyExc_OSError, "getsockaddrlen: "
1819 "unknown PF_SYSTEM protocol");
1820 return 0;
1821 }
1822#endif
Charles-François Natali47413c12011-10-06 19:47:44 +02001823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 /* More cases here... */
Guido van Rossum710e1df1992-06-12 10:39:36 +00001825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001827 PyErr_SetString(PyExc_OSError, "getsockaddrlen: bad family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 return 0;
Guido van Rossum710e1df1992-06-12 10:39:36 +00001829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00001831}
1832
1833
Nick Coghlan96fe56a2011-08-22 11:55:57 +10001834/* Support functions for the sendmsg() and recvmsg[_into]() methods.
1835 Currently, these methods are only compiled if the RFC 2292/3542
1836 CMSG_LEN() macro is available. Older systems seem to have used
1837 sizeof(struct cmsghdr) + (length) where CMSG_LEN() is used now, so
1838 it may be possible to define CMSG_LEN() that way if it's not
1839 provided. Some architectures might need extra padding after the
1840 cmsghdr, however, and CMSG_LEN() would have to take account of
1841 this. */
1842#ifdef CMSG_LEN
1843/* If length is in range, set *result to CMSG_LEN(length) and return
1844 true; otherwise, return false. */
1845static int
1846get_CMSG_LEN(size_t length, size_t *result)
1847{
1848 size_t tmp;
1849
1850 if (length > (SOCKLEN_T_LIMIT - CMSG_LEN(0)))
1851 return 0;
1852 tmp = CMSG_LEN(length);
1853 if (tmp > SOCKLEN_T_LIMIT || tmp < length)
1854 return 0;
1855 *result = tmp;
1856 return 1;
1857}
1858
1859#ifdef CMSG_SPACE
1860/* If length is in range, set *result to CMSG_SPACE(length) and return
1861 true; otherwise, return false. */
1862static int
1863get_CMSG_SPACE(size_t length, size_t *result)
1864{
1865 size_t tmp;
1866
1867 /* Use CMSG_SPACE(1) here in order to take account of the padding
1868 necessary before *and* after the data. */
1869 if (length > (SOCKLEN_T_LIMIT - CMSG_SPACE(1)))
1870 return 0;
1871 tmp = CMSG_SPACE(length);
1872 if (tmp > SOCKLEN_T_LIMIT || tmp < length)
1873 return 0;
1874 *result = tmp;
1875 return 1;
1876}
1877#endif
1878
1879/* Return true iff msg->msg_controllen is valid, cmsgh is a valid
1880 pointer in msg->msg_control with at least "space" bytes after it,
1881 and its cmsg_len member inside the buffer. */
1882static int
1883cmsg_min_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t space)
1884{
1885 size_t cmsg_offset;
1886 static const size_t cmsg_len_end = (offsetof(struct cmsghdr, cmsg_len) +
1887 sizeof(cmsgh->cmsg_len));
1888
Charles-François Natali466517d2011-08-28 18:23:43 +02001889 /* Note that POSIX allows msg_controllen to be of signed type. */
1890 if (cmsgh == NULL || msg->msg_control == NULL || msg->msg_controllen < 0)
Nick Coghlan96fe56a2011-08-22 11:55:57 +10001891 return 0;
1892 if (space < cmsg_len_end)
1893 space = cmsg_len_end;
1894 cmsg_offset = (char *)cmsgh - (char *)msg->msg_control;
1895 return (cmsg_offset <= (size_t)-1 - space &&
1896 cmsg_offset + space <= msg->msg_controllen);
1897}
1898
1899/* If pointer CMSG_DATA(cmsgh) is in buffer msg->msg_control, set
1900 *space to number of bytes following it in the buffer and return
1901 true; otherwise, return false. Assumes cmsgh, msg->msg_control and
1902 msg->msg_controllen are valid. */
1903static int
1904get_cmsg_data_space(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *space)
1905{
1906 size_t data_offset;
1907 char *data_ptr;
1908
1909 if ((data_ptr = (char *)CMSG_DATA(cmsgh)) == NULL)
1910 return 0;
1911 data_offset = data_ptr - (char *)msg->msg_control;
1912 if (data_offset > msg->msg_controllen)
1913 return 0;
1914 *space = msg->msg_controllen - data_offset;
1915 return 1;
1916}
1917
1918/* If cmsgh is invalid or not contained in the buffer pointed to by
1919 msg->msg_control, return -1. If cmsgh is valid and its associated
1920 data is entirely contained in the buffer, set *data_len to the
1921 length of the associated data and return 0. If only part of the
1922 associated data is contained in the buffer but cmsgh is otherwise
1923 valid, set *data_len to the length contained in the buffer and
1924 return 1. */
1925static int
1926get_cmsg_data_len(struct msghdr *msg, struct cmsghdr *cmsgh, size_t *data_len)
1927{
1928 size_t space, cmsg_data_len;
1929
1930 if (!cmsg_min_space(msg, cmsgh, CMSG_LEN(0)) ||
1931 cmsgh->cmsg_len < CMSG_LEN(0))
1932 return -1;
1933 cmsg_data_len = cmsgh->cmsg_len - CMSG_LEN(0);
1934 if (!get_cmsg_data_space(msg, cmsgh, &space))
1935 return -1;
1936 if (space >= cmsg_data_len) {
1937 *data_len = cmsg_data_len;
1938 return 0;
1939 }
1940 *data_len = space;
1941 return 1;
1942}
1943#endif /* CMSG_LEN */
1944
1945
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001946/* s._accept() -> (fd, address) */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001947
Guido van Rossum73624e91994-10-10 17:59:00 +00001948static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001949sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 sock_addr_t addrbuf;
1952 SOCKET_T newfd = INVALID_SOCKET;
1953 socklen_t addrlen;
1954 PyObject *sock = NULL;
1955 PyObject *addr = NULL;
1956 PyObject *res = NULL;
1957 int timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 if (!getsockaddrlen(s, &addrlen))
1959 return NULL;
1960 memset(&addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 if (!IS_SELECTABLE(s))
1963 return select_error();
Neal Norwitz082b2df2006-02-07 07:04:46 +00001964
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00001965 BEGIN_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00001967 timeout = internal_select_ex(s, 0, interval);
Antoine Pitroub1c54962010-10-14 15:05:38 +00001968 if (!timeout) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Antoine Pitroub1c54962010-10-14 15:05:38 +00001970 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 if (timeout == 1) {
1974 PyErr_SetString(socket_timeout, "timed out");
1975 return NULL;
1976 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00001977 END_SELECT_LOOP(s)
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 if (newfd == INVALID_SOCKET)
1980 return s->errorhandler();
Barry Warsaw752300b1997-01-03 17:18:10 +00001981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 sock = PyLong_FromSocket_t(newfd);
1983 if (sock == NULL) {
1984 SOCKETCLOSE(newfd);
1985 goto finally;
1986 }
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
1989 addrlen, s->sock_proto);
1990 if (addr == NULL)
1991 goto finally;
Barry Warsaw752300b1997-01-03 17:18:10 +00001992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 res = PyTuple_Pack(2, sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001994
Guido van Rossum67f7a382002-06-06 21:08:16 +00001995finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 Py_XDECREF(sock);
1997 Py_XDECREF(addr);
1998 return res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001999}
2000
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002001PyDoc_STRVAR(accept_doc,
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002002"_accept() -> (integer, address info)\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002003\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002004Wait for an incoming connection. Return a new socket file descriptor\n\
2005representing the connection, and the address of the client.\n\
2006For IP sockets, the address info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002007
Guido van Rossum11ba0942002-06-13 15:07:44 +00002008/* s.setblocking(flag) method. Argument:
2009 False -- non-blocking mode; same as settimeout(0)
2010 True -- blocking mode; same as settimeout(None)
2011*/
Guido van Rossume4485b01994-09-07 14:32:49 +00002012
Guido van Rossum73624e91994-10-10 17:59:00 +00002013static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002014sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00002015{
Serhiy Storchaka78980432013-01-15 01:12:17 +02002016 long block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 block = PyLong_AsLong(arg);
2019 if (block == -1 && PyErr_Occurred())
2020 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 s->sock_timeout = block ? -1.0 : 0.0;
2023 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00002024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 Py_INCREF(Py_None);
2026 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00002027}
Guido van Rossume4485b01994-09-07 14:32:49 +00002028
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002029PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002030"setblocking(flag)\n\
2031\n\
2032Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00002033setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002034setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002035
Guido van Rossum11ba0942002-06-13 15:07:44 +00002036/* s.settimeout(timeout) method. Argument:
2037 None -- no timeout, blocking mode; same as setblocking(True)
2038 0.0 -- non-blocking mode; same as setblocking(False)
2039 > 0 -- timeout mode; operations time out after timeout seconds
2040 < 0 -- illegal; raises an exception
2041*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00002042static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002043sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00002044{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 double timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 if (arg == Py_None)
2048 timeout = -1.0;
2049 else {
2050 timeout = PyFloat_AsDouble(arg);
2051 if (timeout < 0.0) {
2052 if (!PyErr_Occurred())
2053 PyErr_SetString(PyExc_ValueError,
2054 "Timeout value out of range");
2055 return NULL;
2056 }
2057 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 s->sock_timeout = timeout;
2060 internal_setblocking(s, timeout < 0.0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 Py_INCREF(Py_None);
2063 return Py_None;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002064}
2065
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002066PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002067"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00002068\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00002069Set a timeout on socket operations. 'timeout' can be a float,\n\
2070giving in seconds, or None. Setting a timeout of None disables\n\
2071the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002072Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00002073
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002074/* s.gettimeout() method.
2075 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00002076static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002077sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00002078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 if (s->sock_timeout < 0.0) {
2080 Py_INCREF(Py_None);
2081 return Py_None;
2082 }
2083 else
2084 return PyFloat_FromDouble(s->sock_timeout);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002085}
2086
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002087PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002088"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00002089\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03002090Returns the timeout in seconds (float) associated with socket \n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00002091operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002092operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00002093
Guido van Rossumaee08791992-09-08 09:05:33 +00002094/* s.setsockopt() method.
2095 With an integer third argument, sets an integer option.
2096 With a string third argument, sets an option from a buffer;
2097 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002098
Guido van Rossum73624e91994-10-10 17:59:00 +00002099static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002100sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 int level;
2103 int optname;
2104 int res;
2105 char *buf;
2106 int buflen;
2107 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 if (PyArg_ParseTuple(args, "iii:setsockopt",
2110 &level, &optname, &flag)) {
2111 buf = (char *) &flag;
2112 buflen = sizeof flag;
2113 }
2114 else {
2115 PyErr_Clear();
2116 if (!PyArg_ParseTuple(args, "iiy#:setsockopt",
2117 &level, &optname, &buf, &buflen))
2118 return NULL;
2119 }
2120 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
2121 if (res < 0)
2122 return s->errorhandler();
2123 Py_INCREF(Py_None);
2124 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002125}
2126
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002127PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002128"setsockopt(level, option, value)\n\
2129\n\
2130Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002131The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002132
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002133
Guido van Rossumaee08791992-09-08 09:05:33 +00002134/* s.getsockopt() method.
2135 With two arguments, retrieves an integer option.
2136 With a third integer argument, retrieves a string buffer of that size;
2137 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002138
Guido van Rossum73624e91994-10-10 17:59:00 +00002139static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002140sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 int level;
2143 int optname;
2144 int res;
2145 PyObject *buf;
2146 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
2149 &level, &optname, &buflen))
2150 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 if (buflen == 0) {
2153 int flag = 0;
2154 socklen_t flagsize = sizeof flag;
2155 res = getsockopt(s->sock_fd, level, optname,
2156 (void *)&flag, &flagsize);
2157 if (res < 0)
2158 return s->errorhandler();
2159 return PyLong_FromLong(flag);
2160 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002161#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 /* socklen_t is unsigned so no negative test is needed,
2163 test buflen == 0 is previously done */
2164 if (buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002165#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 if (buflen <= 0 || buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002167#endif
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002168 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 "getsockopt buflen out of range");
2170 return NULL;
2171 }
2172 buf = PyBytes_FromStringAndSize((char *)NULL, buflen);
2173 if (buf == NULL)
2174 return NULL;
2175 res = getsockopt(s->sock_fd, level, optname,
2176 (void *)PyBytes_AS_STRING(buf), &buflen);
2177 if (res < 0) {
2178 Py_DECREF(buf);
2179 return s->errorhandler();
2180 }
2181 _PyBytes_Resize(&buf, buflen);
2182 return buf;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002183}
2184
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002185PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002186"getsockopt(level, option[, buffersize]) -> value\n\
2187\n\
2188Get a socket option. See the Unix manual for level and option.\n\
2189If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002190string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002191
Guido van Rossum0e69587d1992-06-05 15:11:30 +00002192
Fred Drake728819a2000-07-01 03:40:12 +00002193/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002194
Guido van Rossum73624e91994-10-10 17:59:00 +00002195static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002196sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 sock_addr_t addrbuf;
2199 int addrlen;
2200 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2203 return NULL;
2204 Py_BEGIN_ALLOW_THREADS
2205 res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);
2206 Py_END_ALLOW_THREADS
2207 if (res < 0)
2208 return s->errorhandler();
2209 Py_INCREF(Py_None);
2210 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002211}
2212
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002213PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002214"bind(address)\n\
2215\n\
2216Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00002217pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002218sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002219
Guido van Rossum30a685f1991-06-27 15:51:29 +00002220
2221/* s.close() method.
2222 Set the file descriptor to -1 so operations tried subsequently
2223 will surely fail. */
2224
Guido van Rossum73624e91994-10-10 17:59:00 +00002225static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002226sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 if ((fd = s->sock_fd) != -1) {
2231 s->sock_fd = -1;
2232 Py_BEGIN_ALLOW_THREADS
2233 (void) SOCKETCLOSE(fd);
2234 Py_END_ALLOW_THREADS
2235 }
2236 Py_INCREF(Py_None);
2237 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002238}
2239
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002240PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002241"close()\n\
2242\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002243Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002244
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002245static PyObject *
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002246sock_detach(PySocketSockObject *s)
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002247{
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002248 SOCKET_T fd = s->sock_fd;
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002249 s->sock_fd = -1;
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002250 return PyLong_FromSocket_t(fd);
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002251}
2252
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002253PyDoc_STRVAR(detach_doc,
2254"detach()\n\
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002255\n\
2256Close the socket object without closing the underlying file descriptor.\
2257The object cannot be used after this call, but the file descriptor\
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002258can be reused for other purposes. The file descriptor is returned.");
Antoine Pitroue43f9d02010-08-08 23:24:50 +00002259
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002260static int
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002261internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 int *timeoutp)
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 int res, timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 timeout = 0;
2267 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002268
2269#ifdef MS_WINDOWS
2270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 if (s->sock_timeout > 0.0) {
2272 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK &&
2273 IS_SELECTABLE(s)) {
2274 /* This is a mess. Best solution: trust select */
2275 fd_set fds;
2276 fd_set fds_exc;
2277 struct timeval tv;
2278 tv.tv_sec = (int)s->sock_timeout;
2279 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
2280 FD_ZERO(&fds);
2281 FD_SET(s->sock_fd, &fds);
2282 FD_ZERO(&fds_exc);
2283 FD_SET(s->sock_fd, &fds_exc);
Antoine Pitrou19467d22010-08-17 19:33:30 +00002284 res = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
2285 NULL, &fds, &fds_exc, &tv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 if (res == 0) {
2287 res = WSAEWOULDBLOCK;
2288 timeout = 1;
2289 } else if (res > 0) {
2290 if (FD_ISSET(s->sock_fd, &fds))
2291 /* The socket is in the writable set - this
2292 means connected */
2293 res = 0;
2294 else {
2295 /* As per MS docs, we need to call getsockopt()
2296 to get the underlying error */
2297 int res_size = sizeof res;
2298 /* It must be in the exception set */
2299 assert(FD_ISSET(s->sock_fd, &fds_exc));
2300 if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
2301 (char *)&res, &res_size))
2302 /* getsockopt also clears WSAGetLastError,
2303 so reset it back. */
2304 WSASetLastError(res);
2305 else
2306 res = WSAGetLastError();
2307 }
2308 }
2309 /* else if (res < 0) an error occurred */
2310 }
2311 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 if (res < 0)
2314 res = WSAGetLastError();
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002315
2316#else
2317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 if (s->sock_timeout > 0.0) {
2319 if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) {
2320 timeout = internal_select(s, 1);
2321 if (timeout == 0) {
2322 /* Bug #1019808: in case of an EINPROGRESS,
2323 use getsockopt(SO_ERROR) to get the real
2324 error. */
2325 socklen_t res_size = sizeof res;
2326 (void)getsockopt(s->sock_fd, SOL_SOCKET,
2327 SO_ERROR, &res, &res_size);
2328 if (res == EISCONN)
2329 res = 0;
2330 errno = res;
2331 }
2332 else if (timeout == -1) {
2333 res = errno; /* had error */
2334 }
2335 else
2336 res = EWOULDBLOCK; /* timed out */
2337 }
2338 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 if (res < 0)
2341 res = errno;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002342
2343#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 *timeoutp = timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 return res;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002347}
Guido van Rossum30a685f1991-06-27 15:51:29 +00002348
Fred Drake728819a2000-07-01 03:40:12 +00002349/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002350
Guido van Rossum73624e91994-10-10 17:59:00 +00002351static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002352sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 sock_addr_t addrbuf;
2355 int addrlen;
2356 int res;
2357 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2360 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 Py_BEGIN_ALLOW_THREADS
2363 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
2364 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 if (timeout == 1) {
2367 PyErr_SetString(socket_timeout, "timed out");
2368 return NULL;
2369 }
2370 if (res != 0)
2371 return s->errorhandler();
2372 Py_INCREF(Py_None);
2373 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002374}
2375
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002376PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002377"connect(address)\n\
2378\n\
2379Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002380is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002381
Guido van Rossum30a685f1991-06-27 15:51:29 +00002382
Fred Drake728819a2000-07-01 03:40:12 +00002383/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002384
2385static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002386sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 sock_addr_t addrbuf;
2389 int addrlen;
2390 int res;
2391 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2394 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 Py_BEGIN_ALLOW_THREADS
2397 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
2398 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 /* Signals are not errors (though they may raise exceptions). Adapted
2401 from PyErr_SetFromErrnoWithFilenameObject(). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002402#ifdef EINTR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 if (res == EINTR && PyErr_CheckSignals())
2404 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002405#endif
2406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 return PyLong_FromLong((long) res);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002408}
2409
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002410PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002411"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002412\n\
2413This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002414instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002415
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002416
Guido van Rossumed233a51992-06-23 09:07:03 +00002417/* s.fileno() method */
2418
Guido van Rossum73624e91994-10-10 17:59:00 +00002419static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002420sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00002421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 return PyLong_FromSocket_t(s->sock_fd);
Guido van Rossumed233a51992-06-23 09:07:03 +00002423}
2424
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002425PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002426"fileno() -> integer\n\
2427\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002428Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002429
Guido van Rossumed233a51992-06-23 09:07:03 +00002430
Guido van Rossumc89705d1992-11-26 08:54:07 +00002431/* s.getsockname() method */
2432
Guido van Rossum73624e91994-10-10 17:59:00 +00002433static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002434sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 sock_addr_t addrbuf;
2437 int res;
2438 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 if (!getsockaddrlen(s, &addrlen))
2441 return NULL;
2442 memset(&addrbuf, 0, addrlen);
2443 Py_BEGIN_ALLOW_THREADS
2444 res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2445 Py_END_ALLOW_THREADS
2446 if (res < 0)
2447 return s->errorhandler();
2448 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2449 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002450}
2451
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002452PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002453"getsockname() -> address info\n\
2454\n\
2455Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002456info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002457
Guido van Rossumc89705d1992-11-26 08:54:07 +00002458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002460/* s.getpeername() method */
2461
Guido van Rossum73624e91994-10-10 17:59:00 +00002462static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002463sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002464{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 sock_addr_t addrbuf;
2466 int res;
2467 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 if (!getsockaddrlen(s, &addrlen))
2470 return NULL;
2471 memset(&addrbuf, 0, addrlen);
2472 Py_BEGIN_ALLOW_THREADS
2473 res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2474 Py_END_ALLOW_THREADS
2475 if (res < 0)
2476 return s->errorhandler();
2477 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2478 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002479}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002480
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002481PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002482"getpeername() -> address info\n\
2483\n\
2484Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002485info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002486
Guido van Rossumb6775db1994-08-01 11:34:53 +00002487#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002488
2489
Guido van Rossum30a685f1991-06-27 15:51:29 +00002490/* s.listen(n) method */
2491
Guido van Rossum73624e91994-10-10 17:59:00 +00002492static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002493sock_listen(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002495 int backlog;
2496 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002497
Serhiy Storchaka78980432013-01-15 01:12:17 +02002498 backlog = _PyLong_AsInt(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 if (backlog == -1 && PyErr_Occurred())
2500 return NULL;
2501 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou1be815a2011-05-10 19:16:29 +02002502 /* To avoid problems on systems that don't allow a negative backlog
2503 * (which doesn't make sense anyway) we force a minimum value of 0. */
2504 if (backlog < 0)
2505 backlog = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 res = listen(s->sock_fd, backlog);
2507 Py_END_ALLOW_THREADS
2508 if (res < 0)
2509 return s->errorhandler();
2510 Py_INCREF(Py_None);
2511 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002512}
2513
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002514PyDoc_STRVAR(listen_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002515"listen(backlog)\n\
2516\n\
2517Enable a server to accept connections. The backlog argument must be at\n\
Antoine Pitrou1be815a2011-05-10 19:16:29 +02002518least 0 (if it is lower, it is set to 0); it specifies the number of\n\
2519unaccepted connections that the system will allow before refusing new\n\
2520connections.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002521
2522
Thomas Wouters477c8d52006-05-27 19:21:47 +00002523/*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002524 * This is the guts of the recv() and recv_into() methods, which reads into a
Thomas Wouters902d6eb2007-01-09 23:18:33 +00002525 * char buffer. If you have any inc/dec ref to do to the objects that contain
Thomas Wouters477c8d52006-05-27 19:21:47 +00002526 * the buffer, do it in the caller. This function returns the number of bytes
Ezio Melotti13925002011-03-16 11:05:33 +02002527 * successfully read. If there was an error, it returns -1. Note that it is
Thomas Wouters477c8d52006-05-27 19:21:47 +00002528 * also possible that we return a number of bytes smaller than the request
2529 * bytes.
2530 */
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002531
Antoine Pitrou19467d22010-08-17 19:33:30 +00002532static Py_ssize_t
2533sock_recv_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002534{
Antoine Pitrou19467d22010-08-17 19:33:30 +00002535 Py_ssize_t outlen = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002536 int timeout;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002537#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 int remaining;
2539 char *read_buf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002540#endif
2541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002542 if (!IS_SELECTABLE(s)) {
2543 select_error();
2544 return -1;
2545 }
2546 if (len == 0) {
2547 /* If 0 bytes were requested, do nothing. */
2548 return 0;
2549 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002550
2551#ifndef __VMS
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002552 BEGIN_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002553 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002554 timeout = internal_select_ex(s, 0, interval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 if (!timeout)
2556 outlen = recv(s->sock_fd, cbuf, len, flags);
2557 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002559 if (timeout == 1) {
2560 PyErr_SetString(socket_timeout, "timed out");
2561 return -1;
2562 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002563 END_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002564 if (outlen < 0) {
2565 /* Note: the call to errorhandler() ALWAYS indirectly returned
2566 NULL, so ignore its return value */
2567 s->errorhandler();
2568 return -1;
2569 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002570#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 read_buf = cbuf;
2572 remaining = len;
2573 while (remaining != 0) {
2574 unsigned int segment;
2575 int nread = -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 segment = remaining /SEGMENT_SIZE;
2578 if (segment != 0) {
2579 segment = SEGMENT_SIZE;
2580 }
2581 else {
2582 segment = remaining;
2583 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002584
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002585 BEGIN_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002587 timeout = internal_select_ex(s, 0, interval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 if (!timeout)
2589 nread = recv(s->sock_fd, read_buf, segment, flags);
2590 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 if (timeout == 1) {
2592 PyErr_SetString(socket_timeout, "timed out");
2593 return -1;
2594 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002595 END_SELECT_LOOP(s)
2596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597 if (nread < 0) {
2598 s->errorhandler();
2599 return -1;
2600 }
2601 if (nread != remaining) {
2602 read_buf += nread;
2603 break;
2604 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 remaining -= segment;
2607 read_buf += segment;
2608 }
2609 outlen = read_buf - cbuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002610#endif /* !__VMS */
2611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612 return outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002613}
2614
Guido van Rossum48a680c2001-03-02 06:34:14 +00002615
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002616/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002617
Guido van Rossum73624e91994-10-10 17:59:00 +00002618static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002619sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002620{
Antoine Pitrou19467d22010-08-17 19:33:30 +00002621 Py_ssize_t recvlen, outlen;
2622 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002623 PyObject *buf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002624
Antoine Pitrou19467d22010-08-17 19:33:30 +00002625 if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 if (recvlen < 0) {
2629 PyErr_SetString(PyExc_ValueError,
2630 "negative buffersize in recv");
2631 return NULL;
2632 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002634 /* Allocate a new string. */
2635 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2636 if (buf == NULL)
2637 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002639 /* Call the guts */
2640 outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags);
2641 if (outlen < 0) {
2642 /* An error occurred, release the string and return an
2643 error. */
2644 Py_DECREF(buf);
2645 return NULL;
2646 }
2647 if (outlen != recvlen) {
2648 /* We did not read as many bytes as we anticipated, resize the
2649 string if possible and be successful. */
2650 _PyBytes_Resize(&buf, outlen);
2651 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 return buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002654}
2655
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002656PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002657"recv(buffersize[, flags]) -> data\n\
2658\n\
2659Receive up to buffersize bytes from the socket. For the optional flags\n\
2660argument, see the Unix manual. When no data is available, block until\n\
2661at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002662the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002663
Guido van Rossum30a685f1991-06-27 15:51:29 +00002664
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002665/* s.recv_into(buffer, [nbytes [,flags]]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002666
Thomas Wouters477c8d52006-05-27 19:21:47 +00002667static PyObject*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002668sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00002671
Antoine Pitrou19467d22010-08-17 19:33:30 +00002672 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 Py_buffer pbuf;
2674 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002675 Py_ssize_t buflen, readlen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 /* Get the buffer's memory */
Antoine Pitrou19467d22010-08-17 19:33:30 +00002678 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 &pbuf, &recvlen, &flags))
2680 return NULL;
2681 buf = pbuf.buf;
2682 buflen = pbuf.len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 if (recvlen < 0) {
2685 PyBuffer_Release(&pbuf);
2686 PyErr_SetString(PyExc_ValueError,
2687 "negative buffersize in recv_into");
2688 return NULL;
2689 }
2690 if (recvlen == 0) {
2691 /* If nbytes was not specified, use the buffer's length */
2692 recvlen = buflen;
2693 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002695 /* Check if the buffer is large enough */
2696 if (buflen < recvlen) {
2697 PyBuffer_Release(&pbuf);
2698 PyErr_SetString(PyExc_ValueError,
2699 "buffer too small for requested bytes");
2700 return NULL;
2701 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 /* Call the guts */
2704 readlen = sock_recv_guts(s, buf, recvlen, flags);
2705 if (readlen < 0) {
2706 /* Return an error. */
2707 PyBuffer_Release(&pbuf);
2708 return NULL;
2709 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 PyBuffer_Release(&pbuf);
2712 /* Return the number of bytes read. Note that we do not do anything
2713 special here in the case that readlen < recvlen. */
2714 return PyLong_FromSsize_t(readlen);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002715}
2716
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002717PyDoc_STRVAR(recv_into_doc,
2718"recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002719\n\
2720A version of recv() that stores its data into a buffer rather than creating \n\
2721a new string. Receive up to buffersize bytes from the socket. If buffersize \n\
2722is not specified (or 0), receive up to the size available in the given buffer.\n\
2723\n\
2724See recv() for documentation about the flags.");
2725
2726
2727/*
Christian Heimes99170a52007-12-19 02:07:34 +00002728 * This is the guts of the recvfrom() and recvfrom_into() methods, which reads
2729 * into a char buffer. If you have any inc/def ref to do to the objects that
2730 * contain the buffer, do it in the caller. This function returns the number
Ezio Melotti13925002011-03-16 11:05:33 +02002731 * of bytes successfully read. If there was an error, it returns -1. Note
Christian Heimes99170a52007-12-19 02:07:34 +00002732 * that it is also possible that we return a number of bytes smaller than the
2733 * request bytes.
Thomas Wouters477c8d52006-05-27 19:21:47 +00002734 *
2735 * 'addr' is a return value for the address object. Note that you must decref
2736 * it yourself.
2737 */
Antoine Pitrou19467d22010-08-17 19:33:30 +00002738static Py_ssize_t
2739sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 PyObject** addr)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 sock_addr_t addrbuf;
2743 int timeout;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002744 Py_ssize_t n = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 *addr = NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 if (!getsockaddrlen(s, &addrlen))
2750 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 if (!IS_SELECTABLE(s)) {
2753 select_error();
2754 return -1;
2755 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002756
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002757 BEGIN_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 Py_BEGIN_ALLOW_THREADS
2759 memset(&addrbuf, 0, addrlen);
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002760 timeout = internal_select_ex(s, 0, interval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 if (!timeout) {
Guido van Rossum8d665e61996-06-26 18:22:49 +00002762#ifndef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 n = recvfrom(s->sock_fd, cbuf, len, flags,
2764 (void *) &addrbuf, &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002765#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 n = recvfrom(s->sock_fd, cbuf, len, flags,
2767 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002768#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 }
2770 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 if (timeout == 1) {
2773 PyErr_SetString(socket_timeout, "timed out");
2774 return -1;
2775 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002776 END_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 if (n < 0) {
2778 s->errorhandler();
2779 return -1;
2780 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002782 if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
2783 addrlen, s->sock_proto)))
2784 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 return n;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002787}
2788
2789/* s.recvfrom(nbytes [,flags]) method */
2790
2791static PyObject *
2792sock_recvfrom(PySocketSockObject *s, PyObject *args)
2793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 PyObject *buf = NULL;
2795 PyObject *addr = NULL;
2796 PyObject *ret = NULL;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002797 int flags = 0;
2798 Py_ssize_t recvlen, outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002799
Antoine Pitrou19467d22010-08-17 19:33:30 +00002800 if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 if (recvlen < 0) {
2804 PyErr_SetString(PyExc_ValueError,
2805 "negative buffersize in recvfrom");
2806 return NULL;
2807 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2810 if (buf == NULL)
2811 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf),
2814 recvlen, flags, &addr);
2815 if (outlen < 0) {
2816 goto finally;
2817 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 if (outlen != recvlen) {
2820 /* We did not read as many bytes as we anticipated, resize the
Ezio Melotti13925002011-03-16 11:05:33 +02002821 string if possible and be successful. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 if (_PyBytes_Resize(&buf, outlen) < 0)
Ezio Melotti13925002011-03-16 11:05:33 +02002823 /* Oopsy, not so successful after all. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 goto finally;
2825 }
Barry Warsaw752300b1997-01-03 17:18:10 +00002826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002828
2829finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 Py_XDECREF(buf);
2831 Py_XDECREF(addr);
2832 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002833}
2834
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002835PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002836"recvfrom(buffersize[, flags]) -> (data, address info)\n\
2837\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002838Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002839
Thomas Wouters477c8d52006-05-27 19:21:47 +00002840
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002841/* s.recvfrom_into(buffer[, nbytes [,flags]]) method */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002842
2843static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002844sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002845{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00002847
Antoine Pitrou19467d22010-08-17 19:33:30 +00002848 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 Py_buffer pbuf;
2850 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002851 Py_ssize_t readlen, buflen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 PyObject *addr = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002854
Antoine Pitrou19467d22010-08-17 19:33:30 +00002855 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 kwlist, &pbuf,
2857 &recvlen, &flags))
2858 return NULL;
2859 buf = pbuf.buf;
2860 buflen = pbuf.len;
2861 assert(buf != 0 && buflen > 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 if (recvlen < 0) {
2864 PyBuffer_Release(&pbuf);
2865 PyErr_SetString(PyExc_ValueError,
2866 "negative buffersize in recvfrom_into");
2867 return NULL;
2868 }
2869 if (recvlen == 0) {
2870 /* If nbytes was not specified, use the buffer's length */
2871 recvlen = buflen;
2872 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
2875 if (readlen < 0) {
2876 PyBuffer_Release(&pbuf);
2877 /* Return an error */
2878 Py_XDECREF(addr);
2879 return NULL;
2880 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 PyBuffer_Release(&pbuf);
2883 /* Return the number of bytes read and the address. Note that we do
2884 not do anything special here in the case that readlen < recvlen. */
Antoine Pitrou19467d22010-08-17 19:33:30 +00002885 return Py_BuildValue("nN", readlen, addr);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002886}
2887
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002888PyDoc_STRVAR(recvfrom_into_doc,
2889"recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002890\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002891Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002892
2893
Nick Coghlan96fe56a2011-08-22 11:55:57 +10002894/* The sendmsg() and recvmsg[_into]() methods require a working
2895 CMSG_LEN(). See the comment near get_CMSG_LEN(). */
2896#ifdef CMSG_LEN
2897/*
2898 * Call recvmsg() with the supplied iovec structures, flags, and
2899 * ancillary data buffer size (controllen). Returns the tuple return
2900 * value for recvmsg() or recvmsg_into(), with the first item provided
2901 * by the supplied makeval() function. makeval() will be called with
2902 * the length read and makeval_data as arguments, and must return a
2903 * new reference (which will be decrefed if there is a subsequent
2904 * error). On error, closes any file descriptors received via
2905 * SCM_RIGHTS.
2906 */
2907static PyObject *
2908sock_recvmsg_guts(PySocketSockObject *s, struct iovec *iov, int iovlen,
2909 int flags, Py_ssize_t controllen,
2910 PyObject *(*makeval)(ssize_t, void *), void *makeval_data)
2911{
2912 ssize_t bytes_received = -1;
2913 int timeout;
2914 sock_addr_t addrbuf;
2915 socklen_t addrbuflen;
Charles-François Natalie9e95ae2011-08-24 21:40:53 +02002916 struct msghdr msg = {0};
Nick Coghlan96fe56a2011-08-22 11:55:57 +10002917 PyObject *cmsg_list = NULL, *retval = NULL;
2918 void *controlbuf = NULL;
2919 struct cmsghdr *cmsgh;
2920 size_t cmsgdatalen = 0;
2921 int cmsg_status;
2922
2923 /* XXX: POSIX says that msg_name and msg_namelen "shall be
2924 ignored" when the socket is connected (Linux fills them in
2925 anyway for AF_UNIX sockets at least). Normally msg_namelen
2926 seems to be set to 0 if there's no address, but try to
2927 initialize msg_name to something that won't be mistaken for a
2928 real address if that doesn't happen. */
2929 if (!getsockaddrlen(s, &addrbuflen))
2930 return NULL;
2931 memset(&addrbuf, 0, addrbuflen);
2932 SAS2SA(&addrbuf)->sa_family = AF_UNSPEC;
2933
2934 if (controllen < 0 || controllen > SOCKLEN_T_LIMIT) {
2935 PyErr_SetString(PyExc_ValueError,
2936 "invalid ancillary data buffer length");
2937 return NULL;
2938 }
2939 if (controllen > 0 && (controlbuf = PyMem_Malloc(controllen)) == NULL)
2940 return PyErr_NoMemory();
2941
2942 /* Make the system call. */
2943 if (!IS_SELECTABLE(s)) {
2944 select_error();
2945 goto finally;
2946 }
2947
2948 BEGIN_SELECT_LOOP(s)
2949 Py_BEGIN_ALLOW_THREADS;
Nick Coghlan96fe56a2011-08-22 11:55:57 +10002950 msg.msg_name = SAS2SA(&addrbuf);
2951 msg.msg_namelen = addrbuflen;
2952 msg.msg_iov = iov;
2953 msg.msg_iovlen = iovlen;
2954 msg.msg_control = controlbuf;
2955 msg.msg_controllen = controllen;
2956 timeout = internal_select_ex(s, 0, interval);
2957 if (!timeout)
2958 bytes_received = recvmsg(s->sock_fd, &msg, flags);
2959 Py_END_ALLOW_THREADS;
2960 if (timeout == 1) {
2961 PyErr_SetString(socket_timeout, "timed out");
2962 goto finally;
2963 }
2964 END_SELECT_LOOP(s)
2965
2966 if (bytes_received < 0) {
2967 s->errorhandler();
2968 goto finally;
2969 }
2970
2971 /* Make list of (level, type, data) tuples from control messages. */
2972 if ((cmsg_list = PyList_New(0)) == NULL)
2973 goto err_closefds;
2974 /* Check for empty ancillary data as old CMSG_FIRSTHDR()
2975 implementations didn't do so. */
2976 for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL);
2977 cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) {
2978 PyObject *bytes, *tuple;
2979 int tmp;
2980
2981 cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen);
2982 if (cmsg_status != 0) {
2983 if (PyErr_WarnEx(PyExc_RuntimeWarning,
2984 "received malformed or improperly-truncated "
2985 "ancillary data", 1) == -1)
2986 goto err_closefds;
2987 }
2988 if (cmsg_status < 0)
2989 break;
2990 if (cmsgdatalen > PY_SSIZE_T_MAX) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002991 PyErr_SetString(PyExc_OSError, "control message too long");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10002992 goto err_closefds;
2993 }
2994
2995 bytes = PyBytes_FromStringAndSize((char *)CMSG_DATA(cmsgh),
2996 cmsgdatalen);
2997 tuple = Py_BuildValue("iiN", (int)cmsgh->cmsg_level,
2998 (int)cmsgh->cmsg_type, bytes);
2999 if (tuple == NULL)
3000 goto err_closefds;
3001 tmp = PyList_Append(cmsg_list, tuple);
3002 Py_DECREF(tuple);
3003 if (tmp != 0)
3004 goto err_closefds;
3005
3006 if (cmsg_status != 0)
3007 break;
3008 }
3009
3010 retval = Py_BuildValue("NOiN",
3011 (*makeval)(bytes_received, makeval_data),
3012 cmsg_list,
3013 (int)msg.msg_flags,
3014 makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
3015 ((msg.msg_namelen > addrbuflen) ?
3016 addrbuflen : msg.msg_namelen),
3017 s->sock_proto));
3018 if (retval == NULL)
3019 goto err_closefds;
3020
3021finally:
3022 Py_XDECREF(cmsg_list);
3023 PyMem_Free(controlbuf);
3024 return retval;
3025
3026err_closefds:
3027#ifdef SCM_RIGHTS
3028 /* Close all descriptors coming from SCM_RIGHTS, so they don't leak. */
3029 for (cmsgh = ((msg.msg_controllen > 0) ? CMSG_FIRSTHDR(&msg) : NULL);
3030 cmsgh != NULL; cmsgh = CMSG_NXTHDR(&msg, cmsgh)) {
3031 cmsg_status = get_cmsg_data_len(&msg, cmsgh, &cmsgdatalen);
3032 if (cmsg_status < 0)
3033 break;
3034 if (cmsgh->cmsg_level == SOL_SOCKET &&
3035 cmsgh->cmsg_type == SCM_RIGHTS) {
3036 size_t numfds;
3037 int *fdp;
3038
3039 numfds = cmsgdatalen / sizeof(int);
3040 fdp = (int *)CMSG_DATA(cmsgh);
3041 while (numfds-- > 0)
3042 close(*fdp++);
3043 }
3044 if (cmsg_status != 0)
3045 break;
3046 }
3047#endif /* SCM_RIGHTS */
3048 goto finally;
3049}
3050
3051
3052static PyObject *
3053makeval_recvmsg(ssize_t received, void *data)
3054{
3055 PyObject **buf = data;
3056
3057 if (received < PyBytes_GET_SIZE(*buf))
3058 _PyBytes_Resize(buf, received);
3059 Py_XINCREF(*buf);
3060 return *buf;
3061}
3062
3063/* s.recvmsg(bufsize[, ancbufsize[, flags]]) method */
3064
3065static PyObject *
3066sock_recvmsg(PySocketSockObject *s, PyObject *args)
3067{
3068 Py_ssize_t bufsize, ancbufsize = 0;
3069 int flags = 0;
3070 struct iovec iov;
3071 PyObject *buf = NULL, *retval = NULL;
3072
3073 if (!PyArg_ParseTuple(args, "n|ni:recvmsg", &bufsize, &ancbufsize, &flags))
3074 return NULL;
3075
3076 if (bufsize < 0) {
3077 PyErr_SetString(PyExc_ValueError, "negative buffer size in recvmsg()");
3078 return NULL;
3079 }
3080 if ((buf = PyBytes_FromStringAndSize(NULL, bufsize)) == NULL)
3081 return NULL;
3082 iov.iov_base = PyBytes_AS_STRING(buf);
3083 iov.iov_len = bufsize;
3084
3085 /* Note that we're passing a pointer to *our pointer* to the bytes
3086 object here (&buf); makeval_recvmsg() may incref the object, or
3087 deallocate it and set our pointer to NULL. */
3088 retval = sock_recvmsg_guts(s, &iov, 1, flags, ancbufsize,
3089 &makeval_recvmsg, &buf);
3090 Py_XDECREF(buf);
3091 return retval;
3092}
3093
3094PyDoc_STRVAR(recvmsg_doc,
3095"recvmsg(bufsize[, ancbufsize[, flags]]) -> (data, ancdata, msg_flags, address)\n\
3096\n\
3097Receive normal data (up to bufsize bytes) and ancillary data from the\n\
3098socket. The ancbufsize argument sets the size in bytes of the\n\
3099internal buffer used to receive the ancillary data; it defaults to 0,\n\
3100meaning that no ancillary data will be received. Appropriate buffer\n\
3101sizes for ancillary data can be calculated using CMSG_SPACE() or\n\
3102CMSG_LEN(), and items which do not fit into the buffer might be\n\
3103truncated or discarded. The flags argument defaults to 0 and has the\n\
3104same meaning as for recv().\n\
3105\n\
3106The return value is a 4-tuple: (data, ancdata, msg_flags, address).\n\
3107The data item is a bytes object holding the non-ancillary data\n\
3108received. The ancdata item is a list of zero or more tuples\n\
3109(cmsg_level, cmsg_type, cmsg_data) representing the ancillary data\n\
3110(control messages) received: cmsg_level and cmsg_type are integers\n\
3111specifying the protocol level and protocol-specific type respectively,\n\
3112and cmsg_data is a bytes object holding the associated data. The\n\
3113msg_flags item is the bitwise OR of various flags indicating\n\
3114conditions on the received message; see your system documentation for\n\
3115details. If the receiving socket is unconnected, address is the\n\
3116address of the sending socket, if available; otherwise, its value is\n\
3117unspecified.\n\
3118\n\
3119If recvmsg() raises an exception after the system call returns, it\n\
3120will first attempt to close any file descriptors received via the\n\
3121SCM_RIGHTS mechanism.");
3122
3123
3124static PyObject *
3125makeval_recvmsg_into(ssize_t received, void *data)
3126{
3127 return PyLong_FromSsize_t(received);
3128}
3129
3130/* s.recvmsg_into(buffers[, ancbufsize[, flags]]) method */
3131
3132static PyObject *
3133sock_recvmsg_into(PySocketSockObject *s, PyObject *args)
3134{
3135 Py_ssize_t ancbufsize = 0;
3136 int flags = 0;
3137 struct iovec *iovs = NULL;
3138 Py_ssize_t i, nitems, nbufs = 0;
3139 Py_buffer *bufs = NULL;
3140 PyObject *buffers_arg, *fast, *retval = NULL;
3141
3142 if (!PyArg_ParseTuple(args, "O|ni:recvmsg_into",
3143 &buffers_arg, &ancbufsize, &flags))
3144 return NULL;
3145
3146 if ((fast = PySequence_Fast(buffers_arg,
3147 "recvmsg_into() argument 1 must be an "
3148 "iterable")) == NULL)
3149 return NULL;
3150 nitems = PySequence_Fast_GET_SIZE(fast);
3151 if (nitems > INT_MAX) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003152 PyErr_SetString(PyExc_OSError, "recvmsg_into() argument 1 is too long");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003153 goto finally;
3154 }
3155
3156 /* Fill in an iovec for each item, and save the Py_buffer
3157 structs to release afterwards. */
3158 if (nitems > 0 && ((iovs = PyMem_New(struct iovec, nitems)) == NULL ||
3159 (bufs = PyMem_New(Py_buffer, nitems)) == NULL)) {
3160 PyErr_NoMemory();
3161 goto finally;
3162 }
3163 for (; nbufs < nitems; nbufs++) {
3164 if (!PyArg_Parse(PySequence_Fast_GET_ITEM(fast, nbufs),
3165 "w*;recvmsg_into() argument 1 must be an iterable "
3166 "of single-segment read-write buffers",
3167 &bufs[nbufs]))
3168 goto finally;
3169 iovs[nbufs].iov_base = bufs[nbufs].buf;
3170 iovs[nbufs].iov_len = bufs[nbufs].len;
3171 }
3172
3173 retval = sock_recvmsg_guts(s, iovs, nitems, flags, ancbufsize,
3174 &makeval_recvmsg_into, NULL);
3175finally:
3176 for (i = 0; i < nbufs; i++)
3177 PyBuffer_Release(&bufs[i]);
3178 PyMem_Free(bufs);
3179 PyMem_Free(iovs);
3180 Py_DECREF(fast);
3181 return retval;
3182}
3183
3184PyDoc_STRVAR(recvmsg_into_doc,
3185"recvmsg_into(buffers[, ancbufsize[, flags]]) -> (nbytes, ancdata, msg_flags, address)\n\
3186\n\
3187Receive normal data and ancillary data from the socket, scattering the\n\
3188non-ancillary data into a series of buffers. The buffers argument\n\
3189must be an iterable of objects that export writable buffers\n\
3190(e.g. bytearray objects); these will be filled with successive chunks\n\
3191of the non-ancillary data until it has all been written or there are\n\
3192no more buffers. The ancbufsize argument sets the size in bytes of\n\
3193the internal buffer used to receive the ancillary data; it defaults to\n\
31940, meaning that no ancillary data will be received. Appropriate\n\
3195buffer sizes for ancillary data can be calculated using CMSG_SPACE()\n\
3196or CMSG_LEN(), and items which do not fit into the buffer might be\n\
3197truncated or discarded. The flags argument defaults to 0 and has the\n\
3198same meaning as for recv().\n\
3199\n\
3200The return value is a 4-tuple: (nbytes, ancdata, msg_flags, address).\n\
3201The nbytes item is the total number of bytes of non-ancillary data\n\
3202written into the buffers. The ancdata item is a list of zero or more\n\
3203tuples (cmsg_level, cmsg_type, cmsg_data) representing the ancillary\n\
3204data (control messages) received: cmsg_level and cmsg_type are\n\
3205integers specifying the protocol level and protocol-specific type\n\
3206respectively, and cmsg_data is a bytes object holding the associated\n\
3207data. The msg_flags item is the bitwise OR of various flags\n\
3208indicating conditions on the received message; see your system\n\
3209documentation for details. If the receiving socket is unconnected,\n\
3210address is the address of the sending socket, if available; otherwise,\n\
3211its value is unspecified.\n\
3212\n\
3213If recvmsg_into() raises an exception after the system call returns,\n\
3214it will first attempt to close any file descriptors received via the\n\
3215SCM_RIGHTS mechanism.");
3216#endif /* CMSG_LEN */
3217
3218
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00003219/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00003220
Guido van Rossum73624e91994-10-10 17:59:00 +00003221static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003222sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00003223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003225 Py_ssize_t len, n = -1;
3226 int flags = 0, timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 Py_buffer pbuf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags))
3230 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 if (!IS_SELECTABLE(s)) {
3233 PyBuffer_Release(&pbuf);
3234 return select_error();
3235 }
3236 buf = pbuf.buf;
3237 len = pbuf.len;
Neal Norwitz082b2df2006-02-07 07:04:46 +00003238
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003239 BEGIN_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003241 timeout = internal_select_ex(s, 1, interval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 if (!timeout)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003243#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 n = sendsegmented(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003245#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003247#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003249 if (timeout == 1) {
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003250 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 PyErr_SetString(socket_timeout, "timed out");
3252 return NULL;
3253 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003254 END_SELECT_LOOP(s)
3255
3256 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 if (n < 0)
3258 return s->errorhandler();
Antoine Pitrou19467d22010-08-17 19:33:30 +00003259 return PyLong_FromSsize_t(n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00003260}
3261
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003262PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003263"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003264\n\
3265Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003266argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003267sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003268
3269
3270/* s.sendall(data [,flags]) method */
3271
3272static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003273sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003275 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00003276 Py_ssize_t len, n = -1;
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003277 int flags = 0, timeout, saved_errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003278 Py_buffer pbuf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280 if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags))
3281 return NULL;
3282 buf = pbuf.buf;
3283 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003285 if (!IS_SELECTABLE(s)) {
3286 PyBuffer_Release(&pbuf);
3287 return select_error();
3288 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00003289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 do {
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003291 Py_BEGIN_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003292 timeout = internal_select(s, 1);
3293 n = -1;
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003294 if (!timeout) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003295#ifdef __VMS
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003296 n = sendsegmented(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003297#else
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003298 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003299#endif
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003300 }
3301 Py_END_ALLOW_THREADS
3302 if (timeout == 1) {
3303 PyBuffer_Release(&pbuf);
3304 PyErr_SetString(socket_timeout, "timed out");
3305 return NULL;
3306 }
3307 /* PyErr_CheckSignals() might change errno */
3308 saved_errno = errno;
3309 /* We must run our signal handlers before looping again.
3310 send() can return a successful partial write when it is
3311 interrupted, so we can't restrict ourselves to EINTR. */
3312 if (PyErr_CheckSignals()) {
3313 PyBuffer_Release(&pbuf);
3314 return NULL;
3315 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 if (n < 0) {
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003317 /* If interrupted, try again */
3318 if (saved_errno == EINTR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 continue;
Antoine Pitrou6d7df632010-09-27 17:52:25 +00003320 else
3321 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 }
3323 buf += n;
3324 len -= n;
3325 } while (len > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 PyBuffer_Release(&pbuf);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003328 if (n < 0)
3329 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00003330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003331 Py_INCREF(Py_None);
3332 return Py_None;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003333}
3334
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003335PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00003336"sendall(data[, flags])\n\
3337\n\
3338Send a data string to the socket. For the optional flags\n\
3339argument, see the Unix manual. This calls send() repeatedly\n\
3340until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003341to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003342
Guido van Rossum30a685f1991-06-27 15:51:29 +00003343
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00003344/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00003345
Guido van Rossum73624e91994-10-10 17:59:00 +00003346static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003347sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003349 Py_buffer pbuf;
3350 PyObject *addro;
3351 char *buf;
Antoine Pitrou5e981412011-03-17 22:38:37 +01003352 Py_ssize_t len, arglen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 sock_addr_t addrbuf;
3354 int addrlen, n = -1, flags, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 flags = 0;
Antoine Pitrou5e981412011-03-17 22:38:37 +01003357 arglen = PyTuple_Size(args);
3358 switch (arglen) {
3359 case 2:
3360 PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro);
3361 break;
3362 case 3:
3363 PyArg_ParseTuple(args, "y*iO:sendto",
3364 &pbuf, &flags, &addro);
3365 break;
3366 default:
3367 PyErr_Format(PyExc_TypeError,
3368 "sendto() takes 2 or 3 arguments (%d given)",
3369 arglen);
Victor Stinner77af1722011-05-26 14:05:59 +02003370 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 }
Antoine Pitrou5e981412011-03-17 22:38:37 +01003372 if (PyErr_Occurred())
3373 return NULL;
3374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 buf = pbuf.buf;
3376 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003378 if (!IS_SELECTABLE(s)) {
3379 PyBuffer_Release(&pbuf);
3380 return select_error();
3381 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00003382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) {
3384 PyBuffer_Release(&pbuf);
3385 return NULL;
3386 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003387
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003388 BEGIN_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003389 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003390 timeout = internal_select_ex(s, 1, interval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 if (!timeout)
3392 n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen);
3393 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00003394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003395 if (timeout == 1) {
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003396 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 PyErr_SetString(socket_timeout, "timed out");
3398 return NULL;
3399 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00003400 END_SELECT_LOOP(s)
3401 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 if (n < 0)
3403 return s->errorhandler();
Antoine Pitrou19467d22010-08-17 19:33:30 +00003404 return PyLong_FromSsize_t(n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003405}
3406
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003407PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003408"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003409\n\
3410Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003411For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003412
Guido van Rossum30a685f1991-06-27 15:51:29 +00003413
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003414/* The sendmsg() and recvmsg[_into]() methods require a working
3415 CMSG_LEN(). See the comment near get_CMSG_LEN(). */
3416#ifdef CMSG_LEN
3417/* s.sendmsg(buffers[, ancdata[, flags[, address]]]) method */
3418
3419static PyObject *
3420sock_sendmsg(PySocketSockObject *s, PyObject *args)
3421{
3422 Py_ssize_t i, ndataparts, ndatabufs = 0, ncmsgs, ncmsgbufs = 0;
3423 Py_buffer *databufs = NULL;
3424 struct iovec *iovs = NULL;
3425 sock_addr_t addrbuf;
Charles-François Natalie9e95ae2011-08-24 21:40:53 +02003426 struct msghdr msg = {0};
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003427 struct cmsginfo {
3428 int level;
3429 int type;
3430 Py_buffer data;
3431 } *cmsgs = NULL;
3432 void *controlbuf = NULL;
3433 size_t controllen, controllen_last;
3434 ssize_t bytes_sent = -1;
3435 int addrlen, timeout, flags = 0;
3436 PyObject *data_arg, *cmsg_arg = NULL, *addr_arg = NULL, *data_fast = NULL,
3437 *cmsg_fast = NULL, *retval = NULL;
3438
3439 if (!PyArg_ParseTuple(args, "O|OiO:sendmsg",
3440 &data_arg, &cmsg_arg, &flags, &addr_arg))
3441 return NULL;
3442
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003443 /* Parse destination address. */
3444 if (addr_arg != NULL && addr_arg != Py_None) {
3445 if (!getsockaddrarg(s, addr_arg, SAS2SA(&addrbuf), &addrlen))
3446 goto finally;
3447 msg.msg_name = &addrbuf;
3448 msg.msg_namelen = addrlen;
3449 }
3450
3451 /* Fill in an iovec for each message part, and save the Py_buffer
3452 structs to release afterwards. */
3453 if ((data_fast = PySequence_Fast(data_arg,
3454 "sendmsg() argument 1 must be an "
3455 "iterable")) == NULL)
3456 goto finally;
3457 ndataparts = PySequence_Fast_GET_SIZE(data_fast);
3458 if (ndataparts > INT_MAX) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003459 PyErr_SetString(PyExc_OSError, "sendmsg() argument 1 is too long");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003460 goto finally;
3461 }
3462 msg.msg_iovlen = ndataparts;
3463 if (ndataparts > 0 &&
3464 ((msg.msg_iov = iovs = PyMem_New(struct iovec, ndataparts)) == NULL ||
3465 (databufs = PyMem_New(Py_buffer, ndataparts)) == NULL)) {
3466 PyErr_NoMemory();
3467 goto finally;
3468 }
3469 for (; ndatabufs < ndataparts; ndatabufs++) {
3470 if (!PyArg_Parse(PySequence_Fast_GET_ITEM(data_fast, ndatabufs),
3471 "y*;sendmsg() argument 1 must be an iterable of "
3472 "buffer-compatible objects",
3473 &databufs[ndatabufs]))
3474 goto finally;
3475 iovs[ndatabufs].iov_base = databufs[ndatabufs].buf;
3476 iovs[ndatabufs].iov_len = databufs[ndatabufs].len;
3477 }
3478
3479 if (cmsg_arg == NULL)
3480 ncmsgs = 0;
3481 else {
3482 if ((cmsg_fast = PySequence_Fast(cmsg_arg,
3483 "sendmsg() argument 2 must be an "
3484 "iterable")) == NULL)
3485 goto finally;
3486 ncmsgs = PySequence_Fast_GET_SIZE(cmsg_fast);
3487 }
3488
3489#ifndef CMSG_SPACE
3490 if (ncmsgs > 1) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003491 PyErr_SetString(PyExc_OSError,
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003492 "sending multiple control messages is not supported "
3493 "on this system");
3494 goto finally;
3495 }
3496#endif
3497 /* Save level, type and Py_buffer for each control message,
3498 and calculate total size. */
3499 if (ncmsgs > 0 && (cmsgs = PyMem_New(struct cmsginfo, ncmsgs)) == NULL) {
3500 PyErr_NoMemory();
3501 goto finally;
3502 }
3503 controllen = controllen_last = 0;
3504 while (ncmsgbufs < ncmsgs) {
3505 size_t bufsize, space;
3506
3507 if (!PyArg_Parse(PySequence_Fast_GET_ITEM(cmsg_fast, ncmsgbufs),
3508 "(iiy*):[sendmsg() ancillary data items]",
3509 &cmsgs[ncmsgbufs].level,
3510 &cmsgs[ncmsgbufs].type,
3511 &cmsgs[ncmsgbufs].data))
3512 goto finally;
3513 bufsize = cmsgs[ncmsgbufs++].data.len;
3514
3515#ifdef CMSG_SPACE
3516 if (!get_CMSG_SPACE(bufsize, &space)) {
3517#else
3518 if (!get_CMSG_LEN(bufsize, &space)) {
3519#endif
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003520 PyErr_SetString(PyExc_OSError, "ancillary data item too large");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003521 goto finally;
3522 }
3523 controllen += space;
3524 if (controllen > SOCKLEN_T_LIMIT || controllen < controllen_last) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02003525 PyErr_SetString(PyExc_OSError, "too much ancillary data");
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003526 goto finally;
3527 }
3528 controllen_last = controllen;
3529 }
3530
3531 /* Construct ancillary data block from control message info. */
3532 if (ncmsgbufs > 0) {
3533 struct cmsghdr *cmsgh = NULL;
3534
3535 if ((msg.msg_control = controlbuf =
3536 PyMem_Malloc(controllen)) == NULL) {
3537 PyErr_NoMemory();
3538 goto finally;
3539 }
3540 msg.msg_controllen = controllen;
3541
3542 /* Need to zero out the buffer as a workaround for glibc's
3543 CMSG_NXTHDR() implementation. After getting the pointer to
3544 the next header, it checks its (uninitialized) cmsg_len
3545 member to see if the "message" fits in the buffer, and
3546 returns NULL if it doesn't. Zero-filling the buffer
Terry Jan Reedy0f847642013-03-11 18:34:00 -04003547 ensures that this doesn't happen. */
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003548 memset(controlbuf, 0, controllen);
3549
3550 for (i = 0; i < ncmsgbufs; i++) {
3551 size_t msg_len, data_len = cmsgs[i].data.len;
3552 int enough_space = 0;
3553
3554 cmsgh = (i == 0) ? CMSG_FIRSTHDR(&msg) : CMSG_NXTHDR(&msg, cmsgh);
3555 if (cmsgh == NULL) {
3556 PyErr_Format(PyExc_RuntimeError,
3557 "unexpected NULL result from %s()",
3558 (i == 0) ? "CMSG_FIRSTHDR" : "CMSG_NXTHDR");
3559 goto finally;
3560 }
3561 if (!get_CMSG_LEN(data_len, &msg_len)) {
3562 PyErr_SetString(PyExc_RuntimeError,
3563 "item size out of range for CMSG_LEN()");
3564 goto finally;
3565 }
3566 if (cmsg_min_space(&msg, cmsgh, msg_len)) {
3567 size_t space;
3568
3569 cmsgh->cmsg_len = msg_len;
3570 if (get_cmsg_data_space(&msg, cmsgh, &space))
3571 enough_space = (space >= data_len);
3572 }
3573 if (!enough_space) {
3574 PyErr_SetString(PyExc_RuntimeError,
3575 "ancillary data does not fit in calculated "
3576 "space");
3577 goto finally;
3578 }
3579 cmsgh->cmsg_level = cmsgs[i].level;
3580 cmsgh->cmsg_type = cmsgs[i].type;
3581 memcpy(CMSG_DATA(cmsgh), cmsgs[i].data.buf, data_len);
3582 }
3583 }
3584
3585 /* Make the system call. */
3586 if (!IS_SELECTABLE(s)) {
3587 select_error();
3588 goto finally;
3589 }
3590
3591 BEGIN_SELECT_LOOP(s)
3592 Py_BEGIN_ALLOW_THREADS;
3593 timeout = internal_select_ex(s, 1, interval);
3594 if (!timeout)
3595 bytes_sent = sendmsg(s->sock_fd, &msg, flags);
3596 Py_END_ALLOW_THREADS;
3597 if (timeout == 1) {
3598 PyErr_SetString(socket_timeout, "timed out");
3599 goto finally;
3600 }
3601 END_SELECT_LOOP(s)
3602
3603 if (bytes_sent < 0) {
3604 s->errorhandler();
3605 goto finally;
3606 }
3607 retval = PyLong_FromSsize_t(bytes_sent);
3608
3609finally:
3610 PyMem_Free(controlbuf);
3611 for (i = 0; i < ncmsgbufs; i++)
3612 PyBuffer_Release(&cmsgs[i].data);
3613 PyMem_Free(cmsgs);
3614 Py_XDECREF(cmsg_fast);
3615 for (i = 0; i < ndatabufs; i++)
3616 PyBuffer_Release(&databufs[i]);
3617 PyMem_Free(databufs);
3618 PyMem_Free(iovs);
3619 Py_XDECREF(data_fast);
3620 return retval;
3621}
3622
3623PyDoc_STRVAR(sendmsg_doc,
3624"sendmsg(buffers[, ancdata[, flags[, address]]]) -> count\n\
3625\n\
3626Send normal and ancillary data to the socket, gathering the\n\
3627non-ancillary data from a series of buffers and concatenating it into\n\
3628a single message. The buffers argument specifies the non-ancillary\n\
3629data as an iterable of buffer-compatible objects (e.g. bytes objects).\n\
3630The ancdata argument specifies the ancillary data (control messages)\n\
3631as an iterable of zero or more tuples (cmsg_level, cmsg_type,\n\
3632cmsg_data), where cmsg_level and cmsg_type are integers specifying the\n\
3633protocol level and protocol-specific type respectively, and cmsg_data\n\
3634is a buffer-compatible object holding the associated data. The flags\n\
3635argument defaults to 0 and has the same meaning as for send(). If\n\
3636address is supplied and not None, it sets a destination address for\n\
3637the message. The return value is the number of bytes of non-ancillary\n\
3638data sent.");
3639#endif /* CMSG_LEN */
3640
3641
Guido van Rossum30a685f1991-06-27 15:51:29 +00003642/* s.shutdown(how) method */
3643
Guido van Rossum73624e91994-10-10 17:59:00 +00003644static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003645sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003646{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003647 int how;
3648 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00003649
Serhiy Storchaka78980432013-01-15 01:12:17 +02003650 how = _PyLong_AsInt(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003651 if (how == -1 && PyErr_Occurred())
3652 return NULL;
3653 Py_BEGIN_ALLOW_THREADS
3654 res = shutdown(s->sock_fd, how);
3655 Py_END_ALLOW_THREADS
3656 if (res < 0)
3657 return s->errorhandler();
3658 Py_INCREF(Py_None);
3659 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003660}
3661
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003662PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003663"shutdown(flag)\n\
3664\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00003665Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
3666of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003667
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00003668#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Christian Heimesfaf2f632008-01-06 16:59:19 +00003669static PyObject*
3670sock_ioctl(PySocketSockObject *s, PyObject *arg)
3671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003672 unsigned long cmd = SIO_RCVALL;
3673 PyObject *argO;
3674 DWORD recv;
Christian Heimesfaf2f632008-01-06 16:59:19 +00003675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO))
3677 return NULL;
Christian Heimesfaf2f632008-01-06 16:59:19 +00003678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003679 switch (cmd) {
3680 case SIO_RCVALL: {
3681 unsigned int option = RCVALL_ON;
3682 if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option))
3683 return NULL;
3684 if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option),
3685 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
3686 return set_error();
3687 }
3688 return PyLong_FromUnsignedLong(recv); }
3689 case SIO_KEEPALIVE_VALS: {
3690 struct tcp_keepalive ka;
3691 if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd,
3692 &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval))
3693 return NULL;
3694 if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka),
3695 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
3696 return set_error();
3697 }
3698 return PyLong_FromUnsignedLong(recv); }
3699 default:
3700 PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd);
3701 return NULL;
3702 }
Christian Heimesfaf2f632008-01-06 16:59:19 +00003703}
3704PyDoc_STRVAR(sock_ioctl_doc,
3705"ioctl(cmd, option) -> long\n\
3706\n\
Kristján Valur Jónsson847ec752009-09-27 21:10:38 +00003707Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\
3708SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\
3709SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval).");
Kristján Valur Jónsson10f383a2012-04-07 11:23:31 +00003710#endif
3711
3712#if defined(MS_WINDOWS)
3713static PyObject*
3714sock_share(PySocketSockObject *s, PyObject *arg)
3715{
3716 WSAPROTOCOL_INFO info;
3717 DWORD processId;
3718 int result;
3719
3720 if (!PyArg_ParseTuple(arg, "I", &processId))
3721 return NULL;
3722
3723 Py_BEGIN_ALLOW_THREADS
3724 result = WSADuplicateSocket(s->sock_fd, processId, &info);
3725 Py_END_ALLOW_THREADS
3726 if (result == SOCKET_ERROR)
3727 return set_error();
3728 return PyBytes_FromStringAndSize((const char*)&info, sizeof(info));
3729}
3730PyDoc_STRVAR(sock_share_doc,
3731"share(process_id) -> bytes\n\
3732\n\
3733Share the socket with another process. The target process id\n\
3734must be provided and the resulting bytes object passed to the target\n\
3735process. There the shared socket can be instantiated by calling\n\
3736socket.fromshare().");
3737
Christian Heimesfaf2f632008-01-06 16:59:19 +00003738
3739#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00003740
3741/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003742
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003743static PyMethodDef sock_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003744 {"_accept", (PyCFunction)sock_accept, METH_NOARGS,
3745 accept_doc},
3746 {"bind", (PyCFunction)sock_bind, METH_O,
3747 bind_doc},
3748 {"close", (PyCFunction)sock_close, METH_NOARGS,
3749 close_doc},
3750 {"connect", (PyCFunction)sock_connect, METH_O,
3751 connect_doc},
3752 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
3753 connect_ex_doc},
Antoine Pitrou6e451df2010-08-09 20:39:54 +00003754 {"detach", (PyCFunction)sock_detach, METH_NOARGS,
3755 detach_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003756 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
3757 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003758#ifdef HAVE_GETPEERNAME
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003759 {"getpeername", (PyCFunction)sock_getpeername,
3760 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00003761#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 {"getsockname", (PyCFunction)sock_getsockname,
3763 METH_NOARGS, getsockname_doc},
3764 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
3765 getsockopt_doc},
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00003766#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003767 {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS,
3768 sock_ioctl_doc},
Christian Heimesfaf2f632008-01-06 16:59:19 +00003769#endif
Kristján Valur Jónsson10f383a2012-04-07 11:23:31 +00003770#if defined(MS_WINDOWS)
3771 {"share", (PyCFunction)sock_share, METH_VARARGS,
3772 sock_share_doc},
3773#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003774 {"listen", (PyCFunction)sock_listen, METH_O,
3775 listen_doc},
3776 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
3777 recv_doc},
3778 {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
3779 recv_into_doc},
3780 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
3781 recvfrom_doc},
3782 {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
3783 recvfrom_into_doc},
3784 {"send", (PyCFunction)sock_send, METH_VARARGS,
3785 send_doc},
3786 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
3787 sendall_doc},
3788 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
3789 sendto_doc},
3790 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
3791 setblocking_doc},
3792 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
3793 settimeout_doc},
3794 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
3795 gettimeout_doc},
3796 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
3797 setsockopt_doc},
3798 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
3799 shutdown_doc},
Nick Coghlan96fe56a2011-08-22 11:55:57 +10003800#ifdef CMSG_LEN
3801 {"recvmsg", (PyCFunction)sock_recvmsg, METH_VARARGS,
3802 recvmsg_doc},
3803 {"recvmsg_into", (PyCFunction)sock_recvmsg_into, METH_VARARGS,
3804 recvmsg_into_doc,},
3805 {"sendmsg", (PyCFunction)sock_sendmsg, METH_VARARGS,
3806 sendmsg_doc},
3807#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003808 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003809};
3810
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003811/* SockObject members */
3812static PyMemberDef sock_memberlist[] = {
3813 {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},
3814 {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},
3815 {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},
3816 {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"},
3817 {0},
3818};
Guido van Rossum30a685f1991-06-27 15:51:29 +00003819
Guido van Rossum73624e91994-10-10 17:59:00 +00003820/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00003821 First close the file description. */
3822
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003823static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003824sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003825{
Antoine Pitroue033e062010-10-29 10:38:18 +00003826 if (s->sock_fd != -1) {
3827 PyObject *exc, *val, *tb;
3828 Py_ssize_t old_refcount = Py_REFCNT(s);
3829 ++Py_REFCNT(s);
3830 PyErr_Fetch(&exc, &val, &tb);
3831 if (PyErr_WarnFormat(PyExc_ResourceWarning, 1,
3832 "unclosed %R", s))
3833 /* Spurious errors can appear at shutdown */
3834 if (PyErr_ExceptionMatches(PyExc_Warning))
3835 PyErr_WriteUnraisable((PyObject *) s);
3836 PyErr_Restore(exc, val, tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003837 (void) SOCKETCLOSE(s->sock_fd);
Antoine Pitroue033e062010-10-29 10:38:18 +00003838 Py_REFCNT(s) = old_refcount;
3839 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003840 Py_TYPE(s)->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003841}
3842
Guido van Rossum30a685f1991-06-27 15:51:29 +00003843
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003844static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003845sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003846{
Fred Drakea04eaad2000-06-30 02:46:07 +00003847#if SIZEOF_SOCKET_T > SIZEOF_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003848 if (s->sock_fd > LONG_MAX) {
3849 /* this can occur on Win64, and actually there is a special
3850 ugly printf formatter for decimal pointer length integer
3851 printing, only bother if necessary*/
3852 PyErr_SetString(PyExc_OverflowError,
3853 "no printf formatter to display "
3854 "the socket descriptor in decimal");
3855 return NULL;
3856 }
Fred Drakea04eaad2000-06-30 02:46:07 +00003857#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003858 return PyUnicode_FromFormat(
3859 "<socket object, fd=%ld, family=%d, type=%d, proto=%d>",
3860 (long)s->sock_fd, s->sock_family,
3861 s->sock_type,
3862 s->sock_proto);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003863}
3864
3865
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003866/* Create a new, uninitialized socket object. */
3867
3868static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003869sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003871 PyObject *new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003873 new = type->tp_alloc(type, 0);
3874 if (new != NULL) {
3875 ((PySocketSockObject *)new)->sock_fd = -1;
3876 ((PySocketSockObject *)new)->sock_timeout = -1.0;
3877 ((PySocketSockObject *)new)->errorhandler = &set_error;
3878 }
3879 return new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003880}
3881
3882
3883/* Initialize a new socket object. */
3884
3885/*ARGSUSED*/
3886static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00003887sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003889 PySocketSockObject *s = (PySocketSockObject *)self;
3890 PyObject *fdobj = NULL;
3891 SOCKET_T fd = INVALID_SOCKET;
3892 int family = AF_INET, type = SOCK_STREAM, proto = 0;
3893 static char *keywords[] = {"family", "type", "proto", "fileno", 0};
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003895 if (!PyArg_ParseTupleAndKeywords(args, kwds,
3896 "|iiiO:socket", keywords,
3897 &family, &type, &proto, &fdobj))
3898 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003900 if (fdobj != NULL && fdobj != Py_None) {
Kristján Valur Jónsson10f383a2012-04-07 11:23:31 +00003901#ifdef MS_WINDOWS
3902 /* recreate a socket that was duplicated */
3903 if (PyBytes_Check(fdobj)) {
3904 WSAPROTOCOL_INFO info;
3905 if (PyBytes_GET_SIZE(fdobj) != sizeof(info)) {
3906 PyErr_Format(PyExc_ValueError,
3907 "socket descriptor string has wrong size, "
3908 "should be %zu bytes.", sizeof(info));
3909 return -1;
3910 }
3911 memcpy(&info, PyBytes_AS_STRING(fdobj), sizeof(info));
3912 Py_BEGIN_ALLOW_THREADS
3913 fd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
3914 FROM_PROTOCOL_INFO, &info, 0, WSA_FLAG_OVERLAPPED);
3915 Py_END_ALLOW_THREADS
3916 if (fd == INVALID_SOCKET) {
3917 set_error();
3918 return -1;
3919 }
3920 family = info.iAddressFamily;
3921 type = info.iSocketType;
3922 proto = info.iProtocol;
3923 }
3924 else
3925#endif
3926 {
3927 fd = PyLong_AsSocket_t(fdobj);
3928 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
3929 return -1;
3930 if (fd == INVALID_SOCKET) {
3931 PyErr_SetString(PyExc_ValueError,
3932 "can't use invalid socket value");
3933 return -1;
3934 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003935 }
3936 }
3937 else {
3938 Py_BEGIN_ALLOW_THREADS
3939 fd = socket(family, type, proto);
3940 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00003941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003942 if (fd == INVALID_SOCKET) {
3943 set_error();
3944 return -1;
3945 }
3946 }
3947 init_sockobject(s, fd, family, type, proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003949 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003950
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003951}
3952
3953
Guido van Rossumb6775db1994-08-01 11:34:53 +00003954/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00003955
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003956static PyTypeObject sock_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
3958 "_socket.socket", /* tp_name */
3959 sizeof(PySocketSockObject), /* tp_basicsize */
3960 0, /* tp_itemsize */
3961 (destructor)sock_dealloc, /* tp_dealloc */
3962 0, /* tp_print */
3963 0, /* tp_getattr */
3964 0, /* tp_setattr */
3965 0, /* tp_reserved */
3966 (reprfunc)sock_repr, /* tp_repr */
3967 0, /* tp_as_number */
3968 0, /* tp_as_sequence */
3969 0, /* tp_as_mapping */
3970 0, /* tp_hash */
3971 0, /* tp_call */
3972 0, /* tp_str */
3973 PyObject_GenericGetAttr, /* tp_getattro */
3974 0, /* tp_setattro */
3975 0, /* tp_as_buffer */
3976 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
3977 sock_doc, /* tp_doc */
3978 0, /* tp_traverse */
3979 0, /* tp_clear */
3980 0, /* tp_richcompare */
3981 0, /* tp_weaklistoffset */
3982 0, /* tp_iter */
3983 0, /* tp_iternext */
3984 sock_methods, /* tp_methods */
3985 sock_memberlist, /* tp_members */
3986 0, /* tp_getset */
3987 0, /* tp_base */
3988 0, /* tp_dict */
3989 0, /* tp_descr_get */
3990 0, /* tp_descr_set */
3991 0, /* tp_dictoffset */
3992 sock_initobj, /* tp_init */
3993 PyType_GenericAlloc, /* tp_alloc */
3994 sock_new, /* tp_new */
3995 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003996};
3997
Guido van Rossum30a685f1991-06-27 15:51:29 +00003998
Guido van Rossum81194471991-07-27 21:42:02 +00003999/* Python interface to gethostname(). */
4000
4001/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00004002static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004003socket_gethostname(PyObject *self, PyObject *unused)
Guido van Rossum81194471991-07-27 21:42:02 +00004004{
Martin v. Löwis72f48422010-10-29 18:20:08 +00004005#ifdef MS_WINDOWS
4006 /* Don't use winsock's gethostname, as this returns the ANSI
4007 version of the hostname, whereas we need a Unicode string.
4008 Otherwise, gethostname apparently also returns the DNS name. */
Hirokazu Yamamoto09fff7a2010-11-05 17:24:13 +00004009 wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1];
Victor Stinner63941882011-09-29 00:42:28 +02004010 DWORD size = Py_ARRAY_LENGTH(buf);
Victor Stinner74168972011-11-17 01:11:36 +01004011 wchar_t *name;
Hirokazu Yamamoto09fff7a2010-11-05 17:24:13 +00004012 PyObject *result;
Victor Stinner74168972011-11-17 01:11:36 +01004013
4014 if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size))
Victor Stinner9d3b93b2011-11-22 02:27:30 +01004015 return PyUnicode_FromWideChar(buf, size);
Victor Stinner74168972011-11-17 01:11:36 +01004016
4017 if (GetLastError() != ERROR_MORE_DATA)
4018 return PyErr_SetFromWindowsErr(0);
4019
4020 if (size == 0)
4021 return PyUnicode_New(0, 0);
4022
4023 /* MSDN says ERROR_MORE_DATA may occur because DNS allows longer
4024 names */
4025 name = PyMem_Malloc(size * sizeof(wchar_t));
4026 if (!name)
4027 return NULL;
4028 if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname,
4029 name,
4030 &size))
4031 {
4032 PyMem_Free(name);
4033 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis72f48422010-10-29 18:20:08 +00004034 }
Victor Stinner74168972011-11-17 01:11:36 +01004035
4036 result = PyUnicode_FromWideChar(name, size);
4037 PyMem_Free(name);
4038 return result;
Martin v. Löwis72f48422010-10-29 18:20:08 +00004039#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004040 char buf[1024];
4041 int res;
4042 Py_BEGIN_ALLOW_THREADS
4043 res = gethostname(buf, (int) sizeof buf - 1);
4044 Py_END_ALLOW_THREADS
4045 if (res < 0)
4046 return set_error();
4047 buf[sizeof buf - 1] = '\0';
Victor Stinnera534fc42013-06-03 22:07:27 +02004048 return PyUnicode_DecodeFSDefault(buf);
Martin v. Löwis72f48422010-10-29 18:20:08 +00004049#endif
Guido van Rossum81194471991-07-27 21:42:02 +00004050}
Guido van Rossumff4949e1992-08-05 19:58:53 +00004051
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004052PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004053"gethostname() -> string\n\
4054\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004055Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004056
Antoine Pitrou061cfb52011-02-28 22:25:22 +00004057#ifdef HAVE_SETHOSTNAME
4058PyDoc_STRVAR(sethostname_doc,
4059"sethostname(name)\n\n\
4060Sets the hostname to name.");
4061
4062static PyObject *
4063socket_sethostname(PyObject *self, PyObject *args)
4064{
4065 PyObject *hnobj;
4066 Py_buffer buf;
4067 int res, flag = 0;
4068
4069 if (!PyArg_ParseTuple(args, "S:sethostname", &hnobj)) {
4070 PyErr_Clear();
4071 if (!PyArg_ParseTuple(args, "O&:sethostname",
4072 PyUnicode_FSConverter, &hnobj))
4073 return NULL;
4074 flag = 1;
4075 }
4076 res = PyObject_GetBuffer(hnobj, &buf, PyBUF_SIMPLE);
4077 if (!res) {
4078 res = sethostname(buf.buf, buf.len);
4079 PyBuffer_Release(&buf);
4080 }
4081 if (flag)
4082 Py_DECREF(hnobj);
4083 if (res)
4084 return set_error();
4085 Py_RETURN_NONE;
4086}
4087#endif
Guido van Rossumff4949e1992-08-05 19:58:53 +00004088
Guido van Rossum30a685f1991-06-27 15:51:29 +00004089/* Python interface to gethostbyname(name). */
4090
4091/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00004092static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004093socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00004094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004095 char *name;
4096 sock_addr_t addrbuf;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004097 PyObject *ret = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004098
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004099 if (!PyArg_ParseTuple(args, "et:gethostbyname", "idna", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004100 return NULL;
4101 if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004102 goto finally;
4103 ret = makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
4104finally:
4105 PyMem_Free(name);
4106 return ret;
Guido van Rossum30a685f1991-06-27 15:51:29 +00004107}
4108
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004109PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004110"gethostbyname(host) -> address\n\
4111\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004112Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004113
4114
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004115/* Convenience function common to gethostbyname_ex and gethostbyaddr */
4116
4117static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004118gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004119{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004120 char **pch;
4121 PyObject *rtn_tuple = (PyObject *)NULL;
4122 PyObject *name_list = (PyObject *)NULL;
4123 PyObject *addr_list = (PyObject *)NULL;
4124 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 if (h == NULL) {
4127 /* Let's get real error message to return */
4128 set_herror(h_errno);
4129 return NULL;
4130 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004132 if (h->h_addrtype != af) {
4133 /* Let's get real error message to return */
Victor Stinner1f33f2b2011-12-17 04:45:09 +01004134 errno = EAFNOSUPPORT;
4135 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004136 return NULL;
4137 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004139 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00004140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004141 case AF_INET:
4142 if (alen < sizeof(struct sockaddr_in))
4143 return NULL;
4144 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004145
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00004146#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 case AF_INET6:
4148 if (alen < sizeof(struct sockaddr_in6))
4149 return NULL;
4150 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004151#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00004152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004153 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 if ((name_list = PyList_New(0)) == NULL)
4156 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 if ((addr_list = PyList_New(0)) == NULL)
4159 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004161 /* SF #1511317: h_aliases can be NULL */
4162 if (h->h_aliases) {
4163 for (pch = h->h_aliases; *pch != NULL; pch++) {
4164 int status;
4165 tmp = PyUnicode_FromString(*pch);
4166 if (tmp == NULL)
4167 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004169 status = PyList_Append(name_list, tmp);
4170 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00004171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004172 if (status)
4173 goto err;
4174 }
4175 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004177 for (pch = h->h_addr_list; *pch != NULL; pch++) {
4178 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004180 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00004181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004182 case AF_INET:
4183 {
4184 struct sockaddr_in sin;
4185 memset(&sin, 0, sizeof(sin));
4186 sin.sin_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004187#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004188 sin.sin_len = sizeof(sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004189#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004190 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
4191 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00004192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004193 if (pch == h->h_addr_list && alen >= sizeof(sin))
4194 memcpy((char *) addr, &sin, sizeof(sin));
4195 break;
4196 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004197
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00004198#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004199 case AF_INET6:
4200 {
4201 struct sockaddr_in6 sin6;
4202 memset(&sin6, 0, sizeof(sin6));
4203 sin6.sin6_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004204#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004205 sin6.sin6_len = sizeof(sin6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004206#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004207 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
4208 tmp = makeipaddr((struct sockaddr *)&sin6,
4209 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00004210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004211 if (pch == h->h_addr_list && alen >= sizeof(sin6))
4212 memcpy((char *) addr, &sin6, sizeof(sin6));
4213 break;
4214 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004215#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00004216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004217 default: /* can't happen */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004218 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004219 "unsupported address family");
4220 return NULL;
4221 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 if (tmp == NULL)
4224 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00004225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004226 status = PyList_Append(addr_list, tmp);
4227 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00004228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 if (status)
4230 goto err;
4231 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00004232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004233 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00004234
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004235 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004236 Py_XDECREF(name_list);
4237 Py_XDECREF(addr_list);
4238 return rtn_tuple;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004239}
4240
4241
4242/* Python interface to gethostbyname_ex(name). */
4243
4244/*ARGSUSED*/
4245static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004246socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004248 char *name;
4249 struct hostent *h;
Charles-François Natali8b759652011-12-23 16:44:51 +01004250 sock_addr_t addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004251 struct sockaddr *sa;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004252 PyObject *ret = NULL;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004253#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004254 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004255#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004256 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004257#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004258 char buf[16384];
4259 int buf_len = (sizeof buf) - 1;
4260 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004261#endif
Ross Lagerwall8c159762012-03-06 21:36:18 +02004262#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004263 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004264#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004265#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004266
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004267 if (!PyArg_ParseTuple(args, "et:gethostbyname_ex", "idna", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004268 return NULL;
Charles-François Natali8b759652011-12-23 16:44:51 +01004269 if (setipaddr(name, SAS2SA(&addr), sizeof(addr), AF_INET) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004270 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004271 Py_BEGIN_ALLOW_THREADS
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004272#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004273#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Ross Lagerwall8c159762012-03-06 21:36:18 +02004274 gethostbyname_r(name, &hp_allocated, buf, buf_len,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004275 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004276#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004277 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004278#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004279 memset((void *) &data, '\0', sizeof(data));
4280 result = gethostbyname_r(name, &hp_allocated, &data);
4281 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004282#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004283#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00004284#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004286#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004287 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004288#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 Py_END_ALLOW_THREADS
4290 /* Some C libraries would require addr.__ss_family instead of
4291 addr.ss_family.
4292 Therefore, we cast the sockaddr_storage into sockaddr to
4293 access sa_family. */
Charles-François Natali8b759652011-12-23 16:44:51 +01004294 sa = SAS2SA(&addr);
4295 ret = gethost_common(h, SAS2SA(&addr), sizeof(addr),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004296 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00004297#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004298 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00004299#endif
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004300finally:
4301 PyMem_Free(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004303}
4304
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004305PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004306"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
4307\n\
4308Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004309for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00004310
4311
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004312/* Python interface to gethostbyaddr(IP). */
4313
4314/*ARGSUSED*/
4315static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004316socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004317{
Charles-François Natali8b759652011-12-23 16:44:51 +01004318 sock_addr_t addr;
4319 struct sockaddr *sa = SAS2SA(&addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004320 char *ip_num;
4321 struct hostent *h;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004322 PyObject *ret = NULL;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004323#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004324 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004325#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004327#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004328 /* glibcs up to 2.10 assume that the buf argument to
4329 gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc
4330 does not ensure. The attribute below instructs the compiler
4331 to maintain this alignment. */
4332 char buf[16384] Py_ALIGNED(8);
4333 int buf_len = (sizeof buf) - 1;
4334 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004335#endif
Ross Lagerwall8c159762012-03-06 21:36:18 +02004336#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004337 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004338#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004339#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004340 char *ap;
4341 int al;
4342 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004343
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004344 if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004345 return NULL;
4346 af = AF_UNSPEC;
4347 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004348 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004349 af = sa->sa_family;
4350 ap = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00004351 /* al = 0; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004352 switch (af) {
4353 case AF_INET:
4354 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
4355 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
4356 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00004357#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 case AF_INET6:
4359 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
4360 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
4361 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004362#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004363 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004364 PyErr_SetString(PyExc_OSError, "unsupported address family");
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004365 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004366 }
4367 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004368#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004369#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Ross Lagerwall8c159762012-03-06 21:36:18 +02004370 gethostbyaddr_r(ap, al, af,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004371 &hp_allocated, buf, buf_len,
4372 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004373#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 h = gethostbyaddr_r(ap, al, af,
4375 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00004376#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 memset((void *) &data, '\0', sizeof(data));
4378 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
4379 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00004380#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004381#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00004382#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004383 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004384#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004385 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004386#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004387 Py_END_ALLOW_THREADS
Charles-François Natali8b759652011-12-23 16:44:51 +01004388 ret = gethost_common(h, SAS2SA(&addr), sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00004389#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004390 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00004391#endif
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00004392finally:
4393 PyMem_Free(ip_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004394 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00004395}
4396
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004397PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004398"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
4399\n\
4400Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004401for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004402
Guido van Rossum30a685f1991-06-27 15:51:29 +00004403
4404/* Python interface to getservbyname(name).
4405 This only returns the port number, since the other info is already
4406 known or not useful (like the list of aliases). */
4407
4408/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00004409static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004410socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00004411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 char *name, *proto=NULL;
4413 struct servent *sp;
4414 if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
4415 return NULL;
4416 Py_BEGIN_ALLOW_THREADS
4417 sp = getservbyname(name, proto);
4418 Py_END_ALLOW_THREADS
4419 if (sp == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004420 PyErr_SetString(PyExc_OSError, "service/proto not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004421 return NULL;
4422 }
4423 return PyLong_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00004424}
4425
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004426PyDoc_STRVAR(getservbyname_doc,
Barry Warsaw11b91a02004-06-28 00:50:43 +00004427"getservbyname(servicename[, protocolname]) -> integer\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00004428\n\
4429Return a port number from a service name and protocol name.\n\
Barry Warsaw11b91a02004-06-28 00:50:43 +00004430The optional protocol name, if given, should be 'tcp' or 'udp',\n\
4431otherwise any protocol will match.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004432
Guido van Rossum30a685f1991-06-27 15:51:29 +00004433
Barry Warsaw11b91a02004-06-28 00:50:43 +00004434/* Python interface to getservbyport(port).
4435 This only returns the service name, since the other info is already
4436 known or not useful (like the list of aliases). */
4437
4438/*ARGSUSED*/
4439static PyObject *
4440socket_getservbyport(PyObject *self, PyObject *args)
4441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004442 int port;
4443 char *proto=NULL;
4444 struct servent *sp;
4445 if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto))
4446 return NULL;
4447 if (port < 0 || port > 0xffff) {
4448 PyErr_SetString(
4449 PyExc_OverflowError,
4450 "getservbyport: port must be 0-65535.");
4451 return NULL;
4452 }
4453 Py_BEGIN_ALLOW_THREADS
4454 sp = getservbyport(htons((short)port), proto);
4455 Py_END_ALLOW_THREADS
4456 if (sp == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004457 PyErr_SetString(PyExc_OSError, "port/proto not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 return NULL;
4459 }
4460 return PyUnicode_FromString(sp->s_name);
Barry Warsaw11b91a02004-06-28 00:50:43 +00004461}
4462
4463PyDoc_STRVAR(getservbyport_doc,
4464"getservbyport(port[, protocolname]) -> string\n\
4465\n\
4466Return the service name from a port number and protocol name.\n\
4467The optional protocol name, if given, should be 'tcp' or 'udp',\n\
4468otherwise any protocol will match.");
4469
Guido van Rossum3901d851996-12-19 16:35:04 +00004470/* Python interface to getprotobyname(name).
4471 This only returns the protocol number, since the other info is
4472 already known or not useful (like the list of aliases). */
4473
4474/*ARGSUSED*/
4475static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004476socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00004477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004478 char *name;
4479 struct protoent *sp;
4480 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
4481 return NULL;
4482 Py_BEGIN_ALLOW_THREADS
4483 sp = getprotobyname(name);
4484 Py_END_ALLOW_THREADS
4485 if (sp == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004486 PyErr_SetString(PyExc_OSError, "protocol not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 return NULL;
4488 }
4489 return PyLong_FromLong((long) sp->p_proto);
Guido van Rossum3901d851996-12-19 16:35:04 +00004490}
4491
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004492PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004493"getprotobyname(name) -> integer\n\
4494\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004495Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004496
Guido van Rossum3901d851996-12-19 16:35:04 +00004497
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004498#ifndef NO_DUP
4499/* dup() function for socket fds */
4500
4501static PyObject *
4502socket_dup(PyObject *self, PyObject *fdobj)
4503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 SOCKET_T fd, newfd;
4505 PyObject *newfdobj;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004506
4507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508 fd = PyLong_AsSocket_t(fdobj);
4509 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
4510 return NULL;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 newfd = dup_socket(fd);
4513 if (newfd == INVALID_SOCKET)
4514 return set_error();
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004516 newfdobj = PyLong_FromSocket_t(newfd);
4517 if (newfdobj == NULL)
4518 SOCKETCLOSE(newfd);
4519 return newfdobj;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004520}
4521
4522PyDoc_STRVAR(dup_doc,
4523"dup(integer) -> integer\n\
4524\n\
4525Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\
4526sockets; on some platforms os.dup() won't work for socket file descriptors.");
4527#endif
4528
4529
Dave Cole331708b2004-08-09 04:51:41 +00004530#ifdef HAVE_SOCKETPAIR
4531/* Create a pair of sockets using the socketpair() function.
Dave Cole07fda7e2004-08-23 05:16:23 +00004532 Arguments as for socket() except the default family is AF_UNIX if
Dave Colee8bbfe42004-08-26 00:51:16 +00004533 defined on the platform; otherwise, the default is AF_INET. */
Dave Cole331708b2004-08-09 04:51:41 +00004534
4535/*ARGSUSED*/
4536static PyObject *
4537socket_socketpair(PyObject *self, PyObject *args)
4538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 PySocketSockObject *s0 = NULL, *s1 = NULL;
4540 SOCKET_T sv[2];
4541 int family, type = SOCK_STREAM, proto = 0;
4542 PyObject *res = NULL;
Dave Cole331708b2004-08-09 04:51:41 +00004543
4544#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545 family = AF_UNIX;
Dave Cole331708b2004-08-09 04:51:41 +00004546#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004547 family = AF_INET;
Dave Cole331708b2004-08-09 04:51:41 +00004548#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004549 if (!PyArg_ParseTuple(args, "|iii:socketpair",
4550 &family, &type, &proto))
4551 return NULL;
4552 /* Create a pair of socket fds */
4553 if (socketpair(family, type, proto, sv) < 0)
4554 return set_error();
4555 s0 = new_sockobject(sv[0], family, type, proto);
4556 if (s0 == NULL)
4557 goto finally;
4558 s1 = new_sockobject(sv[1], family, type, proto);
4559 if (s1 == NULL)
4560 goto finally;
4561 res = PyTuple_Pack(2, s0, s1);
Dave Cole331708b2004-08-09 04:51:41 +00004562
4563finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004564 if (res == NULL) {
4565 if (s0 == NULL)
4566 SOCKETCLOSE(sv[0]);
4567 if (s1 == NULL)
4568 SOCKETCLOSE(sv[1]);
4569 }
4570 Py_XDECREF(s0);
4571 Py_XDECREF(s1);
4572 return res;
Dave Cole331708b2004-08-09 04:51:41 +00004573}
4574
4575PyDoc_STRVAR(socketpair_doc,
4576"socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\
4577\n\
4578Create a pair of socket objects from the sockets returned by the platform\n\
4579socketpair() function.\n\
Dave Cole07fda7e2004-08-23 05:16:23 +00004580The arguments are the same as for socket() except the default family is\n\
Dave Colee8bbfe42004-08-26 00:51:16 +00004581AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
Dave Cole331708b2004-08-09 04:51:41 +00004582
4583#endif /* HAVE_SOCKETPAIR */
4584
4585
Guido van Rossum006bf911996-06-12 04:04:55 +00004586static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004587socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00004588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00004590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004591 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
4592 return NULL;
4593 }
4594 if (x1 < 0) {
4595 PyErr_SetString(PyExc_OverflowError,
4596 "can't convert negative number to unsigned long");
4597 return NULL;
4598 }
4599 x2 = (unsigned int)ntohs((unsigned short)x1);
4600 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00004601}
4602
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004603PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004604"ntohs(integer) -> integer\n\
4605\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004606Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004607
4608
Guido van Rossum006bf911996-06-12 04:04:55 +00004609static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004610socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00004611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004612 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00004613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004614 if (PyLong_Check(arg)) {
4615 x = PyLong_AsUnsignedLong(arg);
4616 if (x == (unsigned long) -1 && PyErr_Occurred())
4617 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004618#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004619 {
4620 unsigned long y;
4621 /* only want the trailing 32 bits */
4622 y = x & 0xFFFFFFFFUL;
4623 if (y ^ x)
4624 return PyErr_Format(PyExc_OverflowError,
4625 "long int larger than 32 bits");
4626 x = y;
4627 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004628#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 }
4630 else
4631 return PyErr_Format(PyExc_TypeError,
4632 "expected int/long, %s found",
4633 Py_TYPE(arg)->tp_name);
4634 if (x == (unsigned long) -1 && PyErr_Occurred())
4635 return NULL;
4636 return PyLong_FromUnsignedLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00004637}
4638
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004639PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004640"ntohl(integer) -> integer\n\
4641\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004642Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004643
4644
Guido van Rossum006bf911996-06-12 04:04:55 +00004645static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004646socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00004647{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004648 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00004649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004650 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
4651 return NULL;
4652 }
4653 if (x1 < 0) {
4654 PyErr_SetString(PyExc_OverflowError,
4655 "can't convert negative number to unsigned long");
4656 return NULL;
4657 }
4658 x2 = (unsigned int)htons((unsigned short)x1);
4659 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00004660}
4661
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004662PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004663"htons(integer) -> integer\n\
4664\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004665Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004666
4667
Guido van Rossum006bf911996-06-12 04:04:55 +00004668static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004669socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00004670{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004671 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00004672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004673 if (PyLong_Check(arg)) {
4674 x = PyLong_AsUnsignedLong(arg);
4675 if (x == (unsigned long) -1 && PyErr_Occurred())
4676 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004677#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004678 {
4679 unsigned long y;
4680 /* only want the trailing 32 bits */
4681 y = x & 0xFFFFFFFFUL;
4682 if (y ^ x)
4683 return PyErr_Format(PyExc_OverflowError,
4684 "long int larger than 32 bits");
4685 x = y;
4686 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004687#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004688 }
4689 else
4690 return PyErr_Format(PyExc_TypeError,
4691 "expected int/long, %s found",
4692 Py_TYPE(arg)->tp_name);
4693 return PyLong_FromUnsignedLong(htonl((unsigned long)x));
Guido van Rossum006bf911996-06-12 04:04:55 +00004694}
4695
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004696PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00004697"htonl(integer) -> integer\n\
4698\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004699Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004700
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004701/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004702
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004703PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum7d0a8262007-05-21 23:13:11 +00004704"inet_aton(string) -> bytes giving packed 32-bit IP representation\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004705\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00004706Convert 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 +00004707binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004708
4709static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004710socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004711{
Guido van Rossuma2e48551999-09-09 15:42:59 +00004712#ifndef INADDR_NONE
4713#define INADDR_NONE (-1)
4714#endif
Neal Norwitz88f115b2003-02-13 02:15:42 +00004715#ifdef HAVE_INET_ATON
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004716 struct in_addr buf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00004717#endif
4718
4719#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
Benjamin Petersonf91df042009-02-13 02:50:59 +00004720#if (SIZEOF_INT != 4)
4721#error "Not sure if in_addr_t exists and int is not 32-bits."
4722#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004723 /* Have to use inet_addr() instead */
4724 unsigned int packed_addr;
Tim Peters1df9fdd2003-02-13 03:13:40 +00004725#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004726 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00004727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004728 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
4729 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00004730
Tim Peters1df9fdd2003-02-13 03:13:40 +00004731
4732#ifdef HAVE_INET_ATON
Thomas Wouters477c8d52006-05-27 19:21:47 +00004733
4734#ifdef USE_INET_ATON_WEAKLINK
4735 if (inet_aton != NULL) {
4736#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004737 if (inet_aton(ip_addr, &buf))
4738 return PyBytes_FromStringAndSize((char *)(&buf),
4739 sizeof(buf));
Guido van Rossumad05cdf2003-02-12 23:08:22 +00004740
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004741 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004742 "illegal IP address string passed to inet_aton");
4743 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00004744
Thomas Wouters477c8d52006-05-27 19:21:47 +00004745#ifdef USE_INET_ATON_WEAKLINK
4746 } else {
4747#endif
4748
4749#endif
4750
4751#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
4752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004753 /* special-case this address as inet_addr might return INADDR_NONE
4754 * for this */
4755 if (strcmp(ip_addr, "255.255.255.255") == 0) {
4756 packed_addr = 0xFFFFFFFF;
4757 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00004758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004759 packed_addr = inet_addr(ip_addr);
Guido van Rossumc6a164b1999-08-20 19:11:27 +00004760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004761 if (packed_addr == INADDR_NONE) { /* invalid address */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004762 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004763 "illegal IP address string passed to inet_aton");
4764 return NULL;
4765 }
4766 }
4767 return PyBytes_FromStringAndSize((char *) &packed_addr,
4768 sizeof(packed_addr));
Thomas Wouters477c8d52006-05-27 19:21:47 +00004769
4770#ifdef USE_INET_ATON_WEAKLINK
4771 }
4772#endif
4773
Guido van Rossumad05cdf2003-02-12 23:08:22 +00004774#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004775}
4776
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004777PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00004778"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004779\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004780Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004781
4782static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004783socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004785 char *packed_str;
4786 int addr_len;
4787 struct in_addr packed_addr;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004789 if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) {
4790 return NULL;
4791 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00004792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004793 if (addr_len != sizeof(packed_addr)) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004794 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004795 "packed IP wrong length for inet_ntoa");
4796 return NULL;
4797 }
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004799 memcpy(&packed_addr, packed_str, addr_len);
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004801 return PyUnicode_FromString(inet_ntoa(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00004802}
Guido van Rossum82a5c661998-07-07 20:45:43 +00004803
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004804#ifdef HAVE_INET_PTON
4805
4806PyDoc_STRVAR(inet_pton_doc,
4807"inet_pton(af, ip) -> packed IP address string\n\
4808\n\
4809Convert an IP address from string format to a packed string suitable\n\
4810for use with low-level network functions.");
4811
4812static PyObject *
4813socket_inet_pton(PyObject *self, PyObject *args)
4814{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004815 int af;
4816 char* ip;
4817 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00004818#ifdef ENABLE_IPV6
Victor Stinner640c35c2013-06-04 23:14:37 +02004819 char packed[Py_MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00004820#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004821 char packed[sizeof(struct in_addr)];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00004822#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004823 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
4824 return NULL;
4825 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004826
Martin v. Löwis04697e82004-06-02 12:35:29 +00004827#if !defined(ENABLE_IPV6) && defined(AF_INET6)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004828 if(af == AF_INET6) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004829 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004830 "can't use AF_INET6, IPv6 is disabled");
4831 return NULL;
4832 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004833#endif
Martin v. Löwis10649092003-08-05 06:25:06 +00004834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004835 retval = inet_pton(af, ip, packed);
4836 if (retval < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004837 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004838 return NULL;
4839 } else if (retval == 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004840 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004841 "illegal IP address string passed to inet_pton");
4842 return NULL;
4843 } else if (af == AF_INET) {
4844 return PyBytes_FromStringAndSize(packed,
4845 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00004846#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004847 } else if (af == AF_INET6) {
4848 return PyBytes_FromStringAndSize(packed,
4849 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00004850#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004851 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004852 PyErr_SetString(PyExc_OSError, "unknown address family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004853 return NULL;
4854 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004855}
Thomas Wouters477c8d52006-05-27 19:21:47 +00004856
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004857PyDoc_STRVAR(inet_ntop_doc,
4858"inet_ntop(af, packed_ip) -> string formatted IP address\n\
4859\n\
4860Convert a packed IP address of the given family to string format.");
4861
4862static PyObject *
4863socket_inet_ntop(PyObject *self, PyObject *args)
4864{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004865 int af;
4866 char* packed;
4867 int len;
4868 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00004869#ifdef ENABLE_IPV6
Victor Stinner640c35c2013-06-04 23:14:37 +02004870 char ip[Py_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00004871#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004872 char ip[INET_ADDRSTRLEN + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00004873#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00004874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004875 /* Guarantee NUL-termination for PyUnicode_FromString() below */
4876 memset((void *) &ip[0], '\0', sizeof(ip));
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004878 if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) {
4879 return NULL;
4880 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004882 if (af == AF_INET) {
4883 if (len != sizeof(struct in_addr)) {
4884 PyErr_SetString(PyExc_ValueError,
4885 "invalid length of packed IP address string");
4886 return NULL;
4887 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00004888#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004889 } else if (af == AF_INET6) {
4890 if (len != sizeof(struct in6_addr)) {
4891 PyErr_SetString(PyExc_ValueError,
4892 "invalid length of packed IP address string");
4893 return NULL;
4894 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00004895#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004896 } else {
4897 PyErr_Format(PyExc_ValueError,
4898 "unknown address family %d", af);
4899 return NULL;
4900 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004902 retval = inet_ntop(af, packed, ip, sizeof(ip));
4903 if (!retval) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004904 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004905 return NULL;
4906 } else {
4907 return PyUnicode_FromString(retval);
4908 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004910 /* NOTREACHED */
4911 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
4912 return NULL;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004913}
4914
4915#endif /* HAVE_INET_PTON */
4916
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004917/* Python interface to getaddrinfo(host, port). */
4918
4919/*ARGSUSED*/
4920static PyObject *
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00004921socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004922{
Victor Stinner77af1722011-05-26 14:05:59 +02004923 static char* kwnames[] = {"host", "port", "family", "type", "proto",
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00004924 "flags", 0};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004925 struct addrinfo hints, *res;
4926 struct addrinfo *res0 = NULL;
4927 PyObject *hobj = NULL;
4928 PyObject *pobj = (PyObject *)NULL;
4929 char pbuf[30];
4930 char *hptr, *pptr;
4931 int family, socktype, protocol, flags;
4932 int error;
4933 PyObject *all = (PyObject *)NULL;
4934 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004936 family = socktype = protocol = flags = 0;
4937 family = AF_UNSPEC;
Victor Stinner77af1722011-05-26 14:05:59 +02004938 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iiii:getaddrinfo",
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00004939 kwnames, &hobj, &pobj, &family, &socktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004940 &protocol, &flags)) {
4941 return NULL;
4942 }
4943 if (hobj == Py_None) {
4944 hptr = NULL;
4945 } else if (PyUnicode_Check(hobj)) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004946 _Py_IDENTIFIER(encode);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004947
4948 idna = _PyObject_CallMethodId(hobj, &PyId_encode, "s", "idna");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004949 if (!idna)
4950 return NULL;
4951 assert(PyBytes_Check(idna));
4952 hptr = PyBytes_AS_STRING(idna);
4953 } else if (PyBytes_Check(hobj)) {
4954 hptr = PyBytes_AsString(hobj);
4955 } else {
4956 PyErr_SetString(PyExc_TypeError,
4957 "getaddrinfo() argument 1 must be string or None");
4958 return NULL;
4959 }
4960 if (PyLong_CheckExact(pobj)) {
4961 long value = PyLong_AsLong(pobj);
4962 if (value == -1 && PyErr_Occurred())
4963 goto err;
4964 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value);
4965 pptr = pbuf;
4966 } else if (PyUnicode_Check(pobj)) {
4967 pptr = _PyUnicode_AsString(pobj);
Alexander Belopolskye239d232010-12-08 23:31:48 +00004968 if (pptr == NULL)
4969 goto err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004970 } else if (PyBytes_Check(pobj)) {
Alexander Belopolskye239d232010-12-08 23:31:48 +00004971 pptr = PyBytes_AS_STRING(pobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004972 } else if (pobj == Py_None) {
4973 pptr = (char *)NULL;
4974 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02004975 PyErr_SetString(PyExc_OSError, "Int or String expected");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004976 goto err;
4977 }
Ronald Oussorena822d3662013-06-10 10:36:28 +02004978#if defined(__APPLE__) && defined(AI_NUMERICSERV)
Ronald Oussoren27a4ac52013-05-24 13:47:37 +02004979 if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) {
4980 /* On OSX upto at least OSX 10.8 getaddrinfo crashes
4981 * if AI_NUMERICSERV is set and the servname is NULL or "0".
4982 * This workaround avoids a segfault in libsystem.
4983 */
4984 pptr = "00";
4985 }
4986#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004987 memset(&hints, 0, sizeof(hints));
4988 hints.ai_family = family;
4989 hints.ai_socktype = socktype;
4990 hints.ai_protocol = protocol;
4991 hints.ai_flags = flags;
4992 Py_BEGIN_ALLOW_THREADS
4993 ACQUIRE_GETADDRINFO_LOCK
4994 error = getaddrinfo(hptr, pptr, &hints, &res0);
4995 Py_END_ALLOW_THREADS
4996 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
4997 if (error) {
4998 set_gaierror(error);
4999 goto err;
5000 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005002 if ((all = PyList_New(0)) == NULL)
5003 goto err;
5004 for (res = res0; res; res = res->ai_next) {
5005 PyObject *single;
5006 PyObject *addr =
5007 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
5008 if (addr == NULL)
5009 goto err;
5010 single = Py_BuildValue("iiisO", res->ai_family,
5011 res->ai_socktype, res->ai_protocol,
5012 res->ai_canonname ? res->ai_canonname : "",
5013 addr);
5014 Py_DECREF(addr);
5015 if (single == NULL)
5016 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005018 if (PyList_Append(all, single))
5019 goto err;
5020 Py_XDECREF(single);
5021 }
5022 Py_XDECREF(idna);
5023 if (res0)
5024 freeaddrinfo(res0);
5025 return all;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005026 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005027 Py_XDECREF(all);
5028 Py_XDECREF(idna);
5029 if (res0)
5030 freeaddrinfo(res0);
5031 return (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005032}
5033
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005034PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005035"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
5036 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005037\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005038Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005039
5040/* Python interface to getnameinfo(sa, flags). */
5041
5042/*ARGSUSED*/
5043static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005044socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005045{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005046 PyObject *sa = (PyObject *)NULL;
5047 int flags;
5048 char *hostp;
Charles-François Natali366999a2012-01-02 15:47:29 +01005049 int port;
5050 unsigned int flowinfo, scope_id;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005051 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
5052 struct addrinfo hints, *res = NULL;
5053 int error;
5054 PyObject *ret = (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005056 flags = flowinfo = scope_id = 0;
5057 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
5058 return NULL;
5059 if (!PyTuple_Check(sa)) {
5060 PyErr_SetString(PyExc_TypeError,
5061 "getnameinfo() argument 1 must be a tuple");
5062 return NULL;
5063 }
Charles-François Natali366999a2012-01-02 15:47:29 +01005064 if (!PyArg_ParseTuple(sa, "si|II",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005065 &hostp, &port, &flowinfo, &scope_id))
5066 return NULL;
Charles-François Natali9b0c0062012-06-23 10:17:05 +02005067 if (flowinfo > 0xfffff) {
Charles-François Natali366999a2012-01-02 15:47:29 +01005068 PyErr_SetString(PyExc_OverflowError,
5069 "getsockaddrarg: flowinfo must be 0-1048575.");
5070 return NULL;
5071 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005072 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
5073 memset(&hints, 0, sizeof(hints));
5074 hints.ai_family = AF_UNSPEC;
5075 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Martin v. Löwis112c0f32010-08-25 07:38:15 +00005076 hints.ai_flags = AI_NUMERICHOST; /* don't do any name resolution */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005077 Py_BEGIN_ALLOW_THREADS
5078 ACQUIRE_GETADDRINFO_LOCK
5079 error = getaddrinfo(hostp, pbuf, &hints, &res);
5080 Py_END_ALLOW_THREADS
5081 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
5082 if (error) {
5083 set_gaierror(error);
5084 goto fail;
5085 }
5086 if (res->ai_next) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005087 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005088 "sockaddr resolved to multiple addresses");
5089 goto fail;
5090 }
5091 switch (res->ai_family) {
5092 case AF_INET:
5093 {
5094 if (PyTuple_GET_SIZE(sa) != 2) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005095 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005096 "IPv4 sockaddr must be 2 tuple");
5097 goto fail;
5098 }
5099 break;
5100 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00005101#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005102 case AF_INET6:
5103 {
5104 struct sockaddr_in6 *sin6;
5105 sin6 = (struct sockaddr_in6 *)res->ai_addr;
Charles-François Natali366999a2012-01-02 15:47:29 +01005106 sin6->sin6_flowinfo = htonl(flowinfo);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005107 sin6->sin6_scope_id = scope_id;
5108 break;
5109 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005110#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005111 }
Antoine Pitrou19467d22010-08-17 19:33:30 +00005112 error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005113 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
5114 if (error) {
5115 set_gaierror(error);
5116 goto fail;
5117 }
5118 ret = Py_BuildValue("ss", hbuf, pbuf);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005119
5120fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005121 if (res)
5122 freeaddrinfo(res);
5123 return ret;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005124}
5125
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005126PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005127"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005128\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005129Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005130
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005131
5132/* Python API to getting and setting the default timeout value. */
5133
5134static PyObject *
5135socket_getdefaulttimeout(PyObject *self)
5136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005137 if (defaulttimeout < 0.0) {
5138 Py_INCREF(Py_None);
5139 return Py_None;
5140 }
5141 else
5142 return PyFloat_FromDouble(defaulttimeout);
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005143}
5144
5145PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005146"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005147\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03005148Returns the default timeout in seconds (float) for new socket objects.\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005149A value of None indicates that new socket objects have no timeout.\n\
5150When the socket module is first imported, the default is None.");
5151
5152static PyObject *
5153socket_setdefaulttimeout(PyObject *self, PyObject *arg)
5154{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005155 double timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005157 if (arg == Py_None)
5158 timeout = -1.0;
5159 else {
5160 timeout = PyFloat_AsDouble(arg);
5161 if (timeout < 0.0) {
5162 if (!PyErr_Occurred())
5163 PyErr_SetString(PyExc_ValueError,
5164 "Timeout value out of range");
5165 return NULL;
5166 }
5167 }
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005169 defaulttimeout = timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005171 Py_INCREF(Py_None);
5172 return Py_None;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005173}
5174
5175PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005176"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005177\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03005178Set the default timeout in seconds (float) for new socket objects.\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005179A value of None indicates that new socket objects have no timeout.\n\
5180When the socket module is first imported, the default is None.");
5181
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005182#ifdef HAVE_IF_NAMEINDEX
5183/* Python API for getting interface indices and names */
5184
5185static PyObject *
5186socket_if_nameindex(PyObject *self, PyObject *arg)
5187{
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005188 PyObject *list;
Charles-François Natali60713592011-05-20 16:55:06 +02005189 int i;
5190 struct if_nameindex *ni;
Victor Stinner77af1722011-05-26 14:05:59 +02005191
Charles-François Natali60713592011-05-20 16:55:06 +02005192 ni = if_nameindex();
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005193 if (ni == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005194 PyErr_SetFromErrno(PyExc_OSError);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005195 return NULL;
5196 }
5197
5198 list = PyList_New(0);
5199 if (list == NULL) {
5200 if_freenameindex(ni);
5201 return NULL;
5202 }
5203
Charles-François Natali60713592011-05-20 16:55:06 +02005204 for (i = 0; ni[i].if_index != 0 && i < INT_MAX; i++) {
5205 PyObject *ni_tuple = Py_BuildValue("IO&",
5206 ni[i].if_index, PyUnicode_DecodeFSDefault, ni[i].if_name);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005207
5208 if (ni_tuple == NULL || PyList_Append(list, ni_tuple) == -1) {
5209 Py_XDECREF(ni_tuple);
Charles-François Natali60713592011-05-20 16:55:06 +02005210 Py_DECREF(list);
5211 if_freenameindex(ni);
5212 return NULL;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005213 }
5214 Py_DECREF(ni_tuple);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005215 }
5216
5217 if_freenameindex(ni);
5218 return list;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005219}
5220
5221PyDoc_STRVAR(if_nameindex_doc,
5222"if_nameindex()\n\
5223\n\
5224Returns a list of network interface information (index, name) tuples.");
5225
Charles-François Natali60713592011-05-20 16:55:06 +02005226static PyObject *
5227socket_if_nametoindex(PyObject *self, PyObject *args)
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005228{
Charles-François Natali60713592011-05-20 16:55:06 +02005229 PyObject *oname;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005230 unsigned long index;
5231
Charles-François Natali60713592011-05-20 16:55:06 +02005232 if (!PyArg_ParseTuple(args, "O&:if_nametoindex",
5233 PyUnicode_FSConverter, &oname))
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005234 return NULL;
5235
Charles-François Natali60713592011-05-20 16:55:06 +02005236 index = if_nametoindex(PyBytes_AS_STRING(oname));
5237 Py_DECREF(oname);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005238 if (index == 0) {
Charles-François Natali60713592011-05-20 16:55:06 +02005239 /* if_nametoindex() doesn't set errno */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005240 PyErr_SetString(PyExc_OSError, "no interface with this name");
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005241 return NULL;
5242 }
5243
5244 return PyLong_FromUnsignedLong(index);
5245}
5246
5247PyDoc_STRVAR(if_nametoindex_doc,
5248"if_nametoindex(if_name)\n\
5249\n\
5250Returns the interface index corresponding to the interface name if_name.");
5251
Charles-François Natali60713592011-05-20 16:55:06 +02005252static PyObject *
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005253socket_if_indextoname(PyObject *self, PyObject *arg)
5254{
Charles-François Natali60713592011-05-20 16:55:06 +02005255 unsigned long index;
5256 char name[IF_NAMESIZE + 1];
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005257
Charles-François Natali60713592011-05-20 16:55:06 +02005258 index = PyLong_AsUnsignedLong(arg);
5259 if (index == (unsigned long) -1)
5260 return NULL;
5261
5262 if (if_indextoname(index, name) == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005263 PyErr_SetFromErrno(PyExc_OSError);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005264 return NULL;
5265 }
5266
Charles-François Natali60713592011-05-20 16:55:06 +02005267 return PyUnicode_DecodeFSDefault(name);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005268}
5269
5270PyDoc_STRVAR(if_indextoname_doc,
5271"if_indextoname(if_index)\n\
5272\n\
5273Returns the interface name corresponding to the interface index if_index.");
5274
5275#endif /* HAVE_IF_NAMEINDEX */
5276
5277
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005278#ifdef CMSG_LEN
5279/* Python interface to CMSG_LEN(length). */
5280
5281static PyObject *
5282socket_CMSG_LEN(PyObject *self, PyObject *args)
5283{
5284 Py_ssize_t length;
5285 size_t result;
5286
5287 if (!PyArg_ParseTuple(args, "n:CMSG_LEN", &length))
5288 return NULL;
5289 if (length < 0 || !get_CMSG_LEN(length, &result)) {
5290 PyErr_Format(PyExc_OverflowError, "CMSG_LEN() argument out of range");
5291 return NULL;
5292 }
5293 return PyLong_FromSize_t(result);
5294}
5295
5296PyDoc_STRVAR(CMSG_LEN_doc,
5297"CMSG_LEN(length) -> control message length\n\
5298\n\
5299Return the total length, without trailing padding, of an ancillary\n\
5300data item with associated data of the given length. This value can\n\
5301often be used as the buffer size for recvmsg() to receive a single\n\
5302item of ancillary data, but RFC 3542 requires portable applications to\n\
5303use CMSG_SPACE() and thus include space for padding, even when the\n\
5304item will be the last in the buffer. Raises OverflowError if length\n\
5305is outside the permissible range of values.");
5306
5307
5308#ifdef CMSG_SPACE
5309/* Python interface to CMSG_SPACE(length). */
5310
5311static PyObject *
5312socket_CMSG_SPACE(PyObject *self, PyObject *args)
5313{
5314 Py_ssize_t length;
5315 size_t result;
5316
5317 if (!PyArg_ParseTuple(args, "n:CMSG_SPACE", &length))
5318 return NULL;
5319 if (length < 0 || !get_CMSG_SPACE(length, &result)) {
5320 PyErr_SetString(PyExc_OverflowError,
5321 "CMSG_SPACE() argument out of range");
5322 return NULL;
5323 }
5324 return PyLong_FromSize_t(result);
5325}
5326
5327PyDoc_STRVAR(CMSG_SPACE_doc,
5328"CMSG_SPACE(length) -> buffer size\n\
5329\n\
5330Return the buffer size needed for recvmsg() to receive an ancillary\n\
5331data item with associated data of the given length, along with any\n\
5332trailing padding. The buffer space needed to receive multiple items\n\
5333is the sum of the CMSG_SPACE() values for their associated data\n\
5334lengths. Raises OverflowError if length is outside the permissible\n\
5335range of values.");
5336#endif /* CMSG_SPACE */
5337#endif /* CMSG_LEN */
5338
5339
Guido van Rossum30a685f1991-06-27 15:51:29 +00005340/* List of functions exported by this module. */
5341
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005342static PyMethodDef socket_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005343 {"gethostbyname", socket_gethostbyname,
5344 METH_VARARGS, gethostbyname_doc},
5345 {"gethostbyname_ex", socket_gethostbyname_ex,
5346 METH_VARARGS, ghbn_ex_doc},
5347 {"gethostbyaddr", socket_gethostbyaddr,
5348 METH_VARARGS, gethostbyaddr_doc},
5349 {"gethostname", socket_gethostname,
5350 METH_NOARGS, gethostname_doc},
Antoine Pitrou061cfb52011-02-28 22:25:22 +00005351#ifdef HAVE_SETHOSTNAME
5352 {"sethostname", socket_sethostname,
5353 METH_VARARGS, sethostname_doc},
5354#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005355 {"getservbyname", socket_getservbyname,
5356 METH_VARARGS, getservbyname_doc},
5357 {"getservbyport", socket_getservbyport,
5358 METH_VARARGS, getservbyport_doc},
5359 {"getprotobyname", socket_getprotobyname,
5360 METH_VARARGS, getprotobyname_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005361#ifndef NO_DUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005362 {"dup", socket_dup,
5363 METH_O, dup_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005364#endif
Dave Cole331708b2004-08-09 04:51:41 +00005365#ifdef HAVE_SOCKETPAIR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005366 {"socketpair", socket_socketpair,
5367 METH_VARARGS, socketpair_doc},
Dave Cole331708b2004-08-09 04:51:41 +00005368#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005369 {"ntohs", socket_ntohs,
5370 METH_VARARGS, ntohs_doc},
5371 {"ntohl", socket_ntohl,
5372 METH_O, ntohl_doc},
5373 {"htons", socket_htons,
5374 METH_VARARGS, htons_doc},
5375 {"htonl", socket_htonl,
5376 METH_O, htonl_doc},
5377 {"inet_aton", socket_inet_aton,
5378 METH_VARARGS, inet_aton_doc},
5379 {"inet_ntoa", socket_inet_ntoa,
5380 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005381#ifdef HAVE_INET_PTON
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005382 {"inet_pton", socket_inet_pton,
5383 METH_VARARGS, inet_pton_doc},
5384 {"inet_ntop", socket_inet_ntop,
5385 METH_VARARGS, inet_ntop_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005386#endif
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005387 {"getaddrinfo", (PyCFunction)socket_getaddrinfo,
5388 METH_VARARGS | METH_KEYWORDS, getaddrinfo_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005389 {"getnameinfo", socket_getnameinfo,
5390 METH_VARARGS, getnameinfo_doc},
5391 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
5392 METH_NOARGS, getdefaulttimeout_doc},
5393 {"setdefaulttimeout", socket_setdefaulttimeout,
5394 METH_O, setdefaulttimeout_doc},
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005395#ifdef HAVE_IF_NAMEINDEX
5396 {"if_nameindex", socket_if_nameindex,
5397 METH_NOARGS, if_nameindex_doc},
5398 {"if_nametoindex", socket_if_nametoindex,
Charles-François Natali60713592011-05-20 16:55:06 +02005399 METH_VARARGS, if_nametoindex_doc},
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005400 {"if_indextoname", socket_if_indextoname,
5401 METH_O, if_indextoname_doc},
5402#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005403#ifdef CMSG_LEN
5404 {"CMSG_LEN", socket_CMSG_LEN,
5405 METH_VARARGS, CMSG_LEN_doc},
5406#ifdef CMSG_SPACE
5407 {"CMSG_SPACE", socket_CMSG_SPACE,
5408 METH_VARARGS, CMSG_SPACE_doc},
5409#endif
5410#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005411 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005412};
5413
Guido van Rossum30a685f1991-06-27 15:51:29 +00005414
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005415#ifdef MS_WINDOWS
5416#define OS_INIT_DEFINED
5417
5418/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00005419
5420static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005421os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00005422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005423 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00005424}
5425
5426static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005427os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00005428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005429 WSADATA WSAData;
5430 int ret;
5431 ret = WSAStartup(0x0101, &WSAData);
5432 switch (ret) {
5433 case 0: /* No error */
5434 Py_AtExit(os_cleanup);
5435 return 1; /* Success */
5436 case WSASYSNOTREADY:
5437 PyErr_SetString(PyExc_ImportError,
5438 "WSAStartup failed: network not ready");
5439 break;
5440 case WSAVERNOTSUPPORTED:
5441 case WSAEINVAL:
5442 PyErr_SetString(
5443 PyExc_ImportError,
5444 "WSAStartup failed: requested version not supported");
5445 break;
5446 default:
5447 PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret);
5448 break;
5449 }
5450 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00005451}
5452
Guido van Rossum8d665e61996-06-26 18:22:49 +00005453#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00005454
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005455
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005456
5457#ifndef OS_INIT_DEFINED
5458static int
5459os_init(void)
5460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005461 return 1; /* Success */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005462}
5463#endif
5464
5465
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00005466/* C API table - always add new things to the end for binary
5467 compatibility. */
5468static
5469PySocketModule_APIObject PySocketModuleAPI =
5470{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005471 &sock_type,
Antoine Pitrouc4df7842010-12-03 19:59:41 +00005472 NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005473 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00005474};
5475
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005476
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005477/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005478
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005479 This module is actually called "_socket", and there's a wrapper
Guido van Rossum7d0a8262007-05-21 23:13:11 +00005480 "socket.py" which implements some additional functionality.
5481 The import of "_socket" may fail with an ImportError exception if
5482 os-specific initialization fails. On Windows, this does WINSOCK
Ezio Melotti13925002011-03-16 11:05:33 +02005483 initialization. When WINSOCK is initialized successfully, a call to
Guido van Rossum7d0a8262007-05-21 23:13:11 +00005484 WSACleanup() is scheduled to be made at exit time.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005485*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005486
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005487PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005488"Implementation module for socket operations.\n\
5489\n\
5490See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00005491
Martin v. Löwis1a214512008-06-11 05:26:20 +00005492static struct PyModuleDef socketmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005493 PyModuleDef_HEAD_INIT,
5494 PySocket_MODULE_NAME,
5495 socket_doc,
5496 -1,
5497 socket_methods,
5498 NULL,
5499 NULL,
5500 NULL,
5501 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00005502};
5503
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005504PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00005505PyInit__socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005507 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00005508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005509 if (!os_init())
5510 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005512 Py_TYPE(&sock_type) = &PyType_Type;
5513 m = PyModule_Create(&socketmodule);
5514 if (m == NULL)
5515 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005516
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005517 Py_INCREF(PyExc_OSError);
5518 PySocketModuleAPI.error = PyExc_OSError;
5519 Py_INCREF(PyExc_OSError);
5520 PyModule_AddObject(m, "error", PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005521 socket_herror = PyErr_NewException("socket.herror",
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005522 PyExc_OSError, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005523 if (socket_herror == NULL)
5524 return NULL;
5525 Py_INCREF(socket_herror);
5526 PyModule_AddObject(m, "herror", socket_herror);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005527 socket_gaierror = PyErr_NewException("socket.gaierror", PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005528 NULL);
5529 if (socket_gaierror == NULL)
5530 return NULL;
5531 Py_INCREF(socket_gaierror);
5532 PyModule_AddObject(m, "gaierror", socket_gaierror);
5533 socket_timeout = PyErr_NewException("socket.timeout",
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005534 PyExc_OSError, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005535 if (socket_timeout == NULL)
5536 return NULL;
Antoine Pitrouc4df7842010-12-03 19:59:41 +00005537 PySocketModuleAPI.timeout_error = socket_timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005538 Py_INCREF(socket_timeout);
5539 PyModule_AddObject(m, "timeout", socket_timeout);
5540 Py_INCREF((PyObject *)&sock_type);
5541 if (PyModule_AddObject(m, "SocketType",
5542 (PyObject *)&sock_type) != 0)
5543 return NULL;
5544 Py_INCREF((PyObject *)&sock_type);
5545 if (PyModule_AddObject(m, "socket",
5546 (PyObject *)&sock_type) != 0)
5547 return NULL;
Guido van Rossum09be4091999-08-09 14:40:40 +00005548
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005549#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005550 has_ipv6 = Py_True;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005551#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005552 has_ipv6 = Py_False;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005553#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005554 Py_INCREF(has_ipv6);
5555 PyModule_AddObject(m, "has_ipv6", has_ipv6);
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005557 /* Export C API */
5558 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
5559 PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL)
5560 ) != 0)
5561 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00005562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005563 /* Address families (we only support AF_INET and AF_UNIX) */
Guido van Rossum09be4091999-08-09 14:40:40 +00005564#ifdef AF_UNSPEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005565 PyModule_AddIntMacro(m, AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00005566#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005567 PyModule_AddIntMacro(m, AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005568#ifdef AF_INET6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005569 PyModule_AddIntMacro(m, AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005570#endif /* AF_INET6 */
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00005571#if defined(AF_UNIX)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005572 PyModule_AddIntMacro(m, AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005573#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00005574#ifdef AF_AX25
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005575 /* Amateur Radio AX.25 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005576 PyModule_AddIntMacro(m, AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00005577#endif
5578#ifdef AF_IPX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005579 PyModule_AddIntMacro(m, AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00005580#endif
5581#ifdef AF_APPLETALK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005582 /* Appletalk DDP */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005583 PyModule_AddIntMacro(m, AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00005584#endif
5585#ifdef AF_NETROM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005586 /* Amateur radio NetROM */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005587 PyModule_AddIntMacro(m, AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00005588#endif
5589#ifdef AF_BRIDGE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005590 /* Multiprotocol bridge */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005591 PyModule_AddIntMacro(m, AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00005592#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005593#ifdef AF_ATMPVC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005594 /* ATM PVCs */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005595 PyModule_AddIntMacro(m, AF_ATMPVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005596#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00005597#ifdef AF_AAL5
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005598 /* Reserved for Werner's ATM */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005599 PyModule_AddIntMacro(m, AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00005600#endif
5601#ifdef AF_X25
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005602 /* Reserved for X.25 project */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005603 PyModule_AddIntMacro(m, AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00005604#endif
5605#ifdef AF_INET6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005606 PyModule_AddIntMacro(m, AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00005607#endif
5608#ifdef AF_ROSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005609 /* Amateur Radio X.25 PLP */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005610 PyModule_AddIntMacro(m, AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00005611#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005612#ifdef AF_DECnet
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005613 /* Reserved for DECnet project */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005614 PyModule_AddIntMacro(m, AF_DECnet);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005615#endif
5616#ifdef AF_NETBEUI
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005617 /* Reserved for 802.2LLC project */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005618 PyModule_AddIntMacro(m, AF_NETBEUI);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005619#endif
5620#ifdef AF_SECURITY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005621 /* Security callback pseudo AF */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005622 PyModule_AddIntMacro(m, AF_SECURITY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005623#endif
5624#ifdef AF_KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005625 /* PF_KEY key management API */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005626 PyModule_AddIntMacro(m, AF_KEY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005627#endif
5628#ifdef AF_NETLINK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005629 /* */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005630 PyModule_AddIntMacro(m, AF_NETLINK);
5631 PyModule_AddIntMacro(m, NETLINK_ROUTE);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005632#ifdef NETLINK_SKIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005633 PyModule_AddIntMacro(m, NETLINK_SKIP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005634#endif
5635#ifdef NETLINK_W1
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005636 PyModule_AddIntMacro(m, NETLINK_W1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005637#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005638 PyModule_AddIntMacro(m, NETLINK_USERSOCK);
5639 PyModule_AddIntMacro(m, NETLINK_FIREWALL);
Guido van Rossum668a94a2006-02-21 01:07:27 +00005640#ifdef NETLINK_TCPDIAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005641 PyModule_AddIntMacro(m, NETLINK_TCPDIAG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00005642#endif
5643#ifdef NETLINK_NFLOG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005644 PyModule_AddIntMacro(m, NETLINK_NFLOG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00005645#endif
Neal Norwitz65851662006-01-16 04:31:40 +00005646#ifdef NETLINK_XFRM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005647 PyModule_AddIntMacro(m, NETLINK_XFRM);
Neal Norwitz65851662006-01-16 04:31:40 +00005648#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005649#ifdef NETLINK_ARPD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005650 PyModule_AddIntMacro(m, NETLINK_ARPD);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005651#endif
5652#ifdef NETLINK_ROUTE6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005653 PyModule_AddIntMacro(m, NETLINK_ROUTE6);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005654#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005655 PyModule_AddIntMacro(m, NETLINK_IP6_FW);
Thomas Wouterscf297e42007-02-23 15:07:44 +00005656#ifdef NETLINK_DNRTMSG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005657 PyModule_AddIntMacro(m, NETLINK_DNRTMSG);
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005658#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005659#ifdef NETLINK_TAPBASE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005660 PyModule_AddIntMacro(m, NETLINK_TAPBASE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005661#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005662#endif /* AF_NETLINK */
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005663#ifdef AF_ROUTE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005664 /* Alias to emulate 4.4BSD */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005665 PyModule_AddIntMacro(m, AF_ROUTE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005666#endif
Giampaolo Rodola'80e1c432013-05-21 21:02:04 +02005667#ifdef AF_LINK
5668 PyModule_AddIntMacro(m, AF_LINK);
5669#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005670#ifdef AF_ASH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005671 /* Ash */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005672 PyModule_AddIntMacro(m, AF_ASH);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005673#endif
5674#ifdef AF_ECONET
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005675 /* Acorn Econet */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005676 PyModule_AddIntMacro(m, AF_ECONET);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005677#endif
5678#ifdef AF_ATMSVC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005679 /* ATM SVCs */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005680 PyModule_AddIntMacro(m, AF_ATMSVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005681#endif
5682#ifdef AF_SNA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005683 /* Linux SNA Project (nutters!) */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005684 PyModule_AddIntMacro(m, AF_SNA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005685#endif
5686#ifdef AF_IRDA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005687 /* IRDA sockets */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005688 PyModule_AddIntMacro(m, AF_IRDA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005689#endif
5690#ifdef AF_PPPOX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005691 /* PPPoX sockets */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005692 PyModule_AddIntMacro(m, AF_PPPOX);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005693#endif
5694#ifdef AF_WANPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005695 /* Wanpipe API Sockets */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005696 PyModule_AddIntMacro(m, AF_WANPIPE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005697#endif
5698#ifdef AF_LLC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005699 /* Linux LLC */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005700 PyModule_AddIntMacro(m, AF_LLC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005701#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00005702
Hye-Shik Chang81268602004-02-02 06:05:24 +00005703#ifdef USE_BLUETOOTH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005704 PyModule_AddIntMacro(m, AF_BLUETOOTH);
5705 PyModule_AddIntMacro(m, BTPROTO_L2CAP);
5706 PyModule_AddIntMacro(m, BTPROTO_HCI);
5707 PyModule_AddIntMacro(m, SOL_HCI);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00005708#if !defined(__NetBSD__) && !defined(__DragonFly__)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005709 PyModule_AddIntMacro(m, HCI_FILTER);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00005710#endif
Hye-Shik Chang81268602004-02-02 06:05:24 +00005711#if !defined(__FreeBSD__)
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00005712#if !defined(__NetBSD__) && !defined(__DragonFly__)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005713 PyModule_AddIntMacro(m, HCI_TIME_STAMP);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00005714#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005715 PyModule_AddIntMacro(m, HCI_DATA_DIR);
5716 PyModule_AddIntMacro(m, BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00005717#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005718 PyModule_AddIntMacro(m, BTPROTO_RFCOMM);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005719 PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
5720 PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
Martin v. Löwis12af0482004-01-31 12:34:17 +00005721#endif
5722
Charles-François Natali47413c12011-10-06 19:47:44 +02005723#ifdef AF_CAN
5724 /* Controller Area Network */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005725 PyModule_AddIntMacro(m, AF_CAN);
Charles-François Natali47413c12011-10-06 19:47:44 +02005726#endif
5727#ifdef PF_CAN
5728 /* Controller Area Network */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005729 PyModule_AddIntMacro(m, PF_CAN);
Charles-François Natali47413c12011-10-06 19:47:44 +02005730#endif
5731
Charles-François Natali10b8cf42011-11-10 19:21:37 +01005732/* Reliable Datagram Sockets */
5733#ifdef AF_RDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005734 PyModule_AddIntMacro(m, AF_RDS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01005735#endif
5736#ifdef PF_RDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005737 PyModule_AddIntMacro(m, PF_RDS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01005738#endif
5739
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01005740/* Kernel event messages */
5741#ifdef PF_SYSTEM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005742 PyModule_AddIntMacro(m, PF_SYSTEM);
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01005743#endif
5744#ifdef AF_SYSTEM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005745 PyModule_AddIntMacro(m, AF_SYSTEM);
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01005746#endif
5747
Antoine Pitroub156a462010-10-27 20:13:57 +00005748#ifdef AF_PACKET
5749 PyModule_AddIntMacro(m, AF_PACKET);
5750#endif
5751#ifdef PF_PACKET
5752 PyModule_AddIntMacro(m, PF_PACKET);
5753#endif
5754#ifdef PACKET_HOST
5755 PyModule_AddIntMacro(m, PACKET_HOST);
5756#endif
5757#ifdef PACKET_BROADCAST
5758 PyModule_AddIntMacro(m, PACKET_BROADCAST);
5759#endif
5760#ifdef PACKET_MULTICAST
5761 PyModule_AddIntMacro(m, PACKET_MULTICAST);
5762#endif
5763#ifdef PACKET_OTHERHOST
5764 PyModule_AddIntMacro(m, PACKET_OTHERHOST);
5765#endif
5766#ifdef PACKET_OUTGOING
5767 PyModule_AddIntMacro(m, PACKET_OUTGOING);
5768#endif
5769#ifdef PACKET_LOOPBACK
5770 PyModule_AddIntMacro(m, PACKET_LOOPBACK);
5771#endif
5772#ifdef PACKET_FASTROUTE
5773 PyModule_AddIntMacro(m, PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00005774#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00005775
Christian Heimes043d6f62008-01-07 17:19:16 +00005776#ifdef HAVE_LINUX_TIPC_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005777 PyModule_AddIntMacro(m, AF_TIPC);
Christian Heimes043d6f62008-01-07 17:19:16 +00005778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005779 /* for addresses */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005780 PyModule_AddIntMacro(m, TIPC_ADDR_NAMESEQ);
5781 PyModule_AddIntMacro(m, TIPC_ADDR_NAME);
5782 PyModule_AddIntMacro(m, TIPC_ADDR_ID);
Christian Heimes043d6f62008-01-07 17:19:16 +00005783
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005784 PyModule_AddIntMacro(m, TIPC_ZONE_SCOPE);
5785 PyModule_AddIntMacro(m, TIPC_CLUSTER_SCOPE);
5786 PyModule_AddIntMacro(m, TIPC_NODE_SCOPE);
Christian Heimes043d6f62008-01-07 17:19:16 +00005787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005788 /* for setsockopt() */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005789 PyModule_AddIntMacro(m, SOL_TIPC);
5790 PyModule_AddIntMacro(m, TIPC_IMPORTANCE);
5791 PyModule_AddIntMacro(m, TIPC_SRC_DROPPABLE);
5792 PyModule_AddIntMacro(m, TIPC_DEST_DROPPABLE);
5793 PyModule_AddIntMacro(m, TIPC_CONN_TIMEOUT);
Christian Heimes043d6f62008-01-07 17:19:16 +00005794
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005795 PyModule_AddIntMacro(m, TIPC_LOW_IMPORTANCE);
5796 PyModule_AddIntMacro(m, TIPC_MEDIUM_IMPORTANCE);
5797 PyModule_AddIntMacro(m, TIPC_HIGH_IMPORTANCE);
5798 PyModule_AddIntMacro(m, TIPC_CRITICAL_IMPORTANCE);
Christian Heimes043d6f62008-01-07 17:19:16 +00005799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005800 /* for subscriptions */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005801 PyModule_AddIntMacro(m, TIPC_SUB_PORTS);
5802 PyModule_AddIntMacro(m, TIPC_SUB_SERVICE);
Christian Heimes25bb7832008-01-11 16:17:00 +00005803#ifdef TIPC_SUB_CANCEL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005804 /* doesn't seem to be available everywhere */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005805 PyModule_AddIntMacro(m, TIPC_SUB_CANCEL);
Christian Heimes25bb7832008-01-11 16:17:00 +00005806#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005807 PyModule_AddIntMacro(m, TIPC_WAIT_FOREVER);
5808 PyModule_AddIntMacro(m, TIPC_PUBLISHED);
5809 PyModule_AddIntMacro(m, TIPC_WITHDRAWN);
5810 PyModule_AddIntMacro(m, TIPC_SUBSCR_TIMEOUT);
5811 PyModule_AddIntMacro(m, TIPC_CFG_SRV);
5812 PyModule_AddIntMacro(m, TIPC_TOP_SRV);
Christian Heimes043d6f62008-01-07 17:19:16 +00005813#endif
5814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005815 /* Socket types */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005816 PyModule_AddIntMacro(m, SOCK_STREAM);
5817 PyModule_AddIntMacro(m, SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00005818/* We have incomplete socket support. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005819 PyModule_AddIntMacro(m, SOCK_RAW);
5820 PyModule_AddIntMacro(m, SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00005821#if defined(SOCK_RDM)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005822 PyModule_AddIntMacro(m, SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00005823#endif
Antoine Pitroub1c54962010-10-14 15:05:38 +00005824#ifdef SOCK_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005825 PyModule_AddIntMacro(m, SOCK_CLOEXEC);
Antoine Pitroub1c54962010-10-14 15:05:38 +00005826#endif
5827#ifdef SOCK_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005828 PyModule_AddIntMacro(m, SOCK_NONBLOCK);
Antoine Pitroub1c54962010-10-14 15:05:38 +00005829#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005831#ifdef SO_DEBUG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005832 PyModule_AddIntMacro(m, SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005833#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005834#ifdef SO_ACCEPTCONN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005835 PyModule_AddIntMacro(m, SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005836#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005837#ifdef SO_REUSEADDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005838 PyModule_AddIntMacro(m, SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005839#endif
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00005840#ifdef SO_EXCLUSIVEADDRUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005841 PyModule_AddIntMacro(m, SO_EXCLUSIVEADDRUSE);
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00005842#endif
5843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005844#ifdef SO_KEEPALIVE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005845 PyModule_AddIntMacro(m, SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005846#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005847#ifdef SO_DONTROUTE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005848 PyModule_AddIntMacro(m, SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005849#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005850#ifdef SO_BROADCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005851 PyModule_AddIntMacro(m, SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005852#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005853#ifdef SO_USELOOPBACK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005854 PyModule_AddIntMacro(m, SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005855#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005856#ifdef SO_LINGER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005857 PyModule_AddIntMacro(m, SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005858#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005859#ifdef SO_OOBINLINE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005860 PyModule_AddIntMacro(m, SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005861#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005862#ifdef SO_REUSEPORT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005863 PyModule_AddIntMacro(m, SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005864#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005865#ifdef SO_SNDBUF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005866 PyModule_AddIntMacro(m, SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005867#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005868#ifdef SO_RCVBUF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005869 PyModule_AddIntMacro(m, SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005870#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005871#ifdef SO_SNDLOWAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005872 PyModule_AddIntMacro(m, SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005873#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005874#ifdef SO_RCVLOWAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005875 PyModule_AddIntMacro(m, SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005876#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005877#ifdef SO_SNDTIMEO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005878 PyModule_AddIntMacro(m, SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005879#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005880#ifdef SO_RCVTIMEO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005881 PyModule_AddIntMacro(m, SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005882#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005883#ifdef SO_ERROR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005884 PyModule_AddIntMacro(m, SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005885#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005886#ifdef SO_TYPE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005887 PyModule_AddIntMacro(m, SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005888#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005889#ifdef SO_SETFIB
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005890 PyModule_AddIntMacro(m, SO_SETFIB);
Larry Hastingsf0f37952010-04-02 11:47:10 +00005891#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005892#ifdef SO_PASSCRED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005893 PyModule_AddIntMacro(m, SO_PASSCRED);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005894#endif
5895#ifdef SO_PEERCRED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005896 PyModule_AddIntMacro(m, SO_PEERCRED);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005897#endif
5898#ifdef LOCAL_PEERCRED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005899 PyModule_AddIntMacro(m, LOCAL_PEERCRED);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005900#endif
Victor Stinnercb98bed2011-10-25 13:45:48 +02005901#ifdef SO_BINDTODEVICE
5902 PyModule_AddIntMacro(m, SO_BINDTODEVICE);
5903#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005905 /* Maximum number of connections for "listen" */
5906#ifdef SOMAXCONN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005907 PyModule_AddIntMacro(m, SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005908#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005909 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005910#endif
5911
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005912 /* Ancilliary message types */
5913#ifdef SCM_RIGHTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005914 PyModule_AddIntMacro(m, SCM_RIGHTS);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005915#endif
5916#ifdef SCM_CREDENTIALS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005917 PyModule_AddIntMacro(m, SCM_CREDENTIALS);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005918#endif
5919#ifdef SCM_CREDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005920 PyModule_AddIntMacro(m, SCM_CREDS);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005921#endif
5922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005923 /* Flags for send, recv */
5924#ifdef MSG_OOB
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005925 PyModule_AddIntMacro(m, MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005926#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005927#ifdef MSG_PEEK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005928 PyModule_AddIntMacro(m, MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005929#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005930#ifdef MSG_DONTROUTE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005931 PyModule_AddIntMacro(m, MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005932#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005933#ifdef MSG_DONTWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005934 PyModule_AddIntMacro(m, MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00005935#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005936#ifdef MSG_EOR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005937 PyModule_AddIntMacro(m, MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005938#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005939#ifdef MSG_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005940 PyModule_AddIntMacro(m, MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005941#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005942#ifdef MSG_CTRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005943 PyModule_AddIntMacro(m, MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005944#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005945#ifdef MSG_WAITALL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005946 PyModule_AddIntMacro(m, MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005947#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005948#ifdef MSG_BTAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005949 PyModule_AddIntMacro(m, MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005950#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005951#ifdef MSG_ETAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005952 PyModule_AddIntMacro(m, MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005953#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005954#ifdef MSG_NOSIGNAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005955 PyModule_AddIntMacro(m, MSG_NOSIGNAL);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005956#endif
5957#ifdef MSG_NOTIFICATION
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005958 PyModule_AddIntMacro(m, MSG_NOTIFICATION);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005959#endif
5960#ifdef MSG_CMSG_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005961 PyModule_AddIntMacro(m, MSG_CMSG_CLOEXEC);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005962#endif
5963#ifdef MSG_ERRQUEUE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005964 PyModule_AddIntMacro(m, MSG_ERRQUEUE);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005965#endif
5966#ifdef MSG_CONFIRM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005967 PyModule_AddIntMacro(m, MSG_CONFIRM);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005968#endif
5969#ifdef MSG_MORE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005970 PyModule_AddIntMacro(m, MSG_MORE);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005971#endif
5972#ifdef MSG_EOF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005973 PyModule_AddIntMacro(m, MSG_EOF);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005974#endif
5975#ifdef MSG_BCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005976 PyModule_AddIntMacro(m, MSG_BCAST);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005977#endif
5978#ifdef MSG_MCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005979 PyModule_AddIntMacro(m, MSG_MCAST);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005980#endif
Benjamin Peterson57b66722012-12-12 22:24:47 -05005981#ifdef MSG_FASTOPEN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005982 PyModule_AddIntMacro(m, MSG_FASTOPEN);
Benjamin Peterson57b66722012-12-12 22:24:47 -05005983#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005985 /* Protocol level and numbers, usable for [gs]etsockopt */
5986#ifdef SOL_SOCKET
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005987 PyModule_AddIntMacro(m, SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005988#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005989#ifdef SOL_IP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005990 PyModule_AddIntMacro(m, SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00005991#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005992 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00005993#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005994#ifdef SOL_IPX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005995 PyModule_AddIntMacro(m, SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00005996#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005997#ifdef SOL_AX25
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02005998 PyModule_AddIntMacro(m, SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00005999#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006000#ifdef SOL_ATALK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006001 PyModule_AddIntMacro(m, SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00006002#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006003#ifdef SOL_NETROM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006004 PyModule_AddIntMacro(m, SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00006005#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006006#ifdef SOL_ROSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006007 PyModule_AddIntMacro(m, SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00006008#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006009#ifdef SOL_TCP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006010 PyModule_AddIntMacro(m, SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006011#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006012 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00006013#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006014#ifdef SOL_UDP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006015 PyModule_AddIntMacro(m, SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006016#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006017 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00006018#endif
Charles-François Natali47413c12011-10-06 19:47:44 +02006019#ifdef SOL_CAN_BASE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006020 PyModule_AddIntMacro(m, SOL_CAN_BASE);
Charles-François Natali47413c12011-10-06 19:47:44 +02006021#endif
6022#ifdef SOL_CAN_RAW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006023 PyModule_AddIntMacro(m, SOL_CAN_RAW);
6024 PyModule_AddIntMacro(m, CAN_RAW);
Charles-François Natali47413c12011-10-06 19:47:44 +02006025#endif
6026#ifdef HAVE_LINUX_CAN_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006027 PyModule_AddIntMacro(m, CAN_EFF_FLAG);
6028 PyModule_AddIntMacro(m, CAN_RTR_FLAG);
6029 PyModule_AddIntMacro(m, CAN_ERR_FLAG);
Charles-François Natali47413c12011-10-06 19:47:44 +02006030
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006031 PyModule_AddIntMacro(m, CAN_SFF_MASK);
6032 PyModule_AddIntMacro(m, CAN_EFF_MASK);
6033 PyModule_AddIntMacro(m, CAN_ERR_MASK);
Charles-François Natali47413c12011-10-06 19:47:44 +02006034#endif
6035#ifdef HAVE_LINUX_CAN_RAW_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006036 PyModule_AddIntMacro(m, CAN_RAW_FILTER);
6037 PyModule_AddIntMacro(m, CAN_RAW_ERR_FILTER);
6038 PyModule_AddIntMacro(m, CAN_RAW_LOOPBACK);
6039 PyModule_AddIntMacro(m, CAN_RAW_RECV_OWN_MSGS);
Charles-François Natali47413c12011-10-06 19:47:44 +02006040#endif
Charles-François Natali773e42d2013-02-05 19:42:01 +01006041#ifdef HAVE_LINUX_CAN_BCM_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006042 PyModule_AddIntMacro(m, CAN_BCM);
Charles-François Natali773e42d2013-02-05 19:42:01 +01006043 PyModule_AddIntConstant(m, "CAN_BCM_TX_SETUP", TX_SETUP);
6044 PyModule_AddIntConstant(m, "CAN_BCM_TX_DELETE", TX_DELETE);
6045 PyModule_AddIntConstant(m, "CAN_BCM_TX_READ", TX_READ);
6046 PyModule_AddIntConstant(m, "CAN_BCM_TX_SEND", TX_SEND);
6047 PyModule_AddIntConstant(m, "CAN_BCM_RX_SETUP", RX_SETUP);
6048 PyModule_AddIntConstant(m, "CAN_BCM_RX_DELETE", RX_DELETE);
6049 PyModule_AddIntConstant(m, "CAN_BCM_RX_READ", RX_READ);
6050 PyModule_AddIntConstant(m, "CAN_BCM_TX_STATUS", TX_STATUS);
6051 PyModule_AddIntConstant(m, "CAN_BCM_TX_EXPIRED", TX_EXPIRED);
6052 PyModule_AddIntConstant(m, "CAN_BCM_RX_STATUS", RX_STATUS);
6053 PyModule_AddIntConstant(m, "CAN_BCM_RX_TIMEOUT", RX_TIMEOUT);
6054 PyModule_AddIntConstant(m, "CAN_BCM_RX_CHANGED", RX_CHANGED);
6055#endif
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006056#ifdef SOL_RDS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006057 PyModule_AddIntMacro(m, SOL_RDS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006058#endif
6059#ifdef RDS_CANCEL_SENT_TO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006060 PyModule_AddIntMacro(m, RDS_CANCEL_SENT_TO);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006061#endif
6062#ifdef RDS_GET_MR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006063 PyModule_AddIntMacro(m, RDS_GET_MR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006064#endif
6065#ifdef RDS_FREE_MR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006066 PyModule_AddIntMacro(m, RDS_FREE_MR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006067#endif
6068#ifdef RDS_RECVERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006069 PyModule_AddIntMacro(m, RDS_RECVERR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006070#endif
6071#ifdef RDS_CONG_MONITOR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006072 PyModule_AddIntMacro(m, RDS_CONG_MONITOR);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006073#endif
6074#ifdef RDS_GET_MR_FOR_DEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006075 PyModule_AddIntMacro(m, RDS_GET_MR_FOR_DEST);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006076#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006077#ifdef IPPROTO_IP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006078 PyModule_AddIntMacro(m, IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00006079#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006080 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006081#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006082#ifdef IPPROTO_HOPOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006083 PyModule_AddIntMacro(m, IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006084#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006085#ifdef IPPROTO_ICMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006086 PyModule_AddIntMacro(m, IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00006087#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006088 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006089#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006090#ifdef IPPROTO_IGMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006091 PyModule_AddIntMacro(m, IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006092#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006093#ifdef IPPROTO_GGP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006094 PyModule_AddIntMacro(m, IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006095#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006096#ifdef IPPROTO_IPV4
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006097 PyModule_AddIntMacro(m, IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006098#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006099#ifdef IPPROTO_IPV6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006100 PyModule_AddIntMacro(m, IPPROTO_IPV6);
Martin v. Löwisa0f17342003-10-03 13:56:20 +00006101#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006102#ifdef IPPROTO_IPIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006103 PyModule_AddIntMacro(m, IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006104#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006105#ifdef IPPROTO_TCP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006106 PyModule_AddIntMacro(m, IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00006107#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006108 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006109#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006110#ifdef IPPROTO_EGP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006111 PyModule_AddIntMacro(m, IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006112#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006113#ifdef IPPROTO_PUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006114 PyModule_AddIntMacro(m, IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006115#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006116#ifdef IPPROTO_UDP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006117 PyModule_AddIntMacro(m, IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00006118#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006119 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006120#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006121#ifdef IPPROTO_IDP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006122 PyModule_AddIntMacro(m, IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006123#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006124#ifdef IPPROTO_HELLO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006125 PyModule_AddIntMacro(m, IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006126#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006127#ifdef IPPROTO_ND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006128 PyModule_AddIntMacro(m, IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006129#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006130#ifdef IPPROTO_TP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006131 PyModule_AddIntMacro(m, IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006132#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006133#ifdef IPPROTO_IPV6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006134 PyModule_AddIntMacro(m, IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006135#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006136#ifdef IPPROTO_ROUTING
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006137 PyModule_AddIntMacro(m, IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006138#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006139#ifdef IPPROTO_FRAGMENT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006140 PyModule_AddIntMacro(m, IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006141#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006142#ifdef IPPROTO_RSVP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006143 PyModule_AddIntMacro(m, IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006144#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006145#ifdef IPPROTO_GRE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006146 PyModule_AddIntMacro(m, IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006147#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006148#ifdef IPPROTO_ESP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006149 PyModule_AddIntMacro(m, IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006150#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006151#ifdef IPPROTO_AH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006152 PyModule_AddIntMacro(m, IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006153#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006154#ifdef IPPROTO_MOBILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006155 PyModule_AddIntMacro(m, IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006156#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006157#ifdef IPPROTO_ICMPV6
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006158 PyModule_AddIntMacro(m, IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006159#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006160#ifdef IPPROTO_NONE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006161 PyModule_AddIntMacro(m, IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006162#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006163#ifdef IPPROTO_DSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006164 PyModule_AddIntMacro(m, IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006165#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006166#ifdef IPPROTO_XTP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006167 PyModule_AddIntMacro(m, IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006168#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006169#ifdef IPPROTO_EON
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006170 PyModule_AddIntMacro(m, IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006171#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006172#ifdef IPPROTO_PIM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006173 PyModule_AddIntMacro(m, IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006174#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006175#ifdef IPPROTO_IPCOMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006176 PyModule_AddIntMacro(m, IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006177#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006178#ifdef IPPROTO_VRRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006179 PyModule_AddIntMacro(m, IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006180#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006181#ifdef IPPROTO_SCTP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006182 PyModule_AddIntMacro(m, IPPROTO_SCTP);
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006183#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006184#ifdef IPPROTO_BIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006185 PyModule_AddIntMacro(m, IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006186#endif
6187/**/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006188#ifdef IPPROTO_RAW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006189 PyModule_AddIntMacro(m, IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00006190#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006191 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006192#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006193#ifdef IPPROTO_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006194 PyModule_AddIntMacro(m, IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006195#endif
6196
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006197#ifdef SYSPROTO_CONTROL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006198 PyModule_AddIntMacro(m, SYSPROTO_CONTROL);
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006199#endif
6200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006201 /* Some port configuration */
6202#ifdef IPPORT_RESERVED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006203 PyModule_AddIntMacro(m, IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006204#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006205 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006206#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006207#ifdef IPPORT_USERRESERVED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006208 PyModule_AddIntMacro(m, IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006209#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006210 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006211#endif
6212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006213 /* Some reserved IP v.4 addresses */
6214#ifdef INADDR_ANY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006215 PyModule_AddIntMacro(m, INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006216#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006217 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006218#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006219#ifdef INADDR_BROADCAST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006220 PyModule_AddIntMacro(m, INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006221#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006222 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006223#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006224#ifdef INADDR_LOOPBACK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006225 PyModule_AddIntMacro(m, INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006226#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006227 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006228#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006229#ifdef INADDR_UNSPEC_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006230 PyModule_AddIntMacro(m, INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006231#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006232 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006233#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006234#ifdef INADDR_ALLHOSTS_GROUP
6235 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
6236 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006237#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006238 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006239#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006240#ifdef INADDR_MAX_LOCAL_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006241 PyModule_AddIntMacro(m, INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006242#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006243 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006244#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006245#ifdef INADDR_NONE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006246 PyModule_AddIntMacro(m, INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006247#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006248 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006249#endif
6250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006251 /* IPv4 [gs]etsockopt options */
6252#ifdef IP_OPTIONS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006253 PyModule_AddIntMacro(m, IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006254#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006255#ifdef IP_HDRINCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006256 PyModule_AddIntMacro(m, IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006257#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006258#ifdef IP_TOS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006259 PyModule_AddIntMacro(m, IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006260#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006261#ifdef IP_TTL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006262 PyModule_AddIntMacro(m, IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006263#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006264#ifdef IP_RECVOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006265 PyModule_AddIntMacro(m, IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006266#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006267#ifdef IP_RECVRETOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006268 PyModule_AddIntMacro(m, IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006269#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006270#ifdef IP_RECVDSTADDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006271 PyModule_AddIntMacro(m, IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006272#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006273#ifdef IP_RETOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006274 PyModule_AddIntMacro(m, IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006275#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006276#ifdef IP_MULTICAST_IF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006277 PyModule_AddIntMacro(m, IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006278#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006279#ifdef IP_MULTICAST_TTL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006280 PyModule_AddIntMacro(m, IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006281#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006282#ifdef IP_MULTICAST_LOOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006283 PyModule_AddIntMacro(m, IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006284#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006285#ifdef IP_ADD_MEMBERSHIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006286 PyModule_AddIntMacro(m, IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006287#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006288#ifdef IP_DROP_MEMBERSHIP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006289 PyModule_AddIntMacro(m, IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006290#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006291#ifdef IP_DEFAULT_MULTICAST_TTL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006292 PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00006293#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006294#ifdef IP_DEFAULT_MULTICAST_LOOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006295 PyModule_AddIntMacro(m, IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006296#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006297#ifdef IP_MAX_MEMBERSHIPS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006298 PyModule_AddIntMacro(m, IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00006299#endif
Charles-François Natali564a42c2011-12-17 14:59:56 +01006300#ifdef IP_TRANSPARENT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006301 PyModule_AddIntMacro(m, IP_TRANSPARENT);
Charles-François Natali564a42c2011-12-17 14:59:56 +01006302#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006304 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
6305#ifdef IPV6_JOIN_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006306 PyModule_AddIntMacro(m, IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006307#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006308#ifdef IPV6_LEAVE_GROUP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006309 PyModule_AddIntMacro(m, IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006310#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006311#ifdef IPV6_MULTICAST_HOPS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006312 PyModule_AddIntMacro(m, IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006313#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006314#ifdef IPV6_MULTICAST_IF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006315 PyModule_AddIntMacro(m, IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006316#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006317#ifdef IPV6_MULTICAST_LOOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006318 PyModule_AddIntMacro(m, IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006319#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006320#ifdef IPV6_UNICAST_HOPS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006321 PyModule_AddIntMacro(m, IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006322#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006323 /* Additional IPV6 socket options, defined in RFC 3493 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00006324#ifdef IPV6_V6ONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006325 PyModule_AddIntMacro(m, IPV6_V6ONLY);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006326#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006327 /* Advanced IPV6 socket options, from RFC 3542 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00006328#ifdef IPV6_CHECKSUM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006329 PyModule_AddIntMacro(m, IPV6_CHECKSUM);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006330#endif
6331#ifdef IPV6_DONTFRAG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006332 PyModule_AddIntMacro(m, IPV6_DONTFRAG);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006333#endif
6334#ifdef IPV6_DSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006335 PyModule_AddIntMacro(m, IPV6_DSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006336#endif
6337#ifdef IPV6_HOPLIMIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006338 PyModule_AddIntMacro(m, IPV6_HOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006339#endif
6340#ifdef IPV6_HOPOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006341 PyModule_AddIntMacro(m, IPV6_HOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006342#endif
6343#ifdef IPV6_NEXTHOP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006344 PyModule_AddIntMacro(m, IPV6_NEXTHOP);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006345#endif
6346#ifdef IPV6_PATHMTU
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006347 PyModule_AddIntMacro(m, IPV6_PATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006348#endif
6349#ifdef IPV6_PKTINFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006350 PyModule_AddIntMacro(m, IPV6_PKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006351#endif
6352#ifdef IPV6_RECVDSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006353 PyModule_AddIntMacro(m, IPV6_RECVDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006354#endif
6355#ifdef IPV6_RECVHOPLIMIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006356 PyModule_AddIntMacro(m, IPV6_RECVHOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006357#endif
6358#ifdef IPV6_RECVHOPOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006359 PyModule_AddIntMacro(m, IPV6_RECVHOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006360#endif
6361#ifdef IPV6_RECVPKTINFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006362 PyModule_AddIntMacro(m, IPV6_RECVPKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006363#endif
6364#ifdef IPV6_RECVRTHDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006365 PyModule_AddIntMacro(m, IPV6_RECVRTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006366#endif
6367#ifdef IPV6_RECVTCLASS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006368 PyModule_AddIntMacro(m, IPV6_RECVTCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006369#endif
6370#ifdef IPV6_RTHDR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006371 PyModule_AddIntMacro(m, IPV6_RTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006372#endif
6373#ifdef IPV6_RTHDRDSTOPTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006374 PyModule_AddIntMacro(m, IPV6_RTHDRDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006375#endif
6376#ifdef IPV6_RTHDR_TYPE_0
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006377 PyModule_AddIntMacro(m, IPV6_RTHDR_TYPE_0);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006378#endif
6379#ifdef IPV6_RECVPATHMTU
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006380 PyModule_AddIntMacro(m, IPV6_RECVPATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006381#endif
6382#ifdef IPV6_TCLASS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006383 PyModule_AddIntMacro(m, IPV6_TCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006384#endif
6385#ifdef IPV6_USE_MIN_MTU
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006386 PyModule_AddIntMacro(m, IPV6_USE_MIN_MTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006387#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006389 /* TCP options */
6390#ifdef TCP_NODELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006391 PyModule_AddIntMacro(m, TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00006392#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006393#ifdef TCP_MAXSEG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006394 PyModule_AddIntMacro(m, TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00006395#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006396#ifdef TCP_CORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006397 PyModule_AddIntMacro(m, TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006398#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006399#ifdef TCP_KEEPIDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006400 PyModule_AddIntMacro(m, TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006401#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006402#ifdef TCP_KEEPINTVL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006403 PyModule_AddIntMacro(m, TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006404#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006405#ifdef TCP_KEEPCNT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006406 PyModule_AddIntMacro(m, TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006407#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006408#ifdef TCP_SYNCNT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006409 PyModule_AddIntMacro(m, TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006410#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006411#ifdef TCP_LINGER2
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006412 PyModule_AddIntMacro(m, TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006413#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006414#ifdef TCP_DEFER_ACCEPT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006415 PyModule_AddIntMacro(m, TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006416#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006417#ifdef TCP_WINDOW_CLAMP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006418 PyModule_AddIntMacro(m, TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006419#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006420#ifdef TCP_INFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006421 PyModule_AddIntMacro(m, TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006422#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006423#ifdef TCP_QUICKACK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006424 PyModule_AddIntMacro(m, TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006425#endif
Benjamin Peterson57b66722012-12-12 22:24:47 -05006426#ifdef TCP_FASTOPEN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006427 PyModule_AddIntMacro(m, TCP_FASTOPEN);
Benjamin Peterson57b66722012-12-12 22:24:47 -05006428#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006430 /* IPX options */
6431#ifdef IPX_TYPE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006432 PyModule_AddIntMacro(m, IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00006433#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00006434
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006435/* Reliable Datagram Sockets */
6436#ifdef RDS_CMSG_RDMA_ARGS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006437 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_ARGS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006438#endif
6439#ifdef RDS_CMSG_RDMA_DEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006440 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_DEST);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006441#endif
6442#ifdef RDS_CMSG_RDMA_MAP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006443 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_MAP);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006444#endif
6445#ifdef RDS_CMSG_RDMA_STATUS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006446 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_STATUS);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006447#endif
6448#ifdef RDS_CMSG_RDMA_UPDATE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006449 PyModule_AddIntMacro(m, RDS_CMSG_RDMA_UPDATE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006450#endif
6451#ifdef RDS_RDMA_READWRITE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006452 PyModule_AddIntMacro(m, RDS_RDMA_READWRITE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006453#endif
6454#ifdef RDS_RDMA_FENCE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006455 PyModule_AddIntMacro(m, RDS_RDMA_FENCE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006456#endif
6457#ifdef RDS_RDMA_INVALIDATE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006458 PyModule_AddIntMacro(m, RDS_RDMA_INVALIDATE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006459#endif
6460#ifdef RDS_RDMA_USE_ONCE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006461 PyModule_AddIntMacro(m, RDS_RDMA_USE_ONCE);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006462#endif
6463#ifdef RDS_RDMA_DONTWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006464 PyModule_AddIntMacro(m, RDS_RDMA_DONTWAIT);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006465#endif
6466#ifdef RDS_RDMA_NOTIFY_ME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006467 PyModule_AddIntMacro(m, RDS_RDMA_NOTIFY_ME);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006468#endif
6469#ifdef RDS_RDMA_SILENT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006470 PyModule_AddIntMacro(m, RDS_RDMA_SILENT);
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006471#endif
6472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006473 /* get{addr,name}info parameters */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006474#ifdef EAI_ADDRFAMILY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006475 PyModule_AddIntMacro(m, EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006476#endif
6477#ifdef EAI_AGAIN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006478 PyModule_AddIntMacro(m, EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006479#endif
6480#ifdef EAI_BADFLAGS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006481 PyModule_AddIntMacro(m, EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006482#endif
6483#ifdef EAI_FAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006484 PyModule_AddIntMacro(m, EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006485#endif
6486#ifdef EAI_FAMILY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006487 PyModule_AddIntMacro(m, EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006488#endif
6489#ifdef EAI_MEMORY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006490 PyModule_AddIntMacro(m, EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006491#endif
6492#ifdef EAI_NODATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006493 PyModule_AddIntMacro(m, EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006494#endif
6495#ifdef EAI_NONAME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006496 PyModule_AddIntMacro(m, EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006497#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00006498#ifdef EAI_OVERFLOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006499 PyModule_AddIntMacro(m, EAI_OVERFLOW);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006500#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006501#ifdef EAI_SERVICE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006502 PyModule_AddIntMacro(m, EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006503#endif
6504#ifdef EAI_SOCKTYPE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006505 PyModule_AddIntMacro(m, EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006506#endif
6507#ifdef EAI_SYSTEM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006508 PyModule_AddIntMacro(m, EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006509#endif
6510#ifdef EAI_BADHINTS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006511 PyModule_AddIntMacro(m, EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006512#endif
6513#ifdef EAI_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006514 PyModule_AddIntMacro(m, EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006515#endif
6516#ifdef EAI_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006517 PyModule_AddIntMacro(m, EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006518#endif
6519#ifdef AI_PASSIVE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006520 PyModule_AddIntMacro(m, AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006521#endif
6522#ifdef AI_CANONNAME
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006523 PyModule_AddIntMacro(m, AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006524#endif
6525#ifdef AI_NUMERICHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006526 PyModule_AddIntMacro(m, AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006527#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00006528#ifdef AI_NUMERICSERV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006529 PyModule_AddIntMacro(m, AI_NUMERICSERV);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006530#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006531#ifdef AI_MASK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006532 PyModule_AddIntMacro(m, AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006533#endif
6534#ifdef AI_ALL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006535 PyModule_AddIntMacro(m, AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006536#endif
6537#ifdef AI_V4MAPPED_CFG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006538 PyModule_AddIntMacro(m, AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006539#endif
6540#ifdef AI_ADDRCONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006541 PyModule_AddIntMacro(m, AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006542#endif
6543#ifdef AI_V4MAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006544 PyModule_AddIntMacro(m, AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006545#endif
6546#ifdef AI_DEFAULT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006547 PyModule_AddIntMacro(m, AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006548#endif
6549#ifdef NI_MAXHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006550 PyModule_AddIntMacro(m, NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006551#endif
6552#ifdef NI_MAXSERV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006553 PyModule_AddIntMacro(m, NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006554#endif
6555#ifdef NI_NOFQDN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006556 PyModule_AddIntMacro(m, NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006557#endif
6558#ifdef NI_NUMERICHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006559 PyModule_AddIntMacro(m, NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006560#endif
6561#ifdef NI_NAMEREQD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006562 PyModule_AddIntMacro(m, NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006563#endif
6564#ifdef NI_NUMERICSERV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006565 PyModule_AddIntMacro(m, NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006566#endif
6567#ifdef NI_DGRAM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006568 PyModule_AddIntMacro(m, NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006569#endif
6570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006571 /* shutdown() parameters */
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006572#ifdef SHUT_RD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006573 PyModule_AddIntMacro(m, SHUT_RD);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006574#elif defined(SD_RECEIVE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006575 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006576#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006577 PyModule_AddIntConstant(m, "SHUT_RD", 0);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006578#endif
6579#ifdef SHUT_WR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006580 PyModule_AddIntMacro(m, SHUT_WR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006581#elif defined(SD_SEND)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006582 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006583#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006584 PyModule_AddIntConstant(m, "SHUT_WR", 1);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006585#endif
6586#ifdef SHUT_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006587 PyModule_AddIntMacro(m, SHUT_RDWR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006588#elif defined(SD_BOTH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006589 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006590#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006591 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006592#endif
6593
Christian Heimesfaf2f632008-01-06 16:59:19 +00006594#ifdef SIO_RCVALL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006595 {
6596 DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS};
6597 const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"};
6598 int i;
Victor Stinner63941882011-09-29 00:42:28 +02006599 for(i = 0; i<Py_ARRAY_LENGTH(codes); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006600 PyObject *tmp;
6601 tmp = PyLong_FromUnsignedLong(codes[i]);
6602 if (tmp == NULL)
6603 return NULL;
6604 PyModule_AddObject(m, names[i], tmp);
6605 }
6606 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006607 PyModule_AddIntMacro(m, RCVALL_OFF);
6608 PyModule_AddIntMacro(m, RCVALL_ON);
6609 PyModule_AddIntMacro(m, RCVALL_SOCKETLEVELONLY);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00006610#ifdef RCVALL_IPLEVEL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006611 PyModule_AddIntMacro(m, RCVALL_IPLEVEL);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00006612#endif
6613#ifdef RCVALL_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02006614 PyModule_AddIntMacro(m, RCVALL_MAX);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00006615#endif
Christian Heimesfaf2f632008-01-06 16:59:19 +00006616#endif /* _MSTCPIP_ */
6617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006618 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00006619#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006620 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00006621#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006622 return m;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00006623}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006624
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006625
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006626#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +00006627#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006628
6629/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00006630/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006631
Guido van Rossum3eede5a2002-06-07 02:08:35 +00006632int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006633inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006635 if (af == AF_INET) {
Benjamin Petersonf91df042009-02-13 02:50:59 +00006636#if (SIZEOF_INT != 4)
6637#error "Not sure if in_addr_t exists and int is not 32-bits."
6638#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006639 unsigned int packed_addr;
6640 packed_addr = inet_addr(src);
6641 if (packed_addr == INADDR_NONE)
6642 return 0;
6643 memcpy(dst, &packed_addr, 4);
6644 return 1;
6645 }
6646 /* Should set errno to EAFNOSUPPORT */
6647 return -1;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006648}
6649
Martin v. Löwisc925b1532001-07-21 09:42:15 +00006650const char *
6651inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006653 if (af == AF_INET) {
6654 struct in_addr packed_addr;
6655 if (size < 16)
6656 /* Should set errno to ENOSPC. */
6657 return NULL;
6658 memcpy(&packed_addr, src, sizeof(packed_addr));
6659 return strncpy(dst, inet_ntoa(packed_addr), size);
6660 }
6661 /* Should set errno to EAFNOSUPPORT */
6662 return NULL;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006663}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006664
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006665#endif
Christian Heimesb6150692007-11-15 23:37:07 +00006666#endif