blob: eac5b4e4d0e85227ae9349cfbb433039213f69d4 [file] [log] [blame]
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001/* Socket module */
2
3/*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005This module provides an interface to Berkeley socket IPC.
6
7Limitations:
8
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00009- Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a
Christian Heimes043d6f62008-01-07 17:19:16 +000010 portable manner, though AF_PACKET, AF_NETLINK and AF_TIPC are supported
11 under Linux.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000012- No read/write operations (use sendall/recv or makefile instead).
13- Additional restrictions apply on some non-Unix platforms (compensated
14 for by socket.py).
Guido van Rossum6574b3e1991-06-25 21:36:08 +000015
Guido van Rossum27e177d1995-03-16 15:43:47 +000016Module interface:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000017
Guido van Rossum27e177d1995-03-16 15:43:47 +000018- socket.error: exception raised for socket specific errors
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000019- socket.gaierror: exception raised for getaddrinfo/getnameinfo errors,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 a subclass of socket.error
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000021- socket.herror: exception raised for gethostby* errors,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 a subclass of socket.error
Guido van Rossum30a685f1991-06-27 15:51:29 +000023- socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000024- socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])
Guido van Rossum27e177d1995-03-16 15:43:47 +000025- socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')
Guido van Rossum25405c71996-12-19 16:42:52 +000026- socket.getprotobyname(protocolname) --> protocol number
Barry Warsaw11b91a02004-06-28 00:50:43 +000027- socket.getservbyname(servicename[, protocolname]) --> port number
28- socket.getservbyport(portnumber[, protocolname]) --> service name
Guido van Rossum7d0a8262007-05-21 23:13:11 +000029- socket.socket([family[, type [, proto, fileno]]]) --> new socket object
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 (fileno specifies a pre-existing socket file descriptor)
Dave Cole331708b2004-08-09 04:51:41 +000031- socket.socketpair([family[, type [, proto]]]) --> (socket, socket)
Guido van Rossum006bf911996-06-12 04:04:55 +000032- socket.ntohs(16 bit value) --> new int object
33- socket.ntohl(32 bit value) --> new int object
34- socket.htons(16 bit value) --> new int object
35- socket.htonl(32 bit value) --> new int object
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000036- socket.getaddrinfo(host, port [, family, socktype, proto, flags])
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 --> List of (family, socktype, proto, canonname, sockaddr)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000038- socket.getnameinfo(sockaddr, flags) --> (host, port)
Guido van Rossum27e177d1995-03-16 15:43:47 +000039- socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
Guido van Rossum47dfa4a2003-04-25 05:48:32 +000040- socket.has_ipv6: boolean value indicating if IPv6 is supported
Guido van Rossum5c9eb211999-08-20 18:21:51 +000041- socket.inet_aton(IP address) -> 32-bit packed IP representation
42- socket.inet_ntoa(packed IP) -> IP address string
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +000043- socket.getdefaulttimeout() -> None | float
44- socket.setdefaulttimeout(None | float)
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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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{
Daniel Stutzbach6c765282010-09-03 12:38:33 +0000354 WSAPROTOCOL_INFO info;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000355
Daniel Stutzbach6c765282010-09-03 12:38:33 +0000356 if (WSADuplicateSocket(handle, GetCurrentProcessId(), &info))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 return INVALID_SOCKET;
Daniel Stutzbach6c765282010-09-03 12:38:33 +0000358
359 return WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
360 FROM_PROTOCOL_INFO, &info, 0, 0);
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000361}
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000362#define SOCKETCLOSE closesocket
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000363#else
364/* On Unix we can use dup to duplicate the file descriptor of a socket*/
365#define dup_socket(fd) dup(fd)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000366#endif
367
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000368#ifdef MS_WIN32
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000369#define EAFNOSUPPORT WSAEAFNOSUPPORT
370#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000371#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000372
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000373#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000374#define SOCKETCLOSE soclose
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000375#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000376#endif
377
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000378#ifndef SOCKETCLOSE
379#define SOCKETCLOSE close
380#endif
381
Jesse Noller32d68c22009-03-31 18:48:42 +0000382#if defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H) && !defined(__NetBSD__)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000383#define USE_BLUETOOTH 1
384#if defined(__FreeBSD__)
385#define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP
386#define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM
Thomas Wouterscf297e42007-02-23 15:07:44 +0000387#define BTPROTO_HCI BLUETOOTH_PROTO_HCI
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000388#define SOL_HCI SOL_HCI_RAW
389#define HCI_FILTER SO_HCI_RAW_FILTER
Hye-Shik Chang81268602004-02-02 06:05:24 +0000390#define sockaddr_l2 sockaddr_l2cap
391#define sockaddr_rc sockaddr_rfcomm
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000392#define hci_dev hci_node
Hye-Shik Chang81268602004-02-02 06:05:24 +0000393#define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb)
394#define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000395#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000396#elif defined(__NetBSD__)
397#define sockaddr_l2 sockaddr_bt
398#define sockaddr_rc sockaddr_bt
Thomas Wouterscf297e42007-02-23 15:07:44 +0000399#define sockaddr_hci sockaddr_bt
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000400#define sockaddr_sco sockaddr_bt
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000401#define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb)
402#define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000403#define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000404#define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000405#else
Hye-Shik Chang81268602004-02-02 06:05:24 +0000406#define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)
407#define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000408#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000409#define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)
410#endif
411#endif
412
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000413#ifdef __VMS
414/* TCP/IP Services for VMS uses a maximum send/recv buffer length */
415#define SEGMENT_SIZE (32 * 1024 -1)
416#endif
417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418#define SAS2SA(x) ((struct sockaddr *)(x))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000419
Martin v. Löwise9416172003-05-03 10:12:45 +0000420/*
421 * Constants for getnameinfo()
422 */
423#if !defined(NI_MAXHOST)
424#define NI_MAXHOST 1025
425#endif
426#if !defined(NI_MAXSERV)
427#define NI_MAXSERV 32
428#endif
429
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000430#ifndef INVALID_SOCKET /* MS defines this */
431#define INVALID_SOCKET (-1)
432#endif
433
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000434/* XXX There's a problem here: *static* functions are not supposed to have
435 a Py prefix (or use CapitalizedWords). Later... */
436
Guido van Rossum30a685f1991-06-27 15:51:29 +0000437/* Global variable holding the exception type for errors detected
438 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000439static PyObject *socket_error;
440static PyObject *socket_herror;
441static PyObject *socket_gaierror;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000442static PyObject *socket_timeout;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000443
Tim Peters643a7fc2002-02-17 04:13:21 +0000444/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000445 The sock_type variable contains pointers to various functions,
446 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000447 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000448static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000449
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000450#if defined(HAVE_POLL_H)
451#include <poll.h>
452#elif defined(HAVE_SYS_POLL_H)
453#include <sys/poll.h>
454#endif
455
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000456#ifdef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE
457/* Platform can select file descriptors beyond FD_SETSIZE */
458#define IS_SELECTABLE(s) 1
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000459#elif defined(HAVE_POLL)
460/* Instead of select(), we'll use poll() since poll() works on any fd. */
461#define IS_SELECTABLE(s) 1
462/* Can we call select() with this socket without a buffer overrun? */
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000463#else
464/* POSIX says selecting file descriptors beyond FD_SETSIZE
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000465 has undefined behaviour. If there's no timeout left, we don't have to
466 call select, so it's a safe, little white lie. */
467#define IS_SELECTABLE(s) ((s)->sock_fd < FD_SETSIZE || s->sock_timeout <= 0.0)
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000468#endif
Neal Norwitz082b2df2006-02-07 07:04:46 +0000469
470static PyObject*
471select_error(void)
472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 PyErr_SetString(socket_error, "unable to select on socket");
474 return NULL;
Neal Norwitz082b2df2006-02-07 07:04:46 +0000475}
476
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000477#ifdef MS_WINDOWS
Antoine Pitrouc8f01962010-09-28 22:03:27 +0000478#ifndef WSAEAGAIN
479#define WSAEAGAIN WSAEWOULDBLOCK
480#endif
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000481#define CHECK_ERRNO(expected) \
482 (WSAGetLastError() == WSA ## expected)
483#else
484#define CHECK_ERRNO(expected) \
485 (errno == expected)
486#endif
487
Guido van Rossum30a685f1991-06-27 15:51:29 +0000488/* Convenience function to raise an error according to errno
489 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000490
Guido van Rossum73624e91994-10-10 17:59:00 +0000491static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000492set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000493{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000494#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 int err_no = WSAGetLastError();
496 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
497 recognizes the error codes used by both GetLastError() and
498 WSAGetLastError */
499 if (err_no)
500 return PyErr_SetExcFromWindowsErr(socket_error, err_no);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000501#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000502
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000503#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 if (sock_errno() != NO_ERROR) {
505 APIRET rc;
506 ULONG msglen;
507 char outbuf[100];
508 int myerrorcode = sock_errno();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 /* Retrieve socket-related error message from MPTN.MSG file */
511 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
512 myerrorcode - SOCBASEERR + 26,
513 "mptn.msg",
514 &msglen);
515 if (rc == NO_ERROR) {
516 PyObject *v;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 /* OS/2 doesn't guarantee a terminator */
519 outbuf[msglen] = '\0';
520 if (strlen(outbuf) > 0) {
521 /* If non-empty msg, trim CRLF */
522 char *lastc = &outbuf[ strlen(outbuf)-1 ];
523 while (lastc > outbuf &&
524 isspace(Py_CHARMASK(*lastc))) {
525 /* Trim trailing whitespace (CRLF) */
526 *lastc-- = '\0';
527 }
528 }
529 v = Py_BuildValue("(is)", myerrorcode, outbuf);
530 if (v != NULL) {
531 PyErr_SetObject(socket_error, v);
532 Py_DECREF(v);
533 }
534 return NULL;
535 }
536 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000537#endif
538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 return PyErr_SetFromErrno(socket_error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000540}
541
Guido van Rossum30a685f1991-06-27 15:51:29 +0000542
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000543static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000544set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000547
548#ifdef HAVE_HSTRERROR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000550#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 v = Py_BuildValue("(is)", h_error, "host not found");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000552#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 if (v != NULL) {
554 PyErr_SetObject(socket_herror, v);
555 Py_DECREF(v);
556 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000559}
560
561
562static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000563set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000564{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 PyObject *v;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000566
Martin v. Löwis272cb402002-03-01 08:31:07 +0000567#ifdef EAI_SYSTEM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 /* EAI_SYSTEM is not available on Windows XP. */
569 if (error == EAI_SYSTEM)
570 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000571#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000572
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000573#ifdef HAVE_GAI_STRERROR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000575#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000577#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 if (v != NULL) {
579 PyErr_SetObject(socket_gaierror, v);
580 Py_DECREF(v);
581 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000584}
585
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000586#ifdef __VMS
587/* Function to send in segments */
588static int
589sendsegmented(int sock_fd, char *buf, int len, int flags)
590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 int n = 0;
592 int remaining = len;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 while (remaining > 0) {
595 unsigned int segment;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining);
598 n = send(sock_fd, buf, segment, flags);
599 if (n < 0) {
600 return n;
601 }
602 remaining -= segment;
603 buf += segment;
604 } /* end while */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 return len;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000607}
608#endif
609
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000610/* Function to perform the setting of socket blocking mode
611 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000612static int
613internal_setblocking(PySocketSockObject *s, int block)
614{
Guido van Rossum67f7a382002-06-06 21:08:16 +0000615#ifndef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 int delay_flag;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000617#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +0000618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 Py_BEGIN_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000620#ifndef MS_WINDOWS
621#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 block = !block;
623 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000624#elif defined(__VMS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 block = !block;
626 ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000627#else /* !PYOS_OS2 && !__VMS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
629 if (block)
630 delay_flag &= (~O_NONBLOCK);
631 else
632 delay_flag |= O_NONBLOCK;
633 fcntl(s->sock_fd, F_SETFL, delay_flag);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000634#endif /* !PYOS_OS2 */
635#else /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 block = !block;
637 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000638#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 /* Since these don't return anything */
642 return 1;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000643}
644
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000645/* Do a select()/poll() on the socket, if necessary (sock_timeout > 0).
Guido van Rossum11ba0942002-06-13 15:07:44 +0000646 The argument writing indicates the direction.
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000647 This does not raise an exception; we'll let our caller do that
648 after they've reacquired the interpreter lock.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000649 Returns 1 on timeout, -1 on error, 0 otherwise. */
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000650static int
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000651internal_select_ex(PySocketSockObject *s, int writing, double interval)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 int n;
Guido van Rossum11ba0942002-06-13 15:07:44 +0000654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 /* Nothing to do unless we're in timeout mode (not non-blocking) */
656 if (s->sock_timeout <= 0.0)
657 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 /* Guard against closed socket */
660 if (s->sock_fd < 0)
661 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000662
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000663 /* Handling this condition here simplifies the select loops */
664 if (interval < 0.0)
665 return 1;
666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 /* Prefer poll, if available, since you can poll() any fd
668 * which can't be done with select(). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000669#ifdef HAVE_POLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 {
671 struct pollfd pollfd;
672 int timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 pollfd.fd = s->sock_fd;
675 pollfd.events = writing ? POLLOUT : POLLIN;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 /* s->sock_timeout is in seconds, timeout in ms */
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000678 timeout = (int)(interval * 1000 + 0.5);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 n = poll(&pollfd, 1, timeout);
680 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000681#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 {
683 /* Construct the arguments to select */
684 fd_set fds;
685 struct timeval tv;
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000686 tv.tv_sec = (int)interval;
687 tv.tv_usec = (int)((interval - tv.tv_sec) * 1e6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 FD_ZERO(&fds);
689 FD_SET(s->sock_fd, &fds);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 /* See if the socket is ready */
692 if (writing)
Antoine Pitrou19467d22010-08-17 19:33:30 +0000693 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
694 NULL, &fds, NULL, &tv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 else
Antoine Pitrou19467d22010-08-17 19:33:30 +0000696 n = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
697 &fds, NULL, NULL, &tv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000699#endif
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 if (n < 0)
702 return -1;
703 if (n == 0)
704 return 1;
705 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000706}
707
Antoine Pitrou3e1fd272010-09-28 21:23:11 +0000708static int
709internal_select(PySocketSockObject *s, int writing)
710{
711 return internal_select_ex(s, writing, s->sock_timeout);
712}
713
714/*
715 Two macros for automatic retry of select() in case of false positives
716 (for example, select() could indicate a socket is ready for reading
717 but the data then discarded by the OS because of a wrong checksum).
718 Here is an example of use:
719
720 BEGIN_SELECT_LOOP(s)
721 Py_BEGIN_ALLOW_THREADS
722 timeout = internal_select_ex(s, 0, interval);
723 if (!timeout)
724 outlen = recv(s->sock_fd, cbuf, len, flags);
725 Py_END_ALLOW_THREADS
726 if (timeout == 1) {
727 PyErr_SetString(socket_timeout, "timed out");
728 return -1;
729 }
730 END_SELECT_LOOP(s)
731*/
732
733#define BEGIN_SELECT_LOOP(s) \
734 { \
735 _PyTime_timeval now, deadline = {0, 0}; \
736 double interval = s->sock_timeout; \
737 int has_timeout = s->sock_timeout > 0.0; \
738 if (has_timeout) { \
739 _PyTime_gettimeofday(&now); \
740 deadline = now; \
741 _PyTime_ADD_SECONDS(deadline, s->sock_timeout); \
742 } \
743 while (1) { \
744 errno = 0; \
745
746#define END_SELECT_LOOP(s) \
747 if (!has_timeout || \
748 (!CHECK_ERRNO(EWOULDBLOCK) && !CHECK_ERRNO(EAGAIN))) \
749 break; \
750 _PyTime_gettimeofday(&now); \
751 interval = _PyTime_INTERVAL(now, deadline); \
752 } \
753 } \
754
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000755/* Initialize a new socket object. */
756
Tim Petersa12b4cf2002-07-18 22:38:44 +0000757static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000758
Martin v. Löwis1a214512008-06-11 05:26:20 +0000759static void
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000760init_sockobject(PySocketSockObject *s,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 SOCKET_T fd, int family, int type, int proto)
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 s->sock_fd = fd;
764 s->sock_family = family;
765 s->sock_type = type;
766 s->sock_proto = proto;
767 s->sock_timeout = defaulttimeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 s->errorhandler = &set_error;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 if (defaulttimeout >= 0.0)
772 internal_setblocking(s, 0);
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000773
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000774}
775
776
Guido van Rossum30a685f1991-06-27 15:51:29 +0000777/* Create a new socket object.
778 This just creates the object and initializes it.
779 If the creation fails, return NULL and set an exception (implicit
780 in NEWOBJ()). */
781
Guido van Rossum73624e91994-10-10 17:59:00 +0000782static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000783new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 PySocketSockObject *s;
786 s = (PySocketSockObject *)
787 PyType_GenericNew(&sock_type, NULL, NULL);
788 if (s != NULL)
789 init_sockobject(s, fd, family, type, proto);
790 return s;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000791}
792
Guido van Rossum30a685f1991-06-27 15:51:29 +0000793
Guido van Rossum48a680c2001-03-02 06:34:14 +0000794/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000795 thread to be in gethostbyname or getaddrinfo */
796#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
797PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000798#endif
799
800
Guido van Rossum30a685f1991-06-27 15:51:29 +0000801/* Convert a string specifying a host name or one of a few symbolic
802 names to a numeric IP address. This usually calls gethostbyname()
803 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000804 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000805 an error occurred; then an exception is raised. */
806
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000807static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000808setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000809{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 struct addrinfo hints, *res;
811 int error;
812 int d1, d2, d3, d4;
813 char ch;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
816 if (name[0] == '\0') {
817 int siz;
818 memset(&hints, 0, sizeof(hints));
819 hints.ai_family = af;
820 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
821 hints.ai_flags = AI_PASSIVE;
822 Py_BEGIN_ALLOW_THREADS
823 ACQUIRE_GETADDRINFO_LOCK
824 error = getaddrinfo(NULL, "0", &hints, &res);
825 Py_END_ALLOW_THREADS
826 /* We assume that those thread-unsafe getaddrinfo() versions
827 *are* safe regarding their return value, ie. that a
828 subsequent call to getaddrinfo() does not destroy the
829 outcome of the first call. */
830 RELEASE_GETADDRINFO_LOCK
831 if (error) {
832 set_gaierror(error);
833 return -1;
834 }
835 switch (res->ai_family) {
836 case AF_INET:
837 siz = 4;
838 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000839#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 case AF_INET6:
841 siz = 16;
842 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000843#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 default:
845 freeaddrinfo(res);
846 PyErr_SetString(socket_error,
847 "unsupported address family");
848 return -1;
849 }
850 if (res->ai_next) {
851 freeaddrinfo(res);
852 PyErr_SetString(socket_error,
853 "wildcard resolved to multiple address");
854 return -1;
855 }
856 if (res->ai_addrlen < addr_ret_size)
857 addr_ret_size = res->ai_addrlen;
858 memcpy(addr_ret, res->ai_addr, addr_ret_size);
859 freeaddrinfo(res);
860 return siz;
861 }
862 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
863 struct sockaddr_in *sin;
864 if (af != AF_INET && af != AF_UNSPEC) {
865 PyErr_SetString(socket_error,
866 "address family mismatched");
867 return -1;
868 }
869 sin = (struct sockaddr_in *)addr_ret;
870 memset((void *) sin, '\0', sizeof(*sin));
871 sin->sin_family = AF_INET;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000872#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 sin->sin_len = sizeof(*sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000874#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 sin->sin_addr.s_addr = INADDR_BROADCAST;
876 return sizeof(sin->sin_addr);
877 }
878 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
879 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
880 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
881 struct sockaddr_in *sin;
882 sin = (struct sockaddr_in *)addr_ret;
883 sin->sin_addr.s_addr = htonl(
884 ((long) d1 << 24) | ((long) d2 << 16) |
885 ((long) d3 << 8) | ((long) d4 << 0));
886 sin->sin_family = AF_INET;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000887#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 sin->sin_len = sizeof(*sin);
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000889#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 return 4;
891 }
892 memset(&hints, 0, sizeof(hints));
893 hints.ai_family = af;
894 Py_BEGIN_ALLOW_THREADS
895 ACQUIRE_GETADDRINFO_LOCK
896 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000897#if defined(__digital__) && defined(__unix__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 if (error == EAI_NONAME && af == AF_UNSPEC) {
899 /* On Tru64 V5.1, numeric-to-addr conversion fails
900 if no address family is given. Assume IPv4 for now.*/
901 hints.ai_family = AF_INET;
902 error = getaddrinfo(name, NULL, &hints, &res);
903 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000904#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 Py_END_ALLOW_THREADS
906 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
907 if (error) {
908 set_gaierror(error);
909 return -1;
910 }
911 if (res->ai_addrlen < addr_ret_size)
912 addr_ret_size = res->ai_addrlen;
913 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
914 freeaddrinfo(res);
915 switch (addr_ret->sa_family) {
916 case AF_INET:
917 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000918#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 case AF_INET6:
920 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000921#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 default:
923 PyErr_SetString(socket_error, "unknown address family");
924 return -1;
925 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000926}
927
Guido van Rossum30a685f1991-06-27 15:51:29 +0000928
Guido van Rossum30a685f1991-06-27 15:51:29 +0000929/* Create a string object representing an IP address.
930 This is always a string of the form 'dd.dd.dd.dd' (with variable
931 size numbers). */
932
Guido van Rossum73624e91994-10-10 17:59:00 +0000933static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000934makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 char buf[NI_MAXHOST];
937 int error;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
940 NI_NUMERICHOST);
941 if (error) {
942 set_gaierror(error);
943 return NULL;
944 }
945 return PyUnicode_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000946}
947
948
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000949#ifdef USE_BLUETOOTH
950/* Convert a string representation of a Bluetooth address into a numeric
951 address. Returns the length (6), or raises an exception and returns -1 if
952 an error occurred. */
953
954static int
955setbdaddr(char *name, bdaddr_t *bdaddr)
956{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 unsigned int b0, b1, b2, b3, b4, b5;
958 char ch;
959 int n;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
962 &b5, &b4, &b3, &b2, &b1, &b0, &ch);
963 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
964 bdaddr->b[0] = b0;
965 bdaddr->b[1] = b1;
966 bdaddr->b[2] = b2;
967 bdaddr->b[3] = b3;
968 bdaddr->b[4] = b4;
969 bdaddr->b[5] = b5;
970 return 6;
971 } else {
972 PyErr_SetString(socket_error, "bad bluetooth address");
973 return -1;
974 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000975}
976
977/* Create a string representation of the Bluetooth address. This is always a
978 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
979 value (zero padded if necessary). */
980
981static PyObject *
982makebdaddr(bdaddr_t *bdaddr)
983{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 char buf[(6 * 2) + 5 + 1];
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
987 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
988 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
989 return PyUnicode_FromString(buf);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000990}
991#endif
992
993
Guido van Rossum30a685f1991-06-27 15:51:29 +0000994/* Create an object representing the given socket address,
995 suitable for passing it back to bind(), connect() etc.
996 The family field of the sockaddr structure is inspected
997 to determine what kind of address it really is. */
998
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000999/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00001000static PyObject *
Antoine Pitrou19467d22010-08-17 19:33:30 +00001001makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001002{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 if (addrlen == 0) {
1004 /* No address -- may be recvfrom() from known socket */
1005 Py_INCREF(Py_None);
1006 return Py_None;
1007 }
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 switch (addr->sa_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +00001010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 case AF_INET:
1012 {
1013 struct sockaddr_in *a;
1014 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
1015 PyObject *ret = NULL;
1016 if (addrobj) {
1017 a = (struct sockaddr_in *)addr;
1018 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
1019 Py_DECREF(addrobj);
1020 }
1021 return ret;
1022 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00001023
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001024#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 case AF_UNIX:
1026 {
1027 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001028#ifdef linux
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 if (a->sun_path[0] == 0) { /* Linux abstract namespace */
1030 addrlen -= offsetof(struct sockaddr_un, sun_path);
1031 return PyBytes_FromStringAndSize(a->sun_path, addrlen);
1032 }
1033 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001034#endif /* linux */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 {
1036 /* regular NULL-terminated string */
1037 return PyUnicode_FromString(a->sun_path);
1038 }
1039 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001040#endif /* AF_UNIX */
1041
Martin v. Löwis11017b12006-01-14 18:12:57 +00001042#if defined(AF_NETLINK)
1043 case AF_NETLINK:
1044 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
1046 return Py_BuildValue("II", a->nl_pid, a->nl_groups);
Martin v. Löwis11017b12006-01-14 18:12:57 +00001047 }
1048#endif /* AF_NETLINK */
1049
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001050#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 case AF_INET6:
1052 {
1053 struct sockaddr_in6 *a;
1054 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
1055 PyObject *ret = NULL;
1056 if (addrobj) {
1057 a = (struct sockaddr_in6 *)addr;
1058 ret = Py_BuildValue("Oiii",
1059 addrobj,
1060 ntohs(a->sin6_port),
1061 a->sin6_flowinfo,
1062 a->sin6_scope_id);
1063 Py_DECREF(addrobj);
1064 }
1065 return ret;
1066 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001067#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001068
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001069#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 case AF_BLUETOOTH:
1071 switch (proto) {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 case BTPROTO_L2CAP:
1074 {
1075 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
1076 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
1077 PyObject *ret = NULL;
1078 if (addrobj) {
1079 ret = Py_BuildValue("Oi",
1080 addrobj,
1081 _BT_L2_MEMB(a, psm));
1082 Py_DECREF(addrobj);
1083 }
1084 return ret;
1085 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 case BTPROTO_RFCOMM:
1088 {
1089 struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
1090 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
1091 PyObject *ret = NULL;
1092 if (addrobj) {
1093 ret = Py_BuildValue("Oi",
1094 addrobj,
1095 _BT_RC_MEMB(a, channel));
1096 Py_DECREF(addrobj);
1097 }
1098 return ret;
1099 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 case BTPROTO_HCI:
1102 {
1103 struct sockaddr_hci *a = (struct sockaddr_hci *) addr;
1104 PyObject *ret = NULL;
1105 ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
1106 return ret;
1107 }
Thomas Wouterscf297e42007-02-23 15:07:44 +00001108
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001109#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 case BTPROTO_SCO:
1111 {
1112 struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
1113 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
1114 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001115#endif
1116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 default:
1118 PyErr_SetString(PyExc_ValueError,
1119 "Unknown Bluetooth protocol");
1120 return NULL;
1121 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001122#endif
1123
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001124#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 case AF_PACKET:
1126 {
1127 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
1128 char *ifname = "";
1129 struct ifreq ifr;
1130 /* need to look up interface name give index */
1131 if (a->sll_ifindex) {
1132 ifr.ifr_ifindex = a->sll_ifindex;
1133 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
1134 ifname = ifr.ifr_name;
1135 }
1136 return Py_BuildValue("shbhy#",
1137 ifname,
1138 ntohs(a->sll_protocol),
1139 a->sll_pkttype,
1140 a->sll_hatype,
1141 a->sll_addr,
1142 a->sll_halen);
1143 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001144#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001145
Christian Heimes043d6f62008-01-07 17:19:16 +00001146#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 case AF_TIPC:
1148 {
1149 struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr;
1150 if (a->addrtype == TIPC_ADDR_NAMESEQ) {
1151 return Py_BuildValue("IIIII",
1152 a->addrtype,
1153 a->addr.nameseq.type,
1154 a->addr.nameseq.lower,
1155 a->addr.nameseq.upper,
1156 a->scope);
1157 } else if (a->addrtype == TIPC_ADDR_NAME) {
1158 return Py_BuildValue("IIIII",
1159 a->addrtype,
1160 a->addr.name.name.type,
1161 a->addr.name.name.instance,
1162 a->addr.name.name.instance,
1163 a->scope);
1164 } else if (a->addrtype == TIPC_ADDR_ID) {
1165 return Py_BuildValue("IIIII",
1166 a->addrtype,
1167 a->addr.id.node,
1168 a->addr.id.ref,
1169 0,
1170 a->scope);
1171 } else {
1172 PyErr_SetString(PyExc_ValueError,
1173 "Invalid address type");
1174 return NULL;
1175 }
1176 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001177#endif
1178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 default:
1182 /* If we don't know the address family, don't raise an
1183 exception -- return it as an (int, bytes) tuple. */
1184 return Py_BuildValue("iy#",
1185 addr->sa_family,
1186 addr->sa_data,
1187 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001190}
1191
Guido van Rossum30a685f1991-06-27 15:51:29 +00001192
1193/* Parse a socket address argument according to the socket object's
1194 address family. Return 1 if the address was in the proper format,
1195 0 of not. The address is returned through addr_ret, its length
1196 through len_ret. */
1197
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001198static int
Guido van Rossum48a680c2001-03-02 06:34:14 +00001199getsockaddrarg(PySocketSockObject *s, PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 struct sockaddr *addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 switch (s->sock_family) {
Guido van Rossum30a685f1991-06-27 15:51:29 +00001203
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001204#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 case AF_UNIX:
1206 {
1207 struct sockaddr_un* addr;
1208 char *path;
1209 int len;
1210 if (!PyArg_Parse(args, "s#", &path, &len))
1211 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 addr = (struct sockaddr_un*)addr_ret;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001214#ifdef linux
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 if (len > 0 && path[0] == 0) {
1216 /* Linux abstract namespace extension */
1217 if (len > sizeof addr->sun_path) {
1218 PyErr_SetString(socket_error,
1219 "AF_UNIX path too long");
1220 return 0;
1221 }
1222 }
1223 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001224#endif /* linux */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 {
1226 /* regular NULL-terminated string */
1227 if (len >= sizeof addr->sun_path) {
1228 PyErr_SetString(socket_error,
1229 "AF_UNIX path too long");
1230 return 0;
1231 }
1232 addr->sun_path[len] = 0;
1233 }
1234 addr->sun_family = s->sock_family;
1235 memcpy(addr->sun_path, path, len);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001236#if defined(PYOS_OS2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 *len_ret = sizeof(*addr);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001238#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 *len_ret = len + offsetof(struct sockaddr_un, sun_path);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001240#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 return 1;
1242 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001243#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001244
Martin v. Löwis11017b12006-01-14 18:12:57 +00001245#if defined(AF_NETLINK)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 case AF_NETLINK:
1247 {
1248 struct sockaddr_nl* addr;
1249 int pid, groups;
1250 addr = (struct sockaddr_nl *)addr_ret;
1251 if (!PyTuple_Check(args)) {
1252 PyErr_Format(
1253 PyExc_TypeError,
1254 "getsockaddrarg: "
1255 "AF_NETLINK address must be tuple, not %.500s",
1256 Py_TYPE(args)->tp_name);
1257 return 0;
1258 }
1259 if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
1260 return 0;
1261 addr->nl_family = AF_NETLINK;
1262 addr->nl_pid = pid;
1263 addr->nl_groups = groups;
1264 *len_ret = sizeof(*addr);
1265 return 1;
1266 }
Martin v. Löwis11017b12006-01-14 18:12:57 +00001267#endif
1268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 case AF_INET:
1270 {
1271 struct sockaddr_in* addr;
1272 char *host;
1273 int port, result;
1274 if (!PyTuple_Check(args)) {
1275 PyErr_Format(
1276 PyExc_TypeError,
1277 "getsockaddrarg: "
1278 "AF_INET address must be tuple, not %.500s",
1279 Py_TYPE(args)->tp_name);
1280 return 0;
1281 }
1282 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
1283 "idna", &host, &port))
1284 return 0;
1285 addr=(struct sockaddr_in*)addr_ret;
1286 result = setipaddr(host, (struct sockaddr *)addr,
1287 sizeof(*addr), AF_INET);
1288 PyMem_Free(host);
1289 if (result < 0)
1290 return 0;
1291 if (port < 0 || port > 0xffff) {
1292 PyErr_SetString(
1293 PyExc_OverflowError,
1294 "getsockaddrarg: port must be 0-65535.");
1295 return 0;
1296 }
1297 addr->sin_family = AF_INET;
1298 addr->sin_port = htons((short)port);
1299 *len_ret = sizeof *addr;
1300 return 1;
1301 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001302
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001303#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 case AF_INET6:
1305 {
1306 struct sockaddr_in6* addr;
1307 char *host;
1308 int port, flowinfo, scope_id, result;
1309 flowinfo = scope_id = 0;
1310 if (!PyTuple_Check(args)) {
1311 PyErr_Format(
1312 PyExc_TypeError,
1313 "getsockaddrarg: "
1314 "AF_INET6 address must be tuple, not %.500s",
1315 Py_TYPE(args)->tp_name);
1316 return 0;
1317 }
1318 if (!PyArg_ParseTuple(args, "eti|ii",
1319 "idna", &host, &port, &flowinfo,
1320 &scope_id)) {
1321 return 0;
1322 }
1323 addr = (struct sockaddr_in6*)addr_ret;
1324 result = setipaddr(host, (struct sockaddr *)addr,
1325 sizeof(*addr), AF_INET6);
1326 PyMem_Free(host);
1327 if (result < 0)
1328 return 0;
1329 if (port < 0 || port > 0xffff) {
1330 PyErr_SetString(
1331 PyExc_OverflowError,
1332 "getsockaddrarg: port must be 0-65535.");
1333 return 0;
1334 }
1335 addr->sin6_family = s->sock_family;
1336 addr->sin6_port = htons((short)port);
1337 addr->sin6_flowinfo = flowinfo;
1338 addr->sin6_scope_id = scope_id;
1339 *len_ret = sizeof *addr;
1340 return 1;
1341 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001342#endif
1343
Hye-Shik Chang81268602004-02-02 06:05:24 +00001344#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 case AF_BLUETOOTH:
1346 {
1347 switch (s->sock_proto) {
1348 case BTPROTO_L2CAP:
1349 {
1350 struct sockaddr_l2 *addr;
1351 char *straddr;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 addr = (struct sockaddr_l2 *)addr_ret;
1354 memset(addr, 0, sizeof(struct sockaddr_l2));
1355 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
1356 if (!PyArg_ParseTuple(args, "si", &straddr,
1357 &_BT_L2_MEMB(addr, psm))) {
1358 PyErr_SetString(socket_error, "getsockaddrarg: "
1359 "wrong format");
1360 return 0;
1361 }
1362 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
1363 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 *len_ret = sizeof *addr;
1366 return 1;
1367 }
1368 case BTPROTO_RFCOMM:
1369 {
1370 struct sockaddr_rc *addr;
1371 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 addr = (struct sockaddr_rc *)addr_ret;
1374 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
1375 if (!PyArg_ParseTuple(args, "si", &straddr,
1376 &_BT_RC_MEMB(addr, channel))) {
1377 PyErr_SetString(socket_error, "getsockaddrarg: "
1378 "wrong format");
1379 return 0;
1380 }
1381 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
1382 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 *len_ret = sizeof *addr;
1385 return 1;
1386 }
1387 case BTPROTO_HCI:
1388 {
1389 struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret;
1390 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
1391 if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) {
1392 PyErr_SetString(socket_error, "getsockaddrarg: "
1393 "wrong format");
1394 return 0;
1395 }
1396 *len_ret = sizeof *addr;
1397 return 1;
1398 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001399#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 case BTPROTO_SCO:
1401 {
1402 struct sockaddr_sco *addr;
1403 char *straddr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 addr = (struct sockaddr_sco *)addr_ret;
1406 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
1407 if (!PyBytes_Check(args)) {
1408 PyErr_SetString(socket_error, "getsockaddrarg: "
1409 "wrong format");
1410 return 0;
1411 }
1412 straddr = PyBytes_AS_STRING(args);
1413 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
1414 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 *len_ret = sizeof *addr;
1417 return 1;
1418 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001419#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 default:
1421 PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol");
1422 return 0;
1423 }
1424 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001425#endif
1426
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001427#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 case AF_PACKET:
1429 {
1430 struct sockaddr_ll* addr;
1431 struct ifreq ifr;
1432 char *interfaceName;
1433 int protoNumber;
1434 int hatype = 0;
1435 int pkttype = 0;
1436 char *haddr = NULL;
1437 unsigned int halen = 0;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 if (!PyTuple_Check(args)) {
1440 PyErr_Format(
1441 PyExc_TypeError,
1442 "getsockaddrarg: "
1443 "AF_PACKET address must be tuple, not %.500s",
1444 Py_TYPE(args)->tp_name);
1445 return 0;
1446 }
1447 if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName,
1448 &protoNumber, &pkttype, &hatype,
1449 &haddr, &halen))
1450 return 0;
1451 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1452 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
1453 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
1454 s->errorhandler();
1455 return 0;
1456 }
1457 if (halen > 8) {
1458 PyErr_SetString(PyExc_ValueError,
1459 "Hardware address must be 8 bytes or less");
1460 return 0;
1461 }
1462 if (protoNumber < 0 || protoNumber > 0xffff) {
1463 PyErr_SetString(
1464 PyExc_OverflowError,
1465 "getsockaddrarg: protoNumber must be 0-65535.");
1466 return 0;
1467 }
1468 addr = (struct sockaddr_ll*)addr_ret;
1469 addr->sll_family = AF_PACKET;
1470 addr->sll_protocol = htons((short)protoNumber);
1471 addr->sll_ifindex = ifr.ifr_ifindex;
1472 addr->sll_pkttype = pkttype;
1473 addr->sll_hatype = hatype;
1474 if (halen != 0) {
1475 memcpy(&addr->sll_addr, haddr, halen);
1476 }
1477 addr->sll_halen = halen;
1478 *len_ret = sizeof *addr;
1479 return 1;
1480 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001481#endif
1482
Christian Heimes043d6f62008-01-07 17:19:16 +00001483#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 case AF_TIPC:
1485 {
1486 unsigned int atype, v1, v2, v3;
1487 unsigned int scope = TIPC_CLUSTER_SCOPE;
1488 struct sockaddr_tipc *addr;
Christian Heimes043d6f62008-01-07 17:19:16 +00001489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 if (!PyTuple_Check(args)) {
1491 PyErr_Format(
1492 PyExc_TypeError,
1493 "getsockaddrarg: "
1494 "AF_TIPC address must be tuple, not %.500s",
1495 Py_TYPE(args)->tp_name);
1496 return 0;
1497 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 if (!PyArg_ParseTuple(args,
1500 "IIII|I;Invalid TIPC address format",
1501 &atype, &v1, &v2, &v3, &scope))
1502 return 0;
Christian Heimes043d6f62008-01-07 17:19:16 +00001503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 addr = (struct sockaddr_tipc *) addr_ret;
1505 memset(addr, 0, sizeof(struct sockaddr_tipc));
Christian Heimes043d6f62008-01-07 17:19:16 +00001506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 addr->family = AF_TIPC;
1508 addr->scope = scope;
1509 addr->addrtype = atype;
Christian Heimes043d6f62008-01-07 17:19:16 +00001510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 if (atype == TIPC_ADDR_NAMESEQ) {
1512 addr->addr.nameseq.type = v1;
1513 addr->addr.nameseq.lower = v2;
1514 addr->addr.nameseq.upper = v3;
1515 } else if (atype == TIPC_ADDR_NAME) {
1516 addr->addr.name.name.type = v1;
1517 addr->addr.name.name.instance = v2;
1518 } else if (atype == TIPC_ADDR_ID) {
1519 addr->addr.id.node = v1;
1520 addr->addr.id.ref = v2;
1521 } else {
1522 /* Shouldn't happen */
1523 PyErr_SetString(PyExc_TypeError, "Invalid address type");
1524 return 0;
1525 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 *len_ret = sizeof(*addr);
Christian Heimes043d6f62008-01-07 17:19:16 +00001528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 return 1;
1530 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001531#endif
1532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 /* More cases here... */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 default:
1536 PyErr_SetString(socket_error, "getsockaddrarg: bad family");
1537 return 0;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001540}
1541
Guido van Rossum30a685f1991-06-27 15:51:29 +00001542
Guido van Rossum48a680c2001-03-02 06:34:14 +00001543/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00001544 Return 1 if the family is known, 0 otherwise. The length is returned
1545 through len_ret. */
1546
1547static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00001548getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 switch (s->sock_family) {
Guido van Rossum710e1df1992-06-12 10:39:36 +00001551
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001552#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 case AF_UNIX:
1554 {
1555 *len_ret = sizeof (struct sockaddr_un);
1556 return 1;
1557 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001558#endif /* AF_UNIX */
Martin v. Löwis11017b12006-01-14 18:12:57 +00001559#if defined(AF_NETLINK)
1560 case AF_NETLINK:
1561 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 *len_ret = sizeof (struct sockaddr_nl);
1563 return 1;
Martin v. Löwis11017b12006-01-14 18:12:57 +00001564 }
1565#endif
Guido van Rossum710e1df1992-06-12 10:39:36 +00001566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 case AF_INET:
1568 {
1569 *len_ret = sizeof (struct sockaddr_in);
1570 return 1;
1571 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00001572
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001573#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 case AF_INET6:
1575 {
1576 *len_ret = sizeof (struct sockaddr_in6);
1577 return 1;
1578 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001579#endif
1580
Hye-Shik Chang81268602004-02-02 06:05:24 +00001581#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 case AF_BLUETOOTH:
1583 {
1584 switch(s->sock_proto)
1585 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 case BTPROTO_L2CAP:
1588 *len_ret = sizeof (struct sockaddr_l2);
1589 return 1;
1590 case BTPROTO_RFCOMM:
1591 *len_ret = sizeof (struct sockaddr_rc);
1592 return 1;
1593 case BTPROTO_HCI:
1594 *len_ret = sizeof (struct sockaddr_hci);
1595 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001596#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 case BTPROTO_SCO:
1598 *len_ret = sizeof (struct sockaddr_sco);
1599 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001600#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 default:
1602 PyErr_SetString(socket_error, "getsockaddrlen: "
1603 "unknown BT protocol");
1604 return 0;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 }
1607 }
Martin v. Löwis12af0482004-01-31 12:34:17 +00001608#endif
1609
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001610#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 case AF_PACKET:
1612 {
1613 *len_ret = sizeof (struct sockaddr_ll);
1614 return 1;
1615 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001616#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001617
Christian Heimes043d6f62008-01-07 17:19:16 +00001618#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 case AF_TIPC:
1620 {
1621 *len_ret = sizeof (struct sockaddr_tipc);
1622 return 1;
1623 }
Christian Heimes043d6f62008-01-07 17:19:16 +00001624#endif
1625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 /* More cases here... */
Guido van Rossum710e1df1992-06-12 10:39:36 +00001627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 default:
1629 PyErr_SetString(socket_error, "getsockaddrlen: bad family");
1630 return 0;
Guido van Rossum710e1df1992-06-12 10:39:36 +00001631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 }
Guido van Rossum710e1df1992-06-12 10:39:36 +00001633}
1634
1635
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001636/* s._accept() -> (fd, address) */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001637
Guido van Rossum73624e91994-10-10 17:59:00 +00001638static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001639sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 sock_addr_t addrbuf;
1642 SOCKET_T newfd = INVALID_SOCKET;
1643 socklen_t addrlen;
1644 PyObject *sock = NULL;
1645 PyObject *addr = NULL;
1646 PyObject *res = NULL;
1647 int timeout;
Barry Warsaw752300b1997-01-03 17:18:10 +00001648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 if (!getsockaddrlen(s, &addrlen))
1650 return NULL;
1651 memset(&addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 if (!IS_SELECTABLE(s))
1654 return select_error();
Neal Norwitz082b2df2006-02-07 07:04:46 +00001655
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00001656 BEGIN_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00001658 timeout = internal_select_ex(s, 0, interval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 if (!timeout)
1660 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
1661 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 if (timeout == 1) {
1664 PyErr_SetString(socket_timeout, "timed out");
1665 return NULL;
1666 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00001667 END_SELECT_LOOP(s)
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 if (newfd == INVALID_SOCKET)
1670 return s->errorhandler();
Barry Warsaw752300b1997-01-03 17:18:10 +00001671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 sock = PyLong_FromSocket_t(newfd);
1673 if (sock == NULL) {
1674 SOCKETCLOSE(newfd);
1675 goto finally;
1676 }
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
1679 addrlen, s->sock_proto);
1680 if (addr == NULL)
1681 goto finally;
Barry Warsaw752300b1997-01-03 17:18:10 +00001682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 res = PyTuple_Pack(2, sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001684
Guido van Rossum67f7a382002-06-06 21:08:16 +00001685finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 Py_XDECREF(sock);
1687 Py_XDECREF(addr);
1688 return res;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001689}
1690
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001691PyDoc_STRVAR(accept_doc,
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001692"_accept() -> (integer, address info)\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001693\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001694Wait for an incoming connection. Return a new socket file descriptor\n\
1695representing the connection, and the address of the client.\n\
1696For IP sockets, the address info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001697
Guido van Rossum11ba0942002-06-13 15:07:44 +00001698/* s.setblocking(flag) method. Argument:
1699 False -- non-blocking mode; same as settimeout(0)
1700 True -- blocking mode; same as settimeout(None)
1701*/
Guido van Rossume4485b01994-09-07 14:32:49 +00001702
Guido van Rossum73624e91994-10-10 17:59:00 +00001703static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001704sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00001705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 int block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 block = PyLong_AsLong(arg);
1709 if (block == -1 && PyErr_Occurred())
1710 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 s->sock_timeout = block ? -1.0 : 0.0;
1713 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00001714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 Py_INCREF(Py_None);
1716 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00001717}
Guido van Rossume4485b01994-09-07 14:32:49 +00001718
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001719PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001720"setblocking(flag)\n\
1721\n\
1722Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001723setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001724setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001725
Guido van Rossum11ba0942002-06-13 15:07:44 +00001726/* s.settimeout(timeout) method. Argument:
1727 None -- no timeout, blocking mode; same as setblocking(True)
1728 0.0 -- non-blocking mode; same as setblocking(False)
1729 > 0 -- timeout mode; operations time out after timeout seconds
1730 < 0 -- illegal; raises an exception
1731*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00001732static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001733sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 double timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 if (arg == Py_None)
1738 timeout = -1.0;
1739 else {
1740 timeout = PyFloat_AsDouble(arg);
1741 if (timeout < 0.0) {
1742 if (!PyErr_Occurred())
1743 PyErr_SetString(PyExc_ValueError,
1744 "Timeout value out of range");
1745 return NULL;
1746 }
1747 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00001748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 s->sock_timeout = timeout;
1750 internal_setblocking(s, timeout < 0.0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 Py_INCREF(Py_None);
1753 return Py_None;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001754}
1755
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001756PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001757"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001758\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001759Set a timeout on socket operations. 'timeout' can be a float,\n\
1760giving in seconds, or None. Setting a timeout of None disables\n\
1761the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001762Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001763
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001764/* s.gettimeout() method.
1765 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001766static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001767sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 if (s->sock_timeout < 0.0) {
1770 Py_INCREF(Py_None);
1771 return Py_None;
1772 }
1773 else
1774 return PyFloat_FromDouble(s->sock_timeout);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001775}
1776
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001777PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001778"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001779\n\
1780Returns the timeout in floating seconds associated with socket \n\
1781operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001782operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00001783
Guido van Rossumaee08791992-09-08 09:05:33 +00001784/* s.setsockopt() method.
1785 With an integer third argument, sets an integer option.
1786 With a string third argument, sets an option from a buffer;
1787 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001788
Guido van Rossum73624e91994-10-10 17:59:00 +00001789static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001790sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 int level;
1793 int optname;
1794 int res;
1795 char *buf;
1796 int buflen;
1797 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 if (PyArg_ParseTuple(args, "iii:setsockopt",
1800 &level, &optname, &flag)) {
1801 buf = (char *) &flag;
1802 buflen = sizeof flag;
1803 }
1804 else {
1805 PyErr_Clear();
1806 if (!PyArg_ParseTuple(args, "iiy#:setsockopt",
1807 &level, &optname, &buf, &buflen))
1808 return NULL;
1809 }
1810 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
1811 if (res < 0)
1812 return s->errorhandler();
1813 Py_INCREF(Py_None);
1814 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001815}
1816
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001817PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001818"setsockopt(level, option, value)\n\
1819\n\
1820Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001821The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001822
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001823
Guido van Rossumaee08791992-09-08 09:05:33 +00001824/* s.getsockopt() method.
1825 With two arguments, retrieves an integer option.
1826 With a third integer argument, retrieves a string buffer of that size;
1827 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001828
Guido van Rossum73624e91994-10-10 17:59:00 +00001829static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001830sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001831{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 int level;
1833 int optname;
1834 int res;
1835 PyObject *buf;
1836 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
1839 &level, &optname, &buflen))
1840 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 if (buflen == 0) {
1843 int flag = 0;
1844 socklen_t flagsize = sizeof flag;
1845 res = getsockopt(s->sock_fd, level, optname,
1846 (void *)&flag, &flagsize);
1847 if (res < 0)
1848 return s->errorhandler();
1849 return PyLong_FromLong(flag);
1850 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001851#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 /* socklen_t is unsigned so no negative test is needed,
1853 test buflen == 0 is previously done */
1854 if (buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001855#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 if (buflen <= 0 || buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001857#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 PyErr_SetString(socket_error,
1859 "getsockopt buflen out of range");
1860 return NULL;
1861 }
1862 buf = PyBytes_FromStringAndSize((char *)NULL, buflen);
1863 if (buf == NULL)
1864 return NULL;
1865 res = getsockopt(s->sock_fd, level, optname,
1866 (void *)PyBytes_AS_STRING(buf), &buflen);
1867 if (res < 0) {
1868 Py_DECREF(buf);
1869 return s->errorhandler();
1870 }
1871 _PyBytes_Resize(&buf, buflen);
1872 return buf;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001873}
1874
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001875PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001876"getsockopt(level, option[, buffersize]) -> value\n\
1877\n\
1878Get a socket option. See the Unix manual for level and option.\n\
1879If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001880string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001881
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001882
Fred Drake728819a2000-07-01 03:40:12 +00001883/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001884
Guido van Rossum73624e91994-10-10 17:59:00 +00001885static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001886sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 sock_addr_t addrbuf;
1889 int addrlen;
1890 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
1893 return NULL;
1894 Py_BEGIN_ALLOW_THREADS
1895 res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);
1896 Py_END_ALLOW_THREADS
1897 if (res < 0)
1898 return s->errorhandler();
1899 Py_INCREF(Py_None);
1900 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001901}
1902
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001903PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001904"bind(address)\n\
1905\n\
1906Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00001907pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001908sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001909
Guido van Rossum30a685f1991-06-27 15:51:29 +00001910
1911/* s.close() method.
1912 Set the file descriptor to -1 so operations tried subsequently
1913 will surely fail. */
1914
Guido van Rossum73624e91994-10-10 17:59:00 +00001915static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001916sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 if ((fd = s->sock_fd) != -1) {
1921 s->sock_fd = -1;
1922 Py_BEGIN_ALLOW_THREADS
1923 (void) SOCKETCLOSE(fd);
1924 Py_END_ALLOW_THREADS
1925 }
1926 Py_INCREF(Py_None);
1927 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001928}
1929
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001930PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001931"close()\n\
1932\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001933Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001934
Antoine Pitroue43f9d02010-08-08 23:24:50 +00001935static PyObject *
Antoine Pitrou6e451df2010-08-09 20:39:54 +00001936sock_detach(PySocketSockObject *s)
Antoine Pitroue43f9d02010-08-08 23:24:50 +00001937{
Antoine Pitrou6e451df2010-08-09 20:39:54 +00001938 SOCKET_T fd = s->sock_fd;
Antoine Pitroue43f9d02010-08-08 23:24:50 +00001939 s->sock_fd = -1;
Antoine Pitrou6e451df2010-08-09 20:39:54 +00001940 return PyLong_FromSocket_t(fd);
Antoine Pitroue43f9d02010-08-08 23:24:50 +00001941}
1942
Antoine Pitrou6e451df2010-08-09 20:39:54 +00001943PyDoc_STRVAR(detach_doc,
1944"detach()\n\
Antoine Pitroue43f9d02010-08-08 23:24:50 +00001945\n\
1946Close the socket object without closing the underlying file descriptor.\
1947The object cannot be used after this call, but the file descriptor\
Antoine Pitrou6e451df2010-08-09 20:39:54 +00001948can be reused for other purposes. The file descriptor is returned.");
Antoine Pitroue43f9d02010-08-08 23:24:50 +00001949
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001950static int
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001951internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 int *timeoutp)
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 int res, timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 timeout = 0;
1957 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001958
1959#ifdef MS_WINDOWS
1960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 if (s->sock_timeout > 0.0) {
1962 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK &&
1963 IS_SELECTABLE(s)) {
1964 /* This is a mess. Best solution: trust select */
1965 fd_set fds;
1966 fd_set fds_exc;
1967 struct timeval tv;
1968 tv.tv_sec = (int)s->sock_timeout;
1969 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
1970 FD_ZERO(&fds);
1971 FD_SET(s->sock_fd, &fds);
1972 FD_ZERO(&fds_exc);
1973 FD_SET(s->sock_fd, &fds_exc);
Antoine Pitrou19467d22010-08-17 19:33:30 +00001974 res = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int),
1975 NULL, &fds, &fds_exc, &tv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 if (res == 0) {
1977 res = WSAEWOULDBLOCK;
1978 timeout = 1;
1979 } else if (res > 0) {
1980 if (FD_ISSET(s->sock_fd, &fds))
1981 /* The socket is in the writable set - this
1982 means connected */
1983 res = 0;
1984 else {
1985 /* As per MS docs, we need to call getsockopt()
1986 to get the underlying error */
1987 int res_size = sizeof res;
1988 /* It must be in the exception set */
1989 assert(FD_ISSET(s->sock_fd, &fds_exc));
1990 if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
1991 (char *)&res, &res_size))
1992 /* getsockopt also clears WSAGetLastError,
1993 so reset it back. */
1994 WSASetLastError(res);
1995 else
1996 res = WSAGetLastError();
1997 }
1998 }
1999 /* else if (res < 0) an error occurred */
2000 }
2001 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 if (res < 0)
2004 res = WSAGetLastError();
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002005
2006#else
2007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 if (s->sock_timeout > 0.0) {
2009 if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) {
2010 timeout = internal_select(s, 1);
2011 if (timeout == 0) {
2012 /* Bug #1019808: in case of an EINPROGRESS,
2013 use getsockopt(SO_ERROR) to get the real
2014 error. */
2015 socklen_t res_size = sizeof res;
2016 (void)getsockopt(s->sock_fd, SOL_SOCKET,
2017 SO_ERROR, &res, &res_size);
2018 if (res == EISCONN)
2019 res = 0;
2020 errno = res;
2021 }
2022 else if (timeout == -1) {
2023 res = errno; /* had error */
2024 }
2025 else
2026 res = EWOULDBLOCK; /* timed out */
2027 }
2028 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 if (res < 0)
2031 res = errno;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002032
2033#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 *timeoutp = timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 return res;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00002037}
Guido van Rossum30a685f1991-06-27 15:51:29 +00002038
Fred Drake728819a2000-07-01 03:40:12 +00002039/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002040
Guido van Rossum73624e91994-10-10 17:59:00 +00002041static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002042sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002043{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 sock_addr_t addrbuf;
2045 int addrlen;
2046 int res;
2047 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2050 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 Py_BEGIN_ALLOW_THREADS
2053 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
2054 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 if (timeout == 1) {
2057 PyErr_SetString(socket_timeout, "timed out");
2058 return NULL;
2059 }
2060 if (res != 0)
2061 return s->errorhandler();
2062 Py_INCREF(Py_None);
2063 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002064}
2065
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002066PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002067"connect(address)\n\
2068\n\
2069Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002070is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002071
Guido van Rossum30a685f1991-06-27 15:51:29 +00002072
Fred Drake728819a2000-07-01 03:40:12 +00002073/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002074
2075static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002076sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 sock_addr_t addrbuf;
2079 int addrlen;
2080 int res;
2081 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2084 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 Py_BEGIN_ALLOW_THREADS
2087 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
2088 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 /* Signals are not errors (though they may raise exceptions). Adapted
2091 from PyErr_SetFromErrnoWithFilenameObject(). */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002092#ifdef EINTR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 if (res == EINTR && PyErr_CheckSignals())
2094 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002095#endif
2096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 return PyLong_FromLong((long) res);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002098}
2099
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002100PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002101"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002102\n\
2103This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002104instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002105
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002106
Guido van Rossumed233a51992-06-23 09:07:03 +00002107/* s.fileno() method */
2108
Guido van Rossum73624e91994-10-10 17:59:00 +00002109static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002110sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00002111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 return PyLong_FromSocket_t(s->sock_fd);
Guido van Rossumed233a51992-06-23 09:07:03 +00002113}
2114
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002115PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002116"fileno() -> integer\n\
2117\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002118Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002119
Guido van Rossumed233a51992-06-23 09:07:03 +00002120
Guido van Rossumc89705d1992-11-26 08:54:07 +00002121/* s.getsockname() method */
2122
Guido van Rossum73624e91994-10-10 17:59:00 +00002123static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002124sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 sock_addr_t addrbuf;
2127 int res;
2128 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 if (!getsockaddrlen(s, &addrlen))
2131 return NULL;
2132 memset(&addrbuf, 0, addrlen);
2133 Py_BEGIN_ALLOW_THREADS
2134 res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2135 Py_END_ALLOW_THREADS
2136 if (res < 0)
2137 return s->errorhandler();
2138 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2139 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002140}
2141
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002142PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002143"getsockname() -> address info\n\
2144\n\
2145Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002146info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002147
Guido van Rossumc89705d1992-11-26 08:54:07 +00002148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002150/* s.getpeername() method */
2151
Guido van Rossum73624e91994-10-10 17:59:00 +00002152static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002153sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002154{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 sock_addr_t addrbuf;
2156 int res;
2157 socklen_t addrlen;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 if (!getsockaddrlen(s, &addrlen))
2160 return NULL;
2161 memset(&addrbuf, 0, addrlen);
2162 Py_BEGIN_ALLOW_THREADS
2163 res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
2164 Py_END_ALLOW_THREADS
2165 if (res < 0)
2166 return s->errorhandler();
2167 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
2168 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002169}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002170
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002171PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002172"getpeername() -> address info\n\
2173\n\
2174Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002175info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002176
Guido van Rossumb6775db1994-08-01 11:34:53 +00002177#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002178
2179
Guido van Rossum30a685f1991-06-27 15:51:29 +00002180/* s.listen(n) method */
2181
Guido van Rossum73624e91994-10-10 17:59:00 +00002182static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002183sock_listen(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 int backlog;
2186 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 backlog = PyLong_AsLong(arg);
2189 if (backlog == -1 && PyErr_Occurred())
2190 return NULL;
2191 Py_BEGIN_ALLOW_THREADS
2192 if (backlog < 1)
2193 backlog = 1;
2194 res = listen(s->sock_fd, backlog);
2195 Py_END_ALLOW_THREADS
2196 if (res < 0)
2197 return s->errorhandler();
2198 Py_INCREF(Py_None);
2199 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002200}
2201
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002202PyDoc_STRVAR(listen_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002203"listen(backlog)\n\
2204\n\
2205Enable a server to accept connections. The backlog argument must be at\n\
2206least 1; it specifies the number of unaccepted connection that the system\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002207will allow before refusing new connections.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002208
2209
Thomas Wouters477c8d52006-05-27 19:21:47 +00002210/*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002211 * This is the guts of the recv() and recv_into() methods, which reads into a
Thomas Wouters902d6eb2007-01-09 23:18:33 +00002212 * char buffer. If you have any inc/dec ref to do to the objects that contain
Thomas Wouters477c8d52006-05-27 19:21:47 +00002213 * the buffer, do it in the caller. This function returns the number of bytes
2214 * succesfully read. If there was an error, it returns -1. Note that it is
2215 * also possible that we return a number of bytes smaller than the request
2216 * bytes.
2217 */
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002218
Antoine Pitrou19467d22010-08-17 19:33:30 +00002219static Py_ssize_t
2220sock_recv_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002221{
Antoine Pitrou19467d22010-08-17 19:33:30 +00002222 Py_ssize_t outlen = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 int timeout;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002224#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 int remaining;
2226 char *read_buf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002227#endif
2228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 if (!IS_SELECTABLE(s)) {
2230 select_error();
2231 return -1;
2232 }
2233 if (len == 0) {
2234 /* If 0 bytes were requested, do nothing. */
2235 return 0;
2236 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002237
2238#ifndef __VMS
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002239 BEGIN_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002240 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002241 timeout = internal_select_ex(s, 0, interval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 if (!timeout)
2243 outlen = recv(s->sock_fd, cbuf, len, flags);
2244 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 if (timeout == 1) {
2247 PyErr_SetString(socket_timeout, "timed out");
2248 return -1;
2249 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002250 END_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 if (outlen < 0) {
2252 /* Note: the call to errorhandler() ALWAYS indirectly returned
2253 NULL, so ignore its return value */
2254 s->errorhandler();
2255 return -1;
2256 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002257#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 read_buf = cbuf;
2259 remaining = len;
2260 while (remaining != 0) {
2261 unsigned int segment;
2262 int nread = -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 segment = remaining /SEGMENT_SIZE;
2265 if (segment != 0) {
2266 segment = SEGMENT_SIZE;
2267 }
2268 else {
2269 segment = remaining;
2270 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002271
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002272 BEGIN_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002274 timeout = internal_select_ex(s, 0, interval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 if (!timeout)
2276 nread = recv(s->sock_fd, read_buf, segment, flags);
2277 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 if (timeout == 1) {
2279 PyErr_SetString(socket_timeout, "timed out");
2280 return -1;
2281 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002282 END_SELECT_LOOP(s)
2283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 if (nread < 0) {
2285 s->errorhandler();
2286 return -1;
2287 }
2288 if (nread != remaining) {
2289 read_buf += nread;
2290 break;
2291 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 remaining -= segment;
2294 read_buf += segment;
2295 }
2296 outlen = read_buf - cbuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002297#endif /* !__VMS */
2298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 return outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002300}
2301
Guido van Rossum48a680c2001-03-02 06:34:14 +00002302
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002303/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002304
Guido van Rossum73624e91994-10-10 17:59:00 +00002305static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002306sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002307{
Antoine Pitrou19467d22010-08-17 19:33:30 +00002308 Py_ssize_t recvlen, outlen;
2309 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 PyObject *buf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002311
Antoine Pitrou19467d22010-08-17 19:33:30 +00002312 if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 if (recvlen < 0) {
2316 PyErr_SetString(PyExc_ValueError,
2317 "negative buffersize in recv");
2318 return NULL;
2319 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 /* Allocate a new string. */
2322 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2323 if (buf == NULL)
2324 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 /* Call the guts */
2327 outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags);
2328 if (outlen < 0) {
2329 /* An error occurred, release the string and return an
2330 error. */
2331 Py_DECREF(buf);
2332 return NULL;
2333 }
2334 if (outlen != recvlen) {
2335 /* We did not read as many bytes as we anticipated, resize the
2336 string if possible and be successful. */
2337 _PyBytes_Resize(&buf, outlen);
2338 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 return buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002341}
2342
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002343PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002344"recv(buffersize[, flags]) -> data\n\
2345\n\
2346Receive up to buffersize bytes from the socket. For the optional flags\n\
2347argument, see the Unix manual. When no data is available, block until\n\
2348at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002349the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002350
Guido van Rossum30a685f1991-06-27 15:51:29 +00002351
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002352/* s.recv_into(buffer, [nbytes [,flags]]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002353
Thomas Wouters477c8d52006-05-27 19:21:47 +00002354static PyObject*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002355sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00002358
Antoine Pitrou19467d22010-08-17 19:33:30 +00002359 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 Py_buffer pbuf;
2361 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002362 Py_ssize_t buflen, readlen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 /* Get the buffer's memory */
Antoine Pitrou19467d22010-08-17 19:33:30 +00002365 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recv_into", kwlist,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 &pbuf, &recvlen, &flags))
2367 return NULL;
2368 buf = pbuf.buf;
2369 buflen = pbuf.len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 if (recvlen < 0) {
2372 PyBuffer_Release(&pbuf);
2373 PyErr_SetString(PyExc_ValueError,
2374 "negative buffersize in recv_into");
2375 return NULL;
2376 }
2377 if (recvlen == 0) {
2378 /* If nbytes was not specified, use the buffer's length */
2379 recvlen = buflen;
2380 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 /* Check if the buffer is large enough */
2383 if (buflen < recvlen) {
2384 PyBuffer_Release(&pbuf);
2385 PyErr_SetString(PyExc_ValueError,
2386 "buffer too small for requested bytes");
2387 return NULL;
2388 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 /* Call the guts */
2391 readlen = sock_recv_guts(s, buf, recvlen, flags);
2392 if (readlen < 0) {
2393 /* Return an error. */
2394 PyBuffer_Release(&pbuf);
2395 return NULL;
2396 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 PyBuffer_Release(&pbuf);
2399 /* Return the number of bytes read. Note that we do not do anything
2400 special here in the case that readlen < recvlen. */
2401 return PyLong_FromSsize_t(readlen);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002402}
2403
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002404PyDoc_STRVAR(recv_into_doc,
2405"recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002406\n\
2407A version of recv() that stores its data into a buffer rather than creating \n\
2408a new string. Receive up to buffersize bytes from the socket. If buffersize \n\
2409is not specified (or 0), receive up to the size available in the given buffer.\n\
2410\n\
2411See recv() for documentation about the flags.");
2412
2413
2414/*
Christian Heimes99170a52007-12-19 02:07:34 +00002415 * This is the guts of the recvfrom() and recvfrom_into() methods, which reads
2416 * into a char buffer. If you have any inc/def ref to do to the objects that
2417 * contain the buffer, do it in the caller. This function returns the number
2418 * of bytes succesfully read. If there was an error, it returns -1. Note
2419 * that it is also possible that we return a number of bytes smaller than the
2420 * request bytes.
Thomas Wouters477c8d52006-05-27 19:21:47 +00002421 *
2422 * 'addr' is a return value for the address object. Note that you must decref
2423 * it yourself.
2424 */
Antoine Pitrou19467d22010-08-17 19:33:30 +00002425static Py_ssize_t
2426sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 PyObject** addr)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 sock_addr_t addrbuf;
2430 int timeout;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002431 Py_ssize_t n = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 *addr = NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 if (!getsockaddrlen(s, &addrlen))
2437 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 if (!IS_SELECTABLE(s)) {
2440 select_error();
2441 return -1;
2442 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002443
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002444 BEGIN_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 Py_BEGIN_ALLOW_THREADS
2446 memset(&addrbuf, 0, addrlen);
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002447 timeout = internal_select_ex(s, 0, interval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 if (!timeout) {
Guido van Rossum8d665e61996-06-26 18:22:49 +00002449#ifndef MS_WINDOWS
Andrew MacIntyreba43e872002-03-03 03:03:52 +00002450#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 n = recvfrom(s->sock_fd, cbuf, len, flags,
2452 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum32c575d1997-12-02 20:37:32 +00002453#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 n = recvfrom(s->sock_fd, cbuf, len, flags,
2455 (void *) &addrbuf, &addrlen);
Guido van Rossum32c575d1997-12-02 20:37:32 +00002456#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002457#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 n = recvfrom(s->sock_fd, cbuf, len, flags,
2459 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002460#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 }
2462 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 if (timeout == 1) {
2465 PyErr_SetString(socket_timeout, "timed out");
2466 return -1;
2467 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002468 END_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 if (n < 0) {
2470 s->errorhandler();
2471 return -1;
2472 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002474 if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
2475 addrlen, s->sock_proto)))
2476 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 return n;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002479}
2480
2481/* s.recvfrom(nbytes [,flags]) method */
2482
2483static PyObject *
2484sock_recvfrom(PySocketSockObject *s, PyObject *args)
2485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 PyObject *buf = NULL;
2487 PyObject *addr = NULL;
2488 PyObject *ret = NULL;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002489 int flags = 0;
2490 Py_ssize_t recvlen, outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002491
Antoine Pitrou19467d22010-08-17 19:33:30 +00002492 if (!PyArg_ParseTuple(args, "n|i:recvfrom", &recvlen, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002495 if (recvlen < 0) {
2496 PyErr_SetString(PyExc_ValueError,
2497 "negative buffersize in recvfrom");
2498 return NULL;
2499 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00002500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
2502 if (buf == NULL)
2503 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf),
2506 recvlen, flags, &addr);
2507 if (outlen < 0) {
2508 goto finally;
2509 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 if (outlen != recvlen) {
2512 /* We did not read as many bytes as we anticipated, resize the
2513 string if possible and be succesful. */
2514 if (_PyBytes_Resize(&buf, outlen) < 0)
2515 /* Oopsy, not so succesful after all. */
2516 goto finally;
2517 }
Barry Warsaw752300b1997-01-03 17:18:10 +00002518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002520
2521finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 Py_XDECREF(buf);
2523 Py_XDECREF(addr);
2524 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002525}
2526
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002527PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002528"recvfrom(buffersize[, flags]) -> (data, address info)\n\
2529\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002530Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002531
Thomas Wouters477c8d52006-05-27 19:21:47 +00002532
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002533/* s.recvfrom_into(buffer[, nbytes [,flags]]) method */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002534
2535static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002536sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00002539
Antoine Pitrou19467d22010-08-17 19:33:30 +00002540 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 Py_buffer pbuf;
2542 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002543 Py_ssize_t readlen, buflen, recvlen = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002545 PyObject *addr = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002546
Antoine Pitrou19467d22010-08-17 19:33:30 +00002547 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ni:recvfrom_into",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 kwlist, &pbuf,
2549 &recvlen, &flags))
2550 return NULL;
2551 buf = pbuf.buf;
2552 buflen = pbuf.len;
2553 assert(buf != 0 && buflen > 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 if (recvlen < 0) {
2556 PyBuffer_Release(&pbuf);
2557 PyErr_SetString(PyExc_ValueError,
2558 "negative buffersize in recvfrom_into");
2559 return NULL;
2560 }
2561 if (recvlen == 0) {
2562 /* If nbytes was not specified, use the buffer's length */
2563 recvlen = buflen;
2564 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
2567 if (readlen < 0) {
2568 PyBuffer_Release(&pbuf);
2569 /* Return an error */
2570 Py_XDECREF(addr);
2571 return NULL;
2572 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 PyBuffer_Release(&pbuf);
2575 /* Return the number of bytes read and the address. Note that we do
2576 not do anything special here in the case that readlen < recvlen. */
Antoine Pitrou19467d22010-08-17 19:33:30 +00002577 return Py_BuildValue("nN", readlen, addr);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002578}
2579
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002580PyDoc_STRVAR(recvfrom_into_doc,
2581"recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002582\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002583Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002584
2585
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002586/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002587
Guido van Rossum73624e91994-10-10 17:59:00 +00002588static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002589sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002592 Py_ssize_t len, n = -1;
2593 int flags = 0, timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 Py_buffer pbuf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags))
2597 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599 if (!IS_SELECTABLE(s)) {
2600 PyBuffer_Release(&pbuf);
2601 return select_error();
2602 }
2603 buf = pbuf.buf;
2604 len = pbuf.len;
Neal Norwitz082b2df2006-02-07 07:04:46 +00002605
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002606 BEGIN_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002608 timeout = internal_select_ex(s, 1, interval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 if (!timeout)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002610#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 n = sendsegmented(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002612#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002614#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002615 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 if (timeout == 1) {
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002617 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002618 PyErr_SetString(socket_timeout, "timed out");
2619 return NULL;
2620 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002621 END_SELECT_LOOP(s)
2622
2623 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002624 if (n < 0)
2625 return s->errorhandler();
Antoine Pitrou19467d22010-08-17 19:33:30 +00002626 return PyLong_FromSsize_t(n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00002627}
2628
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002629PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002630"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002631\n\
2632Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002633argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002634sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002635
2636
2637/* s.sendall(data [,flags]) method */
2638
2639static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002640sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 char *buf;
Antoine Pitrou19467d22010-08-17 19:33:30 +00002643 Py_ssize_t len, n = -1;
Antoine Pitrou6d7df632010-09-27 17:52:25 +00002644 int flags = 0, timeout, saved_errno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 Py_buffer pbuf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags))
2648 return NULL;
2649 buf = pbuf.buf;
2650 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 if (!IS_SELECTABLE(s)) {
2653 PyBuffer_Release(&pbuf);
2654 return select_error();
2655 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657 do {
Antoine Pitrou6d7df632010-09-27 17:52:25 +00002658 Py_BEGIN_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002659 timeout = internal_select(s, 1);
2660 n = -1;
Antoine Pitrou6d7df632010-09-27 17:52:25 +00002661 if (!timeout) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002662#ifdef __VMS
Antoine Pitrou6d7df632010-09-27 17:52:25 +00002663 n = sendsegmented(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002664#else
Antoine Pitrou6d7df632010-09-27 17:52:25 +00002665 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002666#endif
Antoine Pitrou6d7df632010-09-27 17:52:25 +00002667 }
2668 Py_END_ALLOW_THREADS
2669 if (timeout == 1) {
2670 PyBuffer_Release(&pbuf);
2671 PyErr_SetString(socket_timeout, "timed out");
2672 return NULL;
2673 }
2674 /* PyErr_CheckSignals() might change errno */
2675 saved_errno = errno;
2676 /* We must run our signal handlers before looping again.
2677 send() can return a successful partial write when it is
2678 interrupted, so we can't restrict ourselves to EINTR. */
2679 if (PyErr_CheckSignals()) {
2680 PyBuffer_Release(&pbuf);
2681 return NULL;
2682 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 if (n < 0) {
Antoine Pitrou6d7df632010-09-27 17:52:25 +00002684 /* If interrupted, try again */
2685 if (saved_errno == EINTR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 continue;
Antoine Pitrou6d7df632010-09-27 17:52:25 +00002687 else
2688 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 }
2690 buf += n;
2691 len -= n;
2692 } while (len > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002693 PyBuffer_Release(&pbuf);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002695 if (n < 0)
2696 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00002697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 Py_INCREF(Py_None);
2699 return Py_None;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002700}
2701
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002702PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002703"sendall(data[, flags])\n\
2704\n\
2705Send a data string to the socket. For the optional flags\n\
2706argument, see the Unix manual. This calls send() repeatedly\n\
2707until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002708to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002709
Guido van Rossum30a685f1991-06-27 15:51:29 +00002710
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002711/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002712
Guido van Rossum73624e91994-10-10 17:59:00 +00002713static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002714sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 Py_buffer pbuf;
2717 PyObject *addro;
2718 char *buf;
2719 Py_ssize_t len;
2720 sock_addr_t addrbuf;
2721 int addrlen, n = -1, flags, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 flags = 0;
2724 if (!PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro)) {
2725 PyErr_Clear();
2726 if (!PyArg_ParseTuple(args, "y*iO:sendto",
2727 &pbuf, &flags, &addro))
2728 return NULL;
2729 }
2730 buf = pbuf.buf;
2731 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 if (!IS_SELECTABLE(s)) {
2734 PyBuffer_Release(&pbuf);
2735 return select_error();
2736 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002738 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) {
2739 PyBuffer_Release(&pbuf);
2740 return NULL;
2741 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002742
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002743 BEGIN_SELECT_LOOP(s)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002745 timeout = internal_select_ex(s, 1, interval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 if (!timeout)
2747 n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen);
2748 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 if (timeout == 1) {
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002751 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 PyErr_SetString(socket_timeout, "timed out");
2753 return NULL;
2754 }
Antoine Pitrou3e1fd272010-09-28 21:23:11 +00002755 END_SELECT_LOOP(s)
2756 PyBuffer_Release(&pbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 if (n < 0)
2758 return s->errorhandler();
Antoine Pitrou19467d22010-08-17 19:33:30 +00002759 return PyLong_FromSsize_t(n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002760}
2761
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002762PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002763"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002764\n\
2765Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002766For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002767
Guido van Rossum30a685f1991-06-27 15:51:29 +00002768
2769/* s.shutdown(how) method */
2770
Guido van Rossum73624e91994-10-10 17:59:00 +00002771static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002772sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002773{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 int how;
2775 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 how = PyLong_AsLong(arg);
2778 if (how == -1 && PyErr_Occurred())
2779 return NULL;
2780 Py_BEGIN_ALLOW_THREADS
2781 res = shutdown(s->sock_fd, how);
2782 Py_END_ALLOW_THREADS
2783 if (res < 0)
2784 return s->errorhandler();
2785 Py_INCREF(Py_None);
2786 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002787}
2788
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002789PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002790"shutdown(flag)\n\
2791\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00002792Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
2793of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002794
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00002795#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Christian Heimesfaf2f632008-01-06 16:59:19 +00002796static PyObject*
2797sock_ioctl(PySocketSockObject *s, PyObject *arg)
2798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 unsigned long cmd = SIO_RCVALL;
2800 PyObject *argO;
2801 DWORD recv;
Christian Heimesfaf2f632008-01-06 16:59:19 +00002802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO))
2804 return NULL;
Christian Heimesfaf2f632008-01-06 16:59:19 +00002805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 switch (cmd) {
2807 case SIO_RCVALL: {
2808 unsigned int option = RCVALL_ON;
2809 if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option))
2810 return NULL;
2811 if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option),
2812 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
2813 return set_error();
2814 }
2815 return PyLong_FromUnsignedLong(recv); }
2816 case SIO_KEEPALIVE_VALS: {
2817 struct tcp_keepalive ka;
2818 if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd,
2819 &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval))
2820 return NULL;
2821 if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka),
2822 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
2823 return set_error();
2824 }
2825 return PyLong_FromUnsignedLong(recv); }
2826 default:
2827 PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd);
2828 return NULL;
2829 }
Christian Heimesfaf2f632008-01-06 16:59:19 +00002830}
2831PyDoc_STRVAR(sock_ioctl_doc,
2832"ioctl(cmd, option) -> long\n\
2833\n\
Kristján Valur Jónsson847ec752009-09-27 21:10:38 +00002834Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\
2835SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\
2836SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval).");
Christian Heimesfaf2f632008-01-06 16:59:19 +00002837
2838#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00002839
2840/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002841
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002842static PyMethodDef sock_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 {"_accept", (PyCFunction)sock_accept, METH_NOARGS,
2844 accept_doc},
2845 {"bind", (PyCFunction)sock_bind, METH_O,
2846 bind_doc},
2847 {"close", (PyCFunction)sock_close, METH_NOARGS,
2848 close_doc},
2849 {"connect", (PyCFunction)sock_connect, METH_O,
2850 connect_doc},
2851 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
2852 connect_ex_doc},
Antoine Pitrou6e451df2010-08-09 20:39:54 +00002853 {"detach", (PyCFunction)sock_detach, METH_NOARGS,
2854 detach_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
2856 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002857#ifdef HAVE_GETPEERNAME
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858 {"getpeername", (PyCFunction)sock_getpeername,
2859 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00002860#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 {"getsockname", (PyCFunction)sock_getsockname,
2862 METH_NOARGS, getsockname_doc},
2863 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
2864 getsockopt_doc},
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00002865#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS,
2867 sock_ioctl_doc},
Christian Heimesfaf2f632008-01-06 16:59:19 +00002868#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 {"listen", (PyCFunction)sock_listen, METH_O,
2870 listen_doc},
2871 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
2872 recv_doc},
2873 {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
2874 recv_into_doc},
2875 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
2876 recvfrom_doc},
2877 {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
2878 recvfrom_into_doc},
2879 {"send", (PyCFunction)sock_send, METH_VARARGS,
2880 send_doc},
2881 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
2882 sendall_doc},
2883 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
2884 sendto_doc},
2885 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
2886 setblocking_doc},
2887 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
2888 settimeout_doc},
2889 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
2890 gettimeout_doc},
2891 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
2892 setsockopt_doc},
2893 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
2894 shutdown_doc},
2895 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002896};
2897
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002898/* SockObject members */
2899static PyMemberDef sock_memberlist[] = {
2900 {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},
2901 {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},
2902 {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},
2903 {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"},
2904 {0},
2905};
Guido van Rossum30a685f1991-06-27 15:51:29 +00002906
Guido van Rossum73624e91994-10-10 17:59:00 +00002907/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00002908 First close the file description. */
2909
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002910static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002911sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 if (s->sock_fd != -1)
2914 (void) SOCKETCLOSE(s->sock_fd);
2915 Py_TYPE(s)->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002916}
2917
Guido van Rossum30a685f1991-06-27 15:51:29 +00002918
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002919static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002920sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002921{
Fred Drakea04eaad2000-06-30 02:46:07 +00002922#if SIZEOF_SOCKET_T > SIZEOF_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 if (s->sock_fd > LONG_MAX) {
2924 /* this can occur on Win64, and actually there is a special
2925 ugly printf formatter for decimal pointer length integer
2926 printing, only bother if necessary*/
2927 PyErr_SetString(PyExc_OverflowError,
2928 "no printf formatter to display "
2929 "the socket descriptor in decimal");
2930 return NULL;
2931 }
Fred Drakea04eaad2000-06-30 02:46:07 +00002932#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 return PyUnicode_FromFormat(
2934 "<socket object, fd=%ld, family=%d, type=%d, proto=%d>",
2935 (long)s->sock_fd, s->sock_family,
2936 s->sock_type,
2937 s->sock_proto);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002938}
2939
2940
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002941/* Create a new, uninitialized socket object. */
2942
2943static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002944sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002945{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 PyObject *new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 new = type->tp_alloc(type, 0);
2949 if (new != NULL) {
2950 ((PySocketSockObject *)new)->sock_fd = -1;
2951 ((PySocketSockObject *)new)->sock_timeout = -1.0;
2952 ((PySocketSockObject *)new)->errorhandler = &set_error;
2953 }
2954 return new;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002955}
2956
2957
2958/* Initialize a new socket object. */
2959
2960/*ARGSUSED*/
2961static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002962sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002963{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002964 PySocketSockObject *s = (PySocketSockObject *)self;
2965 PyObject *fdobj = NULL;
2966 SOCKET_T fd = INVALID_SOCKET;
2967 int family = AF_INET, type = SOCK_STREAM, proto = 0;
2968 static char *keywords[] = {"family", "type", "proto", "fileno", 0};
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 if (!PyArg_ParseTupleAndKeywords(args, kwds,
2971 "|iiiO:socket", keywords,
2972 &family, &type, &proto, &fdobj))
2973 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 if (fdobj != NULL && fdobj != Py_None) {
2976 fd = PyLong_AsSocket_t(fdobj);
2977 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
2978 return -1;
2979 if (fd == INVALID_SOCKET) {
2980 PyErr_SetString(PyExc_ValueError,
2981 "can't use invalid socket value");
2982 return -1;
2983 }
2984 }
2985 else {
2986 Py_BEGIN_ALLOW_THREADS
2987 fd = socket(family, type, proto);
2988 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 if (fd == INVALID_SOCKET) {
2991 set_error();
2992 return -1;
2993 }
2994 }
2995 init_sockobject(s, fd, family, type, proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002998
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002999}
3000
3001
Guido van Rossumb6775db1994-08-01 11:34:53 +00003002/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00003003
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003004static PyTypeObject sock_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
3006 "_socket.socket", /* tp_name */
3007 sizeof(PySocketSockObject), /* tp_basicsize */
3008 0, /* tp_itemsize */
3009 (destructor)sock_dealloc, /* tp_dealloc */
3010 0, /* tp_print */
3011 0, /* tp_getattr */
3012 0, /* tp_setattr */
3013 0, /* tp_reserved */
3014 (reprfunc)sock_repr, /* tp_repr */
3015 0, /* tp_as_number */
3016 0, /* tp_as_sequence */
3017 0, /* tp_as_mapping */
3018 0, /* tp_hash */
3019 0, /* tp_call */
3020 0, /* tp_str */
3021 PyObject_GenericGetAttr, /* tp_getattro */
3022 0, /* tp_setattro */
3023 0, /* tp_as_buffer */
3024 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
3025 sock_doc, /* tp_doc */
3026 0, /* tp_traverse */
3027 0, /* tp_clear */
3028 0, /* tp_richcompare */
3029 0, /* tp_weaklistoffset */
3030 0, /* tp_iter */
3031 0, /* tp_iternext */
3032 sock_methods, /* tp_methods */
3033 sock_memberlist, /* tp_members */
3034 0, /* tp_getset */
3035 0, /* tp_base */
3036 0, /* tp_dict */
3037 0, /* tp_descr_get */
3038 0, /* tp_descr_set */
3039 0, /* tp_dictoffset */
3040 sock_initobj, /* tp_init */
3041 PyType_GenericAlloc, /* tp_alloc */
3042 sock_new, /* tp_new */
3043 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003044};
3045
Guido van Rossum30a685f1991-06-27 15:51:29 +00003046
Guido van Rossum81194471991-07-27 21:42:02 +00003047/* Python interface to gethostname(). */
3048
3049/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00003050static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003051socket_gethostname(PyObject *self, PyObject *unused)
Guido van Rossum81194471991-07-27 21:42:02 +00003052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 char buf[1024];
3054 int res;
3055 Py_BEGIN_ALLOW_THREADS
3056 res = gethostname(buf, (int) sizeof buf - 1);
3057 Py_END_ALLOW_THREADS
3058 if (res < 0)
3059 return set_error();
3060 buf[sizeof buf - 1] = '\0';
3061 return PyUnicode_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00003062}
Guido van Rossumff4949e1992-08-05 19:58:53 +00003063
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003064PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003065"gethostname() -> string\n\
3066\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003067Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003068
Guido van Rossumff4949e1992-08-05 19:58:53 +00003069
Guido van Rossum30a685f1991-06-27 15:51:29 +00003070/* Python interface to gethostbyname(name). */
3071
3072/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00003073static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003074socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00003075{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076 char *name;
3077 sock_addr_t addrbuf;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00003078 PyObject *ret = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003079
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00003080 if (!PyArg_ParseTuple(args, "et:gethostbyname", "idna", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 return NULL;
3082 if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00003083 goto finally;
3084 ret = makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
3085finally:
3086 PyMem_Free(name);
3087 return ret;
Guido van Rossum30a685f1991-06-27 15:51:29 +00003088}
3089
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003090PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003091"gethostbyname(host) -> address\n\
3092\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003093Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003094
3095
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003096/* Convenience function common to gethostbyname_ex and gethostbyaddr */
3097
3098static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003099gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 char **pch;
3102 PyObject *rtn_tuple = (PyObject *)NULL;
3103 PyObject *name_list = (PyObject *)NULL;
3104 PyObject *addr_list = (PyObject *)NULL;
3105 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107 if (h == NULL) {
3108 /* Let's get real error message to return */
3109 set_herror(h_errno);
3110 return NULL;
3111 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 if (h->h_addrtype != af) {
3114 /* Let's get real error message to return */
3115 PyErr_SetString(socket_error,
3116 (char *)strerror(EAFNOSUPPORT));
Christian Heimesada8c3b2008-03-18 18:26:33 +00003117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 return NULL;
3119 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00003122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 case AF_INET:
3124 if (alen < sizeof(struct sockaddr_in))
3125 return NULL;
3126 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003127
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003128#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 case AF_INET6:
3130 if (alen < sizeof(struct sockaddr_in6))
3131 return NULL;
3132 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003133#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00003134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003137 if ((name_list = PyList_New(0)) == NULL)
3138 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 if ((addr_list = PyList_New(0)) == NULL)
3141 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 /* SF #1511317: h_aliases can be NULL */
3144 if (h->h_aliases) {
3145 for (pch = h->h_aliases; *pch != NULL; pch++) {
3146 int status;
3147 tmp = PyUnicode_FromString(*pch);
3148 if (tmp == NULL)
3149 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 status = PyList_Append(name_list, tmp);
3152 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 if (status)
3155 goto err;
3156 }
3157 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 for (pch = h->h_addr_list; *pch != NULL; pch++) {
3160 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00003163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 case AF_INET:
3165 {
3166 struct sockaddr_in sin;
3167 memset(&sin, 0, sizeof(sin));
3168 sin.sin_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003169#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003170 sin.sin_len = sizeof(sin);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003171#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003172 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
3173 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00003174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 if (pch == h->h_addr_list && alen >= sizeof(sin))
3176 memcpy((char *) addr, &sin, sizeof(sin));
3177 break;
3178 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003179
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003180#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 case AF_INET6:
3182 {
3183 struct sockaddr_in6 sin6;
3184 memset(&sin6, 0, sizeof(sin6));
3185 sin6.sin6_family = af;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003186#ifdef HAVE_SOCKADDR_SA_LEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 sin6.sin6_len = sizeof(sin6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003188#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
3190 tmp = makeipaddr((struct sockaddr *)&sin6,
3191 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00003192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003193 if (pch == h->h_addr_list && alen >= sizeof(sin6))
3194 memcpy((char *) addr, &sin6, sizeof(sin6));
3195 break;
3196 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003197#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00003198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003199 default: /* can't happen */
3200 PyErr_SetString(socket_error,
3201 "unsupported address family");
3202 return NULL;
3203 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 if (tmp == NULL)
3206 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 status = PyList_Append(addr_list, tmp);
3209 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 if (status)
3212 goto err;
3213 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003215 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003216
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003217 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003218 Py_XDECREF(name_list);
3219 Py_XDECREF(addr_list);
3220 return rtn_tuple;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003221}
3222
3223
3224/* Python interface to gethostbyname_ex(name). */
3225
3226/*ARGSUSED*/
3227static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003228socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 char *name;
3231 struct hostent *h;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00003232#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 struct sockaddr_storage addr;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00003234#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235 struct sockaddr_in addr;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00003236#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003237 struct sockaddr *sa;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00003238 PyObject *ret = NULL;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003239#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003241#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003243#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 char buf[16384];
3245 int buf_len = (sizeof buf) - 1;
3246 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003247#endif
3248#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003249 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003250#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003251#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003252
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00003253 if (!PyArg_ParseTuple(args, "et:gethostbyname_ex", "idna", &name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 return NULL;
3255 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00003256 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 Py_BEGIN_ALLOW_THREADS
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003258#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003259#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
3261 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003262#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003264#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 memset((void *) &data, '\0', sizeof(data));
3266 result = gethostbyname_r(name, &hp_allocated, &data);
3267 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003268#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003269#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003270#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003271 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003272#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003274#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003275 Py_END_ALLOW_THREADS
3276 /* Some C libraries would require addr.__ss_family instead of
3277 addr.ss_family.
3278 Therefore, we cast the sockaddr_storage into sockaddr to
3279 access sa_family. */
3280 sa = (struct sockaddr*)&addr;
3281 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
3282 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003283#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00003285#endif
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00003286finally:
3287 PyMem_Free(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003289}
3290
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003291PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003292"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
3293\n\
3294Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003295for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003296
3297
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003298/* Python interface to gethostbyaddr(IP). */
3299
3300/*ARGSUSED*/
3301static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003302socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003303{
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003304#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003305 struct sockaddr_storage addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003306#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003308#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 struct sockaddr *sa = (struct sockaddr *)&addr;
3310 char *ip_num;
3311 struct hostent *h;
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00003312 PyObject *ret = NULL;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003313#ifdef HAVE_GETHOSTBYNAME_R
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003315#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 struct hostent_data data;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003317#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 /* glibcs up to 2.10 assume that the buf argument to
3319 gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc
3320 does not ensure. The attribute below instructs the compiler
3321 to maintain this alignment. */
3322 char buf[16384] Py_ALIGNED(8);
3323 int buf_len = (sizeof buf) - 1;
3324 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003325#endif
3326#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 int result;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003328#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003329#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 char *ap;
3331 int al;
3332 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003333
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00003334 if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 return NULL;
3336 af = AF_UNSPEC;
3337 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00003338 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003339 af = sa->sa_family;
3340 ap = NULL;
3341 al = 0;
3342 switch (af) {
3343 case AF_INET:
3344 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
3345 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3346 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003347#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 case AF_INET6:
3349 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
3350 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3351 break;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003352#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 default:
3354 PyErr_SetString(socket_error, "unsupported address family");
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00003355 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 }
3357 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003358#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003359#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003360 result = gethostbyaddr_r(ap, al, af,
3361 &hp_allocated, buf, buf_len,
3362 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003363#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 h = gethostbyaddr_r(ap, al, af,
3365 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003366#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 memset((void *) &data, '\0', sizeof(data));
3368 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
3369 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003370#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003371#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003372#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003373 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003374#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003376#endif /* HAVE_GETHOSTBYNAME_R */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003377 Py_END_ALLOW_THREADS
3378 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003379#ifdef USE_GETHOSTBYNAME_LOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003380 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003381#endif
Martin v. Löwisfc0275a2010-08-22 19:33:47 +00003382finally:
3383 PyMem_Free(ip_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003385}
3386
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003387PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003388"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
3389\n\
3390Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003391for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003392
Guido van Rossum30a685f1991-06-27 15:51:29 +00003393
3394/* Python interface to getservbyname(name).
3395 This only returns the port number, since the other info is already
3396 known or not useful (like the list of aliases). */
3397
3398/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00003399static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003400socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00003401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 char *name, *proto=NULL;
3403 struct servent *sp;
3404 if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
3405 return NULL;
3406 Py_BEGIN_ALLOW_THREADS
3407 sp = getservbyname(name, proto);
3408 Py_END_ALLOW_THREADS
3409 if (sp == NULL) {
3410 PyErr_SetString(socket_error, "service/proto not found");
3411 return NULL;
3412 }
3413 return PyLong_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00003414}
3415
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003416PyDoc_STRVAR(getservbyname_doc,
Barry Warsaw11b91a02004-06-28 00:50:43 +00003417"getservbyname(servicename[, protocolname]) -> integer\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003418\n\
3419Return a port number from a service name and protocol name.\n\
Barry Warsaw11b91a02004-06-28 00:50:43 +00003420The optional protocol name, if given, should be 'tcp' or 'udp',\n\
3421otherwise any protocol will match.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003422
Guido van Rossum30a685f1991-06-27 15:51:29 +00003423
Barry Warsaw11b91a02004-06-28 00:50:43 +00003424/* Python interface to getservbyport(port).
3425 This only returns the service name, since the other info is already
3426 known or not useful (like the list of aliases). */
3427
3428/*ARGSUSED*/
3429static PyObject *
3430socket_getservbyport(PyObject *self, PyObject *args)
3431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 int port;
3433 char *proto=NULL;
3434 struct servent *sp;
3435 if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto))
3436 return NULL;
3437 if (port < 0 || port > 0xffff) {
3438 PyErr_SetString(
3439 PyExc_OverflowError,
3440 "getservbyport: port must be 0-65535.");
3441 return NULL;
3442 }
3443 Py_BEGIN_ALLOW_THREADS
3444 sp = getservbyport(htons((short)port), proto);
3445 Py_END_ALLOW_THREADS
3446 if (sp == NULL) {
3447 PyErr_SetString(socket_error, "port/proto not found");
3448 return NULL;
3449 }
3450 return PyUnicode_FromString(sp->s_name);
Barry Warsaw11b91a02004-06-28 00:50:43 +00003451}
3452
3453PyDoc_STRVAR(getservbyport_doc,
3454"getservbyport(port[, protocolname]) -> string\n\
3455\n\
3456Return the service name from a port number and protocol name.\n\
3457The optional protocol name, if given, should be 'tcp' or 'udp',\n\
3458otherwise any protocol will match.");
3459
Guido van Rossum3901d851996-12-19 16:35:04 +00003460/* Python interface to getprotobyname(name).
3461 This only returns the protocol number, since the other info is
3462 already known or not useful (like the list of aliases). */
3463
3464/*ARGSUSED*/
3465static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003466socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00003467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003468 char *name;
3469 struct protoent *sp;
3470 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
3471 return NULL;
3472 Py_BEGIN_ALLOW_THREADS
3473 sp = getprotobyname(name);
3474 Py_END_ALLOW_THREADS
3475 if (sp == NULL) {
3476 PyErr_SetString(socket_error, "protocol not found");
3477 return NULL;
3478 }
3479 return PyLong_FromLong((long) sp->p_proto);
Guido van Rossum3901d851996-12-19 16:35:04 +00003480}
3481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003482PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003483"getprotobyname(name) -> integer\n\
3484\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003485Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003486
Guido van Rossum3901d851996-12-19 16:35:04 +00003487
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003488#ifndef NO_DUP
3489/* dup() function for socket fds */
3490
3491static PyObject *
3492socket_dup(PyObject *self, PyObject *fdobj)
3493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003494 SOCKET_T fd, newfd;
3495 PyObject *newfdobj;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003496
3497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 fd = PyLong_AsSocket_t(fdobj);
3499 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
3500 return NULL;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003502 newfd = dup_socket(fd);
3503 if (newfd == INVALID_SOCKET)
3504 return set_error();
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 newfdobj = PyLong_FromSocket_t(newfd);
3507 if (newfdobj == NULL)
3508 SOCKETCLOSE(newfd);
3509 return newfdobj;
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003510}
3511
3512PyDoc_STRVAR(dup_doc,
3513"dup(integer) -> integer\n\
3514\n\
3515Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\
3516sockets; on some platforms os.dup() won't work for socket file descriptors.");
3517#endif
3518
3519
Dave Cole331708b2004-08-09 04:51:41 +00003520#ifdef HAVE_SOCKETPAIR
3521/* Create a pair of sockets using the socketpair() function.
Dave Cole07fda7e2004-08-23 05:16:23 +00003522 Arguments as for socket() except the default family is AF_UNIX if
Dave Colee8bbfe42004-08-26 00:51:16 +00003523 defined on the platform; otherwise, the default is AF_INET. */
Dave Cole331708b2004-08-09 04:51:41 +00003524
3525/*ARGSUSED*/
3526static PyObject *
3527socket_socketpair(PyObject *self, PyObject *args)
3528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 PySocketSockObject *s0 = NULL, *s1 = NULL;
3530 SOCKET_T sv[2];
3531 int family, type = SOCK_STREAM, proto = 0;
3532 PyObject *res = NULL;
Dave Cole331708b2004-08-09 04:51:41 +00003533
3534#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003535 family = AF_UNIX;
Dave Cole331708b2004-08-09 04:51:41 +00003536#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 family = AF_INET;
Dave Cole331708b2004-08-09 04:51:41 +00003538#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003539 if (!PyArg_ParseTuple(args, "|iii:socketpair",
3540 &family, &type, &proto))
3541 return NULL;
3542 /* Create a pair of socket fds */
3543 if (socketpair(family, type, proto, sv) < 0)
3544 return set_error();
3545 s0 = new_sockobject(sv[0], family, type, proto);
3546 if (s0 == NULL)
3547 goto finally;
3548 s1 = new_sockobject(sv[1], family, type, proto);
3549 if (s1 == NULL)
3550 goto finally;
3551 res = PyTuple_Pack(2, s0, s1);
Dave Cole331708b2004-08-09 04:51:41 +00003552
3553finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 if (res == NULL) {
3555 if (s0 == NULL)
3556 SOCKETCLOSE(sv[0]);
3557 if (s1 == NULL)
3558 SOCKETCLOSE(sv[1]);
3559 }
3560 Py_XDECREF(s0);
3561 Py_XDECREF(s1);
3562 return res;
Dave Cole331708b2004-08-09 04:51:41 +00003563}
3564
3565PyDoc_STRVAR(socketpair_doc,
3566"socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\
3567\n\
3568Create a pair of socket objects from the sockets returned by the platform\n\
3569socketpair() function.\n\
Dave Cole07fda7e2004-08-23 05:16:23 +00003570The arguments are the same as for socket() except the default family is\n\
Dave Colee8bbfe42004-08-26 00:51:16 +00003571AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
Dave Cole331708b2004-08-09 04:51:41 +00003572
3573#endif /* HAVE_SOCKETPAIR */
3574
3575
Guido van Rossum006bf911996-06-12 04:04:55 +00003576static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003577socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003578{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003579 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00003580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
3582 return NULL;
3583 }
3584 if (x1 < 0) {
3585 PyErr_SetString(PyExc_OverflowError,
3586 "can't convert negative number to unsigned long");
3587 return NULL;
3588 }
3589 x2 = (unsigned int)ntohs((unsigned short)x1);
3590 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00003591}
3592
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003593PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003594"ntohs(integer) -> integer\n\
3595\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003596Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003597
3598
Guido van Rossum006bf911996-06-12 04:04:55 +00003599static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003600socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 if (PyLong_Check(arg)) {
3605 x = PyLong_AsUnsignedLong(arg);
3606 if (x == (unsigned long) -1 && PyErr_Occurred())
3607 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003608#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003609 {
3610 unsigned long y;
3611 /* only want the trailing 32 bits */
3612 y = x & 0xFFFFFFFFUL;
3613 if (y ^ x)
3614 return PyErr_Format(PyExc_OverflowError,
3615 "long int larger than 32 bits");
3616 x = y;
3617 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003618#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003619 }
3620 else
3621 return PyErr_Format(PyExc_TypeError,
3622 "expected int/long, %s found",
3623 Py_TYPE(arg)->tp_name);
3624 if (x == (unsigned long) -1 && PyErr_Occurred())
3625 return NULL;
3626 return PyLong_FromUnsignedLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003627}
3628
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003629PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003630"ntohl(integer) -> integer\n\
3631\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003632Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003633
3634
Guido van Rossum006bf911996-06-12 04:04:55 +00003635static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003636socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003638 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00003639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
3641 return NULL;
3642 }
3643 if (x1 < 0) {
3644 PyErr_SetString(PyExc_OverflowError,
3645 "can't convert negative number to unsigned long");
3646 return NULL;
3647 }
3648 x2 = (unsigned int)htons((unsigned short)x1);
3649 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00003650}
3651
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003652PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003653"htons(integer) -> integer\n\
3654\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003655Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003656
3657
Guido van Rossum006bf911996-06-12 04:04:55 +00003658static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003659socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003661 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003663 if (PyLong_Check(arg)) {
3664 x = PyLong_AsUnsignedLong(arg);
3665 if (x == (unsigned long) -1 && PyErr_Occurred())
3666 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003667#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003668 {
3669 unsigned long y;
3670 /* only want the trailing 32 bits */
3671 y = x & 0xFFFFFFFFUL;
3672 if (y ^ x)
3673 return PyErr_Format(PyExc_OverflowError,
3674 "long int larger than 32 bits");
3675 x = y;
3676 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003677#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003678 }
3679 else
3680 return PyErr_Format(PyExc_TypeError,
3681 "expected int/long, %s found",
3682 Py_TYPE(arg)->tp_name);
3683 return PyLong_FromUnsignedLong(htonl((unsigned long)x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003684}
3685
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003686PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003687"htonl(integer) -> integer\n\
3688\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003689Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003690
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003691/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003692
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003693PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum7d0a8262007-05-21 23:13:11 +00003694"inet_aton(string) -> bytes giving packed 32-bit IP representation\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003695\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003696Convert 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 +00003697binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003698
3699static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003700socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003701{
Guido van Rossuma2e48551999-09-09 15:42:59 +00003702#ifndef INADDR_NONE
3703#define INADDR_NONE (-1)
3704#endif
Neal Norwitz88f115b2003-02-13 02:15:42 +00003705#ifdef HAVE_INET_ATON
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003706 struct in_addr buf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003707#endif
3708
3709#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
Benjamin Petersonf91df042009-02-13 02:50:59 +00003710#if (SIZEOF_INT != 4)
3711#error "Not sure if in_addr_t exists and int is not 32-bits."
3712#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003713 /* Have to use inet_addr() instead */
3714 unsigned int packed_addr;
Tim Peters1df9fdd2003-02-13 03:13:40 +00003715#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003716 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
3719 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003720
Tim Peters1df9fdd2003-02-13 03:13:40 +00003721
3722#ifdef HAVE_INET_ATON
Thomas Wouters477c8d52006-05-27 19:21:47 +00003723
3724#ifdef USE_INET_ATON_WEAKLINK
3725 if (inet_aton != NULL) {
3726#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003727 if (inet_aton(ip_addr, &buf))
3728 return PyBytes_FromStringAndSize((char *)(&buf),
3729 sizeof(buf));
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 PyErr_SetString(socket_error,
3732 "illegal IP address string passed to inet_aton");
3733 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003734
Thomas Wouters477c8d52006-05-27 19:21:47 +00003735#ifdef USE_INET_ATON_WEAKLINK
3736 } else {
3737#endif
3738
3739#endif
3740
3741#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
3742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 /* special-case this address as inet_addr might return INADDR_NONE
3744 * for this */
3745 if (strcmp(ip_addr, "255.255.255.255") == 0) {
3746 packed_addr = 0xFFFFFFFF;
3747 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00003748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 packed_addr = inet_addr(ip_addr);
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003751 if (packed_addr == INADDR_NONE) { /* invalid address */
3752 PyErr_SetString(socket_error,
3753 "illegal IP address string passed to inet_aton");
3754 return NULL;
3755 }
3756 }
3757 return PyBytes_FromStringAndSize((char *) &packed_addr,
3758 sizeof(packed_addr));
Thomas Wouters477c8d52006-05-27 19:21:47 +00003759
3760#ifdef USE_INET_ATON_WEAKLINK
3761 }
3762#endif
3763
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003764#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003765}
3766
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003767PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00003768"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003769\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003770Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003771
3772static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003773socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003775 char *packed_str;
3776 int addr_len;
3777 struct in_addr packed_addr;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003779 if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) {
3780 return NULL;
3781 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00003782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 if (addr_len != sizeof(packed_addr)) {
3784 PyErr_SetString(socket_error,
3785 "packed IP wrong length for inet_ntoa");
3786 return NULL;
3787 }
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 memcpy(&packed_addr, packed_str, addr_len);
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 return PyUnicode_FromString(inet_ntoa(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003792}
Guido van Rossum82a5c661998-07-07 20:45:43 +00003793
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003794#ifdef HAVE_INET_PTON
3795
3796PyDoc_STRVAR(inet_pton_doc,
3797"inet_pton(af, ip) -> packed IP address string\n\
3798\n\
3799Convert an IP address from string format to a packed string suitable\n\
3800for use with low-level network functions.");
3801
3802static PyObject *
3803socket_inet_pton(PyObject *self, PyObject *args)
3804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003805 int af;
3806 char* ip;
3807 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003808#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003809 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003810#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 char packed[sizeof(struct in_addr)];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003812#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003813 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
3814 return NULL;
3815 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003816
Martin v. Löwis04697e82004-06-02 12:35:29 +00003817#if !defined(ENABLE_IPV6) && defined(AF_INET6)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 if(af == AF_INET6) {
3819 PyErr_SetString(socket_error,
3820 "can't use AF_INET6, IPv6 is disabled");
3821 return NULL;
3822 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003823#endif
Martin v. Löwis10649092003-08-05 06:25:06 +00003824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003825 retval = inet_pton(af, ip, packed);
3826 if (retval < 0) {
3827 PyErr_SetFromErrno(socket_error);
3828 return NULL;
3829 } else if (retval == 0) {
3830 PyErr_SetString(socket_error,
3831 "illegal IP address string passed to inet_pton");
3832 return NULL;
3833 } else if (af == AF_INET) {
3834 return PyBytes_FromStringAndSize(packed,
3835 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003836#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003837 } else if (af == AF_INET6) {
3838 return PyBytes_FromStringAndSize(packed,
3839 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003840#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003841 } else {
3842 PyErr_SetString(socket_error, "unknown address family");
3843 return NULL;
3844 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003845}
Thomas Wouters477c8d52006-05-27 19:21:47 +00003846
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003847PyDoc_STRVAR(inet_ntop_doc,
3848"inet_ntop(af, packed_ip) -> string formatted IP address\n\
3849\n\
3850Convert a packed IP address of the given family to string format.");
3851
3852static PyObject *
3853socket_inet_ntop(PyObject *self, PyObject *args)
3854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003855 int af;
3856 char* packed;
3857 int len;
3858 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003859#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003860 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003861#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003862 char ip[INET_ADDRSTRLEN + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003863#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00003864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003865 /* Guarantee NUL-termination for PyUnicode_FromString() below */
3866 memset((void *) &ip[0], '\0', sizeof(ip));
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003868 if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) {
3869 return NULL;
3870 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003872 if (af == AF_INET) {
3873 if (len != sizeof(struct in_addr)) {
3874 PyErr_SetString(PyExc_ValueError,
3875 "invalid length of packed IP address string");
3876 return NULL;
3877 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003878#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003879 } else if (af == AF_INET6) {
3880 if (len != sizeof(struct in6_addr)) {
3881 PyErr_SetString(PyExc_ValueError,
3882 "invalid length of packed IP address string");
3883 return NULL;
3884 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003885#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003886 } else {
3887 PyErr_Format(PyExc_ValueError,
3888 "unknown address family %d", af);
3889 return NULL;
3890 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003892 retval = inet_ntop(af, packed, ip, sizeof(ip));
3893 if (!retval) {
3894 PyErr_SetFromErrno(socket_error);
3895 return NULL;
3896 } else {
3897 return PyUnicode_FromString(retval);
3898 }
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003900 /* NOTREACHED */
3901 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
3902 return NULL;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003903}
3904
3905#endif /* HAVE_INET_PTON */
3906
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003907/* Python interface to getaddrinfo(host, port). */
3908
3909/*ARGSUSED*/
3910static PyObject *
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00003911socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003912{
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00003913 static char* kwnames[] = {"host", "port", "family", "type", "proto",
3914 "flags", 0};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003915 struct addrinfo hints, *res;
3916 struct addrinfo *res0 = NULL;
3917 PyObject *hobj = NULL;
3918 PyObject *pobj = (PyObject *)NULL;
3919 char pbuf[30];
3920 char *hptr, *pptr;
3921 int family, socktype, protocol, flags;
3922 int error;
3923 PyObject *all = (PyObject *)NULL;
3924 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003926 family = socktype = protocol = flags = 0;
3927 family = AF_UNSPEC;
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00003928 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iiii:getaddrinfo",
3929 kwnames, &hobj, &pobj, &family, &socktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003930 &protocol, &flags)) {
3931 return NULL;
3932 }
3933 if (hobj == Py_None) {
3934 hptr = NULL;
3935 } else if (PyUnicode_Check(hobj)) {
3936 idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
3937 if (!idna)
3938 return NULL;
3939 assert(PyBytes_Check(idna));
3940 hptr = PyBytes_AS_STRING(idna);
3941 } else if (PyBytes_Check(hobj)) {
3942 hptr = PyBytes_AsString(hobj);
3943 } else {
3944 PyErr_SetString(PyExc_TypeError,
3945 "getaddrinfo() argument 1 must be string or None");
3946 return NULL;
3947 }
3948 if (PyLong_CheckExact(pobj)) {
3949 long value = PyLong_AsLong(pobj);
3950 if (value == -1 && PyErr_Occurred())
3951 goto err;
3952 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value);
3953 pptr = pbuf;
3954 } else if (PyUnicode_Check(pobj)) {
3955 pptr = _PyUnicode_AsString(pobj);
3956 } else if (PyBytes_Check(pobj)) {
3957 pptr = PyBytes_AsString(pobj);
3958 } else if (pobj == Py_None) {
3959 pptr = (char *)NULL;
3960 } else {
3961 PyErr_SetString(socket_error, "Int or String expected");
3962 goto err;
3963 }
3964 memset(&hints, 0, sizeof(hints));
3965 hints.ai_family = family;
3966 hints.ai_socktype = socktype;
3967 hints.ai_protocol = protocol;
3968 hints.ai_flags = flags;
3969 Py_BEGIN_ALLOW_THREADS
3970 ACQUIRE_GETADDRINFO_LOCK
3971 error = getaddrinfo(hptr, pptr, &hints, &res0);
3972 Py_END_ALLOW_THREADS
3973 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
3974 if (error) {
3975 set_gaierror(error);
3976 goto err;
3977 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 if ((all = PyList_New(0)) == NULL)
3980 goto err;
3981 for (res = res0; res; res = res->ai_next) {
3982 PyObject *single;
3983 PyObject *addr =
3984 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
3985 if (addr == NULL)
3986 goto err;
3987 single = Py_BuildValue("iiisO", res->ai_family,
3988 res->ai_socktype, res->ai_protocol,
3989 res->ai_canonname ? res->ai_canonname : "",
3990 addr);
3991 Py_DECREF(addr);
3992 if (single == NULL)
3993 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003995 if (PyList_Append(all, single))
3996 goto err;
3997 Py_XDECREF(single);
3998 }
3999 Py_XDECREF(idna);
4000 if (res0)
4001 freeaddrinfo(res0);
4002 return all;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004003 err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004004 Py_XDECREF(all);
4005 Py_XDECREF(idna);
4006 if (res0)
4007 freeaddrinfo(res0);
4008 return (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004009}
4010
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004011PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004012"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
4013 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004014\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004015Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004016
4017/* Python interface to getnameinfo(sa, flags). */
4018
4019/*ARGSUSED*/
4020static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004021socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004022{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004023 PyObject *sa = (PyObject *)NULL;
4024 int flags;
4025 char *hostp;
4026 int port, flowinfo, scope_id;
4027 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
4028 struct addrinfo hints, *res = NULL;
4029 int error;
4030 PyObject *ret = (PyObject *)NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004032 flags = flowinfo = scope_id = 0;
4033 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
4034 return NULL;
4035 if (!PyTuple_Check(sa)) {
4036 PyErr_SetString(PyExc_TypeError,
4037 "getnameinfo() argument 1 must be a tuple");
4038 return NULL;
4039 }
4040 if (!PyArg_ParseTuple(sa, "si|ii",
4041 &hostp, &port, &flowinfo, &scope_id))
4042 return NULL;
4043 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
4044 memset(&hints, 0, sizeof(hints));
4045 hints.ai_family = AF_UNSPEC;
4046 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Martin v. Löwis112c0f32010-08-25 07:38:15 +00004047 hints.ai_flags = AI_NUMERICHOST; /* don't do any name resolution */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004048 Py_BEGIN_ALLOW_THREADS
4049 ACQUIRE_GETADDRINFO_LOCK
4050 error = getaddrinfo(hostp, pbuf, &hints, &res);
4051 Py_END_ALLOW_THREADS
4052 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
4053 if (error) {
4054 set_gaierror(error);
4055 goto fail;
4056 }
4057 if (res->ai_next) {
4058 PyErr_SetString(socket_error,
4059 "sockaddr resolved to multiple addresses");
4060 goto fail;
4061 }
4062 switch (res->ai_family) {
4063 case AF_INET:
4064 {
4065 if (PyTuple_GET_SIZE(sa) != 2) {
4066 PyErr_SetString(socket_error,
4067 "IPv4 sockaddr must be 2 tuple");
4068 goto fail;
4069 }
4070 break;
4071 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00004072#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 case AF_INET6:
4074 {
4075 struct sockaddr_in6 *sin6;
4076 sin6 = (struct sockaddr_in6 *)res->ai_addr;
4077 sin6->sin6_flowinfo = flowinfo;
4078 sin6->sin6_scope_id = scope_id;
4079 break;
4080 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004081#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004082 }
Antoine Pitrou19467d22010-08-17 19:33:30 +00004083 error = getnameinfo(res->ai_addr, (socklen_t) res->ai_addrlen,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004084 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
4085 if (error) {
4086 set_gaierror(error);
4087 goto fail;
4088 }
4089 ret = Py_BuildValue("ss", hbuf, pbuf);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004090
4091fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 if (res)
4093 freeaddrinfo(res);
4094 return ret;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004095}
4096
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004097PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004098"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004099\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004100Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004101
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004102
4103/* Python API to getting and setting the default timeout value. */
4104
4105static PyObject *
4106socket_getdefaulttimeout(PyObject *self)
4107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004108 if (defaulttimeout < 0.0) {
4109 Py_INCREF(Py_None);
4110 return Py_None;
4111 }
4112 else
4113 return PyFloat_FromDouble(defaulttimeout);
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004114}
4115
4116PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004117"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004118\n\
4119Returns the default timeout in floating seconds for new socket objects.\n\
4120A value of None indicates that new socket objects have no timeout.\n\
4121When the socket module is first imported, the default is None.");
4122
4123static PyObject *
4124socket_setdefaulttimeout(PyObject *self, PyObject *arg)
4125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 double timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004128 if (arg == Py_None)
4129 timeout = -1.0;
4130 else {
4131 timeout = PyFloat_AsDouble(arg);
4132 if (timeout < 0.0) {
4133 if (!PyErr_Occurred())
4134 PyErr_SetString(PyExc_ValueError,
4135 "Timeout value out of range");
4136 return NULL;
4137 }
4138 }
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004140 defaulttimeout = timeout;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004142 Py_INCREF(Py_None);
4143 return Py_None;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004144}
4145
4146PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004147"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004148\n\
4149Set the default timeout in floating seconds for new socket objects.\n\
4150A value of None indicates that new socket objects have no timeout.\n\
4151When the socket module is first imported, the default is None.");
4152
4153
Guido van Rossum30a685f1991-06-27 15:51:29 +00004154/* List of functions exported by this module. */
4155
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004156static PyMethodDef socket_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004157 {"gethostbyname", socket_gethostbyname,
4158 METH_VARARGS, gethostbyname_doc},
4159 {"gethostbyname_ex", socket_gethostbyname_ex,
4160 METH_VARARGS, ghbn_ex_doc},
4161 {"gethostbyaddr", socket_gethostbyaddr,
4162 METH_VARARGS, gethostbyaddr_doc},
4163 {"gethostname", socket_gethostname,
4164 METH_NOARGS, gethostname_doc},
4165 {"getservbyname", socket_getservbyname,
4166 METH_VARARGS, getservbyname_doc},
4167 {"getservbyport", socket_getservbyport,
4168 METH_VARARGS, getservbyport_doc},
4169 {"getprotobyname", socket_getprotobyname,
4170 METH_VARARGS, getprotobyname_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004171#ifndef NO_DUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004172 {"dup", socket_dup,
4173 METH_O, dup_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004174#endif
Dave Cole331708b2004-08-09 04:51:41 +00004175#ifdef HAVE_SOCKETPAIR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004176 {"socketpair", socket_socketpair,
4177 METH_VARARGS, socketpair_doc},
Dave Cole331708b2004-08-09 04:51:41 +00004178#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004179 {"ntohs", socket_ntohs,
4180 METH_VARARGS, ntohs_doc},
4181 {"ntohl", socket_ntohl,
4182 METH_O, ntohl_doc},
4183 {"htons", socket_htons,
4184 METH_VARARGS, htons_doc},
4185 {"htonl", socket_htonl,
4186 METH_O, htonl_doc},
4187 {"inet_aton", socket_inet_aton,
4188 METH_VARARGS, inet_aton_doc},
4189 {"inet_ntoa", socket_inet_ntoa,
4190 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004191#ifdef HAVE_INET_PTON
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004192 {"inet_pton", socket_inet_pton,
4193 METH_VARARGS, inet_pton_doc},
4194 {"inet_ntop", socket_inet_ntop,
4195 METH_VARARGS, inet_ntop_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004196#endif
Giampaolo Rodolàccfb91c2010-08-17 15:30:23 +00004197 {"getaddrinfo", (PyCFunction)socket_getaddrinfo,
4198 METH_VARARGS | METH_KEYWORDS, getaddrinfo_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004199 {"getnameinfo", socket_getnameinfo,
4200 METH_VARARGS, getnameinfo_doc},
4201 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
4202 METH_NOARGS, getdefaulttimeout_doc},
4203 {"setdefaulttimeout", socket_setdefaulttimeout,
4204 METH_O, setdefaulttimeout_doc},
4205 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004206};
4207
Guido van Rossum30a685f1991-06-27 15:51:29 +00004208
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004209#ifdef MS_WINDOWS
4210#define OS_INIT_DEFINED
4211
4212/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004213
4214static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004215os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00004216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004217 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00004218}
4219
4220static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004221os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00004222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 WSADATA WSAData;
4224 int ret;
4225 ret = WSAStartup(0x0101, &WSAData);
4226 switch (ret) {
4227 case 0: /* No error */
4228 Py_AtExit(os_cleanup);
4229 return 1; /* Success */
4230 case WSASYSNOTREADY:
4231 PyErr_SetString(PyExc_ImportError,
4232 "WSAStartup failed: network not ready");
4233 break;
4234 case WSAVERNOTSUPPORTED:
4235 case WSAEINVAL:
4236 PyErr_SetString(
4237 PyExc_ImportError,
4238 "WSAStartup failed: requested version not supported");
4239 break;
4240 default:
4241 PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret);
4242 break;
4243 }
4244 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004245}
4246
Guido van Rossum8d665e61996-06-26 18:22:49 +00004247#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004248
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004249
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004250#ifdef PYOS_OS2
4251#define OS_INIT_DEFINED
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004252
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004253/* Additional initialization for OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004254
4255static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004256os_init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004257{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004258#ifndef PYCC_GCC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004259 int rc = sock_init();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004261 if (rc == 0) {
4262 return 1; /* Success */
4263 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004265 PyErr_Format(PyExc_ImportError, "OS/2 TCP/IP Error# %d", sock_errno());
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004267 return 0; /* Failure */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00004268#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004269 /* No need to initialise sockets with GCC/EMX */
4270 return 1; /* Success */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00004271#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004272}
4273
4274#endif /* PYOS_OS2 */
4275
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004276
4277#ifndef OS_INIT_DEFINED
4278static int
4279os_init(void)
4280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004281 return 1; /* Success */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004282}
4283#endif
4284
4285
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004286/* C API table - always add new things to the end for binary
4287 compatibility. */
4288static
4289PySocketModule_APIObject PySocketModuleAPI =
4290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004291 &sock_type,
4292 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004293};
4294
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004295
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004296/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004297
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004298 This module is actually called "_socket", and there's a wrapper
Guido van Rossum7d0a8262007-05-21 23:13:11 +00004299 "socket.py" which implements some additional functionality.
4300 The import of "_socket" may fail with an ImportError exception if
4301 os-specific initialization fails. On Windows, this does WINSOCK
4302 initialization. When WINSOCK is initialized succesfully, a call to
4303 WSACleanup() is scheduled to be made at exit time.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004304*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004305
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004306PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004307"Implementation module for socket operations.\n\
4308\n\
4309See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004310
Martin v. Löwis1a214512008-06-11 05:26:20 +00004311static struct PyModuleDef socketmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004312 PyModuleDef_HEAD_INIT,
4313 PySocket_MODULE_NAME,
4314 socket_doc,
4315 -1,
4316 socket_methods,
4317 NULL,
4318 NULL,
4319 NULL,
4320 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00004321};
4322
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004323PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00004324PyInit__socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00004327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004328 if (!os_init())
4329 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004331 Py_TYPE(&sock_type) = &PyType_Type;
4332 m = PyModule_Create(&socketmodule);
4333 if (m == NULL)
4334 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004336 socket_error = PyErr_NewException("socket.error",
4337 PyExc_IOError, NULL);
4338 if (socket_error == NULL)
4339 return NULL;
4340 PySocketModuleAPI.error = socket_error;
4341 Py_INCREF(socket_error);
4342 PyModule_AddObject(m, "error", socket_error);
4343 socket_herror = PyErr_NewException("socket.herror",
4344 socket_error, NULL);
4345 if (socket_herror == NULL)
4346 return NULL;
4347 Py_INCREF(socket_herror);
4348 PyModule_AddObject(m, "herror", socket_herror);
4349 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
4350 NULL);
4351 if (socket_gaierror == NULL)
4352 return NULL;
4353 Py_INCREF(socket_gaierror);
4354 PyModule_AddObject(m, "gaierror", socket_gaierror);
4355 socket_timeout = PyErr_NewException("socket.timeout",
4356 socket_error, NULL);
4357 if (socket_timeout == NULL)
4358 return NULL;
4359 Py_INCREF(socket_timeout);
4360 PyModule_AddObject(m, "timeout", socket_timeout);
4361 Py_INCREF((PyObject *)&sock_type);
4362 if (PyModule_AddObject(m, "SocketType",
4363 (PyObject *)&sock_type) != 0)
4364 return NULL;
4365 Py_INCREF((PyObject *)&sock_type);
4366 if (PyModule_AddObject(m, "socket",
4367 (PyObject *)&sock_type) != 0)
4368 return NULL;
Guido van Rossum09be4091999-08-09 14:40:40 +00004369
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004370#ifdef ENABLE_IPV6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004371 has_ipv6 = Py_True;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004372#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004373 has_ipv6 = Py_False;
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004374#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004375 Py_INCREF(has_ipv6);
4376 PyModule_AddObject(m, "has_ipv6", has_ipv6);
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004378 /* Export C API */
4379 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
4380 PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL)
4381 ) != 0)
4382 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004384 /* Address families (we only support AF_INET and AF_UNIX) */
Guido van Rossum09be4091999-08-09 14:40:40 +00004385#ifdef AF_UNSPEC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00004387#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004388 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004389#ifdef AF_INET6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004390 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004391#endif /* AF_INET6 */
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00004392#if defined(AF_UNIX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004393 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004394#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00004395#ifdef AF_AX25
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 /* Amateur Radio AX.25 */
4397 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004398#endif
4399#ifdef AF_IPX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004400 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00004401#endif
4402#ifdef AF_APPLETALK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 /* Appletalk DDP */
4404 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004405#endif
4406#ifdef AF_NETROM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004407 /* Amateur radio NetROM */
4408 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004409#endif
4410#ifdef AF_BRIDGE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004411 /* Multiprotocol bridge */
4412 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004413#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004414#ifdef AF_ATMPVC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004415 /* ATM PVCs */
4416 PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004417#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004418#ifdef AF_AAL5
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004419 /* Reserved for Werner's ATM */
4420 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00004421#endif
4422#ifdef AF_X25
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004423 /* Reserved for X.25 project */
4424 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004425#endif
4426#ifdef AF_INET6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004427 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00004428#endif
4429#ifdef AF_ROSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004430 /* Amateur Radio X.25 PLP */
4431 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004432#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004433#ifdef AF_DECnet
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 /* Reserved for DECnet project */
4435 PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004436#endif
4437#ifdef AF_NETBEUI
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004438 /* Reserved for 802.2LLC project */
4439 PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004440#endif
4441#ifdef AF_SECURITY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004442 /* Security callback pseudo AF */
4443 PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004444#endif
4445#ifdef AF_KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004446 /* PF_KEY key management API */
4447 PyModule_AddIntConstant(m, "AF_KEY", AF_KEY);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004448#endif
4449#ifdef AF_NETLINK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004450 /* */
4451 PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);
4452 PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004453#ifdef NETLINK_SKIP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004455#endif
4456#ifdef NETLINK_W1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004458#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004459 PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK);
4460 PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004461#ifdef NETLINK_TCPDIAG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004463#endif
4464#ifdef NETLINK_NFLOG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004465 PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004466#endif
Neal Norwitz65851662006-01-16 04:31:40 +00004467#ifdef NETLINK_XFRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004468 PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM);
Neal Norwitz65851662006-01-16 04:31:40 +00004469#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004470#ifdef NETLINK_ARPD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004472#endif
4473#ifdef NETLINK_ROUTE6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004475#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004476 PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW);
Thomas Wouterscf297e42007-02-23 15:07:44 +00004477#ifdef NETLINK_DNRTMSG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004478 PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG);
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004479#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004480#ifdef NETLINK_TAPBASE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004482#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004483#endif /* AF_NETLINK */
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004484#ifdef AF_ROUTE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 /* Alias to emulate 4.4BSD */
4486 PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004487#endif
4488#ifdef AF_ASH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 /* Ash */
4490 PyModule_AddIntConstant(m, "AF_ASH", AF_ASH);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004491#endif
4492#ifdef AF_ECONET
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004493 /* Acorn Econet */
4494 PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004495#endif
4496#ifdef AF_ATMSVC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004497 /* ATM SVCs */
4498 PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004499#endif
4500#ifdef AF_SNA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 /* Linux SNA Project (nutters!) */
4502 PyModule_AddIntConstant(m, "AF_SNA", AF_SNA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004503#endif
4504#ifdef AF_IRDA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 /* IRDA sockets */
4506 PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004507#endif
4508#ifdef AF_PPPOX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 /* PPPoX sockets */
4510 PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004511#endif
4512#ifdef AF_WANPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513 /* Wanpipe API Sockets */
4514 PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004515#endif
4516#ifdef AF_LLC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004517 /* Linux LLC */
4518 PyModule_AddIntConstant(m, "AF_LLC", AF_LLC);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004519#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00004520
Hye-Shik Chang81268602004-02-02 06:05:24 +00004521#ifdef USE_BLUETOOTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004522 PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);
4523 PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);
4524 PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI);
4525 PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI);
4526 PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER);
Hye-Shik Chang81268602004-02-02 06:05:24 +00004527#if !defined(__FreeBSD__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004528 PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP);
4529 PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR);
4530 PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00004531#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);
4533 PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
4534 PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
Martin v. Löwis12af0482004-01-31 12:34:17 +00004535#endif
4536
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00004537#ifdef HAVE_NETPACKET_PACKET_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
4539 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
4540 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
4541 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
4542 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
4543 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
4544 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
4545 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
4546 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00004547#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004548
Christian Heimes043d6f62008-01-07 17:19:16 +00004549#ifdef HAVE_LINUX_TIPC_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004550 PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC);
Christian Heimes043d6f62008-01-07 17:19:16 +00004551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004552 /* for addresses */
4553 PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ);
4554 PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME);
4555 PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID);
Christian Heimes043d6f62008-01-07 17:19:16 +00004556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE);
4558 PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE);
4559 PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE);
Christian Heimes043d6f62008-01-07 17:19:16 +00004560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004561 /* for setsockopt() */
4562 PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC);
4563 PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE);
4564 PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE);
4565 PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE",
4566 TIPC_DEST_DROPPABLE);
4567 PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT);
Christian Heimes043d6f62008-01-07 17:19:16 +00004568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004569 PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE",
4570 TIPC_LOW_IMPORTANCE);
4571 PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE",
4572 TIPC_MEDIUM_IMPORTANCE);
4573 PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE",
4574 TIPC_HIGH_IMPORTANCE);
4575 PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE",
4576 TIPC_CRITICAL_IMPORTANCE);
Christian Heimes043d6f62008-01-07 17:19:16 +00004577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004578 /* for subscriptions */
4579 PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS);
4580 PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE);
Christian Heimes25bb7832008-01-11 16:17:00 +00004581#ifdef TIPC_SUB_CANCEL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004582 /* doesn't seem to be available everywhere */
4583 PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL);
Christian Heimes25bb7832008-01-11 16:17:00 +00004584#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004585 PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER);
4586 PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED);
4587 PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN);
4588 PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT);
4589 PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV);
4590 PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV);
Christian Heimes043d6f62008-01-07 17:19:16 +00004591#endif
4592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004593 /* Socket types */
4594 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
4595 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004596/* We have incomplete socket support. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004597 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
4598 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00004599#if defined(SOCK_RDM)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004601#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603#ifdef SO_DEBUG
4604 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004605#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606#ifdef SO_ACCEPTCONN
4607 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004608#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004609#ifdef SO_REUSEADDR
4610 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004611#endif
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00004612#ifdef SO_EXCLUSIVEADDRUSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004613 PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE);
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00004614#endif
4615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004616#ifdef SO_KEEPALIVE
4617 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004618#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004619#ifdef SO_DONTROUTE
4620 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004621#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622#ifdef SO_BROADCAST
4623 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004624#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004625#ifdef SO_USELOOPBACK
4626 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004627#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004628#ifdef SO_LINGER
4629 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004630#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004631#ifdef SO_OOBINLINE
4632 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004633#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004634#ifdef SO_REUSEPORT
4635 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004636#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004637#ifdef SO_SNDBUF
4638 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004639#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004640#ifdef SO_RCVBUF
4641 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004642#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004643#ifdef SO_SNDLOWAT
4644 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004645#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004646#ifdef SO_RCVLOWAT
4647 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004648#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004649#ifdef SO_SNDTIMEO
4650 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004651#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004652#ifdef SO_RCVTIMEO
4653 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004654#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004655#ifdef SO_ERROR
4656 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004657#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004658#ifdef SO_TYPE
4659 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004660#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661#ifdef SO_SETFIB
4662 PyModule_AddIntConstant(m, "SO_SETFIB", SO_SETFIB);
Larry Hastingsf0f37952010-04-02 11:47:10 +00004663#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004665 /* Maximum number of connections for "listen" */
4666#ifdef SOMAXCONN
4667 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004668#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004670#endif
4671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 /* Flags for send, recv */
4673#ifdef MSG_OOB
4674 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004675#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004676#ifdef MSG_PEEK
4677 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004678#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004679#ifdef MSG_DONTROUTE
4680 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004681#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004682#ifdef MSG_DONTWAIT
4683 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00004684#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004685#ifdef MSG_EOR
4686 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004687#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004688#ifdef MSG_TRUNC
4689 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004690#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004691#ifdef MSG_CTRUNC
4692 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004693#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694#ifdef MSG_WAITALL
4695 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004696#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004697#ifdef MSG_BTAG
4698 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004699#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004700#ifdef MSG_ETAG
4701 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004702#endif
4703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004704 /* Protocol level and numbers, usable for [gs]etsockopt */
4705#ifdef SOL_SOCKET
4706 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004707#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004708#ifdef SOL_IP
4709 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004710#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004711 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00004712#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004713#ifdef SOL_IPX
4714 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00004715#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004716#ifdef SOL_AX25
4717 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004718#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004719#ifdef SOL_ATALK
4720 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004721#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004722#ifdef SOL_NETROM
4723 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004724#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004725#ifdef SOL_ROSE
4726 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004727#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004728#ifdef SOL_TCP
4729 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004730#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004731 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00004732#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004733#ifdef SOL_UDP
4734 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004735#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004736 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00004737#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004738#ifdef IPPROTO_IP
4739 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00004740#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004741 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004742#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004743#ifdef IPPROTO_HOPOPTS
4744 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004745#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004746#ifdef IPPROTO_ICMP
4747 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00004748#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004749 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004750#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751#ifdef IPPROTO_IGMP
4752 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004753#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004754#ifdef IPPROTO_GGP
4755 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004756#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004757#ifdef IPPROTO_IPV4
4758 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004759#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004760#ifdef IPPROTO_IPV6
4761 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisa0f17342003-10-03 13:56:20 +00004762#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004763#ifdef IPPROTO_IPIP
4764 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004765#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004766#ifdef IPPROTO_TCP
4767 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00004768#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004769 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004770#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004771#ifdef IPPROTO_EGP
4772 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004773#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004774#ifdef IPPROTO_PUP
4775 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004776#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004777#ifdef IPPROTO_UDP
4778 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00004779#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004780 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004781#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004782#ifdef IPPROTO_IDP
4783 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004784#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004785#ifdef IPPROTO_HELLO
4786 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004787#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004788#ifdef IPPROTO_ND
4789 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004790#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004791#ifdef IPPROTO_TP
4792 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004793#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004794#ifdef IPPROTO_IPV6
4795 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004796#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004797#ifdef IPPROTO_ROUTING
4798 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004799#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004800#ifdef IPPROTO_FRAGMENT
4801 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004802#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004803#ifdef IPPROTO_RSVP
4804 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004805#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004806#ifdef IPPROTO_GRE
4807 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004808#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004809#ifdef IPPROTO_ESP
4810 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004811#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004812#ifdef IPPROTO_AH
4813 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004814#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004815#ifdef IPPROTO_MOBILE
4816 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004817#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004818#ifdef IPPROTO_ICMPV6
4819 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004820#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004821#ifdef IPPROTO_NONE
4822 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004823#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004824#ifdef IPPROTO_DSTOPTS
4825 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004826#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004827#ifdef IPPROTO_XTP
4828 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004829#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004830#ifdef IPPROTO_EON
4831 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004832#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004833#ifdef IPPROTO_PIM
4834 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004835#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004836#ifdef IPPROTO_IPCOMP
4837 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004838#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004839#ifdef IPPROTO_VRRP
4840 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004841#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004842#ifdef IPPROTO_BIP
4843 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004844#endif
4845/**/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004846#ifdef IPPROTO_RAW
4847 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00004848#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004849 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004850#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004851#ifdef IPPROTO_MAX
4852 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004853#endif
4854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004855 /* Some port configuration */
4856#ifdef IPPORT_RESERVED
4857 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004858#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004859 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004860#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004861#ifdef IPPORT_USERRESERVED
4862 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004863#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004864 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004865#endif
4866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004867 /* Some reserved IP v.4 addresses */
4868#ifdef INADDR_ANY
4869 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004870#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004871 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004872#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004873#ifdef INADDR_BROADCAST
4874 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004875#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004876 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004877#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004878#ifdef INADDR_LOOPBACK
4879 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004880#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004881 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004882#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004883#ifdef INADDR_UNSPEC_GROUP
4884 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004885#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004886 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004887#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004888#ifdef INADDR_ALLHOSTS_GROUP
4889 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
4890 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004891#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004892 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004893#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004894#ifdef INADDR_MAX_LOCAL_GROUP
4895 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
4896 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004897#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004898 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004899#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004900#ifdef INADDR_NONE
4901 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004902#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004903 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004904#endif
4905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004906 /* IPv4 [gs]etsockopt options */
4907#ifdef IP_OPTIONS
4908 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004909#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004910#ifdef IP_HDRINCL
4911 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004912#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004913#ifdef IP_TOS
4914 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004915#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004916#ifdef IP_TTL
4917 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004918#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004919#ifdef IP_RECVOPTS
4920 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004921#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004922#ifdef IP_RECVRETOPTS
4923 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004924#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004925#ifdef IP_RECVDSTADDR
4926 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004927#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004928#ifdef IP_RETOPTS
4929 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004930#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004931#ifdef IP_MULTICAST_IF
4932 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004933#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004934#ifdef IP_MULTICAST_TTL
4935 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004936#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004937#ifdef IP_MULTICAST_LOOP
4938 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004939#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004940#ifdef IP_ADD_MEMBERSHIP
4941 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004942#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004943#ifdef IP_DROP_MEMBERSHIP
4944 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004945#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004946#ifdef IP_DEFAULT_MULTICAST_TTL
4947 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
4948 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00004949#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004950#ifdef IP_DEFAULT_MULTICAST_LOOP
4951 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
4952 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004953#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004954#ifdef IP_MAX_MEMBERSHIPS
4955 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00004956#endif
4957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004958 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
4959#ifdef IPV6_JOIN_GROUP
4960 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004961#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004962#ifdef IPV6_LEAVE_GROUP
4963 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004964#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004965#ifdef IPV6_MULTICAST_HOPS
4966 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004967#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004968#ifdef IPV6_MULTICAST_IF
4969 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004970#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004971#ifdef IPV6_MULTICAST_LOOP
4972 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004973#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004974#ifdef IPV6_UNICAST_HOPS
4975 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004976#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004977 /* Additional IPV6 socket options, defined in RFC 3493 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00004978#ifdef IPV6_V6ONLY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004979 PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004980#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004981 /* Advanced IPV6 socket options, from RFC 3542 */
Martin v. Löwisda91d022003-12-30 11:14:01 +00004982#ifdef IPV6_CHECKSUM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004983 PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004984#endif
4985#ifdef IPV6_DONTFRAG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004986 PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004987#endif
4988#ifdef IPV6_DSTOPTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004989 PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004990#endif
4991#ifdef IPV6_HOPLIMIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004992 PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004993#endif
4994#ifdef IPV6_HOPOPTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004995 PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004996#endif
4997#ifdef IPV6_NEXTHOP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004998 PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);
Martin v. Löwisda91d022003-12-30 11:14:01 +00004999#endif
5000#ifdef IPV6_PATHMTU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005001 PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005002#endif
5003#ifdef IPV6_PKTINFO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005004 PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005005#endif
5006#ifdef IPV6_RECVDSTOPTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005007 PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005008#endif
5009#ifdef IPV6_RECVHOPLIMIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005010 PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005011#endif
5012#ifdef IPV6_RECVHOPOPTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005013 PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005014#endif
5015#ifdef IPV6_RECVPKTINFO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005016 PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005017#endif
5018#ifdef IPV6_RECVRTHDR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005019 PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005020#endif
5021#ifdef IPV6_RECVTCLASS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005022 PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005023#endif
5024#ifdef IPV6_RTHDR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005025 PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005026#endif
5027#ifdef IPV6_RTHDRDSTOPTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005028 PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005029#endif
5030#ifdef IPV6_RTHDR_TYPE_0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005031 PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005032#endif
5033#ifdef IPV6_RECVPATHMTU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005034 PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005035#endif
5036#ifdef IPV6_TCLASS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005037 PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005038#endif
5039#ifdef IPV6_USE_MIN_MTU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005040 PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005041#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00005042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005043 /* TCP options */
5044#ifdef TCP_NODELAY
5045 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00005046#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005047#ifdef TCP_MAXSEG
5048 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00005049#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005050#ifdef TCP_CORK
5051 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00005052#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005053#ifdef TCP_KEEPIDLE
5054 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00005055#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005056#ifdef TCP_KEEPINTVL
5057 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00005058#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005059#ifdef TCP_KEEPCNT
5060 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00005061#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005062#ifdef TCP_SYNCNT
5063 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00005064#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005065#ifdef TCP_LINGER2
5066 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00005067#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005068#ifdef TCP_DEFER_ACCEPT
5069 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00005070#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005071#ifdef TCP_WINDOW_CLAMP
5072 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00005073#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005074#ifdef TCP_INFO
5075 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00005076#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005077#ifdef TCP_QUICKACK
5078 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00005079#endif
5080
Guido van Rossum09be4091999-08-09 14:40:40 +00005081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005082 /* IPX options */
5083#ifdef IPX_TYPE
5084 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00005085#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00005086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005087 /* get{addr,name}info parameters */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005088#ifdef EAI_ADDRFAMILY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005089 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005090#endif
5091#ifdef EAI_AGAIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005092 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005093#endif
5094#ifdef EAI_BADFLAGS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005095 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005096#endif
5097#ifdef EAI_FAIL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005098 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005099#endif
5100#ifdef EAI_FAMILY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005101 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005102#endif
5103#ifdef EAI_MEMORY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005104 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005105#endif
5106#ifdef EAI_NODATA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005107 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005108#endif
5109#ifdef EAI_NONAME
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005110 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005111#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00005112#ifdef EAI_OVERFLOW
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005113 PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005114#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005115#ifdef EAI_SERVICE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005116 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005117#endif
5118#ifdef EAI_SOCKTYPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005119 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005120#endif
5121#ifdef EAI_SYSTEM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005122 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005123#endif
5124#ifdef EAI_BADHINTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005125 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005126#endif
5127#ifdef EAI_PROTOCOL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005128 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005129#endif
5130#ifdef EAI_MAX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005131 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005132#endif
5133#ifdef AI_PASSIVE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005134 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005135#endif
5136#ifdef AI_CANONNAME
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005137 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005138#endif
5139#ifdef AI_NUMERICHOST
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005140 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005141#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00005142#ifdef AI_NUMERICSERV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005143 PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);
Martin v. Löwisda91d022003-12-30 11:14:01 +00005144#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005145#ifdef AI_MASK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005146 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005147#endif
5148#ifdef AI_ALL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005149 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005150#endif
5151#ifdef AI_V4MAPPED_CFG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005152 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005153#endif
5154#ifdef AI_ADDRCONFIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005155 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005156#endif
5157#ifdef AI_V4MAPPED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005158 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005159#endif
5160#ifdef AI_DEFAULT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005161 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005162#endif
5163#ifdef NI_MAXHOST
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005164 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005165#endif
5166#ifdef NI_MAXSERV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005167 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005168#endif
5169#ifdef NI_NOFQDN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005170 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005171#endif
5172#ifdef NI_NUMERICHOST
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005173 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005174#endif
5175#ifdef NI_NAMEREQD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005176 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005177#endif
5178#ifdef NI_NUMERICSERV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005179 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005180#endif
5181#ifdef NI_DGRAM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005182 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005183#endif
5184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005185 /* shutdown() parameters */
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005186#ifdef SHUT_RD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005187 PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005188#elif defined(SD_RECEIVE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005189 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005190#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005191 PyModule_AddIntConstant(m, "SHUT_RD", 0);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005192#endif
5193#ifdef SHUT_WR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005194 PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005195#elif defined(SD_SEND)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005196 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005197#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005198 PyModule_AddIntConstant(m, "SHUT_WR", 1);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005199#endif
5200#ifdef SHUT_RDWR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005201 PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005202#elif defined(SD_BOTH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005203 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005204#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005205 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005206#endif
5207
Christian Heimesfaf2f632008-01-06 16:59:19 +00005208#ifdef SIO_RCVALL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005209 {
5210 DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS};
5211 const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"};
5212 int i;
5213 for(i = 0; i<sizeof(codes)/sizeof(*codes); ++i) {
5214 PyObject *tmp;
5215 tmp = PyLong_FromUnsignedLong(codes[i]);
5216 if (tmp == NULL)
5217 return NULL;
5218 PyModule_AddObject(m, names[i], tmp);
5219 }
5220 }
5221 PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF);
5222 PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON);
5223 PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00005224#ifdef RCVALL_IPLEVEL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005225 PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00005226#endif
5227#ifdef RCVALL_MAX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005228 PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00005229#endif
Christian Heimesfaf2f632008-01-06 16:59:19 +00005230#endif /* _MSTCPIP_ */
5231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005232 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00005233#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005234 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00005235#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005236 return m;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005237}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005238
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005239
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005240#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +00005241#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005242
5243/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005244/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005245
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005246int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005247inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005249 if (af == AF_INET) {
Benjamin Petersonf91df042009-02-13 02:50:59 +00005250#if (SIZEOF_INT != 4)
5251#error "Not sure if in_addr_t exists and int is not 32-bits."
5252#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005253 unsigned int packed_addr;
5254 packed_addr = inet_addr(src);
5255 if (packed_addr == INADDR_NONE)
5256 return 0;
5257 memcpy(dst, &packed_addr, 4);
5258 return 1;
5259 }
5260 /* Should set errno to EAFNOSUPPORT */
5261 return -1;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005262}
5263
Martin v. Löwisc925b1532001-07-21 09:42:15 +00005264const char *
5265inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005267 if (af == AF_INET) {
5268 struct in_addr packed_addr;
5269 if (size < 16)
5270 /* Should set errno to ENOSPC. */
5271 return NULL;
5272 memcpy(&packed_addr, src, sizeof(packed_addr));
5273 return strncpy(dst, inet_ntoa(packed_addr), size);
5274 }
5275 /* Should set errno to EAFNOSUPPORT */
5276 return NULL;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005277}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005278
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005279#endif
Christian Heimesb6150692007-11-15 23:37:07 +00005280#endif