blob: baa5b9d954d7dcaed36662061557abc537108eaf [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 Pitrou7f14f0d2010-05-09 16:14:21 +000020 a subclass of socket.error
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000021- socket.herror: exception raised for gethostby* errors,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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)
Guido van Rossum6574b3e1991-06-25 21:36:08 +000045- an Internet socket address is a pair (hostname, port)
46 where hostname can be anything recognized by gethostbyname()
47 (including the dd.dd.dd.dd notation) and port is in host byte order
48- where a hostname is returned, the dd.dd.dd.dd notation is used
Guido van Rossum27e177d1995-03-16 15:43:47 +000049- a UNIX domain socket address is a string specifying the pathname
Jeremy Hylton22308652001-02-02 03:23:09 +000050- an AF_PACKET socket address is a tuple containing a string
51 specifying the ethernet interface and an integer specifying
52 the Ethernet protocol number to be received. For example:
Jeremy Hyltondbfb6622001-02-02 19:55:17 +000053 ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +000054 specify packet-type and ha-type/addr.
Christian Heimes043d6f62008-01-07 17:19:16 +000055- an AF_TIPC socket address is expressed as
56 (addr_type, v1, v2, v3 [, scope]); where addr_type can be one of:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000057 TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID;
Christian Heimes043d6f62008-01-07 17:19:16 +000058 and scope can be one of:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000059 TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE.
Christian Heimes043d6f62008-01-07 17:19:16 +000060 The meaning of v1, v2 and v3 depends on the value of addr_type:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000061 if addr_type is TIPC_ADDR_NAME:
62 v1 is the server type
63 v2 is the port identifier
64 v3 is ignored
65 if addr_type is TIPC_ADDR_NAMESEQ:
66 v1 is the server type
67 v2 is the lower port number
68 v3 is the upper port number
69 if addr_type is TIPC_ADDR_ID:
70 v1 is the node
71 v2 is the ref
72 v3 is ignored
Christian Heimes043d6f62008-01-07 17:19:16 +000073
Guido van Rossum6574b3e1991-06-25 21:36:08 +000074
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000075Local naming conventions:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000076
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000077- names starting with sock_ are socket object methods
78- names starting with socket_ are module-level functions
79- names starting with PySocket are exported through socketmodule.h
Guido van Rossum30a685f1991-06-27 15:51:29 +000080
Guido van Rossum6574b3e1991-06-25 21:36:08 +000081*/
82
Thomas Wouters477c8d52006-05-27 19:21:47 +000083#ifdef __APPLE__
84 /*
85 * inet_aton is not available on OSX 10.3, yet we want to use a binary
86 * that was build on 10.4 or later to work on that release, weak linking
87 * comes to the rescue.
88 */
89# pragma weak inet_aton
90#endif
91
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000092#include "Python.h"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000093#include "structmember.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000094
Guido van Rossum47dfa4a2003-04-25 05:48:32 +000095#undef MAX
96#define MAX(x, y) ((x) < (y) ? (y) : (x))
97
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000098/* Socket object documentation */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000099PyDoc_STRVAR(sock_doc,
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000100"socket([family[, type[, proto]]]) -> socket object\n\
101\n\
102Open a socket of the given type. The family argument specifies the\n\
103address family; it defaults to AF_INET. The type argument specifies\n\
104whether this is a stream (SOCK_STREAM, this is the default)\n\
105or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
106specifying the default protocol. Keyword arguments are accepted.\n\
107\n\
108A socket object represents one endpoint of a network connection.\n\
109\n\
110Methods of socket objects (keyword arguments not allowed):\n\
111\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000112_accept() -- accept connection, returning new socket fd and client address\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000113bind(addr) -- bind the socket to a local address\n\
114close() -- close the socket\n\
115connect(addr) -- connect the socket to a remote address\n\
116connect_ex(addr) -- connect, return an error code instead of an exception\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000117_dup() -- return a new socket fd duplicated from fileno()\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000118fileno() -- return underlying file descriptor\n\
119getpeername() -- return remote address [*]\n\
120getsockname() -- return local address\n\
121getsockopt(level, optname[, buflen]) -- get socket options\n\
122gettimeout() -- return timeout or None\n\
123listen(n) -- start listening for incoming connections\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000124recv(buflen[, flags]) -- receive data\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000125recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000126recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000127recvfrom_into(buffer[, nbytes, [, flags])\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000128 -- receive data and sender\'s address (into a buffer)\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000129sendall(data[, flags]) -- send all data\n\
130send(data[, flags]) -- send data, may not send all of it\n\
131sendto(data[, flags], addr) -- send data to a given address\n\
132setblocking(0 | 1) -- set or clear the blocking I/O flag\n\
133setsockopt(level, optname, value) -- set socket options\n\
134settimeout(None | float) -- set or clear the timeout\n\
135shutdown(how) -- shut down traffic in one or both directions\n\
136\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000137 [*] not available on all platforms!");
Guido van Rossum3baaa131999-03-22 21:44:51 +0000138
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000139/* XXX This is a terrible mess of platform-dependent preprocessor hacks.
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000140 I hope some day someone can clean this up please... */
141
Guido van Rossum9376b741999-09-15 22:01:40 +0000142/* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
143 script doesn't get this right, so we hardcode some platform checks below.
144 On the other hand, not all Linux versions agree, so there the settings
145 computed by the configure script are needed! */
146
147#ifndef linux
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000148# undef HAVE_GETHOSTBYNAME_R_3_ARG
149# undef HAVE_GETHOSTBYNAME_R_5_ARG
150# undef HAVE_GETHOSTBYNAME_R_6_ARG
Guido van Rossum9376b741999-09-15 22:01:40 +0000151#endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000152
Guido van Rossum7a122991999-04-13 04:07:32 +0000153#ifndef WITH_THREAD
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000154# undef HAVE_GETHOSTBYNAME_R
Guido van Rossum7a122991999-04-13 04:07:32 +0000155#endif
156
Guido van Rossume7de2061999-03-24 17:24:33 +0000157#ifdef HAVE_GETHOSTBYNAME_R
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000158# if defined(_AIX) || defined(__osf__)
159# define HAVE_GETHOSTBYNAME_R_3_ARG
160# elif defined(__sun) || defined(__sgi)
161# define HAVE_GETHOSTBYNAME_R_5_ARG
162# elif defined(linux)
Guido van Rossum9376b741999-09-15 22:01:40 +0000163/* Rely on the configure script */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000164# else
165# undef HAVE_GETHOSTBYNAME_R
166# endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000167#endif
168
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000169#if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \
170 !defined(MS_WINDOWS)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000171# define USE_GETHOSTBYNAME_LOCK
Guido van Rossum3baaa131999-03-22 21:44:51 +0000172#endif
173
Hye-Shik Chang9ceebd52005-09-24 14:58:47 +0000174/* To use __FreeBSD_version */
175#ifdef HAVE_SYS_PARAM_H
176#include <sys/param.h>
177#endif
Just van Rossum1040d2c2003-05-09 07:53:18 +0000178/* On systems on which getaddrinfo() is believed to not be thread-safe,
Just van Rossum09aecd72003-05-09 08:03:44 +0000179 (this includes the getaddrinfo emulation) protect access with a lock. */
Hye-Shik Chang9ceebd52005-09-24 14:58:47 +0000180#if defined(WITH_THREAD) && (defined(__APPLE__) || \
181 (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000182 defined(__OpenBSD__) || defined(__NetBSD__) || \
183 defined(__VMS) || !defined(HAVE_GETADDRINFO))
Just van Rossum1040d2c2003-05-09 07:53:18 +0000184#define USE_GETADDRINFO_LOCK
185#endif
186
187#ifdef USE_GETADDRINFO_LOCK
188#define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1);
189#define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock);
190#else
191#define ACQUIRE_GETADDRINFO_LOCK
192#define RELEASE_GETADDRINFO_LOCK
193#endif
194
195#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000196# include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000197#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000198
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000199#if defined(PYCC_VACPP)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000200# include <types.h>
201# include <io.h>
202# include <sys/ioctl.h>
203# include <utils.h>
204# include <ctype.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000205#endif
206
Martin v. Löwis9e437302002-12-06 12:57:26 +0000207#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000208# include <ioctl.h>
209#endif
210
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000211#if defined(PYOS_OS2)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000212# define INCL_DOS
213# define INCL_DOSERRORS
214# define INCL_NOPMAPI
215# include <os2.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000216#endif
217
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000218#if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000219/* make sure that the reentrant (gethostbyaddr_r etc)
220 functions are declared correctly if compiling with
221 MIPSPro 7.x in ANSI C mode (default) */
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000222
Thomas Wouters477c8d52006-05-27 19:21:47 +0000223/* XXX Using _SGIAPI is the wrong thing,
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000224 but I don't know what the right thing is. */
Trent Mick8ea5bdf2004-09-13 17:48:41 +0000225#undef _SGIAPI /* to avoid warning */
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000226#define _SGIAPI 1
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000227
Trent Mick8ea5bdf2004-09-13 17:48:41 +0000228#undef _XOPEN_SOURCE
229#include <sys/socket.h>
230#include <sys/types.h>
231#include <netinet/in.h>
232#ifdef _SS_ALIGNSIZE
233#define HAVE_GETADDRINFO 1
234#define HAVE_GETNAMEINFO 1
235#endif
236
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000237#define HAVE_INET_PTON
238#include <netdb.h>
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000239#endif
240
Thomas Wouters477c8d52006-05-27 19:21:47 +0000241/* Irix 6.5 fails to define this variable at all. This is needed
242 for both GCC and SGI's compiler. I'd say that the SGI headers
Georg Brandldbd83392006-02-20 09:42:33 +0000243 are just busted. Same thing for Solaris. */
244#if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN)
Anthony Baxterbab23cf2003-10-04 08:00:49 +0000245#define INET_ADDRSTRLEN 16
246#endif
247
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000248/* Generic includes */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000249#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000250#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000251#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000252
Marc-André Lemburg976ade62002-02-16 18:47:07 +0000253/* Generic socket object definitions and includes */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000254#define PySocket_BUILDING_SOCKET
Marc-André Lemburgbb8b78b2002-02-16 18:44:52 +0000255#include "socketmodule.h"
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000256
257/* Addressing includes */
258
Guido van Rossum6f489d91996-06-28 20:15:15 +0000259#ifndef MS_WINDOWS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000260
261/* Non-MS WINDOWS includes */
262# include <netdb.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000263
Guido van Rossum9376b741999-09-15 22:01:40 +0000264/* Headers needed for inet_ntoa() and inet_addr() */
Skip Montanaroeb33e5a2007-08-17 12:57:41 +0000265# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000266# include <netdb.h>
Tim Peters603c6832001-11-05 02:45:59 +0000267typedef size_t socklen_t;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000268# else
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000269# include <arpa/inet.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000270# endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000271
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000272# include <fcntl.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000273
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000274#else
Guido van Rossum48a680c2001-03-02 06:34:14 +0000275
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000276/* MS_WINDOWS includes */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277# ifdef HAVE_FCNTL_H
278# include <fcntl.h>
279# endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000280
Jeremy Hylton22308652001-02-02 03:23:09 +0000281#endif
282
Skip Montanaro7befb992004-02-10 16:50:21 +0000283#include <stddef.h>
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000284
285#ifndef offsetof
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000286# define offsetof(type, member) ((size_t)(&((type *)0)->member))
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000287#endif
288
Neal Norwitz39d22e52002-11-02 19:55:21 +0000289#ifndef O_NONBLOCK
290# define O_NONBLOCK O_NDELAY
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000291#endif
292
Trent Micka708d6e2004-09-07 17:48:26 +0000293/* include Python's addrinfo.h unless it causes trouble */
294#if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE)
295 /* Do not include addinfo.h on some newer IRIX versions.
296 * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21,
297 * for example, but not by 6.5.10.
298 */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000299#elif defined(_MSC_VER) && _MSC_VER>1201
Trent Micka708d6e2004-09-07 17:48:26 +0000300 /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and
301 * EAI_* constants are defined in (the already included) ws2tcpip.h.
302 */
303#else
304# include "addrinfo.h"
305#endif
Jason Tishlerc246cb72004-08-09 13:25:59 +0000306
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000307#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +0000308#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000309int inet_pton(int af, const char *src, void *dst);
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000310const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000311#endif
Christian Heimesb6150692007-11-15 23:37:07 +0000312#endif
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000313
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000314#ifdef __APPLE__
315/* On OS X, getaddrinfo returns no error indication of lookup
316 failure, so we must use the emulation instead of the libinfo
317 implementation. Unfortunately, performing an autoconf test
318 for this bug would require DNS access for the machine performing
319 the configuration, which is not acceptable. Therefore, we
320 determine the bug just by checking for __APPLE__. If this bug
321 gets ever fixed, perhaps checking for sys/version.h would be
322 appropriate, which is 10/0 on the system with the bug. */
Jack Jansen84262fb2002-07-02 14:40:42 +0000323#ifndef HAVE_GETNAMEINFO
324/* This bug seems to be fixed in Jaguar. Ths easiest way I could
325 Find to check for Jaguar is that it has getnameinfo(), which
326 older releases don't have */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000327#undef HAVE_GETADDRINFO
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000328#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000329
330#ifdef HAVE_INET_ATON
331#define USE_INET_ATON_WEAKLINK
332#endif
333
Jack Jansen84262fb2002-07-02 14:40:42 +0000334#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000335
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000336/* I know this is a bad practice, but it is the easiest... */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000337#if !defined(HAVE_GETADDRINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000338/* avoid clashes with the C library definition of the symbol. */
339#define getaddrinfo fake_getaddrinfo
340#define gai_strerror fake_gai_strerror
341#define freeaddrinfo fake_freeaddrinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000342#include "getaddrinfo.c"
343#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000344#if !defined(HAVE_GETNAMEINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000345#define getnameinfo fake_getnameinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000346#include "getnameinfo.c"
347#endif
348
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000349#ifdef MS_WINDOWS
350/* On Windows a socket is really a handle not an fd */
351static SOCKET
352dup_socket(SOCKET handle)
353{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000354 HANDLE newhandle;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000355
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000356 if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)handle,
357 GetCurrentProcess(), &newhandle,
358 0, FALSE, DUPLICATE_SAME_ACCESS))
359 {
360 WSASetLastError(GetLastError());
361 return INVALID_SOCKET;
362 }
363 return (SOCKET)newhandle;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000364}
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000365#define SOCKETCLOSE closesocket
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000366#else
367/* On Unix we can use dup to duplicate the file descriptor of a socket*/
368#define dup_socket(fd) dup(fd)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000369#endif
370
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000371#ifdef MS_WIN32
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000372#define EAFNOSUPPORT WSAEAFNOSUPPORT
373#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000374#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000375
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000376#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000377#define SOCKETCLOSE soclose
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000378#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000379#endif
380
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000381#ifndef SOCKETCLOSE
382#define SOCKETCLOSE close
383#endif
384
Jesse Noller32d68c22009-03-31 18:48:42 +0000385#if defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H) && !defined(__NetBSD__)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000386#define USE_BLUETOOTH 1
387#if defined(__FreeBSD__)
388#define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP
389#define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM
Thomas Wouterscf297e42007-02-23 15:07:44 +0000390#define BTPROTO_HCI BLUETOOTH_PROTO_HCI
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000391#define SOL_HCI SOL_HCI_RAW
392#define HCI_FILTER SO_HCI_RAW_FILTER
Hye-Shik Chang81268602004-02-02 06:05:24 +0000393#define sockaddr_l2 sockaddr_l2cap
394#define sockaddr_rc sockaddr_rfcomm
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000395#define hci_dev hci_node
Hye-Shik Chang81268602004-02-02 06:05:24 +0000396#define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb)
397#define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000398#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000399#elif defined(__NetBSD__)
400#define sockaddr_l2 sockaddr_bt
401#define sockaddr_rc sockaddr_bt
Thomas Wouterscf297e42007-02-23 15:07:44 +0000402#define sockaddr_hci sockaddr_bt
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000403#define sockaddr_sco sockaddr_bt
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000404#define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb)
405#define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000406#define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000407#define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000408#else
Hye-Shik Chang81268602004-02-02 06:05:24 +0000409#define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)
410#define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000411#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000412#define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)
413#endif
414#endif
415
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000416#ifdef __VMS
417/* TCP/IP Services for VMS uses a maximum send/recv buffer length */
418#define SEGMENT_SIZE (32 * 1024 -1)
419#endif
420
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000421#define SAS2SA(x) ((struct sockaddr *)(x))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000422
Martin v. Löwise9416172003-05-03 10:12:45 +0000423/*
424 * Constants for getnameinfo()
425 */
426#if !defined(NI_MAXHOST)
427#define NI_MAXHOST 1025
428#endif
429#if !defined(NI_MAXSERV)
430#define NI_MAXSERV 32
431#endif
432
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000433#ifndef INVALID_SOCKET /* MS defines this */
434#define INVALID_SOCKET (-1)
435#endif
436
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000437/* XXX There's a problem here: *static* functions are not supposed to have
438 a Py prefix (or use CapitalizedWords). Later... */
439
Guido van Rossum30a685f1991-06-27 15:51:29 +0000440/* Global variable holding the exception type for errors detected
441 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000442static PyObject *socket_error;
443static PyObject *socket_herror;
444static PyObject *socket_gaierror;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000445static PyObject *socket_timeout;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000446
Tim Peters643a7fc2002-02-17 04:13:21 +0000447/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000448 The sock_type variable contains pointers to various functions,
449 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000450 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000451static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000452
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000453#if defined(HAVE_POLL_H)
454#include <poll.h>
455#elif defined(HAVE_SYS_POLL_H)
456#include <sys/poll.h>
457#endif
458
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000459#ifdef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE
460/* Platform can select file descriptors beyond FD_SETSIZE */
461#define IS_SELECTABLE(s) 1
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000462#elif defined(HAVE_POLL)
463/* Instead of select(), we'll use poll() since poll() works on any fd. */
464#define IS_SELECTABLE(s) 1
465/* Can we call select() with this socket without a buffer overrun? */
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000466#else
467/* POSIX says selecting file descriptors beyond FD_SETSIZE
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000468 has undefined behaviour. If there's no timeout left, we don't have to
469 call select, so it's a safe, little white lie. */
470#define IS_SELECTABLE(s) ((s)->sock_fd < FD_SETSIZE || s->sock_timeout <= 0.0)
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000471#endif
Neal Norwitz082b2df2006-02-07 07:04:46 +0000472
473static PyObject*
474select_error(void)
475{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000476 PyErr_SetString(socket_error, "unable to select on socket");
477 return NULL;
Neal Norwitz082b2df2006-02-07 07:04:46 +0000478}
479
Guido van Rossum30a685f1991-06-27 15:51:29 +0000480/* Convenience function to raise an error according to errno
481 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000482
Guido van Rossum73624e91994-10-10 17:59:00 +0000483static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000484set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000485{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000486#ifdef MS_WINDOWS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000487 int err_no = WSAGetLastError();
488 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
489 recognizes the error codes used by both GetLastError() and
490 WSAGetLastError */
491 if (err_no)
492 return PyErr_SetExcFromWindowsErr(socket_error, err_no);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000493#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000494
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000495#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000496 if (sock_errno() != NO_ERROR) {
497 APIRET rc;
498 ULONG msglen;
499 char outbuf[100];
500 int myerrorcode = sock_errno();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000501
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000502 /* Retrieve socket-related error message from MPTN.MSG file */
503 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
504 myerrorcode - SOCBASEERR + 26,
505 "mptn.msg",
506 &msglen);
507 if (rc == NO_ERROR) {
508 PyObject *v;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000509
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000510 /* OS/2 doesn't guarantee a terminator */
511 outbuf[msglen] = '\0';
512 if (strlen(outbuf) > 0) {
513 /* If non-empty msg, trim CRLF */
514 char *lastc = &outbuf[ strlen(outbuf)-1 ];
515 while (lastc > outbuf &&
516 isspace(Py_CHARMASK(*lastc))) {
517 /* Trim trailing whitespace (CRLF) */
518 *lastc-- = '\0';
519 }
520 }
521 v = Py_BuildValue("(is)", myerrorcode, outbuf);
522 if (v != NULL) {
523 PyErr_SetObject(socket_error, v);
524 Py_DECREF(v);
525 }
526 return NULL;
527 }
528 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000529#endif
530
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000531 return PyErr_SetFromErrno(socket_error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000532}
533
Guido van Rossum30a685f1991-06-27 15:51:29 +0000534
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000535static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000536set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000537{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000538 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000539
540#ifdef HAVE_HSTRERROR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000541 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000542#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000543 v = Py_BuildValue("(is)", h_error, "host not found");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000544#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000545 if (v != NULL) {
546 PyErr_SetObject(socket_herror, v);
547 Py_DECREF(v);
548 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000549
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000550 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000551}
552
553
554static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000555set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000556{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000557 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000558
Martin v. Löwis272cb402002-03-01 08:31:07 +0000559#ifdef EAI_SYSTEM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000560 /* EAI_SYSTEM is not available on Windows XP. */
561 if (error == EAI_SYSTEM)
562 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000563#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000564
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000565#ifdef HAVE_GAI_STRERROR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000566 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000567#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000568 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000569#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000570 if (v != NULL) {
571 PyErr_SetObject(socket_gaierror, v);
572 Py_DECREF(v);
573 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000574
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000575 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000576}
577
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000578#ifdef __VMS
579/* Function to send in segments */
580static int
581sendsegmented(int sock_fd, char *buf, int len, int flags)
582{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000583 int n = 0;
584 int remaining = len;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000585
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000586 while (remaining > 0) {
587 unsigned int segment;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000588
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000589 segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining);
590 n = send(sock_fd, buf, segment, flags);
591 if (n < 0) {
592 return n;
593 }
594 remaining -= segment;
595 buf += segment;
596 } /* end while */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000597
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000598 return len;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000599}
600#endif
601
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000602/* Function to perform the setting of socket blocking mode
603 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000604static int
605internal_setblocking(PySocketSockObject *s, int block)
606{
Guido van Rossum67f7a382002-06-06 21:08:16 +0000607#ifndef MS_WINDOWS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000608 int delay_flag;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000609#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +0000610
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000611 Py_BEGIN_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000612#ifndef MS_WINDOWS
613#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000614 block = !block;
615 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000616#elif defined(__VMS)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000617 block = !block;
618 ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000619#else /* !PYOS_OS2 && !__VMS */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000620 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
621 if (block)
622 delay_flag &= (~O_NONBLOCK);
623 else
624 delay_flag |= O_NONBLOCK;
625 fcntl(s->sock_fd, F_SETFL, delay_flag);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000626#endif /* !PYOS_OS2 */
627#else /* MS_WINDOWS */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000628 block = !block;
629 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000630#endif /* MS_WINDOWS */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000631 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000632
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000633 /* Since these don't return anything */
634 return 1;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000635}
636
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000637/* Do a select()/poll() on the socket, if necessary (sock_timeout > 0).
Guido van Rossum11ba0942002-06-13 15:07:44 +0000638 The argument writing indicates the direction.
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000639 This does not raise an exception; we'll let our caller do that
640 after they've reacquired the interpreter lock.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000641 Returns 1 on timeout, -1 on error, 0 otherwise. */
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000642static int
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000643internal_select(PySocketSockObject *s, int writing)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000644{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000645 int n;
Guido van Rossum11ba0942002-06-13 15:07:44 +0000646
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000647 /* Nothing to do unless we're in timeout mode (not non-blocking) */
648 if (s->sock_timeout <= 0.0)
649 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000650
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000651 /* Guard against closed socket */
652 if (s->sock_fd < 0)
653 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000654
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000655 /* Prefer poll, if available, since you can poll() any fd
656 * which can't be done with select(). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000657#ifdef HAVE_POLL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000658 {
659 struct pollfd pollfd;
660 int timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000661
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000662 pollfd.fd = s->sock_fd;
663 pollfd.events = writing ? POLLOUT : POLLIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000664
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000665 /* s->sock_timeout is in seconds, timeout in ms */
666 timeout = (int)(s->sock_timeout * 1000 + 0.5);
667 n = poll(&pollfd, 1, timeout);
668 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000669#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000670 {
671 /* Construct the arguments to select */
672 fd_set fds;
673 struct timeval tv;
674 tv.tv_sec = (int)s->sock_timeout;
675 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
676 FD_ZERO(&fds);
677 FD_SET(s->sock_fd, &fds);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000678
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000679 /* See if the socket is ready */
680 if (writing)
Antoine Pitrouf72006f2010-08-17 19:39:39 +0000681 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
682 NULL, &fds, NULL, &tv);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000683 else
Antoine Pitrouf72006f2010-08-17 19:39:39 +0000684 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
685 &fds, NULL, NULL, &tv);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000686 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000687#endif
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000688
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000689 if (n < 0)
690 return -1;
691 if (n == 0)
692 return 1;
693 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000694}
695
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000696/* Initialize a new socket object. */
697
Tim Petersa12b4cf2002-07-18 22:38:44 +0000698static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000699
Martin v. Löwis1a214512008-06-11 05:26:20 +0000700static void
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000701init_sockobject(PySocketSockObject *s,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000702 SOCKET_T fd, int family, int type, int proto)
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000703{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000704 s->sock_fd = fd;
705 s->sock_family = family;
706 s->sock_type = type;
707 s->sock_proto = proto;
708 s->sock_timeout = defaulttimeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000709
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000710 s->errorhandler = &set_error;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000711
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000712 if (defaulttimeout >= 0.0)
713 internal_setblocking(s, 0);
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000714
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000715}
716
717
Guido van Rossum30a685f1991-06-27 15:51:29 +0000718/* Create a new socket object.
719 This just creates the object and initializes it.
720 If the creation fails, return NULL and set an exception (implicit
721 in NEWOBJ()). */
722
Guido van Rossum73624e91994-10-10 17:59:00 +0000723static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000724new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000725{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000726 PySocketSockObject *s;
727 s = (PySocketSockObject *)
728 PyType_GenericNew(&sock_type, NULL, NULL);
729 if (s != NULL)
730 init_sockobject(s, fd, family, type, proto);
731 return s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000732}
733
Guido van Rossum30a685f1991-06-27 15:51:29 +0000734
Guido van Rossum48a680c2001-03-02 06:34:14 +0000735/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000736 thread to be in gethostbyname or getaddrinfo */
737#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
738PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000739#endif
740
741
Guido van Rossum30a685f1991-06-27 15:51:29 +0000742/* Convert a string specifying a host name or one of a few symbolic
743 names to a numeric IP address. This usually calls gethostbyname()
744 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000745 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000746 an error occurred; then an exception is raised. */
747
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000748static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000749setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000750{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000751 struct addrinfo hints, *res;
752 int error;
753 int d1, d2, d3, d4;
754 char ch;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000755
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000756 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
757 if (name[0] == '\0') {
758 int siz;
759 memset(&hints, 0, sizeof(hints));
760 hints.ai_family = af;
761 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
762 hints.ai_flags = AI_PASSIVE;
763 Py_BEGIN_ALLOW_THREADS
764 ACQUIRE_GETADDRINFO_LOCK
765 error = getaddrinfo(NULL, "0", &hints, &res);
766 Py_END_ALLOW_THREADS
767 /* We assume that those thread-unsafe getaddrinfo() versions
768 *are* safe regarding their return value, ie. that a
769 subsequent call to getaddrinfo() does not destroy the
770 outcome of the first call. */
771 RELEASE_GETADDRINFO_LOCK
772 if (error) {
773 set_gaierror(error);
774 return -1;
775 }
776 switch (res->ai_family) {
777 case AF_INET:
778 siz = 4;
779 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000780#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000781 case AF_INET6:
782 siz = 16;
783 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000784#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000785 default:
786 freeaddrinfo(res);
787 PyErr_SetString(socket_error,
788 "unsupported address family");
789 return -1;
790 }
791 if (res->ai_next) {
792 freeaddrinfo(res);
793 PyErr_SetString(socket_error,
794 "wildcard resolved to multiple address");
795 return -1;
796 }
797 if (res->ai_addrlen < addr_ret_size)
798 addr_ret_size = res->ai_addrlen;
799 memcpy(addr_ret, res->ai_addr, addr_ret_size);
800 freeaddrinfo(res);
801 return siz;
802 }
803 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
804 struct sockaddr_in *sin;
805 if (af != AF_INET && af != AF_UNSPEC) {
806 PyErr_SetString(socket_error,
807 "address family mismatched");
808 return -1;
809 }
810 sin = (struct sockaddr_in *)addr_ret;
811 memset((void *) sin, '\0', sizeof(*sin));
812 sin->sin_family = AF_INET;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000813#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000814 sin->sin_len = sizeof(*sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000815#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000816 sin->sin_addr.s_addr = INADDR_BROADCAST;
817 return sizeof(sin->sin_addr);
818 }
819 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
820 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
821 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
822 struct sockaddr_in *sin;
823 sin = (struct sockaddr_in *)addr_ret;
824 sin->sin_addr.s_addr = htonl(
825 ((long) d1 << 24) | ((long) d2 << 16) |
826 ((long) d3 << 8) | ((long) d4 << 0));
827 sin->sin_family = AF_INET;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000828#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000829 sin->sin_len = sizeof(*sin);
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000830#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000831 return 4;
832 }
833 memset(&hints, 0, sizeof(hints));
834 hints.ai_family = af;
835 Py_BEGIN_ALLOW_THREADS
836 ACQUIRE_GETADDRINFO_LOCK
837 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000838#if defined(__digital__) && defined(__unix__)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000839 if (error == EAI_NONAME && af == AF_UNSPEC) {
840 /* On Tru64 V5.1, numeric-to-addr conversion fails
841 if no address family is given. Assume IPv4 for now.*/
842 hints.ai_family = AF_INET;
843 error = getaddrinfo(name, NULL, &hints, &res);
844 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000845#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000846 Py_END_ALLOW_THREADS
847 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
848 if (error) {
849 set_gaierror(error);
850 return -1;
851 }
852 if (res->ai_addrlen < addr_ret_size)
853 addr_ret_size = res->ai_addrlen;
854 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
855 freeaddrinfo(res);
856 switch (addr_ret->sa_family) {
857 case AF_INET:
858 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000859#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000860 case AF_INET6:
861 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000862#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000863 default:
864 PyErr_SetString(socket_error, "unknown address family");
865 return -1;
866 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000867}
868
Guido van Rossum30a685f1991-06-27 15:51:29 +0000869
Guido van Rossum30a685f1991-06-27 15:51:29 +0000870/* Create a string object representing an IP address.
871 This is always a string of the form 'dd.dd.dd.dd' (with variable
872 size numbers). */
873
Guido van Rossum73624e91994-10-10 17:59:00 +0000874static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000875makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000876{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000877 char buf[NI_MAXHOST];
878 int error;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000879
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000880 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
881 NI_NUMERICHOST);
882 if (error) {
883 set_gaierror(error);
884 return NULL;
885 }
886 return PyUnicode_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000887}
888
889
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000890#ifdef USE_BLUETOOTH
891/* Convert a string representation of a Bluetooth address into a numeric
892 address. Returns the length (6), or raises an exception and returns -1 if
893 an error occurred. */
894
895static int
896setbdaddr(char *name, bdaddr_t *bdaddr)
897{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000898 unsigned int b0, b1, b2, b3, b4, b5;
899 char ch;
900 int n;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000901
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000902 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
903 &b5, &b4, &b3, &b2, &b1, &b0, &ch);
904 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
905 bdaddr->b[0] = b0;
906 bdaddr->b[1] = b1;
907 bdaddr->b[2] = b2;
908 bdaddr->b[3] = b3;
909 bdaddr->b[4] = b4;
910 bdaddr->b[5] = b5;
911 return 6;
912 } else {
913 PyErr_SetString(socket_error, "bad bluetooth address");
914 return -1;
915 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000916}
917
918/* Create a string representation of the Bluetooth address. This is always a
919 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
920 value (zero padded if necessary). */
921
922static PyObject *
923makebdaddr(bdaddr_t *bdaddr)
924{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000925 char buf[(6 * 2) + 5 + 1];
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000926
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000927 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
928 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
929 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
930 return PyUnicode_FromString(buf);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000931}
932#endif
933
934
Guido van Rossum30a685f1991-06-27 15:51:29 +0000935/* Create an object representing the given socket address,
936 suitable for passing it back to bind(), connect() etc.
937 The family field of the sockaddr structure is inspected
938 to determine what kind of address it really is. */
939
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000940/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000941static PyObject *
Antoine Pitrouf72006f2010-08-17 19:39:39 +0000942makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000943{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000944 if (addrlen == 0) {
945 /* No address -- may be recvfrom() from known socket */
946 Py_INCREF(Py_None);
947 return Py_None;
948 }
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000949
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000950 switch (addr->sa_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +0000951
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000952 case AF_INET:
953 {
954 struct sockaddr_in *a;
955 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
956 PyObject *ret = NULL;
957 if (addrobj) {
958 a = (struct sockaddr_in *)addr;
959 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
960 Py_DECREF(addrobj);
961 }
962 return ret;
963 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000964
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +0000965#if defined(AF_UNIX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000966 case AF_UNIX:
967 {
968 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000969#ifdef linux
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000970 if (a->sun_path[0] == 0) { /* Linux abstract namespace */
971 addrlen -= offsetof(struct sockaddr_un, sun_path);
972 return PyBytes_FromStringAndSize(a->sun_path, addrlen);
973 }
974 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000975#endif /* linux */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000976 {
977 /* regular NULL-terminated string */
978 return PyUnicode_FromString(a->sun_path);
979 }
980 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000981#endif /* AF_UNIX */
982
Martin v. Löwis11017b12006-01-14 18:12:57 +0000983#if defined(AF_NETLINK)
984 case AF_NETLINK:
985 {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000986 struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
987 return Py_BuildValue("II", a->nl_pid, a->nl_groups);
Martin v. Löwis11017b12006-01-14 18:12:57 +0000988 }
989#endif /* AF_NETLINK */
990
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000991#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000992 case AF_INET6:
993 {
994 struct sockaddr_in6 *a;
995 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
996 PyObject *ret = NULL;
997 if (addrobj) {
998 a = (struct sockaddr_in6 *)addr;
999 ret = Py_BuildValue("Oiii",
1000 addrobj,
1001 ntohs(a->sin6_port),
1002 a->sin6_flowinfo,
1003 a->sin6_scope_id);
1004 Py_DECREF(addrobj);
1005 }
1006 return ret;
1007 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001008#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001009
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001010#ifdef USE_BLUETOOTH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001011 case AF_BLUETOOTH:
1012 switch (proto) {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001013
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001014 case BTPROTO_L2CAP:
1015 {
1016 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
1017 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
1018 PyObject *ret = NULL;
1019 if (addrobj) {
1020 ret = Py_BuildValue("Oi",
1021 addrobj,
1022 _BT_L2_MEMB(a, psm));
1023 Py_DECREF(addrobj);
1024 }
1025 return ret;
1026 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001027
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001028 case BTPROTO_RFCOMM:
1029 {
1030 struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
1031 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
1032 PyObject *ret = NULL;
1033 if (addrobj) {
1034 ret = Py_BuildValue("Oi",
1035 addrobj,
1036 _BT_RC_MEMB(a, channel));
1037 Py_DECREF(addrobj);
1038 }
1039 return ret;
1040 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001041
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001042 case BTPROTO_HCI:
1043 {
1044 struct sockaddr_hci *a = (struct sockaddr_hci *) addr;
1045 PyObject *ret = NULL;
1046 ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
1047 return ret;
1048 }
Thomas Wouterscf297e42007-02-23 15:07:44 +00001049
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001050#if !defined(__FreeBSD__)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001051 case BTPROTO_SCO:
1052 {
1053 struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
1054 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
1055 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001056#endif
1057
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001058 default:
1059 PyErr_SetString(PyExc_ValueError,
1060 "Unknown Bluetooth protocol");
1061 return NULL;
1062 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001063#endif
1064
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001065#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001066 case AF_PACKET:
1067 {
1068 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
1069 char *ifname = "";
1070 struct ifreq ifr;
1071 /* need to look up interface name give index */
1072 if (a->sll_ifindex) {
1073 ifr.ifr_ifindex = a->sll_ifindex;
1074 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
1075 ifname = ifr.ifr_name;
1076 }
1077 return Py_BuildValue("shbhy#",
1078 ifname,
1079 ntohs(a->sll_protocol),
1080 a->sll_pkttype,
1081 a->sll_hatype,
1082 a->sll_addr,
1083 a->sll_halen);
1084 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001085#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001086
Christian Heimes043d6f62008-01-07 17:19:16 +00001087#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001088 case AF_TIPC:
1089 {
1090 struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr;
1091 if (a->addrtype == TIPC_ADDR_NAMESEQ) {
1092 return Py_BuildValue("IIIII",
1093 a->addrtype,
1094 a->addr.nameseq.type,
1095 a->addr.nameseq.lower,
1096 a->addr.nameseq.upper,
1097 a->scope);
1098 } else if (a->addrtype == TIPC_ADDR_NAME) {
1099 return Py_BuildValue("IIIII",
1100 a->addrtype,
1101 a->addr.name.name.type,
1102 a->addr.name.name.instance,
1103 a->addr.name.name.instance,
1104 a->scope);
1105 } else if (a->addrtype == TIPC_ADDR_ID) {
1106 return Py_BuildValue("IIIII",
1107 a->addrtype,
1108 a->addr.id.node,
1109 a->addr.id.ref,
1110 0,
1111 a->scope);
1112 } else {
1113 PyErr_SetString(PyExc_ValueError,
1114 "Invalid address type");
1115 return NULL;
1116 }
1117 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001118#endif
1119
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001120 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001121
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001122 default:
1123 /* If we don't know the address family, don't raise an
1124 exception -- return it as an (int, bytes) tuple. */
1125 return Py_BuildValue("iy#",
1126 addr->sa_family,
1127 addr->sa_data,
1128 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001129
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001130 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001131}
1132
Guido van Rossum30a685f1991-06-27 15:51:29 +00001133
1134/* Parse a socket address argument according to the socket object's
1135 address family. Return 1 if the address was in the proper format,
1136 0 of not. The address is returned through addr_ret, its length
1137 through len_ret. */
1138
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001139static int
Guido van Rossum48a680c2001-03-02 06:34:14 +00001140getsockaddrarg(PySocketSockObject *s, PyObject *args,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001141 struct sockaddr *addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001142{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001143 switch (s->sock_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +00001144
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001145#if defined(AF_UNIX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001146 case AF_UNIX:
1147 {
1148 struct sockaddr_un* addr;
1149 char *path;
1150 int len;
1151 if (!PyArg_Parse(args, "s#", &path, &len))
1152 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001153
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001154 addr = (struct sockaddr_un*)addr_ret;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001155#ifdef linux
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001156 if (len > 0 && path[0] == 0) {
1157 /* Linux abstract namespace extension */
1158 if (len > sizeof addr->sun_path) {
1159 PyErr_SetString(socket_error,
1160 "AF_UNIX path too long");
1161 return 0;
1162 }
1163 }
1164 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001165#endif /* linux */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001166 {
1167 /* regular NULL-terminated string */
1168 if (len >= sizeof addr->sun_path) {
1169 PyErr_SetString(socket_error,
1170 "AF_UNIX path too long");
1171 return 0;
1172 }
1173 addr->sun_path[len] = 0;
1174 }
1175 addr->sun_family = s->sock_family;
1176 memcpy(addr->sun_path, path, len);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001177#if defined(PYOS_OS2)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001178 *len_ret = sizeof(*addr);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001179#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001180 *len_ret = len + offsetof(struct sockaddr_un, sun_path);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001181#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001182 return 1;
1183 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001184#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001185
Martin v. Löwis11017b12006-01-14 18:12:57 +00001186#if defined(AF_NETLINK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001187 case AF_NETLINK:
1188 {
1189 struct sockaddr_nl* addr;
1190 int pid, groups;
1191 addr = (struct sockaddr_nl *)addr_ret;
1192 if (!PyTuple_Check(args)) {
1193 PyErr_Format(
1194 PyExc_TypeError,
1195 "getsockaddrarg: "
1196 "AF_NETLINK address must be tuple, not %.500s",
1197 Py_TYPE(args)->tp_name);
1198 return 0;
1199 }
1200 if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
1201 return 0;
1202 addr->nl_family = AF_NETLINK;
1203 addr->nl_pid = pid;
1204 addr->nl_groups = groups;
1205 *len_ret = sizeof(*addr);
1206 return 1;
1207 }
Martin v. Löwis11017b12006-01-14 18:12:57 +00001208#endif
1209
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001210 case AF_INET:
1211 {
1212 struct sockaddr_in* addr;
1213 char *host;
1214 int port, result;
1215 if (!PyTuple_Check(args)) {
1216 PyErr_Format(
1217 PyExc_TypeError,
1218 "getsockaddrarg: "
1219 "AF_INET address must be tuple, not %.500s",
1220 Py_TYPE(args)->tp_name);
1221 return 0;
1222 }
1223 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
1224 "idna", &host, &port))
1225 return 0;
1226 addr=(struct sockaddr_in*)addr_ret;
1227 result = setipaddr(host, (struct sockaddr *)addr,
1228 sizeof(*addr), AF_INET);
1229 PyMem_Free(host);
1230 if (result < 0)
1231 return 0;
1232 if (port < 0 || port > 0xffff) {
1233 PyErr_SetString(
1234 PyExc_OverflowError,
1235 "getsockaddrarg: port must be 0-65535.");
1236 return 0;
1237 }
1238 addr->sin_family = AF_INET;
1239 addr->sin_port = htons((short)port);
1240 *len_ret = sizeof *addr;
1241 return 1;
1242 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001243
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001244#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001245 case AF_INET6:
1246 {
1247 struct sockaddr_in6* addr;
1248 char *host;
1249 int port, flowinfo, scope_id, result;
1250 flowinfo = scope_id = 0;
1251 if (!PyTuple_Check(args)) {
1252 PyErr_Format(
1253 PyExc_TypeError,
1254 "getsockaddrarg: "
1255 "AF_INET6 address must be tuple, not %.500s",
1256 Py_TYPE(args)->tp_name);
1257 return 0;
1258 }
1259 if (!PyArg_ParseTuple(args, "eti|ii",
1260 "idna", &host, &port, &flowinfo,
1261 &scope_id)) {
1262 return 0;
1263 }
1264 addr = (struct sockaddr_in6*)addr_ret;
1265 result = setipaddr(host, (struct sockaddr *)addr,
1266 sizeof(*addr), AF_INET6);
1267 PyMem_Free(host);
1268 if (result < 0)
1269 return 0;
1270 if (port < 0 || port > 0xffff) {
1271 PyErr_SetString(
1272 PyExc_OverflowError,
1273 "getsockaddrarg: port must be 0-65535.");
1274 return 0;
1275 }
1276 addr->sin6_family = s->sock_family;
1277 addr->sin6_port = htons((short)port);
1278 addr->sin6_flowinfo = flowinfo;
1279 addr->sin6_scope_id = scope_id;
1280 *len_ret = sizeof *addr;
1281 return 1;
1282 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001283#endif
1284
Hye-Shik Chang81268602004-02-02 06:05:24 +00001285#ifdef USE_BLUETOOTH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001286 case AF_BLUETOOTH:
1287 {
1288 switch (s->sock_proto) {
1289 case BTPROTO_L2CAP:
1290 {
1291 struct sockaddr_l2 *addr;
1292 char *straddr;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001293
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001294 addr = (struct sockaddr_l2 *)addr_ret;
1295 memset(addr, 0, sizeof(struct sockaddr_l2));
1296 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
1297 if (!PyArg_ParseTuple(args, "si", &straddr,
1298 &_BT_L2_MEMB(addr, psm))) {
1299 PyErr_SetString(socket_error, "getsockaddrarg: "
1300 "wrong format");
1301 return 0;
1302 }
1303 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
1304 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001305
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001306 *len_ret = sizeof *addr;
1307 return 1;
1308 }
1309 case BTPROTO_RFCOMM:
1310 {
1311 struct sockaddr_rc *addr;
1312 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001313
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001314 addr = (struct sockaddr_rc *)addr_ret;
1315 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
1316 if (!PyArg_ParseTuple(args, "si", &straddr,
1317 &_BT_RC_MEMB(addr, channel))) {
1318 PyErr_SetString(socket_error, "getsockaddrarg: "
1319 "wrong format");
1320 return 0;
1321 }
1322 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
1323 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001324
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001325 *len_ret = sizeof *addr;
1326 return 1;
1327 }
1328 case BTPROTO_HCI:
1329 {
1330 struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret;
1331 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
1332 if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) {
1333 PyErr_SetString(socket_error, "getsockaddrarg: "
1334 "wrong format");
1335 return 0;
1336 }
1337 *len_ret = sizeof *addr;
1338 return 1;
1339 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001340#if !defined(__FreeBSD__)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001341 case BTPROTO_SCO:
1342 {
1343 struct sockaddr_sco *addr;
1344 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001345
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001346 addr = (struct sockaddr_sco *)addr_ret;
1347 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
1348 if (!PyBytes_Check(args)) {
1349 PyErr_SetString(socket_error, "getsockaddrarg: "
1350 "wrong format");
1351 return 0;
1352 }
1353 straddr = PyBytes_AS_STRING(args);
1354 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
1355 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001356
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001357 *len_ret = sizeof *addr;
1358 return 1;
1359 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001360#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001361 default:
1362 PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol");
1363 return 0;
1364 }
1365 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001366#endif
1367
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001368#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001369 case AF_PACKET:
1370 {
1371 struct sockaddr_ll* addr;
1372 struct ifreq ifr;
1373 char *interfaceName;
1374 int protoNumber;
1375 int hatype = 0;
1376 int pkttype = 0;
1377 char *haddr = NULL;
1378 unsigned int halen = 0;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001379
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001380 if (!PyTuple_Check(args)) {
1381 PyErr_Format(
1382 PyExc_TypeError,
1383 "getsockaddrarg: "
1384 "AF_PACKET address must be tuple, not %.500s",
1385 Py_TYPE(args)->tp_name);
1386 return 0;
1387 }
1388 if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName,
1389 &protoNumber, &pkttype, &hatype,
1390 &haddr, &halen))
1391 return 0;
1392 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1393 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
1394 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
1395 s->errorhandler();
1396 return 0;
1397 }
1398 if (halen > 8) {
1399 PyErr_SetString(PyExc_ValueError,
1400 "Hardware address must be 8 bytes or less");
1401 return 0;
1402 }
1403 if (protoNumber < 0 || protoNumber > 0xffff) {
1404 PyErr_SetString(
1405 PyExc_OverflowError,
1406 "getsockaddrarg: protoNumber must be 0-65535.");
1407 return 0;
1408 }
1409 addr = (struct sockaddr_ll*)addr_ret;
1410 addr->sll_family = AF_PACKET;
1411 addr->sll_protocol = htons((short)protoNumber);
1412 addr->sll_ifindex = ifr.ifr_ifindex;
1413 addr->sll_pkttype = pkttype;
1414 addr->sll_hatype = hatype;
1415 if (halen != 0) {
1416 memcpy(&addr->sll_addr, haddr, halen);
1417 }
1418 addr->sll_halen = halen;
1419 *len_ret = sizeof *addr;
1420 return 1;
1421 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001422#endif
1423
Christian Heimes043d6f62008-01-07 17:19:16 +00001424#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001425 case AF_TIPC:
1426 {
1427 unsigned int atype, v1, v2, v3;
1428 unsigned int scope = TIPC_CLUSTER_SCOPE;
1429 struct sockaddr_tipc *addr;
Christian Heimes043d6f62008-01-07 17:19:16 +00001430
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001431 if (!PyTuple_Check(args)) {
1432 PyErr_Format(
1433 PyExc_TypeError,
1434 "getsockaddrarg: "
1435 "AF_TIPC address must be tuple, not %.500s",
1436 Py_TYPE(args)->tp_name);
1437 return 0;
1438 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001439
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001440 if (!PyArg_ParseTuple(args,
1441 "IIII|I;Invalid TIPC address format",
1442 &atype, &v1, &v2, &v3, &scope))
1443 return 0;
Christian Heimes043d6f62008-01-07 17:19:16 +00001444
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001445 addr = (struct sockaddr_tipc *) addr_ret;
1446 memset(addr, 0, sizeof(struct sockaddr_tipc));
Christian Heimes043d6f62008-01-07 17:19:16 +00001447
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001448 addr->family = AF_TIPC;
1449 addr->scope = scope;
1450 addr->addrtype = atype;
Christian Heimes043d6f62008-01-07 17:19:16 +00001451
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001452 if (atype == TIPC_ADDR_NAMESEQ) {
1453 addr->addr.nameseq.type = v1;
1454 addr->addr.nameseq.lower = v2;
1455 addr->addr.nameseq.upper = v3;
1456 } else if (atype == TIPC_ADDR_NAME) {
1457 addr->addr.name.name.type = v1;
1458 addr->addr.name.name.instance = v2;
1459 } else if (atype == TIPC_ADDR_ID) {
1460 addr->addr.id.node = v1;
1461 addr->addr.id.ref = v2;
1462 } else {
1463 /* Shouldn't happen */
1464 PyErr_SetString(PyExc_TypeError, "Invalid address type");
1465 return 0;
1466 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001467
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001468 *len_ret = sizeof(*addr);
Christian Heimes043d6f62008-01-07 17:19:16 +00001469
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001470 return 1;
1471 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001472#endif
1473
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001474 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001475
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001476 default:
1477 PyErr_SetString(socket_error, "getsockaddrarg: bad family");
1478 return 0;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001479
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001480 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001481}
1482
Guido van Rossum30a685f1991-06-27 15:51:29 +00001483
Guido van Rossum48a680c2001-03-02 06:34:14 +00001484/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00001485 Return 1 if the family is known, 0 otherwise. The length is returned
1486 through len_ret. */
1487
1488static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00001489getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001490{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001491 switch (s->sock_family) {
Guido van Rossum710e1df1992-06-12 10:39:36 +00001492
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001493#if defined(AF_UNIX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001494 case AF_UNIX:
1495 {
1496 *len_ret = sizeof (struct sockaddr_un);
1497 return 1;
1498 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001499#endif /* AF_UNIX */
Martin v. Löwis11017b12006-01-14 18:12:57 +00001500#if defined(AF_NETLINK)
1501 case AF_NETLINK:
1502 {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001503 *len_ret = sizeof (struct sockaddr_nl);
1504 return 1;
Martin v. Löwis11017b12006-01-14 18:12:57 +00001505 }
1506#endif
Guido van Rossum710e1df1992-06-12 10:39:36 +00001507
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001508 case AF_INET:
1509 {
1510 *len_ret = sizeof (struct sockaddr_in);
1511 return 1;
1512 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00001513
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001514#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001515 case AF_INET6:
1516 {
1517 *len_ret = sizeof (struct sockaddr_in6);
1518 return 1;
1519 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001520#endif
1521
Hye-Shik Chang81268602004-02-02 06:05:24 +00001522#ifdef USE_BLUETOOTH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001523 case AF_BLUETOOTH:
1524 {
1525 switch(s->sock_proto)
1526 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001527
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001528 case BTPROTO_L2CAP:
1529 *len_ret = sizeof (struct sockaddr_l2);
1530 return 1;
1531 case BTPROTO_RFCOMM:
1532 *len_ret = sizeof (struct sockaddr_rc);
1533 return 1;
1534 case BTPROTO_HCI:
1535 *len_ret = sizeof (struct sockaddr_hci);
1536 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001537#if !defined(__FreeBSD__)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001538 case BTPROTO_SCO:
1539 *len_ret = sizeof (struct sockaddr_sco);
1540 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001541#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001542 default:
1543 PyErr_SetString(socket_error, "getsockaddrlen: "
1544 "unknown BT protocol");
1545 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001546
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001547 }
1548 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001549#endif
1550
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001551#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001552 case AF_PACKET:
1553 {
1554 *len_ret = sizeof (struct sockaddr_ll);
1555 return 1;
1556 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001557#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001558
Christian Heimes043d6f62008-01-07 17:19:16 +00001559#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001560 case AF_TIPC:
1561 {
1562 *len_ret = sizeof (struct sockaddr_tipc);
1563 return 1;
1564 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001565#endif
1566
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001567 /* More cases here... */
Guido van Rossum710e1df1992-06-12 10:39:36 +00001568
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001569 default:
1570 PyErr_SetString(socket_error, "getsockaddrlen: bad family");
1571 return 0;
Guido van Rossum710e1df1992-06-12 10:39:36 +00001572
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001573 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00001574}
1575
1576
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001577/* s._accept() -> (fd, address) */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001578
Guido van Rossum73624e91994-10-10 17:59:00 +00001579static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001580sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001581{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001582 sock_addr_t addrbuf;
1583 SOCKET_T newfd = INVALID_SOCKET;
1584 socklen_t addrlen;
1585 PyObject *sock = NULL;
1586 PyObject *addr = NULL;
1587 PyObject *res = NULL;
1588 int timeout;
Barry Warsaw752300b1997-01-03 17:18:10 +00001589
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001590 if (!getsockaddrlen(s, &addrlen))
1591 return NULL;
1592 memset(&addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001593
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001594 if (!IS_SELECTABLE(s))
1595 return select_error();
Neal Norwitz082b2df2006-02-07 07:04:46 +00001596
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001597 Py_BEGIN_ALLOW_THREADS
1598 timeout = internal_select(s, 0);
1599 if (!timeout)
1600 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
1601 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001602
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001603 if (timeout == 1) {
1604 PyErr_SetString(socket_timeout, "timed out");
1605 return NULL;
1606 }
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001607
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001608 if (newfd == INVALID_SOCKET)
1609 return s->errorhandler();
Barry Warsaw752300b1997-01-03 17:18:10 +00001610
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001611 sock = PyLong_FromSocket_t(newfd);
1612 if (sock == NULL) {
1613 SOCKETCLOSE(newfd);
1614 goto finally;
1615 }
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001616
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001617 addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
1618 addrlen, s->sock_proto);
1619 if (addr == NULL)
1620 goto finally;
Barry Warsaw752300b1997-01-03 17:18:10 +00001621
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001622 res = PyTuple_Pack(2, sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001623
Guido van Rossum67f7a382002-06-06 21:08:16 +00001624finally:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001625 Py_XDECREF(sock);
1626 Py_XDECREF(addr);
1627 return res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001628}
1629
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001630PyDoc_STRVAR(accept_doc,
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001631"_accept() -> (integer, address info)\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001632\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001633Wait for an incoming connection. Return a new socket file descriptor\n\
1634representing the connection, and the address of the client.\n\
1635For IP sockets, the address info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001636
Guido van Rossum11ba0942002-06-13 15:07:44 +00001637/* s.setblocking(flag) method. Argument:
1638 False -- non-blocking mode; same as settimeout(0)
1639 True -- blocking mode; same as settimeout(None)
1640*/
Guido van Rossume4485b01994-09-07 14:32:49 +00001641
Guido van Rossum73624e91994-10-10 17:59:00 +00001642static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001643sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00001644{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001645 int block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001646
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001647 block = PyLong_AsLong(arg);
1648 if (block == -1 && PyErr_Occurred())
1649 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001650
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001651 s->sock_timeout = block ? -1.0 : 0.0;
1652 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00001653
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001654 Py_INCREF(Py_None);
1655 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00001656}
Guido van Rossume4485b01994-09-07 14:32:49 +00001657
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001658PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001659"setblocking(flag)\n\
1660\n\
1661Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001662setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001663setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001664
Guido van Rossum11ba0942002-06-13 15:07:44 +00001665/* s.settimeout(timeout) method. Argument:
1666 None -- no timeout, blocking mode; same as setblocking(True)
1667 0.0 -- non-blocking mode; same as setblocking(False)
1668 > 0 -- timeout mode; operations time out after timeout seconds
1669 < 0 -- illegal; raises an exception
1670*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00001671static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001672sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001673{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001674 double timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001675
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001676 if (arg == Py_None)
1677 timeout = -1.0;
1678 else {
1679 timeout = PyFloat_AsDouble(arg);
1680 if (timeout < 0.0) {
1681 if (!PyErr_Occurred())
1682 PyErr_SetString(PyExc_ValueError,
1683 "Timeout value out of range");
1684 return NULL;
1685 }
1686 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001687
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001688 s->sock_timeout = timeout;
1689 internal_setblocking(s, timeout < 0.0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001690
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001691 Py_INCREF(Py_None);
1692 return Py_None;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001693}
1694
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001695PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001696"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001697\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001698Set a timeout on socket operations. 'timeout' can be a float,\n\
1699giving in seconds, or None. Setting a timeout of None disables\n\
1700the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001701Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001702
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001703/* s.gettimeout() method.
1704 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001705static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001706sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001707{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001708 if (s->sock_timeout < 0.0) {
1709 Py_INCREF(Py_None);
1710 return Py_None;
1711 }
1712 else
1713 return PyFloat_FromDouble(s->sock_timeout);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001714}
1715
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001716PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001717"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001718\n\
1719Returns the timeout in floating seconds associated with socket \n\
1720operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001721operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00001722
Guido van Rossumaee08791992-09-08 09:05:33 +00001723/* s.setsockopt() method.
1724 With an integer third argument, sets an integer option.
1725 With a string third argument, sets an option from a buffer;
1726 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001727
Guido van Rossum73624e91994-10-10 17:59:00 +00001728static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001729sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001730{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001731 int level;
1732 int optname;
1733 int res;
1734 char *buf;
1735 int buflen;
1736 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001737
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001738 if (PyArg_ParseTuple(args, "iii:setsockopt",
1739 &level, &optname, &flag)) {
1740 buf = (char *) &flag;
1741 buflen = sizeof flag;
1742 }
1743 else {
1744 PyErr_Clear();
1745 if (!PyArg_ParseTuple(args, "iiy#:setsockopt",
1746 &level, &optname, &buf, &buflen))
1747 return NULL;
1748 }
1749 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
1750 if (res < 0)
1751 return s->errorhandler();
1752 Py_INCREF(Py_None);
1753 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001754}
1755
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001756PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001757"setsockopt(level, option, value)\n\
1758\n\
1759Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001760The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001761
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001762
Guido van Rossumaee08791992-09-08 09:05:33 +00001763/* s.getsockopt() method.
1764 With two arguments, retrieves an integer option.
1765 With a third integer argument, retrieves a string buffer of that size;
1766 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001767
Guido van Rossum73624e91994-10-10 17:59:00 +00001768static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001769sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001770{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001771 int level;
1772 int optname;
1773 int res;
1774 PyObject *buf;
1775 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001776
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001777 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
1778 &level, &optname, &buflen))
1779 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001780
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001781 if (buflen == 0) {
1782 int flag = 0;
1783 socklen_t flagsize = sizeof flag;
1784 res = getsockopt(s->sock_fd, level, optname,
1785 (void *)&flag, &flagsize);
1786 if (res < 0)
1787 return s->errorhandler();
1788 return PyLong_FromLong(flag);
1789 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001790#ifdef __VMS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001791 /* socklen_t is unsigned so no negative test is needed,
1792 test buflen == 0 is previously done */
1793 if (buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001794#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001795 if (buflen <= 0 || buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001796#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001797 PyErr_SetString(socket_error,
1798 "getsockopt buflen out of range");
1799 return NULL;
1800 }
1801 buf = PyBytes_FromStringAndSize((char *)NULL, buflen);
1802 if (buf == NULL)
1803 return NULL;
1804 res = getsockopt(s->sock_fd, level, optname,
1805 (void *)PyBytes_AS_STRING(buf), &buflen);
1806 if (res < 0) {
1807 Py_DECREF(buf);
1808 return s->errorhandler();
1809 }
1810 _PyBytes_Resize(&buf, buflen);
1811 return buf;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001812}
1813
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001814PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001815"getsockopt(level, option[, buffersize]) -> value\n\
1816\n\
1817Get a socket option. See the Unix manual for level and option.\n\
1818If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001819string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001820
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001821
Fred Drake728819a2000-07-01 03:40:12 +00001822/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001823
Guido van Rossum73624e91994-10-10 17:59:00 +00001824static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001825sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001826{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001827 sock_addr_t addrbuf;
1828 int addrlen;
1829 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001830
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001831 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
1832 return NULL;
1833 Py_BEGIN_ALLOW_THREADS
1834 res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);
1835 Py_END_ALLOW_THREADS
1836 if (res < 0)
1837 return s->errorhandler();
1838 Py_INCREF(Py_None);
1839 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001840}
1841
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001842PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001843"bind(address)\n\
1844\n\
1845Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00001846pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001847sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001848
Guido van Rossum30a685f1991-06-27 15:51:29 +00001849
1850/* s.close() method.
1851 Set the file descriptor to -1 so operations tried subsequently
1852 will surely fail. */
1853
Guido van Rossum73624e91994-10-10 17:59:00 +00001854static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001855sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001856{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001857 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001858
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001859 if ((fd = s->sock_fd) != -1) {
1860 s->sock_fd = -1;
1861 Py_BEGIN_ALLOW_THREADS
1862 (void) SOCKETCLOSE(fd);
1863 Py_END_ALLOW_THREADS
1864 }
1865 Py_INCREF(Py_None);
1866 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001867}
1868
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001869PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001870"close()\n\
1871\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001872Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001873
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001874static int
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001875internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001876 int *timeoutp)
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001877{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001878 int res, timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001879
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001880 timeout = 0;
1881 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001882
1883#ifdef MS_WINDOWS
1884
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001885 if (s->sock_timeout > 0.0) {
1886 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK &&
1887 IS_SELECTABLE(s)) {
1888 /* This is a mess. Best solution: trust select */
1889 fd_set fds;
1890 fd_set fds_exc;
1891 struct timeval tv;
1892 tv.tv_sec = (int)s->sock_timeout;
1893 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
1894 FD_ZERO(&fds);
1895 FD_SET(s->sock_fd, &fds);
1896 FD_ZERO(&fds_exc);
1897 FD_SET(s->sock_fd, &fds_exc);
Antoine Pitrouf72006f2010-08-17 19:39:39 +00001898 res = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
1899 NULL, &fds, &fds_exc, &tv);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001900 if (res == 0) {
1901 res = WSAEWOULDBLOCK;
1902 timeout = 1;
1903 } else if (res > 0) {
1904 if (FD_ISSET(s->sock_fd, &fds))
1905 /* The socket is in the writable set - this
1906 means connected */
1907 res = 0;
1908 else {
1909 /* As per MS docs, we need to call getsockopt()
1910 to get the underlying error */
1911 int res_size = sizeof res;
1912 /* It must be in the exception set */
1913 assert(FD_ISSET(s->sock_fd, &fds_exc));
1914 if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
1915 (char *)&res, &res_size))
1916 /* getsockopt also clears WSAGetLastError,
1917 so reset it back. */
1918 WSASetLastError(res);
1919 else
1920 res = WSAGetLastError();
1921 }
1922 }
1923 /* else if (res < 0) an error occurred */
1924 }
1925 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001926
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001927 if (res < 0)
1928 res = WSAGetLastError();
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001929
1930#else
1931
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001932 if (s->sock_timeout > 0.0) {
1933 if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) {
1934 timeout = internal_select(s, 1);
1935 if (timeout == 0) {
1936 /* Bug #1019808: in case of an EINPROGRESS,
1937 use getsockopt(SO_ERROR) to get the real
1938 error. */
1939 socklen_t res_size = sizeof res;
1940 (void)getsockopt(s->sock_fd, SOL_SOCKET,
1941 SO_ERROR, &res, &res_size);
1942 if (res == EISCONN)
1943 res = 0;
1944 errno = res;
1945 }
1946 else if (timeout == -1) {
1947 res = errno; /* had error */
1948 }
1949 else
1950 res = EWOULDBLOCK; /* timed out */
1951 }
1952 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001953
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001954 if (res < 0)
1955 res = errno;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001956
1957#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001958 *timeoutp = timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001959
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001960 return res;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001961}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001962
Fred Drake728819a2000-07-01 03:40:12 +00001963/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001964
Guido van Rossum73624e91994-10-10 17:59:00 +00001965static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001966sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001967{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001968 sock_addr_t addrbuf;
1969 int addrlen;
1970 int res;
1971 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001972
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001973 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
1974 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001975
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001976 Py_BEGIN_ALLOW_THREADS
1977 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
1978 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001979
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001980 if (timeout == 1) {
1981 PyErr_SetString(socket_timeout, "timed out");
1982 return NULL;
1983 }
1984 if (res != 0)
1985 return s->errorhandler();
1986 Py_INCREF(Py_None);
1987 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001988}
1989
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001990PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001991"connect(address)\n\
1992\n\
1993Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001994is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001995
Guido van Rossum30a685f1991-06-27 15:51:29 +00001996
Fred Drake728819a2000-07-01 03:40:12 +00001997/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001998
1999static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002000sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002001{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002002 sock_addr_t addrbuf;
2003 int addrlen;
2004 int res;
2005 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002006
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002007 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2008 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002009
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002010 Py_BEGIN_ALLOW_THREADS
2011 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
2012 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002013
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002014 /* Signals are not errors (though they may raise exceptions). Adapted
2015 from PyErr_SetFromErrnoWithFilenameObject(). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002016#ifdef EINTR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002017 if (res == EINTR && PyErr_CheckSignals())
2018 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002019#endif
2020
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002021 return PyLong_FromLong((long) res);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002022}
2023
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002024PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002025"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002026\n\
2027This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002028instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002029
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002030
Guido van Rossumed233a51992-06-23 09:07:03 +00002031/* s.fileno() method */
2032
Guido van Rossum73624e91994-10-10 17:59:00 +00002033static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002034sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00002035{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002036 return PyLong_FromSocket_t(s->sock_fd);
Guido van Rossumed233a51992-06-23 09:07:03 +00002037}
2038
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002039PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002040"fileno() -> integer\n\
2041\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002042Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002043
Guido van Rossumed233a51992-06-23 09:07:03 +00002044
Guido van Rossumc89705d1992-11-26 08:54:07 +00002045/* s.getsockname() method */
2046
Guido van Rossum73624e91994-10-10 17:59:00 +00002047static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002048sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002049{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002050 sock_addr_t addrbuf;
2051 int res;
2052 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002053
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002054 if (!getsockaddrlen(s, &addrlen))
2055 return NULL;
2056 memset(&addrbuf, 0, addrlen);
2057 Py_BEGIN_ALLOW_THREADS
2058 res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2059 Py_END_ALLOW_THREADS
2060 if (res < 0)
2061 return s->errorhandler();
2062 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2063 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002064}
2065
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002066PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002067"getsockname() -> address info\n\
2068\n\
2069Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002070info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002071
Guido van Rossumc89705d1992-11-26 08:54:07 +00002072
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002073#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002074/* s.getpeername() method */
2075
Guido van Rossum73624e91994-10-10 17:59:00 +00002076static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002077sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002078{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002079 sock_addr_t addrbuf;
2080 int res;
2081 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002082
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002083 if (!getsockaddrlen(s, &addrlen))
2084 return NULL;
2085 memset(&addrbuf, 0, addrlen);
2086 Py_BEGIN_ALLOW_THREADS
2087 res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2088 Py_END_ALLOW_THREADS
2089 if (res < 0)
2090 return s->errorhandler();
2091 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2092 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002093}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002094
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002095PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002096"getpeername() -> address info\n\
2097\n\
2098Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002099info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002100
Guido van Rossumb6775db1994-08-01 11:34:53 +00002101#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002102
2103
Guido van Rossum30a685f1991-06-27 15:51:29 +00002104/* s.listen(n) method */
2105
Guido van Rossum73624e91994-10-10 17:59:00 +00002106static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002107sock_listen(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002108{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002109 int backlog;
2110 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002111
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002112 backlog = PyLong_AsLong(arg);
2113 if (backlog == -1 && PyErr_Occurred())
2114 return NULL;
2115 Py_BEGIN_ALLOW_THREADS
2116 if (backlog < 1)
2117 backlog = 1;
2118 res = listen(s->sock_fd, backlog);
2119 Py_END_ALLOW_THREADS
2120 if (res < 0)
2121 return s->errorhandler();
2122 Py_INCREF(Py_None);
2123 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002124}
2125
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002126PyDoc_STRVAR(listen_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002127"listen(backlog)\n\
2128\n\
2129Enable a server to accept connections. The backlog argument must be at\n\
2130least 1; it specifies the number of unaccepted connection that the system\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002131will allow before refusing new connections.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002132
2133
Thomas Wouters477c8d52006-05-27 19:21:47 +00002134/*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002135 * This is the guts of the recv() and recv_into() methods, which reads into a
Thomas Wouters902d6eb2007-01-09 23:18:33 +00002136 * char buffer. If you have any inc/dec ref to do to the objects that contain
Thomas Wouters477c8d52006-05-27 19:21:47 +00002137 * the buffer, do it in the caller. This function returns the number of bytes
2138 * succesfully read. If there was an error, it returns -1. Note that it is
2139 * also possible that we return a number of bytes smaller than the request
2140 * bytes.
2141 */
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002142static Py_ssize_t
2143sock_recv_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002144{
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002145 Py_ssize_t outlen = -1;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002146 int timeout;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002147#ifdef __VMS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002148 int remaining;
2149 char *read_buf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002150#endif
2151
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002152 if (!IS_SELECTABLE(s)) {
2153 select_error();
2154 return -1;
2155 }
2156 if (len == 0) {
2157 /* If 0 bytes were requested, do nothing. */
2158 return 0;
2159 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002160
2161#ifndef __VMS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002162 Py_BEGIN_ALLOW_THREADS
2163 timeout = internal_select(s, 0);
2164 if (!timeout)
2165 outlen = recv(s->sock_fd, cbuf, len, flags);
2166 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002167
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002168 if (timeout == 1) {
2169 PyErr_SetString(socket_timeout, "timed out");
2170 return -1;
2171 }
2172 if (outlen < 0) {
2173 /* Note: the call to errorhandler() ALWAYS indirectly returned
2174 NULL, so ignore its return value */
2175 s->errorhandler();
2176 return -1;
2177 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002178#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002179 read_buf = cbuf;
2180 remaining = len;
2181 while (remaining != 0) {
2182 unsigned int segment;
2183 int nread = -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002184
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002185 segment = remaining /SEGMENT_SIZE;
2186 if (segment != 0) {
2187 segment = SEGMENT_SIZE;
2188 }
2189 else {
2190 segment = remaining;
2191 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002192
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002193 Py_BEGIN_ALLOW_THREADS
2194 timeout = internal_select(s, 0);
2195 if (!timeout)
2196 nread = recv(s->sock_fd, read_buf, segment, flags);
2197 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002198
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002199 if (timeout == 1) {
2200 PyErr_SetString(socket_timeout, "timed out");
2201 return -1;
2202 }
2203 if (nread < 0) {
2204 s->errorhandler();
2205 return -1;
2206 }
2207 if (nread != remaining) {
2208 read_buf += nread;
2209 break;
2210 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002211
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002212 remaining -= segment;
2213 read_buf += segment;
2214 }
2215 outlen = read_buf - cbuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002216#endif /* !__VMS */
2217
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002218 return outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002219}
2220
Guido van Rossum48a680c2001-03-02 06:34:14 +00002221
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002222/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002223
Guido van Rossum73624e91994-10-10 17:59:00 +00002224static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002225sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002226{
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002227 Py_ssize_t recvlen, outlen;
2228 int flags = 0;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002229 PyObject *buf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002230
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002231 if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002232 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002233
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002234 if (recvlen < 0) {
2235 PyErr_SetString(PyExc_ValueError,
2236 "negative buffersize in recv");
2237 return NULL;
2238 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002239
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002240 /* Allocate a new string. */
2241 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2242 if (buf == NULL)
2243 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002244
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002245 /* Call the guts */
2246 outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags);
2247 if (outlen < 0) {
2248 /* An error occurred, release the string and return an
2249 error. */
2250 Py_DECREF(buf);
2251 return NULL;
2252 }
2253 if (outlen != recvlen) {
2254 /* We did not read as many bytes as we anticipated, resize the
2255 string if possible and be successful. */
2256 _PyBytes_Resize(&buf, outlen);
2257 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002258
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002259 return buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002260}
2261
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002262PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002263"recv(buffersize[, flags]) -> data\n\
2264\n\
2265Receive up to buffersize bytes from the socket. For the optional flags\n\
2266argument, see the Unix manual. When no data is available, block until\n\
2267at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002268the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002269
Guido van Rossum30a685f1991-06-27 15:51:29 +00002270
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002271/* s.recv_into(buffer, [nbytes [,flags]]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002272
Thomas Wouters477c8d52006-05-27 19:21:47 +00002273static PyObject*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002274sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002275{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002276 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00002277
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002278 int flags = 0;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002279 Py_buffer pbuf;
2280 char *buf;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002281 Py_ssize_t buflen, readlen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002282
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002283 /* Get the buffer's memory */
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002284 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002285 &pbuf, &recvlen, &flags))
2286 return NULL;
2287 buf = pbuf.buf;
2288 buflen = pbuf.len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002289
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002290 if (recvlen < 0) {
2291 PyBuffer_Release(&pbuf);
2292 PyErr_SetString(PyExc_ValueError,
2293 "negative buffersize in recv_into");
2294 return NULL;
2295 }
2296 if (recvlen == 0) {
2297 /* If nbytes was not specified, use the buffer's length */
2298 recvlen = buflen;
2299 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002300
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002301 /* Check if the buffer is large enough */
2302 if (buflen < recvlen) {
2303 PyBuffer_Release(&pbuf);
2304 PyErr_SetString(PyExc_ValueError,
2305 "buffer too small for requested bytes");
2306 return NULL;
2307 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002308
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002309 /* Call the guts */
2310 readlen = sock_recv_guts(s, buf, recvlen, flags);
2311 if (readlen < 0) {
2312 /* Return an error. */
2313 PyBuffer_Release(&pbuf);
2314 return NULL;
2315 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002316
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002317 PyBuffer_Release(&pbuf);
2318 /* Return the number of bytes read. Note that we do not do anything
2319 special here in the case that readlen < recvlen. */
2320 return PyLong_FromSsize_t(readlen);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002321}
2322
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002323PyDoc_STRVAR(recv_into_doc,
2324"recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002325\n\
2326A version of recv() that stores its data into a buffer rather than creating \n\
2327a new string. Receive up to buffersize bytes from the socket. If buffersize \n\
2328is not specified (or 0), receive up to the size available in the given buffer.\n\
2329\n\
2330See recv() for documentation about the flags.");
2331
2332
2333/*
Christian Heimes99170a52007-12-19 02:07:34 +00002334 * This is the guts of the recvfrom() and recvfrom_into() methods, which reads
2335 * into a char buffer. If you have any inc/def ref to do to the objects that
2336 * contain the buffer, do it in the caller. This function returns the number
2337 * of bytes succesfully read. If there was an error, it returns -1. Note
2338 * that it is also possible that we return a number of bytes smaller than the
2339 * request bytes.
Thomas Wouters477c8d52006-05-27 19:21:47 +00002340 *
2341 * 'addr' is a return value for the address object. Note that you must decref
2342 * it yourself.
2343 */
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002344static Py_ssize_t
2345sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002346 PyObject** addr)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002347{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002348 sock_addr_t addrbuf;
2349 int timeout;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002350 Py_ssize_t n = -1;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002351 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002352
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002353 *addr = NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002354
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002355 if (!getsockaddrlen(s, &addrlen))
2356 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002357
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002358 if (!IS_SELECTABLE(s)) {
2359 select_error();
2360 return -1;
2361 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002362
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002363 Py_BEGIN_ALLOW_THREADS
2364 memset(&addrbuf, 0, addrlen);
2365 timeout = internal_select(s, 0);
2366 if (!timeout) {
Guido van Rossum8d665e61996-06-26 18:22:49 +00002367#ifndef MS_WINDOWS
Andrew MacIntyreba43e872002-03-03 03:03:52 +00002368#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002369 n = recvfrom(s->sock_fd, cbuf, len, flags,
2370 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum32c575d1997-12-02 20:37:32 +00002371#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002372 n = recvfrom(s->sock_fd, cbuf, len, flags,
2373 (void *) &addrbuf, &addrlen);
Guido van Rossum32c575d1997-12-02 20:37:32 +00002374#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002375#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002376 n = recvfrom(s->sock_fd, cbuf, len, flags,
2377 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002378#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002379 }
2380 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002381
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002382 if (timeout == 1) {
2383 PyErr_SetString(socket_timeout, "timed out");
2384 return -1;
2385 }
2386 if (n < 0) {
2387 s->errorhandler();
2388 return -1;
2389 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002390
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002391 if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
2392 addrlen, s->sock_proto)))
2393 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002394
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002395 return n;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002396}
2397
2398/* s.recvfrom(nbytes [,flags]) method */
2399
2400static PyObject *
2401sock_recvfrom(PySocketSockObject *s, PyObject *args)
2402{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002403 PyObject *buf = NULL;
2404 PyObject *addr = NULL;
2405 PyObject *ret = NULL;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002406 int flags = 0;
2407 Py_ssize_t recvlen, outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002408
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002409 if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002410 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002411
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002412 if (recvlen < 0) {
2413 PyErr_SetString(PyExc_ValueError,
2414 "negative buffersize in recvfrom");
2415 return NULL;
2416 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002417
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002418 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2419 if (buf == NULL)
2420 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002421
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002422 outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf),
2423 recvlen, flags, &addr);
2424 if (outlen < 0) {
2425 goto finally;
2426 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002427
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002428 if (outlen != recvlen) {
2429 /* We did not read as many bytes as we anticipated, resize the
2430 string if possible and be succesful. */
2431 if (_PyBytes_Resize(&buf, outlen) < 0)
2432 /* Oopsy, not so succesful after all. */
2433 goto finally;
2434 }
Barry Warsaw752300b1997-01-03 17:18:10 +00002435
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002436 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002437
2438finally:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002439 Py_XDECREF(buf);
2440 Py_XDECREF(addr);
2441 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002442}
2443
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002444PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002445"recvfrom(buffersize[, flags]) -> (data, address info)\n\
2446\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002447Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002448
Thomas Wouters477c8d52006-05-27 19:21:47 +00002449
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002450/* s.recvfrom_into(buffer[, nbytes [,flags]]) method */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002451
2452static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002453sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002454{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002455 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00002456
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002457 int flags = 0;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002458 Py_buffer pbuf;
2459 char *buf;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002460 Py_ssize_t readlen, buflen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002461
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002462 PyObject *addr = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002463
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002464 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002465 kwlist, &pbuf,
2466 &recvlen, &flags))
2467 return NULL;
2468 buf = pbuf.buf;
2469 buflen = pbuf.len;
2470 assert(buf != 0 && buflen > 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002471
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002472 if (recvlen < 0) {
2473 PyBuffer_Release(&pbuf);
2474 PyErr_SetString(PyExc_ValueError,
2475 "negative buffersize in recvfrom_into");
2476 return NULL;
2477 }
2478 if (recvlen == 0) {
2479 /* If nbytes was not specified, use the buffer's length */
2480 recvlen = buflen;
2481 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002482
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002483 readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
2484 if (readlen < 0) {
2485 PyBuffer_Release(&pbuf);
2486 /* Return an error */
2487 Py_XDECREF(addr);
2488 return NULL;
2489 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002490
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002491 PyBuffer_Release(&pbuf);
2492 /* Return the number of bytes read and the address. Note that we do
2493 not do anything special here in the case that readlen < recvlen. */
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002494 return Py_BuildValue("nN", readlen, addr);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002495}
2496
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002497PyDoc_STRVAR(recvfrom_into_doc,
2498"recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002499\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002500Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002501
2502
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002503/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002504
Guido van Rossum73624e91994-10-10 17:59:00 +00002505static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002506sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002507{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002508 char *buf;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002509 Py_ssize_t len, n = -1;
2510 int flags = 0, timeout;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002511 Py_buffer pbuf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002512
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002513 if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags))
2514 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002515
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002516 if (!IS_SELECTABLE(s)) {
2517 PyBuffer_Release(&pbuf);
2518 return select_error();
2519 }
2520 buf = pbuf.buf;
2521 len = pbuf.len;
Neal Norwitz082b2df2006-02-07 07:04:46 +00002522
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002523 Py_BEGIN_ALLOW_THREADS
2524 timeout = internal_select(s, 1);
2525 if (!timeout)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002526#ifdef __VMS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002527 n = sendsegmented(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002528#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002529 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002530#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002531 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002532
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002533 PyBuffer_Release(&pbuf);
Martin v. Löwis423be952008-08-13 15:53:07 +00002534
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002535 if (timeout == 1) {
2536 PyErr_SetString(socket_timeout, "timed out");
2537 return NULL;
2538 }
2539 if (n < 0)
2540 return s->errorhandler();
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002541 return PyLong_FromSsize_t(n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00002542}
2543
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002544PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002545"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002546\n\
2547Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002548argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002549sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002550
2551
2552/* s.sendall(data [,flags]) method */
2553
2554static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002555sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002556{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002557 char *buf;
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002558 Py_ssize_t len, n = -1;
2559 int flags = 0, timeout;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002560 Py_buffer pbuf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002561
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002562 if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags))
2563 return NULL;
2564 buf = pbuf.buf;
2565 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002566
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002567 if (!IS_SELECTABLE(s)) {
2568 PyBuffer_Release(&pbuf);
2569 return select_error();
2570 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002571
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002572 Py_BEGIN_ALLOW_THREADS
2573 do {
2574 timeout = internal_select(s, 1);
2575 n = -1;
2576 if (timeout)
2577 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002578#ifdef __VMS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002579 n = sendsegmented(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002580#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002581 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002582#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002583 if (n < 0)
2584 break;
2585 buf += n;
2586 len -= n;
2587 } while (len > 0);
2588 Py_END_ALLOW_THREADS
2589 PyBuffer_Release(&pbuf);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002590
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002591 if (timeout == 1) {
2592 PyErr_SetString(socket_timeout, "timed out");
2593 return NULL;
2594 }
2595 if (n < 0)
2596 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00002597
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002598 Py_INCREF(Py_None);
2599 return Py_None;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002600}
2601
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002602PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002603"sendall(data[, flags])\n\
2604\n\
2605Send a data string to the socket. For the optional flags\n\
2606argument, see the Unix manual. This calls send() repeatedly\n\
2607until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002608to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002609
Guido van Rossum30a685f1991-06-27 15:51:29 +00002610
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002611/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002612
Guido van Rossum73624e91994-10-10 17:59:00 +00002613static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002614sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002615{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002616 Py_buffer pbuf;
2617 PyObject *addro;
2618 char *buf;
2619 Py_ssize_t len;
2620 sock_addr_t addrbuf;
2621 int addrlen, n = -1, flags, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002622
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002623 flags = 0;
2624 if (!PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro)) {
2625 PyErr_Clear();
2626 if (!PyArg_ParseTuple(args, "y*iO:sendto",
2627 &pbuf, &flags, &addro))
2628 return NULL;
2629 }
2630 buf = pbuf.buf;
2631 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002632
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002633 if (!IS_SELECTABLE(s)) {
2634 PyBuffer_Release(&pbuf);
2635 return select_error();
2636 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002637
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002638 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) {
2639 PyBuffer_Release(&pbuf);
2640 return NULL;
2641 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002642
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002643 Py_BEGIN_ALLOW_THREADS
2644 timeout = internal_select(s, 1);
2645 if (!timeout)
2646 n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen);
2647 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002648
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002649 PyBuffer_Release(&pbuf);
2650 if (timeout == 1) {
2651 PyErr_SetString(socket_timeout, "timed out");
2652 return NULL;
2653 }
2654 if (n < 0)
2655 return s->errorhandler();
Antoine Pitrouf72006f2010-08-17 19:39:39 +00002656 return PyLong_FromSsize_t(n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002657}
2658
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002659PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002660"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002661\n\
2662Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002663For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002664
Guido van Rossum30a685f1991-06-27 15:51:29 +00002665
2666/* s.shutdown(how) method */
2667
Guido van Rossum73624e91994-10-10 17:59:00 +00002668static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002669sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002670{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002671 int how;
2672 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002673
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002674 how = PyLong_AsLong(arg);
2675 if (how == -1 && PyErr_Occurred())
2676 return NULL;
2677 Py_BEGIN_ALLOW_THREADS
2678 res = shutdown(s->sock_fd, how);
2679 Py_END_ALLOW_THREADS
2680 if (res < 0)
2681 return s->errorhandler();
2682 Py_INCREF(Py_None);
2683 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002684}
2685
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002686PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002687"shutdown(flag)\n\
2688\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00002689Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
2690of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002691
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00002692#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Christian Heimesfaf2f632008-01-06 16:59:19 +00002693static PyObject*
2694sock_ioctl(PySocketSockObject *s, PyObject *arg)
2695{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002696 unsigned long cmd = SIO_RCVALL;
2697 unsigned int option = RCVALL_ON;
2698 DWORD recv;
Christian Heimesfaf2f632008-01-06 16:59:19 +00002699
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002700 if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option))
2701 return NULL;
Christian Heimesfaf2f632008-01-06 16:59:19 +00002702
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002703 if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option),
2704 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
2705 return set_error();
2706 }
2707 return PyLong_FromUnsignedLong(recv);
Christian Heimesfaf2f632008-01-06 16:59:19 +00002708}
2709PyDoc_STRVAR(sock_ioctl_doc,
2710"ioctl(cmd, option) -> long\n\
2711\n\
2712Control the socket with WSAIoctl syscall. Currently only socket.SIO_RCVALL\n\
2713is supported as control. Options must be one of the socket.RCVALL_*\n\
2714constants.");
2715
2716#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00002717
2718/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002719
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002720static PyMethodDef sock_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002721 {"_accept", (PyCFunction)sock_accept, METH_NOARGS,
2722 accept_doc},
2723 {"bind", (PyCFunction)sock_bind, METH_O,
2724 bind_doc},
2725 {"close", (PyCFunction)sock_close, METH_NOARGS,
2726 close_doc},
2727 {"connect", (PyCFunction)sock_connect, METH_O,
2728 connect_doc},
2729 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
2730 connect_ex_doc},
2731 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
2732 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002733#ifdef HAVE_GETPEERNAME
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002734 {"getpeername", (PyCFunction)sock_getpeername,
2735 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00002736#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002737 {"getsockname", (PyCFunction)sock_getsockname,
2738 METH_NOARGS, getsockname_doc},
2739 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
2740 getsockopt_doc},
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00002741#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002742 {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS,
2743 sock_ioctl_doc},
Christian Heimesfaf2f632008-01-06 16:59:19 +00002744#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002745 {"listen", (PyCFunction)sock_listen, METH_O,
2746 listen_doc},
2747 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
2748 recv_doc},
2749 {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
2750 recv_into_doc},
2751 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
2752 recvfrom_doc},
2753 {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
2754 recvfrom_into_doc},
2755 {"send", (PyCFunction)sock_send, METH_VARARGS,
2756 send_doc},
2757 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
2758 sendall_doc},
2759 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
2760 sendto_doc},
2761 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
2762 setblocking_doc},
2763 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
2764 settimeout_doc},
2765 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
2766 gettimeout_doc},
2767 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
2768 setsockopt_doc},
2769 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
2770 shutdown_doc},
2771 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002772};
2773
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002774/* SockObject members */
2775static PyMemberDef sock_memberlist[] = {
2776 {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},
2777 {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},
2778 {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},
2779 {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"},
2780 {0},
2781};
Guido van Rossum30a685f1991-06-27 15:51:29 +00002782
Guido van Rossum73624e91994-10-10 17:59:00 +00002783/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00002784 First close the file description. */
2785
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002786static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002787sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002788{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002789 if (s->sock_fd != -1)
2790 (void) SOCKETCLOSE(s->sock_fd);
2791 Py_TYPE(s)->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002792}
2793
Guido van Rossum30a685f1991-06-27 15:51:29 +00002794
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002795static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002796sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002797{
Fred Drakea04eaad2000-06-30 02:46:07 +00002798#if SIZEOF_SOCKET_T > SIZEOF_LONG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002799 if (s->sock_fd > LONG_MAX) {
2800 /* this can occur on Win64, and actually there is a special
2801 ugly printf formatter for decimal pointer length integer
2802 printing, only bother if necessary*/
2803 PyErr_SetString(PyExc_OverflowError,
2804 "no printf formatter to display "
2805 "the socket descriptor in decimal");
2806 return NULL;
2807 }
Fred Drakea04eaad2000-06-30 02:46:07 +00002808#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002809 return PyUnicode_FromFormat(
2810 "<socket object, fd=%ld, family=%d, type=%d, proto=%d>",
2811 (long)s->sock_fd, s->sock_family,
2812 s->sock_type,
2813 s->sock_proto);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002814}
2815
2816
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002817/* Create a new, uninitialized socket object. */
2818
2819static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002820sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002821{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002822 PyObject *new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002823
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002824 new = type->tp_alloc(type, 0);
2825 if (new != NULL) {
2826 ((PySocketSockObject *)new)->sock_fd = -1;
2827 ((PySocketSockObject *)new)->sock_timeout = -1.0;
2828 ((PySocketSockObject *)new)->errorhandler = &set_error;
2829 }
2830 return new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002831}
2832
2833
2834/* Initialize a new socket object. */
2835
2836/*ARGSUSED*/
2837static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002838sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002839{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002840 PySocketSockObject *s = (PySocketSockObject *)self;
2841 PyObject *fdobj = NULL;
2842 SOCKET_T fd = INVALID_SOCKET;
2843 int family = AF_INET, type = SOCK_STREAM, proto = 0;
2844 static char *keywords[] = {"family", "type", "proto", "fileno", 0};
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002845
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002846 if (!PyArg_ParseTupleAndKeywords(args, kwds,
2847 "|iiiO:socket", keywords,
2848 &family, &type, &proto, &fdobj))
2849 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002850
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002851 if (fdobj != NULL && fdobj != Py_None) {
2852 fd = PyLong_AsSocket_t(fdobj);
2853 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
2854 return -1;
2855 if (fd == INVALID_SOCKET) {
2856 PyErr_SetString(PyExc_ValueError,
2857 "can't use invalid socket value");
2858 return -1;
2859 }
2860 }
2861 else {
2862 Py_BEGIN_ALLOW_THREADS
2863 fd = socket(family, type, proto);
2864 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002865
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002866 if (fd == INVALID_SOCKET) {
2867 set_error();
2868 return -1;
2869 }
2870 }
2871 init_sockobject(s, fd, family, type, proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002872
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002873 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002874
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002875}
2876
2877
Guido van Rossumb6775db1994-08-01 11:34:53 +00002878/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002879
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002880static PyTypeObject sock_type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002881 PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
2882 "_socket.socket", /* tp_name */
2883 sizeof(PySocketSockObject), /* tp_basicsize */
2884 0, /* tp_itemsize */
2885 (destructor)sock_dealloc, /* tp_dealloc */
2886 0, /* tp_print */
2887 0, /* tp_getattr */
2888 0, /* tp_setattr */
2889 0, /* tp_reserved */
2890 (reprfunc)sock_repr, /* tp_repr */
2891 0, /* tp_as_number */
2892 0, /* tp_as_sequence */
2893 0, /* tp_as_mapping */
2894 0, /* tp_hash */
2895 0, /* tp_call */
2896 0, /* tp_str */
2897 PyObject_GenericGetAttr, /* tp_getattro */
2898 0, /* tp_setattro */
2899 0, /* tp_as_buffer */
2900 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2901 sock_doc, /* tp_doc */
2902 0, /* tp_traverse */
2903 0, /* tp_clear */
2904 0, /* tp_richcompare */
2905 0, /* tp_weaklistoffset */
2906 0, /* tp_iter */
2907 0, /* tp_iternext */
2908 sock_methods, /* tp_methods */
2909 sock_memberlist, /* tp_members */
2910 0, /* tp_getset */
2911 0, /* tp_base */
2912 0, /* tp_dict */
2913 0, /* tp_descr_get */
2914 0, /* tp_descr_set */
2915 0, /* tp_dictoffset */
2916 sock_initobj, /* tp_init */
2917 PyType_GenericAlloc, /* tp_alloc */
2918 sock_new, /* tp_new */
2919 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002920};
2921
Guido van Rossum30a685f1991-06-27 15:51:29 +00002922
Guido van Rossum81194471991-07-27 21:42:02 +00002923/* Python interface to gethostname(). */
2924
2925/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002926static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002927socket_gethostname(PyObject *self, PyObject *unused)
Guido van Rossum81194471991-07-27 21:42:02 +00002928{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002929 char buf[1024];
2930 int res;
2931 Py_BEGIN_ALLOW_THREADS
2932 res = gethostname(buf, (int) sizeof buf - 1);
2933 Py_END_ALLOW_THREADS
2934 if (res < 0)
2935 return set_error();
2936 buf[sizeof buf - 1] = '\0';
2937 return PyUnicode_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00002938}
Guido van Rossumff4949e1992-08-05 19:58:53 +00002939
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002940PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002941"gethostname() -> string\n\
2942\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002943Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002944
Guido van Rossumff4949e1992-08-05 19:58:53 +00002945
Guido van Rossum30a685f1991-06-27 15:51:29 +00002946/* Python interface to gethostbyname(name). */
2947
2948/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002949static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002950socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002951{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002952 char *name;
2953 sock_addr_t addrbuf;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002954
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002955 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
2956 return NULL;
2957 if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)
2958 return NULL;
2959 return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002960}
2961
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002962PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002963"gethostbyname(host) -> address\n\
2964\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002965Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002966
2967
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002968/* Convenience function common to gethostbyname_ex and gethostbyaddr */
2969
2970static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002971gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002972{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002973 char **pch;
2974 PyObject *rtn_tuple = (PyObject *)NULL;
2975 PyObject *name_list = (PyObject *)NULL;
2976 PyObject *addr_list = (PyObject *)NULL;
2977 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002978
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002979 if (h == NULL) {
2980 /* Let's get real error message to return */
2981 set_herror(h_errno);
2982 return NULL;
2983 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002984
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002985 if (h->h_addrtype != af) {
2986 /* Let's get real error message to return */
2987 PyErr_SetString(socket_error,
2988 (char *)strerror(EAFNOSUPPORT));
Christian Heimesada8c3b2008-03-18 18:26:33 +00002989
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002990 return NULL;
2991 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002992
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002993 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002994
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002995 case AF_INET:
2996 if (alen < sizeof(struct sockaddr_in))
2997 return NULL;
2998 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002999
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003000#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003001 case AF_INET6:
3002 if (alen < sizeof(struct sockaddr_in6))
3003 return NULL;
3004 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003005#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00003006
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003007 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003008
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003009 if ((name_list = PyList_New(0)) == NULL)
3010 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003011
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003012 if ((addr_list = PyList_New(0)) == NULL)
3013 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003014
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003015 /* SF #1511317: h_aliases can be NULL */
3016 if (h->h_aliases) {
3017 for (pch = h->h_aliases; *pch != NULL; pch++) {
3018 int status;
3019 tmp = PyUnicode_FromString(*pch);
3020 if (tmp == NULL)
3021 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003022
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003023 status = PyList_Append(name_list, tmp);
3024 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003025
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003026 if (status)
3027 goto err;
3028 }
3029 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003030
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003031 for (pch = h->h_addr_list; *pch != NULL; pch++) {
3032 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003033
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003034 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00003035
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003036 case AF_INET:
3037 {
3038 struct sockaddr_in sin;
3039 memset(&sin, 0, sizeof(sin));
3040 sin.sin_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003041#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003042 sin.sin_len = sizeof(sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003043#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003044 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
3045 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00003046
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003047 if (pch == h->h_addr_list && alen >= sizeof(sin))
3048 memcpy((char *) addr, &sin, sizeof(sin));
3049 break;
3050 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003051
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003052#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003053 case AF_INET6:
3054 {
3055 struct sockaddr_in6 sin6;
3056 memset(&sin6, 0, sizeof(sin6));
3057 sin6.sin6_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003058#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003059 sin6.sin6_len = sizeof(sin6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003060#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003061 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
3062 tmp = makeipaddr((struct sockaddr *)&sin6,
3063 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00003064
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003065 if (pch == h->h_addr_list && alen >= sizeof(sin6))
3066 memcpy((char *) addr, &sin6, sizeof(sin6));
3067 break;
3068 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003069#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00003070
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003071 default: /* can't happen */
3072 PyErr_SetString(socket_error,
3073 "unsupported address family");
3074 return NULL;
3075 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003076
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003077 if (tmp == NULL)
3078 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003079
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003080 status = PyList_Append(addr_list, tmp);
3081 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003082
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003083 if (status)
3084 goto err;
3085 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003086
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003087 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003088
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003089 err:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003090 Py_XDECREF(name_list);
3091 Py_XDECREF(addr_list);
3092 return rtn_tuple;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003093}
3094
3095
3096/* Python interface to gethostbyname_ex(name). */
3097
3098/*ARGSUSED*/
3099static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003100socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003101{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003102 char *name;
3103 struct hostent *h;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00003104#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003105 struct sockaddr_storage addr;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00003106#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003107 struct sockaddr_in addr;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00003108#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003109 struct sockaddr *sa;
3110 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003111#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003112 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003113#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003114 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003115#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003116 char buf[16384];
3117 int buf_len = (sizeof buf) - 1;
3118 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003119#endif
3120#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003121 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003122#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003123#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003124
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003125 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
3126 return NULL;
3127 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
3128 return NULL;
3129 Py_BEGIN_ALLOW_THREADS
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003130#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003131#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003132 result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
3133 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003134#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003135 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003136#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003137 memset((void *) &data, '\0', sizeof(data));
3138 result = gethostbyname_r(name, &hp_allocated, &data);
3139 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003140#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003141#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003142#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003143 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003144#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003145 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003146#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003147 Py_END_ALLOW_THREADS
3148 /* Some C libraries would require addr.__ss_family instead of
3149 addr.ss_family.
3150 Therefore, we cast the sockaddr_storage into sockaddr to
3151 access sa_family. */
3152 sa = (struct sockaddr*)&addr;
3153 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
3154 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003155#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003156 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00003157#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003158 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003159}
3160
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003161PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003162"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
3163\n\
3164Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003165for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003166
3167
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003168/* Python interface to gethostbyaddr(IP). */
3169
3170/*ARGSUSED*/
3171static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003172socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003173{
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003174#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003175 struct sockaddr_storage addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003176#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003177 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003178#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003179 struct sockaddr *sa = (struct sockaddr *)&addr;
3180 char *ip_num;
3181 struct hostent *h;
3182 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003183#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003184 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003185#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003186 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003187#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003188 /* glibcs up to 2.10 assume that the buf argument to
3189 gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc
3190 does not ensure. The attribute below instructs the compiler
3191 to maintain this alignment. */
3192 char buf[16384] Py_ALIGNED(8);
3193 int buf_len = (sizeof buf) - 1;
3194 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003195#endif
3196#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003197 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003198#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003199#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003200 char *ap;
3201 int al;
3202 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003203
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003204 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
3205 return NULL;
3206 af = AF_UNSPEC;
3207 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
3208 return NULL;
3209 af = sa->sa_family;
3210 ap = NULL;
3211 al = 0;
3212 switch (af) {
3213 case AF_INET:
3214 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
3215 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3216 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003217#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003218 case AF_INET6:
3219 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
3220 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3221 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003222#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003223 default:
3224 PyErr_SetString(socket_error, "unsupported address family");
3225 return NULL;
3226 }
3227 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003228#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003229#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003230 result = gethostbyaddr_r(ap, al, af,
3231 &hp_allocated, buf, buf_len,
3232 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003233#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003234 h = gethostbyaddr_r(ap, al, af,
3235 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003236#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003237 memset((void *) &data, '\0', sizeof(data));
3238 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
3239 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003240#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003241#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003242#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003243 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003244#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003245 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003246#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003247 Py_END_ALLOW_THREADS
3248 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003249#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003250 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003251#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003252 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003253}
3254
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003255PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003256"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
3257\n\
3258Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003259for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003260
Guido van Rossum30a685f1991-06-27 15:51:29 +00003261
3262/* Python interface to getservbyname(name).
3263 This only returns the port number, since the other info is already
3264 known or not useful (like the list of aliases). */
3265
3266/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00003267static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003268socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00003269{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003270 char *name, *proto=NULL;
3271 struct servent *sp;
3272 if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
3273 return NULL;
3274 Py_BEGIN_ALLOW_THREADS
3275 sp = getservbyname(name, proto);
3276 Py_END_ALLOW_THREADS
3277 if (sp == NULL) {
3278 PyErr_SetString(socket_error, "service/proto not found");
3279 return NULL;
3280 }
3281 return PyLong_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00003282}
3283
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003284PyDoc_STRVAR(getservbyname_doc,
Barry Warsaw11b91a02004-06-28 00:50:43 +00003285"getservbyname(servicename[, protocolname]) -> integer\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003286\n\
3287Return a port number from a service name and protocol name.\n\
Barry Warsaw11b91a02004-06-28 00:50:43 +00003288The optional protocol name, if given, should be 'tcp' or 'udp',\n\
3289otherwise any protocol will match.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003290
Guido van Rossum30a685f1991-06-27 15:51:29 +00003291
Barry Warsaw11b91a02004-06-28 00:50:43 +00003292/* Python interface to getservbyport(port).
3293 This only returns the service name, since the other info is already
3294 known or not useful (like the list of aliases). */
3295
3296/*ARGSUSED*/
3297static PyObject *
3298socket_getservbyport(PyObject *self, PyObject *args)
3299{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003300 int port;
3301 char *proto=NULL;
3302 struct servent *sp;
3303 if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto))
3304 return NULL;
3305 if (port < 0 || port > 0xffff) {
3306 PyErr_SetString(
3307 PyExc_OverflowError,
3308 "getservbyport: port must be 0-65535.");
3309 return NULL;
3310 }
3311 Py_BEGIN_ALLOW_THREADS
3312 sp = getservbyport(htons((short)port), proto);
3313 Py_END_ALLOW_THREADS
3314 if (sp == NULL) {
3315 PyErr_SetString(socket_error, "port/proto not found");
3316 return NULL;
3317 }
3318 return PyUnicode_FromString(sp->s_name);
Barry Warsaw11b91a02004-06-28 00:50:43 +00003319}
3320
3321PyDoc_STRVAR(getservbyport_doc,
3322"getservbyport(port[, protocolname]) -> string\n\
3323\n\
3324Return the service name from a port number and protocol name.\n\
3325The optional protocol name, if given, should be 'tcp' or 'udp',\n\
3326otherwise any protocol will match.");
3327
Guido van Rossum3901d851996-12-19 16:35:04 +00003328/* Python interface to getprotobyname(name).
3329 This only returns the protocol number, since the other info is
3330 already known or not useful (like the list of aliases). */
3331
3332/*ARGSUSED*/
3333static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003334socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00003335{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003336 char *name;
3337 struct protoent *sp;
3338 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
3339 return NULL;
3340 Py_BEGIN_ALLOW_THREADS
3341 sp = getprotobyname(name);
3342 Py_END_ALLOW_THREADS
3343 if (sp == NULL) {
3344 PyErr_SetString(socket_error, "protocol not found");
3345 return NULL;
3346 }
3347 return PyLong_FromLong((long) sp->p_proto);
Guido van Rossum3901d851996-12-19 16:35:04 +00003348}
3349
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003350PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003351"getprotobyname(name) -> integer\n\
3352\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003353Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003354
Guido van Rossum3901d851996-12-19 16:35:04 +00003355
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003356#ifndef NO_DUP
3357/* dup() function for socket fds */
3358
3359static PyObject *
3360socket_dup(PyObject *self, PyObject *fdobj)
3361{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003362 SOCKET_T fd, newfd;
3363 PyObject *newfdobj;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003364
3365
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003366 fd = PyLong_AsSocket_t(fdobj);
3367 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
3368 return NULL;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003369
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003370 newfd = dup_socket(fd);
3371 if (newfd == INVALID_SOCKET)
3372 return set_error();
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003373
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003374 newfdobj = PyLong_FromSocket_t(newfd);
3375 if (newfdobj == NULL)
3376 SOCKETCLOSE(newfd);
3377 return newfdobj;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003378}
3379
3380PyDoc_STRVAR(dup_doc,
3381"dup(integer) -> integer\n\
3382\n\
3383Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\
3384sockets; on some platforms os.dup() won't work for socket file descriptors.");
3385#endif
3386
3387
Dave Cole331708b2004-08-09 04:51:41 +00003388#ifdef HAVE_SOCKETPAIR
3389/* Create a pair of sockets using the socketpair() function.
Dave Cole07fda7e2004-08-23 05:16:23 +00003390 Arguments as for socket() except the default family is AF_UNIX if
Dave Colee8bbfe42004-08-26 00:51:16 +00003391 defined on the platform; otherwise, the default is AF_INET. */
Dave Cole331708b2004-08-09 04:51:41 +00003392
3393/*ARGSUSED*/
3394static PyObject *
3395socket_socketpair(PyObject *self, PyObject *args)
3396{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003397 PySocketSockObject *s0 = NULL, *s1 = NULL;
3398 SOCKET_T sv[2];
3399 int family, type = SOCK_STREAM, proto = 0;
3400 PyObject *res = NULL;
Dave Cole331708b2004-08-09 04:51:41 +00003401
3402#if defined(AF_UNIX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003403 family = AF_UNIX;
Dave Cole331708b2004-08-09 04:51:41 +00003404#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003405 family = AF_INET;
Dave Cole331708b2004-08-09 04:51:41 +00003406#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003407 if (!PyArg_ParseTuple(args, "|iii:socketpair",
3408 &family, &type, &proto))
3409 return NULL;
3410 /* Create a pair of socket fds */
3411 if (socketpair(family, type, proto, sv) < 0)
3412 return set_error();
3413 s0 = new_sockobject(sv[0], family, type, proto);
3414 if (s0 == NULL)
3415 goto finally;
3416 s1 = new_sockobject(sv[1], family, type, proto);
3417 if (s1 == NULL)
3418 goto finally;
3419 res = PyTuple_Pack(2, s0, s1);
Dave Cole331708b2004-08-09 04:51:41 +00003420
3421finally:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003422 if (res == NULL) {
3423 if (s0 == NULL)
3424 SOCKETCLOSE(sv[0]);
3425 if (s1 == NULL)
3426 SOCKETCLOSE(sv[1]);
3427 }
3428 Py_XDECREF(s0);
3429 Py_XDECREF(s1);
3430 return res;
Dave Cole331708b2004-08-09 04:51:41 +00003431}
3432
3433PyDoc_STRVAR(socketpair_doc,
3434"socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\
3435\n\
3436Create a pair of socket objects from the sockets returned by the platform\n\
3437socketpair() function.\n\
Dave Cole07fda7e2004-08-23 05:16:23 +00003438The arguments are the same as for socket() except the default family is\n\
Dave Colee8bbfe42004-08-26 00:51:16 +00003439AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
Dave Cole331708b2004-08-09 04:51:41 +00003440
3441#endif /* HAVE_SOCKETPAIR */
3442
3443
Guido van Rossum006bf911996-06-12 04:04:55 +00003444static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003445socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003446{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003447 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00003448
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003449 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
3450 return NULL;
3451 }
3452 if (x1 < 0) {
3453 PyErr_SetString(PyExc_OverflowError,
3454 "can't convert negative number to unsigned long");
3455 return NULL;
3456 }
3457 x2 = (unsigned int)ntohs((unsigned short)x1);
3458 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00003459}
3460
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003461PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003462"ntohs(integer) -> integer\n\
3463\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003464Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003465
3466
Guido van Rossum006bf911996-06-12 04:04:55 +00003467static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003468socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003469{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003470 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003471
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003472 if (PyLong_Check(arg)) {
3473 x = PyLong_AsUnsignedLong(arg);
3474 if (x == (unsigned long) -1 && PyErr_Occurred())
3475 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003476#if SIZEOF_LONG > 4
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003477 {
3478 unsigned long y;
3479 /* only want the trailing 32 bits */
3480 y = x & 0xFFFFFFFFUL;
3481 if (y ^ x)
3482 return PyErr_Format(PyExc_OverflowError,
3483 "long int larger than 32 bits");
3484 x = y;
3485 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003486#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003487 }
3488 else
3489 return PyErr_Format(PyExc_TypeError,
3490 "expected int/long, %s found",
3491 Py_TYPE(arg)->tp_name);
3492 if (x == (unsigned long) -1 && PyErr_Occurred())
3493 return NULL;
3494 return PyLong_FromUnsignedLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003495}
3496
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003497PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003498"ntohl(integer) -> integer\n\
3499\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003500Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003501
3502
Guido van Rossum006bf911996-06-12 04:04:55 +00003503static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003504socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003505{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003506 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00003507
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003508 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
3509 return NULL;
3510 }
3511 if (x1 < 0) {
3512 PyErr_SetString(PyExc_OverflowError,
3513 "can't convert negative number to unsigned long");
3514 return NULL;
3515 }
3516 x2 = (unsigned int)htons((unsigned short)x1);
3517 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00003518}
3519
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003520PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003521"htons(integer) -> integer\n\
3522\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003523Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003524
3525
Guido van Rossum006bf911996-06-12 04:04:55 +00003526static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003527socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003528{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003529 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003530
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003531 if (PyLong_Check(arg)) {
3532 x = PyLong_AsUnsignedLong(arg);
3533 if (x == (unsigned long) -1 && PyErr_Occurred())
3534 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003535#if SIZEOF_LONG > 4
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003536 {
3537 unsigned long y;
3538 /* only want the trailing 32 bits */
3539 y = x & 0xFFFFFFFFUL;
3540 if (y ^ x)
3541 return PyErr_Format(PyExc_OverflowError,
3542 "long int larger than 32 bits");
3543 x = y;
3544 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003545#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003546 }
3547 else
3548 return PyErr_Format(PyExc_TypeError,
3549 "expected int/long, %s found",
3550 Py_TYPE(arg)->tp_name);
3551 return PyLong_FromUnsignedLong(htonl((unsigned long)x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003552}
3553
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003554PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003555"htonl(integer) -> integer\n\
3556\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003557Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003558
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003559/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003560
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003561PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum7d0a8262007-05-21 23:13:11 +00003562"inet_aton(string) -> bytes giving packed 32-bit IP representation\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003563\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003564Convert 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 +00003565binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003566
3567static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003568socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003569{
Guido van Rossuma2e48551999-09-09 15:42:59 +00003570#ifndef INADDR_NONE
3571#define INADDR_NONE (-1)
3572#endif
Neal Norwitz88f115b2003-02-13 02:15:42 +00003573#ifdef HAVE_INET_ATON
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003574 struct in_addr buf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003575#endif
3576
3577#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
Benjamin Petersonf91df042009-02-13 02:50:59 +00003578#if (SIZEOF_INT != 4)
3579#error "Not sure if in_addr_t exists and int is not 32-bits."
3580#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003581 /* Have to use inet_addr() instead */
3582 unsigned int packed_addr;
Tim Peters1df9fdd2003-02-13 03:13:40 +00003583#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003584 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003585
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003586 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
3587 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003588
Tim Peters1df9fdd2003-02-13 03:13:40 +00003589
3590#ifdef HAVE_INET_ATON
Thomas Wouters477c8d52006-05-27 19:21:47 +00003591
3592#ifdef USE_INET_ATON_WEAKLINK
3593 if (inet_aton != NULL) {
3594#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003595 if (inet_aton(ip_addr, &buf))
3596 return PyBytes_FromStringAndSize((char *)(&buf),
3597 sizeof(buf));
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003598
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003599 PyErr_SetString(socket_error,
3600 "illegal IP address string passed to inet_aton");
3601 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003602
Thomas Wouters477c8d52006-05-27 19:21:47 +00003603#ifdef USE_INET_ATON_WEAKLINK
3604 } else {
3605#endif
3606
3607#endif
3608
3609#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
3610
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003611 /* special-case this address as inet_addr might return INADDR_NONE
3612 * for this */
3613 if (strcmp(ip_addr, "255.255.255.255") == 0) {
3614 packed_addr = 0xFFFFFFFF;
3615 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00003616
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003617 packed_addr = inet_addr(ip_addr);
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003618
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003619 if (packed_addr == INADDR_NONE) { /* invalid address */
3620 PyErr_SetString(socket_error,
3621 "illegal IP address string passed to inet_aton");
3622 return NULL;
3623 }
3624 }
3625 return PyBytes_FromStringAndSize((char *) &packed_addr,
3626 sizeof(packed_addr));
Thomas Wouters477c8d52006-05-27 19:21:47 +00003627
3628#ifdef USE_INET_ATON_WEAKLINK
3629 }
3630#endif
3631
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003632#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003633}
3634
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003635PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00003636"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003637\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003638Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003639
3640static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003641socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003642{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003643 char *packed_str;
3644 int addr_len;
3645 struct in_addr packed_addr;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003646
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003647 if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) {
3648 return NULL;
3649 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00003650
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003651 if (addr_len != sizeof(packed_addr)) {
3652 PyErr_SetString(socket_error,
3653 "packed IP wrong length for inet_ntoa");
3654 return NULL;
3655 }
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003656
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003657 memcpy(&packed_addr, packed_str, addr_len);
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003658
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003659 return PyUnicode_FromString(inet_ntoa(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003660}
Guido van Rossum82a5c661998-07-07 20:45:43 +00003661
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003662#ifdef HAVE_INET_PTON
3663
3664PyDoc_STRVAR(inet_pton_doc,
3665"inet_pton(af, ip) -> packed IP address string\n\
3666\n\
3667Convert an IP address from string format to a packed string suitable\n\
3668for use with low-level network functions.");
3669
3670static PyObject *
3671socket_inet_pton(PyObject *self, PyObject *args)
3672{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003673 int af;
3674 char* ip;
3675 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003676#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003677 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003678#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003679 char packed[sizeof(struct in_addr)];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003680#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003681 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
3682 return NULL;
3683 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003684
Martin v. Löwis04697e82004-06-02 12:35:29 +00003685#if !defined(ENABLE_IPV6) && defined(AF_INET6)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003686 if(af == AF_INET6) {
3687 PyErr_SetString(socket_error,
3688 "can't use AF_INET6, IPv6 is disabled");
3689 return NULL;
3690 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003691#endif
Martin v. Löwis10649092003-08-05 06:25:06 +00003692
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003693 retval = inet_pton(af, ip, packed);
3694 if (retval < 0) {
3695 PyErr_SetFromErrno(socket_error);
3696 return NULL;
3697 } else if (retval == 0) {
3698 PyErr_SetString(socket_error,
3699 "illegal IP address string passed to inet_pton");
3700 return NULL;
3701 } else if (af == AF_INET) {
3702 return PyBytes_FromStringAndSize(packed,
3703 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003704#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003705 } else if (af == AF_INET6) {
3706 return PyBytes_FromStringAndSize(packed,
3707 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003708#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003709 } else {
3710 PyErr_SetString(socket_error, "unknown address family");
3711 return NULL;
3712 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003713}
Thomas Wouters477c8d52006-05-27 19:21:47 +00003714
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003715PyDoc_STRVAR(inet_ntop_doc,
3716"inet_ntop(af, packed_ip) -> string formatted IP address\n\
3717\n\
3718Convert a packed IP address of the given family to string format.");
3719
3720static PyObject *
3721socket_inet_ntop(PyObject *self, PyObject *args)
3722{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003723 int af;
3724 char* packed;
3725 int len;
3726 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003727#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003728 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003729#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003730 char ip[INET_ADDRSTRLEN + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003731#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00003732
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003733 /* Guarantee NUL-termination for PyUnicode_FromString() below */
3734 memset((void *) &ip[0], '\0', sizeof(ip));
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003735
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003736 if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) {
3737 return NULL;
3738 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003739
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003740 if (af == AF_INET) {
3741 if (len != sizeof(struct in_addr)) {
3742 PyErr_SetString(PyExc_ValueError,
3743 "invalid length of packed IP address string");
3744 return NULL;
3745 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003746#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003747 } else if (af == AF_INET6) {
3748 if (len != sizeof(struct in6_addr)) {
3749 PyErr_SetString(PyExc_ValueError,
3750 "invalid length of packed IP address string");
3751 return NULL;
3752 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003753#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003754 } else {
3755 PyErr_Format(PyExc_ValueError,
3756 "unknown address family %d", af);
3757 return NULL;
3758 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003759
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003760 retval = inet_ntop(af, packed, ip, sizeof(ip));
3761 if (!retval) {
3762 PyErr_SetFromErrno(socket_error);
3763 return NULL;
3764 } else {
3765 return PyUnicode_FromString(retval);
3766 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003767
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003768 /* NOTREACHED */
3769 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
3770 return NULL;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003771}
3772
3773#endif /* HAVE_INET_PTON */
3774
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003775/* Python interface to getaddrinfo(host, port). */
3776
3777/*ARGSUSED*/
3778static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003779socket_getaddrinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003780{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003781 struct addrinfo hints, *res;
3782 struct addrinfo *res0 = NULL;
3783 PyObject *hobj = NULL;
3784 PyObject *pobj = (PyObject *)NULL;
3785 char pbuf[30];
3786 char *hptr, *pptr;
3787 int family, socktype, protocol, flags;
3788 int error;
3789 PyObject *all = (PyObject *)NULL;
3790 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003791
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003792 family = socktype = protocol = flags = 0;
3793 family = AF_UNSPEC;
3794 if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
3795 &hobj, &pobj, &family, &socktype,
3796 &protocol, &flags)) {
3797 return NULL;
3798 }
3799 if (hobj == Py_None) {
3800 hptr = NULL;
3801 } else if (PyUnicode_Check(hobj)) {
3802 idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
3803 if (!idna)
3804 return NULL;
3805 assert(PyBytes_Check(idna));
3806 hptr = PyBytes_AS_STRING(idna);
3807 } else if (PyBytes_Check(hobj)) {
3808 hptr = PyBytes_AsString(hobj);
3809 } else {
3810 PyErr_SetString(PyExc_TypeError,
3811 "getaddrinfo() argument 1 must be string or None");
3812 return NULL;
3813 }
3814 if (PyLong_CheckExact(pobj)) {
3815 long value = PyLong_AsLong(pobj);
3816 if (value == -1 && PyErr_Occurred())
3817 goto err;
3818 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value);
3819 pptr = pbuf;
3820 } else if (PyUnicode_Check(pobj)) {
3821 pptr = _PyUnicode_AsString(pobj);
3822 } else if (PyBytes_Check(pobj)) {
3823 pptr = PyBytes_AsString(pobj);
3824 } else if (pobj == Py_None) {
3825 pptr = (char *)NULL;
3826 } else {
3827 PyErr_SetString(socket_error, "Int or String expected");
3828 goto err;
3829 }
3830 memset(&hints, 0, sizeof(hints));
3831 hints.ai_family = family;
3832 hints.ai_socktype = socktype;
3833 hints.ai_protocol = protocol;
3834 hints.ai_flags = flags;
3835 Py_BEGIN_ALLOW_THREADS
3836 ACQUIRE_GETADDRINFO_LOCK
3837 error = getaddrinfo(hptr, pptr, &hints, &res0);
3838 Py_END_ALLOW_THREADS
3839 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
3840 if (error) {
3841 set_gaierror(error);
3842 goto err;
3843 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003844
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003845 if ((all = PyList_New(0)) == NULL)
3846 goto err;
3847 for (res = res0; res; res = res->ai_next) {
3848 PyObject *single;
3849 PyObject *addr =
3850 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
3851 if (addr == NULL)
3852 goto err;
3853 single = Py_BuildValue("iiisO", res->ai_family,
3854 res->ai_socktype, res->ai_protocol,
3855 res->ai_canonname ? res->ai_canonname : "",
3856 addr);
3857 Py_DECREF(addr);
3858 if (single == NULL)
3859 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003860
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003861 if (PyList_Append(all, single))
3862 goto err;
3863 Py_XDECREF(single);
3864 }
3865 Py_XDECREF(idna);
3866 if (res0)
3867 freeaddrinfo(res0);
3868 return all;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003869 err:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003870 Py_XDECREF(all);
3871 Py_XDECREF(idna);
3872 if (res0)
3873 freeaddrinfo(res0);
3874 return (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003875}
3876
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003877PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003878"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
3879 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003880\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003881Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003882
3883/* Python interface to getnameinfo(sa, flags). */
3884
3885/*ARGSUSED*/
3886static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003887socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003888{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003889 PyObject *sa = (PyObject *)NULL;
3890 int flags;
3891 char *hostp;
3892 int port, flowinfo, scope_id;
3893 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
3894 struct addrinfo hints, *res = NULL;
3895 int error;
3896 PyObject *ret = (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003897
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003898 flags = flowinfo = scope_id = 0;
3899 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
3900 return NULL;
3901 if (!PyTuple_Check(sa)) {
3902 PyErr_SetString(PyExc_TypeError,
3903 "getnameinfo() argument 1 must be a tuple");
3904 return NULL;
3905 }
3906 if (!PyArg_ParseTuple(sa, "si|ii",
3907 &hostp, &port, &flowinfo, &scope_id))
3908 return NULL;
3909 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
3910 memset(&hints, 0, sizeof(hints));
3911 hints.ai_family = AF_UNSPEC;
3912 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
3913 Py_BEGIN_ALLOW_THREADS
3914 ACQUIRE_GETADDRINFO_LOCK
3915 error = getaddrinfo(hostp, pbuf, &hints, &res);
3916 Py_END_ALLOW_THREADS
3917 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
3918 if (error) {
3919 set_gaierror(error);
3920 goto fail;
3921 }
3922 if (res->ai_next) {
3923 PyErr_SetString(socket_error,
3924 "sockaddr resolved to multiple addresses");
3925 goto fail;
3926 }
3927 switch (res->ai_family) {
3928 case AF_INET:
3929 {
3930 if (PyTuple_GET_SIZE(sa) != 2) {
3931 PyErr_SetString(socket_error,
3932 "IPv4 sockaddr must be 2 tuple");
3933 goto fail;
3934 }
3935 break;
3936 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003937#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003938 case AF_INET6:
3939 {
3940 struct sockaddr_in6 *sin6;
3941 sin6 = (struct sockaddr_in6 *)res->ai_addr;
3942 sin6->sin6_flowinfo = flowinfo;
3943 sin6->sin6_scope_id = scope_id;
3944 break;
3945 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003946#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003947 }
Antoine Pitrouf72006f2010-08-17 19:39:39 +00003948 error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003949 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
3950 if (error) {
3951 set_gaierror(error);
3952 goto fail;
3953 }
3954 ret = Py_BuildValue("ss", hbuf, pbuf);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003955
3956fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003957 if (res)
3958 freeaddrinfo(res);
3959 return ret;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003960}
3961
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003962PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003963"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003964\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003965Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003966
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003967
3968/* Python API to getting and setting the default timeout value. */
3969
3970static PyObject *
3971socket_getdefaulttimeout(PyObject *self)
3972{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003973 if (defaulttimeout < 0.0) {
3974 Py_INCREF(Py_None);
3975 return Py_None;
3976 }
3977 else
3978 return PyFloat_FromDouble(defaulttimeout);
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003979}
3980
3981PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003982"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003983\n\
3984Returns the default timeout in floating seconds for new socket objects.\n\
3985A value of None indicates that new socket objects have no timeout.\n\
3986When the socket module is first imported, the default is None.");
3987
3988static PyObject *
3989socket_setdefaulttimeout(PyObject *self, PyObject *arg)
3990{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003991 double timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003992
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003993 if (arg == Py_None)
3994 timeout = -1.0;
3995 else {
3996 timeout = PyFloat_AsDouble(arg);
3997 if (timeout < 0.0) {
3998 if (!PyErr_Occurred())
3999 PyErr_SetString(PyExc_ValueError,
4000 "Timeout value out of range");
4001 return NULL;
4002 }
4003 }
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004004
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004005 defaulttimeout = timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004006
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004007 Py_INCREF(Py_None);
4008 return Py_None;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004009}
4010
4011PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004012"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004013\n\
4014Set the default timeout in floating seconds for new socket objects.\n\
4015A value of None indicates that new socket objects have no timeout.\n\
4016When the socket module is first imported, the default is None.");
4017
4018
Guido van Rossum30a685f1991-06-27 15:51:29 +00004019/* List of functions exported by this module. */
4020
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004021static PyMethodDef socket_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004022 {"gethostbyname", socket_gethostbyname,
4023 METH_VARARGS, gethostbyname_doc},
4024 {"gethostbyname_ex", socket_gethostbyname_ex,
4025 METH_VARARGS, ghbn_ex_doc},
4026 {"gethostbyaddr", socket_gethostbyaddr,
4027 METH_VARARGS, gethostbyaddr_doc},
4028 {"gethostname", socket_gethostname,
4029 METH_NOARGS, gethostname_doc},
4030 {"getservbyname", socket_getservbyname,
4031 METH_VARARGS, getservbyname_doc},
4032 {"getservbyport", socket_getservbyport,
4033 METH_VARARGS, getservbyport_doc},
4034 {"getprotobyname", socket_getprotobyname,
4035 METH_VARARGS, getprotobyname_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004036#ifndef NO_DUP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004037 {"dup", socket_dup,
4038 METH_O, dup_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004039#endif
Dave Cole331708b2004-08-09 04:51:41 +00004040#ifdef HAVE_SOCKETPAIR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004041 {"socketpair", socket_socketpair,
4042 METH_VARARGS, socketpair_doc},
Dave Cole331708b2004-08-09 04:51:41 +00004043#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004044 {"ntohs", socket_ntohs,
4045 METH_VARARGS, ntohs_doc},
4046 {"ntohl", socket_ntohl,
4047 METH_O, ntohl_doc},
4048 {"htons", socket_htons,
4049 METH_VARARGS, htons_doc},
4050 {"htonl", socket_htonl,
4051 METH_O, htonl_doc},
4052 {"inet_aton", socket_inet_aton,
4053 METH_VARARGS, inet_aton_doc},
4054 {"inet_ntoa", socket_inet_ntoa,
4055 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004056#ifdef HAVE_INET_PTON
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004057 {"inet_pton", socket_inet_pton,
4058 METH_VARARGS, inet_pton_doc},
4059 {"inet_ntop", socket_inet_ntop,
4060 METH_VARARGS, inet_ntop_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004061#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004062 {"getaddrinfo", socket_getaddrinfo,
4063 METH_VARARGS, getaddrinfo_doc},
4064 {"getnameinfo", socket_getnameinfo,
4065 METH_VARARGS, getnameinfo_doc},
4066 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
4067 METH_NOARGS, getdefaulttimeout_doc},
4068 {"setdefaulttimeout", socket_setdefaulttimeout,
4069 METH_O, setdefaulttimeout_doc},
4070 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004071};
4072
Guido van Rossum30a685f1991-06-27 15:51:29 +00004073
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004074#ifdef MS_WINDOWS
4075#define OS_INIT_DEFINED
4076
4077/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004078
4079static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004080os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00004081{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004082 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00004083}
4084
4085static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004086os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00004087{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004088 WSADATA WSAData;
4089 int ret;
4090 ret = WSAStartup(0x0101, &WSAData);
4091 switch (ret) {
4092 case 0: /* No error */
4093 Py_AtExit(os_cleanup);
4094 return 1; /* Success */
4095 case WSASYSNOTREADY:
4096 PyErr_SetString(PyExc_ImportError,
4097 "WSAStartup failed: network not ready");
4098 break;
4099 case WSAVERNOTSUPPORTED:
4100 case WSAEINVAL:
4101 PyErr_SetString(
4102 PyExc_ImportError,
4103 "WSAStartup failed: requested version not supported");
4104 break;
4105 default:
4106 PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret);
4107 break;
4108 }
4109 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004110}
4111
Guido van Rossum8d665e61996-06-26 18:22:49 +00004112#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004113
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004114
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004115#ifdef PYOS_OS2
4116#define OS_INIT_DEFINED
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004117
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004118/* Additional initialization for OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004119
4120static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004121os_init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004122{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004123#ifndef PYCC_GCC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004124 int rc = sock_init();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004125
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004126 if (rc == 0) {
4127 return 1; /* Success */
4128 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004129
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004130 PyErr_Format(PyExc_ImportError, "OS/2 TCP/IP Error# %d", sock_errno());
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004131
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004132 return 0; /* Failure */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00004133#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004134 /* No need to initialise sockets with GCC/EMX */
4135 return 1; /* Success */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00004136#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004137}
4138
4139#endif /* PYOS_OS2 */
4140
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004141
4142#ifndef OS_INIT_DEFINED
4143static int
4144os_init(void)
4145{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004146 return 1; /* Success */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004147}
4148#endif
4149
4150
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004151/* C API table - always add new things to the end for binary
4152 compatibility. */
4153static
4154PySocketModule_APIObject PySocketModuleAPI =
4155{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004156 &sock_type,
4157 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004158};
4159
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004160
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004161/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004162
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004163 This module is actually called "_socket", and there's a wrapper
Guido van Rossum7d0a8262007-05-21 23:13:11 +00004164 "socket.py" which implements some additional functionality.
4165 The import of "_socket" may fail with an ImportError exception if
4166 os-specific initialization fails. On Windows, this does WINSOCK
4167 initialization. When WINSOCK is initialized succesfully, a call to
4168 WSACleanup() is scheduled to be made at exit time.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004169*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004170
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004171PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004172"Implementation module for socket operations.\n\
4173\n\
4174See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004175
Martin v. Löwis1a214512008-06-11 05:26:20 +00004176static struct PyModuleDef socketmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004177 PyModuleDef_HEAD_INIT,
4178 PySocket_MODULE_NAME,
4179 socket_doc,
4180 -1,
4181 socket_methods,
4182 NULL,
4183 NULL,
4184 NULL,
4185 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00004186};
4187
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004188PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00004189PyInit__socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004190{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004191 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00004192
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004193 if (!os_init())
4194 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004195
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004196 Py_TYPE(&sock_type) = &PyType_Type;
4197 m = PyModule_Create(&socketmodule);
4198 if (m == NULL)
4199 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004200
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004201 socket_error = PyErr_NewException("socket.error",
4202 PyExc_IOError, NULL);
4203 if (socket_error == NULL)
4204 return NULL;
4205 PySocketModuleAPI.error = socket_error;
4206 Py_INCREF(socket_error);
4207 PyModule_AddObject(m, "error", socket_error);
4208 socket_herror = PyErr_NewException("socket.herror",
4209 socket_error, NULL);
4210 if (socket_herror == NULL)
4211 return NULL;
4212 Py_INCREF(socket_herror);
4213 PyModule_AddObject(m, "herror", socket_herror);
4214 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
4215 NULL);
4216 if (socket_gaierror == NULL)
4217 return NULL;
4218 Py_INCREF(socket_gaierror);
4219 PyModule_AddObject(m, "gaierror", socket_gaierror);
4220 socket_timeout = PyErr_NewException("socket.timeout",
4221 socket_error, NULL);
4222 if (socket_timeout == NULL)
4223 return NULL;
4224 Py_INCREF(socket_timeout);
4225 PyModule_AddObject(m, "timeout", socket_timeout);
4226 Py_INCREF((PyObject *)&sock_type);
4227 if (PyModule_AddObject(m, "SocketType",
4228 (PyObject *)&sock_type) != 0)
4229 return NULL;
4230 Py_INCREF((PyObject *)&sock_type);
4231 if (PyModule_AddObject(m, "socket",
4232 (PyObject *)&sock_type) != 0)
4233 return NULL;
Guido van Rossum09be4091999-08-09 14:40:40 +00004234
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004235#ifdef ENABLE_IPV6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004236 has_ipv6 = Py_True;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004237#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004238 has_ipv6 = Py_False;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004239#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004240 Py_INCREF(has_ipv6);
4241 PyModule_AddObject(m, "has_ipv6", has_ipv6);
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004242
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004243 /* Export C API */
4244 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
4245 PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL)
4246 ) != 0)
4247 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004248
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004249 /* Address families (we only support AF_INET and AF_UNIX) */
Guido van Rossum09be4091999-08-09 14:40:40 +00004250#ifdef AF_UNSPEC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004251 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00004252#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004253 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004254#ifdef AF_INET6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004255 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004256#endif /* AF_INET6 */
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00004257#if defined(AF_UNIX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004258 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004259#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00004260#ifdef AF_AX25
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004261 /* Amateur Radio AX.25 */
4262 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004263#endif
4264#ifdef AF_IPX
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004265 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00004266#endif
4267#ifdef AF_APPLETALK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004268 /* Appletalk DDP */
4269 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004270#endif
4271#ifdef AF_NETROM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004272 /* Amateur radio NetROM */
4273 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004274#endif
4275#ifdef AF_BRIDGE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004276 /* Multiprotocol bridge */
4277 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004278#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004279#ifdef AF_ATMPVC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004280 /* ATM PVCs */
4281 PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004282#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004283#ifdef AF_AAL5
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004284 /* Reserved for Werner's ATM */
4285 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00004286#endif
4287#ifdef AF_X25
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004288 /* Reserved for X.25 project */
4289 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004290#endif
4291#ifdef AF_INET6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004292 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00004293#endif
4294#ifdef AF_ROSE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004295 /* Amateur Radio X.25 PLP */
4296 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004297#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004298#ifdef AF_DECnet
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004299 /* Reserved for DECnet project */
4300 PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004301#endif
4302#ifdef AF_NETBEUI
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004303 /* Reserved for 802.2LLC project */
4304 PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004305#endif
4306#ifdef AF_SECURITY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004307 /* Security callback pseudo AF */
4308 PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004309#endif
4310#ifdef AF_KEY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004311 /* PF_KEY key management API */
4312 PyModule_AddIntConstant(m, "AF_KEY", AF_KEY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004313#endif
4314#ifdef AF_NETLINK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004315 /* */
4316 PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);
4317 PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004318#ifdef NETLINK_SKIP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004319 PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004320#endif
4321#ifdef NETLINK_W1
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004322 PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004323#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004324 PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK);
4325 PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004326#ifdef NETLINK_TCPDIAG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004327 PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004328#endif
4329#ifdef NETLINK_NFLOG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004330 PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004331#endif
Neal Norwitz65851662006-01-16 04:31:40 +00004332#ifdef NETLINK_XFRM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004333 PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM);
Neal Norwitz65851662006-01-16 04:31:40 +00004334#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004335#ifdef NETLINK_ARPD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004336 PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004337#endif
4338#ifdef NETLINK_ROUTE6
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004339 PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004340#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004341 PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW);
Thomas Wouterscf297e42007-02-23 15:07:44 +00004342#ifdef NETLINK_DNRTMSG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004343 PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG);
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004344#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004345#ifdef NETLINK_TAPBASE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004346 PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004347#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004348#endif /* AF_NETLINK */
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004349#ifdef AF_ROUTE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004350 /* Alias to emulate 4.4BSD */
4351 PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004352#endif
4353#ifdef AF_ASH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004354 /* Ash */
4355 PyModule_AddIntConstant(m, "AF_ASH", AF_ASH);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004356#endif
4357#ifdef AF_ECONET
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004358 /* Acorn Econet */
4359 PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004360#endif
4361#ifdef AF_ATMSVC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004362 /* ATM SVCs */
4363 PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004364#endif
4365#ifdef AF_SNA
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004366 /* Linux SNA Project (nutters!) */
4367 PyModule_AddIntConstant(m, "AF_SNA", AF_SNA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004368#endif
4369#ifdef AF_IRDA
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004370 /* IRDA sockets */
4371 PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004372#endif
4373#ifdef AF_PPPOX
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004374 /* PPPoX sockets */
4375 PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004376#endif
4377#ifdef AF_WANPIPE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004378 /* Wanpipe API Sockets */
4379 PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004380#endif
4381#ifdef AF_LLC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004382 /* Linux LLC */
4383 PyModule_AddIntConstant(m, "AF_LLC", AF_LLC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004384#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00004385
Hye-Shik Chang81268602004-02-02 06:05:24 +00004386#ifdef USE_BLUETOOTH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004387 PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);
4388 PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);
4389 PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI);
4390 PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI);
4391 PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER);
Hye-Shik Chang81268602004-02-02 06:05:24 +00004392#if !defined(__FreeBSD__)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004393 PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP);
4394 PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR);
4395 PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00004396#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004397 PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);
4398 PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
4399 PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
Martin v. Löwis12af0482004-01-31 12:34:17 +00004400#endif
4401
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00004402#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004403 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
4404 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
4405 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
4406 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
4407 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
4408 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
4409 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
4410 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
4411 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00004412#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004413
Christian Heimes043d6f62008-01-07 17:19:16 +00004414#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004415 PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC);
Christian Heimes043d6f62008-01-07 17:19:16 +00004416
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004417 /* for addresses */
4418 PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ);
4419 PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME);
4420 PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID);
Christian Heimes043d6f62008-01-07 17:19:16 +00004421
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004422 PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE);
4423 PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE);
4424 PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE);
Christian Heimes043d6f62008-01-07 17:19:16 +00004425
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004426 /* for setsockopt() */
4427 PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC);
4428 PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE);
4429 PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE);
4430 PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE",
4431 TIPC_DEST_DROPPABLE);
4432 PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT);
Christian Heimes043d6f62008-01-07 17:19:16 +00004433
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004434 PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE",
4435 TIPC_LOW_IMPORTANCE);
4436 PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE",
4437 TIPC_MEDIUM_IMPORTANCE);
4438 PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE",
4439 TIPC_HIGH_IMPORTANCE);
4440 PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE",
4441 TIPC_CRITICAL_IMPORTANCE);
Christian Heimes043d6f62008-01-07 17:19:16 +00004442
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004443 /* for subscriptions */
4444 PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS);
4445 PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE);
Christian Heimes25bb7832008-01-11 16:17:00 +00004446#ifdef TIPC_SUB_CANCEL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004447 /* doesn't seem to be available everywhere */
4448 PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL);
Christian Heimes25bb7832008-01-11 16:17:00 +00004449#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004450 PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER);
4451 PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED);
4452 PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN);
4453 PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT);
4454 PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV);
4455 PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV);
Christian Heimes043d6f62008-01-07 17:19:16 +00004456#endif
4457
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004458 /* Socket types */
4459 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
4460 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004461/* We have incomplete socket support. */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004462 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
4463 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00004464#if defined(SOCK_RDM)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004465 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004466#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004467
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004468#ifdef SO_DEBUG
4469 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004470#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004471#ifdef SO_ACCEPTCONN
4472 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004473#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004474#ifdef SO_REUSEADDR
4475 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004476#endif
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00004477#ifdef SO_EXCLUSIVEADDRUSE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004478 PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE);
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00004479#endif
4480
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004481#ifdef SO_KEEPALIVE
4482 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004483#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004484#ifdef SO_DONTROUTE
4485 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004486#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004487#ifdef SO_BROADCAST
4488 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004489#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004490#ifdef SO_USELOOPBACK
4491 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004492#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004493#ifdef SO_LINGER
4494 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004495#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004496#ifdef SO_OOBINLINE
4497 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004498#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004499#ifdef SO_REUSEPORT
4500 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004501#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004502#ifdef SO_SNDBUF
4503 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004504#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004505#ifdef SO_RCVBUF
4506 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004507#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004508#ifdef SO_SNDLOWAT
4509 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004510#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004511#ifdef SO_RCVLOWAT
4512 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004513#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004514#ifdef SO_SNDTIMEO
4515 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004516#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004517#ifdef SO_RCVTIMEO
4518 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004519#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004520#ifdef SO_ERROR
4521 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004522#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004523#ifdef SO_TYPE
4524 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004525#endif
4526
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004527 /* Maximum number of connections for "listen" */
4528#ifdef SOMAXCONN
4529 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004530#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004531 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004532#endif
4533
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004534 /* Flags for send, recv */
4535#ifdef MSG_OOB
4536 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004537#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004538#ifdef MSG_PEEK
4539 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004540#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004541#ifdef MSG_DONTROUTE
4542 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004543#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004544#ifdef MSG_DONTWAIT
4545 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00004546#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004547#ifdef MSG_EOR
4548 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004549#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004550#ifdef MSG_TRUNC
4551 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004552#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004553#ifdef MSG_CTRUNC
4554 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004555#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004556#ifdef MSG_WAITALL
4557 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004558#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004559#ifdef MSG_BTAG
4560 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004561#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004562#ifdef MSG_ETAG
4563 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004564#endif
4565
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004566 /* Protocol level and numbers, usable for [gs]etsockopt */
4567#ifdef SOL_SOCKET
4568 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004569#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004570#ifdef SOL_IP
4571 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004572#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004573 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00004574#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004575#ifdef SOL_IPX
4576 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00004577#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004578#ifdef SOL_AX25
4579 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004580#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004581#ifdef SOL_ATALK
4582 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004583#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004584#ifdef SOL_NETROM
4585 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004586#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004587#ifdef SOL_ROSE
4588 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004589#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004590#ifdef SOL_TCP
4591 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004592#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004593 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00004594#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004595#ifdef SOL_UDP
4596 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004597#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004598 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00004599#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004600#ifdef IPPROTO_IP
4601 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00004602#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004603 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004604#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004605#ifdef IPPROTO_HOPOPTS
4606 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004607#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004608#ifdef IPPROTO_ICMP
4609 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00004610#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004611 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004612#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004613#ifdef IPPROTO_IGMP
4614 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004615#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004616#ifdef IPPROTO_GGP
4617 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004618#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004619#ifdef IPPROTO_IPV4
4620 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004621#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004622#ifdef IPPROTO_IPV6
4623 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisa0f17342003-10-03 13:56:20 +00004624#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004625#ifdef IPPROTO_IPIP
4626 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004627#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004628#ifdef IPPROTO_TCP
4629 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00004630#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004631 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004632#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004633#ifdef IPPROTO_EGP
4634 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004635#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004636#ifdef IPPROTO_PUP
4637 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004638#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004639#ifdef IPPROTO_UDP
4640 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00004641#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004642 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004643#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004644#ifdef IPPROTO_IDP
4645 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004646#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004647#ifdef IPPROTO_HELLO
4648 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004649#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004650#ifdef IPPROTO_ND
4651 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004652#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004653#ifdef IPPROTO_TP
4654 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004655#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004656#ifdef IPPROTO_IPV6
4657 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004658#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004659#ifdef IPPROTO_ROUTING
4660 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004661#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004662#ifdef IPPROTO_FRAGMENT
4663 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004664#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004665#ifdef IPPROTO_RSVP
4666 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004667#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004668#ifdef IPPROTO_GRE
4669 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004670#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004671#ifdef IPPROTO_ESP
4672 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004673#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004674#ifdef IPPROTO_AH
4675 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004676#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004677#ifdef IPPROTO_MOBILE
4678 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004679#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004680#ifdef IPPROTO_ICMPV6
4681 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004682#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004683#ifdef IPPROTO_NONE
4684 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004685#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004686#ifdef IPPROTO_DSTOPTS
4687 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004688#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004689#ifdef IPPROTO_XTP
4690 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004691#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004692#ifdef IPPROTO_EON
4693 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004694#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004695#ifdef IPPROTO_PIM
4696 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004697#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004698#ifdef IPPROTO_IPCOMP
4699 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004700#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004701#ifdef IPPROTO_VRRP
4702 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004703#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004704#ifdef IPPROTO_BIP
4705 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004706#endif
4707/**/
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004708#ifdef IPPROTO_RAW
4709 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00004710#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004711 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004712#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004713#ifdef IPPROTO_MAX
4714 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004715#endif
4716
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004717 /* Some port configuration */
4718#ifdef IPPORT_RESERVED
4719 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004720#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004721 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004722#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004723#ifdef IPPORT_USERRESERVED
4724 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004725#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004726 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004727#endif
4728
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004729 /* Some reserved IP v.4 addresses */
4730#ifdef INADDR_ANY
4731 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004732#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004733 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004734#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004735#ifdef INADDR_BROADCAST
4736 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004737#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004738 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004739#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004740#ifdef INADDR_LOOPBACK
4741 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004742#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004743 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004744#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004745#ifdef INADDR_UNSPEC_GROUP
4746 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004747#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004748 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004749#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004750#ifdef INADDR_ALLHOSTS_GROUP
4751 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
4752 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004753#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004754 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004755#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004756#ifdef INADDR_MAX_LOCAL_GROUP
4757 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
4758 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004759#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004760 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004761#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004762#ifdef INADDR_NONE
4763 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004764#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004765 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004766#endif
4767
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004768 /* IPv4 [gs]etsockopt options */
4769#ifdef IP_OPTIONS
4770 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004771#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004772#ifdef IP_HDRINCL
4773 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004774#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004775#ifdef IP_TOS
4776 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004777#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004778#ifdef IP_TTL
4779 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004780#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004781#ifdef IP_RECVOPTS
4782 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004783#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004784#ifdef IP_RECVRETOPTS
4785 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004786#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004787#ifdef IP_RECVDSTADDR
4788 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004789#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004790#ifdef IP_RETOPTS
4791 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004792#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004793#ifdef IP_MULTICAST_IF
4794 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004795#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004796#ifdef IP_MULTICAST_TTL
4797 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004798#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004799#ifdef IP_MULTICAST_LOOP
4800 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004801#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004802#ifdef IP_ADD_MEMBERSHIP
4803 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004804#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004805#ifdef IP_DROP_MEMBERSHIP
4806 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004807#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004808#ifdef IP_DEFAULT_MULTICAST_TTL
4809 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
4810 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00004811#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004812#ifdef IP_DEFAULT_MULTICAST_LOOP
4813 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
4814 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004815#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004816#ifdef IP_MAX_MEMBERSHIPS
4817 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00004818#endif
4819
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004820 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
4821#ifdef IPV6_JOIN_GROUP
4822 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004823#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004824#ifdef IPV6_LEAVE_GROUP
4825 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004826#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004827#ifdef IPV6_MULTICAST_HOPS
4828 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004829#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004830#ifdef IPV6_MULTICAST_IF
4831 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004832#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004833#ifdef IPV6_MULTICAST_LOOP
4834 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004835#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004836#ifdef IPV6_UNICAST_HOPS
4837 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004838#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004839 /* Additional IPV6 socket options, defined in RFC 3493 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00004840#ifdef IPV6_V6ONLY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004841 PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004842#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004843 /* Advanced IPV6 socket options, from RFC 3542 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00004844#ifdef IPV6_CHECKSUM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004845 PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004846#endif
4847#ifdef IPV6_DONTFRAG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004848 PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004849#endif
4850#ifdef IPV6_DSTOPTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004851 PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004852#endif
4853#ifdef IPV6_HOPLIMIT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004854 PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004855#endif
4856#ifdef IPV6_HOPOPTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004857 PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004858#endif
4859#ifdef IPV6_NEXTHOP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004860 PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004861#endif
4862#ifdef IPV6_PATHMTU
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004863 PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004864#endif
4865#ifdef IPV6_PKTINFO
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004866 PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004867#endif
4868#ifdef IPV6_RECVDSTOPTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004869 PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004870#endif
4871#ifdef IPV6_RECVHOPLIMIT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004872 PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004873#endif
4874#ifdef IPV6_RECVHOPOPTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004875 PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004876#endif
4877#ifdef IPV6_RECVPKTINFO
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004878 PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004879#endif
4880#ifdef IPV6_RECVRTHDR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004881 PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004882#endif
4883#ifdef IPV6_RECVTCLASS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004884 PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004885#endif
4886#ifdef IPV6_RTHDR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004887 PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004888#endif
4889#ifdef IPV6_RTHDRDSTOPTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004890 PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004891#endif
4892#ifdef IPV6_RTHDR_TYPE_0
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004893 PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004894#endif
4895#ifdef IPV6_RECVPATHMTU
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004896 PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004897#endif
4898#ifdef IPV6_TCLASS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004899 PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004900#endif
4901#ifdef IPV6_USE_MIN_MTU
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004902 PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004903#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004904
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004905 /* TCP options */
4906#ifdef TCP_NODELAY
4907 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00004908#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004909#ifdef TCP_MAXSEG
4910 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00004911#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004912#ifdef TCP_CORK
4913 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004914#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004915#ifdef TCP_KEEPIDLE
4916 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004917#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004918#ifdef TCP_KEEPINTVL
4919 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004920#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004921#ifdef TCP_KEEPCNT
4922 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004923#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004924#ifdef TCP_SYNCNT
4925 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004926#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004927#ifdef TCP_LINGER2
4928 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004929#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004930#ifdef TCP_DEFER_ACCEPT
4931 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004932#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004933#ifdef TCP_WINDOW_CLAMP
4934 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004935#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004936#ifdef TCP_INFO
4937 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004938#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004939#ifdef TCP_QUICKACK
4940 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004941#endif
4942
Guido van Rossum09be4091999-08-09 14:40:40 +00004943
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004944 /* IPX options */
4945#ifdef IPX_TYPE
4946 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004947#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004948
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004949 /* get{addr,name}info parameters */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004950#ifdef EAI_ADDRFAMILY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004951 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004952#endif
4953#ifdef EAI_AGAIN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004954 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004955#endif
4956#ifdef EAI_BADFLAGS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004957 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004958#endif
4959#ifdef EAI_FAIL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004960 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004961#endif
4962#ifdef EAI_FAMILY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004963 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004964#endif
4965#ifdef EAI_MEMORY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004966 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004967#endif
4968#ifdef EAI_NODATA
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004969 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004970#endif
4971#ifdef EAI_NONAME
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004972 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004973#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004974#ifdef EAI_OVERFLOW
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004975 PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004976#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004977#ifdef EAI_SERVICE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004978 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004979#endif
4980#ifdef EAI_SOCKTYPE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004981 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004982#endif
4983#ifdef EAI_SYSTEM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004984 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004985#endif
4986#ifdef EAI_BADHINTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004987 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004988#endif
4989#ifdef EAI_PROTOCOL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004990 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004991#endif
4992#ifdef EAI_MAX
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004993 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004994#endif
4995#ifdef AI_PASSIVE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004996 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004997#endif
4998#ifdef AI_CANONNAME
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004999 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005000#endif
5001#ifdef AI_NUMERICHOST
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005002 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005003#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00005004#ifdef AI_NUMERICSERV
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005005 PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005006#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005007#ifdef AI_MASK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005008 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005009#endif
5010#ifdef AI_ALL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005011 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005012#endif
5013#ifdef AI_V4MAPPED_CFG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005014 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005015#endif
5016#ifdef AI_ADDRCONFIG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005017 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005018#endif
5019#ifdef AI_V4MAPPED
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005020 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005021#endif
5022#ifdef AI_DEFAULT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005023 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005024#endif
5025#ifdef NI_MAXHOST
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005026 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005027#endif
5028#ifdef NI_MAXSERV
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005029 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005030#endif
5031#ifdef NI_NOFQDN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005032 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005033#endif
5034#ifdef NI_NUMERICHOST
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005035 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005036#endif
5037#ifdef NI_NAMEREQD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005038 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005039#endif
5040#ifdef NI_NUMERICSERV
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005041 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005042#endif
5043#ifdef NI_DGRAM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005044 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005045#endif
5046
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005047 /* shutdown() parameters */
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005048#ifdef SHUT_RD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005049 PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005050#elif defined(SD_RECEIVE)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005051 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005052#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005053 PyModule_AddIntConstant(m, "SHUT_RD", 0);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005054#endif
5055#ifdef SHUT_WR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005056 PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005057#elif defined(SD_SEND)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005058 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005059#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005060 PyModule_AddIntConstant(m, "SHUT_WR", 1);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005061#endif
5062#ifdef SHUT_RDWR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005063 PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005064#elif defined(SD_BOTH)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005065 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005066#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005067 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005068#endif
5069
Christian Heimesfaf2f632008-01-06 16:59:19 +00005070#ifdef SIO_RCVALL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005071 {
5072 PyObject *tmp;
5073 tmp = PyLong_FromUnsignedLong(SIO_RCVALL);
5074 if (tmp == NULL)
5075 return NULL;
5076 PyModule_AddObject(m, "SIO_RCVALL", tmp);
5077 }
5078 PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF);
5079 PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON);
5080 PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00005081#ifdef RCVALL_IPLEVEL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005082 PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00005083#endif
5084#ifdef RCVALL_MAX
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005085 PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00005086#endif
Christian Heimesfaf2f632008-01-06 16:59:19 +00005087#endif /* _MSTCPIP_ */
5088
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005089 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00005090#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005091 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00005092#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005093 return m;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005094}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005095
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005096
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005097#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +00005098#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005099
5100/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005101/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005102
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005103int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005104inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005105{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005106 if (af == AF_INET) {
Benjamin Petersonf91df042009-02-13 02:50:59 +00005107#if (SIZEOF_INT != 4)
5108#error "Not sure if in_addr_t exists and int is not 32-bits."
5109#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005110 unsigned int packed_addr;
5111 packed_addr = inet_addr(src);
5112 if (packed_addr == INADDR_NONE)
5113 return 0;
5114 memcpy(dst, &packed_addr, 4);
5115 return 1;
5116 }
5117 /* Should set errno to EAFNOSUPPORT */
5118 return -1;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005119}
5120
Martin v. Löwisc925b1532001-07-21 09:42:15 +00005121const char *
5122inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005123{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005124 if (af == AF_INET) {
5125 struct in_addr packed_addr;
5126 if (size < 16)
5127 /* Should set errno to ENOSPC. */
5128 return NULL;
5129 memcpy(&packed_addr, src, sizeof(packed_addr));
5130 return strncpy(dst, inet_ntoa(packed_addr), size);
5131 }
5132 /* Should set errno to EAFNOSUPPORT */
5133 return NULL;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005134}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005135
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005136#endif
Christian Heimesb6150692007-11-15 23:37:07 +00005137#endif