blob: 7b6046dbc6ead548bfd25d7a6d5df69da51af895 [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,
20 a subclass of socket.error
21- socket.herror: exception raised for gethostby* errors,
22 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
30 (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])
37 --> List of (family, socktype, proto, canonname, sockaddr)
38- 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:
57 TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID;
58 and scope can be one of:
59 TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE.
60 The meaning of v1, v2 and v3 depends on the value of addr_type:
61 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
73
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
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +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{
354 HANDLE newhandle;
355
356 if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)handle,
357 GetCurrentProcess(), &newhandle,
358 0, FALSE, DUPLICATE_SAME_ACCESS))
359 {
360 WSASetLastError(GetLastError());
361 return INVALID_SOCKET;
362 }
363 return (SOCKET)newhandle;
364}
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000365#define SOCKETCLOSE closesocket
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000366#else
367/* On Unix we can use dup to duplicate the file descriptor of a socket*/
368#define dup_socket(fd) dup(fd)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000369#endif
370
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000371#ifdef MS_WIN32
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000372#define EAFNOSUPPORT WSAEAFNOSUPPORT
373#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000374#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000375
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000376#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000377#define SOCKETCLOSE soclose
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000378#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000379#endif
380
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000381#ifndef SOCKETCLOSE
382#define SOCKETCLOSE close
383#endif
384
Jesse Noller32d68c22009-03-31 18:48:42 +0000385#if defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H) && !defined(__NetBSD__)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000386#define USE_BLUETOOTH 1
387#if defined(__FreeBSD__)
388#define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP
389#define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM
Thomas Wouterscf297e42007-02-23 15:07:44 +0000390#define BTPROTO_HCI BLUETOOTH_PROTO_HCI
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000391#define SOL_HCI SOL_HCI_RAW
392#define HCI_FILTER SO_HCI_RAW_FILTER
Hye-Shik Chang81268602004-02-02 06:05:24 +0000393#define sockaddr_l2 sockaddr_l2cap
394#define sockaddr_rc sockaddr_rfcomm
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000395#define hci_dev hci_node
Hye-Shik Chang81268602004-02-02 06:05:24 +0000396#define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb)
397#define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000398#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000399#elif defined(__NetBSD__)
400#define sockaddr_l2 sockaddr_bt
401#define sockaddr_rc sockaddr_bt
Thomas Wouterscf297e42007-02-23 15:07:44 +0000402#define sockaddr_hci sockaddr_bt
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000403#define sockaddr_sco sockaddr_bt
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000404#define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb)
405#define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000406#define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000407#define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000408#else
Hye-Shik Chang81268602004-02-02 06:05:24 +0000409#define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)
410#define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000411#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000412#define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)
413#endif
414#endif
415
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000416#ifdef __VMS
417/* TCP/IP Services for VMS uses a maximum send/recv buffer length */
418#define SEGMENT_SIZE (32 * 1024 -1)
419#endif
420
Thomas Wouters89f507f2006-12-13 04:49:30 +0000421#define SAS2SA(x) ((struct sockaddr *)(x))
422
Martin v. Löwise9416172003-05-03 10:12:45 +0000423/*
424 * Constants for getnameinfo()
425 */
426#if !defined(NI_MAXHOST)
427#define NI_MAXHOST 1025
428#endif
429#if !defined(NI_MAXSERV)
430#define NI_MAXSERV 32
431#endif
432
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000433#ifndef INVALID_SOCKET /* MS defines this */
434#define INVALID_SOCKET (-1)
435#endif
436
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000437/* XXX There's a problem here: *static* functions are not supposed to have
438 a Py prefix (or use CapitalizedWords). Later... */
439
Guido van Rossum30a685f1991-06-27 15:51:29 +0000440/* Global variable holding the exception type for errors detected
441 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000442static PyObject *socket_error;
443static PyObject *socket_herror;
444static PyObject *socket_gaierror;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000445static PyObject *socket_timeout;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000446
Tim Peters643a7fc2002-02-17 04:13:21 +0000447/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000448 The sock_type variable contains pointers to various functions,
449 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000450 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000451static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000452
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000453#if defined(HAVE_POLL_H)
454#include <poll.h>
455#elif defined(HAVE_SYS_POLL_H)
456#include <sys/poll.h>
457#endif
458
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000459#ifdef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE
460/* Platform can select file descriptors beyond FD_SETSIZE */
461#define IS_SELECTABLE(s) 1
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000462#elif defined(HAVE_POLL)
463/* Instead of select(), we'll use poll() since poll() works on any fd. */
464#define IS_SELECTABLE(s) 1
465/* Can we call select() with this socket without a buffer overrun? */
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000466#else
467/* POSIX says selecting file descriptors beyond FD_SETSIZE
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000468 has undefined behaviour. If there's no timeout left, we don't have to
469 call select, so it's a safe, little white lie. */
470#define IS_SELECTABLE(s) ((s)->sock_fd < FD_SETSIZE || s->sock_timeout <= 0.0)
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000471#endif
Neal Norwitz082b2df2006-02-07 07:04:46 +0000472
473static PyObject*
474select_error(void)
475{
476 PyErr_SetString(socket_error, "unable to select on socket");
477 return NULL;
478}
479
Guido van Rossum30a685f1991-06-27 15:51:29 +0000480/* Convenience function to raise an error according to errno
481 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000482
Guido van Rossum73624e91994-10-10 17:59:00 +0000483static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000484set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000485{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000486#ifdef MS_WINDOWS
Mark Hammond46a733d2000-07-24 01:45:11 +0000487 int err_no = WSAGetLastError();
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000488 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
489 recognizes the error codes used by both GetLastError() and
490 WSAGetLastError */
491 if (err_no)
492 return PyErr_SetExcFromWindowsErr(socket_error, err_no);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000493#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000494
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000495#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000496 if (sock_errno() != NO_ERROR) {
497 APIRET rc;
498 ULONG msglen;
499 char outbuf[100];
500 int myerrorcode = sock_errno();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000501
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000502 /* Retrieve socket-related error message from MPTN.MSG file */
503 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
504 myerrorcode - SOCBASEERR + 26,
505 "mptn.msg",
506 &msglen);
507 if (rc == NO_ERROR) {
508 PyObject *v;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000509
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000510 /* OS/2 doesn't guarantee a terminator */
511 outbuf[msglen] = '\0';
512 if (strlen(outbuf) > 0) {
513 /* If non-empty msg, trim CRLF */
514 char *lastc = &outbuf[ strlen(outbuf)-1 ];
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000515 while (lastc > outbuf &&
516 isspace(Py_CHARMASK(*lastc))) {
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000517 /* Trim trailing whitespace (CRLF) */
518 *lastc-- = '\0';
519 }
520 }
521 v = Py_BuildValue("(is)", myerrorcode, outbuf);
522 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000523 PyErr_SetObject(socket_error, v);
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000524 Py_DECREF(v);
525 }
526 return NULL;
527 }
528 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000529#endif
530
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000531 return PyErr_SetFromErrno(socket_error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000532}
533
Guido van Rossum30a685f1991-06-27 15:51:29 +0000534
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000535static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000536set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000537{
538 PyObject *v;
539
540#ifdef HAVE_HSTRERROR
541 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
542#else
543 v = Py_BuildValue("(is)", h_error, "host not found");
544#endif
545 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000546 PyErr_SetObject(socket_herror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000547 Py_DECREF(v);
548 }
549
550 return NULL;
551}
552
553
554static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000555set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000556{
557 PyObject *v;
558
Martin v. Löwis272cb402002-03-01 08:31:07 +0000559#ifdef EAI_SYSTEM
560 /* EAI_SYSTEM is not available on Windows XP. */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000561 if (error == EAI_SYSTEM)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000562 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000563#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000564
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000565#ifdef HAVE_GAI_STRERROR
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000566 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000567#else
568 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
569#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000570 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000571 PyErr_SetObject(socket_gaierror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000572 Py_DECREF(v);
573 }
574
575 return NULL;
576}
577
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000578#ifdef __VMS
579/* Function to send in segments */
580static int
581sendsegmented(int sock_fd, char *buf, int len, int flags)
582{
583 int n = 0;
584 int remaining = len;
585
586 while (remaining > 0) {
587 unsigned int segment;
588
589 segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining);
590 n = send(sock_fd, buf, segment, flags);
591 if (n < 0) {
592 return n;
593 }
594 remaining -= segment;
595 buf += segment;
596 } /* end while */
597
598 return len;
599}
600#endif
601
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000602/* Function to perform the setting of socket blocking mode
603 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000604static int
605internal_setblocking(PySocketSockObject *s, int block)
606{
Guido van Rossum67f7a382002-06-06 21:08:16 +0000607#ifndef MS_WINDOWS
608 int delay_flag;
609#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +0000610
611 Py_BEGIN_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000612#ifndef MS_WINDOWS
613#if defined(PYOS_OS2) && !defined(PYCC_GCC)
614 block = !block;
615 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000616#elif defined(__VMS)
617 block = !block;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000618 ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);
619#else /* !PYOS_OS2 && !__VMS */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000620 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
621 if (block)
Neal Norwitz39d22e52002-11-02 19:55:21 +0000622 delay_flag &= (~O_NONBLOCK);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000623 else
Neal Norwitz39d22e52002-11-02 19:55:21 +0000624 delay_flag |= O_NONBLOCK;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000625 fcntl(s->sock_fd, F_SETFL, delay_flag);
626#endif /* !PYOS_OS2 */
627#else /* MS_WINDOWS */
628 block = !block;
629 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
630#endif /* MS_WINDOWS */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000631 Py_END_ALLOW_THREADS
632
633 /* Since these don't return anything */
634 return 1;
635}
636
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000637/* Do a select()/poll() on the socket, if necessary (sock_timeout > 0).
Guido van Rossum11ba0942002-06-13 15:07:44 +0000638 The argument writing indicates the direction.
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000639 This does not raise an exception; we'll let our caller do that
640 after they've reacquired the interpreter lock.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000641 Returns 1 on timeout, -1 on error, 0 otherwise. */
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000642static int
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000643internal_select(PySocketSockObject *s, int writing)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000644{
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000645 int n;
Guido van Rossum11ba0942002-06-13 15:07:44 +0000646
Guido van Rossumad654902002-07-19 12:44:59 +0000647 /* Nothing to do unless we're in timeout mode (not non-blocking) */
Guido van Rossum11ba0942002-06-13 15:07:44 +0000648 if (s->sock_timeout <= 0.0)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000649 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000650
Guido van Rossumad654902002-07-19 12:44:59 +0000651 /* Guard against closed socket */
652 if (s->sock_fd < 0)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000653 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000654
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000655 /* Prefer poll, if available, since you can poll() any fd
656 * which can't be done with select(). */
657#ifdef HAVE_POLL
658 {
659 struct pollfd pollfd;
660 int timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000661
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000662 pollfd.fd = s->sock_fd;
663 pollfd.events = writing ? POLLOUT : POLLIN;
664
665 /* s->sock_timeout is in seconds, timeout in ms */
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000666 timeout = (int)(s->sock_timeout * 1000 + 0.5);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000667 n = poll(&pollfd, 1, timeout);
668 }
669#else
670 {
671 /* Construct the arguments to select */
672 fd_set fds;
673 struct timeval tv;
674 tv.tv_sec = (int)s->sock_timeout;
675 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
676 FD_ZERO(&fds);
677 FD_SET(s->sock_fd, &fds);
678
679 /* See if the socket is ready */
680 if (writing)
681 n = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
682 else
683 n = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
684 }
685#endif
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000686
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000687 if (n < 0)
688 return -1;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000689 if (n == 0)
690 return 1;
691 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000692}
693
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000694/* Initialize a new socket object. */
695
Tim Petersa12b4cf2002-07-18 22:38:44 +0000696static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000697
Martin v. Löwis1a214512008-06-11 05:26:20 +0000698static void
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000699init_sockobject(PySocketSockObject *s,
700 SOCKET_T fd, int family, int type, int proto)
701{
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000702 s->sock_fd = fd;
703 s->sock_family = family;
704 s->sock_type = type;
705 s->sock_proto = proto;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000706 s->sock_timeout = defaulttimeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000707
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000708 s->errorhandler = &set_error;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000709
710 if (defaulttimeout >= 0.0)
711 internal_setblocking(s, 0);
712
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000713}
714
715
Guido van Rossum30a685f1991-06-27 15:51:29 +0000716/* Create a new socket object.
717 This just creates the object and initializes it.
718 If the creation fails, return NULL and set an exception (implicit
719 in NEWOBJ()). */
720
Guido van Rossum73624e91994-10-10 17:59:00 +0000721static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000722new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000723{
Guido van Rossum73624e91994-10-10 17:59:00 +0000724 PySocketSockObject *s;
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000725 s = (PySocketSockObject *)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000726 PyType_GenericNew(&sock_type, NULL, NULL);
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000727 if (s != NULL)
728 init_sockobject(s, fd, family, type, proto);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000729 return s;
730}
731
Guido van Rossum30a685f1991-06-27 15:51:29 +0000732
Guido van Rossum48a680c2001-03-02 06:34:14 +0000733/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000734 thread to be in gethostbyname or getaddrinfo */
735#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
736PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000737#endif
738
739
Guido van Rossum30a685f1991-06-27 15:51:29 +0000740/* Convert a string specifying a host name or one of a few symbolic
741 names to a numeric IP address. This usually calls gethostbyname()
742 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000743 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000744 an error occurred; then an exception is raised. */
745
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000746static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000747setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000748{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000749 struct addrinfo hints, *res;
750 int error;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000751 int d1, d2, d3, d4;
752 char ch;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000753
Guido van Rossuma376cc51996-12-05 23:43:35 +0000754 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
Guido van Rossum30a685f1991-06-27 15:51:29 +0000755 if (name[0] == '\0') {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000756 int siz;
757 memset(&hints, 0, sizeof(hints));
758 hints.ai_family = af;
759 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
760 hints.ai_flags = AI_PASSIVE;
Just van Rossum1040d2c2003-05-09 07:53:18 +0000761 Py_BEGIN_ALLOW_THREADS
762 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000763 error = getaddrinfo(NULL, "0", &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +0000764 Py_END_ALLOW_THREADS
765 /* We assume that those thread-unsafe getaddrinfo() versions
766 *are* safe regarding their return value, ie. that a
767 subsequent call to getaddrinfo() does not destroy the
768 outcome of the first call. */
769 RELEASE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000770 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000771 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000772 return -1;
773 }
774 switch (res->ai_family) {
775 case AF_INET:
776 siz = 4;
777 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000778#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000779 case AF_INET6:
780 siz = 16;
781 break;
782#endif
783 default:
784 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000785 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000786 "unsupported address family");
787 return -1;
788 }
789 if (res->ai_next) {
Martin v. Löwisf0b11d22001-11-07 08:31:03 +0000790 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000791 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000792 "wildcard resolved to multiple address");
793 return -1;
794 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000795 if (res->ai_addrlen < addr_ret_size)
796 addr_ret_size = res->ai_addrlen;
797 memcpy(addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000798 freeaddrinfo(res);
799 return siz;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000800 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000801 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000802 struct sockaddr_in *sin;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +0000803 if (af != AF_INET && af != AF_UNSPEC) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000804 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000805 "address family mismatched");
806 return -1;
807 }
808 sin = (struct sockaddr_in *)addr_ret;
809 memset((void *) sin, '\0', sizeof(*sin));
810 sin->sin_family = AF_INET;
811#ifdef HAVE_SOCKADDR_SA_LEN
812 sin->sin_len = sizeof(*sin);
813#endif
814 sin->sin_addr.s_addr = INADDR_BROADCAST;
815 return sizeof(sin->sin_addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000816 }
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000817 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
818 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
819 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
820 struct sockaddr_in *sin;
821 sin = (struct sockaddr_in *)addr_ret;
822 sin->sin_addr.s_addr = htonl(
823 ((long) d1 << 24) | ((long) d2 << 16) |
824 ((long) d3 << 8) | ((long) d4 << 0));
825 sin->sin_family = AF_INET;
826#ifdef HAVE_SOCKADDR_SA_LEN
827 sin->sin_len = sizeof(*sin);
828#endif
829 return 4;
830 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000831 memset(&hints, 0, sizeof(hints));
832 hints.ai_family = af;
Just van Rossum1040d2c2003-05-09 07:53:18 +0000833 Py_BEGIN_ALLOW_THREADS
834 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000835 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000836#if defined(__digital__) && defined(__unix__)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000837 if (error == EAI_NONAME && af == AF_UNSPEC) {
838 /* On Tru64 V5.1, numeric-to-addr conversion fails
839 if no address family is given. Assume IPv4 for now.*/
840 hints.ai_family = AF_INET;
841 error = getaddrinfo(name, NULL, &hints, &res);
842 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000843#endif
Just van Rossum1040d2c2003-05-09 07:53:18 +0000844 Py_END_ALLOW_THREADS
845 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000846 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000847 set_gaierror(error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000848 return -1;
849 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000850 if (res->ai_addrlen < addr_ret_size)
851 addr_ret_size = res->ai_addrlen;
852 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000853 freeaddrinfo(res);
854 switch (addr_ret->sa_family) {
855 case AF_INET:
856 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000857#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000858 case AF_INET6:
859 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000860#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000861 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000862 PyErr_SetString(socket_error, "unknown address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000863 return -1;
864 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000865}
866
Guido van Rossum30a685f1991-06-27 15:51:29 +0000867
Guido van Rossum30a685f1991-06-27 15:51:29 +0000868/* Create a string object representing an IP address.
869 This is always a string of the form 'dd.dd.dd.dd' (with variable
870 size numbers). */
871
Guido van Rossum73624e91994-10-10 17:59:00 +0000872static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000873makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000874{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000875 char buf[NI_MAXHOST];
876 int error;
877
878 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
879 NI_NUMERICHOST);
880 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000881 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000882 return NULL;
883 }
Guido van Rossum32c4ac02007-08-15 03:56:40 +0000884 return PyUnicode_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000885}
886
887
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000888#ifdef USE_BLUETOOTH
889/* Convert a string representation of a Bluetooth address into a numeric
890 address. Returns the length (6), or raises an exception and returns -1 if
891 an error occurred. */
892
893static int
894setbdaddr(char *name, bdaddr_t *bdaddr)
895{
896 unsigned int b0, b1, b2, b3, b4, b5;
897 char ch;
898 int n;
899
900 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
901 &b5, &b4, &b3, &b2, &b1, &b0, &ch);
902 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
903 bdaddr->b[0] = b0;
904 bdaddr->b[1] = b1;
905 bdaddr->b[2] = b2;
906 bdaddr->b[3] = b3;
907 bdaddr->b[4] = b4;
908 bdaddr->b[5] = b5;
909 return 6;
910 } else {
911 PyErr_SetString(socket_error, "bad bluetooth address");
912 return -1;
913 }
914}
915
916/* Create a string representation of the Bluetooth address. This is always a
917 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
918 value (zero padded if necessary). */
919
920static PyObject *
921makebdaddr(bdaddr_t *bdaddr)
922{
923 char buf[(6 * 2) + 5 + 1];
924
925 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
926 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
927 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
Guido van Rossum32c4ac02007-08-15 03:56:40 +0000928 return PyUnicode_FromString(buf);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000929}
930#endif
931
932
Guido van Rossum30a685f1991-06-27 15:51:29 +0000933/* Create an object representing the given socket address,
934 suitable for passing it back to bind(), connect() etc.
935 The family field of the sockaddr structure is inspected
936 to determine what kind of address it really is. */
937
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000938/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000939static PyObject *
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000940makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000941{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000942 if (addrlen == 0) {
943 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +0000944 Py_INCREF(Py_None);
945 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000946 }
947
Guido van Rossum30a685f1991-06-27 15:51:29 +0000948 switch (addr->sa_family) {
949
950 case AF_INET:
951 {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000952 struct sockaddr_in *a;
953 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
Barry Warsaw752300b1997-01-03 17:18:10 +0000954 PyObject *ret = NULL;
955 if (addrobj) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000956 a = (struct sockaddr_in *)addr;
Barry Warsaw752300b1997-01-03 17:18:10 +0000957 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
958 Py_DECREF(addrobj);
959 }
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000960 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000961 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000962
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +0000963#if defined(AF_UNIX)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000964 case AF_UNIX:
965 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000966 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000967#ifdef linux
968 if (a->sun_path[0] == 0) { /* Linux abstract namespace */
Christian Heimesbbffeb62008-01-24 09:42:52 +0000969 addrlen -= offsetof(struct sockaddr_un, sun_path);
Christian Heimes72b710a2008-05-26 13:28:38 +0000970 return PyBytes_FromStringAndSize(a->sun_path, addrlen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000971 }
972 else
973#endif /* linux */
974 {
975 /* regular NULL-terminated string */
Guido van Rossum32c4ac02007-08-15 03:56:40 +0000976 return PyUnicode_FromString(a->sun_path);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000977 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000978 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000979#endif /* AF_UNIX */
980
Martin v. Löwis11017b12006-01-14 18:12:57 +0000981#if defined(AF_NETLINK)
982 case AF_NETLINK:
983 {
984 struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
Martin v. Löwisd0560052006-01-15 07:49:20 +0000985 return Py_BuildValue("II", a->nl_pid, a->nl_groups);
Martin v. Löwis11017b12006-01-14 18:12:57 +0000986 }
987#endif /* AF_NETLINK */
988
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000989#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000990 case AF_INET6:
991 {
992 struct sockaddr_in6 *a;
993 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
994 PyObject *ret = NULL;
995 if (addrobj) {
996 a = (struct sockaddr_in6 *)addr;
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000997 ret = Py_BuildValue("Oiii",
998 addrobj,
999 ntohs(a->sin6_port),
1000 a->sin6_flowinfo,
1001 a->sin6_scope_id);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001002 Py_DECREF(addrobj);
1003 }
1004 return ret;
1005 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001006#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001007
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001008#ifdef USE_BLUETOOTH
1009 case AF_BLUETOOTH:
1010 switch (proto) {
1011
1012 case BTPROTO_L2CAP:
1013 {
1014 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
1015 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
1016 PyObject *ret = NULL;
1017 if (addrobj) {
1018 ret = Py_BuildValue("Oi",
1019 addrobj,
1020 _BT_L2_MEMB(a, psm));
1021 Py_DECREF(addrobj);
1022 }
1023 return ret;
1024 }
1025
1026 case BTPROTO_RFCOMM:
1027 {
1028 struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
1029 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
1030 PyObject *ret = NULL;
1031 if (addrobj) {
1032 ret = Py_BuildValue("Oi",
1033 addrobj,
1034 _BT_RC_MEMB(a, channel));
1035 Py_DECREF(addrobj);
1036 }
1037 return ret;
1038 }
1039
Thomas Wouterscf297e42007-02-23 15:07:44 +00001040 case BTPROTO_HCI:
1041 {
1042 struct sockaddr_hci *a = (struct sockaddr_hci *) addr;
1043 PyObject *ret = NULL;
1044 ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
1045 return ret;
1046 }
1047
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001048#if !defined(__FreeBSD__)
1049 case BTPROTO_SCO:
1050 {
1051 struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
1052 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
1053 }
1054#endif
1055
1056 }
1057#endif
1058
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001059#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001060 case AF_PACKET:
1061 {
1062 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
1063 char *ifname = "";
1064 struct ifreq ifr;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001065 /* need to look up interface name give index */
1066 if (a->sll_ifindex) {
Jeremy Hylton22308652001-02-02 03:23:09 +00001067 ifr.ifr_ifindex = a->sll_ifindex;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001068 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
Jeremy Hylton22308652001-02-02 03:23:09 +00001069 ifname = ifr.ifr_name;
Jeremy Hylton22308652001-02-02 03:23:09 +00001070 }
Guido van Rossum32c4ac02007-08-15 03:56:40 +00001071 return Py_BuildValue("shbhy#",
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001072 ifname,
1073 ntohs(a->sll_protocol),
1074 a->sll_pkttype,
1075 a->sll_hatype,
1076 a->sll_addr,
1077 a->sll_halen);
Jeremy Hylton22308652001-02-02 03:23:09 +00001078 }
1079#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001080
Christian Heimes043d6f62008-01-07 17:19:16 +00001081#ifdef HAVE_LINUX_TIPC_H
1082 case AF_TIPC:
1083 {
1084 struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr;
1085 if (a->addrtype == TIPC_ADDR_NAMESEQ) {
1086 return Py_BuildValue("IIIII",
1087 a->addrtype,
1088 a->addr.nameseq.type,
1089 a->addr.nameseq.lower,
1090 a->addr.nameseq.upper,
1091 a->scope);
1092 } else if (a->addrtype == TIPC_ADDR_NAME) {
1093 return Py_BuildValue("IIIII",
1094 a->addrtype,
1095 a->addr.name.name.type,
1096 a->addr.name.name.instance,
1097 a->addr.name.name.instance,
1098 a->scope);
1099 } else if (a->addrtype == TIPC_ADDR_ID) {
1100 return Py_BuildValue("IIIII",
1101 a->addrtype,
1102 a->addr.id.node,
1103 a->addr.id.ref,
1104 0,
1105 a->scope);
1106 } else {
1107 PyErr_SetString(PyExc_TypeError,
1108 "Invalid address type");
1109 return NULL;
1110 }
1111 }
1112#endif
1113
Guido van Rossum30a685f1991-06-27 15:51:29 +00001114 /* More cases here... */
1115
1116 default:
Guido van Rossumaa948df1997-05-07 17:41:48 +00001117 /* If we don't know the address family, don't raise an
Guido van Rossum9ce8e382007-08-09 21:06:37 +00001118 exception -- return it as an (int, bytes) tuple. */
1119 return Py_BuildValue("iy#",
Guido van Rossumaa948df1997-05-07 17:41:48 +00001120 addr->sa_family,
1121 addr->sa_data,
1122 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001123
Guido van Rossum30a685f1991-06-27 15:51:29 +00001124 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001125}
1126
Guido van Rossum30a685f1991-06-27 15:51:29 +00001127
1128/* Parse a socket address argument according to the socket object's
1129 address family. Return 1 if the address was in the proper format,
1130 0 of not. The address is returned through addr_ret, its length
1131 through len_ret. */
1132
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001133static int
Guido van Rossum48a680c2001-03-02 06:34:14 +00001134getsockaddrarg(PySocketSockObject *s, PyObject *args,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001135 struct sockaddr *addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001136{
Guido van Rossum30a685f1991-06-27 15:51:29 +00001137 switch (s->sock_family) {
1138
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001139#if defined(AF_UNIX)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001140 case AF_UNIX:
1141 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001142 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001143 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001144 int len;
Guido van Rossume22905a2007-08-27 23:09:25 +00001145 if (!PyArg_Parse(args, "s#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001146 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001147
1148 addr = (struct sockaddr_un*)addr_ret;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001149#ifdef linux
1150 if (len > 0 && path[0] == 0) {
1151 /* Linux abstract namespace extension */
1152 if (len > sizeof addr->sun_path) {
1153 PyErr_SetString(socket_error,
1154 "AF_UNIX path too long");
1155 return 0;
1156 }
1157 }
1158 else
1159#endif /* linux */
1160 {
1161 /* regular NULL-terminated string */
1162 if (len >= sizeof addr->sun_path) {
1163 PyErr_SetString(socket_error,
1164 "AF_UNIX path too long");
1165 return 0;
1166 }
1167 addr->sun_path[len] = 0;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001168 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001169 addr->sun_family = s->sock_family;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001170 memcpy(addr->sun_path, path, len);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001171#if defined(PYOS_OS2)
1172 *len_ret = sizeof(*addr);
1173#else
Christian Heimesbbffeb62008-01-24 09:42:52 +00001174 *len_ret = len + offsetof(struct sockaddr_un, sun_path);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001175#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001176 return 1;
1177 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001178#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001179
Martin v. Löwis11017b12006-01-14 18:12:57 +00001180#if defined(AF_NETLINK)
1181 case AF_NETLINK:
1182 {
1183 struct sockaddr_nl* addr;
1184 int pid, groups;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001185 addr = (struct sockaddr_nl *)addr_ret;
Martin v. Löwis11017b12006-01-14 18:12:57 +00001186 if (!PyTuple_Check(args)) {
1187 PyErr_Format(
1188 PyExc_TypeError,
1189 "getsockaddrarg: "
1190 "AF_NETLINK address must be tuple, not %.500s",
Christian Heimes90aa7642007-12-19 02:45:37 +00001191 Py_TYPE(args)->tp_name);
Martin v. Löwis11017b12006-01-14 18:12:57 +00001192 return 0;
1193 }
1194 if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
1195 return 0;
1196 addr->nl_family = AF_NETLINK;
1197 addr->nl_pid = pid;
1198 addr->nl_groups = groups;
Martin v. Löwis11017b12006-01-14 18:12:57 +00001199 *len_ret = sizeof(*addr);
1200 return 1;
1201 }
1202#endif
1203
Guido van Rossum30a685f1991-06-27 15:51:29 +00001204 case AF_INET:
1205 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001206 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001207 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001208 int port, result;
Guido van Rossume4dad902000-12-01 13:13:11 +00001209 if (!PyTuple_Check(args)) {
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001210 PyErr_Format(
1211 PyExc_TypeError,
1212 "getsockaddrarg: "
1213 "AF_INET address must be tuple, not %.500s",
Christian Heimes90aa7642007-12-19 02:45:37 +00001214 Py_TYPE(args)->tp_name);
Guido van Rossume4dad902000-12-01 13:13:11 +00001215 return 0;
1216 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001217 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
Martin v. Löwis2548c732003-04-18 10:39:54 +00001218 "idna", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001219 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001220 addr=(struct sockaddr_in*)addr_ret;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001221 result = setipaddr(host, (struct sockaddr *)addr,
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001222 sizeof(*addr), AF_INET);
1223 PyMem_Free(host);
1224 if (result < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001225 return 0;
Benjamin Peterson5c6d7872009-02-06 02:40:07 +00001226 if (port < 0 || port > 0xffff) {
1227 PyErr_SetString(
1228 PyExc_OverflowError,
1229 "getsockaddrarg: port must be 0-65535.");
1230 return 0;
1231 }
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001232 addr->sin_family = AF_INET;
Guido van Rossum644a12b1997-04-09 19:24:53 +00001233 addr->sin_port = htons((short)port);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001234 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001235 return 1;
1236 }
1237
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001238#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001239 case AF_INET6:
1240 {
1241 struct sockaddr_in6* addr;
1242 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001243 int port, flowinfo, scope_id, result;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001244 flowinfo = scope_id = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001245 if (!PyTuple_Check(args)) {
1246 PyErr_Format(
1247 PyExc_TypeError,
1248 "getsockaddrarg: "
1249 "AF_INET6 address must be tuple, not %.500s",
Christian Heimes90aa7642007-12-19 02:45:37 +00001250 Py_TYPE(args)->tp_name);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001251 return 0;
1252 }
1253 if (!PyArg_ParseTuple(args, "eti|ii",
Martin v. Löwis2548c732003-04-18 10:39:54 +00001254 "idna", &host, &port, &flowinfo,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001255 &scope_id)) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001256 return 0;
1257 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001258 addr = (struct sockaddr_in6*)addr_ret;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001259 result = setipaddr(host, (struct sockaddr *)addr,
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001260 sizeof(*addr), AF_INET6);
1261 PyMem_Free(host);
1262 if (result < 0)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001263 return 0;
Benjamin Peterson5c6d7872009-02-06 02:40:07 +00001264 if (port < 0 || port > 0xffff) {
1265 PyErr_SetString(
1266 PyExc_OverflowError,
1267 "getsockaddrarg: port must be 0-65535.");
1268 return 0;
1269 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001270 addr->sin6_family = s->sock_family;
1271 addr->sin6_port = htons((short)port);
1272 addr->sin6_flowinfo = flowinfo;
1273 addr->sin6_scope_id = scope_id;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001274 *len_ret = sizeof *addr;
1275 return 1;
1276 }
1277#endif
1278
Hye-Shik Chang81268602004-02-02 06:05:24 +00001279#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00001280 case AF_BLUETOOTH:
1281 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001282 switch (s->sock_proto) {
1283 case BTPROTO_L2CAP:
Martin v. Löwis12af0482004-01-31 12:34:17 +00001284 {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001285 struct sockaddr_l2 *addr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001286 char *straddr;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001287
Thomas Wouters89f507f2006-12-13 04:49:30 +00001288 addr = (struct sockaddr_l2 *)addr_ret;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001289 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
1290 if (!PyArg_ParseTuple(args, "si", &straddr,
1291 &_BT_L2_MEMB(addr, psm))) {
1292 PyErr_SetString(socket_error, "getsockaddrarg: "
1293 "wrong format");
Martin v. Löwis12af0482004-01-31 12:34:17 +00001294 return 0;
1295 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001296 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
1297 return 0;
1298
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001299 *len_ret = sizeof *addr;
1300 return 1;
1301 }
1302 case BTPROTO_RFCOMM:
1303 {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001304 struct sockaddr_rc *addr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001305 char *straddr;
1306
Thomas Wouters89f507f2006-12-13 04:49:30 +00001307 addr = (struct sockaddr_rc *)addr_ret;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001308 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
1309 if (!PyArg_ParseTuple(args, "si", &straddr,
1310 &_BT_RC_MEMB(addr, channel))) {
1311 PyErr_SetString(socket_error, "getsockaddrarg: "
1312 "wrong format");
1313 return 0;
1314 }
1315 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
1316 return 0;
1317
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001318 *len_ret = sizeof *addr;
1319 return 1;
1320 }
Thomas Wouterscf297e42007-02-23 15:07:44 +00001321 case BTPROTO_HCI:
1322 {
1323 struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret;
1324 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
1325 if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) {
1326 PyErr_SetString(socket_error, "getsockaddrarg: "
1327 "wrong format");
1328 return 0;
1329 }
1330 *len_ret = sizeof *addr;
1331 return 1;
1332 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001333#if !defined(__FreeBSD__)
1334 case BTPROTO_SCO:
1335 {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001336 struct sockaddr_sco *addr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001337 char *straddr;
1338
Thomas Wouters89f507f2006-12-13 04:49:30 +00001339 addr = (struct sockaddr_sco *)addr_ret;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001340 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
Christian Heimes72b710a2008-05-26 13:28:38 +00001341 if (!PyBytes_Check(args)) {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001342 PyErr_SetString(socket_error, "getsockaddrarg: "
1343 "wrong format");
1344 return 0;
1345 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001346 straddr = PyBytes_AS_STRING(args);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001347 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
1348 return 0;
1349
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001350 *len_ret = sizeof *addr;
1351 return 1;
1352 }
1353#endif
1354 default:
1355 PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol");
1356 return 0;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001357 }
1358 }
1359#endif
1360
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001361#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001362 case AF_PACKET:
1363 {
1364 struct sockaddr_ll* addr;
1365 struct ifreq ifr;
1366 char *interfaceName;
1367 int protoNumber;
1368 int hatype = 0;
1369 int pkttype = 0;
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001370 char *haddr = NULL;
1371 unsigned int halen = 0;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001372
Thomas Wouters477c8d52006-05-27 19:21:47 +00001373 if (!PyTuple_Check(args)) {
1374 PyErr_Format(
1375 PyExc_TypeError,
1376 "getsockaddrarg: "
1377 "AF_PACKET address must be tuple, not %.500s",
Christian Heimes90aa7642007-12-19 02:45:37 +00001378 Py_TYPE(args)->tp_name);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001379 return 0;
1380 }
Guido van Rossum8a392d72007-11-21 22:09:45 +00001381 if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName,
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001382 &protoNumber, &pkttype, &hatype,
1383 &haddr, &halen))
Jeremy Hylton22308652001-02-02 03:23:09 +00001384 return 0;
1385 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1386 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001387 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001388 s->errorhandler();
Jeremy Hylton22308652001-02-02 03:23:09 +00001389 return 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001390 }
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001391 if (halen > 8) {
1392 PyErr_SetString(PyExc_ValueError,
1393 "Hardware address must be 8 bytes or less");
1394 return 0;
1395 }
Benjamin Peterson5c6d7872009-02-06 02:40:07 +00001396 if (protoNumber < 0 || protoNumber > 0xffff) {
1397 PyErr_SetString(
1398 PyExc_OverflowError,
1399 "getsockaddrarg: protoNumber must be 0-65535.");
1400 return 0;
1401 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001402 addr = (struct sockaddr_ll*)addr_ret;
1403 addr->sll_family = AF_PACKET;
1404 addr->sll_protocol = htons((short)protoNumber);
1405 addr->sll_ifindex = ifr.ifr_ifindex;
1406 addr->sll_pkttype = pkttype;
1407 addr->sll_hatype = hatype;
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001408 if (halen != 0) {
1409 memcpy(&addr->sll_addr, haddr, halen);
1410 }
1411 addr->sll_halen = halen;
Jeremy Hylton22308652001-02-02 03:23:09 +00001412 *len_ret = sizeof *addr;
1413 return 1;
1414 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001415#endif
1416
Christian Heimes043d6f62008-01-07 17:19:16 +00001417#ifdef HAVE_LINUX_TIPC_H
1418 case AF_TIPC:
1419 {
1420 unsigned int atype, v1, v2, v3;
1421 unsigned int scope = TIPC_CLUSTER_SCOPE;
1422 struct sockaddr_tipc *addr;
1423
1424 if (!PyTuple_Check(args)) {
1425 PyErr_Format(
1426 PyExc_TypeError,
1427 "getsockaddrarg: "
1428 "AF_TIPC address must be tuple, not %.500s",
1429 Py_TYPE(args)->tp_name);
1430 return 0;
1431 }
1432
1433 if (!PyArg_ParseTuple(args,
1434 "IIII|I;Invalid TIPC address format",
1435 &atype, &v1, &v2, &v3, &scope))
1436 return 0;
1437
1438 addr = (struct sockaddr_tipc *) addr_ret;
1439 memset(addr, 0, sizeof(struct sockaddr_tipc));
1440
1441 addr->family = AF_TIPC;
1442 addr->scope = scope;
1443 addr->addrtype = atype;
1444
1445 if (atype == TIPC_ADDR_NAMESEQ) {
1446 addr->addr.nameseq.type = v1;
1447 addr->addr.nameseq.lower = v2;
1448 addr->addr.nameseq.upper = v3;
1449 } else if (atype == TIPC_ADDR_NAME) {
1450 addr->addr.name.name.type = v1;
1451 addr->addr.name.name.instance = v2;
1452 } else if (atype == TIPC_ADDR_ID) {
1453 addr->addr.id.node = v1;
1454 addr->addr.id.ref = v2;
1455 } else {
1456 /* Shouldn't happen */
1457 PyErr_SetString(PyExc_TypeError, "Invalid address type");
1458 return 0;
1459 }
1460
1461 *len_ret = sizeof(*addr);
1462
1463 return 1;
1464 }
1465#endif
1466
Guido van Rossum30a685f1991-06-27 15:51:29 +00001467 /* More cases here... */
1468
1469 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001470 PyErr_SetString(socket_error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001471 return 0;
1472
1473 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001474}
1475
Guido van Rossum30a685f1991-06-27 15:51:29 +00001476
Guido van Rossum48a680c2001-03-02 06:34:14 +00001477/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00001478 Return 1 if the family is known, 0 otherwise. The length is returned
1479 through len_ret. */
1480
1481static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00001482getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001483{
1484 switch (s->sock_family) {
1485
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001486#if defined(AF_UNIX)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001487 case AF_UNIX:
1488 {
1489 *len_ret = sizeof (struct sockaddr_un);
1490 return 1;
1491 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001492#endif /* AF_UNIX */
Martin v. Löwis11017b12006-01-14 18:12:57 +00001493#if defined(AF_NETLINK)
1494 case AF_NETLINK:
1495 {
1496 *len_ret = sizeof (struct sockaddr_nl);
1497 return 1;
1498 }
1499#endif
Guido van Rossum710e1df1992-06-12 10:39:36 +00001500
1501 case AF_INET:
1502 {
1503 *len_ret = sizeof (struct sockaddr_in);
1504 return 1;
1505 }
1506
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001507#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001508 case AF_INET6:
1509 {
1510 *len_ret = sizeof (struct sockaddr_in6);
1511 return 1;
1512 }
1513#endif
1514
Hye-Shik Chang81268602004-02-02 06:05:24 +00001515#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00001516 case AF_BLUETOOTH:
1517 {
1518 switch(s->sock_proto)
1519 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001520
1521 case BTPROTO_L2CAP:
1522 *len_ret = sizeof (struct sockaddr_l2);
1523 return 1;
1524 case BTPROTO_RFCOMM:
1525 *len_ret = sizeof (struct sockaddr_rc);
1526 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001527 case BTPROTO_HCI:
1528 *len_ret = sizeof (struct sockaddr_hci);
1529 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001530#if !defined(__FreeBSD__)
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001531 case BTPROTO_SCO:
1532 *len_ret = sizeof (struct sockaddr_sco);
1533 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001534#endif
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001535 default:
1536 PyErr_SetString(socket_error, "getsockaddrlen: "
1537 "unknown BT protocol");
1538 return 0;
1539
Martin v. Löwis12af0482004-01-31 12:34:17 +00001540 }
1541 }
1542#endif
1543
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001544#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001545 case AF_PACKET:
1546 {
1547 *len_ret = sizeof (struct sockaddr_ll);
1548 return 1;
1549 }
1550#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001551
Christian Heimes043d6f62008-01-07 17:19:16 +00001552#ifdef HAVE_LINUX_TIPC_H
1553 case AF_TIPC:
1554 {
1555 *len_ret = sizeof (struct sockaddr_tipc);
1556 return 1;
1557 }
1558#endif
1559
Guido van Rossum710e1df1992-06-12 10:39:36 +00001560 /* More cases here... */
1561
1562 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001563 PyErr_SetString(socket_error, "getsockaddrlen: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +00001564 return 0;
1565
1566 }
1567}
1568
1569
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001570/* s._accept() -> (fd, address) */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001571
Guido van Rossum73624e91994-10-10 17:59:00 +00001572static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001573sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001574{
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001575 sock_addr_t addrbuf;
Guido van Rossum7d0a8262007-05-21 23:13:11 +00001576 SOCKET_T newfd = INVALID_SOCKET;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001577 socklen_t addrlen;
Barry Warsaw752300b1997-01-03 17:18:10 +00001578 PyObject *sock = NULL;
1579 PyObject *addr = NULL;
1580 PyObject *res = NULL;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001581 int timeout;
Barry Warsaw752300b1997-01-03 17:18:10 +00001582
Guido van Rossum710e1df1992-06-12 10:39:36 +00001583 if (!getsockaddrlen(s, &addrlen))
1584 return NULL;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001585 memset(&addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001586
Neal Norwitz082b2df2006-02-07 07:04:46 +00001587 if (!IS_SELECTABLE(s))
1588 return select_error();
1589
Guido van Rossum73624e91994-10-10 17:59:00 +00001590 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001591 timeout = internal_select(s, 0);
1592 if (!timeout)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001593 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001594 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001595
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001596 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001597 PyErr_SetString(socket_timeout, "timed out");
1598 return NULL;
1599 }
1600
Fred Drakea04eaad2000-06-30 02:46:07 +00001601 if (newfd == INVALID_SOCKET)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001602 return s->errorhandler();
Barry Warsaw752300b1997-01-03 17:18:10 +00001603
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001604 sock = PyLong_FromSocket_t(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +00001605 if (sock == NULL) {
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001606 SOCKETCLOSE(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +00001607 goto finally;
1608 }
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001609
Thomas Wouters89f507f2006-12-13 04:49:30 +00001610 addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001611 addrlen, s->sock_proto);
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001612 if (addr == NULL)
Barry Warsaw752300b1997-01-03 17:18:10 +00001613 goto finally;
1614
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001615 res = PyTuple_Pack(2, sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001616
Guido van Rossum67f7a382002-06-06 21:08:16 +00001617finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001618 Py_XDECREF(sock);
1619 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001620 return res;
1621}
1622
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001623PyDoc_STRVAR(accept_doc,
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001624"_accept() -> (integer, address info)\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001625\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001626Wait for an incoming connection. Return a new socket file descriptor\n\
1627representing the connection, and the address of the client.\n\
1628For IP sockets, the address info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001629
Guido van Rossum11ba0942002-06-13 15:07:44 +00001630/* s.setblocking(flag) method. Argument:
1631 False -- non-blocking mode; same as settimeout(0)
1632 True -- blocking mode; same as settimeout(None)
1633*/
Guido van Rossume4485b01994-09-07 14:32:49 +00001634
Guido van Rossum73624e91994-10-10 17:59:00 +00001635static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001636sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00001637{
1638 int block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001639
Christian Heimes217cfd12007-12-02 14:31:20 +00001640 block = PyLong_AsLong(arg);
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001641 if (block == -1 && PyErr_Occurred())
Guido van Rossume4485b01994-09-07 14:32:49 +00001642 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001643
Guido van Rossum11ba0942002-06-13 15:07:44 +00001644 s->sock_timeout = block ? -1.0 : 0.0;
Guido van Rossume8008f02002-06-07 03:36:20 +00001645 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00001646
Guido van Rossum73624e91994-10-10 17:59:00 +00001647 Py_INCREF(Py_None);
1648 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00001649}
Guido van Rossume4485b01994-09-07 14:32:49 +00001650
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001651PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001652"setblocking(flag)\n\
1653\n\
1654Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001655setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001656setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001657
Guido van Rossum11ba0942002-06-13 15:07:44 +00001658/* s.settimeout(timeout) method. Argument:
1659 None -- no timeout, blocking mode; same as setblocking(True)
1660 0.0 -- non-blocking mode; same as setblocking(False)
1661 > 0 -- timeout mode; operations time out after timeout seconds
1662 < 0 -- illegal; raises an exception
1663*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00001664static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001665sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001666{
Guido van Rossum11ba0942002-06-13 15:07:44 +00001667 double timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001668
1669 if (arg == Py_None)
Guido van Rossum11ba0942002-06-13 15:07:44 +00001670 timeout = -1.0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001671 else {
Guido van Rossum11ba0942002-06-13 15:07:44 +00001672 timeout = PyFloat_AsDouble(arg);
1673 if (timeout < 0.0) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00001674 if (!PyErr_Occurred())
1675 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001676 "Timeout value out of range");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001677 return NULL;
1678 }
1679 }
1680
Guido van Rossum11ba0942002-06-13 15:07:44 +00001681 s->sock_timeout = timeout;
1682 internal_setblocking(s, timeout < 0.0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001683
1684 Py_INCREF(Py_None);
1685 return Py_None;
1686}
1687
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001688PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001689"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001690\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001691Set a timeout on socket operations. 'timeout' can be a float,\n\
1692giving in seconds, or None. Setting a timeout of None disables\n\
1693the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001694Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001695
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001696/* s.gettimeout() method.
1697 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001698static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001699sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001700{
1701 if (s->sock_timeout < 0.0) {
1702 Py_INCREF(Py_None);
1703 return Py_None;
1704 }
1705 else
1706 return PyFloat_FromDouble(s->sock_timeout);
1707}
1708
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001709PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001710"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001711\n\
1712Returns the timeout in floating seconds associated with socket \n\
1713operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001714operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00001715
Guido van Rossumaee08791992-09-08 09:05:33 +00001716/* s.setsockopt() method.
1717 With an integer third argument, sets an integer option.
1718 With a string third argument, sets an option from a buffer;
1719 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001720
Guido van Rossum73624e91994-10-10 17:59:00 +00001721static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001722sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001723{
1724 int level;
1725 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001726 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +00001727 char *buf;
1728 int buflen;
1729 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001730
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001731 if (PyArg_ParseTuple(args, "iii:setsockopt",
1732 &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001733 buf = (char *) &flag;
1734 buflen = sizeof flag;
1735 }
1736 else {
Guido van Rossum73624e91994-10-10 17:59:00 +00001737 PyErr_Clear();
Guido van Rossum8a392d72007-11-21 22:09:45 +00001738 if (!PyArg_ParseTuple(args, "iiy#:setsockopt",
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001739 &level, &optname, &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +00001740 return NULL;
1741 }
Thomas Wouters334fb892000-07-25 12:56:38 +00001742 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001743 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001744 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001745 Py_INCREF(Py_None);
1746 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001747}
1748
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001749PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001750"setsockopt(level, option, value)\n\
1751\n\
1752Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001753The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001754
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001755
Guido van Rossumaee08791992-09-08 09:05:33 +00001756/* s.getsockopt() method.
1757 With two arguments, retrieves an integer option.
1758 With a third integer argument, retrieves a string buffer of that size;
1759 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001760
Guido van Rossum73624e91994-10-10 17:59:00 +00001761static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001762sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001763{
1764 int level;
1765 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001766 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00001767 PyObject *buf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001768 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001769
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001770 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
1771 &level, &optname, &buflen))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001772 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001773
Guido van Rossumbe32c891996-06-20 16:25:29 +00001774 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001775 int flag = 0;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001776 socklen_t flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001777 res = getsockopt(s->sock_fd, level, optname,
Thomas Wouters334fb892000-07-25 12:56:38 +00001778 (void *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +00001779 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001780 return s->errorhandler();
Christian Heimes217cfd12007-12-02 14:31:20 +00001781 return PyLong_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +00001782 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001783#ifdef __VMS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001784 /* socklen_t is unsigned so no negative test is needed,
1785 test buflen == 0 is previously done */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001786 if (buflen > 1024) {
1787#else
Guido van Rossumaee08791992-09-08 09:05:33 +00001788 if (buflen <= 0 || buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001789#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001790 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +00001791 "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +00001792 return NULL;
1793 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001794 buf = PyBytes_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001795 if (buf == NULL)
1796 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001797 res = getsockopt(s->sock_fd, level, optname,
Christian Heimes72b710a2008-05-26 13:28:38 +00001798 (void *)PyBytes_AS_STRING(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001799 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001800 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001801 return s->errorhandler();
Guido van Rossumaee08791992-09-08 09:05:33 +00001802 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001803 _PyBytes_Resize(&buf, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001804 return buf;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001805}
1806
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001807PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001808"getsockopt(level, option[, buffersize]) -> value\n\
1809\n\
1810Get a socket option. See the Unix manual for level and option.\n\
1811If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001812string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001813
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001814
Fred Drake728819a2000-07-01 03:40:12 +00001815/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001816
Guido van Rossum73624e91994-10-10 17:59:00 +00001817static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001818sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001819{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001820 sock_addr_t addrbuf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001821 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001822 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001823
Thomas Wouters89f507f2006-12-13 04:49:30 +00001824 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001825 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001826 Py_BEGIN_ALLOW_THREADS
Thomas Wouters89f507f2006-12-13 04:49:30 +00001827 res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001828 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001829 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001830 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001831 Py_INCREF(Py_None);
1832 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001833}
1834
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001835PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001836"bind(address)\n\
1837\n\
1838Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00001839pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001840sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001841
Guido van Rossum30a685f1991-06-27 15:51:29 +00001842
1843/* s.close() method.
1844 Set the file descriptor to -1 so operations tried subsequently
1845 will surely fail. */
1846
Guido van Rossum73624e91994-10-10 17:59:00 +00001847static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001848sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001849{
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001850 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001851
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001852 if ((fd = s->sock_fd) != -1) {
1853 s->sock_fd = -1;
Guido van Rossum08481461996-10-12 14:07:22 +00001854 Py_BEGIN_ALLOW_THREADS
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001855 (void) SOCKETCLOSE(fd);
Guido van Rossum08481461996-10-12 14:07:22 +00001856 Py_END_ALLOW_THREADS
1857 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001858 Py_INCREF(Py_None);
1859 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001860}
1861
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001862PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001863"close()\n\
1864\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001865Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001866
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001867static int
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001868internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
1869 int *timeoutp)
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001870{
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001871 int res, timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001872
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001873 timeout = 0;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001874 res = connect(s->sock_fd, addr, addrlen);
1875
1876#ifdef MS_WINDOWS
1877
1878 if (s->sock_timeout > 0.0) {
Neal Norwitz082b2df2006-02-07 07:04:46 +00001879 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK &&
1880 IS_SELECTABLE(s)) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001881 /* This is a mess. Best solution: trust select */
1882 fd_set fds;
Mark Hammonda57ec932004-08-03 05:06:26 +00001883 fd_set fds_exc;
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001884 struct timeval tv;
1885 tv.tv_sec = (int)s->sock_timeout;
1886 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
1887 FD_ZERO(&fds);
1888 FD_SET(s->sock_fd, &fds);
Mark Hammonda57ec932004-08-03 05:06:26 +00001889 FD_ZERO(&fds_exc);
1890 FD_SET(s->sock_fd, &fds_exc);
1891 res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001892 if (res == 0) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001893 res = WSAEWOULDBLOCK;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001894 timeout = 1;
Mark Hammonda57ec932004-08-03 05:06:26 +00001895 } else if (res > 0) {
1896 if (FD_ISSET(s->sock_fd, &fds))
Sean Reifscheider54cf12b2007-09-17 17:55:36 +00001897 /* The socket is in the writable set - this
Mark Hammonda57ec932004-08-03 05:06:26 +00001898 means connected */
1899 res = 0;
1900 else {
1901 /* As per MS docs, we need to call getsockopt()
1902 to get the underlying error */
1903 int res_size = sizeof res;
1904 /* It must be in the exception set */
1905 assert(FD_ISSET(s->sock_fd, &fds_exc));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001906 if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
Mark Hammonda57ec932004-08-03 05:06:26 +00001907 (char *)&res, &res_size))
1908 /* getsockopt also clears WSAGetLastError,
1909 so reset it back. */
1910 WSASetLastError(res);
1911 else
1912 res = WSAGetLastError();
1913 }
1914 }
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001915 /* else if (res < 0) an error occurred */
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001916 }
1917 }
1918
1919 if (res < 0)
1920 res = WSAGetLastError();
1921
1922#else
1923
1924 if (s->sock_timeout > 0.0) {
Christian Heimesfdab48e2008-01-20 09:06:41 +00001925 if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) {
1926 timeout = internal_select(s, 1);
1927 if (timeout == 0) {
1928 /* Bug #1019808: in case of an EINPROGRESS,
1929 use getsockopt(SO_ERROR) to get the real
1930 error. */
1931 socklen_t res_size = sizeof res;
1932 (void)getsockopt(s->sock_fd, SOL_SOCKET,
1933 SO_ERROR, &res, &res_size);
1934 if (res == EISCONN)
1935 res = 0;
1936 errno = res;
1937 }
1938 else if (timeout == -1) {
1939 res = errno; /* had error */
1940 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001941 else
1942 res = EWOULDBLOCK; /* timed out */
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001943 }
1944 }
1945
1946 if (res < 0)
1947 res = errno;
1948
1949#endif
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001950 *timeoutp = timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001951
1952 return res;
1953}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001954
Fred Drake728819a2000-07-01 03:40:12 +00001955/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001956
Guido van Rossum73624e91994-10-10 17:59:00 +00001957static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001958sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001959{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001960 sock_addr_t addrbuf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001961 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001962 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001963 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001964
Thomas Wouters89f507f2006-12-13 04:49:30 +00001965 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001966 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001967
Guido van Rossum73624e91994-10-10 17:59:00 +00001968 Py_BEGIN_ALLOW_THREADS
Thomas Wouters89f507f2006-12-13 04:49:30 +00001969 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
Guido van Rossum11ba0942002-06-13 15:07:44 +00001970 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001971
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001972 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001973 PyErr_SetString(socket_timeout, "timed out");
1974 return NULL;
1975 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001976 if (res != 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001977 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001978 Py_INCREF(Py_None);
1979 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001980}
1981
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001982PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001983"connect(address)\n\
1984\n\
1985Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001986is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001987
Guido van Rossum30a685f1991-06-27 15:51:29 +00001988
Fred Drake728819a2000-07-01 03:40:12 +00001989/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001990
1991static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001992sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001993{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001994 sock_addr_t addrbuf;
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001995 int addrlen;
1996 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001997 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001998
Thomas Wouters89f507f2006-12-13 04:49:30 +00001999 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002000 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002001
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002002 Py_BEGIN_ALLOW_THREADS
Thomas Wouters89f507f2006-12-13 04:49:30 +00002003 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002004 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002005
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002006 /* Signals are not errors (though they may raise exceptions). Adapted
2007 from PyErr_SetFromErrnoWithFilenameObject(). */
2008#ifdef EINTR
2009 if (res == EINTR && PyErr_CheckSignals())
2010 return NULL;
2011#endif
2012
Christian Heimes217cfd12007-12-02 14:31:20 +00002013 return PyLong_FromLong((long) res);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002014}
2015
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002016PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002017"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002018\n\
2019This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002020instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002021
Guido van Rossumfc4255d1997-11-19 18:57:13 +00002022
Guido van Rossumed233a51992-06-23 09:07:03 +00002023/* s.fileno() method */
2024
Guido van Rossum73624e91994-10-10 17:59:00 +00002025static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002026sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00002027{
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002028 return PyLong_FromSocket_t(s->sock_fd);
Guido van Rossumed233a51992-06-23 09:07:03 +00002029}
2030
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002031PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002032"fileno() -> integer\n\
2033\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002034Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002035
Guido van Rossumed233a51992-06-23 09:07:03 +00002036
Guido van Rossumc89705d1992-11-26 08:54:07 +00002037/* s.getsockname() method */
2038
Guido van Rossum73624e91994-10-10 17:59:00 +00002039static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002040sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002041{
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002042 sock_addr_t addrbuf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002043 int res;
2044 socklen_t addrlen;
2045
Guido van Rossumc89705d1992-11-26 08:54:07 +00002046 if (!getsockaddrlen(s, &addrlen))
2047 return NULL;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002048 memset(&addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002049 Py_BEGIN_ALLOW_THREADS
Thomas Wouters89f507f2006-12-13 04:49:30 +00002050 res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002051 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00002052 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002053 return s->errorhandler();
Thomas Wouters89f507f2006-12-13 04:49:30 +00002054 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00002055 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002056}
2057
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002058PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002059"getsockname() -> address info\n\
2060\n\
2061Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002062info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002063
Guido van Rossumc89705d1992-11-26 08:54:07 +00002064
Guido van Rossumb6775db1994-08-01 11:34:53 +00002065#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002066/* s.getpeername() method */
2067
Guido van Rossum73624e91994-10-10 17:59:00 +00002068static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002069sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002070{
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002071 sock_addr_t addrbuf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002072 int res;
2073 socklen_t addrlen;
2074
Guido van Rossumc89705d1992-11-26 08:54:07 +00002075 if (!getsockaddrlen(s, &addrlen))
2076 return NULL;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002077 memset(&addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002078 Py_BEGIN_ALLOW_THREADS
Thomas Wouters89f507f2006-12-13 04:49:30 +00002079 res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002080 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00002081 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002082 return s->errorhandler();
Thomas Wouters89f507f2006-12-13 04:49:30 +00002083 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00002084 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002085}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002086
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002087PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002088"getpeername() -> address info\n\
2089\n\
2090Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002091info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002092
Guido van Rossumb6775db1994-08-01 11:34:53 +00002093#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002094
2095
Guido van Rossum30a685f1991-06-27 15:51:29 +00002096/* s.listen(n) method */
2097
Guido van Rossum73624e91994-10-10 17:59:00 +00002098static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002099sock_listen(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002100{
2101 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002102 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002103
Christian Heimes217cfd12007-12-02 14:31:20 +00002104 backlog = PyLong_AsLong(arg);
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002105 if (backlog == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002106 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002107 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +00002108 if (backlog < 1)
2109 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002110 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +00002111 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002112 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002113 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002114 Py_INCREF(Py_None);
2115 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002116}
2117
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002118PyDoc_STRVAR(listen_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002119"listen(backlog)\n\
2120\n\
2121Enable a server to accept connections. The backlog argument must be at\n\
2122least 1; it specifies the number of unaccepted connection that the system\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002123will allow before refusing new connections.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002124
2125
Thomas Wouters477c8d52006-05-27 19:21:47 +00002126/*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002127 * This is the guts of the recv() and recv_into() methods, which reads into a
Thomas Wouters902d6eb2007-01-09 23:18:33 +00002128 * char buffer. If you have any inc/dec ref to do to the objects that contain
Thomas Wouters477c8d52006-05-27 19:21:47 +00002129 * the buffer, do it in the caller. This function returns the number of bytes
2130 * succesfully read. If there was an error, it returns -1. Note that it is
2131 * also possible that we return a number of bytes smaller than the request
2132 * bytes.
2133 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002134static ssize_t
Thomas Wouters477c8d52006-05-27 19:21:47 +00002135sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags)
2136{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002137 ssize_t outlen = -1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002138 int timeout;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002139#ifdef __VMS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002140 int remaining;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002141 char *read_buf;
2142#endif
2143
2144 if (!IS_SELECTABLE(s)) {
2145 select_error();
2146 return -1;
2147 }
Guido van Rossumc2de7c02007-08-03 22:27:51 +00002148 if (len == 0) {
2149 /* If 0 bytes were requested, do nothing. */
2150 return 0;
2151 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002152
2153#ifndef __VMS
2154 Py_BEGIN_ALLOW_THREADS
2155 timeout = internal_select(s, 0);
2156 if (!timeout)
2157 outlen = recv(s->sock_fd, cbuf, len, flags);
2158 Py_END_ALLOW_THREADS
2159
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002160 if (timeout == 1) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002161 PyErr_SetString(socket_timeout, "timed out");
2162 return -1;
2163 }
2164 if (outlen < 0) {
2165 /* Note: the call to errorhandler() ALWAYS indirectly returned
2166 NULL, so ignore its return value */
2167 s->errorhandler();
2168 return -1;
2169 }
2170#else
2171 read_buf = cbuf;
2172 remaining = len;
2173 while (remaining != 0) {
2174 unsigned int segment;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002175 int nread = -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002176
2177 segment = remaining /SEGMENT_SIZE;
2178 if (segment != 0) {
2179 segment = SEGMENT_SIZE;
2180 }
2181 else {
2182 segment = remaining;
2183 }
2184
2185 Py_BEGIN_ALLOW_THREADS
2186 timeout = internal_select(s, 0);
2187 if (!timeout)
2188 nread = recv(s->sock_fd, read_buf, segment, flags);
2189 Py_END_ALLOW_THREADS
2190
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002191 if (timeout == 1) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002192 PyErr_SetString(socket_timeout, "timed out");
2193 return -1;
2194 }
2195 if (nread < 0) {
2196 s->errorhandler();
2197 return -1;
2198 }
2199 if (nread != remaining) {
2200 read_buf += nread;
2201 break;
2202 }
2203
2204 remaining -= segment;
2205 read_buf += segment;
2206 }
2207 outlen = read_buf - cbuf;
2208#endif /* !__VMS */
2209
2210 return outlen;
2211}
2212
Guido van Rossum48a680c2001-03-02 06:34:14 +00002213
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002214/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002215
Guido van Rossum73624e91994-10-10 17:59:00 +00002216static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002217sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002218{
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002219 int recvlen, flags = 0;
2220 ssize_t outlen;
Guido van Rossum73624e91994-10-10 17:59:00 +00002221 PyObject *buf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002222
Thomas Wouters477c8d52006-05-27 19:21:47 +00002223 if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002224 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002225
Thomas Wouters477c8d52006-05-27 19:21:47 +00002226 if (recvlen < 0) {
Martin v. Löwisfba64e12001-11-19 10:41:26 +00002227 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00002228 "negative buffersize in recv");
Martin v. Löwisfba64e12001-11-19 10:41:26 +00002229 return NULL;
2230 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002231
Thomas Wouters477c8d52006-05-27 19:21:47 +00002232 /* Allocate a new string. */
Christian Heimes72b710a2008-05-26 13:28:38 +00002233 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002234 if (buf == NULL)
2235 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002236
Thomas Wouters477c8d52006-05-27 19:21:47 +00002237 /* Call the guts */
Christian Heimes72b710a2008-05-26 13:28:38 +00002238 outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002239 if (outlen < 0) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002240 /* An error occurred, release the string and return an
Thomas Wouters477c8d52006-05-27 19:21:47 +00002241 error. */
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002242 Py_DECREF(buf);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002243 return NULL;
2244 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002245 if (outlen != recvlen) {
2246 /* We did not read as many bytes as we anticipated, resize the
Kurt B. Kaisere4050372007-08-22 21:38:31 +00002247 string if possible and be successful. */
Christian Heimes72b710a2008-05-26 13:28:38 +00002248 _PyBytes_Resize(&buf, outlen);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002249 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002250
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002251 return buf;
2252}
2253
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002254PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002255"recv(buffersize[, flags]) -> data\n\
2256\n\
2257Receive up to buffersize bytes from the socket. For the optional flags\n\
2258argument, see the Unix manual. When no data is available, block until\n\
2259at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002260the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002261
Guido van Rossum30a685f1991-06-27 15:51:29 +00002262
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002263/* s.recv_into(buffer, [nbytes [,flags]]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002264
Thomas Wouters477c8d52006-05-27 19:21:47 +00002265static PyObject*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002266sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002267{
2268 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
2269
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002270 int recvlen = 0, flags = 0;
2271 ssize_t readlen;
Martin v. Löwis423be952008-08-13 15:53:07 +00002272 Py_buffer pbuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002273 char *buf;
2274 int buflen;
2275
2276 /* Get the buffer's memory */
Martin v. Löwis423be952008-08-13 15:53:07 +00002277 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recv_into", kwlist,
2278 &pbuf, &recvlen, &flags))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002279 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +00002280 buf = pbuf.buf;
2281 buflen = pbuf.len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002282
2283 if (recvlen < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +00002284 PyBuffer_Release(&pbuf);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002285 PyErr_SetString(PyExc_ValueError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002286 "negative buffersize in recv_into");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002287 return NULL;
2288 }
2289 if (recvlen == 0) {
2290 /* If nbytes was not specified, use the buffer's length */
2291 recvlen = buflen;
2292 }
2293
2294 /* Check if the buffer is large enough */
2295 if (buflen < recvlen) {
Martin v. Löwis423be952008-08-13 15:53:07 +00002296 PyBuffer_Release(&pbuf);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002297 PyErr_SetString(PyExc_ValueError,
2298 "buffer too small for requested bytes");
2299 return NULL;
2300 }
2301
2302 /* Call the guts */
2303 readlen = sock_recv_guts(s, buf, recvlen, flags);
2304 if (readlen < 0) {
2305 /* Return an error. */
Martin v. Löwis423be952008-08-13 15:53:07 +00002306 PyBuffer_Release(&pbuf);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002307 return NULL;
2308 }
2309
Martin v. Löwis423be952008-08-13 15:53:07 +00002310 PyBuffer_Release(&pbuf);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002311 /* Return the number of bytes read. Note that we do not do anything
2312 special here in the case that readlen < recvlen. */
Christian Heimes217cfd12007-12-02 14:31:20 +00002313 return PyLong_FromSsize_t(readlen);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002314}
2315
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002316PyDoc_STRVAR(recv_into_doc,
2317"recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002318\n\
2319A version of recv() that stores its data into a buffer rather than creating \n\
2320a new string. Receive up to buffersize bytes from the socket. If buffersize \n\
2321is not specified (or 0), receive up to the size available in the given buffer.\n\
2322\n\
2323See recv() for documentation about the flags.");
2324
2325
2326/*
Christian Heimes99170a52007-12-19 02:07:34 +00002327 * This is the guts of the recvfrom() and recvfrom_into() methods, which reads
2328 * into a char buffer. If you have any inc/def ref to do to the objects that
2329 * contain the buffer, do it in the caller. This function returns the number
2330 * of bytes succesfully read. If there was an error, it returns -1. Note
2331 * that it is also possible that we return a number of bytes smaller than the
2332 * request bytes.
Thomas Wouters477c8d52006-05-27 19:21:47 +00002333 *
2334 * 'addr' is a return value for the address object. Note that you must decref
2335 * it yourself.
2336 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002337static ssize_t
Thomas Wouters477c8d52006-05-27 19:21:47 +00002338sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags,
2339 PyObject** addr)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002340{
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002341 sock_addr_t addrbuf;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002342 int timeout;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002343 ssize_t n = -1;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002344 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002345
Thomas Wouters477c8d52006-05-27 19:21:47 +00002346 *addr = NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002347
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00002348 if (!getsockaddrlen(s, &addrlen))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002349 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002350
Thomas Wouters477c8d52006-05-27 19:21:47 +00002351 if (!IS_SELECTABLE(s)) {
2352 select_error();
2353 return -1;
2354 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002355
Guido van Rossum73624e91994-10-10 17:59:00 +00002356 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002357 memset(&addrbuf, 0, addrlen);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002358 timeout = internal_select(s, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002359 if (!timeout) {
Guido van Rossum8d665e61996-06-26 18:22:49 +00002360#ifndef MS_WINDOWS
Andrew MacIntyreba43e872002-03-03 03:03:52 +00002361#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002362 n = recvfrom(s->sock_fd, cbuf, len, flags,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002363 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum32c575d1997-12-02 20:37:32 +00002364#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002365 n = recvfrom(s->sock_fd, cbuf, len, flags,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002366 (void *) &addrbuf, &addrlen);
Guido van Rossum32c575d1997-12-02 20:37:32 +00002367#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002368#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002369 n = recvfrom(s->sock_fd, cbuf, len, flags,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002370 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002371#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002372 }
Guido van Rossum73624e91994-10-10 17:59:00 +00002373 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002374
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002375 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002376 PyErr_SetString(socket_timeout, "timed out");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002377 return -1;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002378 }
Guido van Rossum7c53b771995-09-13 18:39:47 +00002379 if (n < 0) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002380 s->errorhandler();
2381 return -1;
Guido van Rossum7c53b771995-09-13 18:39:47 +00002382 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002383
Thomas Wouters89f507f2006-12-13 04:49:30 +00002384 if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
Thomas Wouters477c8d52006-05-27 19:21:47 +00002385 addrlen, s->sock_proto)))
2386 return -1;
2387
2388 return n;
2389}
2390
2391/* s.recvfrom(nbytes [,flags]) method */
2392
2393static PyObject *
2394sock_recvfrom(PySocketSockObject *s, PyObject *args)
2395{
2396 PyObject *buf = NULL;
2397 PyObject *addr = NULL;
2398 PyObject *ret = NULL;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002399 int recvlen, flags = 0;
2400 ssize_t outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002401
2402 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002403 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002404
Guido van Rossumd8faa362007-04-27 19:54:29 +00002405 if (recvlen < 0) {
2406 PyErr_SetString(PyExc_ValueError,
2407 "negative buffersize in recvfrom");
2408 return NULL;
2409 }
2410
Christian Heimes72b710a2008-05-26 13:28:38 +00002411 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002412 if (buf == NULL)
2413 return NULL;
2414
Christian Heimes72b710a2008-05-26 13:28:38 +00002415 outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf),
Thomas Wouters477c8d52006-05-27 19:21:47 +00002416 recvlen, flags, &addr);
2417 if (outlen < 0) {
Barry Warsaw752300b1997-01-03 17:18:10 +00002418 goto finally;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002419 }
2420
2421 if (outlen != recvlen) {
2422 /* We did not read as many bytes as we anticipated, resize the
2423 string if possible and be succesful. */
Christian Heimes72b710a2008-05-26 13:28:38 +00002424 if (_PyBytes_Resize(&buf, outlen) < 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002425 /* Oopsy, not so succesful after all. */
2426 goto finally;
2427 }
Barry Warsaw752300b1997-01-03 17:18:10 +00002428
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002429 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002430
2431finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00002432 Py_XDECREF(buf);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002433 Py_XDECREF(addr);
Guido van Rossum6f5afc91993-02-05 09:46:15 +00002434 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002435}
2436
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002437PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002438"recvfrom(buffersize[, flags]) -> (data, address info)\n\
2439\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002440Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002441
Thomas Wouters477c8d52006-05-27 19:21:47 +00002442
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002443/* s.recvfrom_into(buffer[, nbytes [,flags]]) method */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002444
2445static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002446sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002447{
2448 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
2449
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002450 int recvlen = 0, flags = 0;
2451 ssize_t readlen;
Martin v. Löwis423be952008-08-13 15:53:07 +00002452 Py_buffer pbuf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002453 char *buf;
2454 int buflen;
2455
2456 PyObject *addr = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002457
Martin v. Löwis423be952008-08-13 15:53:07 +00002458 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recvfrom_into",
2459 kwlist, &pbuf,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002460 &recvlen, &flags))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002461 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +00002462 buf = pbuf.buf;
2463 buflen = pbuf.len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002464 assert(buf != 0 && buflen > 0);
2465
2466 if (recvlen < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +00002467 PyBuffer_Release(&pbuf);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002468 PyErr_SetString(PyExc_ValueError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002469 "negative buffersize in recvfrom_into");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002470 return NULL;
2471 }
2472 if (recvlen == 0) {
2473 /* If nbytes was not specified, use the buffer's length */
2474 recvlen = buflen;
2475 }
2476
2477 readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
2478 if (readlen < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +00002479 PyBuffer_Release(&pbuf);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002480 /* Return an error */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002481 Py_XDECREF(addr);
2482 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002483 }
2484
Martin v. Löwis423be952008-08-13 15:53:07 +00002485 PyBuffer_Release(&pbuf);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002486 /* Return the number of bytes read and the address. Note that we do
2487 not do anything special here in the case that readlen < recvlen. */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002488 return Py_BuildValue("lN", readlen, addr);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002489}
2490
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002491PyDoc_STRVAR(recvfrom_into_doc,
2492"recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002493\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002494Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002495
2496
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002497/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002498
Guido van Rossum73624e91994-10-10 17:59:00 +00002499static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002500sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002501{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002502 char *buf;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002503 int len, n = -1, flags = 0, timeout;
Martin v. Löwis423be952008-08-13 15:53:07 +00002504 Py_buffer pbuf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002505
Martin v. Löwis423be952008-08-13 15:53:07 +00002506 if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002507 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002508
Martin v. Löwis423be952008-08-13 15:53:07 +00002509 if (!IS_SELECTABLE(s)) {
2510 PyBuffer_Release(&pbuf);
Neal Norwitz082b2df2006-02-07 07:04:46 +00002511 return select_error();
Martin v. Löwis423be952008-08-13 15:53:07 +00002512 }
2513 buf = pbuf.buf;
2514 len = pbuf.len;
Neal Norwitz082b2df2006-02-07 07:04:46 +00002515
Guido van Rossum73624e91994-10-10 17:59:00 +00002516 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002517 timeout = internal_select(s, 1);
2518 if (!timeout)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002519#ifdef __VMS
2520 n = sendsegmented(s->sock_fd, buf, len, flags);
2521#else
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002522 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002523#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00002524 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002525
Martin v. Löwis423be952008-08-13 15:53:07 +00002526 PyBuffer_Release(&pbuf);
2527
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002528 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002529 PyErr_SetString(socket_timeout, "timed out");
2530 return NULL;
2531 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00002532 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002533 return s->errorhandler();
Christian Heimes217cfd12007-12-02 14:31:20 +00002534 return PyLong_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00002535}
2536
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002537PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002538"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002539\n\
2540Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002541argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002542sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002543
2544
2545/* s.sendall(data [,flags]) method */
2546
2547static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002548sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002549{
2550 char *buf;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002551 int len, n = -1, flags = 0, timeout;
Martin v. Löwis423be952008-08-13 15:53:07 +00002552 Py_buffer pbuf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002553
Martin v. Löwis423be952008-08-13 15:53:07 +00002554 if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags))
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002555 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +00002556 buf = pbuf.buf;
2557 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002558
Martin v. Löwis423be952008-08-13 15:53:07 +00002559 if (!IS_SELECTABLE(s)) {
2560 PyBuffer_Release(&pbuf);
Neal Norwitz082b2df2006-02-07 07:04:46 +00002561 return select_error();
Martin v. Löwis423be952008-08-13 15:53:07 +00002562 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002563
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002564 Py_BEGIN_ALLOW_THREADS
2565 do {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002566 timeout = internal_select(s, 1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002567 n = -1;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002568 if (timeout)
2569 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002570#ifdef __VMS
2571 n = sendsegmented(s->sock_fd, buf, len, flags);
2572#else
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002573 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002574#endif
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002575 if (n < 0)
2576 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002577 buf += n;
2578 len -= n;
2579 } while (len > 0);
2580 Py_END_ALLOW_THREADS
Martin v. Löwis423be952008-08-13 15:53:07 +00002581 PyBuffer_Release(&pbuf);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002582
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002583 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002584 PyErr_SetString(socket_timeout, "timed out");
2585 return NULL;
2586 }
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002587 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002588 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00002589
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002590 Py_INCREF(Py_None);
2591 return Py_None;
2592}
2593
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002594PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002595"sendall(data[, flags])\n\
2596\n\
2597Send a data string to the socket. For the optional flags\n\
2598argument, see the Unix manual. This calls send() repeatedly\n\
2599until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002600to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002601
Guido van Rossum30a685f1991-06-27 15:51:29 +00002602
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002603/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002604
Guido van Rossum73624e91994-10-10 17:59:00 +00002605static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002606sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002607{
Martin v. Löwis423be952008-08-13 15:53:07 +00002608 Py_buffer pbuf;
Guido van Rossum73624e91994-10-10 17:59:00 +00002609 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002610 char *buf;
Martin v. Löwis423be952008-08-13 15:53:07 +00002611 Py_ssize_t len;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002612 sock_addr_t addrbuf;
Martin v. Löwis423be952008-08-13 15:53:07 +00002613 int addrlen, n = -1, flags, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002614
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002615 flags = 0;
Martin v. Löwis423be952008-08-13 15:53:07 +00002616 if (!PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00002617 PyErr_Clear();
Martin v. Löwis423be952008-08-13 15:53:07 +00002618 if (!PyArg_ParseTuple(args, "y*iO:sendto",
2619 &pbuf, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002620 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002621 }
Martin v. Löwis423be952008-08-13 15:53:07 +00002622 buf = pbuf.buf;
2623 len = pbuf.len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002624
Martin v. Löwis423be952008-08-13 15:53:07 +00002625 if (!IS_SELECTABLE(s)) {
2626 PyBuffer_Release(&pbuf);
Neal Norwitz082b2df2006-02-07 07:04:46 +00002627 return select_error();
Martin v. Löwis423be952008-08-13 15:53:07 +00002628 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002629
Martin v. Löwis423be952008-08-13 15:53:07 +00002630 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) {
2631 PyBuffer_Release(&pbuf);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002632 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +00002633 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002634
Guido van Rossum73624e91994-10-10 17:59:00 +00002635 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002636 timeout = internal_select(s, 1);
2637 if (!timeout)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002638 n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002639 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002640
Martin v. Löwis423be952008-08-13 15:53:07 +00002641 PyBuffer_Release(&pbuf);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002642 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002643 PyErr_SetString(socket_timeout, "timed out");
2644 return NULL;
2645 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002646 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002647 return s->errorhandler();
Christian Heimes217cfd12007-12-02 14:31:20 +00002648 return PyLong_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002649}
2650
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002651PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002652"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002653\n\
2654Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002655For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002656
Guido van Rossum30a685f1991-06-27 15:51:29 +00002657
2658/* s.shutdown(how) method */
2659
Guido van Rossum73624e91994-10-10 17:59:00 +00002660static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002661sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002662{
2663 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002664 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002665
Christian Heimes217cfd12007-12-02 14:31:20 +00002666 how = PyLong_AsLong(arg);
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002667 if (how == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002668 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002669 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002670 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00002671 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002672 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002673 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002674 Py_INCREF(Py_None);
2675 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002676}
2677
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002678PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002679"shutdown(flag)\n\
2680\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00002681Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
2682of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002683
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00002684#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Christian Heimesfaf2f632008-01-06 16:59:19 +00002685static PyObject*
2686sock_ioctl(PySocketSockObject *s, PyObject *arg)
2687{
2688 unsigned long cmd = SIO_RCVALL;
Kristján Valur Jónsson847ec752009-09-27 21:10:38 +00002689 PyObject *argO;
2690 DWORD recv;
Christian Heimesfaf2f632008-01-06 16:59:19 +00002691
Kristján Valur Jónsson847ec752009-09-27 21:10:38 +00002692 if (!PyArg_ParseTuple(arg, "kO:ioctl", &cmd, &argO))
Christian Heimesfaf2f632008-01-06 16:59:19 +00002693 return NULL;
2694
Kristján Valur Jónsson847ec752009-09-27 21:10:38 +00002695 switch (cmd) {
2696 case SIO_RCVALL: {
2697 unsigned int option = RCVALL_ON;
2698 if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option))
2699 return NULL;
2700 if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option),
2701 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
2702 return set_error();
2703 }
2704 return PyLong_FromUnsignedLong(recv); }
2705 case SIO_KEEPALIVE_VALS: {
2706 struct tcp_keepalive ka;
2707 if (!PyArg_ParseTuple(arg, "k(kkk):ioctl", &cmd,
2708 &ka.onoff, &ka.keepalivetime, &ka.keepaliveinterval))
2709 return NULL;
2710 if (WSAIoctl(s->sock_fd, cmd, &ka, sizeof(ka),
2711 NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
2712 return set_error();
2713 }
2714 return PyLong_FromUnsignedLong(recv); }
2715 default:
2716 PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd);
2717 return NULL;
Christian Heimesfaf2f632008-01-06 16:59:19 +00002718 }
Christian Heimesfaf2f632008-01-06 16:59:19 +00002719}
2720PyDoc_STRVAR(sock_ioctl_doc,
2721"ioctl(cmd, option) -> long\n\
2722\n\
Kristján Valur Jónsson847ec752009-09-27 21:10:38 +00002723Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\
2724SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\
2725SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval).");
Christian Heimesfaf2f632008-01-06 16:59:19 +00002726
2727#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00002728
2729/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002730
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002731static PyMethodDef sock_methods[] = {
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002732 {"_accept", (PyCFunction)sock_accept, METH_NOARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002733 accept_doc},
2734 {"bind", (PyCFunction)sock_bind, METH_O,
2735 bind_doc},
2736 {"close", (PyCFunction)sock_close, METH_NOARGS,
2737 close_doc},
2738 {"connect", (PyCFunction)sock_connect, METH_O,
2739 connect_doc},
2740 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
2741 connect_ex_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002742 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
2743 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002744#ifdef HAVE_GETPEERNAME
Thomas Wouters477c8d52006-05-27 19:21:47 +00002745 {"getpeername", (PyCFunction)sock_getpeername,
2746 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00002747#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00002748 {"getsockname", (PyCFunction)sock_getsockname,
2749 METH_NOARGS, getsockname_doc},
2750 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
2751 getsockopt_doc},
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +00002752#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
Christian Heimesfaf2f632008-01-06 16:59:19 +00002753 {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS,
2754 sock_ioctl_doc},
2755#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00002756 {"listen", (PyCFunction)sock_listen, METH_O,
2757 listen_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002758 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
2759 recv_doc},
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002760 {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
2761 recv_into_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002762 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
2763 recvfrom_doc},
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002764 {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
2765 recvfrom_into_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002766 {"send", (PyCFunction)sock_send, METH_VARARGS,
2767 send_doc},
2768 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
2769 sendall_doc},
2770 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
2771 sendto_doc},
2772 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
2773 setblocking_doc},
2774 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
2775 settimeout_doc},
2776 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
2777 gettimeout_doc},
2778 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
2779 setsockopt_doc},
2780 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
2781 shutdown_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002782 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002783};
2784
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002785/* SockObject members */
2786static PyMemberDef sock_memberlist[] = {
2787 {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},
2788 {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},
2789 {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},
2790 {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"},
2791 {0},
2792};
Guido van Rossum30a685f1991-06-27 15:51:29 +00002793
Guido van Rossum73624e91994-10-10 17:59:00 +00002794/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00002795 First close the file description. */
2796
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002797static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002798sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002799{
Guido van Rossumfa972c92000-04-10 12:45:45 +00002800 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00002801 (void) SOCKETCLOSE(s->sock_fd);
Christian Heimes90aa7642007-12-19 02:45:37 +00002802 Py_TYPE(s)->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002803}
2804
Guido van Rossum30a685f1991-06-27 15:51:29 +00002805
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002806static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002807sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002808{
Fred Drakea04eaad2000-06-30 02:46:07 +00002809#if SIZEOF_SOCKET_T > SIZEOF_LONG
2810 if (s->sock_fd > LONG_MAX) {
2811 /* this can occur on Win64, and actually there is a special
2812 ugly printf formatter for decimal pointer length integer
2813 printing, only bother if necessary*/
2814 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002815 "no printf formatter to display "
2816 "the socket descriptor in decimal");
Fred Drakea04eaad2000-06-30 02:46:07 +00002817 return NULL;
2818 }
2819#endif
Walter Dörwalda7eb93e2007-06-05 13:41:53 +00002820 return PyUnicode_FromFormat(
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002821 "<socket object, fd=%ld, family=%d, type=%d, proto=%d>",
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002822 (long)s->sock_fd, s->sock_family,
2823 s->sock_type,
2824 s->sock_proto);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002825}
2826
2827
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002828/* Create a new, uninitialized socket object. */
2829
2830static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002831sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002832{
2833 PyObject *new;
2834
2835 new = type->tp_alloc(type, 0);
Guido van Rossume1c478f2002-06-06 20:08:25 +00002836 if (new != NULL) {
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002837 ((PySocketSockObject *)new)->sock_fd = -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002838 ((PySocketSockObject *)new)->sock_timeout = -1.0;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002839 ((PySocketSockObject *)new)->errorhandler = &set_error;
Guido van Rossume1c478f2002-06-06 20:08:25 +00002840 }
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002841 return new;
2842}
2843
2844
2845/* Initialize a new socket object. */
2846
2847/*ARGSUSED*/
2848static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002849sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002850{
2851 PySocketSockObject *s = (PySocketSockObject *)self;
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002852 PyObject *fdobj = NULL;
2853 SOCKET_T fd = INVALID_SOCKET;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002854 int family = AF_INET, type = SOCK_STREAM, proto = 0;
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002855 static char *keywords[] = {"family", "type", "proto", "fileno", 0};
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002856
2857 if (!PyArg_ParseTupleAndKeywords(args, kwds,
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002858 "|iiiO:socket", keywords,
2859 &family, &type, &proto, &fdobj))
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002860 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002861
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002862 if (fdobj != NULL && fdobj != Py_None) {
2863 fd = PyLong_AsSocket_t(fdobj);
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002864 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
2865 return -1;
2866 if (fd == INVALID_SOCKET) {
2867 PyErr_SetString(PyExc_ValueError,
2868 "can't use invalid socket value");
2869 return -1;
2870 }
2871 }
2872 else {
2873 Py_BEGIN_ALLOW_THREADS
2874 fd = socket(family, type, proto);
2875 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002876
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002877 if (fd == INVALID_SOCKET) {
2878 set_error();
2879 return -1;
2880 }
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002881 }
2882 init_sockobject(s, fd, family, type, proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002883
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002884 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002885
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002886}
2887
2888
Guido van Rossumb6775db1994-08-01 11:34:53 +00002889/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002890
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002891static PyTypeObject sock_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002892 PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
Guido van Rossumca6dfa52001-10-28 12:31:33 +00002893 "_socket.socket", /* tp_name */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002894 sizeof(PySocketSockObject), /* tp_basicsize */
2895 0, /* tp_itemsize */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002896 (destructor)sock_dealloc, /* tp_dealloc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002897 0, /* tp_print */
2898 0, /* tp_getattr */
2899 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002900 0, /* tp_reserved */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002901 (reprfunc)sock_repr, /* tp_repr */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002902 0, /* tp_as_number */
2903 0, /* tp_as_sequence */
2904 0, /* tp_as_mapping */
2905 0, /* tp_hash */
2906 0, /* tp_call */
2907 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002908 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002909 0, /* tp_setattro */
2910 0, /* tp_as_buffer */
2911 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002912 sock_doc, /* tp_doc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002913 0, /* tp_traverse */
2914 0, /* tp_clear */
2915 0, /* tp_richcompare */
2916 0, /* tp_weaklistoffset */
2917 0, /* tp_iter */
2918 0, /* tp_iternext */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002919 sock_methods, /* tp_methods */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002920 sock_memberlist, /* tp_members */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002921 0, /* tp_getset */
2922 0, /* tp_base */
2923 0, /* tp_dict */
2924 0, /* tp_descr_get */
2925 0, /* tp_descr_set */
2926 0, /* tp_dictoffset */
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002927 sock_initobj, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002928 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002929 sock_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002930 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002931};
2932
Guido van Rossum30a685f1991-06-27 15:51:29 +00002933
Guido van Rossum81194471991-07-27 21:42:02 +00002934/* Python interface to gethostname(). */
2935
2936/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002937static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002938socket_gethostname(PyObject *self, PyObject *unused)
Guido van Rossum81194471991-07-27 21:42:02 +00002939{
2940 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00002941 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00002942 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002943 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00002944 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002945 if (res < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002946 return set_error();
Guido van Rossum81194471991-07-27 21:42:02 +00002947 buf[sizeof buf - 1] = '\0';
Guido van Rossum32c4ac02007-08-15 03:56:40 +00002948 return PyUnicode_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00002949}
Guido van Rossumff4949e1992-08-05 19:58:53 +00002950
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002951PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002952"gethostname() -> string\n\
2953\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002954Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002955
Guido van Rossumff4949e1992-08-05 19:58:53 +00002956
Guido van Rossum30a685f1991-06-27 15:51:29 +00002957/* Python interface to gethostbyname(name). */
2958
2959/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002960static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002961socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002962{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002963 char *name;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002964 sock_addr_t addrbuf;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002965
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002966 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002967 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002968 if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002969 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002970 return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002971}
2972
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002973PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002974"gethostbyname(host) -> address\n\
2975\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002976Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002977
2978
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002979/* Convenience function common to gethostbyname_ex and gethostbyaddr */
2980
2981static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002982gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002983{
2984 char **pch;
2985 PyObject *rtn_tuple = (PyObject *)NULL;
2986 PyObject *name_list = (PyObject *)NULL;
2987 PyObject *addr_list = (PyObject *)NULL;
2988 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002989
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002990 if (h == NULL) {
Guido van Rossum48a680c2001-03-02 06:34:14 +00002991 /* Let's get real error message to return */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002992 set_herror(h_errno);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002993 return NULL;
2994 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002995
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002996 if (h->h_addrtype != af) {
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002997 /* Let's get real error message to return */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002998 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002999 (char *)strerror(EAFNOSUPPORT));
Christian Heimesada8c3b2008-03-18 18:26:33 +00003000
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003001 return NULL;
3002 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003003
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003004 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00003005
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003006 case AF_INET:
3007 if (alen < sizeof(struct sockaddr_in))
3008 return NULL;
3009 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003010
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003011#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003012 case AF_INET6:
3013 if (alen < sizeof(struct sockaddr_in6))
3014 return NULL;
3015 break;
3016#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00003017
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003018 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003019
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003020 if ((name_list = PyList_New(0)) == NULL)
3021 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003022
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003023 if ((addr_list = PyList_New(0)) == NULL)
3024 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003025
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003026 /* SF #1511317: h_aliases can be NULL */
3027 if (h->h_aliases) {
3028 for (pch = h->h_aliases; *pch != NULL; pch++) {
3029 int status;
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003030 tmp = PyUnicode_FromString(*pch);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003031 if (tmp == NULL)
3032 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003033
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003034 status = PyList_Append(name_list, tmp);
3035 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003036
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003037 if (status)
3038 goto err;
3039 }
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003040 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003041
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003042 for (pch = h->h_addr_list; *pch != NULL; pch++) {
3043 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003044
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003045 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00003046
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003047 case AF_INET:
3048 {
3049 struct sockaddr_in sin;
3050 memset(&sin, 0, sizeof(sin));
3051 sin.sin_family = af;
3052#ifdef HAVE_SOCKADDR_SA_LEN
3053 sin.sin_len = sizeof(sin);
3054#endif
3055 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
3056 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00003057
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003058 if (pch == h->h_addr_list && alen >= sizeof(sin))
3059 memcpy((char *) addr, &sin, sizeof(sin));
3060 break;
3061 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003062
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003063#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003064 case AF_INET6:
3065 {
3066 struct sockaddr_in6 sin6;
3067 memset(&sin6, 0, sizeof(sin6));
3068 sin6.sin6_family = af;
3069#ifdef HAVE_SOCKADDR_SA_LEN
3070 sin6.sin6_len = sizeof(sin6);
3071#endif
3072 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
3073 tmp = makeipaddr((struct sockaddr *)&sin6,
3074 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00003075
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003076 if (pch == h->h_addr_list && alen >= sizeof(sin6))
3077 memcpy((char *) addr, &sin6, sizeof(sin6));
3078 break;
3079 }
3080#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00003081
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003082 default: /* can't happen */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003083 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003084 "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003085 return NULL;
3086 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003087
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003088 if (tmp == NULL)
3089 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003090
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003091 status = PyList_Append(addr_list, tmp);
3092 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003093
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003094 if (status)
3095 goto err;
3096 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00003097
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003098 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00003099
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003100 err:
3101 Py_XDECREF(name_list);
3102 Py_XDECREF(addr_list);
3103 return rtn_tuple;
3104}
3105
3106
3107/* Python interface to gethostbyname_ex(name). */
3108
3109/*ARGSUSED*/
3110static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003111socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003112{
3113 char *name;
3114 struct hostent *h;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00003115#ifdef ENABLE_IPV6
3116 struct sockaddr_storage addr;
3117#else
3118 struct sockaddr_in addr;
3119#endif
Martin v. Löwis9db2f572001-07-23 01:30:10 +00003120 struct sockaddr *sa;
Guido van Rossum955becc1999-03-22 20:14:53 +00003121 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003122#ifdef HAVE_GETHOSTBYNAME_R
3123 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003124#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
3125 struct hostent_data data;
3126#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003127 char buf[16384];
3128 int buf_len = (sizeof buf) - 1;
3129 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003130#endif
3131#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003132 int result;
3133#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003134#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003135
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003136 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003137 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003138 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003139 return NULL;
3140 Py_BEGIN_ALLOW_THREADS
3141#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003142#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003143 result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
3144 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003145#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003146 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003147#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00003148 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003149 result = gethostbyname_r(name, &hp_allocated, &data);
3150 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003151#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003152#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003153#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00003154 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003155#endif
3156 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003157#endif /* HAVE_GETHOSTBYNAME_R */
3158 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00003159 /* Some C libraries would require addr.__ss_family instead of
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003160 addr.ss_family.
3161 Therefore, we cast the sockaddr_storage into sockaddr to
3162 access sa_family. */
Martin v. Löwis9db2f572001-07-23 01:30:10 +00003163 sa = (struct sockaddr*)&addr;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003164 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003165 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003166#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00003167 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00003168#endif
3169 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003170}
3171
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003172PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003173"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
3174\n\
3175Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003176for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003177
3178
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003179/* Python interface to gethostbyaddr(IP). */
3180
3181/*ARGSUSED*/
3182static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003183socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003184{
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003185#ifdef ENABLE_IPV6
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003186 struct sockaddr_storage addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003187#else
Guido van Rossum48a680c2001-03-02 06:34:14 +00003188 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003189#endif
3190 struct sockaddr *sa = (struct sockaddr *)&addr;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003191 char *ip_num;
3192 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00003193 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003194#ifdef HAVE_GETHOSTBYNAME_R
3195 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003196#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
3197 struct hostent_data data;
3198#else
Benjamin Peterson058e31e2009-01-16 03:54:08 +00003199 /* glibcs up to 2.10 assume that the buf argument to
3200 gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc
3201 does not ensure. The attribute below instructs the compiler
3202 to maintain this alignment. */
3203 char buf[16384] Py_ALIGNED(8);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003204 int buf_len = (sizeof buf) - 1;
3205 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003206#endif
3207#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003208 int result;
3209#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003210#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003211 char *ap;
3212 int al;
3213 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003214
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003215 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003216 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003217 af = AF_UNSPEC;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00003218 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003219 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003220 af = sa->sa_family;
3221 ap = NULL;
3222 al = 0;
3223 switch (af) {
3224 case AF_INET:
3225 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
3226 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3227 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003228#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003229 case AF_INET6:
3230 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
3231 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3232 break;
3233#endif
3234 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003235 PyErr_SetString(socket_error, "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003236 return NULL;
3237 }
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003238 Py_BEGIN_ALLOW_THREADS
3239#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003240#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003241 result = gethostbyaddr_r(ap, al, af,
3242 &hp_allocated, buf, buf_len,
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003243 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003244#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003245 h = gethostbyaddr_r(ap, al, af,
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003246 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003247#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00003248 memset((void *) &data, '\0', sizeof(data));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003249 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003250 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003251#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003252#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003253#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00003254 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003255#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003256 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003257#endif /* HAVE_GETHOSTBYNAME_R */
3258 Py_END_ALLOW_THREADS
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003259 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003260#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00003261 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003262#endif
3263 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003264}
3265
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003266PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003267"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
3268\n\
3269Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003270for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003271
Guido van Rossum30a685f1991-06-27 15:51:29 +00003272
3273/* Python interface to getservbyname(name).
3274 This only returns the port number, since the other info is already
3275 known or not useful (like the list of aliases). */
3276
3277/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00003278static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003279socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00003280{
Barry Warsaw11b91a02004-06-28 00:50:43 +00003281 char *name, *proto=NULL;
Guido van Rossum30a685f1991-06-27 15:51:29 +00003282 struct servent *sp;
Barry Warsaw11b91a02004-06-28 00:50:43 +00003283 if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00003284 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00003285 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00003286 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00003287 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00003288 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003289 PyErr_SetString(socket_error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00003290 return NULL;
3291 }
Christian Heimes217cfd12007-12-02 14:31:20 +00003292 return PyLong_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00003293}
3294
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003295PyDoc_STRVAR(getservbyname_doc,
Barry Warsaw11b91a02004-06-28 00:50:43 +00003296"getservbyname(servicename[, protocolname]) -> integer\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003297\n\
3298Return a port number from a service name and protocol name.\n\
Barry Warsaw11b91a02004-06-28 00:50:43 +00003299The optional protocol name, if given, should be 'tcp' or 'udp',\n\
3300otherwise any protocol will match.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003301
Guido van Rossum30a685f1991-06-27 15:51:29 +00003302
Barry Warsaw11b91a02004-06-28 00:50:43 +00003303/* Python interface to getservbyport(port).
3304 This only returns the service name, since the other info is already
3305 known or not useful (like the list of aliases). */
3306
3307/*ARGSUSED*/
3308static PyObject *
3309socket_getservbyport(PyObject *self, PyObject *args)
3310{
Benjamin Peterson5c6d7872009-02-06 02:40:07 +00003311 int port;
Barry Warsaw11b91a02004-06-28 00:50:43 +00003312 char *proto=NULL;
3313 struct servent *sp;
Benjamin Peterson5c6d7872009-02-06 02:40:07 +00003314 if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto))
Barry Warsaw11b91a02004-06-28 00:50:43 +00003315 return NULL;
Benjamin Peterson5c6d7872009-02-06 02:40:07 +00003316 if (port < 0 || port > 0xffff) {
3317 PyErr_SetString(
3318 PyExc_OverflowError,
3319 "getservbyport: port must be 0-65535.");
3320 return NULL;
3321 }
Barry Warsaw11b91a02004-06-28 00:50:43 +00003322 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson5c6d7872009-02-06 02:40:07 +00003323 sp = getservbyport(htons((short)port), proto);
Barry Warsaw11b91a02004-06-28 00:50:43 +00003324 Py_END_ALLOW_THREADS
3325 if (sp == NULL) {
3326 PyErr_SetString(socket_error, "port/proto not found");
3327 return NULL;
3328 }
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003329 return PyUnicode_FromString(sp->s_name);
Barry Warsaw11b91a02004-06-28 00:50:43 +00003330}
3331
3332PyDoc_STRVAR(getservbyport_doc,
3333"getservbyport(port[, protocolname]) -> string\n\
3334\n\
3335Return the service name from a port number and protocol name.\n\
3336The optional protocol name, if given, should be 'tcp' or 'udp',\n\
3337otherwise any protocol will match.");
3338
Guido van Rossum3901d851996-12-19 16:35:04 +00003339/* Python interface to getprotobyname(name).
3340 This only returns the protocol number, since the other info is
3341 already known or not useful (like the list of aliases). */
3342
3343/*ARGSUSED*/
3344static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003345socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00003346{
3347 char *name;
3348 struct protoent *sp;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003349 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00003350 return NULL;
3351 Py_BEGIN_ALLOW_THREADS
3352 sp = getprotobyname(name);
3353 Py_END_ALLOW_THREADS
3354 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003355 PyErr_SetString(socket_error, "protocol not found");
Guido van Rossum3901d851996-12-19 16:35:04 +00003356 return NULL;
3357 }
Christian Heimes217cfd12007-12-02 14:31:20 +00003358 return PyLong_FromLong((long) sp->p_proto);
Guido van Rossum3901d851996-12-19 16:35:04 +00003359}
3360
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003361PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003362"getprotobyname(name) -> integer\n\
3363\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003364Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003365
Guido van Rossum3901d851996-12-19 16:35:04 +00003366
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003367#ifndef NO_DUP
3368/* dup() function for socket fds */
3369
3370static PyObject *
3371socket_dup(PyObject *self, PyObject *fdobj)
3372{
3373 SOCKET_T fd, newfd;
3374 PyObject *newfdobj;
3375
3376
3377 fd = PyLong_AsSocket_t(fdobj);
3378 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
3379 return NULL;
3380
3381 newfd = dup_socket(fd);
3382 if (newfd == INVALID_SOCKET)
3383 return set_error();
3384
3385 newfdobj = PyLong_FromSocket_t(newfd);
3386 if (newfdobj == NULL)
3387 SOCKETCLOSE(newfd);
3388 return newfdobj;
3389}
3390
3391PyDoc_STRVAR(dup_doc,
3392"dup(integer) -> integer\n\
3393\n\
3394Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\
3395sockets; on some platforms os.dup() won't work for socket file descriptors.");
3396#endif
3397
3398
Dave Cole331708b2004-08-09 04:51:41 +00003399#ifdef HAVE_SOCKETPAIR
3400/* Create a pair of sockets using the socketpair() function.
Dave Cole07fda7e2004-08-23 05:16:23 +00003401 Arguments as for socket() except the default family is AF_UNIX if
Dave Colee8bbfe42004-08-26 00:51:16 +00003402 defined on the platform; otherwise, the default is AF_INET. */
Dave Cole331708b2004-08-09 04:51:41 +00003403
3404/*ARGSUSED*/
3405static PyObject *
3406socket_socketpair(PyObject *self, PyObject *args)
3407{
3408 PySocketSockObject *s0 = NULL, *s1 = NULL;
3409 SOCKET_T sv[2];
3410 int family, type = SOCK_STREAM, proto = 0;
3411 PyObject *res = NULL;
3412
3413#if defined(AF_UNIX)
3414 family = AF_UNIX;
3415#else
3416 family = AF_INET;
3417#endif
3418 if (!PyArg_ParseTuple(args, "|iii:socketpair",
3419 &family, &type, &proto))
3420 return NULL;
3421 /* Create a pair of socket fds */
3422 if (socketpair(family, type, proto, sv) < 0)
3423 return set_error();
Dave Cole331708b2004-08-09 04:51:41 +00003424 s0 = new_sockobject(sv[0], family, type, proto);
3425 if (s0 == NULL)
3426 goto finally;
3427 s1 = new_sockobject(sv[1], family, type, proto);
3428 if (s1 == NULL)
3429 goto finally;
3430 res = PyTuple_Pack(2, s0, s1);
3431
3432finally:
3433 if (res == NULL) {
3434 if (s0 == NULL)
3435 SOCKETCLOSE(sv[0]);
3436 if (s1 == NULL)
3437 SOCKETCLOSE(sv[1]);
3438 }
3439 Py_XDECREF(s0);
3440 Py_XDECREF(s1);
3441 return res;
3442}
3443
3444PyDoc_STRVAR(socketpair_doc,
3445"socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\
3446\n\
3447Create a pair of socket objects from the sockets returned by the platform\n\
3448socketpair() function.\n\
Dave Cole07fda7e2004-08-23 05:16:23 +00003449The arguments are the same as for socket() except the default family is\n\
Dave Colee8bbfe42004-08-26 00:51:16 +00003450AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
Dave Cole331708b2004-08-09 04:51:41 +00003451
3452#endif /* HAVE_SOCKETPAIR */
3453
3454
Guido van Rossum006bf911996-06-12 04:04:55 +00003455static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003456socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003457{
3458 int x1, x2;
3459
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003460 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00003461 return NULL;
3462 }
Guido van Rossum018919a2007-01-15 00:07:32 +00003463 if (x1 < 0) {
3464 PyErr_SetString(PyExc_OverflowError,
3465 "can't convert negative number to unsigned long");
3466 return NULL;
3467 }
3468 x2 = (unsigned int)ntohs((unsigned short)x1);
Christian Heimes217cfd12007-12-02 14:31:20 +00003469 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00003470}
3471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003472PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003473"ntohs(integer) -> integer\n\
3474\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003475Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003476
3477
Guido van Rossum006bf911996-06-12 04:04:55 +00003478static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003479socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003480{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003481 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003482
Guido van Rossumddefaf32007-01-14 03:31:43 +00003483 if (PyLong_Check(arg)) {
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003484 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003485 if (x == (unsigned long) -1 && PyErr_Occurred())
3486 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003487#if SIZEOF_LONG > 4
3488 {
3489 unsigned long y;
3490 /* only want the trailing 32 bits */
3491 y = x & 0xFFFFFFFFUL;
3492 if (y ^ x)
3493 return PyErr_Format(PyExc_OverflowError,
3494 "long int larger than 32 bits");
3495 x = y;
Tim Peters58141872002-08-06 22:25:02 +00003496 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003497#endif
3498 }
3499 else
Tim Peters58141872002-08-06 22:25:02 +00003500 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003501 "expected int/long, %s found",
Christian Heimes90aa7642007-12-19 02:45:37 +00003502 Py_TYPE(arg)->tp_name);
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003503 if (x == (unsigned long) -1 && PyErr_Occurred())
3504 return NULL;
Guido van Rossum018919a2007-01-15 00:07:32 +00003505 return PyLong_FromUnsignedLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003506}
3507
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003508PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003509"ntohl(integer) -> integer\n\
3510\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003511Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003512
3513
Guido van Rossum006bf911996-06-12 04:04:55 +00003514static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003515socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003516{
Hye-Shik Changa7b673f2003-12-17 09:40:23 +00003517 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00003518
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003519 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00003520 return NULL;
3521 }
Guido van Rossum018919a2007-01-15 00:07:32 +00003522 if (x1 < 0) {
3523 PyErr_SetString(PyExc_OverflowError,
3524 "can't convert negative number to unsigned long");
3525 return NULL;
3526 }
3527 x2 = (unsigned int)htons((unsigned short)x1);
Christian Heimes217cfd12007-12-02 14:31:20 +00003528 return PyLong_FromLong(x2);
Guido van Rossum006bf911996-06-12 04:04:55 +00003529}
3530
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003531PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003532"htons(integer) -> integer\n\
3533\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003534Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003535
3536
Guido van Rossum006bf911996-06-12 04:04:55 +00003537static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003538socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003539{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003540 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003541
Guido van Rossumddefaf32007-01-14 03:31:43 +00003542 if (PyLong_Check(arg)) {
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003543 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003544 if (x == (unsigned long) -1 && PyErr_Occurred())
3545 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003546#if SIZEOF_LONG > 4
3547 {
3548 unsigned long y;
3549 /* only want the trailing 32 bits */
3550 y = x & 0xFFFFFFFFUL;
3551 if (y ^ x)
3552 return PyErr_Format(PyExc_OverflowError,
3553 "long int larger than 32 bits");
3554 x = y;
Tim Peters58141872002-08-06 22:25:02 +00003555 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003556#endif
3557 }
3558 else
Tim Peters58141872002-08-06 22:25:02 +00003559 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003560 "expected int/long, %s found",
Christian Heimes90aa7642007-12-19 02:45:37 +00003561 Py_TYPE(arg)->tp_name);
Guido van Rossum018919a2007-01-15 00:07:32 +00003562 return PyLong_FromUnsignedLong(htonl((unsigned long)x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003563}
3564
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003565PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003566"htonl(integer) -> integer\n\
3567\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003568Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003569
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003570/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003571
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003572PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum7d0a8262007-05-21 23:13:11 +00003573"inet_aton(string) -> bytes giving packed 32-bit IP representation\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003574\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003575Convert 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 +00003576binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003577
3578static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003579socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003580{
Guido van Rossuma2e48551999-09-09 15:42:59 +00003581#ifndef INADDR_NONE
3582#define INADDR_NONE (-1)
3583#endif
Neal Norwitz88f115b2003-02-13 02:15:42 +00003584#ifdef HAVE_INET_ATON
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003585 struct in_addr buf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003586#endif
3587
3588#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
Benjamin Petersonf91df042009-02-13 02:50:59 +00003589#if (SIZEOF_INT != 4)
3590#error "Not sure if in_addr_t exists and int is not 32-bits."
3591#endif
Tim Peters1df9fdd2003-02-13 03:13:40 +00003592 /* Have to use inet_addr() instead */
Benjamin Petersonf91df042009-02-13 02:50:59 +00003593 unsigned int packed_addr;
Tim Peters1df9fdd2003-02-13 03:13:40 +00003594#endif
3595 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003596
Tim Peters1df9fdd2003-02-13 03:13:40 +00003597 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003598 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003599
Tim Peters1df9fdd2003-02-13 03:13:40 +00003600
3601#ifdef HAVE_INET_ATON
Thomas Wouters477c8d52006-05-27 19:21:47 +00003602
3603#ifdef USE_INET_ATON_WEAKLINK
3604 if (inet_aton != NULL) {
3605#endif
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003606 if (inet_aton(ip_addr, &buf))
Christian Heimes72b710a2008-05-26 13:28:38 +00003607 return PyBytes_FromStringAndSize((char *)(&buf),
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003608 sizeof(buf));
3609
3610 PyErr_SetString(socket_error,
3611 "illegal IP address string passed to inet_aton");
3612 return NULL;
3613
Thomas Wouters477c8d52006-05-27 19:21:47 +00003614#ifdef USE_INET_ATON_WEAKLINK
3615 } else {
3616#endif
3617
3618#endif
3619
3620#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
3621
Georg Brandld2e3ba72005-08-26 08:34:00 +00003622 /* special-case this address as inet_addr might return INADDR_NONE
3623 * for this */
3624 if (strcmp(ip_addr, "255.255.255.255") == 0) {
3625 packed_addr = 0xFFFFFFFF;
3626 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00003627
Georg Brandld2e3ba72005-08-26 08:34:00 +00003628 packed_addr = inet_addr(ip_addr);
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003629
Georg Brandld2e3ba72005-08-26 08:34:00 +00003630 if (packed_addr == INADDR_NONE) { /* invalid address */
3631 PyErr_SetString(socket_error,
3632 "illegal IP address string passed to inet_aton");
3633 return NULL;
3634 }
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003635 }
Christian Heimes72b710a2008-05-26 13:28:38 +00003636 return PyBytes_FromStringAndSize((char *) &packed_addr,
Guido van Rossum8a392d72007-11-21 22:09:45 +00003637 sizeof(packed_addr));
Thomas Wouters477c8d52006-05-27 19:21:47 +00003638
3639#ifdef USE_INET_ATON_WEAKLINK
3640 }
3641#endif
3642
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003643#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003644}
3645
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003646PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00003647"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003648\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003649Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003650
3651static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003652socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003653{
3654 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003655 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003656 struct in_addr packed_addr;
3657
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003658 if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003659 return NULL;
3660 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00003661
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003662 if (addr_len != sizeof(packed_addr)) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003663 PyErr_SetString(socket_error,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003664 "packed IP wrong length for inet_ntoa");
3665 return NULL;
3666 }
3667
3668 memcpy(&packed_addr, packed_str, addr_len);
3669
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003670 return PyUnicode_FromString(inet_ntoa(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003671}
Guido van Rossum82a5c661998-07-07 20:45:43 +00003672
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003673#ifdef HAVE_INET_PTON
3674
3675PyDoc_STRVAR(inet_pton_doc,
3676"inet_pton(af, ip) -> packed IP address string\n\
3677\n\
3678Convert an IP address from string format to a packed string suitable\n\
3679for use with low-level network functions.");
3680
3681static PyObject *
3682socket_inet_pton(PyObject *self, PyObject *args)
3683{
3684 int af;
3685 char* ip;
3686 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003687#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003688 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003689#else
3690 char packed[sizeof(struct in_addr)];
3691#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003692 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
3693 return NULL;
3694 }
3695
Martin v. Löwis04697e82004-06-02 12:35:29 +00003696#if !defined(ENABLE_IPV6) && defined(AF_INET6)
Martin v. Löwis10649092003-08-05 06:25:06 +00003697 if(af == AF_INET6) {
3698 PyErr_SetString(socket_error,
3699 "can't use AF_INET6, IPv6 is disabled");
3700 return NULL;
3701 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003702#endif
Martin v. Löwis10649092003-08-05 06:25:06 +00003703
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003704 retval = inet_pton(af, ip, packed);
3705 if (retval < 0) {
3706 PyErr_SetFromErrno(socket_error);
3707 return NULL;
3708 } else if (retval == 0) {
3709 PyErr_SetString(socket_error,
3710 "illegal IP address string passed to inet_pton");
3711 return NULL;
3712 } else if (af == AF_INET) {
Christian Heimes72b710a2008-05-26 13:28:38 +00003713 return PyBytes_FromStringAndSize(packed,
Guido van Rossum8a392d72007-11-21 22:09:45 +00003714 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003715#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003716 } else if (af == AF_INET6) {
Christian Heimes72b710a2008-05-26 13:28:38 +00003717 return PyBytes_FromStringAndSize(packed,
Guido van Rossum8a392d72007-11-21 22:09:45 +00003718 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003719#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003720 } else {
3721 PyErr_SetString(socket_error, "unknown address family");
3722 return NULL;
3723 }
3724}
Thomas Wouters477c8d52006-05-27 19:21:47 +00003725
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003726PyDoc_STRVAR(inet_ntop_doc,
3727"inet_ntop(af, packed_ip) -> string formatted IP address\n\
3728\n\
3729Convert a packed IP address of the given family to string format.");
3730
3731static PyObject *
3732socket_inet_ntop(PyObject *self, PyObject *args)
3733{
3734 int af;
3735 char* packed;
3736 int len;
3737 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003738#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003739 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003740#else
3741 char ip[INET_ADDRSTRLEN + 1];
3742#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00003743
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003744 /* Guarantee NUL-termination for PyUnicode_FromString() below */
Jeremy Hylton80961f32004-11-07 14:24:25 +00003745 memset((void *) &ip[0], '\0', sizeof(ip));
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003746
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003747 if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) {
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003748 return NULL;
3749 }
3750
3751 if (af == AF_INET) {
3752 if (len != sizeof(struct in_addr)) {
3753 PyErr_SetString(PyExc_ValueError,
3754 "invalid length of packed IP address string");
3755 return NULL;
3756 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003757#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003758 } else if (af == AF_INET6) {
3759 if (len != sizeof(struct in6_addr)) {
3760 PyErr_SetString(PyExc_ValueError,
3761 "invalid length of packed IP address string");
3762 return NULL;
3763 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003764#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003765 } else {
3766 PyErr_Format(PyExc_ValueError,
3767 "unknown address family %d", af);
3768 return NULL;
3769 }
3770
3771 retval = inet_ntop(af, packed, ip, sizeof(ip));
3772 if (!retval) {
3773 PyErr_SetFromErrno(socket_error);
3774 return NULL;
3775 } else {
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003776 return PyUnicode_FromString(retval);
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003777 }
3778
3779 /* NOTREACHED */
3780 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
3781 return NULL;
3782}
3783
3784#endif /* HAVE_INET_PTON */
3785
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003786/* Python interface to getaddrinfo(host, port). */
3787
3788/*ARGSUSED*/
3789static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003790socket_getaddrinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003791{
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003792 struct addrinfo hints, *res;
3793 struct addrinfo *res0 = NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003794 PyObject *hobj = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003795 PyObject *pobj = (PyObject *)NULL;
Martin v. Löwisf65b1a12001-08-12 09:28:40 +00003796 char pbuf[30];
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003797 char *hptr, *pptr;
3798 int family, socktype, protocol, flags;
3799 int error;
3800 PyObject *all = (PyObject *)NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003801 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003802
3803 family = socktype = protocol = flags = 0;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003804 family = AF_UNSPEC;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003805 if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
3806 &hobj, &pobj, &family, &socktype,
3807 &protocol, &flags)) {
3808 return NULL;
3809 }
3810 if (hobj == Py_None) {
3811 hptr = NULL;
3812 } else if (PyUnicode_Check(hobj)) {
3813 idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
3814 if (!idna)
3815 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003816 assert(PyBytes_Check(idna));
3817 hptr = PyBytes_AS_STRING(idna);
3818 } else if (PyBytes_Check(hobj)) {
3819 hptr = PyBytes_AsString(hobj);
Martin v. Löwis2548c732003-04-18 10:39:54 +00003820 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00003821 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2548c732003-04-18 10:39:54 +00003822 "getaddrinfo() argument 1 must be string or None");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003823 return NULL;
3824 }
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00003825 if (PyLong_CheckExact(pobj)) {
3826 long value = PyLong_AsLong(pobj);
3827 if (value == -1 && PyErr_Occurred())
3828 goto err;
3829 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003830 pptr = pbuf;
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003831 } else if (PyUnicode_Check(pobj)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003832 pptr = _PyUnicode_AsString(pobj);
Christian Heimes72b710a2008-05-26 13:28:38 +00003833 } else if (PyBytes_Check(pobj)) {
3834 pptr = PyBytes_AsString(pobj);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003835 } else if (pobj == Py_None) {
3836 pptr = (char *)NULL;
3837 } else {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003838 PyErr_SetString(socket_error, "Int or String expected");
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003839 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003840 }
3841 memset(&hints, 0, sizeof(hints));
3842 hints.ai_family = family;
3843 hints.ai_socktype = socktype;
3844 hints.ai_protocol = protocol;
3845 hints.ai_flags = flags;
Just van Rossum1040d2c2003-05-09 07:53:18 +00003846 Py_BEGIN_ALLOW_THREADS
3847 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003848 error = getaddrinfo(hptr, pptr, &hints, &res0);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003849 Py_END_ALLOW_THREADS
3850 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003851 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003852 set_gaierror(error);
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003853 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003854 }
3855
3856 if ((all = PyList_New(0)) == NULL)
3857 goto err;
3858 for (res = res0; res; res = res->ai_next) {
Guido van Rossum9ce8e382007-08-09 21:06:37 +00003859 PyObject *single;
Guido van Rossum716aac02001-10-12 18:59:27 +00003860 PyObject *addr =
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00003861 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
Guido van Rossum716aac02001-10-12 18:59:27 +00003862 if (addr == NULL)
3863 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003864 single = Py_BuildValue("iiisO", res->ai_family,
3865 res->ai_socktype, res->ai_protocol,
3866 res->ai_canonname ? res->ai_canonname : "",
Guido van Rossum716aac02001-10-12 18:59:27 +00003867 addr);
3868 Py_DECREF(addr);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003869 if (single == NULL)
3870 goto err;
3871
3872 if (PyList_Append(all, single))
3873 goto err;
3874 Py_XDECREF(single);
3875 }
Martin v. Löwis2548c732003-04-18 10:39:54 +00003876 Py_XDECREF(idna);
Neal Norwitz90128ba2002-08-09 03:37:42 +00003877 if (res0)
3878 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003879 return all;
3880 err:
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003881 Py_XDECREF(all);
Martin v. Löwis2548c732003-04-18 10:39:54 +00003882 Py_XDECREF(idna);
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003883 if (res0)
3884 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003885 return (PyObject *)NULL;
3886}
3887
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003888PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003889"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
3890 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003891\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003892Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003893
3894/* Python interface to getnameinfo(sa, flags). */
3895
3896/*ARGSUSED*/
3897static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003898socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003899{
3900 PyObject *sa = (PyObject *)NULL;
3901 int flags;
3902 char *hostp;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003903 int port, flowinfo, scope_id;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003904 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
3905 struct addrinfo hints, *res = NULL;
3906 int error;
3907 PyObject *ret = (PyObject *)NULL;
3908
3909 flags = flowinfo = scope_id = 0;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003910 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003911 return NULL;
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00003912 if (!PyTuple_Check(sa)) {
3913 PyErr_SetString(PyExc_TypeError,
3914 "getnameinfo() argument 1 must be a tuple");
3915 return NULL;
3916 }
3917 if (!PyArg_ParseTuple(sa, "si|ii",
3918 &hostp, &port, &flowinfo, &scope_id))
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003919 return NULL;
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003920 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003921 memset(&hints, 0, sizeof(hints));
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003922 hints.ai_family = AF_UNSPEC;
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003923 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Just van Rossum1040d2c2003-05-09 07:53:18 +00003924 Py_BEGIN_ALLOW_THREADS
3925 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003926 error = getaddrinfo(hostp, pbuf, &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003927 Py_END_ALLOW_THREADS
3928 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003929 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003930 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003931 goto fail;
3932 }
3933 if (res->ai_next) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003934 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003935 "sockaddr resolved to multiple addresses");
3936 goto fail;
3937 }
3938 switch (res->ai_family) {
3939 case AF_INET:
3940 {
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00003941 if (PyTuple_GET_SIZE(sa) != 2) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003942 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003943 "IPv4 sockaddr must be 2 tuple");
3944 goto fail;
3945 }
3946 break;
3947 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003948#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003949 case AF_INET6:
3950 {
3951 struct sockaddr_in6 *sin6;
3952 sin6 = (struct sockaddr_in6 *)res->ai_addr;
3953 sin6->sin6_flowinfo = flowinfo;
3954 sin6->sin6_scope_id = scope_id;
3955 break;
3956 }
3957#endif
3958 }
3959 error = getnameinfo(res->ai_addr, res->ai_addrlen,
3960 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
3961 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003962 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003963 goto fail;
3964 }
3965 ret = Py_BuildValue("ss", hbuf, pbuf);
3966
3967fail:
3968 if (res)
3969 freeaddrinfo(res);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003970 return ret;
3971}
3972
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003973PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003974"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003975\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003976Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003977
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003978
3979/* Python API to getting and setting the default timeout value. */
3980
3981static PyObject *
3982socket_getdefaulttimeout(PyObject *self)
3983{
3984 if (defaulttimeout < 0.0) {
3985 Py_INCREF(Py_None);
3986 return Py_None;
3987 }
3988 else
3989 return PyFloat_FromDouble(defaulttimeout);
3990}
3991
3992PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003993"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003994\n\
3995Returns the default timeout in floating seconds for new socket objects.\n\
3996A value of None indicates that new socket objects have no timeout.\n\
3997When the socket module is first imported, the default is None.");
3998
3999static PyObject *
4000socket_setdefaulttimeout(PyObject *self, PyObject *arg)
4001{
4002 double timeout;
4003
4004 if (arg == Py_None)
4005 timeout = -1.0;
4006 else {
4007 timeout = PyFloat_AsDouble(arg);
4008 if (timeout < 0.0) {
4009 if (!PyErr_Occurred())
4010 PyErr_SetString(PyExc_ValueError,
4011 "Timeout value out of range");
4012 return NULL;
4013 }
4014 }
4015
4016 defaulttimeout = timeout;
4017
4018 Py_INCREF(Py_None);
4019 return Py_None;
4020}
4021
4022PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004023"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004024\n\
4025Set the default timeout in floating seconds for new socket objects.\n\
4026A value of None indicates that new socket objects have no timeout.\n\
4027When the socket module is first imported, the default is None.");
4028
4029
Guido van Rossum30a685f1991-06-27 15:51:29 +00004030/* List of functions exported by this module. */
4031
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004032static PyMethodDef socket_methods[] = {
4033 {"gethostbyname", socket_gethostbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004034 METH_VARARGS, gethostbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004035 {"gethostbyname_ex", socket_gethostbyname_ex,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004036 METH_VARARGS, ghbn_ex_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004037 {"gethostbyaddr", socket_gethostbyaddr,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004038 METH_VARARGS, gethostbyaddr_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004039 {"gethostname", socket_gethostname,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004040 METH_NOARGS, gethostname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004041 {"getservbyname", socket_getservbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004042 METH_VARARGS, getservbyname_doc},
Barry Warsaw11b91a02004-06-28 00:50:43 +00004043 {"getservbyport", socket_getservbyport,
4044 METH_VARARGS, getservbyport_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004045 {"getprotobyname", socket_getprotobyname,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004046 METH_VARARGS, getprotobyname_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004047#ifndef NO_DUP
4048 {"dup", socket_dup,
4049 METH_O, dup_doc},
4050#endif
Dave Cole331708b2004-08-09 04:51:41 +00004051#ifdef HAVE_SOCKETPAIR
4052 {"socketpair", socket_socketpair,
4053 METH_VARARGS, socketpair_doc},
4054#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004055 {"ntohs", socket_ntohs,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004056 METH_VARARGS, ntohs_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004057 {"ntohl", socket_ntohl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004058 METH_O, ntohl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004059 {"htons", socket_htons,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004060 METH_VARARGS, htons_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004061 {"htonl", socket_htonl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00004062 METH_O, htonl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004063 {"inet_aton", socket_inet_aton,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004064 METH_VARARGS, inet_aton_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004065 {"inet_ntoa", socket_inet_ntoa,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00004066 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004067#ifdef HAVE_INET_PTON
4068 {"inet_pton", socket_inet_pton,
4069 METH_VARARGS, inet_pton_doc},
4070 {"inet_ntop", socket_inet_ntop,
4071 METH_VARARGS, inet_ntop_doc},
4072#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004073 {"getaddrinfo", socket_getaddrinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004074 METH_VARARGS, getaddrinfo_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004075 {"getnameinfo", socket_getnameinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004076 METH_VARARGS, getnameinfo_doc},
Guido van Rossum1693ba82002-07-18 21:11:26 +00004077 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00004078 METH_NOARGS, getdefaulttimeout_doc},
4079 {"setdefaulttimeout", socket_setdefaulttimeout,
4080 METH_O, setdefaulttimeout_doc},
Guido van Rossum30a685f1991-06-27 15:51:29 +00004081 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004082};
4083
Guido van Rossum30a685f1991-06-27 15:51:29 +00004084
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004085#ifdef MS_WINDOWS
4086#define OS_INIT_DEFINED
4087
4088/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004089
4090static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004091os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00004092{
4093 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00004094}
4095
4096static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004097os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00004098{
4099 WSADATA WSAData;
4100 int ret;
Guido van Rossumbe32c891996-06-20 16:25:29 +00004101 ret = WSAStartup(0x0101, &WSAData);
4102 switch (ret) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004103 case 0: /* No error */
Mark Hammond14350ab2003-07-09 04:57:46 +00004104 Py_AtExit(os_cleanup);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004105 return 1; /* Success */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004106 case WSASYSNOTREADY:
4107 PyErr_SetString(PyExc_ImportError,
4108 "WSAStartup failed: network not ready");
4109 break;
4110 case WSAVERNOTSUPPORTED:
4111 case WSAEINVAL:
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004112 PyErr_SetString(
4113 PyExc_ImportError,
4114 "WSAStartup failed: requested version not supported");
Guido van Rossumbe32c891996-06-20 16:25:29 +00004115 break;
4116 default:
Walter Dörwald0bbd8ab2007-06-05 13:49:43 +00004117 PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret);
Guido van Rossumbe32c891996-06-20 16:25:29 +00004118 break;
4119 }
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004120 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004121}
4122
Guido van Rossum8d665e61996-06-26 18:22:49 +00004123#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00004124
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004125
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004126#ifdef PYOS_OS2
4127#define OS_INIT_DEFINED
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004128
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004129/* Additional initialization for OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004130
4131static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004132os_init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004133{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004134#ifndef PYCC_GCC
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004135 int rc = sock_init();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004136
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004137 if (rc == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004138 return 1; /* Success */
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004139 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004140
Walter Dörwald0bbd8ab2007-06-05 13:49:43 +00004141 PyErr_Format(PyExc_ImportError, "OS/2 TCP/IP Error# %d", sock_errno());
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004142
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004143 return 0; /* Failure */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00004144#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004145 /* No need to initialise sockets with GCC/EMX */
4146 return 1; /* Success */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00004147#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004148}
4149
4150#endif /* PYOS_OS2 */
4151
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004152
4153#ifndef OS_INIT_DEFINED
4154static int
4155os_init(void)
4156{
4157 return 1; /* Success */
4158}
4159#endif
4160
4161
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004162/* C API table - always add new things to the end for binary
4163 compatibility. */
4164static
4165PySocketModule_APIObject PySocketModuleAPI =
4166{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004167 &sock_type,
Brett Cannon06c34792004-03-23 23:16:54 +00004168 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004169};
4170
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004171
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004172/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004173
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004174 This module is actually called "_socket", and there's a wrapper
Guido van Rossum7d0a8262007-05-21 23:13:11 +00004175 "socket.py" which implements some additional functionality.
4176 The import of "_socket" may fail with an ImportError exception if
4177 os-specific initialization fails. On Windows, this does WINSOCK
4178 initialization. When WINSOCK is initialized succesfully, a call to
4179 WSACleanup() is scheduled to be made at exit time.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004180*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004181
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004182PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004183"Implementation module for socket operations.\n\
4184\n\
4185See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004186
Martin v. Löwis1a214512008-06-11 05:26:20 +00004187static struct PyModuleDef socketmodule = {
4188 PyModuleDef_HEAD_INIT,
4189 PySocket_MODULE_NAME,
4190 socket_doc,
4191 -1,
4192 socket_methods,
4193 NULL,
4194 NULL,
4195 NULL,
4196 NULL
4197};
4198
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004199PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00004200PyInit__socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004201{
Christian Heimes25bb7832008-01-11 16:17:00 +00004202 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00004203
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004204 if (!os_init())
Martin v. Löwis1a214512008-06-11 05:26:20 +00004205 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004206
Christian Heimes90aa7642007-12-19 02:45:37 +00004207 Py_TYPE(&sock_type) = &PyType_Type;
Martin v. Löwis1a214512008-06-11 05:26:20 +00004208 m = PyModule_Create(&socketmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00004209 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00004210 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004211
Gregory P. Smithc111d9f2007-09-09 23:55:55 +00004212 socket_error = PyErr_NewException("socket.error",
4213 PyExc_IOError, NULL);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004214 if (socket_error == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00004215 return NULL;
Brett Cannon06c34792004-03-23 23:16:54 +00004216 PySocketModuleAPI.error = socket_error;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004217 Py_INCREF(socket_error);
4218 PyModule_AddObject(m, "error", socket_error);
4219 socket_herror = PyErr_NewException("socket.herror",
4220 socket_error, NULL);
4221 if (socket_herror == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00004222 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004223 Py_INCREF(socket_herror);
4224 PyModule_AddObject(m, "herror", socket_herror);
4225 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004226 NULL);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004227 if (socket_gaierror == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00004228 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004229 Py_INCREF(socket_gaierror);
4230 PyModule_AddObject(m, "gaierror", socket_gaierror);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00004231 socket_timeout = PyErr_NewException("socket.timeout",
4232 socket_error, NULL);
4233 if (socket_timeout == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00004234 return NULL;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00004235 Py_INCREF(socket_timeout);
4236 PyModule_AddObject(m, "timeout", socket_timeout);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004237 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00004238 if (PyModule_AddObject(m, "SocketType",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004239 (PyObject *)&sock_type) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00004240 return NULL;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004241 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00004242 if (PyModule_AddObject(m, "socket",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004243 (PyObject *)&sock_type) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00004244 return NULL;
Guido van Rossum09be4091999-08-09 14:40:40 +00004245
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004246#ifdef ENABLE_IPV6
4247 has_ipv6 = Py_True;
4248#else
4249 has_ipv6 = Py_False;
4250#endif
4251 Py_INCREF(has_ipv6);
4252 PyModule_AddObject(m, "has_ipv6", has_ipv6);
4253
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004254 /* Export C API */
Fred Drake4baedc12002-04-01 14:53:37 +00004255 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
Benjamin Petersonb173f782009-05-05 22:31:58 +00004256 PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004257 ) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00004258 return NULL;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004259
Guido van Rossum09be4091999-08-09 14:40:40 +00004260 /* Address families (we only support AF_INET and AF_UNIX) */
4261#ifdef AF_UNSPEC
Fred Drake4baedc12002-04-01 14:53:37 +00004262 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00004263#endif
Fred Drake4baedc12002-04-01 14:53:37 +00004264 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004265#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00004266 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004267#endif /* AF_INET6 */
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00004268#if defined(AF_UNIX)
Fred Drake4baedc12002-04-01 14:53:37 +00004269 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004270#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00004271#ifdef AF_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00004272 /* Amateur Radio AX.25 */
4273 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004274#endif
4275#ifdef AF_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00004276 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00004277#endif
4278#ifdef AF_APPLETALK
Fred Drake4baedc12002-04-01 14:53:37 +00004279 /* Appletalk DDP */
4280 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004281#endif
4282#ifdef AF_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00004283 /* Amateur radio NetROM */
4284 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004285#endif
4286#ifdef AF_BRIDGE
Fred Drake4baedc12002-04-01 14:53:37 +00004287 /* Multiprotocol bridge */
4288 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004289#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004290#ifdef AF_ATMPVC
4291 /* ATM PVCs */
4292 PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC);
4293#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004294#ifdef AF_AAL5
Fred Drake4baedc12002-04-01 14:53:37 +00004295 /* Reserved for Werner's ATM */
4296 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00004297#endif
4298#ifdef AF_X25
Fred Drake4baedc12002-04-01 14:53:37 +00004299 /* Reserved for X.25 project */
4300 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004301#endif
4302#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00004303 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00004304#endif
4305#ifdef AF_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00004306 /* Amateur Radio X.25 PLP */
4307 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004308#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004309#ifdef AF_DECnet
4310 /* Reserved for DECnet project */
4311 PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet);
4312#endif
4313#ifdef AF_NETBEUI
4314 /* Reserved for 802.2LLC project */
4315 PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI);
4316#endif
4317#ifdef AF_SECURITY
4318 /* Security callback pseudo AF */
4319 PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY);
4320#endif
4321#ifdef AF_KEY
4322 /* PF_KEY key management API */
4323 PyModule_AddIntConstant(m, "AF_KEY", AF_KEY);
4324#endif
4325#ifdef AF_NETLINK
4326 /* */
4327 PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);
Martin v. Löwis11017b12006-01-14 18:12:57 +00004328 PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004329#ifdef NETLINK_SKIP
Martin v. Löwis11017b12006-01-14 18:12:57 +00004330 PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004331#endif
4332#ifdef NETLINK_W1
4333 PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1);
4334#endif
Martin v. Löwis11017b12006-01-14 18:12:57 +00004335 PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK);
4336 PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004337#ifdef NETLINK_TCPDIAG
Martin v. Löwis11017b12006-01-14 18:12:57 +00004338 PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004339#endif
4340#ifdef NETLINK_NFLOG
Martin v. Löwis11017b12006-01-14 18:12:57 +00004341 PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004342#endif
Neal Norwitz65851662006-01-16 04:31:40 +00004343#ifdef NETLINK_XFRM
Martin v. Löwis11017b12006-01-14 18:12:57 +00004344 PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM);
Neal Norwitz65851662006-01-16 04:31:40 +00004345#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004346#ifdef NETLINK_ARPD
Martin v. Löwis11017b12006-01-14 18:12:57 +00004347 PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004348#endif
4349#ifdef NETLINK_ROUTE6
Martin v. Löwis11017b12006-01-14 18:12:57 +00004350 PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004351#endif
Martin v. Löwis11017b12006-01-14 18:12:57 +00004352 PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW);
Thomas Wouterscf297e42007-02-23 15:07:44 +00004353#ifdef NETLINK_DNRTMSG
Martin v. Löwis11017b12006-01-14 18:12:57 +00004354 PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG);
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004355#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004356#ifdef NETLINK_TAPBASE
Martin v. Löwis11017b12006-01-14 18:12:57 +00004357 PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004358#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004359#endif /* AF_NETLINK */
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004360#ifdef AF_ROUTE
4361 /* Alias to emulate 4.4BSD */
4362 PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE);
4363#endif
4364#ifdef AF_ASH
4365 /* Ash */
4366 PyModule_AddIntConstant(m, "AF_ASH", AF_ASH);
4367#endif
4368#ifdef AF_ECONET
4369 /* Acorn Econet */
4370 PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET);
4371#endif
4372#ifdef AF_ATMSVC
4373 /* ATM SVCs */
4374 PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC);
4375#endif
4376#ifdef AF_SNA
4377 /* Linux SNA Project (nutters!) */
4378 PyModule_AddIntConstant(m, "AF_SNA", AF_SNA);
4379#endif
4380#ifdef AF_IRDA
4381 /* IRDA sockets */
4382 PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA);
4383#endif
4384#ifdef AF_PPPOX
4385 /* PPPoX sockets */
4386 PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX);
4387#endif
4388#ifdef AF_WANPIPE
4389 /* Wanpipe API Sockets */
4390 PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE);
4391#endif
4392#ifdef AF_LLC
4393 /* Linux LLC */
4394 PyModule_AddIntConstant(m, "AF_LLC", AF_LLC);
4395#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00004396
Hye-Shik Chang81268602004-02-02 06:05:24 +00004397#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00004398 PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);
4399 PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);
Thomas Wouterscf297e42007-02-23 15:07:44 +00004400 PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI);
4401 PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI);
Thomas Wouterscf297e42007-02-23 15:07:44 +00004402 PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER);
Hye-Shik Chang81268602004-02-02 06:05:24 +00004403#if !defined(__FreeBSD__)
Guido van Rossumcd16bf62007-06-13 18:07:49 +00004404 PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP);
4405 PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR);
Martin v. Löwis12af0482004-01-31 12:34:17 +00004406 PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00004407#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00004408 PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004409 PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
4410 PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
Martin v. Löwis12af0482004-01-31 12:34:17 +00004411#endif
4412
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00004413#ifdef HAVE_NETPACKET_PACKET_H
Fred Drake4baedc12002-04-01 14:53:37 +00004414 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
4415 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
4416 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
4417 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
4418 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
4419 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
4420 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
4421 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
4422 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00004423#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004424
Christian Heimes043d6f62008-01-07 17:19:16 +00004425#ifdef HAVE_LINUX_TIPC_H
4426 PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC);
4427
4428 /* for addresses */
4429 PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ);
4430 PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME);
4431 PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID);
4432
4433 PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE);
4434 PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE);
4435 PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE);
4436
4437 /* for setsockopt() */
4438 PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC);
4439 PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE);
4440 PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE);
4441 PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE",
4442 TIPC_DEST_DROPPABLE);
4443 PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT);
4444
4445 PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE",
4446 TIPC_LOW_IMPORTANCE);
4447 PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE",
4448 TIPC_MEDIUM_IMPORTANCE);
4449 PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE",
4450 TIPC_HIGH_IMPORTANCE);
4451 PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE",
4452 TIPC_CRITICAL_IMPORTANCE);
4453
4454 /* for subscriptions */
4455 PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS);
4456 PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE);
Christian Heimes25bb7832008-01-11 16:17:00 +00004457#ifdef TIPC_SUB_CANCEL
4458 /* doesn't seem to be available everywhere */
Christian Heimes043d6f62008-01-07 17:19:16 +00004459 PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL);
Christian Heimes25bb7832008-01-11 16:17:00 +00004460#endif
Christian Heimes043d6f62008-01-07 17:19:16 +00004461 PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER);
4462 PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED);
4463 PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN);
4464 PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT);
4465 PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV);
4466 PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV);
4467#endif
4468
Guido van Rossum09be4091999-08-09 14:40:40 +00004469 /* Socket types */
Fred Drake4baedc12002-04-01 14:53:37 +00004470 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
4471 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004472/* We have incomplete socket support. */
Fred Drake4baedc12002-04-01 14:53:37 +00004473 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
4474 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00004475#if defined(SOCK_RDM)
Fred Drake4baedc12002-04-01 14:53:37 +00004476 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004477#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004478
4479#ifdef SO_DEBUG
Fred Drake4baedc12002-04-01 14:53:37 +00004480 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004481#endif
4482#ifdef SO_ACCEPTCONN
Fred Drake4baedc12002-04-01 14:53:37 +00004483 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004484#endif
4485#ifdef SO_REUSEADDR
Fred Drake4baedc12002-04-01 14:53:37 +00004486 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004487#endif
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00004488#ifdef SO_EXCLUSIVEADDRUSE
4489 PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE);
4490#endif
4491
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004492#ifdef SO_KEEPALIVE
Fred Drake4baedc12002-04-01 14:53:37 +00004493 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004494#endif
4495#ifdef SO_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00004496 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004497#endif
4498#ifdef SO_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00004499 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004500#endif
4501#ifdef SO_USELOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00004502 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004503#endif
4504#ifdef SO_LINGER
Fred Drake4baedc12002-04-01 14:53:37 +00004505 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004506#endif
4507#ifdef SO_OOBINLINE
Fred Drake4baedc12002-04-01 14:53:37 +00004508 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004509#endif
4510#ifdef SO_REUSEPORT
Fred Drake4baedc12002-04-01 14:53:37 +00004511 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004512#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004513#ifdef SO_SNDBUF
Fred Drake4baedc12002-04-01 14:53:37 +00004514 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004515#endif
4516#ifdef SO_RCVBUF
Fred Drake4baedc12002-04-01 14:53:37 +00004517 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004518#endif
4519#ifdef SO_SNDLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00004520 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004521#endif
4522#ifdef SO_RCVLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00004523 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004524#endif
4525#ifdef SO_SNDTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00004526 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004527#endif
4528#ifdef SO_RCVTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00004529 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004530#endif
4531#ifdef SO_ERROR
Fred Drake4baedc12002-04-01 14:53:37 +00004532 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004533#endif
4534#ifdef SO_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004535 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004536#endif
4537
4538 /* Maximum number of connections for "listen" */
4539#ifdef SOMAXCONN
Fred Drake4baedc12002-04-01 14:53:37 +00004540 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004541#else
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004542 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004543#endif
4544
4545 /* Flags for send, recv */
4546#ifdef MSG_OOB
Fred Drake4baedc12002-04-01 14:53:37 +00004547 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004548#endif
4549#ifdef MSG_PEEK
Fred Drake4baedc12002-04-01 14:53:37 +00004550 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004551#endif
4552#ifdef MSG_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00004553 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004554#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00004555#ifdef MSG_DONTWAIT
Fred Drake4baedc12002-04-01 14:53:37 +00004556 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00004557#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004558#ifdef MSG_EOR
Fred Drake4baedc12002-04-01 14:53:37 +00004559 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004560#endif
4561#ifdef MSG_TRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00004562 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004563#endif
4564#ifdef MSG_CTRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00004565 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004566#endif
4567#ifdef MSG_WAITALL
Fred Drake4baedc12002-04-01 14:53:37 +00004568 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004569#endif
4570#ifdef MSG_BTAG
Fred Drake4baedc12002-04-01 14:53:37 +00004571 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004572#endif
4573#ifdef MSG_ETAG
Fred Drake4baedc12002-04-01 14:53:37 +00004574 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004575#endif
4576
4577 /* Protocol level and numbers, usable for [gs]etsockopt */
4578#ifdef SOL_SOCKET
Fred Drake4baedc12002-04-01 14:53:37 +00004579 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004580#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004581#ifdef SOL_IP
Fred Drake4baedc12002-04-01 14:53:37 +00004582 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004583#else
Fred Drake4baedc12002-04-01 14:53:37 +00004584 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00004585#endif
4586#ifdef SOL_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00004587 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00004588#endif
4589#ifdef SOL_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00004590 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004591#endif
4592#ifdef SOL_ATALK
Fred Drake4baedc12002-04-01 14:53:37 +00004593 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004594#endif
4595#ifdef SOL_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00004596 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004597#endif
4598#ifdef SOL_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00004599 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004600#endif
4601#ifdef SOL_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00004602 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004603#else
Fred Drake4baedc12002-04-01 14:53:37 +00004604 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00004605#endif
4606#ifdef SOL_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00004607 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004608#else
Fred Drake4baedc12002-04-01 14:53:37 +00004609 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00004610#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004611#ifdef IPPROTO_IP
Fred Drake4baedc12002-04-01 14:53:37 +00004612 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00004613#else
Fred Drake4baedc12002-04-01 14:53:37 +00004614 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004615#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004616#ifdef IPPROTO_HOPOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004617 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004618#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004619#ifdef IPPROTO_ICMP
Fred Drake4baedc12002-04-01 14:53:37 +00004620 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00004621#else
Fred Drake4baedc12002-04-01 14:53:37 +00004622 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004623#endif
4624#ifdef IPPROTO_IGMP
Fred Drake4baedc12002-04-01 14:53:37 +00004625 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004626#endif
4627#ifdef IPPROTO_GGP
Fred Drake4baedc12002-04-01 14:53:37 +00004628 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004629#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004630#ifdef IPPROTO_IPV4
Fred Drake4baedc12002-04-01 14:53:37 +00004631 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004632#endif
Martin v. Löwisa0f17342003-10-03 13:56:20 +00004633#ifdef IPPROTO_IPV6
4634 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
4635#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004636#ifdef IPPROTO_IPIP
Fred Drake4baedc12002-04-01 14:53:37 +00004637 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004638#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004639#ifdef IPPROTO_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00004640 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00004641#else
Fred Drake4baedc12002-04-01 14:53:37 +00004642 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004643#endif
4644#ifdef IPPROTO_EGP
Fred Drake4baedc12002-04-01 14:53:37 +00004645 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004646#endif
4647#ifdef IPPROTO_PUP
Fred Drake4baedc12002-04-01 14:53:37 +00004648 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004649#endif
4650#ifdef IPPROTO_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00004651 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00004652#else
Fred Drake4baedc12002-04-01 14:53:37 +00004653 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004654#endif
4655#ifdef IPPROTO_IDP
Fred Drake4baedc12002-04-01 14:53:37 +00004656 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004657#endif
4658#ifdef IPPROTO_HELLO
Fred Drake4baedc12002-04-01 14:53:37 +00004659 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004660#endif
4661#ifdef IPPROTO_ND
Fred Drake4baedc12002-04-01 14:53:37 +00004662 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004663#endif
4664#ifdef IPPROTO_TP
Fred Drake4baedc12002-04-01 14:53:37 +00004665 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004666#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004667#ifdef IPPROTO_IPV6
Fred Drake4baedc12002-04-01 14:53:37 +00004668 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004669#endif
4670#ifdef IPPROTO_ROUTING
Fred Drake4baedc12002-04-01 14:53:37 +00004671 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004672#endif
4673#ifdef IPPROTO_FRAGMENT
Fred Drake4baedc12002-04-01 14:53:37 +00004674 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004675#endif
4676#ifdef IPPROTO_RSVP
Fred Drake4baedc12002-04-01 14:53:37 +00004677 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004678#endif
4679#ifdef IPPROTO_GRE
Fred Drake4baedc12002-04-01 14:53:37 +00004680 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004681#endif
4682#ifdef IPPROTO_ESP
Fred Drake4baedc12002-04-01 14:53:37 +00004683 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004684#endif
4685#ifdef IPPROTO_AH
Fred Drake4baedc12002-04-01 14:53:37 +00004686 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004687#endif
4688#ifdef IPPROTO_MOBILE
Fred Drake4baedc12002-04-01 14:53:37 +00004689 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004690#endif
4691#ifdef IPPROTO_ICMPV6
Fred Drake4baedc12002-04-01 14:53:37 +00004692 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004693#endif
4694#ifdef IPPROTO_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00004695 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004696#endif
4697#ifdef IPPROTO_DSTOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004698 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004699#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004700#ifdef IPPROTO_XTP
Fred Drake4baedc12002-04-01 14:53:37 +00004701 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004702#endif
4703#ifdef IPPROTO_EON
Fred Drake4baedc12002-04-01 14:53:37 +00004704 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004705#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004706#ifdef IPPROTO_PIM
Fred Drake4baedc12002-04-01 14:53:37 +00004707 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004708#endif
4709#ifdef IPPROTO_IPCOMP
Fred Drake4baedc12002-04-01 14:53:37 +00004710 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004711#endif
4712#ifdef IPPROTO_VRRP
Fred Drake4baedc12002-04-01 14:53:37 +00004713 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004714#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004715#ifdef IPPROTO_BIP
Fred Drake4baedc12002-04-01 14:53:37 +00004716 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004717#endif
4718/**/
4719#ifdef IPPROTO_RAW
Fred Drake4baedc12002-04-01 14:53:37 +00004720 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00004721#else
Fred Drake4baedc12002-04-01 14:53:37 +00004722 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004723#endif
4724#ifdef IPPROTO_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00004725 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004726#endif
4727
4728 /* Some port configuration */
4729#ifdef IPPORT_RESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00004730 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004731#else
Fred Drake4baedc12002-04-01 14:53:37 +00004732 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004733#endif
4734#ifdef IPPORT_USERRESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00004735 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004736#else
Fred Drake4baedc12002-04-01 14:53:37 +00004737 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004738#endif
4739
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004740 /* Some reserved IP v.4 addresses */
4741#ifdef INADDR_ANY
Fred Drake4baedc12002-04-01 14:53:37 +00004742 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004743#else
Fred Drake4baedc12002-04-01 14:53:37 +00004744 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004745#endif
4746#ifdef INADDR_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00004747 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004748#else
Fred Drake4baedc12002-04-01 14:53:37 +00004749 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004750#endif
4751#ifdef INADDR_LOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00004752 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004753#else
Fred Drake4baedc12002-04-01 14:53:37 +00004754 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004755#endif
4756#ifdef INADDR_UNSPEC_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004757 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004758#else
Fred Drake4baedc12002-04-01 14:53:37 +00004759 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004760#endif
4761#ifdef INADDR_ALLHOSTS_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004762 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
4763 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004764#else
Fred Drake4baedc12002-04-01 14:53:37 +00004765 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004766#endif
4767#ifdef INADDR_MAX_LOCAL_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004768 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
4769 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004770#else
Fred Drake4baedc12002-04-01 14:53:37 +00004771 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004772#endif
4773#ifdef INADDR_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00004774 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004775#else
Fred Drake4baedc12002-04-01 14:53:37 +00004776 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004777#endif
4778
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004779 /* IPv4 [gs]etsockopt options */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004780#ifdef IP_OPTIONS
Fred Drake4baedc12002-04-01 14:53:37 +00004781 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004782#endif
4783#ifdef IP_HDRINCL
Fred Drake4baedc12002-04-01 14:53:37 +00004784 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004785#endif
4786#ifdef IP_TOS
Fred Drake4baedc12002-04-01 14:53:37 +00004787 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004788#endif
4789#ifdef IP_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004790 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004791#endif
4792#ifdef IP_RECVOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004793 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004794#endif
4795#ifdef IP_RECVRETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004796 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004797#endif
4798#ifdef IP_RECVDSTADDR
Fred Drake4baedc12002-04-01 14:53:37 +00004799 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004800#endif
4801#ifdef IP_RETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004802 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004803#endif
4804#ifdef IP_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00004805 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004806#endif
4807#ifdef IP_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004808 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004809#endif
4810#ifdef IP_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004811 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004812#endif
4813#ifdef IP_ADD_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00004814 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004815#endif
4816#ifdef IP_DROP_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00004817 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004818#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004819#ifdef IP_DEFAULT_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004820 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
4821 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00004822#endif
4823#ifdef IP_DEFAULT_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004824 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
4825 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004826#endif
4827#ifdef IP_MAX_MEMBERSHIPS
Fred Drake4baedc12002-04-01 14:53:37 +00004828 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00004829#endif
4830
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004831 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
4832#ifdef IPV6_JOIN_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004833 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004834#endif
4835#ifdef IPV6_LEAVE_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004836 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004837#endif
4838#ifdef IPV6_MULTICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00004839 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004840#endif
4841#ifdef IPV6_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00004842 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004843#endif
4844#ifdef IPV6_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004845 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004846#endif
4847#ifdef IPV6_UNICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00004848 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004849#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004850 /* Additional IPV6 socket options, defined in RFC 3493 */
4851#ifdef IPV6_V6ONLY
4852 PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);
4853#endif
4854 /* Advanced IPV6 socket options, from RFC 3542 */
4855#ifdef IPV6_CHECKSUM
4856 PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);
4857#endif
4858#ifdef IPV6_DONTFRAG
4859 PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);
4860#endif
4861#ifdef IPV6_DSTOPTS
4862 PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);
4863#endif
4864#ifdef IPV6_HOPLIMIT
4865 PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);
4866#endif
4867#ifdef IPV6_HOPOPTS
4868 PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);
4869#endif
4870#ifdef IPV6_NEXTHOP
4871 PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);
4872#endif
4873#ifdef IPV6_PATHMTU
4874 PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);
4875#endif
4876#ifdef IPV6_PKTINFO
4877 PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);
4878#endif
4879#ifdef IPV6_RECVDSTOPTS
4880 PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);
4881#endif
4882#ifdef IPV6_RECVHOPLIMIT
4883 PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);
4884#endif
4885#ifdef IPV6_RECVHOPOPTS
4886 PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);
4887#endif
4888#ifdef IPV6_RECVPKTINFO
4889 PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);
4890#endif
4891#ifdef IPV6_RECVRTHDR
4892 PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);
4893#endif
4894#ifdef IPV6_RECVTCLASS
4895 PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);
4896#endif
4897#ifdef IPV6_RTHDR
4898 PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);
4899#endif
4900#ifdef IPV6_RTHDRDSTOPTS
4901 PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);
4902#endif
4903#ifdef IPV6_RTHDR_TYPE_0
4904 PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);
4905#endif
4906#ifdef IPV6_RECVPATHMTU
4907 PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);
4908#endif
4909#ifdef IPV6_TCLASS
4910 PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);
4911#endif
4912#ifdef IPV6_USE_MIN_MTU
4913 PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);
4914#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004915
Guido van Rossum09be4091999-08-09 14:40:40 +00004916 /* TCP options */
4917#ifdef TCP_NODELAY
Fred Drake4baedc12002-04-01 14:53:37 +00004918 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00004919#endif
4920#ifdef TCP_MAXSEG
Fred Drake4baedc12002-04-01 14:53:37 +00004921 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00004922#endif
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004923#ifdef TCP_CORK
Fred Drake4baedc12002-04-01 14:53:37 +00004924 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004925#endif
4926#ifdef TCP_KEEPIDLE
Fred Drake4baedc12002-04-01 14:53:37 +00004927 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004928#endif
4929#ifdef TCP_KEEPINTVL
Fred Drake4baedc12002-04-01 14:53:37 +00004930 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004931#endif
4932#ifdef TCP_KEEPCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004933 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004934#endif
4935#ifdef TCP_SYNCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004936 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004937#endif
4938#ifdef TCP_LINGER2
Fred Drake4baedc12002-04-01 14:53:37 +00004939 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004940#endif
4941#ifdef TCP_DEFER_ACCEPT
Fred Drake4baedc12002-04-01 14:53:37 +00004942 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004943#endif
4944#ifdef TCP_WINDOW_CLAMP
Fred Drake4baedc12002-04-01 14:53:37 +00004945 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004946#endif
4947#ifdef TCP_INFO
Fred Drake4baedc12002-04-01 14:53:37 +00004948 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004949#endif
4950#ifdef TCP_QUICKACK
Fred Drake4baedc12002-04-01 14:53:37 +00004951 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004952#endif
4953
Guido van Rossum09be4091999-08-09 14:40:40 +00004954
4955 /* IPX options */
4956#ifdef IPX_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004957 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004958#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004959
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004960 /* get{addr,name}info parameters */
4961#ifdef EAI_ADDRFAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004962 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004963#endif
4964#ifdef EAI_AGAIN
Fred Drake4baedc12002-04-01 14:53:37 +00004965 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004966#endif
4967#ifdef EAI_BADFLAGS
Fred Drake4baedc12002-04-01 14:53:37 +00004968 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004969#endif
4970#ifdef EAI_FAIL
Fred Drake4baedc12002-04-01 14:53:37 +00004971 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004972#endif
4973#ifdef EAI_FAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004974 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004975#endif
4976#ifdef EAI_MEMORY
Fred Drake4baedc12002-04-01 14:53:37 +00004977 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004978#endif
4979#ifdef EAI_NODATA
Fred Drake4baedc12002-04-01 14:53:37 +00004980 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004981#endif
4982#ifdef EAI_NONAME
Fred Drake4baedc12002-04-01 14:53:37 +00004983 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004984#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004985#ifdef EAI_OVERFLOW
4986 PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);
4987#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004988#ifdef EAI_SERVICE
Fred Drake4baedc12002-04-01 14:53:37 +00004989 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004990#endif
4991#ifdef EAI_SOCKTYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004992 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004993#endif
4994#ifdef EAI_SYSTEM
Fred Drake4baedc12002-04-01 14:53:37 +00004995 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004996#endif
4997#ifdef EAI_BADHINTS
Fred Drake4baedc12002-04-01 14:53:37 +00004998 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004999#endif
5000#ifdef EAI_PROTOCOL
Fred Drake4baedc12002-04-01 14:53:37 +00005001 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005002#endif
5003#ifdef EAI_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00005004 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005005#endif
5006#ifdef AI_PASSIVE
Fred Drake4baedc12002-04-01 14:53:37 +00005007 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005008#endif
5009#ifdef AI_CANONNAME
Fred Drake4baedc12002-04-01 14:53:37 +00005010 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005011#endif
5012#ifdef AI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00005013 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005014#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00005015#ifdef AI_NUMERICSERV
5016 PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);
5017#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005018#ifdef AI_MASK
Fred Drake4baedc12002-04-01 14:53:37 +00005019 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005020#endif
5021#ifdef AI_ALL
Fred Drake4baedc12002-04-01 14:53:37 +00005022 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005023#endif
5024#ifdef AI_V4MAPPED_CFG
Fred Drake4baedc12002-04-01 14:53:37 +00005025 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005026#endif
5027#ifdef AI_ADDRCONFIG
Fred Drake4baedc12002-04-01 14:53:37 +00005028 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005029#endif
5030#ifdef AI_V4MAPPED
Fred Drake4baedc12002-04-01 14:53:37 +00005031 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005032#endif
5033#ifdef AI_DEFAULT
Fred Drake4baedc12002-04-01 14:53:37 +00005034 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005035#endif
5036#ifdef NI_MAXHOST
Fred Drake4baedc12002-04-01 14:53:37 +00005037 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005038#endif
5039#ifdef NI_MAXSERV
Fred Drake4baedc12002-04-01 14:53:37 +00005040 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005041#endif
5042#ifdef NI_NOFQDN
Fred Drake4baedc12002-04-01 14:53:37 +00005043 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005044#endif
5045#ifdef NI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00005046 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005047#endif
5048#ifdef NI_NAMEREQD
Fred Drake4baedc12002-04-01 14:53:37 +00005049 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005050#endif
5051#ifdef NI_NUMERICSERV
Fred Drake4baedc12002-04-01 14:53:37 +00005052 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005053#endif
5054#ifdef NI_DGRAM
Fred Drake4baedc12002-04-01 14:53:37 +00005055 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00005056#endif
5057
Martin v. Löwis94681fc2003-11-27 19:40:22 +00005058 /* shutdown() parameters */
5059#ifdef SHUT_RD
5060 PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);
5061#elif defined(SD_RECEIVE)
5062 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
5063#else
5064 PyModule_AddIntConstant(m, "SHUT_RD", 0);
5065#endif
5066#ifdef SHUT_WR
5067 PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);
5068#elif defined(SD_SEND)
5069 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
5070#else
5071 PyModule_AddIntConstant(m, "SHUT_WR", 1);
5072#endif
5073#ifdef SHUT_RDWR
5074 PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);
5075#elif defined(SD_BOTH)
5076 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
5077#else
5078 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
5079#endif
5080
Christian Heimesfaf2f632008-01-06 16:59:19 +00005081#ifdef SIO_RCVALL
Christian Heimesd32ed6f2008-01-14 18:49:24 +00005082 {
Kristján Valur Jónsson847ec752009-09-27 21:10:38 +00005083 DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS};
5084 const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"};
5085 int i;
5086 for(i = 0; i<sizeof(codes)/sizeof(*codes); ++i) {
5087 PyObject *tmp;
5088 tmp = PyLong_FromUnsignedLong(codes[i]);
5089 if (tmp == NULL)
5090 return NULL;
5091 PyModule_AddObject(m, names[i], tmp);
5092 }
Christian Heimesd32ed6f2008-01-14 18:49:24 +00005093 }
Christian Heimesfaf2f632008-01-06 16:59:19 +00005094 PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF);
5095 PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON);
5096 PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00005097#ifdef RCVALL_IPLEVEL
Christian Heimesfaf2f632008-01-06 16:59:19 +00005098 PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00005099#endif
5100#ifdef RCVALL_MAX
Christian Heimesfaf2f632008-01-06 16:59:19 +00005101 PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX);
Amaury Forgeot d'Arc762681b2008-06-12 23:03:41 +00005102#endif
Christian Heimesfaf2f632008-01-06 16:59:19 +00005103#endif /* _MSTCPIP_ */
5104
Guido van Rossum4f199ea1998-04-09 20:56:35 +00005105 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00005106#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
5107 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00005108#endif
Martin v. Löwis1a214512008-06-11 05:26:20 +00005109 return m;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00005110}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005111
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005112
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005113#ifndef HAVE_INET_PTON
Christian Heimes96e7b3d2007-11-20 06:51:17 +00005114#if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_LONGHORN)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005115
5116/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00005117/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005118
Guido van Rossum3eede5a2002-06-07 02:08:35 +00005119int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005120inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005121{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005122 if (af == AF_INET) {
Benjamin Petersonf91df042009-02-13 02:50:59 +00005123#if (SIZEOF_INT != 4)
5124#error "Not sure if in_addr_t exists and int is not 32-bits."
5125#endif
5126 unsigned int packed_addr;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005127 packed_addr = inet_addr(src);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005128 if (packed_addr == INADDR_NONE)
5129 return 0;
5130 memcpy(dst, &packed_addr, 4);
5131 return 1;
5132 }
5133 /* Should set errno to EAFNOSUPPORT */
5134 return -1;
5135}
5136
Martin v. Löwisc925b1532001-07-21 09:42:15 +00005137const char *
5138inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005139{
5140 if (af == AF_INET) {
5141 struct in_addr packed_addr;
5142 if (size < 16)
5143 /* Should set errno to ENOSPC. */
5144 return NULL;
5145 memcpy(&packed_addr, src, sizeof(packed_addr));
5146 return strncpy(dst, inet_ntoa(packed_addr), size);
5147 }
5148 /* Should set errno to EAFNOSUPPORT */
5149 return NULL;
5150}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00005151
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00005152#endif
Christian Heimesb6150692007-11-15 23:37:07 +00005153#endif