blob: 1c3396fd53e0582dd2122cedb9dcdcd73d5a70e3 [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
Guido van Rossum27e177d1995-03-16 15:43:47 +000018- socket.error: exception raised for socket specific errors
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 Rossum47dfa4a2003-04-25 05:48:32 +000098#undef MAX
99#define MAX(x, y) ((x) < (y) ? (y) : (x))
100
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000101/* Socket object documentation */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000102PyDoc_STRVAR(sock_doc,
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000103"socket([family[, type[, proto]]]) -> socket object\n\
104\n\
105Open a socket of the given type. The family argument specifies the\n\
106address family; it defaults to AF_INET. The type argument specifies\n\
107whether this is a stream (SOCK_STREAM, this is the default)\n\
108or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
109specifying the default protocol. Keyword arguments are accepted.\n\
110\n\
111A socket object represents one endpoint of a network connection.\n\
112\n\
113Methods of socket objects (keyword arguments not allowed):\n\
114\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000115_accept() -- accept connection, returning new socket fd and client address\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000116bind(addr) -- bind the socket to a local address\n\
117close() -- close the socket\n\
118connect(addr) -- connect the socket to a remote address\n\
119connect_ex(addr) -- connect, return an error code instead of an exception\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000120_dup() -- return a new socket fd duplicated from fileno()\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000121fileno() -- return underlying file descriptor\n\
122getpeername() -- return remote address [*]\n\
123getsockname() -- return local address\n\
124getsockopt(level, optname[, buflen]) -- get socket options\n\
125gettimeout() -- return timeout or None\n\
126listen(n) -- start listening for incoming connections\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000127recv(buflen[, flags]) -- receive data\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000128recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000129recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000130recvfrom_into(buffer[, nbytes, [, flags])\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000131 -- receive data and sender\'s address (into a buffer)\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000132sendall(data[, flags]) -- send all data\n\
133send(data[, flags]) -- send data, may not send all of it\n\
134sendto(data[, flags], addr) -- send data to a given address\n\
135setblocking(0 | 1) -- set or clear the blocking I/O flag\n\
136setsockopt(level, optname, value) -- set socket options\n\
137settimeout(None | float) -- set or clear the timeout\n\
138shutdown(how) -- shut down traffic in one or both directions\n\
Gregory P. Smith5ed2e772011-05-15 00:26:45 -0700139if_nameindex() -- return all network interface indices and names\n\
Gregory P. Smithb6471db2011-05-22 22:47:55 -0700140if_nametoindex(name) -- return the corresponding interface index\n\
141if_indextoname(index) -- return the corresponding interface name\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000142\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000143 [*] not available on all platforms!");
Guido van Rossum3baaa131999-03-22 21:44:51 +0000144
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000145/* XXX This is a terrible mess of platform-dependent preprocessor hacks.
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000146 I hope some day someone can clean this up please... */
147
Guido van Rossum9376b741999-09-15 22:01:40 +0000148/* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
149 script doesn't get this right, so we hardcode some platform checks below.
150 On the other hand, not all Linux versions agree, so there the settings
151 computed by the configure script are needed! */
152
153#ifndef linux
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000154# undef HAVE_GETHOSTBYNAME_R_3_ARG
155# undef HAVE_GETHOSTBYNAME_R_5_ARG
156# undef HAVE_GETHOSTBYNAME_R_6_ARG
Guido van Rossum9376b741999-09-15 22:01:40 +0000157#endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000158
Victor Stinner710d27e2011-08-23 10:57:32 +0200159#if defined(__OpenBSD__)
160# include <sys/uio.h>
161#endif
162
Guido van Rossum7a122991999-04-13 04:07:32 +0000163#ifndef WITH_THREAD
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000164# undef HAVE_GETHOSTBYNAME_R
Guido van Rossum7a122991999-04-13 04:07:32 +0000165#endif
166
Guido van Rossume7de2061999-03-24 17:24:33 +0000167#ifdef HAVE_GETHOSTBYNAME_R
Jesus Cea736e7fc2011-03-14 17:36:54 +0100168# if defined(_AIX)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000169# define HAVE_GETHOSTBYNAME_R_3_ARG
170# elif defined(__sun) || defined(__sgi)
171# define HAVE_GETHOSTBYNAME_R_5_ARG
172# elif defined(linux)
Guido van Rossum9376b741999-09-15 22:01:40 +0000173/* Rely on the configure script */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000174# else
175# undef HAVE_GETHOSTBYNAME_R
176# endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000177#endif
178
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000179#if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \
180 !defined(MS_WINDOWS)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000181# define USE_GETHOSTBYNAME_LOCK
Guido van Rossum3baaa131999-03-22 21:44:51 +0000182#endif
183
Hye-Shik Chang9ceebd52005-09-24 14:58:47 +0000184/* To use __FreeBSD_version */
185#ifdef HAVE_SYS_PARAM_H
186#include <sys/param.h>
187#endif
Just van Rossum1040d2c2003-05-09 07:53:18 +0000188/* On systems on which getaddrinfo() is believed to not be thread-safe,
Just van Rossum09aecd72003-05-09 08:03:44 +0000189 (this includes the getaddrinfo emulation) protect access with a lock. */
Hye-Shik Chang9ceebd52005-09-24 14:58:47 +0000190#if defined(WITH_THREAD) && (defined(__APPLE__) || \
191 (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000192 defined(__OpenBSD__) || defined(__NetBSD__) || \
193 defined(__VMS) || !defined(HAVE_GETADDRINFO))
Just van Rossum1040d2c2003-05-09 07:53:18 +0000194#define USE_GETADDRINFO_LOCK
195#endif
196
197#ifdef USE_GETADDRINFO_LOCK
198#define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1);
199#define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock);
200#else
201#define ACQUIRE_GETADDRINFO_LOCK
202#define RELEASE_GETADDRINFO_LOCK
203#endif
204
205#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000206# include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000207#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000208
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000209#if defined(PYCC_VACPP)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000210# include <types.h>
211# include <io.h>
212# include <sys/ioctl.h>
213# include <utils.h>
214# include <ctype.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000215#endif
216
Martin v. Löwis9e437302002-12-06 12:57:26 +0000217#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000218# include <ioctl.h>
219#endif
220
Martin v. Löwis9d6c6692012-02-03 17:44:58 +0100221#ifdef __APPLE__
222# include <sys/ioctl.h>
223#endif
224
225
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000226#if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000227/* make sure that the reentrant (gethostbyaddr_r etc)
228 functions are declared correctly if compiling with
229 MIPSPro 7.x in ANSI C mode (default) */
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000230
Thomas Wouters477c8d52006-05-27 19:21:47 +0000231/* XXX Using _SGIAPI is the wrong thing,
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000232 but I don't know what the right thing is. */
Trent Mick8ea5bdf2004-09-13 17:48:41 +0000233#undef _SGIAPI /* to avoid warning */
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000234#define _SGIAPI 1
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000235
Trent Mick8ea5bdf2004-09-13 17:48:41 +0000236#undef _XOPEN_SOURCE
237#include <sys/socket.h>
238#include <sys/types.h>
239#include <netinet/in.h>
240#ifdef _SS_ALIGNSIZE
241#define HAVE_GETADDRINFO 1
242#define HAVE_GETNAMEINFO 1
243#endif
244
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000245#define HAVE_INET_PTON
246#include <netdb.h>
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000247#endif
248
Thomas Wouters477c8d52006-05-27 19:21:47 +0000249/* Irix 6.5 fails to define this variable at all. This is needed
250 for both GCC and SGI's compiler. I'd say that the SGI headers
Georg Brandldbd83392006-02-20 09:42:33 +0000251 are just busted. Same thing for Solaris. */
252#if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN)
Anthony Baxterbab23cf2003-10-04 08:00:49 +0000253#define INET_ADDRSTRLEN 16
254#endif
255
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000256/* Generic includes */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000257#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000258#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000259#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000260
Gregory P. Smith3b1f2c32011-05-15 12:18:23 -0700261#ifdef HAVE_SYS_SOCKET_H
262#include <sys/socket.h>
263#endif
264
265#ifdef HAVE_NET_IF_H
266#include <net/if.h>
267#endif
268
Marc-André Lemburg976ade62002-02-16 18:47:07 +0000269/* Generic socket object definitions and includes */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000270#define PySocket_BUILDING_SOCKET
Marc-André Lemburgbb8b78b2002-02-16 18:44:52 +0000271#include "socketmodule.h"
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000272
273/* Addressing includes */
274
Guido van Rossum6f489d91996-06-28 20:15:15 +0000275#ifndef MS_WINDOWS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000276
277/* Non-MS WINDOWS includes */
278# include <netdb.h>
Nick Coghlan4ecf6cf2011-08-22 15:45:19 +1000279# include <unistd.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000280
Guido van Rossum9376b741999-09-15 22:01:40 +0000281/* Headers needed for inet_ntoa() and inet_addr() */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000282# include <arpa/inet.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000283
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000284# include <fcntl.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000285
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000286#else
Guido van Rossum48a680c2001-03-02 06:34:14 +0000287
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000288/* MS_WINDOWS includes */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000289# ifdef HAVE_FCNTL_H
290# include <fcntl.h>
291# endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000292
Jeremy Hylton22308652001-02-02 03:23:09 +0000293#endif
294
Skip Montanaro7befb992004-02-10 16:50:21 +0000295#include <stddef.h>
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000296
297#ifndef offsetof
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298# define offsetof(type, member) ((size_t)(&((type *)0)->member))
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000299#endif
300
Neal Norwitz39d22e52002-11-02 19:55:21 +0000301#ifndef O_NONBLOCK
302# define O_NONBLOCK O_NDELAY
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000303#endif
304
Trent Micka708d6e2004-09-07 17:48:26 +0000305/* include Python's addrinfo.h unless it causes trouble */
306#if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE)
307 /* Do not include addinfo.h on some newer IRIX versions.
308 * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21,
309 * for example, but not by 6.5.10.
310 */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000311#elif defined(_MSC_VER) && _MSC_VER>1201
Trent Micka708d6e2004-09-07 17:48:26 +0000312 /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and
313 * EAI_* constants are defined in (the already included) ws2tcpip.h.
314 */
315#else
316# include "addrinfo.h"
317#endif
Jason Tishlerc246cb72004-08-09 13:25:59 +0000318
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000319#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +0000320#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000321int inet_pton(int af, const char *src, void *dst);
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000322const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000323#endif
Christian Heimesb6150692007-11-15 23:37:07 +0000324#endif
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000325
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000326#ifdef __APPLE__
327/* On OS X, getaddrinfo returns no error indication of lookup
328 failure, so we must use the emulation instead of the libinfo
329 implementation. Unfortunately, performing an autoconf test
330 for this bug would require DNS access for the machine performing
331 the configuration, which is not acceptable. Therefore, we
332 determine the bug just by checking for __APPLE__. If this bug
333 gets ever fixed, perhaps checking for sys/version.h would be
334 appropriate, which is 10/0 on the system with the bug. */
Jack Jansen84262fb2002-07-02 14:40:42 +0000335#ifndef HAVE_GETNAMEINFO
336/* This bug seems to be fixed in Jaguar. Ths easiest way I could
337 Find to check for Jaguar is that it has getnameinfo(), which
338 older releases don't have */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000339#undef HAVE_GETADDRINFO
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000340#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000341
342#ifdef HAVE_INET_ATON
343#define USE_INET_ATON_WEAKLINK
344#endif
345
Jack Jansen84262fb2002-07-02 14:40:42 +0000346#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000347
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000348/* I know this is a bad practice, but it is the easiest... */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000349#if !defined(HAVE_GETADDRINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000350/* avoid clashes with the C library definition of the symbol. */
351#define getaddrinfo fake_getaddrinfo
352#define gai_strerror fake_gai_strerror
353#define freeaddrinfo fake_freeaddrinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000354#include "getaddrinfo.c"
355#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000356#if !defined(HAVE_GETNAMEINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000357#define getnameinfo fake_getnameinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000358#include "getnameinfo.c"
359#endif
360
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000361#ifdef MS_WINDOWS
362/* On Windows a socket is really a handle not an fd */
363static SOCKET
364dup_socket(SOCKET handle)
365{
Daniel Stutzbach6c765282010-09-03 12:38:33 +0000366 WSAPROTOCOL_INFO info;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000367
Daniel Stutzbach6c765282010-09-03 12:38:33 +0000368 if (WSADuplicateSocket(handle, GetCurrentProcessId(), &info))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 return INVALID_SOCKET;
Daniel Stutzbach6c765282010-09-03 12:38:33 +0000370
371 return WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
Antoine Pitrou9a54a262012-04-01 01:14:39 +0200372 FROM_PROTOCOL_INFO, &info, 0, WSA_FLAG_OVERLAPPED);
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000373}
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000374#define SOCKETCLOSE closesocket
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000375#else
376/* On Unix we can use dup to duplicate the file descriptor of a socket*/
377#define dup_socket(fd) dup(fd)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000378#endif
379
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000380#ifdef MS_WIN32
Kristján Valur Jónsson310052c2012-03-31 13:35:00 +0000381#undef EAFNOSUPPORT
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000382#define EAFNOSUPPORT WSAEAFNOSUPPORT
383#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000384#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000385
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000386#ifndef SOCKETCLOSE
387#define SOCKETCLOSE close
388#endif
389
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +0000390#if (defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)) && !defined(__NetBSD__) && !defined(__DragonFly__)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000391#define USE_BLUETOOTH 1
392#if defined(__FreeBSD__)
393#define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP
394#define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM
Thomas Wouterscf297e42007-02-23 15:07:44 +0000395#define BTPROTO_HCI BLUETOOTH_PROTO_HCI
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000396#define SOL_HCI SOL_HCI_RAW
397#define HCI_FILTER SO_HCI_RAW_FILTER
Hye-Shik Chang81268602004-02-02 06:05:24 +0000398#define sockaddr_l2 sockaddr_l2cap
399#define sockaddr_rc sockaddr_rfcomm
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000400#define hci_dev hci_node
Hye-Shik Chang81268602004-02-02 06:05:24 +0000401#define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb)
402#define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000403#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +0000404#elif defined(__NetBSD__) || defined(__DragonFly__)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000405#define sockaddr_l2 sockaddr_bt
406#define sockaddr_rc sockaddr_bt
Thomas Wouterscf297e42007-02-23 15:07:44 +0000407#define sockaddr_hci sockaddr_bt
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000408#define sockaddr_sco sockaddr_bt
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +0000409#define SOL_HCI BTPROTO_HCI
410#define HCI_DATA_DIR SO_HCI_DIRECTION
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000411#define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb)
412#define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000413#define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000414#define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000415#else
Hye-Shik Chang81268602004-02-02 06:05:24 +0000416#define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)
417#define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000418#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000419#define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)
420#endif
421#endif
422
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000423#ifdef __VMS
424/* TCP/IP Services for VMS uses a maximum send/recv buffer length */
425#define SEGMENT_SIZE (32 * 1024 -1)
426#endif
427
Charles-François Natali8b759652011-12-23 16:44:51 +0100428/* Convert "sock_addr_t *" to "struct sockaddr *". */
429#define SAS2SA(x) (&((x)->sa))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000430
Martin v. Löwise9416172003-05-03 10:12:45 +0000431/*
432 * Constants for getnameinfo()
433 */
434#if !defined(NI_MAXHOST)
435#define NI_MAXHOST 1025
436#endif
437#if !defined(NI_MAXSERV)
438#define NI_MAXSERV 32
439#endif
440
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000441#ifndef INVALID_SOCKET /* MS defines this */
442#define INVALID_SOCKET (-1)
443#endif
444
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000445/* XXX There's a problem here: *static* functions are not supposed to have
446 a Py prefix (or use CapitalizedWords). Later... */
447
Guido van Rossum30a685f1991-06-27 15:51:29 +0000448/* Global variable holding the exception type for errors detected
449 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000450static PyObject *socket_herror;
451static PyObject *socket_gaierror;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000452static PyObject *socket_timeout;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000453
Tim Peters643a7fc2002-02-17 04:13:21 +0000454/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000455 The sock_type variable contains pointers to various functions,
456 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000457 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000458static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000459
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000460#if defined(HAVE_POLL_H)
461#include <poll.h>
462#elif defined(HAVE_SYS_POLL_H)
463#include <sys/poll.h>
464#endif
465
Nick Coghlan96fe56a2011-08-22 11:55:57 +1000466/* Largest value to try to store in a socklen_t (used when handling
467 ancillary data). POSIX requires socklen_t to hold at least
468 (2**31)-1 and recommends against storing larger values, but
469 socklen_t was originally int in the BSD interface, so to be on the
470 safe side we use the smaller of (2**31)-1 and INT_MAX. */
471#if INT_MAX > 0x7fffffff
472#define SOCKLEN_T_LIMIT 0x7fffffff
473#else
474#define SOCKLEN_T_LIMIT INT_MAX
475#endif
476
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200477#ifdef HAVE_POLL
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000478/* Instead of select(), we'll use poll() since poll() works on any fd. */
479#define IS_SELECTABLE(s) 1
480/* Can we call select() with this socket without a buffer overrun? */
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000481#else
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200482/* If there's no timeout left, we don't have to call select, so it's a safe,
483 * little white lie. */
484#define IS_SELECTABLE(s) (_PyIsSelectable_fd((s)->sock_fd) || (s)->sock_timeout <= 0.0)
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000485#endif
Neal Norwitz082b2df2006-02-07 07:04:46 +0000486
487static PyObject*
488select_error(void)
489{
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200490 PyErr_SetString(PyExc_OSError, "unable to select on socket");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 return NULL;
Neal Norwitz082b2df2006-02-07 07:04:46 +0000492}
493
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000494#ifdef MS_WINDOWS
Antoine Pitrouc8f01962010-09-28 22:03:27 +0000495#ifndef WSAEAGAIN
496#define WSAEAGAIN WSAEWOULDBLOCK
497#endif
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000498#define CHECK_ERRNO(expected) \
499 (WSAGetLastError() == WSA ## expected)
500#else
501#define CHECK_ERRNO(expected) \
502 (errno == expected)
503#endif
504
Guido van Rossum30a685f1991-06-27 15:51:29 +0000505/* Convenience function to raise an error according to errno
506 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000507
Guido van Rossum73624e91994-10-10 17:59:00 +0000508static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000509set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000510{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000511#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 int err_no = WSAGetLastError();
513 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
514 recognizes the error codes used by both GetLastError() and
515 WSAGetLastError */
516 if (err_no)
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200517 return PyErr_SetExcFromWindowsErr(PyExc_OSError, err_no);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000518#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000519
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200520 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000521}
522
Guido van Rossum30a685f1991-06-27 15:51:29 +0000523
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000524static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000525set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000528
529#ifdef HAVE_HSTRERROR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000531#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 v = Py_BuildValue("(is)", h_error, "host not found");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000533#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 if (v != NULL) {
535 PyErr_SetObject(socket_herror, v);
536 Py_DECREF(v);
537 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000540}
541
542
543static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000544set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000547
Martin v. Löwis272cb402002-03-01 08:31:07 +0000548#ifdef EAI_SYSTEM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 /* EAI_SYSTEM is not available on Windows XP. */
550 if (error == EAI_SYSTEM)
551 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000552#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000553
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000554#ifdef HAVE_GAI_STRERROR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000556#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000558#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 if (v != NULL) {
560 PyErr_SetObject(socket_gaierror, v);
561 Py_DECREF(v);
562 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000565}
566
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000567#ifdef __VMS
568/* Function to send in segments */
569static int
570sendsegmented(int sock_fd, char *buf, int len, int flags)
571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 int n = 0;
573 int remaining = len;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 while (remaining > 0) {
576 unsigned int segment;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining);
579 n = send(sock_fd, buf, segment, flags);
580 if (n < 0) {
581 return n;
582 }
583 remaining -= segment;
584 buf += segment;
585 } /* end while */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 return len;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000588}
589#endif
590
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000591/* Function to perform the setting of socket blocking mode
592 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000593static int
594internal_setblocking(PySocketSockObject *s, int block)
595{
Guido van Rossum67f7a382002-06-06 21:08:16 +0000596#ifndef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 int delay_flag;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000598#endif
Antoine Pitroub1c54962010-10-14 15:05:38 +0000599#ifdef SOCK_NONBLOCK
600 if (block)
601 s->sock_type &= (~SOCK_NONBLOCK);
602 else
603 s->sock_type |= SOCK_NONBLOCK;
604#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +0000605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 Py_BEGIN_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000607#ifndef MS_WINDOWS
Jesus Cea14c81ab2012-10-05 02:11:36 +0200608#if defined(__VMS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 block = !block;
610 ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);
Jesus Cea14c81ab2012-10-05 02:11:36 +0200611#else /* !__VMS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
613 if (block)
614 delay_flag &= (~O_NONBLOCK);
615 else
616 delay_flag |= O_NONBLOCK;
617 fcntl(s->sock_fd, F_SETFL, delay_flag);
Jesus Cea14c81ab2012-10-05 02:11:36 +0200618#endif /* !__VMS */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000619#else /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 block = !block;
621 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000622#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 /* Since these don't return anything */
626 return 1;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000627}
628
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000629/* Do a select()/poll() on the socket, if necessary (sock_timeout > 0).
Guido van Rossum11ba0942002-06-13 15:07:44 +0000630 The argument writing indicates the direction.
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000631 This does not raise an exception; we'll let our caller do that
632 after they've reacquired the interpreter lock.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000633 Returns 1 on timeout, -1 on error, 0 otherwise. */
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000634static int
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000635internal_select_ex(PySocketSockObject *s, int writing, double interval)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000636{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 int n;
Guido van Rossum11ba0942002-06-13 15:07:44 +0000638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 /* Nothing to do unless we're in timeout mode (not non-blocking) */
640 if (s->sock_timeout <= 0.0)
641 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 /* Guard against closed socket */
644 if (s->sock_fd < 0)
645 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000646
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000647 /* Handling this condition here simplifies the select loops */
648 if (interval < 0.0)
649 return 1;
650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 /* Prefer poll, if available, since you can poll() any fd
652 * which can't be done with select(). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000653#ifdef HAVE_POLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 {
655 struct pollfd pollfd;
656 int timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 pollfd.fd = s->sock_fd;
659 pollfd.events = writing ? POLLOUT : POLLIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 /* s->sock_timeout is in seconds, timeout in ms */
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000662 timeout = (int)(interval * 1000 + 0.5);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 n = poll(&pollfd, 1, timeout);
664 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000665#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 {
667 /* Construct the arguments to select */
668 fd_set fds;
669 struct timeval tv;
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000670 tv.tv_sec = (int)interval;
671 tv.tv_usec = (int)((interval - tv.tv_sec) * 1e6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 FD_ZERO(&fds);
673 FD_SET(s->sock_fd, &fds);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 /* See if the socket is ready */
676 if (writing)
Antoine Pitrou19467d22010-08-17 19:33:30 +0000677 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
678 NULL, &fds, NULL, &tv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 else
Antoine Pitrou19467d22010-08-17 19:33:30 +0000680 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
681 &fds, NULL, NULL, &tv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000683#endif
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 if (n < 0)
686 return -1;
687 if (n == 0)
688 return 1;
689 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000690}
691
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000692static int
693internal_select(PySocketSockObject *s, int writing)
694{
695 return internal_select_ex(s, writing, s->sock_timeout);
696}
697
698/*
699 Two macros for automatic retry of select() in case of false positives
700 (for example, select() could indicate a socket is ready for reading
701 but the data then discarded by the OS because of a wrong checksum).
702 Here is an example of use:
703
704 BEGIN_SELECT_LOOP(s)
705 Py_BEGIN_ALLOW_THREADS
706 timeout = internal_select_ex(s, 0, interval);
707 if (!timeout)
708 outlen = recv(s->sock_fd, cbuf, len, flags);
709 Py_END_ALLOW_THREADS
710 if (timeout == 1) {
711 PyErr_SetString(socket_timeout, "timed out");
712 return -1;
713 }
714 END_SELECT_LOOP(s)
715*/
716
717#define BEGIN_SELECT_LOOP(s) \
718 { \
719 _PyTime_timeval now, deadline = {0, 0}; \
720 double interval = s->sock_timeout; \
721 int has_timeout = s->sock_timeout > 0.0; \
722 if (has_timeout) { \
723 _PyTime_gettimeofday(&now); \
724 deadline = now; \
725 _PyTime_ADD_SECONDS(deadline, s->sock_timeout); \
726 } \
727 while (1) { \
728 errno = 0; \
729
730#define END_SELECT_LOOP(s) \
731 if (!has_timeout || \
732 (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \
733 break; \
734 _PyTime_gettimeofday(&now); \
735 interval = _PyTime_INTERVAL(now, deadline); \
736 } \
737 } \
738
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000739/* Initialize a new socket object. */
740
Tim Petersa12b4cf2002-07-18 22:38:44 +0000741static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000742
Martin v. Löwis1a214512008-06-11 05:26:20 +0000743static void
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000744init_sockobject(PySocketSockObject *s,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 SOCKET_T fd, int family, int type, int proto)
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 s->sock_fd = fd;
748 s->sock_family = family;
749 s->sock_type = type;
750 s->sock_proto = proto;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 s->errorhandler = &set_error;
Antoine Pitroub1c54962010-10-14 15:05:38 +0000753#ifdef SOCK_NONBLOCK
754 if (type & SOCK_NONBLOCK)
755 s->sock_timeout = 0.0;
756 else
757#endif
758 {
759 s->sock_timeout = defaulttimeout;
760 if (defaulttimeout >= 0.0)
761 internal_setblocking(s, 0);
762 }
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000763
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000764}
765
766
Guido van Rossum30a685f1991-06-27 15:51:29 +0000767/* Create a new socket object.
768 This just creates the object and initializes it.
769 If the creation fails, return NULL and set an exception (implicit
770 in NEWOBJ()). */
771
Guido van Rossum73624e91994-10-10 17:59:00 +0000772static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000773new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 PySocketSockObject *s;
776 s = (PySocketSockObject *)
777 PyType_GenericNew(&sock_type, NULL, NULL);
778 if (s != NULL)
779 init_sockobject(s, fd, family, type, proto);
780 return s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000781}
782
Guido van Rossum30a685f1991-06-27 15:51:29 +0000783
Guido van Rossum48a680c2001-03-02 06:34:14 +0000784/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000785 thread to be in gethostbyname or getaddrinfo */
786#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Matthias Klosed182a6c2012-08-14 17:29:04 +0200787static PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000788#endif
789
790
Guido van Rossum30a685f1991-06-27 15:51:29 +0000791/* Convert a string specifying a host name or one of a few symbolic
792 names to a numeric IP address. This usually calls gethostbyname()
793 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000794 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000795 an error occurred; then an exception is raised. */
796
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000797static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000798setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 struct addrinfo hints, *res;
801 int error;
802 int d1, d2, d3, d4;
803 char ch;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
806 if (name[0] == '\0') {
807 int siz;
808 memset(&hints, 0, sizeof(hints));
809 hints.ai_family = af;
810 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
811 hints.ai_flags = AI_PASSIVE;
812 Py_BEGIN_ALLOW_THREADS
813 ACQUIRE_GETADDRINFO_LOCK
814 error = getaddrinfo(NULL, "0", &hints, &res);
815 Py_END_ALLOW_THREADS
816 /* We assume that those thread-unsafe getaddrinfo() versions
817 *are* safe regarding their return value, ie. that a
818 subsequent call to getaddrinfo() does not destroy the
819 outcome of the first call. */
820 RELEASE_GETADDRINFO_LOCK
821 if (error) {
822 set_gaierror(error);
823 return -1;
824 }
825 switch (res->ai_family) {
826 case AF_INET:
827 siz = 4;
828 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000829#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 case AF_INET6:
831 siz = 16;
832 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000833#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 default:
835 freeaddrinfo(res);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200836 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 "unsupported address family");
838 return -1;
839 }
840 if (res->ai_next) {
841 freeaddrinfo(res);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200842 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 "wildcard resolved to multiple address");
844 return -1;
845 }
846 if (res->ai_addrlen < addr_ret_size)
847 addr_ret_size = res->ai_addrlen;
848 memcpy(addr_ret, res->ai_addr, addr_ret_size);
849 freeaddrinfo(res);
850 return siz;
851 }
852 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
853 struct sockaddr_in *sin;
854 if (af != AF_INET && af != AF_UNSPEC) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200855 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 "address family mismatched");
857 return -1;
858 }
859 sin = (struct sockaddr_in *)addr_ret;
860 memset((void *) sin, '\0', sizeof(*sin));
861 sin->sin_family = AF_INET;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000862#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 sin->sin_len = sizeof(*sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000864#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 sin->sin_addr.s_addr = INADDR_BROADCAST;
866 return sizeof(sin->sin_addr);
867 }
868 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
869 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
870 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
871 struct sockaddr_in *sin;
872 sin = (struct sockaddr_in *)addr_ret;
873 sin->sin_addr.s_addr = htonl(
874 ((long) d1 << 24) | ((long) d2 << 16) |
875 ((long) d3 << 8) | ((long) d4 << 0));
876 sin->sin_family = AF_INET;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000877#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 sin->sin_len = sizeof(*sin);
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000879#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 return 4;
881 }
882 memset(&hints, 0, sizeof(hints));
883 hints.ai_family = af;
884 Py_BEGIN_ALLOW_THREADS
885 ACQUIRE_GETADDRINFO_LOCK
886 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000887#if defined(__digital__) && defined(__unix__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 if (error == EAI_NONAME && af == AF_UNSPEC) {
889 /* On Tru64 V5.1, numeric-to-addr conversion fails
890 if no address family is given. Assume IPv4 for now.*/
891 hints.ai_family = AF_INET;
892 error = getaddrinfo(name, NULL, &hints, &res);
893 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000894#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 Py_END_ALLOW_THREADS
896 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
897 if (error) {
898 set_gaierror(error);
899 return -1;
900 }
901 if (res->ai_addrlen < addr_ret_size)
902 addr_ret_size = res->ai_addrlen;
903 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
904 freeaddrinfo(res);
905 switch (addr_ret->sa_family) {
906 case AF_INET:
907 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000908#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 case AF_INET6:
910 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000911#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200913 PyErr_SetString(PyExc_OSError, "unknown address family");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 return -1;
915 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000916}
917
Guido van Rossum30a685f1991-06-27 15:51:29 +0000918
Guido van Rossum30a685f1991-06-27 15:51:29 +0000919/* Create a string object representing an IP address.
920 This is always a string of the form 'dd.dd.dd.dd' (with variable
921 size numbers). */
922
Guido van Rossum73624e91994-10-10 17:59:00 +0000923static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000924makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 char buf[NI_MAXHOST];
927 int error;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
930 NI_NUMERICHOST);
931 if (error) {
932 set_gaierror(error);
933 return NULL;
934 }
935 return PyUnicode_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000936}
937
938
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000939#ifdef USE_BLUETOOTH
940/* Convert a string representation of a Bluetooth address into a numeric
941 address. Returns the length (6), or raises an exception and returns -1 if
942 an error occurred. */
943
944static int
945setbdaddr(char *name, bdaddr_t *bdaddr)
946{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 unsigned int b0, b1, b2, b3, b4, b5;
948 char ch;
949 int n;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
952 &b5, &b4, &b3, &b2, &b1, &b0, &ch);
953 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
954 bdaddr->b[0] = b0;
955 bdaddr->b[1] = b1;
956 bdaddr->b[2] = b2;
957 bdaddr->b[3] = b3;
958 bdaddr->b[4] = b4;
959 bdaddr->b[5] = b5;
960 return 6;
961 } else {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200962 PyErr_SetString(PyExc_OSError, "bad bluetooth address");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 return -1;
964 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000965}
966
967/* Create a string representation of the Bluetooth address. This is always a
968 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
969 value (zero padded if necessary). */
970
971static PyObject *
972makebdaddr(bdaddr_t *bdaddr)
973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 char buf[(6 * 2) + 5 + 1];
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
977 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
978 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
979 return PyUnicode_FromString(buf);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000980}
981#endif
982
983
Guido van Rossum30a685f1991-06-27 15:51:29 +0000984/* Create an object representing the given socket address,
985 suitable for passing it back to bind(), connect() etc.
986 The family field of the sockaddr structure is inspected
987 to determine what kind of address it really is. */
988
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000989/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000990static PyObject *
Antoine Pitrou19467d22010-08-17 19:33:30 +0000991makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000992{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 if (addrlen == 0) {
994 /* No address -- may be recvfrom() from known socket */
995 Py_INCREF(Py_None);
996 return Py_None;
997 }
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 switch (addr->sa_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +00001000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 case AF_INET:
1002 {
1003 struct sockaddr_in *a;
1004 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
1005 PyObject *ret = NULL;
1006 if (addrobj) {
1007 a = (struct sockaddr_in *)addr;
1008 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
1009 Py_DECREF(addrobj);
1010 }
1011 return ret;
1012 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00001013
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001014#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 case AF_UNIX:
1016 {
1017 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001018#ifdef linux
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 if (a->sun_path[0] == 0) { /* Linux abstract namespace */
1020 addrlen -= offsetof(struct sockaddr_un, sun_path);
1021 return PyBytes_FromStringAndSize(a->sun_path, addrlen);
1022 }
1023 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001024#endif /* linux */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 {
1026 /* regular NULL-terminated string */
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001027 return PyUnicode_DecodeFSDefault(a->sun_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 }
1029 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001030#endif /* AF_UNIX */
1031
Martin v. Löwis11017b12006-01-14 18:12:57 +00001032#if defined(AF_NETLINK)
1033 case AF_NETLINK:
1034 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
1036 return Py_BuildValue("II", a->nl_pid, a->nl_groups);
Martin v. Löwis11017b12006-01-14 18:12:57 +00001037 }
1038#endif /* AF_NETLINK */
1039
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001040#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 case AF_INET6:
1042 {
1043 struct sockaddr_in6 *a;
1044 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
1045 PyObject *ret = NULL;
1046 if (addrobj) {
1047 a = (struct sockaddr_in6 *)addr;
Charles-François Natali366999a2012-01-02 15:47:29 +01001048 ret = Py_BuildValue("OiII",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 addrobj,
1050 ntohs(a->sin6_port),
Charles-François Natali366999a2012-01-02 15:47:29 +01001051 ntohl(a->sin6_flowinfo),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 a->sin6_scope_id);
1053 Py_DECREF(addrobj);
1054 }
1055 return ret;
1056 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001057#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001058
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001059#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 case AF_BLUETOOTH:
1061 switch (proto) {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 case BTPROTO_L2CAP:
1064 {
1065 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
1066 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
1067 PyObject *ret = NULL;
1068 if (addrobj) {
1069 ret = Py_BuildValue("Oi",
1070 addrobj,
1071 _BT_L2_MEMB(a, psm));
1072 Py_DECREF(addrobj);
1073 }
1074 return ret;
1075 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 case BTPROTO_RFCOMM:
1078 {
1079 struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
1080 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
1081 PyObject *ret = NULL;
1082 if (addrobj) {
1083 ret = Py_BuildValue("Oi",
1084 addrobj,
1085 _BT_RC_MEMB(a, channel));
1086 Py_DECREF(addrobj);
1087 }
1088 return ret;
1089 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 case BTPROTO_HCI:
1092 {
1093 struct sockaddr_hci *a = (struct sockaddr_hci *) addr;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001094#if defined(__NetBSD__) || defined(__DragonFly__)
1095 return makebdaddr(&_BT_HCI_MEMB(a, bdaddr));
1096#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 PyObject *ret = NULL;
1098 ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
1099 return ret;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001100#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 }
Thomas Wouterscf297e42007-02-23 15:07:44 +00001102
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001103#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 case BTPROTO_SCO:
1105 {
1106 struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
1107 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
1108 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001109#endif
1110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 default:
1112 PyErr_SetString(PyExc_ValueError,
1113 "Unknown Bluetooth protocol");
1114 return NULL;
1115 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001116#endif
1117
Antoine Pitroub156a462010-10-27 20:13:57 +00001118#if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 case AF_PACKET:
1120 {
1121 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
1122 char *ifname = "";
1123 struct ifreq ifr;
1124 /* need to look up interface name give index */
1125 if (a->sll_ifindex) {
1126 ifr.ifr_ifindex = a->sll_ifindex;
1127 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
1128 ifname = ifr.ifr_name;
1129 }
1130 return Py_BuildValue("shbhy#",
1131 ifname,
1132 ntohs(a->sll_protocol),
1133 a->sll_pkttype,
1134 a->sll_hatype,
1135 a->sll_addr,
1136 a->sll_halen);
1137 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001138#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001139
Christian Heimes043d6f62008-01-07 17:19:16 +00001140#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 case AF_TIPC:
1142 {
1143 struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr;
1144 if (a->addrtype == TIPC_ADDR_NAMESEQ) {
1145 return Py_BuildValue("IIIII",
1146 a->addrtype,
1147 a->addr.nameseq.type,
1148 a->addr.nameseq.lower,
1149 a->addr.nameseq.upper,
1150 a->scope);
1151 } else if (a->addrtype == TIPC_ADDR_NAME) {
1152 return Py_BuildValue("IIIII",
1153 a->addrtype,
1154 a->addr.name.name.type,
1155 a->addr.name.name.instance,
1156 a->addr.name.name.instance,
1157 a->scope);
1158 } else if (a->addrtype == TIPC_ADDR_ID) {
1159 return Py_BuildValue("IIIII",
1160 a->addrtype,
1161 a->addr.id.node,
1162 a->addr.id.ref,
1163 0,
1164 a->scope);
1165 } else {
1166 PyErr_SetString(PyExc_ValueError,
1167 "Invalid address type");
1168 return NULL;
1169 }
1170 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001171#endif
1172
Charles-François Natali30589c92011-10-07 22:47:08 +02001173#ifdef AF_CAN
Charles-François Natali47413c12011-10-06 19:47:44 +02001174 case AF_CAN:
1175 {
1176 struct sockaddr_can *a = (struct sockaddr_can *)addr;
1177 char *ifname = "";
1178 struct ifreq ifr;
1179 /* need to look up interface name given index */
1180 if (a->can_ifindex) {
1181 ifr.ifr_ifindex = a->can_ifindex;
1182 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
1183 ifname = ifr.ifr_name;
1184 }
1185
1186 return Py_BuildValue("O&h", PyUnicode_DecodeFSDefault,
1187 ifname,
1188 a->can_family);
1189 }
1190#endif
1191
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001192#ifdef PF_SYSTEM
1193 case PF_SYSTEM:
1194 switch(proto) {
1195#ifdef SYSPROTO_CONTROL
1196 case SYSPROTO_CONTROL:
1197 {
1198 struct sockaddr_ctl *a = (struct sockaddr_ctl *)addr;
1199 return Py_BuildValue("(II)", a->sc_id, a->sc_unit);
1200 }
1201#endif
1202 default:
1203 PyErr_SetString(PyExc_ValueError,
1204 "Invalid address type");
1205 return 0;
1206 }
1207#endif
1208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 default:
1212 /* If we don't know the address family, don't raise an
1213 exception -- return it as an (int, bytes) tuple. */
1214 return Py_BuildValue("iy#",
1215 addr->sa_family,
1216 addr->sa_data,
1217 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001220}
1221
Guido van Rossum30a685f1991-06-27 15:51:29 +00001222
1223/* Parse a socket address argument according to the socket object's
1224 address family. Return 1 if the address was in the proper format,
1225 0 of not. The address is returned through addr_ret, its length
1226 through len_ret. */
1227
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001228static int
Guido van Rossum48a680c2001-03-02 06:34:14 +00001229getsockaddrarg(PySocketSockObject *s, PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 struct sockaddr *addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 switch (s->sock_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +00001233
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001234#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 case AF_UNIX:
1236 {
1237 struct sockaddr_un* addr;
1238 char *path;
1239 int len;
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001240 int retval = 0;
1241
1242 /* PEP 383. Not using PyUnicode_FSConverter since we need to
1243 allow embedded nulls on Linux. */
1244 if (PyUnicode_Check(args)) {
1245 if ((args = PyUnicode_EncodeFSDefault(args)) == NULL)
1246 return 0;
1247 }
1248 else
1249 Py_INCREF(args);
1250 if (!PyArg_Parse(args, "y#", &path, &len))
1251 goto unix_out;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 addr = (struct sockaddr_un*)addr_ret;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001254#ifdef linux
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 if (len > 0 && path[0] == 0) {
1256 /* Linux abstract namespace extension */
1257 if (len > sizeof addr->sun_path) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001258 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 "AF_UNIX path too long");
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001260 goto unix_out;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 }
1262 }
1263 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001264#endif /* linux */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 {
1266 /* regular NULL-terminated string */
1267 if (len >= sizeof addr->sun_path) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001268 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 "AF_UNIX path too long");
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001270 goto unix_out;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 }
1272 addr->sun_path[len] = 0;
1273 }
1274 addr->sun_family = s->sock_family;
1275 memcpy(addr->sun_path, path, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 *len_ret = len + offsetof(struct sockaddr_un, sun_path);
Antoine Pitrou6ec29e22011-12-16 14:46:36 +01001277 retval = 1;
1278 unix_out:
1279 Py_DECREF(args);
1280 return retval;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001282#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001283
Martin v. Löwis11017b12006-01-14 18:12:57 +00001284#if defined(AF_NETLINK)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 case AF_NETLINK:
1286 {
1287 struct sockaddr_nl* addr;
1288 int pid, groups;
1289 addr = (struct sockaddr_nl *)addr_ret;
1290 if (!PyTuple_Check(args)) {
1291 PyErr_Format(
1292 PyExc_TypeError,
1293 "getsockaddrarg: "
1294 "AF_NETLINK address must be tuple, not %.500s",
1295 Py_TYPE(args)->tp_name);
1296 return 0;
1297 }
1298 if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
1299 return 0;
1300 addr->nl_family = AF_NETLINK;
1301 addr->nl_pid = pid;
1302 addr->nl_groups = groups;
1303 *len_ret = sizeof(*addr);
1304 return 1;
1305 }
Martin v. Löwis11017b12006-01-14 18:12:57 +00001306#endif
1307
Charles-François Natali10b8cf42011-11-10 19:21:37 +01001308#ifdef AF_RDS
1309 case AF_RDS:
1310 /* RDS sockets use sockaddr_in: fall-through */
1311#endif
1312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 case AF_INET:
1314 {
1315 struct sockaddr_in* addr;
1316 char *host;
1317 int port, result;
1318 if (!PyTuple_Check(args)) {
1319 PyErr_Format(
1320 PyExc_TypeError,
1321 "getsockaddrarg: "
1322 "AF_INET address must be tuple, not %.500s",
1323 Py_TYPE(args)->tp_name);
1324 return 0;
1325 }
1326 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
1327 "idna", &host, &port))
1328 return 0;
1329 addr=(struct sockaddr_in*)addr_ret;
1330 result = setipaddr(host, (struct sockaddr *)addr,
1331 sizeof(*addr), AF_INET);
1332 PyMem_Free(host);
1333 if (result < 0)
1334 return 0;
1335 if (port < 0 || port > 0xffff) {
1336 PyErr_SetString(
1337 PyExc_OverflowError,
1338 "getsockaddrarg: port must be 0-65535.");
1339 return 0;
1340 }
1341 addr->sin_family = AF_INET;
1342 addr->sin_port = htons((short)port);
1343 *len_ret = sizeof *addr;
1344 return 1;
1345 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001346
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001347#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 case AF_INET6:
1349 {
1350 struct sockaddr_in6* addr;
1351 char *host;
Charles-François Natali366999a2012-01-02 15:47:29 +01001352 int port, result;
1353 unsigned int flowinfo, scope_id;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 flowinfo = scope_id = 0;
1355 if (!PyTuple_Check(args)) {
1356 PyErr_Format(
1357 PyExc_TypeError,
1358 "getsockaddrarg: "
1359 "AF_INET6 address must be tuple, not %.500s",
1360 Py_TYPE(args)->tp_name);
1361 return 0;
1362 }
Charles-François Natali366999a2012-01-02 15:47:29 +01001363 if (!PyArg_ParseTuple(args, "eti|II",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 "idna", &host, &port, &flowinfo,
1365 &scope_id)) {
1366 return 0;
1367 }
1368 addr = (struct sockaddr_in6*)addr_ret;
1369 result = setipaddr(host, (struct sockaddr *)addr,
1370 sizeof(*addr), AF_INET6);
1371 PyMem_Free(host);
1372 if (result < 0)
1373 return 0;
1374 if (port < 0 || port > 0xffff) {
1375 PyErr_SetString(
1376 PyExc_OverflowError,
1377 "getsockaddrarg: port must be 0-65535.");
1378 return 0;
1379 }
Charles-François Natali9b0c0062012-06-23 10:17:05 +02001380 if (flowinfo > 0xfffff) {
Charles-François Natali366999a2012-01-02 15:47:29 +01001381 PyErr_SetString(
1382 PyExc_OverflowError,
1383 "getsockaddrarg: flowinfo must be 0-1048575.");
1384 return 0;
1385 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 addr->sin6_family = s->sock_family;
1387 addr->sin6_port = htons((short)port);
Charles-François Natali366999a2012-01-02 15:47:29 +01001388 addr->sin6_flowinfo = htonl(flowinfo);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 addr->sin6_scope_id = scope_id;
1390 *len_ret = sizeof *addr;
1391 return 1;
1392 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001393#endif
1394
Hye-Shik Chang81268602004-02-02 06:05:24 +00001395#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 case AF_BLUETOOTH:
1397 {
1398 switch (s->sock_proto) {
1399 case BTPROTO_L2CAP:
1400 {
1401 struct sockaddr_l2 *addr;
1402 char *straddr;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 addr = (struct sockaddr_l2 *)addr_ret;
1405 memset(addr, 0, sizeof(struct sockaddr_l2));
1406 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
1407 if (!PyArg_ParseTuple(args, "si", &straddr,
1408 &_BT_L2_MEMB(addr, psm))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001409 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 "wrong format");
1411 return 0;
1412 }
1413 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
1414 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 *len_ret = sizeof *addr;
1417 return 1;
1418 }
1419 case BTPROTO_RFCOMM:
1420 {
1421 struct sockaddr_rc *addr;
1422 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 addr = (struct sockaddr_rc *)addr_ret;
1425 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
1426 if (!PyArg_ParseTuple(args, "si", &straddr,
1427 &_BT_RC_MEMB(addr, channel))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001428 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 "wrong format");
1430 return 0;
1431 }
1432 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
1433 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 *len_ret = sizeof *addr;
1436 return 1;
1437 }
1438 case BTPROTO_HCI:
1439 {
1440 struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001441#if defined(__NetBSD__) || defined(__DragonFly__)
Alexander Belopolskye239d232010-12-08 23:31:48 +00001442 char *straddr = PyBytes_AS_STRING(args);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001443
Alexander Belopolskye239d232010-12-08 23:31:48 +00001444 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001445 if (straddr == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001446 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001447 "wrong format");
1448 return 0;
1449 }
1450 if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0)
1451 return 0;
1452#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
1454 if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001455 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 "wrong format");
1457 return 0;
1458 }
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00001459#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 *len_ret = sizeof *addr;
1461 return 1;
1462 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001463#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 case BTPROTO_SCO:
1465 {
1466 struct sockaddr_sco *addr;
1467 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 addr = (struct sockaddr_sco *)addr_ret;
1470 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
1471 if (!PyBytes_Check(args)) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001472 PyErr_SetString(PyExc_OSError, "getsockaddrarg: "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 "wrong format");
1474 return 0;
1475 }
1476 straddr = PyBytes_AS_STRING(args);
1477 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
1478 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 *len_ret = sizeof *addr;
1481 return 1;
1482 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001483#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 default:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001485 PyErr_SetString(PyExc_OSError, "getsockaddrarg: unknown Bluetooth protocol");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 return 0;
1487 }
1488 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001489#endif
1490
Antoine Pitroub156a462010-10-27 20:13:57 +00001491#if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 case AF_PACKET:
1493 {
1494 struct sockaddr_ll* addr;
1495 struct ifreq ifr;
1496 char *interfaceName;
1497 int protoNumber;
1498 int hatype = 0;
1499 int pkttype = 0;
1500 char *haddr = NULL;
1501 unsigned int halen = 0;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 if (!PyTuple_Check(args)) {
1504 PyErr_Format(
1505 PyExc_TypeError,
1506 "getsockaddrarg: "
1507 "AF_PACKET address must be tuple, not %.500s",
1508 Py_TYPE(args)->tp_name);
1509 return 0;
1510 }
1511 if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName,
1512 &protoNumber, &pkttype, &hatype,
1513 &haddr, &halen))
1514 return 0;
1515 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1516 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
1517 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
1518 s->errorhandler();
1519 return 0;
1520 }
1521 if (halen > 8) {
1522 PyErr_SetString(PyExc_ValueError,
1523 "Hardware address must be 8 bytes or less");
1524 return 0;
1525 }
1526 if (protoNumber < 0 || protoNumber > 0xffff) {
1527 PyErr_SetString(
1528 PyExc_OverflowError,
1529 "getsockaddrarg: protoNumber must be 0-65535.");
1530 return 0;
1531 }
1532 addr = (struct sockaddr_ll*)addr_ret;
1533 addr->sll_family = AF_PACKET;
1534 addr->sll_protocol = htons((short)protoNumber);
1535 addr->sll_ifindex = ifr.ifr_ifindex;
1536 addr->sll_pkttype = pkttype;
1537 addr->sll_hatype = hatype;
1538 if (halen != 0) {
1539 memcpy(&addr->sll_addr, haddr, halen);
1540 }
1541 addr->sll_halen = halen;
1542 *len_ret = sizeof *addr;
1543 return 1;
1544 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001545#endif
1546
Christian Heimes043d6f62008-01-07 17:19:16 +00001547#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 case AF_TIPC:
1549 {
1550 unsigned int atype, v1, v2, v3;
1551 unsigned int scope = TIPC_CLUSTER_SCOPE;
1552 struct sockaddr_tipc *addr;
Christian Heimes043d6f62008-01-07 17:19:16 +00001553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 if (!PyTuple_Check(args)) {
1555 PyErr_Format(
1556 PyExc_TypeError,
1557 "getsockaddrarg: "
1558 "AF_TIPC address must be tuple, not %.500s",
1559 Py_TYPE(args)->tp_name);
1560 return 0;
1561 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 if (!PyArg_ParseTuple(args,
1564 "IIII|I;Invalid TIPC address format",
1565 &atype, &v1, &v2, &v3, &scope))
1566 return 0;
Christian Heimes043d6f62008-01-07 17:19:16 +00001567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 addr = (struct sockaddr_tipc *) addr_ret;
1569 memset(addr, 0, sizeof(struct sockaddr_tipc));
Christian Heimes043d6f62008-01-07 17:19:16 +00001570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 addr->family = AF_TIPC;
1572 addr->scope = scope;
1573 addr->addrtype = atype;
Christian Heimes043d6f62008-01-07 17:19:16 +00001574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 if (atype == TIPC_ADDR_NAMESEQ) {
1576 addr->addr.nameseq.type = v1;
1577 addr->addr.nameseq.lower = v2;
1578 addr->addr.nameseq.upper = v3;
1579 } else if (atype == TIPC_ADDR_NAME) {
1580 addr->addr.name.name.type = v1;
1581 addr->addr.name.name.instance = v2;
1582 } else if (atype == TIPC_ADDR_ID) {
1583 addr->addr.id.node = v1;
1584 addr->addr.id.ref = v2;
1585 } else {
1586 /* Shouldn't happen */
1587 PyErr_SetString(PyExc_TypeError, "Invalid address type");
1588 return 0;
1589 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 *len_ret = sizeof(*addr);
Christian Heimes043d6f62008-01-07 17:19:16 +00001592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 return 1;
1594 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001595#endif
1596
Charles-François Natali30589c92011-10-07 22:47:08 +02001597#ifdef AF_CAN
Charles-François Natali47413c12011-10-06 19:47:44 +02001598 case AF_CAN:
1599 switch (s->sock_proto) {
1600 case CAN_RAW:
1601 {
1602 struct sockaddr_can *addr;
1603 PyObject *interfaceName;
1604 struct ifreq ifr;
1605 addr = (struct sockaddr_can *)addr_ret;
1606 Py_ssize_t len;
1607
1608 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
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001644
1645#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;
1652
1653 addr = (struct sockaddr_ctl *)addr_ret;
1654 addr->sc_family = AF_SYSTEM;
1655 addr->ss_sysaddr = AF_SYS_CONTROL;
1656
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 }
1681
1682 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");
1688
1689 return 0;
1690 }
1691
1692 *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
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01001808
1809#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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 int 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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 backlog = PyLong_AsLong(arg);
2499 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
3547 ensures that that doesn't happen. */
3548 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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003650 how = PyLong_AsLong(arg);
3651 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';
4048 return PyUnicode_FromString(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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004819 char packed[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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004870 char ip[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 }
4978 memset(&hints, 0, sizeof(hints));
4979 hints.ai_family = family;
4980 hints.ai_socktype = socktype;
4981 hints.ai_protocol = protocol;
4982 hints.ai_flags = flags;
4983 Py_BEGIN_ALLOW_THREADS
4984 ACQUIRE_GETADDRINFO_LOCK
4985 error = getaddrinfo(hptr, pptr, &hints, &res0);
4986 Py_END_ALLOW_THREADS
4987 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
4988 if (error) {
4989 set_gaierror(error);
4990 goto err;
4991 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004993 if ((all = PyList_New(0)) == NULL)
4994 goto err;
4995 for (res = res0; res; res = res->ai_next) {
4996 PyObject *single;
4997 PyObject *addr =
4998 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
4999 if (addr == NULL)
5000 goto err;
5001 single = Py_BuildValue("iiisO", res->ai_family,
5002 res->ai_socktype, res->ai_protocol,
5003 res->ai_canonname ? res->ai_canonname : "",
5004 addr);
5005 Py_DECREF(addr);
5006 if (single == NULL)
5007 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005009 if (PyList_Append(all, single))
5010 goto err;
5011 Py_XDECREF(single);
5012 }
5013 Py_XDECREF(idna);
5014 if (res0)
5015 freeaddrinfo(res0);
5016 return all;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005017 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005018 Py_XDECREF(all);
5019 Py_XDECREF(idna);
5020 if (res0)
5021 freeaddrinfo(res0);
5022 return (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005023}
5024
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005025PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005026"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
5027 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005028\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005029Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005030
5031/* Python interface to getnameinfo(sa, flags). */
5032
5033/*ARGSUSED*/
5034static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005035socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005037 PyObject *sa = (PyObject *)NULL;
5038 int flags;
5039 char *hostp;
Charles-François Natali366999a2012-01-02 15:47:29 +01005040 int port;
5041 unsigned int flowinfo, scope_id;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005042 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
5043 struct addrinfo hints, *res = NULL;
5044 int error;
5045 PyObject *ret = (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005047 flags = flowinfo = scope_id = 0;
5048 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
5049 return NULL;
5050 if (!PyTuple_Check(sa)) {
5051 PyErr_SetString(PyExc_TypeError,
5052 "getnameinfo() argument 1 must be a tuple");
5053 return NULL;
5054 }
Charles-François Natali366999a2012-01-02 15:47:29 +01005055 if (!PyArg_ParseTuple(sa, "si|II",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005056 &hostp, &port, &flowinfo, &scope_id))
5057 return NULL;
Charles-François Natali9b0c0062012-06-23 10:17:05 +02005058 if (flowinfo > 0xfffff) {
Charles-François Natali366999a2012-01-02 15:47:29 +01005059 PyErr_SetString(PyExc_OverflowError,
5060 "getsockaddrarg: flowinfo must be 0-1048575.");
5061 return NULL;
5062 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005063 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
5064 memset(&hints, 0, sizeof(hints));
5065 hints.ai_family = AF_UNSPEC;
5066 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Martin v. Löwis112c0f32010-08-25 07:38:15 +00005067 hints.ai_flags = AI_NUMERICHOST; /* don't do any name resolution */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005068 Py_BEGIN_ALLOW_THREADS
5069 ACQUIRE_GETADDRINFO_LOCK
5070 error = getaddrinfo(hostp, pbuf, &hints, &res);
5071 Py_END_ALLOW_THREADS
5072 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
5073 if (error) {
5074 set_gaierror(error);
5075 goto fail;
5076 }
5077 if (res->ai_next) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005078 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005079 "sockaddr resolved to multiple addresses");
5080 goto fail;
5081 }
5082 switch (res->ai_family) {
5083 case AF_INET:
5084 {
5085 if (PyTuple_GET_SIZE(sa) != 2) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005086 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005087 "IPv4 sockaddr must be 2 tuple");
5088 goto fail;
5089 }
5090 break;
5091 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00005092#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005093 case AF_INET6:
5094 {
5095 struct sockaddr_in6 *sin6;
5096 sin6 = (struct sockaddr_in6 *)res->ai_addr;
Charles-François Natali366999a2012-01-02 15:47:29 +01005097 sin6->sin6_flowinfo = htonl(flowinfo);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005098 sin6->sin6_scope_id = scope_id;
5099 break;
5100 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005101#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005102 }
Antoine Pitrou19467d22010-08-17 19:33:30 +00005103 error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005104 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
5105 if (error) {
5106 set_gaierror(error);
5107 goto fail;
5108 }
5109 ret = Py_BuildValue("ss", hbuf, pbuf);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005110
5111fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005112 if (res)
5113 freeaddrinfo(res);
5114 return ret;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005115}
5116
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005117PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005118"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005119\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005120Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005121
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005122
5123/* Python API to getting and setting the default timeout value. */
5124
5125static PyObject *
5126socket_getdefaulttimeout(PyObject *self)
5127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005128 if (defaulttimeout < 0.0) {
5129 Py_INCREF(Py_None);
5130 return Py_None;
5131 }
5132 else
5133 return PyFloat_FromDouble(defaulttimeout);
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005134}
5135
5136PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005137"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005138\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03005139Returns the default timeout in seconds (float) for new socket objects.\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005140A value of None indicates that new socket objects have no timeout.\n\
5141When the socket module is first imported, the default is None.");
5142
5143static PyObject *
5144socket_setdefaulttimeout(PyObject *self, PyObject *arg)
5145{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005146 double timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005148 if (arg == Py_None)
5149 timeout = -1.0;
5150 else {
5151 timeout = PyFloat_AsDouble(arg);
5152 if (timeout < 0.0) {
5153 if (!PyErr_Occurred())
5154 PyErr_SetString(PyExc_ValueError,
5155 "Timeout value out of range");
5156 return NULL;
5157 }
5158 }
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005160 defaulttimeout = timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005162 Py_INCREF(Py_None);
5163 return Py_None;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005164}
5165
5166PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005167"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005168\n\
Ezio Melotti388c9452011-08-14 08:28:57 +03005169Set the default timeout in seconds (float) for new socket objects.\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00005170A value of None indicates that new socket objects have no timeout.\n\
5171When the socket module is first imported, the default is None.");
5172
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005173#ifdef HAVE_IF_NAMEINDEX
5174/* Python API for getting interface indices and names */
5175
5176static PyObject *
5177socket_if_nameindex(PyObject *self, PyObject *arg)
5178{
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005179 PyObject *list;
Charles-François Natali60713592011-05-20 16:55:06 +02005180 int i;
5181 struct if_nameindex *ni;
Victor Stinner77af1722011-05-26 14:05:59 +02005182
Charles-François Natali60713592011-05-20 16:55:06 +02005183 ni = if_nameindex();
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005184 if (ni == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005185 PyErr_SetFromErrno(PyExc_OSError);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005186 return NULL;
5187 }
5188
5189 list = PyList_New(0);
5190 if (list == NULL) {
5191 if_freenameindex(ni);
5192 return NULL;
5193 }
5194
Charles-François Natali60713592011-05-20 16:55:06 +02005195 for (i = 0; ni[i].if_index != 0 && i < INT_MAX; i++) {
5196 PyObject *ni_tuple = Py_BuildValue("IO&",
5197 ni[i].if_index, PyUnicode_DecodeFSDefault, ni[i].if_name);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005198
5199 if (ni_tuple == NULL || PyList_Append(list, ni_tuple) == -1) {
5200 Py_XDECREF(ni_tuple);
Charles-François Natali60713592011-05-20 16:55:06 +02005201 Py_DECREF(list);
5202 if_freenameindex(ni);
5203 return NULL;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005204 }
5205 Py_DECREF(ni_tuple);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005206 }
5207
5208 if_freenameindex(ni);
5209 return list;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005210}
5211
5212PyDoc_STRVAR(if_nameindex_doc,
5213"if_nameindex()\n\
5214\n\
5215Returns a list of network interface information (index, name) tuples.");
5216
Charles-François Natali60713592011-05-20 16:55:06 +02005217static PyObject *
5218socket_if_nametoindex(PyObject *self, PyObject *args)
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005219{
Charles-François Natali60713592011-05-20 16:55:06 +02005220 PyObject *oname;
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005221 unsigned long index;
5222
Charles-François Natali60713592011-05-20 16:55:06 +02005223 if (!PyArg_ParseTuple(args, "O&:if_nametoindex",
5224 PyUnicode_FSConverter, &oname))
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005225 return NULL;
5226
Charles-François Natali60713592011-05-20 16:55:06 +02005227 index = if_nametoindex(PyBytes_AS_STRING(oname));
5228 Py_DECREF(oname);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005229 if (index == 0) {
Charles-François Natali60713592011-05-20 16:55:06 +02005230 /* if_nametoindex() doesn't set errno */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005231 PyErr_SetString(PyExc_OSError, "no interface with this name");
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005232 return NULL;
5233 }
5234
5235 return PyLong_FromUnsignedLong(index);
5236}
5237
5238PyDoc_STRVAR(if_nametoindex_doc,
5239"if_nametoindex(if_name)\n\
5240\n\
5241Returns the interface index corresponding to the interface name if_name.");
5242
Charles-François Natali60713592011-05-20 16:55:06 +02005243static PyObject *
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005244socket_if_indextoname(PyObject *self, PyObject *arg)
5245{
Charles-François Natali60713592011-05-20 16:55:06 +02005246 unsigned long index;
5247 char name[IF_NAMESIZE + 1];
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005248
Charles-François Natali60713592011-05-20 16:55:06 +02005249 index = PyLong_AsUnsignedLong(arg);
5250 if (index == (unsigned long) -1)
5251 return NULL;
5252
5253 if (if_indextoname(index, name) == NULL) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005254 PyErr_SetFromErrno(PyExc_OSError);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005255 return NULL;
5256 }
5257
Charles-François Natali60713592011-05-20 16:55:06 +02005258 return PyUnicode_DecodeFSDefault(name);
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005259}
5260
5261PyDoc_STRVAR(if_indextoname_doc,
5262"if_indextoname(if_index)\n\
5263\n\
5264Returns the interface name corresponding to the interface index if_index.");
5265
5266#endif /* HAVE_IF_NAMEINDEX */
5267
5268
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005269#ifdef CMSG_LEN
5270/* Python interface to CMSG_LEN(length). */
5271
5272static PyObject *
5273socket_CMSG_LEN(PyObject *self, PyObject *args)
5274{
5275 Py_ssize_t length;
5276 size_t result;
5277
5278 if (!PyArg_ParseTuple(args, "n:CMSG_LEN", &length))
5279 return NULL;
5280 if (length < 0 || !get_CMSG_LEN(length, &result)) {
5281 PyErr_Format(PyExc_OverflowError, "CMSG_LEN() argument out of range");
5282 return NULL;
5283 }
5284 return PyLong_FromSize_t(result);
5285}
5286
5287PyDoc_STRVAR(CMSG_LEN_doc,
5288"CMSG_LEN(length) -> control message length\n\
5289\n\
5290Return the total length, without trailing padding, of an ancillary\n\
5291data item with associated data of the given length. This value can\n\
5292often be used as the buffer size for recvmsg() to receive a single\n\
5293item of ancillary data, but RFC 3542 requires portable applications to\n\
5294use CMSG_SPACE() and thus include space for padding, even when the\n\
5295item will be the last in the buffer. Raises OverflowError if length\n\
5296is outside the permissible range of values.");
5297
5298
5299#ifdef CMSG_SPACE
5300/* Python interface to CMSG_SPACE(length). */
5301
5302static PyObject *
5303socket_CMSG_SPACE(PyObject *self, PyObject *args)
5304{
5305 Py_ssize_t length;
5306 size_t result;
5307
5308 if (!PyArg_ParseTuple(args, "n:CMSG_SPACE", &length))
5309 return NULL;
5310 if (length < 0 || !get_CMSG_SPACE(length, &result)) {
5311 PyErr_SetString(PyExc_OverflowError,
5312 "CMSG_SPACE() argument out of range");
5313 return NULL;
5314 }
5315 return PyLong_FromSize_t(result);
5316}
5317
5318PyDoc_STRVAR(CMSG_SPACE_doc,
5319"CMSG_SPACE(length) -> buffer size\n\
5320\n\
5321Return the buffer size needed for recvmsg() to receive an ancillary\n\
5322data item with associated data of the given length, along with any\n\
5323trailing padding. The buffer space needed to receive multiple items\n\
5324is the sum of the CMSG_SPACE() values for their associated data\n\
5325lengths. Raises OverflowError if length is outside the permissible\n\
5326range of values.");
5327#endif /* CMSG_SPACE */
5328#endif /* CMSG_LEN */
5329
5330
Guido van Rossum30a685f1991-06-27 15:51:29 +00005331/* List of functions exported by this module. */
5332
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005333static PyMethodDef socket_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005334 {"gethostbyname", socket_gethostbyname,
5335 METH_VARARGS, gethostbyname_doc},
5336 {"gethostbyname_ex", socket_gethostbyname_ex,
5337 METH_VARARGS, ghbn_ex_doc},
5338 {"gethostbyaddr", socket_gethostbyaddr,
5339 METH_VARARGS, gethostbyaddr_doc},
5340 {"gethostname", socket_gethostname,
5341 METH_NOARGS, gethostname_doc},
Antoine Pitrou061cfb52011-02-28 22:25:22 +00005342#ifdef HAVE_SETHOSTNAME
5343 {"sethostname", socket_sethostname,
5344 METH_VARARGS, sethostname_doc},
5345#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005346 {"getservbyname", socket_getservbyname,
5347 METH_VARARGS, getservbyname_doc},
5348 {"getservbyport", socket_getservbyport,
5349 METH_VARARGS, getservbyport_doc},
5350 {"getprotobyname", socket_getprotobyname,
5351 METH_VARARGS, getprotobyname_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005352#ifndef NO_DUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005353 {"dup", socket_dup,
5354 METH_O, dup_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005355#endif
Dave Cole331708b2004-08-09 04:51:41 +00005356#ifdef HAVE_SOCKETPAIR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005357 {"socketpair", socket_socketpair,
5358 METH_VARARGS, socketpair_doc},
Dave Cole331708b2004-08-09 04:51:41 +00005359#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005360 {"ntohs", socket_ntohs,
5361 METH_VARARGS, ntohs_doc},
5362 {"ntohl", socket_ntohl,
5363 METH_O, ntohl_doc},
5364 {"htons", socket_htons,
5365 METH_VARARGS, htons_doc},
5366 {"htonl", socket_htonl,
5367 METH_O, htonl_doc},
5368 {"inet_aton", socket_inet_aton,
5369 METH_VARARGS, inet_aton_doc},
5370 {"inet_ntoa", socket_inet_ntoa,
5371 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005372#ifdef HAVE_INET_PTON
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005373 {"inet_pton", socket_inet_pton,
5374 METH_VARARGS, inet_pton_doc},
5375 {"inet_ntop", socket_inet_ntop,
5376 METH_VARARGS, inet_ntop_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005377#endif
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00005378 {"getaddrinfo", (PyCFunction)socket_getaddrinfo,
5379 METH_VARARGS | METH_KEYWORDS, getaddrinfo_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005380 {"getnameinfo", socket_getnameinfo,
5381 METH_VARARGS, getnameinfo_doc},
5382 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
5383 METH_NOARGS, getdefaulttimeout_doc},
5384 {"setdefaulttimeout", socket_setdefaulttimeout,
5385 METH_O, setdefaulttimeout_doc},
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005386#ifdef HAVE_IF_NAMEINDEX
5387 {"if_nameindex", socket_if_nameindex,
5388 METH_NOARGS, if_nameindex_doc},
5389 {"if_nametoindex", socket_if_nametoindex,
Charles-François Natali60713592011-05-20 16:55:06 +02005390 METH_VARARGS, if_nametoindex_doc},
Gregory P. Smith5ed2e772011-05-15 00:26:45 -07005391 {"if_indextoname", socket_if_indextoname,
5392 METH_O, if_indextoname_doc},
5393#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005394#ifdef CMSG_LEN
5395 {"CMSG_LEN", socket_CMSG_LEN,
5396 METH_VARARGS, CMSG_LEN_doc},
5397#ifdef CMSG_SPACE
5398 {"CMSG_SPACE", socket_CMSG_SPACE,
5399 METH_VARARGS, CMSG_SPACE_doc},
5400#endif
5401#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005402 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005403};
5404
Guido van Rossum30a685f1991-06-27 15:51:29 +00005405
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005406#ifdef MS_WINDOWS
5407#define OS_INIT_DEFINED
5408
5409/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00005410
5411static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005412os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00005413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005414 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00005415}
5416
5417static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005418os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00005419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005420 WSADATA WSAData;
5421 int ret;
5422 ret = WSAStartup(0x0101, &WSAData);
5423 switch (ret) {
5424 case 0: /* No error */
5425 Py_AtExit(os_cleanup);
5426 return 1; /* Success */
5427 case WSASYSNOTREADY:
5428 PyErr_SetString(PyExc_ImportError,
5429 "WSAStartup failed: network not ready");
5430 break;
5431 case WSAVERNOTSUPPORTED:
5432 case WSAEINVAL:
5433 PyErr_SetString(
5434 PyExc_ImportError,
5435 "WSAStartup failed: requested version not supported");
5436 break;
5437 default:
5438 PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret);
5439 break;
5440 }
5441 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00005442}
5443
Guido van Rossum8d665e61996-06-26 18:22:49 +00005444#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00005445
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005446
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005447
5448#ifndef OS_INIT_DEFINED
5449static int
5450os_init(void)
5451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005452 return 1; /* Success */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005453}
5454#endif
5455
5456
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00005457/* C API table - always add new things to the end for binary
5458 compatibility. */
5459static
5460PySocketModule_APIObject PySocketModuleAPI =
5461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005462 &sock_type,
Antoine Pitrouc4df7842010-12-03 19:59:41 +00005463 NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005464 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00005465};
5466
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005467
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005468/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005469
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005470 This module is actually called "_socket", and there's a wrapper
Guido van Rossum7d0a8262007-05-21 23:13:11 +00005471 "socket.py" which implements some additional functionality.
5472 The import of "_socket" may fail with an ImportError exception if
5473 os-specific initialization fails. On Windows, this does WINSOCK
Ezio Melotti13925002011-03-16 11:05:33 +02005474 initialization. When WINSOCK is initialized successfully, a call to
Guido van Rossum7d0a8262007-05-21 23:13:11 +00005475 WSACleanup() is scheduled to be made at exit time.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005476*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005477
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005478PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00005479"Implementation module for socket operations.\n\
5480\n\
5481See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00005482
Martin v. Löwis1a214512008-06-11 05:26:20 +00005483static struct PyModuleDef socketmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005484 PyModuleDef_HEAD_INIT,
5485 PySocket_MODULE_NAME,
5486 socket_doc,
5487 -1,
5488 socket_methods,
5489 NULL,
5490 NULL,
5491 NULL,
5492 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00005493};
5494
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005495PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00005496PyInit__socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005498 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00005499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005500 if (!os_init())
5501 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005503 Py_TYPE(&sock_type) = &PyType_Type;
5504 m = PyModule_Create(&socketmodule);
5505 if (m == NULL)
5506 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005507
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005508 Py_INCREF(PyExc_OSError);
5509 PySocketModuleAPI.error = PyExc_OSError;
5510 Py_INCREF(PyExc_OSError);
5511 PyModule_AddObject(m, "error", PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005512 socket_herror = PyErr_NewException("socket.herror",
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005513 PyExc_OSError, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005514 if (socket_herror == NULL)
5515 return NULL;
5516 Py_INCREF(socket_herror);
5517 PyModule_AddObject(m, "herror", socket_herror);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005518 socket_gaierror = PyErr_NewException("socket.gaierror", PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005519 NULL);
5520 if (socket_gaierror == NULL)
5521 return NULL;
5522 Py_INCREF(socket_gaierror);
5523 PyModule_AddObject(m, "gaierror", socket_gaierror);
5524 socket_timeout = PyErr_NewException("socket.timeout",
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02005525 PyExc_OSError, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005526 if (socket_timeout == NULL)
5527 return NULL;
Antoine Pitrouc4df7842010-12-03 19:59:41 +00005528 PySocketModuleAPI.timeout_error = socket_timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005529 Py_INCREF(socket_timeout);
5530 PyModule_AddObject(m, "timeout", socket_timeout);
5531 Py_INCREF((PyObject *)&sock_type);
5532 if (PyModule_AddObject(m, "SocketType",
5533 (PyObject *)&sock_type) != 0)
5534 return NULL;
5535 Py_INCREF((PyObject *)&sock_type);
5536 if (PyModule_AddObject(m, "socket",
5537 (PyObject *)&sock_type) != 0)
5538 return NULL;
Guido van Rossum09be4091999-08-09 14:40:40 +00005539
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005540#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005541 has_ipv6 = Py_True;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005542#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005543 has_ipv6 = Py_False;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005544#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005545 Py_INCREF(has_ipv6);
5546 PyModule_AddObject(m, "has_ipv6", has_ipv6);
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005548 /* Export C API */
5549 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
5550 PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL)
5551 ) != 0)
5552 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00005553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005554 /* Address families (we only support AF_INET and AF_UNIX) */
Guido van Rossum09be4091999-08-09 14:40:40 +00005555#ifdef AF_UNSPEC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005556 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00005557#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005558 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005559#ifdef AF_INET6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005560 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005561#endif /* AF_INET6 */
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00005562#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005563 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005564#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00005565#ifdef AF_AX25
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005566 /* Amateur Radio AX.25 */
5567 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00005568#endif
5569#ifdef AF_IPX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005570 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00005571#endif
5572#ifdef AF_APPLETALK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005573 /* Appletalk DDP */
5574 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00005575#endif
5576#ifdef AF_NETROM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005577 /* Amateur radio NetROM */
5578 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00005579#endif
5580#ifdef AF_BRIDGE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005581 /* Multiprotocol bridge */
5582 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00005583#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005584#ifdef AF_ATMPVC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005585 /* ATM PVCs */
5586 PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005587#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00005588#ifdef AF_AAL5
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005589 /* Reserved for Werner's ATM */
5590 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00005591#endif
5592#ifdef AF_X25
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005593 /* Reserved for X.25 project */
5594 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00005595#endif
5596#ifdef AF_INET6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005597 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00005598#endif
5599#ifdef AF_ROSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005600 /* Amateur Radio X.25 PLP */
5601 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00005602#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005603#ifdef AF_DECnet
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005604 /* Reserved for DECnet project */
5605 PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005606#endif
5607#ifdef AF_NETBEUI
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005608 /* Reserved for 802.2LLC project */
5609 PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005610#endif
5611#ifdef AF_SECURITY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005612 /* Security callback pseudo AF */
5613 PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005614#endif
5615#ifdef AF_KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005616 /* PF_KEY key management API */
5617 PyModule_AddIntConstant(m, "AF_KEY", AF_KEY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005618#endif
5619#ifdef AF_NETLINK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005620 /* */
5621 PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);
5622 PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005623#ifdef NETLINK_SKIP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005624 PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005625#endif
5626#ifdef NETLINK_W1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005627 PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005628#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005629 PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK);
5630 PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL);
Guido van Rossum668a94a2006-02-21 01:07:27 +00005631#ifdef NETLINK_TCPDIAG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005632 PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00005633#endif
5634#ifdef NETLINK_NFLOG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005635 PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00005636#endif
Neal Norwitz65851662006-01-16 04:31:40 +00005637#ifdef NETLINK_XFRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005638 PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM);
Neal Norwitz65851662006-01-16 04:31:40 +00005639#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005640#ifdef NETLINK_ARPD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005641 PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005642#endif
5643#ifdef NETLINK_ROUTE6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005644 PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005645#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005646 PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW);
Thomas Wouterscf297e42007-02-23 15:07:44 +00005647#ifdef NETLINK_DNRTMSG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005648 PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG);
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00005649#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005650#ifdef NETLINK_TAPBASE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005651 PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005652#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005653#endif /* AF_NETLINK */
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005654#ifdef AF_ROUTE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005655 /* Alias to emulate 4.4BSD */
5656 PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005657#endif
5658#ifdef AF_ASH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005659 /* Ash */
5660 PyModule_AddIntConstant(m, "AF_ASH", AF_ASH);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005661#endif
5662#ifdef AF_ECONET
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005663 /* Acorn Econet */
5664 PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005665#endif
5666#ifdef AF_ATMSVC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005667 /* ATM SVCs */
5668 PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005669#endif
5670#ifdef AF_SNA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005671 /* Linux SNA Project (nutters!) */
5672 PyModule_AddIntConstant(m, "AF_SNA", AF_SNA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005673#endif
5674#ifdef AF_IRDA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005675 /* IRDA sockets */
5676 PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005677#endif
5678#ifdef AF_PPPOX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005679 /* PPPoX sockets */
5680 PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005681#endif
5682#ifdef AF_WANPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005683 /* Wanpipe API Sockets */
5684 PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005685#endif
5686#ifdef AF_LLC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005687 /* Linux LLC */
5688 PyModule_AddIntConstant(m, "AF_LLC", AF_LLC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00005689#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00005690
Hye-Shik Chang81268602004-02-02 06:05:24 +00005691#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005692 PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);
5693 PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);
5694 PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI);
5695 PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00005696#if !defined(__NetBSD__) && !defined(__DragonFly__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005697 PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00005698#endif
Hye-Shik Chang81268602004-02-02 06:05:24 +00005699#if !defined(__FreeBSD__)
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00005700#if !defined(__NetBSD__) && !defined(__DragonFly__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005701 PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP);
Gregory P. Smith3e85dfd2010-10-17 00:43:10 +00005702#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005703 PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR);
5704 PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00005705#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005706 PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);
5707 PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
5708 PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
Martin v. Löwis12af0482004-01-31 12:34:17 +00005709#endif
5710
Charles-François Natali47413c12011-10-06 19:47:44 +02005711#ifdef AF_CAN
5712 /* Controller Area Network */
5713 PyModule_AddIntConstant(m, "AF_CAN", AF_CAN);
5714#endif
5715#ifdef PF_CAN
5716 /* Controller Area Network */
5717 PyModule_AddIntConstant(m, "PF_CAN", PF_CAN);
5718#endif
5719
Charles-François Natali10b8cf42011-11-10 19:21:37 +01005720/* Reliable Datagram Sockets */
5721#ifdef AF_RDS
5722 PyModule_AddIntConstant(m, "AF_RDS", AF_RDS);
5723#endif
5724#ifdef PF_RDS
5725 PyModule_AddIntConstant(m, "PF_RDS", PF_RDS);
5726#endif
5727
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01005728/* Kernel event messages */
5729#ifdef PF_SYSTEM
5730 PyModule_AddIntConstant(m, "PF_SYSTEM", PF_SYSTEM);
5731#endif
5732#ifdef AF_SYSTEM
5733 PyModule_AddIntConstant(m, "AF_SYSTEM", AF_SYSTEM);
5734#endif
5735
Antoine Pitroub156a462010-10-27 20:13:57 +00005736#ifdef AF_PACKET
5737 PyModule_AddIntMacro(m, AF_PACKET);
5738#endif
5739#ifdef PF_PACKET
5740 PyModule_AddIntMacro(m, PF_PACKET);
5741#endif
5742#ifdef PACKET_HOST
5743 PyModule_AddIntMacro(m, PACKET_HOST);
5744#endif
5745#ifdef PACKET_BROADCAST
5746 PyModule_AddIntMacro(m, PACKET_BROADCAST);
5747#endif
5748#ifdef PACKET_MULTICAST
5749 PyModule_AddIntMacro(m, PACKET_MULTICAST);
5750#endif
5751#ifdef PACKET_OTHERHOST
5752 PyModule_AddIntMacro(m, PACKET_OTHERHOST);
5753#endif
5754#ifdef PACKET_OUTGOING
5755 PyModule_AddIntMacro(m, PACKET_OUTGOING);
5756#endif
5757#ifdef PACKET_LOOPBACK
5758 PyModule_AddIntMacro(m, PACKET_LOOPBACK);
5759#endif
5760#ifdef PACKET_FASTROUTE
5761 PyModule_AddIntMacro(m, PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00005762#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00005763
Christian Heimes043d6f62008-01-07 17:19:16 +00005764#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005765 PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC);
Christian Heimes043d6f62008-01-07 17:19:16 +00005766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005767 /* for addresses */
5768 PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ);
5769 PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME);
5770 PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID);
Christian Heimes043d6f62008-01-07 17:19:16 +00005771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005772 PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE);
5773 PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE);
5774 PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE);
Christian Heimes043d6f62008-01-07 17:19:16 +00005775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005776 /* for setsockopt() */
5777 PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC);
5778 PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE);
5779 PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE);
5780 PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE",
5781 TIPC_DEST_DROPPABLE);
5782 PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT);
Christian Heimes043d6f62008-01-07 17:19:16 +00005783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005784 PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE",
5785 TIPC_LOW_IMPORTANCE);
5786 PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE",
5787 TIPC_MEDIUM_IMPORTANCE);
5788 PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE",
5789 TIPC_HIGH_IMPORTANCE);
5790 PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE",
5791 TIPC_CRITICAL_IMPORTANCE);
Christian Heimes043d6f62008-01-07 17:19:16 +00005792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005793 /* for subscriptions */
5794 PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS);
5795 PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE);
Christian Heimes25bb7832008-01-11 16:17:00 +00005796#ifdef TIPC_SUB_CANCEL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005797 /* doesn't seem to be available everywhere */
5798 PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL);
Christian Heimes25bb7832008-01-11 16:17:00 +00005799#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005800 PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER);
5801 PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED);
5802 PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN);
5803 PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT);
5804 PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV);
5805 PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV);
Christian Heimes043d6f62008-01-07 17:19:16 +00005806#endif
5807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005808 /* Socket types */
5809 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
5810 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00005811/* We have incomplete socket support. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005812 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
5813 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00005814#if defined(SOCK_RDM)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005815 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00005816#endif
Antoine Pitroub1c54962010-10-14 15:05:38 +00005817#ifdef SOCK_CLOEXEC
5818 PyModule_AddIntConstant(m, "SOCK_CLOEXEC", SOCK_CLOEXEC);
5819#endif
5820#ifdef SOCK_NONBLOCK
5821 PyModule_AddIntConstant(m, "SOCK_NONBLOCK", SOCK_NONBLOCK);
5822#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005824#ifdef SO_DEBUG
5825 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005826#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005827#ifdef SO_ACCEPTCONN
5828 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005829#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005830#ifdef SO_REUSEADDR
5831 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005832#endif
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00005833#ifdef SO_EXCLUSIVEADDRUSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005834 PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE);
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00005835#endif
5836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005837#ifdef SO_KEEPALIVE
5838 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005839#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005840#ifdef SO_DONTROUTE
5841 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005842#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005843#ifdef SO_BROADCAST
5844 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005845#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005846#ifdef SO_USELOOPBACK
5847 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005848#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005849#ifdef SO_LINGER
5850 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005851#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005852#ifdef SO_OOBINLINE
5853 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005854#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005855#ifdef SO_REUSEPORT
5856 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005857#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005858#ifdef SO_SNDBUF
5859 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005860#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005861#ifdef SO_RCVBUF
5862 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005863#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005864#ifdef SO_SNDLOWAT
5865 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005866#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005867#ifdef SO_RCVLOWAT
5868 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005869#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005870#ifdef SO_SNDTIMEO
5871 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005872#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005873#ifdef SO_RCVTIMEO
5874 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005875#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005876#ifdef SO_ERROR
5877 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005878#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005879#ifdef SO_TYPE
5880 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005881#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005882#ifdef SO_SETFIB
5883 PyModule_AddIntConstant(m, "SO_SETFIB", SO_SETFIB);
Larry Hastingsf0f37952010-04-02 11:47:10 +00005884#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005885#ifdef SO_PASSCRED
5886 PyModule_AddIntConstant(m, "SO_PASSCRED", SO_PASSCRED);
5887#endif
5888#ifdef SO_PEERCRED
5889 PyModule_AddIntConstant(m, "SO_PEERCRED", SO_PEERCRED);
5890#endif
5891#ifdef LOCAL_PEERCRED
5892 PyModule_AddIntConstant(m, "LOCAL_PEERCRED", LOCAL_PEERCRED);
5893#endif
Victor Stinnercb98bed2011-10-25 13:45:48 +02005894#ifdef SO_BINDTODEVICE
5895 PyModule_AddIntMacro(m, SO_BINDTODEVICE);
5896#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005898 /* Maximum number of connections for "listen" */
5899#ifdef SOMAXCONN
5900 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005901#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005902 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005903#endif
5904
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005905 /* Ancilliary message types */
5906#ifdef SCM_RIGHTS
5907 PyModule_AddIntConstant(m, "SCM_RIGHTS", SCM_RIGHTS);
5908#endif
5909#ifdef SCM_CREDENTIALS
5910 PyModule_AddIntConstant(m, "SCM_CREDENTIALS", SCM_CREDENTIALS);
5911#endif
5912#ifdef SCM_CREDS
5913 PyModule_AddIntConstant(m, "SCM_CREDS", SCM_CREDS);
5914#endif
5915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005916 /* Flags for send, recv */
5917#ifdef MSG_OOB
5918 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005919#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005920#ifdef MSG_PEEK
5921 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005922#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005923#ifdef MSG_DONTROUTE
5924 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005925#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005926#ifdef MSG_DONTWAIT
5927 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00005928#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005929#ifdef MSG_EOR
5930 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005931#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005932#ifdef MSG_TRUNC
5933 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005934#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005935#ifdef MSG_CTRUNC
5936 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005937#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005938#ifdef MSG_WAITALL
5939 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005940#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005941#ifdef MSG_BTAG
5942 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005943#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005944#ifdef MSG_ETAG
5945 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005946#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10005947#ifdef MSG_NOSIGNAL
5948 PyModule_AddIntConstant(m, "MSG_NOSIGNAL", MSG_NOSIGNAL);
5949#endif
5950#ifdef MSG_NOTIFICATION
5951 PyModule_AddIntConstant(m, "MSG_NOTIFICATION", MSG_NOTIFICATION);
5952#endif
5953#ifdef MSG_CMSG_CLOEXEC
5954 PyModule_AddIntConstant(m, "MSG_CMSG_CLOEXEC", MSG_CMSG_CLOEXEC);
5955#endif
5956#ifdef MSG_ERRQUEUE
5957 PyModule_AddIntConstant(m, "MSG_ERRQUEUE", MSG_ERRQUEUE);
5958#endif
5959#ifdef MSG_CONFIRM
5960 PyModule_AddIntConstant(m, "MSG_CONFIRM", MSG_CONFIRM);
5961#endif
5962#ifdef MSG_MORE
5963 PyModule_AddIntConstant(m, "MSG_MORE", MSG_MORE);
5964#endif
5965#ifdef MSG_EOF
5966 PyModule_AddIntConstant(m, "MSG_EOF", MSG_EOF);
5967#endif
5968#ifdef MSG_BCAST
5969 PyModule_AddIntConstant(m, "MSG_BCAST", MSG_BCAST);
5970#endif
5971#ifdef MSG_MCAST
5972 PyModule_AddIntConstant(m, "MSG_MCAST", MSG_MCAST);
5973#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005975 /* Protocol level and numbers, usable for [gs]etsockopt */
5976#ifdef SOL_SOCKET
5977 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00005978#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005979#ifdef SOL_IP
5980 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00005981#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005982 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00005983#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005984#ifdef SOL_IPX
5985 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00005986#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005987#ifdef SOL_AX25
5988 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00005989#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005990#ifdef SOL_ATALK
5991 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00005992#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005993#ifdef SOL_NETROM
5994 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00005995#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005996#ifdef SOL_ROSE
5997 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00005998#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005999#ifdef SOL_TCP
6000 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006001#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006002 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00006003#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006004#ifdef SOL_UDP
6005 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006006#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006007 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00006008#endif
Charles-François Natali47413c12011-10-06 19:47:44 +02006009#ifdef SOL_CAN_BASE
6010 PyModule_AddIntConstant(m, "SOL_CAN_BASE", SOL_CAN_BASE);
6011#endif
6012#ifdef SOL_CAN_RAW
6013 PyModule_AddIntConstant(m, "SOL_CAN_RAW", SOL_CAN_RAW);
6014 PyModule_AddIntConstant(m, "CAN_RAW", CAN_RAW);
6015#endif
6016#ifdef HAVE_LINUX_CAN_H
6017 PyModule_AddIntConstant(m, "CAN_EFF_FLAG", CAN_EFF_FLAG);
6018 PyModule_AddIntConstant(m, "CAN_RTR_FLAG", CAN_RTR_FLAG);
6019 PyModule_AddIntConstant(m, "CAN_ERR_FLAG", CAN_ERR_FLAG);
6020
6021 PyModule_AddIntConstant(m, "CAN_SFF_MASK", CAN_SFF_MASK);
6022 PyModule_AddIntConstant(m, "CAN_EFF_MASK", CAN_EFF_MASK);
6023 PyModule_AddIntConstant(m, "CAN_ERR_MASK", CAN_ERR_MASK);
6024#endif
6025#ifdef HAVE_LINUX_CAN_RAW_H
6026 PyModule_AddIntConstant(m, "CAN_RAW_FILTER", CAN_RAW_FILTER);
6027 PyModule_AddIntConstant(m, "CAN_RAW_ERR_FILTER", CAN_RAW_ERR_FILTER);
6028 PyModule_AddIntConstant(m, "CAN_RAW_LOOPBACK", CAN_RAW_LOOPBACK);
6029 PyModule_AddIntConstant(m, "CAN_RAW_RECV_OWN_MSGS", CAN_RAW_RECV_OWN_MSGS);
6030#endif
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006031#ifdef SOL_RDS
6032 PyModule_AddIntConstant(m, "SOL_RDS", SOL_RDS);
6033#endif
6034#ifdef RDS_CANCEL_SENT_TO
6035 PyModule_AddIntConstant(m, "RDS_CANCEL_SENT_TO", RDS_CANCEL_SENT_TO);
6036#endif
6037#ifdef RDS_GET_MR
6038 PyModule_AddIntConstant(m, "RDS_GET_MR", RDS_GET_MR);
6039#endif
6040#ifdef RDS_FREE_MR
6041 PyModule_AddIntConstant(m, "RDS_FREE_MR", RDS_FREE_MR);
6042#endif
6043#ifdef RDS_RECVERR
6044 PyModule_AddIntConstant(m, "RDS_RECVERR", RDS_RECVERR);
6045#endif
6046#ifdef RDS_CONG_MONITOR
6047 PyModule_AddIntConstant(m, "RDS_CONG_MONITOR", RDS_CONG_MONITOR);
6048#endif
6049#ifdef RDS_GET_MR_FOR_DEST
6050 PyModule_AddIntConstant(m, "RDS_GET_MR_FOR_DEST", RDS_GET_MR_FOR_DEST);
6051#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006052#ifdef IPPROTO_IP
6053 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00006054#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006055 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006056#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006057#ifdef IPPROTO_HOPOPTS
6058 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006059#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006060#ifdef IPPROTO_ICMP
6061 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00006062#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006063 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006064#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006065#ifdef IPPROTO_IGMP
6066 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006067#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006068#ifdef IPPROTO_GGP
6069 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006070#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006071#ifdef IPPROTO_IPV4
6072 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006073#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006074#ifdef IPPROTO_IPV6
6075 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisa0f17342003-10-03 13:56:20 +00006076#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006077#ifdef IPPROTO_IPIP
6078 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006079#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006080#ifdef IPPROTO_TCP
6081 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00006082#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006083 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006084#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006085#ifdef IPPROTO_EGP
6086 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006087#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006088#ifdef IPPROTO_PUP
6089 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006090#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006091#ifdef IPPROTO_UDP
6092 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00006093#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006094 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006095#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006096#ifdef IPPROTO_IDP
6097 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006098#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006099#ifdef IPPROTO_HELLO
6100 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006101#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006102#ifdef IPPROTO_ND
6103 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006104#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006105#ifdef IPPROTO_TP
6106 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006107#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006108#ifdef IPPROTO_IPV6
6109 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006110#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006111#ifdef IPPROTO_ROUTING
6112 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006113#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006114#ifdef IPPROTO_FRAGMENT
6115 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006116#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006117#ifdef IPPROTO_RSVP
6118 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006119#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006120#ifdef IPPROTO_GRE
6121 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006122#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006123#ifdef IPPROTO_ESP
6124 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006125#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006126#ifdef IPPROTO_AH
6127 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006128#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006129#ifdef IPPROTO_MOBILE
6130 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006131#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006132#ifdef IPPROTO_ICMPV6
6133 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006134#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006135#ifdef IPPROTO_NONE
6136 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006137#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006138#ifdef IPPROTO_DSTOPTS
6139 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006140#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006141#ifdef IPPROTO_XTP
6142 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006143#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006144#ifdef IPPROTO_EON
6145 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006146#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006147#ifdef IPPROTO_PIM
6148 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006149#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006150#ifdef IPPROTO_IPCOMP
6151 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006152#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006153#ifdef IPPROTO_VRRP
6154 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006155#endif
Nick Coghlan96fe56a2011-08-22 11:55:57 +10006156#ifdef IPPROTO_SCTP
6157 PyModule_AddIntConstant(m, "IPPROTO_SCTP", IPPROTO_SCTP);
6158#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006159#ifdef IPPROTO_BIP
6160 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006161#endif
6162/**/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006163#ifdef IPPROTO_RAW
6164 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00006165#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006166 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006167#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006168#ifdef IPPROTO_MAX
6169 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006170#endif
6171
Martin v. Löwis9d6c6692012-02-03 17:44:58 +01006172#ifdef SYSPROTO_CONTROL
6173 PyModule_AddIntConstant(m, "SYSPROTO_CONTROL", SYSPROTO_CONTROL);
6174#endif
6175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006176 /* Some port configuration */
6177#ifdef IPPORT_RESERVED
6178 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006179#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006180 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006181#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006182#ifdef IPPORT_USERRESERVED
6183 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006184#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006185 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006186#endif
6187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006188 /* Some reserved IP v.4 addresses */
6189#ifdef INADDR_ANY
6190 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006191#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006192 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006193#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006194#ifdef INADDR_BROADCAST
6195 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006196#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006197 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006198#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006199#ifdef INADDR_LOOPBACK
6200 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006201#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006202 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006203#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006204#ifdef INADDR_UNSPEC_GROUP
6205 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006206#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006207 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006208#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006209#ifdef INADDR_ALLHOSTS_GROUP
6210 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
6211 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006212#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006213 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006214#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006215#ifdef INADDR_MAX_LOCAL_GROUP
6216 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
6217 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006218#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006219 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006220#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006221#ifdef INADDR_NONE
6222 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006223#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006224 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00006225#endif
6226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006227 /* IPv4 [gs]etsockopt options */
6228#ifdef IP_OPTIONS
6229 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006230#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006231#ifdef IP_HDRINCL
6232 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006233#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006234#ifdef IP_TOS
6235 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006236#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006237#ifdef IP_TTL
6238 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006239#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006240#ifdef IP_RECVOPTS
6241 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006242#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006243#ifdef IP_RECVRETOPTS
6244 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006245#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006246#ifdef IP_RECVDSTADDR
6247 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006248#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006249#ifdef IP_RETOPTS
6250 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006251#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006252#ifdef IP_MULTICAST_IF
6253 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006254#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006255#ifdef IP_MULTICAST_TTL
6256 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006257#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006258#ifdef IP_MULTICAST_LOOP
6259 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006260#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006261#ifdef IP_ADD_MEMBERSHIP
6262 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006263#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006264#ifdef IP_DROP_MEMBERSHIP
6265 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00006266#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006267#ifdef IP_DEFAULT_MULTICAST_TTL
6268 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
6269 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00006270#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006271#ifdef IP_DEFAULT_MULTICAST_LOOP
6272 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
6273 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00006274#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006275#ifdef IP_MAX_MEMBERSHIPS
6276 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00006277#endif
Charles-François Natali564a42c2011-12-17 14:59:56 +01006278#ifdef IP_TRANSPARENT
6279 PyModule_AddIntConstant(m, "IP_TRANSPARENT", IP_TRANSPARENT);
6280#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00006281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006282 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
6283#ifdef IPV6_JOIN_GROUP
6284 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006285#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006286#ifdef IPV6_LEAVE_GROUP
6287 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006288#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006289#ifdef IPV6_MULTICAST_HOPS
6290 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006291#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006292#ifdef IPV6_MULTICAST_IF
6293 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006294#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006295#ifdef IPV6_MULTICAST_LOOP
6296 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006297#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006298#ifdef IPV6_UNICAST_HOPS
6299 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006300#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006301 /* Additional IPV6 socket options, defined in RFC 3493 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00006302#ifdef IPV6_V6ONLY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006303 PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006304#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006305 /* Advanced IPV6 socket options, from RFC 3542 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00006306#ifdef IPV6_CHECKSUM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006307 PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006308#endif
6309#ifdef IPV6_DONTFRAG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006310 PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006311#endif
6312#ifdef IPV6_DSTOPTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006313 PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006314#endif
6315#ifdef IPV6_HOPLIMIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006316 PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006317#endif
6318#ifdef IPV6_HOPOPTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006319 PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006320#endif
6321#ifdef IPV6_NEXTHOP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006322 PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006323#endif
6324#ifdef IPV6_PATHMTU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006325 PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006326#endif
6327#ifdef IPV6_PKTINFO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006328 PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006329#endif
6330#ifdef IPV6_RECVDSTOPTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006331 PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006332#endif
6333#ifdef IPV6_RECVHOPLIMIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006334 PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006335#endif
6336#ifdef IPV6_RECVHOPOPTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006337 PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006338#endif
6339#ifdef IPV6_RECVPKTINFO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006340 PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006341#endif
6342#ifdef IPV6_RECVRTHDR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006343 PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006344#endif
6345#ifdef IPV6_RECVTCLASS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006346 PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006347#endif
6348#ifdef IPV6_RTHDR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006349 PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006350#endif
6351#ifdef IPV6_RTHDRDSTOPTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006352 PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006353#endif
6354#ifdef IPV6_RTHDR_TYPE_0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006355 PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006356#endif
6357#ifdef IPV6_RECVPATHMTU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006358 PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006359#endif
6360#ifdef IPV6_TCLASS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006361 PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006362#endif
6363#ifdef IPV6_USE_MIN_MTU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006364 PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006365#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00006366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006367 /* TCP options */
6368#ifdef TCP_NODELAY
6369 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00006370#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006371#ifdef TCP_MAXSEG
6372 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00006373#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006374#ifdef TCP_CORK
6375 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006376#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006377#ifdef TCP_KEEPIDLE
6378 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006379#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006380#ifdef TCP_KEEPINTVL
6381 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006382#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006383#ifdef TCP_KEEPCNT
6384 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006385#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006386#ifdef TCP_SYNCNT
6387 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006388#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006389#ifdef TCP_LINGER2
6390 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006391#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006392#ifdef TCP_DEFER_ACCEPT
6393 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006394#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006395#ifdef TCP_WINDOW_CLAMP
6396 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006397#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006398#ifdef TCP_INFO
6399 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006400#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006401#ifdef TCP_QUICKACK
6402 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00006403#endif
6404
Guido van Rossum09be4091999-08-09 14:40:40 +00006405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006406 /* IPX options */
6407#ifdef IPX_TYPE
6408 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00006409#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00006410
Charles-François Natali10b8cf42011-11-10 19:21:37 +01006411/* Reliable Datagram Sockets */
6412#ifdef RDS_CMSG_RDMA_ARGS
6413 PyModule_AddIntConstant(m, "RDS_CMSG_RDMA_ARGS", RDS_CMSG_RDMA_ARGS);
6414#endif
6415#ifdef RDS_CMSG_RDMA_DEST
6416 PyModule_AddIntConstant(m, "RDS_CMSG_RDMA_DEST", RDS_CMSG_RDMA_DEST);
6417#endif
6418#ifdef RDS_CMSG_RDMA_MAP
6419 PyModule_AddIntConstant(m, "RDS_CMSG_RDMA_MAP", RDS_CMSG_RDMA_MAP);
6420#endif
6421#ifdef RDS_CMSG_RDMA_STATUS
6422 PyModule_AddIntConstant(m, "RDS_CMSG_RDMA_STATUS", RDS_CMSG_RDMA_STATUS);
6423#endif
6424#ifdef RDS_CMSG_RDMA_UPDATE
6425 PyModule_AddIntConstant(m, "RDS_CMSG_RDMA_UPDATE", RDS_CMSG_RDMA_UPDATE);
6426#endif
6427#ifdef RDS_RDMA_READWRITE
6428 PyModule_AddIntConstant(m, "RDS_RDMA_READWRITE", RDS_RDMA_READWRITE);
6429#endif
6430#ifdef RDS_RDMA_FENCE
6431 PyModule_AddIntConstant(m, "RDS_RDMA_FENCE", RDS_RDMA_FENCE);
6432#endif
6433#ifdef RDS_RDMA_INVALIDATE
6434 PyModule_AddIntConstant(m, "RDS_RDMA_INVALIDATE", RDS_RDMA_INVALIDATE);
6435#endif
6436#ifdef RDS_RDMA_USE_ONCE
6437 PyModule_AddIntConstant(m, "RDS_RDMA_USE_ONCE", RDS_RDMA_USE_ONCE);
6438#endif
6439#ifdef RDS_RDMA_DONTWAIT
6440 PyModule_AddIntConstant(m, "RDS_RDMA_DONTWAIT", RDS_RDMA_DONTWAIT);
6441#endif
6442#ifdef RDS_RDMA_NOTIFY_ME
6443 PyModule_AddIntConstant(m, "RDS_RDMA_NOTIFY_ME", RDS_RDMA_NOTIFY_ME);
6444#endif
6445#ifdef RDS_RDMA_SILENT
6446 PyModule_AddIntConstant(m, "RDS_RDMA_SILENT", RDS_RDMA_SILENT);
6447#endif
6448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006449 /* get{addr,name}info parameters */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006450#ifdef EAI_ADDRFAMILY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006451 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006452#endif
6453#ifdef EAI_AGAIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006454 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006455#endif
6456#ifdef EAI_BADFLAGS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006457 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006458#endif
6459#ifdef EAI_FAIL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006460 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006461#endif
6462#ifdef EAI_FAMILY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006463 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006464#endif
6465#ifdef EAI_MEMORY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006466 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006467#endif
6468#ifdef EAI_NODATA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006469 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006470#endif
6471#ifdef EAI_NONAME
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006472 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006473#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00006474#ifdef EAI_OVERFLOW
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006475 PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006476#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006477#ifdef EAI_SERVICE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006478 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006479#endif
6480#ifdef EAI_SOCKTYPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006481 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006482#endif
6483#ifdef EAI_SYSTEM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006484 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006485#endif
6486#ifdef EAI_BADHINTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006487 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006488#endif
6489#ifdef EAI_PROTOCOL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006490 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006491#endif
6492#ifdef EAI_MAX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006493 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006494#endif
6495#ifdef AI_PASSIVE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006496 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006497#endif
6498#ifdef AI_CANONNAME
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006499 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006500#endif
6501#ifdef AI_NUMERICHOST
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006502 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006503#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00006504#ifdef AI_NUMERICSERV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006505 PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);
Martin v. Löwisda91d022003-12-30 11:14:01 +00006506#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006507#ifdef AI_MASK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006508 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006509#endif
6510#ifdef AI_ALL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006511 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006512#endif
6513#ifdef AI_V4MAPPED_CFG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006514 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006515#endif
6516#ifdef AI_ADDRCONFIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006517 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006518#endif
6519#ifdef AI_V4MAPPED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006520 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006521#endif
6522#ifdef AI_DEFAULT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006523 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006524#endif
6525#ifdef NI_MAXHOST
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006526 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006527#endif
6528#ifdef NI_MAXSERV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006529 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006530#endif
6531#ifdef NI_NOFQDN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006532 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006533#endif
6534#ifdef NI_NUMERICHOST
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006535 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006536#endif
6537#ifdef NI_NAMEREQD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006538 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006539#endif
6540#ifdef NI_NUMERICSERV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006541 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006542#endif
6543#ifdef NI_DGRAM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006544 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00006545#endif
6546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006547 /* shutdown() parameters */
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006548#ifdef SHUT_RD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006549 PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006550#elif defined(SD_RECEIVE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006551 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006552#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006553 PyModule_AddIntConstant(m, "SHUT_RD", 0);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006554#endif
6555#ifdef SHUT_WR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006556 PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006557#elif defined(SD_SEND)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006558 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006559#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006560 PyModule_AddIntConstant(m, "SHUT_WR", 1);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006561#endif
6562#ifdef SHUT_RDWR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006563 PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006564#elif defined(SD_BOTH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006565 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006566#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006567 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00006568#endif
6569
Christian Heimesfaf2f632008-01-06 16:59:19 +00006570#ifdef SIO_RCVALL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006571 {
6572 DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS};
6573 const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"};
6574 int i;
Victor Stinner63941882011-09-29 00:42:28 +02006575 for(i = 0; i<Py_ARRAY_LENGTH(codes); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006576 PyObject *tmp;
6577 tmp = PyLong_FromUnsignedLong(codes[i]);
6578 if (tmp == NULL)
6579 return NULL;
6580 PyModule_AddObject(m, names[i], tmp);
6581 }
6582 }
6583 PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF);
6584 PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON);
6585 PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00006586#ifdef RCVALL_IPLEVEL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006587 PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00006588#endif
6589#ifdef RCVALL_MAX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006590 PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00006591#endif
Christian Heimesfaf2f632008-01-06 16:59:19 +00006592#endif /* _MSTCPIP_ */
6593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006594 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00006595#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006596 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00006597#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006598 return m;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00006599}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006600
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006601
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006602#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +00006603#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006604
6605/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00006606/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006607
Guido van Rossum3eede5a2002-06-07 02:08:35 +00006608int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006609inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006611 if (af == AF_INET) {
Benjamin Petersonf91df042009-02-13 02:50:59 +00006612#if (SIZEOF_INT != 4)
6613#error "Not sure if in_addr_t exists and int is not 32-bits."
6614#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006615 unsigned int packed_addr;
6616 packed_addr = inet_addr(src);
6617 if (packed_addr == INADDR_NONE)
6618 return 0;
6619 memcpy(dst, &packed_addr, 4);
6620 return 1;
6621 }
6622 /* Should set errno to EAFNOSUPPORT */
6623 return -1;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006624}
6625
Martin v. Löwisc925b1532001-07-21 09:42:15 +00006626const char *
6627inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006629 if (af == AF_INET) {
6630 struct in_addr packed_addr;
6631 if (size < 16)
6632 /* Should set errno to ENOSPC. */
6633 return NULL;
6634 memcpy(&packed_addr, src, sizeof(packed_addr));
6635 return strncpy(dst, inet_ntoa(packed_addr), size);
6636 }
6637 /* Should set errno to EAFNOSUPPORT */
6638 return NULL;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006639}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00006640
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00006641#endif
Christian Heimesb6150692007-11-15 23:37:07 +00006642#endif