blob: f5ad29286522912a5d7117d0d5e6fae48517ee38 [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
Martin v. Löwis11017b12006-01-14 18:12:57 +000010 portable manner, though AF_PACKET and AF_NETLINK are supported under Linux.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000011- No read/write operations (use sendall/recv or makefile instead).
12- Additional restrictions apply on some non-Unix platforms (compensated
13 for by socket.py).
Guido van Rossum6574b3e1991-06-25 21:36:08 +000014
Guido van Rossum27e177d1995-03-16 15:43:47 +000015Module interface:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000016
Guido van Rossum27e177d1995-03-16 15:43:47 +000017- socket.error: exception raised for socket specific errors
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000018- socket.gaierror: exception raised for getaddrinfo/getnameinfo errors,
19 a subclass of socket.error
20- socket.herror: exception raised for gethostby* errors,
21 a subclass of socket.error
Guido van Rossum30a685f1991-06-27 15:51:29 +000022- socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000023- socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])
Guido van Rossum27e177d1995-03-16 15:43:47 +000024- socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')
Guido van Rossum25405c71996-12-19 16:42:52 +000025- socket.getprotobyname(protocolname) --> protocol number
Barry Warsaw11b91a02004-06-28 00:50:43 +000026- socket.getservbyname(servicename[, protocolname]) --> port number
27- socket.getservbyport(portnumber[, protocolname]) --> service name
Guido van Rossum7d0a8262007-05-21 23:13:11 +000028- socket.socket([family[, type [, proto, fileno]]]) --> new socket object
29 (fileno specifies a pre-existing socket file descriptor)
Dave Cole331708b2004-08-09 04:51:41 +000030- socket.socketpair([family[, type [, proto]]]) --> (socket, socket)
Guido van Rossum006bf911996-06-12 04:04:55 +000031- socket.ntohs(16 bit value) --> new int object
32- socket.ntohl(32 bit value) --> new int object
33- socket.htons(16 bit value) --> new int object
34- socket.htonl(32 bit value) --> new int object
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000035- socket.getaddrinfo(host, port [, family, socktype, proto, flags])
36 --> List of (family, socktype, proto, canonname, sockaddr)
37- socket.getnameinfo(sockaddr, flags) --> (host, port)
Guido van Rossum27e177d1995-03-16 15:43:47 +000038- socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
Guido van Rossum47dfa4a2003-04-25 05:48:32 +000039- socket.has_ipv6: boolean value indicating if IPv6 is supported
Guido van Rossum5c9eb211999-08-20 18:21:51 +000040- socket.inet_aton(IP address) -> 32-bit packed IP representation
41- socket.inet_ntoa(packed IP) -> IP address string
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +000042- socket.getdefaulttimeout() -> None | float
43- socket.setdefaulttimeout(None | float)
Guido van Rossum6574b3e1991-06-25 21:36:08 +000044- an Internet socket address is a pair (hostname, port)
45 where hostname can be anything recognized by gethostbyname()
46 (including the dd.dd.dd.dd notation) and port is in host byte order
47- where a hostname is returned, the dd.dd.dd.dd notation is used
Guido van Rossum27e177d1995-03-16 15:43:47 +000048- a UNIX domain socket address is a string specifying the pathname
Jeremy Hylton22308652001-02-02 03:23:09 +000049- an AF_PACKET socket address is a tuple containing a string
50 specifying the ethernet interface and an integer specifying
51 the Ethernet protocol number to be received. For example:
Jeremy Hyltondbfb6622001-02-02 19:55:17 +000052 ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +000053 specify packet-type and ha-type/addr.
Guido van Rossum6574b3e1991-06-25 21:36:08 +000054
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000055Local naming conventions:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000056
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000057- names starting with sock_ are socket object methods
58- names starting with socket_ are module-level functions
59- names starting with PySocket are exported through socketmodule.h
Guido van Rossum30a685f1991-06-27 15:51:29 +000060
Guido van Rossum6574b3e1991-06-25 21:36:08 +000061*/
62
Thomas Wouters477c8d52006-05-27 19:21:47 +000063#ifdef __APPLE__
64 /*
65 * inet_aton is not available on OSX 10.3, yet we want to use a binary
66 * that was build on 10.4 or later to work on that release, weak linking
67 * comes to the rescue.
68 */
69# pragma weak inet_aton
70#endif
71
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000072#include "Python.h"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000073#include "structmember.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000074
Guido van Rossum47dfa4a2003-04-25 05:48:32 +000075#undef MAX
76#define MAX(x, y) ((x) < (y) ? (y) : (x))
77
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000078/* Socket object documentation */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000079PyDoc_STRVAR(sock_doc,
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000080"socket([family[, type[, proto]]]) -> socket object\n\
81\n\
82Open a socket of the given type. The family argument specifies the\n\
83address family; it defaults to AF_INET. The type argument specifies\n\
84whether this is a stream (SOCK_STREAM, this is the default)\n\
85or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
86specifying the default protocol. Keyword arguments are accepted.\n\
87\n\
88A socket object represents one endpoint of a network connection.\n\
89\n\
90Methods of socket objects (keyword arguments not allowed):\n\
91\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +000092_accept() -- accept connection, returning new socket fd and client address\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000093bind(addr) -- bind the socket to a local address\n\
94close() -- close the socket\n\
95connect(addr) -- connect the socket to a remote address\n\
96connect_ex(addr) -- connect, return an error code instead of an exception\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +000097_dup() -- return a new socket fd duplicated from fileno()\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000098fileno() -- return underlying file descriptor\n\
99getpeername() -- return remote address [*]\n\
100getsockname() -- return local address\n\
101getsockopt(level, optname[, buflen]) -- get socket options\n\
102gettimeout() -- return timeout or None\n\
103listen(n) -- start listening for incoming connections\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000104recv(buflen[, flags]) -- receive data\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000105recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000106recvfrom(buflen[, flags]) -- receive data and sender\'s address\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000107recvfrom_into(buffer[, nbytes, [, flags])\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000108 -- receive data and sender\'s address (into a buffer)\n\
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000109sendall(data[, flags]) -- send all data\n\
110send(data[, flags]) -- send data, may not send all of it\n\
111sendto(data[, flags], addr) -- send data to a given address\n\
112setblocking(0 | 1) -- set or clear the blocking I/O flag\n\
113setsockopt(level, optname, value) -- set socket options\n\
114settimeout(None | float) -- set or clear the timeout\n\
115shutdown(how) -- shut down traffic in one or both directions\n\
116\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000117 [*] not available on all platforms!");
Guido van Rossum3baaa131999-03-22 21:44:51 +0000118
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000119/* XXX This is a terrible mess of platform-dependent preprocessor hacks.
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000120 I hope some day someone can clean this up please... */
121
Guido van Rossum9376b741999-09-15 22:01:40 +0000122/* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
123 script doesn't get this right, so we hardcode some platform checks below.
124 On the other hand, not all Linux versions agree, so there the settings
125 computed by the configure script are needed! */
126
127#ifndef linux
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000128# undef HAVE_GETHOSTBYNAME_R_3_ARG
129# undef HAVE_GETHOSTBYNAME_R_5_ARG
130# undef HAVE_GETHOSTBYNAME_R_6_ARG
Guido van Rossum9376b741999-09-15 22:01:40 +0000131#endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000132
Guido van Rossum7a122991999-04-13 04:07:32 +0000133#ifndef WITH_THREAD
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000134# undef HAVE_GETHOSTBYNAME_R
Guido van Rossum7a122991999-04-13 04:07:32 +0000135#endif
136
Guido van Rossume7de2061999-03-24 17:24:33 +0000137#ifdef HAVE_GETHOSTBYNAME_R
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000138# if defined(_AIX) || defined(__osf__)
139# define HAVE_GETHOSTBYNAME_R_3_ARG
140# elif defined(__sun) || defined(__sgi)
141# define HAVE_GETHOSTBYNAME_R_5_ARG
142# elif defined(linux)
Guido van Rossum9376b741999-09-15 22:01:40 +0000143/* Rely on the configure script */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000144# else
145# undef HAVE_GETHOSTBYNAME_R
146# endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000147#endif
148
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000149#if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \
150 !defined(MS_WINDOWS)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000151# define USE_GETHOSTBYNAME_LOCK
Guido van Rossum3baaa131999-03-22 21:44:51 +0000152#endif
153
Hye-Shik Chang9ceebd52005-09-24 14:58:47 +0000154/* To use __FreeBSD_version */
155#ifdef HAVE_SYS_PARAM_H
156#include <sys/param.h>
157#endif
Just van Rossum1040d2c2003-05-09 07:53:18 +0000158/* On systems on which getaddrinfo() is believed to not be thread-safe,
Just van Rossum09aecd72003-05-09 08:03:44 +0000159 (this includes the getaddrinfo emulation) protect access with a lock. */
Hye-Shik Chang9ceebd52005-09-24 14:58:47 +0000160#if defined(WITH_THREAD) && (defined(__APPLE__) || \
161 (defined(__FreeBSD__) && __FreeBSD_version+0 < 503000) || \
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000162 defined(__OpenBSD__) || defined(__NetBSD__) || \
163 defined(__VMS) || !defined(HAVE_GETADDRINFO))
Just van Rossum1040d2c2003-05-09 07:53:18 +0000164#define USE_GETADDRINFO_LOCK
165#endif
166
167#ifdef USE_GETADDRINFO_LOCK
168#define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1);
169#define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock);
170#else
171#define ACQUIRE_GETADDRINFO_LOCK
172#define RELEASE_GETADDRINFO_LOCK
173#endif
174
175#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000176# include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000177#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000178
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000179#if defined(PYCC_VACPP)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000180# include <types.h>
181# include <io.h>
182# include <sys/ioctl.h>
183# include <utils.h>
184# include <ctype.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000185#endif
186
Martin v. Löwis9e437302002-12-06 12:57:26 +0000187#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000188# include <ioctl.h>
189#endif
190
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000191#if defined(PYOS_OS2)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000192# define INCL_DOS
193# define INCL_DOSERRORS
194# define INCL_NOPMAPI
195# include <os2.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000196#endif
197
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000198#if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000199/* make sure that the reentrant (gethostbyaddr_r etc)
200 functions are declared correctly if compiling with
201 MIPSPro 7.x in ANSI C mode (default) */
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000202
Thomas Wouters477c8d52006-05-27 19:21:47 +0000203/* XXX Using _SGIAPI is the wrong thing,
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000204 but I don't know what the right thing is. */
Trent Mick8ea5bdf2004-09-13 17:48:41 +0000205#undef _SGIAPI /* to avoid warning */
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000206#define _SGIAPI 1
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000207
Trent Mick8ea5bdf2004-09-13 17:48:41 +0000208#undef _XOPEN_SOURCE
209#include <sys/socket.h>
210#include <sys/types.h>
211#include <netinet/in.h>
212#ifdef _SS_ALIGNSIZE
213#define HAVE_GETADDRINFO 1
214#define HAVE_GETNAMEINFO 1
215#endif
216
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000217#define HAVE_INET_PTON
218#include <netdb.h>
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000219#endif
220
Thomas Wouters477c8d52006-05-27 19:21:47 +0000221/* Irix 6.5 fails to define this variable at all. This is needed
222 for both GCC and SGI's compiler. I'd say that the SGI headers
Georg Brandldbd83392006-02-20 09:42:33 +0000223 are just busted. Same thing for Solaris. */
224#if (defined(__sgi) || defined(sun)) && !defined(INET_ADDRSTRLEN)
Anthony Baxterbab23cf2003-10-04 08:00:49 +0000225#define INET_ADDRSTRLEN 16
226#endif
227
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000228/* Generic includes */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000229#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000230#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000231#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000232
Marc-André Lemburg976ade62002-02-16 18:47:07 +0000233/* Generic socket object definitions and includes */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000234#define PySocket_BUILDING_SOCKET
Marc-André Lemburgbb8b78b2002-02-16 18:44:52 +0000235#include "socketmodule.h"
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000236
237/* Addressing includes */
238
Guido van Rossum6f489d91996-06-28 20:15:15 +0000239#ifndef MS_WINDOWS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000240
241/* Non-MS WINDOWS includes */
242# include <netdb.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000243
Guido van Rossum9376b741999-09-15 22:01:40 +0000244/* Headers needed for inet_ntoa() and inet_addr() */
Skip Montanaroeb33e5a2007-08-17 12:57:41 +0000245# if defined(PYOS_OS2) && defined(PYCC_VACPP)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000246# include <netdb.h>
Tim Peters603c6832001-11-05 02:45:59 +0000247typedef size_t socklen_t;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000248# else
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000249# include <arpa/inet.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000250# endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000251
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000252# include <fcntl.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000253
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000254#else
Guido van Rossum48a680c2001-03-02 06:34:14 +0000255
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000256/* MS_WINDOWS includes */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000257# ifdef HAVE_FCNTL_H
258# include <fcntl.h>
259# endif
Guido van Rossum48a680c2001-03-02 06:34:14 +0000260
Jeremy Hylton22308652001-02-02 03:23:09 +0000261#endif
262
Skip Montanaro7befb992004-02-10 16:50:21 +0000263#include <stddef.h>
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000264
265#ifndef offsetof
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000266# define offsetof(type, member) ((size_t)(&((type *)0)->member))
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000267#endif
268
Neal Norwitz39d22e52002-11-02 19:55:21 +0000269#ifndef O_NONBLOCK
270# define O_NONBLOCK O_NDELAY
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000271#endif
272
Trent Micka708d6e2004-09-07 17:48:26 +0000273/* include Python's addrinfo.h unless it causes trouble */
274#if defined(__sgi) && _COMPILER_VERSION>700 && defined(_SS_ALIGNSIZE)
275 /* Do not include addinfo.h on some newer IRIX versions.
276 * _SS_ALIGNSIZE is defined in sys/socket.h by 6.5.21,
277 * for example, but not by 6.5.10.
278 */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000279#elif defined(_MSC_VER) && _MSC_VER>1201
Trent Micka708d6e2004-09-07 17:48:26 +0000280 /* Do not include addrinfo.h for MSVC7 or greater. 'addrinfo' and
281 * EAI_* constants are defined in (the already included) ws2tcpip.h.
282 */
283#else
284# include "addrinfo.h"
285#endif
Jason Tishlerc246cb72004-08-09 13:25:59 +0000286
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000287#ifndef HAVE_INET_PTON
Christian Heimesb6150692007-11-15 23:37:07 +0000288#if !(defined(_MSC_VER) && _MSC_VER>1499)
289/* Don't redefine inet_pton in VS2008 */
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000290int inet_pton(int af, const char *src, void *dst);
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000291const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000292#endif
Christian Heimesb6150692007-11-15 23:37:07 +0000293#endif
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000294
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000295#ifdef __APPLE__
296/* On OS X, getaddrinfo returns no error indication of lookup
297 failure, so we must use the emulation instead of the libinfo
298 implementation. Unfortunately, performing an autoconf test
299 for this bug would require DNS access for the machine performing
300 the configuration, which is not acceptable. Therefore, we
301 determine the bug just by checking for __APPLE__. If this bug
302 gets ever fixed, perhaps checking for sys/version.h would be
303 appropriate, which is 10/0 on the system with the bug. */
Jack Jansen84262fb2002-07-02 14:40:42 +0000304#ifndef HAVE_GETNAMEINFO
305/* This bug seems to be fixed in Jaguar. Ths easiest way I could
306 Find to check for Jaguar is that it has getnameinfo(), which
307 older releases don't have */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000308#undef HAVE_GETADDRINFO
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000309#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000310
311#ifdef HAVE_INET_ATON
312#define USE_INET_ATON_WEAKLINK
313#endif
314
Jack Jansen84262fb2002-07-02 14:40:42 +0000315#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000316
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000317/* I know this is a bad practice, but it is the easiest... */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000318#if !defined(HAVE_GETADDRINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000319/* avoid clashes with the C library definition of the symbol. */
320#define getaddrinfo fake_getaddrinfo
321#define gai_strerror fake_gai_strerror
322#define freeaddrinfo fake_freeaddrinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000323#include "getaddrinfo.c"
324#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000325#if !defined(HAVE_GETNAMEINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000326#define getnameinfo fake_getnameinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000327#include "getnameinfo.c"
328#endif
329
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000330#ifdef MS_WINDOWS
331/* On Windows a socket is really a handle not an fd */
332static SOCKET
333dup_socket(SOCKET handle)
334{
335 HANDLE newhandle;
336
337 if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)handle,
338 GetCurrentProcess(), &newhandle,
339 0, FALSE, DUPLICATE_SAME_ACCESS))
340 {
341 WSASetLastError(GetLastError());
342 return INVALID_SOCKET;
343 }
344 return (SOCKET)newhandle;
345}
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000346#define SOCKETCLOSE closesocket
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000347#else
348/* On Unix we can use dup to duplicate the file descriptor of a socket*/
349#define dup_socket(fd) dup(fd)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000350#endif
351
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000352#ifdef MS_WIN32
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000353#define EAFNOSUPPORT WSAEAFNOSUPPORT
354#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000355#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000356
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000357#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000358#define SOCKETCLOSE soclose
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000359#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000360#endif
361
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000362#ifndef SOCKETCLOSE
363#define SOCKETCLOSE close
364#endif
365
Hye-Shik Chang81268602004-02-02 06:05:24 +0000366#if defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)
367#define USE_BLUETOOTH 1
368#if defined(__FreeBSD__)
369#define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP
370#define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM
Thomas Wouterscf297e42007-02-23 15:07:44 +0000371#define BTPROTO_HCI BLUETOOTH_PROTO_HCI
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000372#define SOL_HCI SOL_HCI_RAW
373#define HCI_FILTER SO_HCI_RAW_FILTER
Hye-Shik Chang81268602004-02-02 06:05:24 +0000374#define sockaddr_l2 sockaddr_l2cap
375#define sockaddr_rc sockaddr_rfcomm
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000376#define hci_dev hci_node
Hye-Shik Chang81268602004-02-02 06:05:24 +0000377#define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb)
378#define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000379#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000380#elif defined(__NetBSD__)
381#define sockaddr_l2 sockaddr_bt
382#define sockaddr_rc sockaddr_bt
Thomas Wouterscf297e42007-02-23 15:07:44 +0000383#define sockaddr_hci sockaddr_bt
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000384#define sockaddr_sco sockaddr_bt
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000385#define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb)
386#define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000387#define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000388#define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000389#else
Hye-Shik Chang81268602004-02-02 06:05:24 +0000390#define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)
391#define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000392#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000393#define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)
394#endif
395#endif
396
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000397#ifdef __VMS
398/* TCP/IP Services for VMS uses a maximum send/recv buffer length */
399#define SEGMENT_SIZE (32 * 1024 -1)
400#endif
401
Thomas Wouters89f507f2006-12-13 04:49:30 +0000402#define SAS2SA(x) ((struct sockaddr *)(x))
403
Martin v. Löwise9416172003-05-03 10:12:45 +0000404/*
405 * Constants for getnameinfo()
406 */
407#if !defined(NI_MAXHOST)
408#define NI_MAXHOST 1025
409#endif
410#if !defined(NI_MAXSERV)
411#define NI_MAXSERV 32
412#endif
413
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000414#ifndef INVALID_SOCKET /* MS defines this */
415#define INVALID_SOCKET (-1)
416#endif
417
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000418/* XXX There's a problem here: *static* functions are not supposed to have
419 a Py prefix (or use CapitalizedWords). Later... */
420
Guido van Rossum30a685f1991-06-27 15:51:29 +0000421/* Global variable holding the exception type for errors detected
422 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000423static PyObject *socket_error;
424static PyObject *socket_herror;
425static PyObject *socket_gaierror;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000426static PyObject *socket_timeout;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000427
Tim Peters643a7fc2002-02-17 04:13:21 +0000428/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000429 The sock_type variable contains pointers to various functions,
430 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000431 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000432static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000433
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000434#if defined(HAVE_POLL_H)
435#include <poll.h>
436#elif defined(HAVE_SYS_POLL_H)
437#include <sys/poll.h>
438#endif
439
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000440#ifdef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE
441/* Platform can select file descriptors beyond FD_SETSIZE */
442#define IS_SELECTABLE(s) 1
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000443#elif defined(HAVE_POLL)
444/* Instead of select(), we'll use poll() since poll() works on any fd. */
445#define IS_SELECTABLE(s) 1
446/* Can we call select() with this socket without a buffer overrun? */
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000447#else
448/* POSIX says selecting file descriptors beyond FD_SETSIZE
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000449 has undefined behaviour. If there's no timeout left, we don't have to
450 call select, so it's a safe, little white lie. */
451#define IS_SELECTABLE(s) ((s)->sock_fd < FD_SETSIZE || s->sock_timeout <= 0.0)
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000452#endif
Neal Norwitz082b2df2006-02-07 07:04:46 +0000453
454static PyObject*
455select_error(void)
456{
457 PyErr_SetString(socket_error, "unable to select on socket");
458 return NULL;
459}
460
Guido van Rossum30a685f1991-06-27 15:51:29 +0000461/* Convenience function to raise an error according to errno
462 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000463
Guido van Rossum73624e91994-10-10 17:59:00 +0000464static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000465set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000466{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000467#ifdef MS_WINDOWS
Mark Hammond46a733d2000-07-24 01:45:11 +0000468 int err_no = WSAGetLastError();
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000469 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
470 recognizes the error codes used by both GetLastError() and
471 WSAGetLastError */
472 if (err_no)
473 return PyErr_SetExcFromWindowsErr(socket_error, err_no);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000474#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000475
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000476#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000477 if (sock_errno() != NO_ERROR) {
478 APIRET rc;
479 ULONG msglen;
480 char outbuf[100];
481 int myerrorcode = sock_errno();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000482
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000483 /* Retrieve socket-related error message from MPTN.MSG file */
484 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
485 myerrorcode - SOCBASEERR + 26,
486 "mptn.msg",
487 &msglen);
488 if (rc == NO_ERROR) {
489 PyObject *v;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000490
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000491 /* OS/2 doesn't guarantee a terminator */
492 outbuf[msglen] = '\0';
493 if (strlen(outbuf) > 0) {
494 /* If non-empty msg, trim CRLF */
495 char *lastc = &outbuf[ strlen(outbuf)-1 ];
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000496 while (lastc > outbuf &&
497 isspace(Py_CHARMASK(*lastc))) {
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000498 /* Trim trailing whitespace (CRLF) */
499 *lastc-- = '\0';
500 }
501 }
502 v = Py_BuildValue("(is)", myerrorcode, outbuf);
503 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000504 PyErr_SetObject(socket_error, v);
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000505 Py_DECREF(v);
506 }
507 return NULL;
508 }
509 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000510#endif
511
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000512 return PyErr_SetFromErrno(socket_error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000513}
514
Guido van Rossum30a685f1991-06-27 15:51:29 +0000515
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000516static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000517set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000518{
519 PyObject *v;
520
521#ifdef HAVE_HSTRERROR
522 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
523#else
524 v = Py_BuildValue("(is)", h_error, "host not found");
525#endif
526 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000527 PyErr_SetObject(socket_herror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000528 Py_DECREF(v);
529 }
530
531 return NULL;
532}
533
534
535static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000536set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000537{
538 PyObject *v;
539
Martin v. Löwis272cb402002-03-01 08:31:07 +0000540#ifdef EAI_SYSTEM
541 /* EAI_SYSTEM is not available on Windows XP. */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000542 if (error == EAI_SYSTEM)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000543 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000544#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000545
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000546#ifdef HAVE_GAI_STRERROR
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000547 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000548#else
549 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
550#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000551 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000552 PyErr_SetObject(socket_gaierror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000553 Py_DECREF(v);
554 }
555
556 return NULL;
557}
558
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000559#ifdef __VMS
560/* Function to send in segments */
561static int
562sendsegmented(int sock_fd, char *buf, int len, int flags)
563{
564 int n = 0;
565 int remaining = len;
566
567 while (remaining > 0) {
568 unsigned int segment;
569
570 segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining);
571 n = send(sock_fd, buf, segment, flags);
572 if (n < 0) {
573 return n;
574 }
575 remaining -= segment;
576 buf += segment;
577 } /* end while */
578
579 return len;
580}
581#endif
582
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000583/* Function to perform the setting of socket blocking mode
584 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000585static int
586internal_setblocking(PySocketSockObject *s, int block)
587{
Guido van Rossum67f7a382002-06-06 21:08:16 +0000588#ifndef MS_WINDOWS
589 int delay_flag;
590#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +0000591
592 Py_BEGIN_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000593#ifndef MS_WINDOWS
594#if defined(PYOS_OS2) && !defined(PYCC_GCC)
595 block = !block;
596 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000597#elif defined(__VMS)
598 block = !block;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000599 ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);
600#else /* !PYOS_OS2 && !__VMS */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000601 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
602 if (block)
Neal Norwitz39d22e52002-11-02 19:55:21 +0000603 delay_flag &= (~O_NONBLOCK);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000604 else
Neal Norwitz39d22e52002-11-02 19:55:21 +0000605 delay_flag |= O_NONBLOCK;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000606 fcntl(s->sock_fd, F_SETFL, delay_flag);
607#endif /* !PYOS_OS2 */
608#else /* MS_WINDOWS */
609 block = !block;
610 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
611#endif /* MS_WINDOWS */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000612 Py_END_ALLOW_THREADS
613
614 /* Since these don't return anything */
615 return 1;
616}
617
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000618/* Do a select()/poll() on the socket, if necessary (sock_timeout > 0).
Guido van Rossum11ba0942002-06-13 15:07:44 +0000619 The argument writing indicates the direction.
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000620 This does not raise an exception; we'll let our caller do that
621 after they've reacquired the interpreter lock.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000622 Returns 1 on timeout, -1 on error, 0 otherwise. */
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000623static int
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000624internal_select(PySocketSockObject *s, int writing)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000625{
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000626 int n;
Guido van Rossum11ba0942002-06-13 15:07:44 +0000627
Guido van Rossumad654902002-07-19 12:44:59 +0000628 /* Nothing to do unless we're in timeout mode (not non-blocking) */
Guido van Rossum11ba0942002-06-13 15:07:44 +0000629 if (s->sock_timeout <= 0.0)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000630 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000631
Guido van Rossumad654902002-07-19 12:44:59 +0000632 /* Guard against closed socket */
633 if (s->sock_fd < 0)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000634 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000635
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000636 /* Prefer poll, if available, since you can poll() any fd
637 * which can't be done with select(). */
638#ifdef HAVE_POLL
639 {
640 struct pollfd pollfd;
641 int timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000642
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000643 pollfd.fd = s->sock_fd;
644 pollfd.events = writing ? POLLOUT : POLLIN;
645
646 /* s->sock_timeout is in seconds, timeout in ms */
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000647 timeout = (int)(s->sock_timeout * 1000 + 0.5);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000648 n = poll(&pollfd, 1, timeout);
649 }
650#else
651 {
652 /* Construct the arguments to select */
653 fd_set fds;
654 struct timeval tv;
655 tv.tv_sec = (int)s->sock_timeout;
656 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
657 FD_ZERO(&fds);
658 FD_SET(s->sock_fd, &fds);
659
660 /* See if the socket is ready */
661 if (writing)
662 n = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
663 else
664 n = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
665 }
666#endif
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000667
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000668 if (n < 0)
669 return -1;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000670 if (n == 0)
671 return 1;
672 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000673}
674
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000675/* Initialize a new socket object. */
676
Tim Petersa12b4cf2002-07-18 22:38:44 +0000677static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000678
Mark Hammond62b1ab12002-07-23 06:31:15 +0000679PyMODINIT_FUNC
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000680init_sockobject(PySocketSockObject *s,
681 SOCKET_T fd, int family, int type, int proto)
682{
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000683 s->sock_fd = fd;
684 s->sock_family = family;
685 s->sock_type = type;
686 s->sock_proto = proto;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000687 s->sock_timeout = defaulttimeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000688
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000689 s->errorhandler = &set_error;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000690
691 if (defaulttimeout >= 0.0)
692 internal_setblocking(s, 0);
693
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000694}
695
696
Guido van Rossum30a685f1991-06-27 15:51:29 +0000697/* Create a new socket object.
698 This just creates the object and initializes it.
699 If the creation fails, return NULL and set an exception (implicit
700 in NEWOBJ()). */
701
Guido van Rossum73624e91994-10-10 17:59:00 +0000702static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000703new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000704{
Guido van Rossum73624e91994-10-10 17:59:00 +0000705 PySocketSockObject *s;
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000706 s = (PySocketSockObject *)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000707 PyType_GenericNew(&sock_type, NULL, NULL);
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000708 if (s != NULL)
709 init_sockobject(s, fd, family, type, proto);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000710 return s;
711}
712
Guido van Rossum30a685f1991-06-27 15:51:29 +0000713
Guido van Rossum48a680c2001-03-02 06:34:14 +0000714/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000715 thread to be in gethostbyname or getaddrinfo */
716#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
717PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000718#endif
719
720
Guido van Rossum30a685f1991-06-27 15:51:29 +0000721/* Convert a string specifying a host name or one of a few symbolic
722 names to a numeric IP address. This usually calls gethostbyname()
723 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000724 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000725 an error occurred; then an exception is raised. */
726
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000727static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000728setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000729{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000730 struct addrinfo hints, *res;
731 int error;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000732 int d1, d2, d3, d4;
733 char ch;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000734
Guido van Rossuma376cc51996-12-05 23:43:35 +0000735 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
Guido van Rossum30a685f1991-06-27 15:51:29 +0000736 if (name[0] == '\0') {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000737 int siz;
738 memset(&hints, 0, sizeof(hints));
739 hints.ai_family = af;
740 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
741 hints.ai_flags = AI_PASSIVE;
Just van Rossum1040d2c2003-05-09 07:53:18 +0000742 Py_BEGIN_ALLOW_THREADS
743 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000744 error = getaddrinfo(NULL, "0", &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +0000745 Py_END_ALLOW_THREADS
746 /* We assume that those thread-unsafe getaddrinfo() versions
747 *are* safe regarding their return value, ie. that a
748 subsequent call to getaddrinfo() does not destroy the
749 outcome of the first call. */
750 RELEASE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000751 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000752 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000753 return -1;
754 }
755 switch (res->ai_family) {
756 case AF_INET:
757 siz = 4;
758 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000759#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000760 case AF_INET6:
761 siz = 16;
762 break;
763#endif
764 default:
765 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000766 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000767 "unsupported address family");
768 return -1;
769 }
770 if (res->ai_next) {
Martin v. Löwisf0b11d22001-11-07 08:31:03 +0000771 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000772 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000773 "wildcard resolved to multiple address");
774 return -1;
775 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000776 if (res->ai_addrlen < addr_ret_size)
777 addr_ret_size = res->ai_addrlen;
778 memcpy(addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000779 freeaddrinfo(res);
780 return siz;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000781 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000782 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000783 struct sockaddr_in *sin;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +0000784 if (af != AF_INET && af != AF_UNSPEC) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000785 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000786 "address family mismatched");
787 return -1;
788 }
789 sin = (struct sockaddr_in *)addr_ret;
790 memset((void *) sin, '\0', sizeof(*sin));
791 sin->sin_family = AF_INET;
792#ifdef HAVE_SOCKADDR_SA_LEN
793 sin->sin_len = sizeof(*sin);
794#endif
795 sin->sin_addr.s_addr = INADDR_BROADCAST;
796 return sizeof(sin->sin_addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000797 }
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000798 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
799 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
800 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
801 struct sockaddr_in *sin;
802 sin = (struct sockaddr_in *)addr_ret;
803 sin->sin_addr.s_addr = htonl(
804 ((long) d1 << 24) | ((long) d2 << 16) |
805 ((long) d3 << 8) | ((long) d4 << 0));
806 sin->sin_family = AF_INET;
807#ifdef HAVE_SOCKADDR_SA_LEN
808 sin->sin_len = sizeof(*sin);
809#endif
810 return 4;
811 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000812 memset(&hints, 0, sizeof(hints));
813 hints.ai_family = af;
Just van Rossum1040d2c2003-05-09 07:53:18 +0000814 Py_BEGIN_ALLOW_THREADS
815 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000816 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000817#if defined(__digital__) && defined(__unix__)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000818 if (error == EAI_NONAME && af == AF_UNSPEC) {
819 /* On Tru64 V5.1, numeric-to-addr conversion fails
820 if no address family is given. Assume IPv4 for now.*/
821 hints.ai_family = AF_INET;
822 error = getaddrinfo(name, NULL, &hints, &res);
823 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000824#endif
Just van Rossum1040d2c2003-05-09 07:53:18 +0000825 Py_END_ALLOW_THREADS
826 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000827 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000828 set_gaierror(error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000829 return -1;
830 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000831 if (res->ai_addrlen < addr_ret_size)
832 addr_ret_size = res->ai_addrlen;
833 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000834 freeaddrinfo(res);
835 switch (addr_ret->sa_family) {
836 case AF_INET:
837 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000838#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000839 case AF_INET6:
840 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000841#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000842 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000843 PyErr_SetString(socket_error, "unknown address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000844 return -1;
845 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000846}
847
Guido van Rossum30a685f1991-06-27 15:51:29 +0000848
Guido van Rossum30a685f1991-06-27 15:51:29 +0000849/* Create a string object representing an IP address.
850 This is always a string of the form 'dd.dd.dd.dd' (with variable
851 size numbers). */
852
Guido van Rossum73624e91994-10-10 17:59:00 +0000853static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000854makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000855{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000856 char buf[NI_MAXHOST];
857 int error;
858
859 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
860 NI_NUMERICHOST);
861 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000862 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000863 return NULL;
864 }
Guido van Rossum32c4ac02007-08-15 03:56:40 +0000865 return PyUnicode_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000866}
867
868
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000869#ifdef USE_BLUETOOTH
870/* Convert a string representation of a Bluetooth address into a numeric
871 address. Returns the length (6), or raises an exception and returns -1 if
872 an error occurred. */
873
874static int
875setbdaddr(char *name, bdaddr_t *bdaddr)
876{
877 unsigned int b0, b1, b2, b3, b4, b5;
878 char ch;
879 int n;
880
881 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
882 &b5, &b4, &b3, &b2, &b1, &b0, &ch);
883 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
884 bdaddr->b[0] = b0;
885 bdaddr->b[1] = b1;
886 bdaddr->b[2] = b2;
887 bdaddr->b[3] = b3;
888 bdaddr->b[4] = b4;
889 bdaddr->b[5] = b5;
890 return 6;
891 } else {
892 PyErr_SetString(socket_error, "bad bluetooth address");
893 return -1;
894 }
895}
896
897/* Create a string representation of the Bluetooth address. This is always a
898 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
899 value (zero padded if necessary). */
900
901static PyObject *
902makebdaddr(bdaddr_t *bdaddr)
903{
904 char buf[(6 * 2) + 5 + 1];
905
906 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
907 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
908 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
Guido van Rossum32c4ac02007-08-15 03:56:40 +0000909 return PyUnicode_FromString(buf);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000910}
911#endif
912
913
Guido van Rossum30a685f1991-06-27 15:51:29 +0000914/* Create an object representing the given socket address,
915 suitable for passing it back to bind(), connect() etc.
916 The family field of the sockaddr structure is inspected
917 to determine what kind of address it really is. */
918
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000919/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000920static PyObject *
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000921makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000922{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000923 if (addrlen == 0) {
924 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +0000925 Py_INCREF(Py_None);
926 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000927 }
928
Guido van Rossum30a685f1991-06-27 15:51:29 +0000929 switch (addr->sa_family) {
930
931 case AF_INET:
932 {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000933 struct sockaddr_in *a;
934 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
Barry Warsaw752300b1997-01-03 17:18:10 +0000935 PyObject *ret = NULL;
936 if (addrobj) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000937 a = (struct sockaddr_in *)addr;
Barry Warsaw752300b1997-01-03 17:18:10 +0000938 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
939 Py_DECREF(addrobj);
940 }
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000941 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000942 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000943
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +0000944#if defined(AF_UNIX)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000945 case AF_UNIX:
946 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000947 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000948#ifdef linux
949 if (a->sun_path[0] == 0) { /* Linux abstract namespace */
950 addrlen -= (sizeof(*a) - sizeof(a->sun_path));
Guido van Rossum32c4ac02007-08-15 03:56:40 +0000951 return PyBytes_FromStringAndSize(a->sun_path, addrlen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000952 }
953 else
954#endif /* linux */
955 {
956 /* regular NULL-terminated string */
Guido van Rossum32c4ac02007-08-15 03:56:40 +0000957 return PyUnicode_FromString(a->sun_path);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000958 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000959 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000960#endif /* AF_UNIX */
961
Martin v. Löwis11017b12006-01-14 18:12:57 +0000962#if defined(AF_NETLINK)
963 case AF_NETLINK:
964 {
965 struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
Martin v. Löwisd0560052006-01-15 07:49:20 +0000966 return Py_BuildValue("II", a->nl_pid, a->nl_groups);
Martin v. Löwis11017b12006-01-14 18:12:57 +0000967 }
968#endif /* AF_NETLINK */
969
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000970#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000971 case AF_INET6:
972 {
973 struct sockaddr_in6 *a;
974 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
975 PyObject *ret = NULL;
976 if (addrobj) {
977 a = (struct sockaddr_in6 *)addr;
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000978 ret = Py_BuildValue("Oiii",
979 addrobj,
980 ntohs(a->sin6_port),
981 a->sin6_flowinfo,
982 a->sin6_scope_id);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000983 Py_DECREF(addrobj);
984 }
985 return ret;
986 }
Jeremy Hylton22308652001-02-02 03:23:09 +0000987#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +0000988
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000989#ifdef USE_BLUETOOTH
990 case AF_BLUETOOTH:
991 switch (proto) {
992
993 case BTPROTO_L2CAP:
994 {
995 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
996 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
997 PyObject *ret = NULL;
998 if (addrobj) {
999 ret = Py_BuildValue("Oi",
1000 addrobj,
1001 _BT_L2_MEMB(a, psm));
1002 Py_DECREF(addrobj);
1003 }
1004 return ret;
1005 }
1006
1007 case BTPROTO_RFCOMM:
1008 {
1009 struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
1010 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
1011 PyObject *ret = NULL;
1012 if (addrobj) {
1013 ret = Py_BuildValue("Oi",
1014 addrobj,
1015 _BT_RC_MEMB(a, channel));
1016 Py_DECREF(addrobj);
1017 }
1018 return ret;
1019 }
1020
Thomas Wouterscf297e42007-02-23 15:07:44 +00001021 case BTPROTO_HCI:
1022 {
1023 struct sockaddr_hci *a = (struct sockaddr_hci *) addr;
1024 PyObject *ret = NULL;
1025 ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
1026 return ret;
1027 }
1028
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001029#if !defined(__FreeBSD__)
1030 case BTPROTO_SCO:
1031 {
1032 struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
1033 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
1034 }
1035#endif
1036
1037 }
1038#endif
1039
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001040#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001041 case AF_PACKET:
1042 {
1043 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
1044 char *ifname = "";
1045 struct ifreq ifr;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001046 /* need to look up interface name give index */
1047 if (a->sll_ifindex) {
Jeremy Hylton22308652001-02-02 03:23:09 +00001048 ifr.ifr_ifindex = a->sll_ifindex;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001049 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
Jeremy Hylton22308652001-02-02 03:23:09 +00001050 ifname = ifr.ifr_name;
Jeremy Hylton22308652001-02-02 03:23:09 +00001051 }
Guido van Rossum32c4ac02007-08-15 03:56:40 +00001052 return Py_BuildValue("shbhy#",
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001053 ifname,
1054 ntohs(a->sll_protocol),
1055 a->sll_pkttype,
1056 a->sll_hatype,
1057 a->sll_addr,
1058 a->sll_halen);
Jeremy Hylton22308652001-02-02 03:23:09 +00001059 }
1060#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001061
Guido van Rossum30a685f1991-06-27 15:51:29 +00001062 /* More cases here... */
1063
1064 default:
Guido van Rossumaa948df1997-05-07 17:41:48 +00001065 /* If we don't know the address family, don't raise an
Guido van Rossum9ce8e382007-08-09 21:06:37 +00001066 exception -- return it as an (int, bytes) tuple. */
1067 return Py_BuildValue("iy#",
Guido van Rossumaa948df1997-05-07 17:41:48 +00001068 addr->sa_family,
1069 addr->sa_data,
1070 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001071
Guido van Rossum30a685f1991-06-27 15:51:29 +00001072 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001073}
1074
Guido van Rossum30a685f1991-06-27 15:51:29 +00001075
1076/* Parse a socket address argument according to the socket object's
1077 address family. Return 1 if the address was in the proper format,
1078 0 of not. The address is returned through addr_ret, its length
1079 through len_ret. */
1080
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001081static int
Guido van Rossum48a680c2001-03-02 06:34:14 +00001082getsockaddrarg(PySocketSockObject *s, PyObject *args,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001083 struct sockaddr *addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001084{
Guido van Rossum30a685f1991-06-27 15:51:29 +00001085 switch (s->sock_family) {
1086
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001087#if defined(AF_UNIX)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001088 case AF_UNIX:
1089 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001090 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001091 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001092 int len;
Guido van Rossume22905a2007-08-27 23:09:25 +00001093 if (!PyArg_Parse(args, "s#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001094 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001095
1096 addr = (struct sockaddr_un*)addr_ret;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001097#ifdef linux
1098 if (len > 0 && path[0] == 0) {
1099 /* Linux abstract namespace extension */
1100 if (len > sizeof addr->sun_path) {
1101 PyErr_SetString(socket_error,
1102 "AF_UNIX path too long");
1103 return 0;
1104 }
1105 }
1106 else
1107#endif /* linux */
1108 {
1109 /* regular NULL-terminated string */
1110 if (len >= sizeof addr->sun_path) {
1111 PyErr_SetString(socket_error,
1112 "AF_UNIX path too long");
1113 return 0;
1114 }
1115 addr->sun_path[len] = 0;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001116 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001117 addr->sun_family = s->sock_family;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001118 memcpy(addr->sun_path, path, len);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001119#if defined(PYOS_OS2)
1120 *len_ret = sizeof(*addr);
1121#else
Guido van Rossum65af28a1996-06-11 18:36:33 +00001122 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001123#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001124 return 1;
1125 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001126#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001127
Martin v. Löwis11017b12006-01-14 18:12:57 +00001128#if defined(AF_NETLINK)
1129 case AF_NETLINK:
1130 {
1131 struct sockaddr_nl* addr;
1132 int pid, groups;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001133 addr = (struct sockaddr_nl *)addr_ret;
Martin v. Löwis11017b12006-01-14 18:12:57 +00001134 if (!PyTuple_Check(args)) {
1135 PyErr_Format(
1136 PyExc_TypeError,
1137 "getsockaddrarg: "
1138 "AF_NETLINK address must be tuple, not %.500s",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001139 Py_Type(args)->tp_name);
Martin v. Löwis11017b12006-01-14 18:12:57 +00001140 return 0;
1141 }
1142 if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
1143 return 0;
1144 addr->nl_family = AF_NETLINK;
1145 addr->nl_pid = pid;
1146 addr->nl_groups = groups;
Martin v. Löwis11017b12006-01-14 18:12:57 +00001147 *len_ret = sizeof(*addr);
1148 return 1;
1149 }
1150#endif
1151
Guido van Rossum30a685f1991-06-27 15:51:29 +00001152 case AF_INET:
1153 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001154 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001155 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001156 int port, result;
Guido van Rossume4dad902000-12-01 13:13:11 +00001157 if (!PyTuple_Check(args)) {
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001158 PyErr_Format(
1159 PyExc_TypeError,
1160 "getsockaddrarg: "
1161 "AF_INET address must be tuple, not %.500s",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001162 Py_Type(args)->tp_name);
Guido van Rossume4dad902000-12-01 13:13:11 +00001163 return 0;
1164 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001165 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
Martin v. Löwis2548c732003-04-18 10:39:54 +00001166 "idna", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001167 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001168 addr=(struct sockaddr_in*)addr_ret;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001169 result = setipaddr(host, (struct sockaddr *)addr,
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001170 sizeof(*addr), AF_INET);
1171 PyMem_Free(host);
1172 if (result < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001173 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001174 addr->sin_family = AF_INET;
Guido van Rossum644a12b1997-04-09 19:24:53 +00001175 addr->sin_port = htons((short)port);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001176 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001177 return 1;
1178 }
1179
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001180#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001181 case AF_INET6:
1182 {
1183 struct sockaddr_in6* addr;
1184 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001185 int port, flowinfo, scope_id, result;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001186 flowinfo = scope_id = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001187 if (!PyTuple_Check(args)) {
1188 PyErr_Format(
1189 PyExc_TypeError,
1190 "getsockaddrarg: "
1191 "AF_INET6 address must be tuple, not %.500s",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001192 Py_Type(args)->tp_name);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001193 return 0;
1194 }
1195 if (!PyArg_ParseTuple(args, "eti|ii",
Martin v. Löwis2548c732003-04-18 10:39:54 +00001196 "idna", &host, &port, &flowinfo,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001197 &scope_id)) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001198 return 0;
1199 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001200 addr = (struct sockaddr_in6*)addr_ret;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001201 result = setipaddr(host, (struct sockaddr *)addr,
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001202 sizeof(*addr), AF_INET6);
1203 PyMem_Free(host);
1204 if (result < 0)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001205 return 0;
1206 addr->sin6_family = s->sock_family;
1207 addr->sin6_port = htons((short)port);
1208 addr->sin6_flowinfo = flowinfo;
1209 addr->sin6_scope_id = scope_id;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001210 *len_ret = sizeof *addr;
1211 return 1;
1212 }
1213#endif
1214
Hye-Shik Chang81268602004-02-02 06:05:24 +00001215#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00001216 case AF_BLUETOOTH:
1217 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001218 switch (s->sock_proto) {
1219 case BTPROTO_L2CAP:
Martin v. Löwis12af0482004-01-31 12:34:17 +00001220 {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001221 struct sockaddr_l2 *addr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001222 char *straddr;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001223
Thomas Wouters89f507f2006-12-13 04:49:30 +00001224 addr = (struct sockaddr_l2 *)addr_ret;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001225 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
1226 if (!PyArg_ParseTuple(args, "si", &straddr,
1227 &_BT_L2_MEMB(addr, psm))) {
1228 PyErr_SetString(socket_error, "getsockaddrarg: "
1229 "wrong format");
Martin v. Löwis12af0482004-01-31 12:34:17 +00001230 return 0;
1231 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001232 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
1233 return 0;
1234
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001235 *len_ret = sizeof *addr;
1236 return 1;
1237 }
1238 case BTPROTO_RFCOMM:
1239 {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001240 struct sockaddr_rc *addr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001241 char *straddr;
1242
Thomas Wouters89f507f2006-12-13 04:49:30 +00001243 addr = (struct sockaddr_rc *)addr_ret;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001244 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
1245 if (!PyArg_ParseTuple(args, "si", &straddr,
1246 &_BT_RC_MEMB(addr, channel))) {
1247 PyErr_SetString(socket_error, "getsockaddrarg: "
1248 "wrong format");
1249 return 0;
1250 }
1251 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
1252 return 0;
1253
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001254 *len_ret = sizeof *addr;
1255 return 1;
1256 }
Thomas Wouterscf297e42007-02-23 15:07:44 +00001257 case BTPROTO_HCI:
1258 {
1259 struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret;
1260 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
1261 if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) {
1262 PyErr_SetString(socket_error, "getsockaddrarg: "
1263 "wrong format");
1264 return 0;
1265 }
1266 *len_ret = sizeof *addr;
1267 return 1;
1268 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001269#if !defined(__FreeBSD__)
1270 case BTPROTO_SCO:
1271 {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001272 struct sockaddr_sco *addr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001273 char *straddr;
1274
Thomas Wouters89f507f2006-12-13 04:49:30 +00001275 addr = (struct sockaddr_sco *)addr_ret;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001276 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
Guido van Rossum32c4ac02007-08-15 03:56:40 +00001277 if (!PyBytes_Check(args)) {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001278 PyErr_SetString(socket_error, "getsockaddrarg: "
1279 "wrong format");
1280 return 0;
1281 }
Guido van Rossum32c4ac02007-08-15 03:56:40 +00001282 straddr = PyBytes_AS_STRING(args);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001283 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
1284 return 0;
1285
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001286 *len_ret = sizeof *addr;
1287 return 1;
1288 }
1289#endif
1290 default:
1291 PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol");
1292 return 0;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001293 }
1294 }
1295#endif
1296
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001297#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001298 case AF_PACKET:
1299 {
1300 struct sockaddr_ll* addr;
1301 struct ifreq ifr;
1302 char *interfaceName;
1303 int protoNumber;
1304 int hatype = 0;
1305 int pkttype = 0;
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001306 char *haddr = NULL;
1307 unsigned int halen = 0;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001308
Thomas Wouters477c8d52006-05-27 19:21:47 +00001309 if (!PyTuple_Check(args)) {
1310 PyErr_Format(
1311 PyExc_TypeError,
1312 "getsockaddrarg: "
1313 "AF_PACKET address must be tuple, not %.500s",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001314 Py_Type(args)->tp_name);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001315 return 0;
1316 }
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001317 if (!PyArg_ParseTuple(args, "si|iis#", &interfaceName,
1318 &protoNumber, &pkttype, &hatype,
1319 &haddr, &halen))
Jeremy Hylton22308652001-02-02 03:23:09 +00001320 return 0;
1321 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1322 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001323 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001324 s->errorhandler();
Jeremy Hylton22308652001-02-02 03:23:09 +00001325 return 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001326 }
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001327 if (halen > 8) {
1328 PyErr_SetString(PyExc_ValueError,
1329 "Hardware address must be 8 bytes or less");
1330 return 0;
1331 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001332 addr = (struct sockaddr_ll*)addr_ret;
1333 addr->sll_family = AF_PACKET;
1334 addr->sll_protocol = htons((short)protoNumber);
1335 addr->sll_ifindex = ifr.ifr_ifindex;
1336 addr->sll_pkttype = pkttype;
1337 addr->sll_hatype = hatype;
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001338 if (halen != 0) {
1339 memcpy(&addr->sll_addr, haddr, halen);
1340 }
1341 addr->sll_halen = halen;
Jeremy Hylton22308652001-02-02 03:23:09 +00001342 *len_ret = sizeof *addr;
1343 return 1;
1344 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001345#endif
1346
Guido van Rossum30a685f1991-06-27 15:51:29 +00001347 /* More cases here... */
1348
1349 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001350 PyErr_SetString(socket_error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001351 return 0;
1352
1353 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001354}
1355
Guido van Rossum30a685f1991-06-27 15:51:29 +00001356
Guido van Rossum48a680c2001-03-02 06:34:14 +00001357/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00001358 Return 1 if the family is known, 0 otherwise. The length is returned
1359 through len_ret. */
1360
1361static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00001362getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001363{
1364 switch (s->sock_family) {
1365
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001366#if defined(AF_UNIX)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001367 case AF_UNIX:
1368 {
1369 *len_ret = sizeof (struct sockaddr_un);
1370 return 1;
1371 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001372#endif /* AF_UNIX */
Martin v. Löwis11017b12006-01-14 18:12:57 +00001373#if defined(AF_NETLINK)
1374 case AF_NETLINK:
1375 {
1376 *len_ret = sizeof (struct sockaddr_nl);
1377 return 1;
1378 }
1379#endif
Guido van Rossum710e1df1992-06-12 10:39:36 +00001380
1381 case AF_INET:
1382 {
1383 *len_ret = sizeof (struct sockaddr_in);
1384 return 1;
1385 }
1386
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001387#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001388 case AF_INET6:
1389 {
1390 *len_ret = sizeof (struct sockaddr_in6);
1391 return 1;
1392 }
1393#endif
1394
Hye-Shik Chang81268602004-02-02 06:05:24 +00001395#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00001396 case AF_BLUETOOTH:
1397 {
1398 switch(s->sock_proto)
1399 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001400
1401 case BTPROTO_L2CAP:
1402 *len_ret = sizeof (struct sockaddr_l2);
1403 return 1;
1404 case BTPROTO_RFCOMM:
1405 *len_ret = sizeof (struct sockaddr_rc);
1406 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001407 case BTPROTO_HCI:
1408 *len_ret = sizeof (struct sockaddr_hci);
1409 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001410#if !defined(__FreeBSD__)
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001411 case BTPROTO_SCO:
1412 *len_ret = sizeof (struct sockaddr_sco);
1413 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001414#endif
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001415 default:
1416 PyErr_SetString(socket_error, "getsockaddrlen: "
1417 "unknown BT protocol");
1418 return 0;
1419
Martin v. Löwis12af0482004-01-31 12:34:17 +00001420 }
1421 }
1422#endif
1423
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001424#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001425 case AF_PACKET:
1426 {
1427 *len_ret = sizeof (struct sockaddr_ll);
1428 return 1;
1429 }
1430#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001431
Guido van Rossum710e1df1992-06-12 10:39:36 +00001432 /* More cases here... */
1433
1434 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001435 PyErr_SetString(socket_error, "getsockaddrlen: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +00001436 return 0;
1437
1438 }
1439}
1440
1441
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001442/* s._accept() -> (fd, address) */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001443
Guido van Rossum73624e91994-10-10 17:59:00 +00001444static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001445sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001446{
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001447 sock_addr_t addrbuf;
Guido van Rossum7d0a8262007-05-21 23:13:11 +00001448 SOCKET_T newfd = INVALID_SOCKET;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001449 socklen_t addrlen;
Barry Warsaw752300b1997-01-03 17:18:10 +00001450 PyObject *sock = NULL;
1451 PyObject *addr = NULL;
1452 PyObject *res = NULL;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001453 int timeout;
Barry Warsaw752300b1997-01-03 17:18:10 +00001454
Guido van Rossum710e1df1992-06-12 10:39:36 +00001455 if (!getsockaddrlen(s, &addrlen))
1456 return NULL;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001457 memset(&addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001458
Neal Norwitz082b2df2006-02-07 07:04:46 +00001459 if (!IS_SELECTABLE(s))
1460 return select_error();
1461
Guido van Rossum73624e91994-10-10 17:59:00 +00001462 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001463 timeout = internal_select(s, 0);
1464 if (!timeout)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001465 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001466 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001467
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001468 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001469 PyErr_SetString(socket_timeout, "timed out");
1470 return NULL;
1471 }
1472
Fred Drakea04eaad2000-06-30 02:46:07 +00001473 if (newfd == INVALID_SOCKET)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001474 return s->errorhandler();
Barry Warsaw752300b1997-01-03 17:18:10 +00001475
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001476 sock = PyLong_FromSocket_t(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +00001477 if (sock == NULL) {
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001478 SOCKETCLOSE(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +00001479 goto finally;
1480 }
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001481
Thomas Wouters89f507f2006-12-13 04:49:30 +00001482 addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001483 addrlen, s->sock_proto);
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001484 if (addr == NULL)
Barry Warsaw752300b1997-01-03 17:18:10 +00001485 goto finally;
1486
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001487 res = PyTuple_Pack(2, sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001488
Guido van Rossum67f7a382002-06-06 21:08:16 +00001489finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001490 Py_XDECREF(sock);
1491 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001492 return res;
1493}
1494
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001495PyDoc_STRVAR(accept_doc,
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001496"_accept() -> (integer, address info)\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001497\n\
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001498Wait for an incoming connection. Return a new socket file descriptor\n\
1499representing the connection, and the address of the client.\n\
1500For IP sockets, the address info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001501
Guido van Rossum11ba0942002-06-13 15:07:44 +00001502/* s.setblocking(flag) method. Argument:
1503 False -- non-blocking mode; same as settimeout(0)
1504 True -- blocking mode; same as settimeout(None)
1505*/
Guido van Rossume4485b01994-09-07 14:32:49 +00001506
Guido van Rossum73624e91994-10-10 17:59:00 +00001507static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001508sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00001509{
1510 int block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001511
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001512 block = PyInt_AsLong(arg);
1513 if (block == -1 && PyErr_Occurred())
Guido van Rossume4485b01994-09-07 14:32:49 +00001514 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001515
Guido van Rossum11ba0942002-06-13 15:07:44 +00001516 s->sock_timeout = block ? -1.0 : 0.0;
Guido van Rossume8008f02002-06-07 03:36:20 +00001517 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00001518
Guido van Rossum73624e91994-10-10 17:59:00 +00001519 Py_INCREF(Py_None);
1520 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00001521}
Guido van Rossume4485b01994-09-07 14:32:49 +00001522
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001523PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001524"setblocking(flag)\n\
1525\n\
1526Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001527setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001528setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001529
Guido van Rossum11ba0942002-06-13 15:07:44 +00001530/* s.settimeout(timeout) method. Argument:
1531 None -- no timeout, blocking mode; same as setblocking(True)
1532 0.0 -- non-blocking mode; same as setblocking(False)
1533 > 0 -- timeout mode; operations time out after timeout seconds
1534 < 0 -- illegal; raises an exception
1535*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00001536static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001537sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001538{
Guido van Rossum11ba0942002-06-13 15:07:44 +00001539 double timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001540
1541 if (arg == Py_None)
Guido van Rossum11ba0942002-06-13 15:07:44 +00001542 timeout = -1.0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001543 else {
Guido van Rossum11ba0942002-06-13 15:07:44 +00001544 timeout = PyFloat_AsDouble(arg);
1545 if (timeout < 0.0) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00001546 if (!PyErr_Occurred())
1547 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001548 "Timeout value out of range");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001549 return NULL;
1550 }
1551 }
1552
Guido van Rossum11ba0942002-06-13 15:07:44 +00001553 s->sock_timeout = timeout;
1554 internal_setblocking(s, timeout < 0.0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001555
1556 Py_INCREF(Py_None);
1557 return Py_None;
1558}
1559
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001560PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001561"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001562\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001563Set a timeout on socket operations. 'timeout' can be a float,\n\
1564giving in seconds, or None. Setting a timeout of None disables\n\
1565the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001566Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001567
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001568/* s.gettimeout() method.
1569 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001570static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001571sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001572{
1573 if (s->sock_timeout < 0.0) {
1574 Py_INCREF(Py_None);
1575 return Py_None;
1576 }
1577 else
1578 return PyFloat_FromDouble(s->sock_timeout);
1579}
1580
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001581PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001582"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001583\n\
1584Returns the timeout in floating seconds associated with socket \n\
1585operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001586operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00001587
Guido van Rossumaee08791992-09-08 09:05:33 +00001588/* s.setsockopt() method.
1589 With an integer third argument, sets an integer option.
1590 With a string third argument, sets an option from a buffer;
1591 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001592
Guido van Rossum73624e91994-10-10 17:59:00 +00001593static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001594sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001595{
1596 int level;
1597 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001598 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +00001599 char *buf;
1600 int buflen;
1601 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001602
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001603 if (PyArg_ParseTuple(args, "iii:setsockopt",
1604 &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001605 buf = (char *) &flag;
1606 buflen = sizeof flag;
1607 }
1608 else {
Guido van Rossum73624e91994-10-10 17:59:00 +00001609 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001610 if (!PyArg_ParseTuple(args, "iis#:setsockopt",
1611 &level, &optname, &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +00001612 return NULL;
1613 }
Thomas Wouters334fb892000-07-25 12:56:38 +00001614 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001615 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001616 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001617 Py_INCREF(Py_None);
1618 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001619}
1620
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001621PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001622"setsockopt(level, option, value)\n\
1623\n\
1624Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001625The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001626
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001627
Guido van Rossumaee08791992-09-08 09:05:33 +00001628/* s.getsockopt() method.
1629 With two arguments, retrieves an integer option.
1630 With a third integer argument, retrieves a string buffer of that size;
1631 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001632
Guido van Rossum73624e91994-10-10 17:59:00 +00001633static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001634sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001635{
1636 int level;
1637 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001638 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00001639 PyObject *buf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001640 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001641
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001642 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
1643 &level, &optname, &buflen))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001644 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001645
Guido van Rossumbe32c891996-06-20 16:25:29 +00001646 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001647 int flag = 0;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001648 socklen_t flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001649 res = getsockopt(s->sock_fd, level, optname,
Thomas Wouters334fb892000-07-25 12:56:38 +00001650 (void *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +00001651 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001652 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001653 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +00001654 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001655#ifdef __VMS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001656 /* socklen_t is unsigned so no negative test is needed,
1657 test buflen == 0 is previously done */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001658 if (buflen > 1024) {
1659#else
Guido van Rossumaee08791992-09-08 09:05:33 +00001660 if (buflen <= 0 || buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001661#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001662 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +00001663 "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +00001664 return NULL;
1665 }
Guido van Rossum7d0a8262007-05-21 23:13:11 +00001666 buf = PyBytes_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001667 if (buf == NULL)
1668 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001669 res = getsockopt(s->sock_fd, level, optname,
Guido van Rossum7d0a8262007-05-21 23:13:11 +00001670 (void *)PyBytes_AS_STRING(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001671 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001672 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001673 return s->errorhandler();
Guido van Rossumaee08791992-09-08 09:05:33 +00001674 }
Guido van Rossum7d0a8262007-05-21 23:13:11 +00001675 if (PyBytes_Resize(buf, buflen) < 0) {
1676 Py_DECREF(buf);
1677 return NULL;
1678 }
Guido van Rossumaee08791992-09-08 09:05:33 +00001679 return buf;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001680}
1681
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001682PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001683"getsockopt(level, option[, buffersize]) -> value\n\
1684\n\
1685Get a socket option. See the Unix manual for level and option.\n\
1686If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001687string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001688
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001689
Fred Drake728819a2000-07-01 03:40:12 +00001690/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001691
Guido van Rossum73624e91994-10-10 17:59:00 +00001692static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001693sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001694{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001695 sock_addr_t addrbuf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001696 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001697 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001698
Thomas Wouters89f507f2006-12-13 04:49:30 +00001699 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001700 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001701 Py_BEGIN_ALLOW_THREADS
Thomas Wouters89f507f2006-12-13 04:49:30 +00001702 res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001703 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001704 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001705 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001706 Py_INCREF(Py_None);
1707 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001708}
1709
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001710PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001711"bind(address)\n\
1712\n\
1713Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00001714pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001715sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001716
Guido van Rossum30a685f1991-06-27 15:51:29 +00001717
1718/* s.close() method.
1719 Set the file descriptor to -1 so operations tried subsequently
1720 will surely fail. */
1721
Guido van Rossum73624e91994-10-10 17:59:00 +00001722static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001723sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001724{
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001725 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001726
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001727 if ((fd = s->sock_fd) != -1) {
1728 s->sock_fd = -1;
Guido van Rossum08481461996-10-12 14:07:22 +00001729 Py_BEGIN_ALLOW_THREADS
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001730 (void) SOCKETCLOSE(fd);
Guido van Rossum08481461996-10-12 14:07:22 +00001731 Py_END_ALLOW_THREADS
1732 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001733 Py_INCREF(Py_None);
1734 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001735}
1736
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001737PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001738"close()\n\
1739\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001740Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001741
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001742static int
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001743internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
1744 int *timeoutp)
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001745{
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001746 int res, timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001747
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001748 timeout = 0;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001749 res = connect(s->sock_fd, addr, addrlen);
1750
1751#ifdef MS_WINDOWS
1752
1753 if (s->sock_timeout > 0.0) {
Neal Norwitz082b2df2006-02-07 07:04:46 +00001754 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK &&
1755 IS_SELECTABLE(s)) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001756 /* This is a mess. Best solution: trust select */
1757 fd_set fds;
Mark Hammonda57ec932004-08-03 05:06:26 +00001758 fd_set fds_exc;
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001759 struct timeval tv;
1760 tv.tv_sec = (int)s->sock_timeout;
1761 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
1762 FD_ZERO(&fds);
1763 FD_SET(s->sock_fd, &fds);
Mark Hammonda57ec932004-08-03 05:06:26 +00001764 FD_ZERO(&fds_exc);
1765 FD_SET(s->sock_fd, &fds_exc);
1766 res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001767 if (res == 0) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001768 res = WSAEWOULDBLOCK;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001769 timeout = 1;
Mark Hammonda57ec932004-08-03 05:06:26 +00001770 } else if (res > 0) {
1771 if (FD_ISSET(s->sock_fd, &fds))
Sean Reifscheider54cf12b2007-09-17 17:55:36 +00001772 /* The socket is in the writable set - this
Mark Hammonda57ec932004-08-03 05:06:26 +00001773 means connected */
1774 res = 0;
1775 else {
1776 /* As per MS docs, we need to call getsockopt()
1777 to get the underlying error */
1778 int res_size = sizeof res;
1779 /* It must be in the exception set */
1780 assert(FD_ISSET(s->sock_fd, &fds_exc));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001781 if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
Mark Hammonda57ec932004-08-03 05:06:26 +00001782 (char *)&res, &res_size))
1783 /* getsockopt also clears WSAGetLastError,
1784 so reset it back. */
1785 WSASetLastError(res);
1786 else
1787 res = WSAGetLastError();
1788 }
1789 }
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001790 /* else if (res < 0) an error occurred */
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001791 }
1792 }
1793
1794 if (res < 0)
1795 res = WSAGetLastError();
1796
1797#else
1798
1799 if (s->sock_timeout > 0.0) {
Neal Norwitz082b2df2006-02-07 07:04:46 +00001800 if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001801 timeout = internal_select(s, 1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001802 if (timeout == 0) {
1803 res = connect(s->sock_fd, addr, addrlen);
1804 if (res < 0 && errno == EISCONN)
1805 res = 0;
1806 }
1807 else if (timeout == -1)
1808 res = errno; /* had error */
1809 else
1810 res = EWOULDBLOCK; /* timed out */
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001811 }
1812 }
1813
1814 if (res < 0)
1815 res = errno;
1816
1817#endif
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001818 *timeoutp = timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001819
1820 return res;
1821}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001822
Fred Drake728819a2000-07-01 03:40:12 +00001823/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001824
Guido van Rossum73624e91994-10-10 17:59:00 +00001825static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001826sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001827{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001828 sock_addr_t addrbuf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001829 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001830 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001831 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001832
Thomas Wouters89f507f2006-12-13 04:49:30 +00001833 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001834 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001835
Guido van Rossum73624e91994-10-10 17:59:00 +00001836 Py_BEGIN_ALLOW_THREADS
Thomas Wouters89f507f2006-12-13 04:49:30 +00001837 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
Guido van Rossum11ba0942002-06-13 15:07:44 +00001838 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001839
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001840 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001841 PyErr_SetString(socket_timeout, "timed out");
1842 return NULL;
1843 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001844 if (res != 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001845 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001846 Py_INCREF(Py_None);
1847 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001848}
1849
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001850PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001851"connect(address)\n\
1852\n\
1853Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001854is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001855
Guido van Rossum30a685f1991-06-27 15:51:29 +00001856
Fred Drake728819a2000-07-01 03:40:12 +00001857/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001858
1859static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001860sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001861{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001862 sock_addr_t addrbuf;
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001863 int addrlen;
1864 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001865 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001866
Thomas Wouters89f507f2006-12-13 04:49:30 +00001867 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001868 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001869
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001870 Py_BEGIN_ALLOW_THREADS
Thomas Wouters89f507f2006-12-13 04:49:30 +00001871 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001872 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001873
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001874 /* Signals are not errors (though they may raise exceptions). Adapted
1875 from PyErr_SetFromErrnoWithFilenameObject(). */
1876#ifdef EINTR
1877 if (res == EINTR && PyErr_CheckSignals())
1878 return NULL;
1879#endif
1880
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001881 return PyInt_FromLong((long) res);
1882}
1883
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001884PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001885"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001886\n\
1887This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001888instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001889
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001890
Guido van Rossumed233a51992-06-23 09:07:03 +00001891/* s.fileno() method */
1892
Guido van Rossum73624e91994-10-10 17:59:00 +00001893static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001894sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00001895{
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00001896 return PyLong_FromSocket_t(s->sock_fd);
Guido van Rossumed233a51992-06-23 09:07:03 +00001897}
1898
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001899PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001900"fileno() -> integer\n\
1901\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001902Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001903
Guido van Rossumed233a51992-06-23 09:07:03 +00001904
Guido van Rossumc89705d1992-11-26 08:54:07 +00001905/* s.getsockname() method */
1906
Guido van Rossum73624e91994-10-10 17:59:00 +00001907static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001908sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001909{
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001910 sock_addr_t addrbuf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001911 int res;
1912 socklen_t addrlen;
1913
Guido van Rossumc89705d1992-11-26 08:54:07 +00001914 if (!getsockaddrlen(s, &addrlen))
1915 return NULL;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001916 memset(&addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001917 Py_BEGIN_ALLOW_THREADS
Thomas Wouters89f507f2006-12-13 04:49:30 +00001918 res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001919 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001920 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001921 return s->errorhandler();
Thomas Wouters89f507f2006-12-13 04:49:30 +00001922 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001923 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001924}
1925
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001926PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001927"getsockname() -> address info\n\
1928\n\
1929Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001930info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001931
Guido van Rossumc89705d1992-11-26 08:54:07 +00001932
Guido van Rossumb6775db1994-08-01 11:34:53 +00001933#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001934/* s.getpeername() method */
1935
Guido van Rossum73624e91994-10-10 17:59:00 +00001936static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001937sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001938{
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001939 sock_addr_t addrbuf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001940 int res;
1941 socklen_t addrlen;
1942
Guido van Rossumc89705d1992-11-26 08:54:07 +00001943 if (!getsockaddrlen(s, &addrlen))
1944 return NULL;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001945 memset(&addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001946 Py_BEGIN_ALLOW_THREADS
Thomas Wouters89f507f2006-12-13 04:49:30 +00001947 res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001948 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001949 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001950 return s->errorhandler();
Thomas Wouters89f507f2006-12-13 04:49:30 +00001951 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001952 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001953}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001954
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001955PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001956"getpeername() -> address info\n\
1957\n\
1958Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001959info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001960
Guido van Rossumb6775db1994-08-01 11:34:53 +00001961#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001962
1963
Guido van Rossum30a685f1991-06-27 15:51:29 +00001964/* s.listen(n) method */
1965
Guido van Rossum73624e91994-10-10 17:59:00 +00001966static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001967sock_listen(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001968{
1969 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001970 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001971
1972 backlog = PyInt_AsLong(arg);
1973 if (backlog == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001974 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001975 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001976 if (backlog < 1)
1977 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001978 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +00001979 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001980 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001981 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001982 Py_INCREF(Py_None);
1983 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001984}
1985
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001986PyDoc_STRVAR(listen_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001987"listen(backlog)\n\
1988\n\
1989Enable a server to accept connections. The backlog argument must be at\n\
1990least 1; it specifies the number of unaccepted connection that the system\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001991will allow before refusing new connections.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001992
1993
Thomas Wouters477c8d52006-05-27 19:21:47 +00001994/*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001995 * This is the guts of the recv() and recv_into() methods, which reads into a
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001996 * char buffer. If you have any inc/dec ref to do to the objects that contain
Thomas Wouters477c8d52006-05-27 19:21:47 +00001997 * the buffer, do it in the caller. This function returns the number of bytes
1998 * succesfully read. If there was an error, it returns -1. Note that it is
1999 * also possible that we return a number of bytes smaller than the request
2000 * bytes.
2001 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002002static ssize_t
Thomas Wouters477c8d52006-05-27 19:21:47 +00002003sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags)
2004{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002005 ssize_t outlen = -1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002006 int timeout;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002007#ifdef __VMS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002008 int remaining;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002009 char *read_buf;
2010#endif
2011
2012 if (!IS_SELECTABLE(s)) {
2013 select_error();
2014 return -1;
2015 }
Guido van Rossumc2de7c02007-08-03 22:27:51 +00002016 if (len == 0) {
2017 /* If 0 bytes were requested, do nothing. */
2018 return 0;
2019 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002020
2021#ifndef __VMS
2022 Py_BEGIN_ALLOW_THREADS
2023 timeout = internal_select(s, 0);
2024 if (!timeout)
2025 outlen = recv(s->sock_fd, cbuf, len, flags);
2026 Py_END_ALLOW_THREADS
2027
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002028 if (timeout == 1) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002029 PyErr_SetString(socket_timeout, "timed out");
2030 return -1;
2031 }
2032 if (outlen < 0) {
2033 /* Note: the call to errorhandler() ALWAYS indirectly returned
2034 NULL, so ignore its return value */
2035 s->errorhandler();
2036 return -1;
2037 }
2038#else
2039 read_buf = cbuf;
2040 remaining = len;
2041 while (remaining != 0) {
2042 unsigned int segment;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002043 int nread = -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002044
2045 segment = remaining /SEGMENT_SIZE;
2046 if (segment != 0) {
2047 segment = SEGMENT_SIZE;
2048 }
2049 else {
2050 segment = remaining;
2051 }
2052
2053 Py_BEGIN_ALLOW_THREADS
2054 timeout = internal_select(s, 0);
2055 if (!timeout)
2056 nread = recv(s->sock_fd, read_buf, segment, flags);
2057 Py_END_ALLOW_THREADS
2058
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002059 if (timeout == 1) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002060 PyErr_SetString(socket_timeout, "timed out");
2061 return -1;
2062 }
2063 if (nread < 0) {
2064 s->errorhandler();
2065 return -1;
2066 }
2067 if (nread != remaining) {
2068 read_buf += nread;
2069 break;
2070 }
2071
2072 remaining -= segment;
2073 read_buf += segment;
2074 }
2075 outlen = read_buf - cbuf;
2076#endif /* !__VMS */
2077
2078 return outlen;
2079}
2080
Guido van Rossum48a680c2001-03-02 06:34:14 +00002081
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002082/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002083
Guido van Rossum73624e91994-10-10 17:59:00 +00002084static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002085sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002086{
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002087 int recvlen, flags = 0;
2088 ssize_t outlen;
Guido van Rossum73624e91994-10-10 17:59:00 +00002089 PyObject *buf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002090
Thomas Wouters477c8d52006-05-27 19:21:47 +00002091 if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002092 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002093
Thomas Wouters477c8d52006-05-27 19:21:47 +00002094 if (recvlen < 0) {
Martin v. Löwisfba64e12001-11-19 10:41:26 +00002095 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00002096 "negative buffersize in recv");
Martin v. Löwisfba64e12001-11-19 10:41:26 +00002097 return NULL;
2098 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002099
Thomas Wouters477c8d52006-05-27 19:21:47 +00002100 /* Allocate a new string. */
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002101 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002102 if (buf == NULL)
2103 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002104
Thomas Wouters477c8d52006-05-27 19:21:47 +00002105 /* Call the guts */
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002106 outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002107 if (outlen < 0) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002108 /* An error occurred, release the string and return an
Thomas Wouters477c8d52006-05-27 19:21:47 +00002109 error. */
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002110 Py_DECREF(buf);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002111 return NULL;
2112 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002113 if (outlen != recvlen) {
2114 /* We did not read as many bytes as we anticipated, resize the
Kurt B. Kaisere4050372007-08-22 21:38:31 +00002115 string if possible and be successful. */
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002116 if (PyBytes_Resize(buf, outlen) < 0)
Kurt B. Kaisere4050372007-08-22 21:38:31 +00002117 /* Oopsy, not so successful after all. */
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002118 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002119 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002120
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002121 return buf;
2122}
2123
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002124PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002125"recv(buffersize[, flags]) -> data\n\
2126\n\
2127Receive up to buffersize bytes from the socket. For the optional flags\n\
2128argument, see the Unix manual. When no data is available, block until\n\
2129at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002130the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002131
Guido van Rossum30a685f1991-06-27 15:51:29 +00002132
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002133/* s.recv_into(buffer, [nbytes [,flags]]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002134
Thomas Wouters477c8d52006-05-27 19:21:47 +00002135static PyObject*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002136sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002137{
2138 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
2139
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002140 int recvlen = 0, flags = 0;
2141 ssize_t readlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002142 char *buf;
2143 int buflen;
2144
2145 /* Get the buffer's memory */
Guido van Rossumd8faa362007-04-27 19:54:29 +00002146 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recv_into", kwlist,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002147 &buf, &buflen, &recvlen, &flags))
2148 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002149
2150 if (recvlen < 0) {
2151 PyErr_SetString(PyExc_ValueError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002152 "negative buffersize in recv_into");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002153 return NULL;
2154 }
2155 if (recvlen == 0) {
2156 /* If nbytes was not specified, use the buffer's length */
2157 recvlen = buflen;
2158 }
2159
2160 /* Check if the buffer is large enough */
2161 if (buflen < recvlen) {
2162 PyErr_SetString(PyExc_ValueError,
2163 "buffer too small for requested bytes");
2164 return NULL;
2165 }
2166
2167 /* Call the guts */
2168 readlen = sock_recv_guts(s, buf, recvlen, flags);
2169 if (readlen < 0) {
2170 /* Return an error. */
2171 return NULL;
2172 }
2173
2174 /* Return the number of bytes read. Note that we do not do anything
2175 special here in the case that readlen < recvlen. */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002176 return PyInt_FromSsize_t(readlen);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002177}
2178
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002179PyDoc_STRVAR(recv_into_doc,
2180"recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002181\n\
2182A version of recv() that stores its data into a buffer rather than creating \n\
2183a new string. Receive up to buffersize bytes from the socket. If buffersize \n\
2184is not specified (or 0), receive up to the size available in the given buffer.\n\
2185\n\
2186See recv() for documentation about the flags.");
2187
2188
2189/*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002190 * This is the guts of the recv() and recv_into() methods, which reads into a
Thomas Wouters477c8d52006-05-27 19:21:47 +00002191 * char buffer. If you have any inc/def ref to do to the objects that contain
2192 * the buffer, do it in the caller. This function returns the number of bytes
2193 * succesfully read. If there was an error, it returns -1. Note that it is
2194 * also possible that we return a number of bytes smaller than the request
2195 * bytes.
2196 *
2197 * 'addr' is a return value for the address object. Note that you must decref
2198 * it yourself.
2199 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002200static ssize_t
Thomas Wouters477c8d52006-05-27 19:21:47 +00002201sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags,
2202 PyObject** addr)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002203{
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002204 sock_addr_t addrbuf;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002205 int timeout;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002206 ssize_t n = -1;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002207 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002208
Thomas Wouters477c8d52006-05-27 19:21:47 +00002209 *addr = NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002210
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00002211 if (!getsockaddrlen(s, &addrlen))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002212 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002213
Thomas Wouters477c8d52006-05-27 19:21:47 +00002214 if (!IS_SELECTABLE(s)) {
2215 select_error();
2216 return -1;
2217 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002218
Guido van Rossum73624e91994-10-10 17:59:00 +00002219 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002220 memset(&addrbuf, 0, addrlen);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002221 timeout = internal_select(s, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002222 if (!timeout) {
Guido van Rossum8d665e61996-06-26 18:22:49 +00002223#ifndef MS_WINDOWS
Andrew MacIntyreba43e872002-03-03 03:03:52 +00002224#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002225 n = recvfrom(s->sock_fd, cbuf, len, flags,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002226 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum32c575d1997-12-02 20:37:32 +00002227#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002228 n = recvfrom(s->sock_fd, cbuf, len, flags,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002229 (void *) &addrbuf, &addrlen);
Guido van Rossum32c575d1997-12-02 20:37:32 +00002230#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002231#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002232 n = recvfrom(s->sock_fd, cbuf, len, flags,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002233 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002234#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002235 }
Guido van Rossum73624e91994-10-10 17:59:00 +00002236 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002237
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002238 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002239 PyErr_SetString(socket_timeout, "timed out");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002240 return -1;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002241 }
Guido van Rossum7c53b771995-09-13 18:39:47 +00002242 if (n < 0) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002243 s->errorhandler();
2244 return -1;
Guido van Rossum7c53b771995-09-13 18:39:47 +00002245 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002246
Thomas Wouters89f507f2006-12-13 04:49:30 +00002247 if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
Thomas Wouters477c8d52006-05-27 19:21:47 +00002248 addrlen, s->sock_proto)))
2249 return -1;
2250
2251 return n;
2252}
2253
2254/* s.recvfrom(nbytes [,flags]) method */
2255
2256static PyObject *
2257sock_recvfrom(PySocketSockObject *s, PyObject *args)
2258{
2259 PyObject *buf = NULL;
2260 PyObject *addr = NULL;
2261 PyObject *ret = NULL;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002262 int recvlen, flags = 0;
2263 ssize_t outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002264
2265 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002266 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002267
Guido van Rossumd8faa362007-04-27 19:54:29 +00002268 if (recvlen < 0) {
2269 PyErr_SetString(PyExc_ValueError,
2270 "negative buffersize in recvfrom");
2271 return NULL;
2272 }
2273
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002274 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002275 if (buf == NULL)
2276 return NULL;
2277
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002278 outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf),
Thomas Wouters477c8d52006-05-27 19:21:47 +00002279 recvlen, flags, &addr);
2280 if (outlen < 0) {
Barry Warsaw752300b1997-01-03 17:18:10 +00002281 goto finally;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002282 }
2283
2284 if (outlen != recvlen) {
2285 /* We did not read as many bytes as we anticipated, resize the
2286 string if possible and be succesful. */
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002287 if (PyBytes_Resize(buf, outlen) < 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002288 /* Oopsy, not so succesful after all. */
2289 goto finally;
2290 }
Barry Warsaw752300b1997-01-03 17:18:10 +00002291
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002292 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002293
2294finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00002295 Py_XDECREF(buf);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002296 Py_XDECREF(addr);
Guido van Rossum6f5afc91993-02-05 09:46:15 +00002297 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002298}
2299
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002300PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002301"recvfrom(buffersize[, flags]) -> (data, address info)\n\
2302\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002303Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002304
Thomas Wouters477c8d52006-05-27 19:21:47 +00002305
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002306/* s.recvfrom_into(buffer[, nbytes [,flags]]) method */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002307
2308static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002309sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002310{
2311 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
2312
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002313 int recvlen = 0, flags = 0;
2314 ssize_t readlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002315 char *buf;
2316 int buflen;
2317
2318 PyObject *addr = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002319
Guido van Rossumd8faa362007-04-27 19:54:29 +00002320 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recvfrom_into",
2321 kwlist, &buf, &buflen,
2322 &recvlen, &flags))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002323 return NULL;
2324 assert(buf != 0 && buflen > 0);
2325
2326 if (recvlen < 0) {
2327 PyErr_SetString(PyExc_ValueError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002328 "negative buffersize in recvfrom_into");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002329 return NULL;
2330 }
2331 if (recvlen == 0) {
2332 /* If nbytes was not specified, use the buffer's length */
2333 recvlen = buflen;
2334 }
2335
2336 readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
2337 if (readlen < 0) {
2338 /* Return an error */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002339 Py_XDECREF(addr);
2340 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002341 }
2342
2343 /* Return the number of bytes read and the address. Note that we do
2344 not do anything special here in the case that readlen < recvlen. */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002345 return Py_BuildValue("lN", readlen, addr);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002346}
2347
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002348PyDoc_STRVAR(recvfrom_into_doc,
2349"recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002350\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002351Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002352
2353
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002354/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002355
Guido van Rossum73624e91994-10-10 17:59:00 +00002356static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002357sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002358{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002359 char *buf;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002360 int len, n = -1, flags = 0, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002361
Guido van Rossum43713e52000-02-29 13:59:29 +00002362 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002363 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002364
Neal Norwitz082b2df2006-02-07 07:04:46 +00002365 if (!IS_SELECTABLE(s))
2366 return select_error();
2367
Guido van Rossum73624e91994-10-10 17:59:00 +00002368 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002369 timeout = internal_select(s, 1);
2370 if (!timeout)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002371#ifdef __VMS
2372 n = sendsegmented(s->sock_fd, buf, len, flags);
2373#else
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002374 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002375#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00002376 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002377
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002378 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002379 PyErr_SetString(socket_timeout, "timed out");
2380 return NULL;
2381 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00002382 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002383 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002384 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00002385}
2386
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002387PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002388"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002389\n\
2390Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002391argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002392sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002393
2394
2395/* s.sendall(data [,flags]) method */
2396
2397static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002398sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002399{
2400 char *buf;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002401 int len, n = -1, flags = 0, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002402
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002403 if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags))
2404 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002405
Neal Norwitz082b2df2006-02-07 07:04:46 +00002406 if (!IS_SELECTABLE(s))
2407 return select_error();
2408
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002409 Py_BEGIN_ALLOW_THREADS
2410 do {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002411 timeout = internal_select(s, 1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002412 n = -1;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002413 if (timeout)
2414 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002415#ifdef __VMS
2416 n = sendsegmented(s->sock_fd, buf, len, flags);
2417#else
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002418 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002419#endif
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002420 if (n < 0)
2421 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002422 buf += n;
2423 len -= n;
2424 } while (len > 0);
2425 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002426
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002427 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002428 PyErr_SetString(socket_timeout, "timed out");
2429 return NULL;
2430 }
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002431 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002432 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00002433
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002434 Py_INCREF(Py_None);
2435 return Py_None;
2436}
2437
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002438PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002439"sendall(data[, flags])\n\
2440\n\
2441Send a data string to the socket. For the optional flags\n\
2442argument, see the Unix manual. This calls send() repeatedly\n\
2443until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002444to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002445
Guido van Rossum30a685f1991-06-27 15:51:29 +00002446
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002447/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002448
Guido van Rossum73624e91994-10-10 17:59:00 +00002449static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002450sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002451{
Guido van Rossum73624e91994-10-10 17:59:00 +00002452 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002453 char *buf;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002454 sock_addr_t addrbuf;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002455 int addrlen, len, n = -1, flags, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002456
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002457 flags = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002458 if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00002459 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002460 if (!PyArg_ParseTuple(args, "s#iO:sendto",
2461 &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002462 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002463 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002464
Neal Norwitz082b2df2006-02-07 07:04:46 +00002465 if (!IS_SELECTABLE(s))
2466 return select_error();
2467
Thomas Wouters89f507f2006-12-13 04:49:30 +00002468 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2469 return NULL;
2470
Guido van Rossum73624e91994-10-10 17:59:00 +00002471 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002472 timeout = internal_select(s, 1);
2473 if (!timeout)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002474 n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002475 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002476
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002477 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002478 PyErr_SetString(socket_timeout, "timed out");
2479 return NULL;
2480 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002481 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002482 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002483 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002484}
2485
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002486PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002487"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002488\n\
2489Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002490For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002491
Guido van Rossum30a685f1991-06-27 15:51:29 +00002492
2493/* s.shutdown(how) method */
2494
Guido van Rossum73624e91994-10-10 17:59:00 +00002495static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002496sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002497{
2498 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002499 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002500
2501 how = PyInt_AsLong(arg);
2502 if (how == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002503 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002504 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002505 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00002506 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002507 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002508 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002509 Py_INCREF(Py_None);
2510 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002511}
2512
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002513PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002514"shutdown(flag)\n\
2515\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00002516Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
2517of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002518
Guido van Rossum30a685f1991-06-27 15:51:29 +00002519
2520/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002521
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002522static PyMethodDef sock_methods[] = {
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002523 {"_accept", (PyCFunction)sock_accept, METH_NOARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002524 accept_doc},
2525 {"bind", (PyCFunction)sock_bind, METH_O,
2526 bind_doc},
2527 {"close", (PyCFunction)sock_close, METH_NOARGS,
2528 close_doc},
2529 {"connect", (PyCFunction)sock_connect, METH_O,
2530 connect_doc},
2531 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
2532 connect_ex_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002533 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
2534 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002535#ifdef HAVE_GETPEERNAME
Thomas Wouters477c8d52006-05-27 19:21:47 +00002536 {"getpeername", (PyCFunction)sock_getpeername,
2537 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00002538#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00002539 {"getsockname", (PyCFunction)sock_getsockname,
2540 METH_NOARGS, getsockname_doc},
2541 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
2542 getsockopt_doc},
2543 {"listen", (PyCFunction)sock_listen, METH_O,
2544 listen_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002545 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
2546 recv_doc},
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002547 {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
2548 recv_into_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002549 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
2550 recvfrom_doc},
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002551 {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
2552 recvfrom_into_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002553 {"send", (PyCFunction)sock_send, METH_VARARGS,
2554 send_doc},
2555 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
2556 sendall_doc},
2557 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
2558 sendto_doc},
2559 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
2560 setblocking_doc},
2561 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
2562 settimeout_doc},
2563 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
2564 gettimeout_doc},
2565 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
2566 setsockopt_doc},
2567 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
2568 shutdown_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002569 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002570};
2571
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002572/* SockObject members */
2573static PyMemberDef sock_memberlist[] = {
2574 {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},
2575 {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},
2576 {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},
2577 {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"},
2578 {0},
2579};
Guido van Rossum30a685f1991-06-27 15:51:29 +00002580
Guido van Rossum73624e91994-10-10 17:59:00 +00002581/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00002582 First close the file description. */
2583
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002584static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002585sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002586{
Guido van Rossumfa972c92000-04-10 12:45:45 +00002587 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00002588 (void) SOCKETCLOSE(s->sock_fd);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002589 Py_Type(s)->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002590}
2591
Guido van Rossum30a685f1991-06-27 15:51:29 +00002592
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002593static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002594sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002595{
Fred Drakea04eaad2000-06-30 02:46:07 +00002596#if SIZEOF_SOCKET_T > SIZEOF_LONG
2597 if (s->sock_fd > LONG_MAX) {
2598 /* this can occur on Win64, and actually there is a special
2599 ugly printf formatter for decimal pointer length integer
2600 printing, only bother if necessary*/
2601 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002602 "no printf formatter to display "
2603 "the socket descriptor in decimal");
Fred Drakea04eaad2000-06-30 02:46:07 +00002604 return NULL;
2605 }
2606#endif
Walter Dörwalda7eb93e2007-06-05 13:41:53 +00002607 return PyUnicode_FromFormat(
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002608 "<socket object, fd=%ld, family=%d, type=%d, proto=%d>",
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002609 (long)s->sock_fd, s->sock_family,
2610 s->sock_type,
2611 s->sock_proto);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002612}
2613
2614
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002615/* Create a new, uninitialized socket object. */
2616
2617static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002618sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002619{
2620 PyObject *new;
2621
2622 new = type->tp_alloc(type, 0);
Guido van Rossume1c478f2002-06-06 20:08:25 +00002623 if (new != NULL) {
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002624 ((PySocketSockObject *)new)->sock_fd = -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002625 ((PySocketSockObject *)new)->sock_timeout = -1.0;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002626 ((PySocketSockObject *)new)->errorhandler = &set_error;
Guido van Rossume1c478f2002-06-06 20:08:25 +00002627 }
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002628 return new;
2629}
2630
2631
2632/* Initialize a new socket object. */
2633
2634/*ARGSUSED*/
2635static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002636sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002637{
2638 PySocketSockObject *s = (PySocketSockObject *)self;
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002639 PyObject *fdobj = NULL;
2640 SOCKET_T fd = INVALID_SOCKET;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002641 int family = AF_INET, type = SOCK_STREAM, proto = 0;
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002642 static char *keywords[] = {"family", "type", "proto", "fileno", 0};
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002643
2644 if (!PyArg_ParseTupleAndKeywords(args, kwds,
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002645 "|iiiO:socket", keywords,
2646 &family, &type, &proto, &fdobj))
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002647 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002648
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00002649 if (fdobj != NULL && fdobj != Py_None) {
2650 fd = PyLong_AsSocket_t(fdobj);
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002651 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
2652 return -1;
2653 if (fd == INVALID_SOCKET) {
2654 PyErr_SetString(PyExc_ValueError,
2655 "can't use invalid socket value");
2656 return -1;
2657 }
2658 }
2659 else {
2660 Py_BEGIN_ALLOW_THREADS
2661 fd = socket(family, type, proto);
2662 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002663
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002664 if (fd == INVALID_SOCKET) {
2665 set_error();
2666 return -1;
2667 }
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002668 }
2669 init_sockobject(s, fd, family, type, proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002670
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002671 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002672
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002673}
2674
2675
Guido van Rossumb6775db1994-08-01 11:34:53 +00002676/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002677
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002678static PyTypeObject sock_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002679 PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
Guido van Rossumca6dfa52001-10-28 12:31:33 +00002680 "_socket.socket", /* tp_name */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002681 sizeof(PySocketSockObject), /* tp_basicsize */
2682 0, /* tp_itemsize */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002683 (destructor)sock_dealloc, /* tp_dealloc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002684 0, /* tp_print */
2685 0, /* tp_getattr */
2686 0, /* tp_setattr */
2687 0, /* tp_compare */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002688 (reprfunc)sock_repr, /* tp_repr */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002689 0, /* tp_as_number */
2690 0, /* tp_as_sequence */
2691 0, /* tp_as_mapping */
2692 0, /* tp_hash */
2693 0, /* tp_call */
2694 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002695 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002696 0, /* tp_setattro */
2697 0, /* tp_as_buffer */
2698 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002699 sock_doc, /* tp_doc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002700 0, /* tp_traverse */
2701 0, /* tp_clear */
2702 0, /* tp_richcompare */
2703 0, /* tp_weaklistoffset */
2704 0, /* tp_iter */
2705 0, /* tp_iternext */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002706 sock_methods, /* tp_methods */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002707 sock_memberlist, /* tp_members */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002708 0, /* tp_getset */
2709 0, /* tp_base */
2710 0, /* tp_dict */
2711 0, /* tp_descr_get */
2712 0, /* tp_descr_set */
2713 0, /* tp_dictoffset */
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002714 sock_initobj, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002715 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002716 sock_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002717 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002718};
2719
Guido van Rossum30a685f1991-06-27 15:51:29 +00002720
Guido van Rossum81194471991-07-27 21:42:02 +00002721/* Python interface to gethostname(). */
2722
2723/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002724static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002725socket_gethostname(PyObject *self, PyObject *unused)
Guido van Rossum81194471991-07-27 21:42:02 +00002726{
2727 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00002728 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00002729 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002730 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00002731 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002732 if (res < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002733 return set_error();
Guido van Rossum81194471991-07-27 21:42:02 +00002734 buf[sizeof buf - 1] = '\0';
Guido van Rossum32c4ac02007-08-15 03:56:40 +00002735 return PyUnicode_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00002736}
Guido van Rossumff4949e1992-08-05 19:58:53 +00002737
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002738PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002739"gethostname() -> string\n\
2740\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002741Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002742
Guido van Rossumff4949e1992-08-05 19:58:53 +00002743
Guido van Rossum30a685f1991-06-27 15:51:29 +00002744/* Python interface to gethostbyname(name). */
2745
2746/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002747static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002748socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002749{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002750 char *name;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002751 sock_addr_t addrbuf;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002752
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002753 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002754 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002755 if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002756 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002757 return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002758}
2759
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002760PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002761"gethostbyname(host) -> address\n\
2762\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002763Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002764
2765
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002766/* Convenience function common to gethostbyname_ex and gethostbyaddr */
2767
2768static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002769gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002770{
2771 char **pch;
2772 PyObject *rtn_tuple = (PyObject *)NULL;
2773 PyObject *name_list = (PyObject *)NULL;
2774 PyObject *addr_list = (PyObject *)NULL;
2775 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002776
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002777 if (h == NULL) {
Guido van Rossum48a680c2001-03-02 06:34:14 +00002778 /* Let's get real error message to return */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002779 set_herror(h_errno);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002780 return NULL;
2781 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002782
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002783 if (h->h_addrtype != af) {
2784#ifdef HAVE_STRERROR
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002785 /* Let's get real error message to return */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002786 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002787 (char *)strerror(EAFNOSUPPORT));
2788#else
2789 PyErr_SetString(
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002790 socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002791 "Address family not supported by protocol family");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002792#endif
2793 return NULL;
2794 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002795
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002796 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002797
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002798 case AF_INET:
2799 if (alen < sizeof(struct sockaddr_in))
2800 return NULL;
2801 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002802
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002803#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002804 case AF_INET6:
2805 if (alen < sizeof(struct sockaddr_in6))
2806 return NULL;
2807 break;
2808#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002809
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002810 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002811
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002812 if ((name_list = PyList_New(0)) == NULL)
2813 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002814
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002815 if ((addr_list = PyList_New(0)) == NULL)
2816 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002817
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002818 /* SF #1511317: h_aliases can be NULL */
2819 if (h->h_aliases) {
2820 for (pch = h->h_aliases; *pch != NULL; pch++) {
2821 int status;
Guido van Rossum32c4ac02007-08-15 03:56:40 +00002822 tmp = PyUnicode_FromString(*pch);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002823 if (tmp == NULL)
2824 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002825
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002826 status = PyList_Append(name_list, tmp);
2827 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002828
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002829 if (status)
2830 goto err;
2831 }
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002832 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002833
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002834 for (pch = h->h_addr_list; *pch != NULL; pch++) {
2835 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002836
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002837 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002838
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002839 case AF_INET:
2840 {
2841 struct sockaddr_in sin;
2842 memset(&sin, 0, sizeof(sin));
2843 sin.sin_family = af;
2844#ifdef HAVE_SOCKADDR_SA_LEN
2845 sin.sin_len = sizeof(sin);
2846#endif
2847 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
2848 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002849
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002850 if (pch == h->h_addr_list && alen >= sizeof(sin))
2851 memcpy((char *) addr, &sin, sizeof(sin));
2852 break;
2853 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002854
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002855#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002856 case AF_INET6:
2857 {
2858 struct sockaddr_in6 sin6;
2859 memset(&sin6, 0, sizeof(sin6));
2860 sin6.sin6_family = af;
2861#ifdef HAVE_SOCKADDR_SA_LEN
2862 sin6.sin6_len = sizeof(sin6);
2863#endif
2864 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
2865 tmp = makeipaddr((struct sockaddr *)&sin6,
2866 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002867
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002868 if (pch == h->h_addr_list && alen >= sizeof(sin6))
2869 memcpy((char *) addr, &sin6, sizeof(sin6));
2870 break;
2871 }
2872#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002873
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002874 default: /* can't happen */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002875 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002876 "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002877 return NULL;
2878 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002879
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002880 if (tmp == NULL)
2881 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002882
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002883 status = PyList_Append(addr_list, tmp);
2884 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002885
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002886 if (status)
2887 goto err;
2888 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002889
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002890 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002891
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002892 err:
2893 Py_XDECREF(name_list);
2894 Py_XDECREF(addr_list);
2895 return rtn_tuple;
2896}
2897
2898
2899/* Python interface to gethostbyname_ex(name). */
2900
2901/*ARGSUSED*/
2902static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002903socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002904{
2905 char *name;
2906 struct hostent *h;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002907#ifdef ENABLE_IPV6
2908 struct sockaddr_storage addr;
2909#else
2910 struct sockaddr_in addr;
2911#endif
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002912 struct sockaddr *sa;
Guido van Rossum955becc1999-03-22 20:14:53 +00002913 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002914#ifdef HAVE_GETHOSTBYNAME_R
2915 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002916#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2917 struct hostent_data data;
2918#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002919 char buf[16384];
2920 int buf_len = (sizeof buf) - 1;
2921 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002922#endif
2923#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002924 int result;
2925#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002926#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002927
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002928 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002929 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00002930 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002931 return NULL;
2932 Py_BEGIN_ALLOW_THREADS
2933#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002934#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002935 result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
2936 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002937#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002938 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002939#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002940 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002941 result = gethostbyname_r(name, &hp_allocated, &data);
2942 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002943#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002944#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002945#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002946 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002947#endif
2948 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002949#endif /* HAVE_GETHOSTBYNAME_R */
2950 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002951 /* Some C libraries would require addr.__ss_family instead of
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002952 addr.ss_family.
2953 Therefore, we cast the sockaddr_storage into sockaddr to
2954 access sa_family. */
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002955 sa = (struct sockaddr*)&addr;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002956 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002957 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002958#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002959 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00002960#endif
2961 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002962}
2963
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002964PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002965"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
2966\n\
2967Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002968for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002969
2970
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002971/* Python interface to gethostbyaddr(IP). */
2972
2973/*ARGSUSED*/
2974static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002975socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002976{
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002977#ifdef ENABLE_IPV6
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002978 struct sockaddr_storage addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002979#else
Guido van Rossum48a680c2001-03-02 06:34:14 +00002980 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002981#endif
2982 struct sockaddr *sa = (struct sockaddr *)&addr;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002983 char *ip_num;
2984 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00002985 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002986#ifdef HAVE_GETHOSTBYNAME_R
2987 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002988#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2989 struct hostent_data data;
2990#else
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002991 char buf[16384];
2992 int buf_len = (sizeof buf) - 1;
2993 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002994#endif
2995#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002996 int result;
2997#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002998#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002999 char *ap;
3000 int al;
3001 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003002
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003003 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003004 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003005 af = AF_UNSPEC;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00003006 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003007 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003008 af = sa->sa_family;
3009 ap = NULL;
3010 al = 0;
3011 switch (af) {
3012 case AF_INET:
3013 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
3014 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3015 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003016#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003017 case AF_INET6:
3018 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
3019 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3020 break;
3021#endif
3022 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003023 PyErr_SetString(socket_error, "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003024 return NULL;
3025 }
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003026 Py_BEGIN_ALLOW_THREADS
3027#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003028#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003029 result = gethostbyaddr_r(ap, al, af,
3030 &hp_allocated, buf, buf_len,
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003031 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003032#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003033 h = gethostbyaddr_r(ap, al, af,
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003034 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003035#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00003036 memset((void *) &data, '\0', sizeof(data));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003037 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003038 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003039#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003040#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003041#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00003042 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003043#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003044 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003045#endif /* HAVE_GETHOSTBYNAME_R */
3046 Py_END_ALLOW_THREADS
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003047 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003048#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00003049 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003050#endif
3051 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003052}
3053
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003054PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003055"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
3056\n\
3057Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003058for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003059
Guido van Rossum30a685f1991-06-27 15:51:29 +00003060
3061/* Python interface to getservbyname(name).
3062 This only returns the port number, since the other info is already
3063 known or not useful (like the list of aliases). */
3064
3065/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00003066static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003067socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00003068{
Barry Warsaw11b91a02004-06-28 00:50:43 +00003069 char *name, *proto=NULL;
Guido van Rossum30a685f1991-06-27 15:51:29 +00003070 struct servent *sp;
Barry Warsaw11b91a02004-06-28 00:50:43 +00003071 if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00003072 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00003073 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00003074 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00003075 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00003076 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003077 PyErr_SetString(socket_error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00003078 return NULL;
3079 }
Guido van Rossum73624e91994-10-10 17:59:00 +00003080 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00003081}
3082
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003083PyDoc_STRVAR(getservbyname_doc,
Barry Warsaw11b91a02004-06-28 00:50:43 +00003084"getservbyname(servicename[, protocolname]) -> integer\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003085\n\
3086Return a port number from a service name and protocol name.\n\
Barry Warsaw11b91a02004-06-28 00:50:43 +00003087The optional protocol name, if given, should be 'tcp' or 'udp',\n\
3088otherwise any protocol will match.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003089
Guido van Rossum30a685f1991-06-27 15:51:29 +00003090
Barry Warsaw11b91a02004-06-28 00:50:43 +00003091/* Python interface to getservbyport(port).
3092 This only returns the service name, since the other info is already
3093 known or not useful (like the list of aliases). */
3094
3095/*ARGSUSED*/
3096static PyObject *
3097socket_getservbyport(PyObject *self, PyObject *args)
3098{
Raymond Hettinger87de0ca2004-09-28 02:19:40 +00003099 unsigned short port;
Barry Warsaw11b91a02004-06-28 00:50:43 +00003100 char *proto=NULL;
3101 struct servent *sp;
Raymond Hettinger87de0ca2004-09-28 02:19:40 +00003102 if (!PyArg_ParseTuple(args, "H|s:getservbyport", &port, &proto))
Barry Warsaw11b91a02004-06-28 00:50:43 +00003103 return NULL;
3104 Py_BEGIN_ALLOW_THREADS
3105 sp = getservbyport(htons(port), proto);
3106 Py_END_ALLOW_THREADS
3107 if (sp == NULL) {
3108 PyErr_SetString(socket_error, "port/proto not found");
3109 return NULL;
3110 }
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003111 return PyUnicode_FromString(sp->s_name);
Barry Warsaw11b91a02004-06-28 00:50:43 +00003112}
3113
3114PyDoc_STRVAR(getservbyport_doc,
3115"getservbyport(port[, protocolname]) -> string\n\
3116\n\
3117Return the service name from a port number and protocol name.\n\
3118The optional protocol name, if given, should be 'tcp' or 'udp',\n\
3119otherwise any protocol will match.");
3120
Guido van Rossum3901d851996-12-19 16:35:04 +00003121/* Python interface to getprotobyname(name).
3122 This only returns the protocol number, since the other info is
3123 already known or not useful (like the list of aliases). */
3124
3125/*ARGSUSED*/
3126static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003127socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00003128{
3129 char *name;
3130 struct protoent *sp;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003131 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00003132 return NULL;
3133 Py_BEGIN_ALLOW_THREADS
3134 sp = getprotobyname(name);
3135 Py_END_ALLOW_THREADS
3136 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003137 PyErr_SetString(socket_error, "protocol not found");
Guido van Rossum3901d851996-12-19 16:35:04 +00003138 return NULL;
3139 }
3140 return PyInt_FromLong((long) sp->p_proto);
3141}
3142
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003143PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003144"getprotobyname(name) -> integer\n\
3145\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003146Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003147
Guido van Rossum3901d851996-12-19 16:35:04 +00003148
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003149#ifndef NO_DUP
3150/* dup() function for socket fds */
3151
3152static PyObject *
3153socket_dup(PyObject *self, PyObject *fdobj)
3154{
3155 SOCKET_T fd, newfd;
3156 PyObject *newfdobj;
3157
3158
3159 fd = PyLong_AsSocket_t(fdobj);
3160 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
3161 return NULL;
3162
3163 newfd = dup_socket(fd);
3164 if (newfd == INVALID_SOCKET)
3165 return set_error();
3166
3167 newfdobj = PyLong_FromSocket_t(newfd);
3168 if (newfdobj == NULL)
3169 SOCKETCLOSE(newfd);
3170 return newfdobj;
3171}
3172
3173PyDoc_STRVAR(dup_doc,
3174"dup(integer) -> integer\n\
3175\n\
3176Duplicate an integer socket file descriptor. This is like os.dup(), but for\n\
3177sockets; on some platforms os.dup() won't work for socket file descriptors.");
3178#endif
3179
3180
Dave Cole331708b2004-08-09 04:51:41 +00003181#ifdef HAVE_SOCKETPAIR
3182/* Create a pair of sockets using the socketpair() function.
Dave Cole07fda7e2004-08-23 05:16:23 +00003183 Arguments as for socket() except the default family is AF_UNIX if
Dave Colee8bbfe42004-08-26 00:51:16 +00003184 defined on the platform; otherwise, the default is AF_INET. */
Dave Cole331708b2004-08-09 04:51:41 +00003185
3186/*ARGSUSED*/
3187static PyObject *
3188socket_socketpair(PyObject *self, PyObject *args)
3189{
3190 PySocketSockObject *s0 = NULL, *s1 = NULL;
3191 SOCKET_T sv[2];
3192 int family, type = SOCK_STREAM, proto = 0;
3193 PyObject *res = NULL;
3194
3195#if defined(AF_UNIX)
3196 family = AF_UNIX;
3197#else
3198 family = AF_INET;
3199#endif
3200 if (!PyArg_ParseTuple(args, "|iii:socketpair",
3201 &family, &type, &proto))
3202 return NULL;
3203 /* Create a pair of socket fds */
3204 if (socketpair(family, type, proto, sv) < 0)
3205 return set_error();
Dave Cole331708b2004-08-09 04:51:41 +00003206 s0 = new_sockobject(sv[0], family, type, proto);
3207 if (s0 == NULL)
3208 goto finally;
3209 s1 = new_sockobject(sv[1], family, type, proto);
3210 if (s1 == NULL)
3211 goto finally;
3212 res = PyTuple_Pack(2, s0, s1);
3213
3214finally:
3215 if (res == NULL) {
3216 if (s0 == NULL)
3217 SOCKETCLOSE(sv[0]);
3218 if (s1 == NULL)
3219 SOCKETCLOSE(sv[1]);
3220 }
3221 Py_XDECREF(s0);
3222 Py_XDECREF(s1);
3223 return res;
3224}
3225
3226PyDoc_STRVAR(socketpair_doc,
3227"socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\
3228\n\
3229Create a pair of socket objects from the sockets returned by the platform\n\
3230socketpair() function.\n\
Dave Cole07fda7e2004-08-23 05:16:23 +00003231The arguments are the same as for socket() except the default family is\n\
Dave Colee8bbfe42004-08-26 00:51:16 +00003232AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
Dave Cole331708b2004-08-09 04:51:41 +00003233
3234#endif /* HAVE_SOCKETPAIR */
3235
3236
Guido van Rossum006bf911996-06-12 04:04:55 +00003237static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003238socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003239{
3240 int x1, x2;
3241
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003242 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00003243 return NULL;
3244 }
Guido van Rossum018919a2007-01-15 00:07:32 +00003245 if (x1 < 0) {
3246 PyErr_SetString(PyExc_OverflowError,
3247 "can't convert negative number to unsigned long");
3248 return NULL;
3249 }
3250 x2 = (unsigned int)ntohs((unsigned short)x1);
Guido van Rossum006bf911996-06-12 04:04:55 +00003251 return PyInt_FromLong(x2);
3252}
3253
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003254PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003255"ntohs(integer) -> integer\n\
3256\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003257Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003258
3259
Guido van Rossum006bf911996-06-12 04:04:55 +00003260static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003261socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003262{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003263 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003264
Guido van Rossumddefaf32007-01-14 03:31:43 +00003265 if (PyLong_Check(arg)) {
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003266 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003267 if (x == (unsigned long) -1 && PyErr_Occurred())
3268 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003269#if SIZEOF_LONG > 4
3270 {
3271 unsigned long y;
3272 /* only want the trailing 32 bits */
3273 y = x & 0xFFFFFFFFUL;
3274 if (y ^ x)
3275 return PyErr_Format(PyExc_OverflowError,
3276 "long int larger than 32 bits");
3277 x = y;
Tim Peters58141872002-08-06 22:25:02 +00003278 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003279#endif
3280 }
3281 else
Tim Peters58141872002-08-06 22:25:02 +00003282 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003283 "expected int/long, %s found",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003284 Py_Type(arg)->tp_name);
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003285 if (x == (unsigned long) -1 && PyErr_Occurred())
3286 return NULL;
Guido van Rossum018919a2007-01-15 00:07:32 +00003287 return PyLong_FromUnsignedLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003288}
3289
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003290PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003291"ntohl(integer) -> integer\n\
3292\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003293Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003294
3295
Guido van Rossum006bf911996-06-12 04:04:55 +00003296static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003297socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003298{
Hye-Shik Changa7b673f2003-12-17 09:40:23 +00003299 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00003300
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003301 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00003302 return NULL;
3303 }
Guido van Rossum018919a2007-01-15 00:07:32 +00003304 if (x1 < 0) {
3305 PyErr_SetString(PyExc_OverflowError,
3306 "can't convert negative number to unsigned long");
3307 return NULL;
3308 }
3309 x2 = (unsigned int)htons((unsigned short)x1);
Guido van Rossum006bf911996-06-12 04:04:55 +00003310 return PyInt_FromLong(x2);
3311}
3312
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003313PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003314"htons(integer) -> integer\n\
3315\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003316Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003317
3318
Guido van Rossum006bf911996-06-12 04:04:55 +00003319static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003320socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003321{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003322 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003323
Guido van Rossumddefaf32007-01-14 03:31:43 +00003324 if (PyLong_Check(arg)) {
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003325 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003326 if (x == (unsigned long) -1 && PyErr_Occurred())
3327 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003328#if SIZEOF_LONG > 4
3329 {
3330 unsigned long y;
3331 /* only want the trailing 32 bits */
3332 y = x & 0xFFFFFFFFUL;
3333 if (y ^ x)
3334 return PyErr_Format(PyExc_OverflowError,
3335 "long int larger than 32 bits");
3336 x = y;
Tim Peters58141872002-08-06 22:25:02 +00003337 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003338#endif
3339 }
3340 else
Tim Peters58141872002-08-06 22:25:02 +00003341 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003342 "expected int/long, %s found",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003343 Py_Type(arg)->tp_name);
Guido van Rossum018919a2007-01-15 00:07:32 +00003344 return PyLong_FromUnsignedLong(htonl((unsigned long)x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003345}
3346
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003347PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003348"htonl(integer) -> integer\n\
3349\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003350Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003351
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003352/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003353
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003354PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum7d0a8262007-05-21 23:13:11 +00003355"inet_aton(string) -> bytes giving packed 32-bit IP representation\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003356\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003357Convert 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 +00003358binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003359
3360static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003361socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003362{
Guido van Rossuma2e48551999-09-09 15:42:59 +00003363#ifndef INADDR_NONE
3364#define INADDR_NONE (-1)
3365#endif
Neal Norwitz88f115b2003-02-13 02:15:42 +00003366#ifdef HAVE_INET_ATON
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003367 struct in_addr buf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003368#endif
3369
3370#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
Tim Peters1df9fdd2003-02-13 03:13:40 +00003371 /* Have to use inet_addr() instead */
3372 unsigned long packed_addr;
3373#endif
3374 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003375
Tim Peters1df9fdd2003-02-13 03:13:40 +00003376 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003377 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003378
Tim Peters1df9fdd2003-02-13 03:13:40 +00003379
3380#ifdef HAVE_INET_ATON
Thomas Wouters477c8d52006-05-27 19:21:47 +00003381
3382#ifdef USE_INET_ATON_WEAKLINK
3383 if (inet_aton != NULL) {
3384#endif
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003385 if (inet_aton(ip_addr, &buf))
Guido van Rossum7d0a8262007-05-21 23:13:11 +00003386 return PyBytes_FromStringAndSize((char *)(&buf),
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003387 sizeof(buf));
3388
3389 PyErr_SetString(socket_error,
3390 "illegal IP address string passed to inet_aton");
3391 return NULL;
3392
Thomas Wouters477c8d52006-05-27 19:21:47 +00003393#ifdef USE_INET_ATON_WEAKLINK
3394 } else {
3395#endif
3396
3397#endif
3398
3399#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
3400
Georg Brandld2e3ba72005-08-26 08:34:00 +00003401 /* special-case this address as inet_addr might return INADDR_NONE
3402 * for this */
3403 if (strcmp(ip_addr, "255.255.255.255") == 0) {
3404 packed_addr = 0xFFFFFFFF;
3405 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00003406
Georg Brandld2e3ba72005-08-26 08:34:00 +00003407 packed_addr = inet_addr(ip_addr);
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003408
Georg Brandld2e3ba72005-08-26 08:34:00 +00003409 if (packed_addr == INADDR_NONE) { /* invalid address */
3410 PyErr_SetString(socket_error,
3411 "illegal IP address string passed to inet_aton");
3412 return NULL;
3413 }
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003414 }
Guido van Rossum7d0a8262007-05-21 23:13:11 +00003415 return PyBytes_FromStringAndSize((char *) &packed_addr,
3416 sizeof(packed_addr));
Thomas Wouters477c8d52006-05-27 19:21:47 +00003417
3418#ifdef USE_INET_ATON_WEAKLINK
3419 }
3420#endif
3421
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003422#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003423}
3424
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003425PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00003426"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003427\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003428Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003429
3430static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003431socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003432{
3433 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003434 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003435 struct in_addr packed_addr;
3436
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003437 if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003438 return NULL;
3439 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00003440
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003441 if (addr_len != sizeof(packed_addr)) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003442 PyErr_SetString(socket_error,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003443 "packed IP wrong length for inet_ntoa");
3444 return NULL;
3445 }
3446
3447 memcpy(&packed_addr, packed_str, addr_len);
3448
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003449 return PyUnicode_FromString(inet_ntoa(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003450}
Guido van Rossum82a5c661998-07-07 20:45:43 +00003451
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003452#ifdef HAVE_INET_PTON
3453
3454PyDoc_STRVAR(inet_pton_doc,
3455"inet_pton(af, ip) -> packed IP address string\n\
3456\n\
3457Convert an IP address from string format to a packed string suitable\n\
3458for use with low-level network functions.");
3459
3460static PyObject *
3461socket_inet_pton(PyObject *self, PyObject *args)
3462{
3463 int af;
3464 char* ip;
3465 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003466#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003467 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003468#else
3469 char packed[sizeof(struct in_addr)];
3470#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003471 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
3472 return NULL;
3473 }
3474
Martin v. Löwis04697e82004-06-02 12:35:29 +00003475#if !defined(ENABLE_IPV6) && defined(AF_INET6)
Martin v. Löwis10649092003-08-05 06:25:06 +00003476 if(af == AF_INET6) {
3477 PyErr_SetString(socket_error,
3478 "can't use AF_INET6, IPv6 is disabled");
3479 return NULL;
3480 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003481#endif
Martin v. Löwis10649092003-08-05 06:25:06 +00003482
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003483 retval = inet_pton(af, ip, packed);
3484 if (retval < 0) {
3485 PyErr_SetFromErrno(socket_error);
3486 return NULL;
3487 } else if (retval == 0) {
3488 PyErr_SetString(socket_error,
3489 "illegal IP address string passed to inet_pton");
3490 return NULL;
3491 } else if (af == AF_INET) {
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003492 return PyBytes_FromStringAndSize(packed,
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003493 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003494#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003495 } else if (af == AF_INET6) {
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003496 return PyBytes_FromStringAndSize(packed,
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003497 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003498#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003499 } else {
3500 PyErr_SetString(socket_error, "unknown address family");
3501 return NULL;
3502 }
3503}
Thomas Wouters477c8d52006-05-27 19:21:47 +00003504
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003505PyDoc_STRVAR(inet_ntop_doc,
3506"inet_ntop(af, packed_ip) -> string formatted IP address\n\
3507\n\
3508Convert a packed IP address of the given family to string format.");
3509
3510static PyObject *
3511socket_inet_ntop(PyObject *self, PyObject *args)
3512{
3513 int af;
3514 char* packed;
3515 int len;
3516 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003517#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003518 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003519#else
3520 char ip[INET_ADDRSTRLEN + 1];
3521#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00003522
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003523 /* Guarantee NUL-termination for PyUnicode_FromString() below */
Jeremy Hylton80961f32004-11-07 14:24:25 +00003524 memset((void *) &ip[0], '\0', sizeof(ip));
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003525
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003526 if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) {
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003527 return NULL;
3528 }
3529
3530 if (af == AF_INET) {
3531 if (len != sizeof(struct in_addr)) {
3532 PyErr_SetString(PyExc_ValueError,
3533 "invalid length of packed IP address string");
3534 return NULL;
3535 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003536#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003537 } else if (af == AF_INET6) {
3538 if (len != sizeof(struct in6_addr)) {
3539 PyErr_SetString(PyExc_ValueError,
3540 "invalid length of packed IP address string");
3541 return NULL;
3542 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003543#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003544 } else {
3545 PyErr_Format(PyExc_ValueError,
3546 "unknown address family %d", af);
3547 return NULL;
3548 }
3549
3550 retval = inet_ntop(af, packed, ip, sizeof(ip));
3551 if (!retval) {
3552 PyErr_SetFromErrno(socket_error);
3553 return NULL;
3554 } else {
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003555 return PyUnicode_FromString(retval);
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003556 }
3557
3558 /* NOTREACHED */
3559 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
3560 return NULL;
3561}
3562
3563#endif /* HAVE_INET_PTON */
3564
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003565/* Python interface to getaddrinfo(host, port). */
3566
3567/*ARGSUSED*/
3568static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003569socket_getaddrinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003570{
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003571 struct addrinfo hints, *res;
3572 struct addrinfo *res0 = NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003573 PyObject *hobj = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003574 PyObject *pobj = (PyObject *)NULL;
Martin v. Löwisf65b1a12001-08-12 09:28:40 +00003575 char pbuf[30];
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003576 char *hptr, *pptr;
3577 int family, socktype, protocol, flags;
3578 int error;
3579 PyObject *all = (PyObject *)NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003580 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003581
3582 family = socktype = protocol = flags = 0;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003583 family = AF_UNSPEC;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003584 if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
3585 &hobj, &pobj, &family, &socktype,
3586 &protocol, &flags)) {
3587 return NULL;
3588 }
3589 if (hobj == Py_None) {
3590 hptr = NULL;
3591 } else if (PyUnicode_Check(hobj)) {
3592 idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
3593 if (!idna)
3594 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00003595 assert(PyString_Check(idna));
3596 hptr = PyString_AS_STRING(idna);
Martin v. Löwis2548c732003-04-18 10:39:54 +00003597 } else if (PyString_Check(hobj)) {
3598 hptr = PyString_AsString(hobj);
3599 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00003600 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2548c732003-04-18 10:39:54 +00003601 "getaddrinfo() argument 1 must be string or None");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003602 return NULL;
3603 }
Guido van Rossumddefaf32007-01-14 03:31:43 +00003604 if (PyInt_CheckExact(pobj)) {
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003605 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003606 pptr = pbuf;
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003607 } else if (PyUnicode_Check(pobj)) {
3608 pptr = PyUnicode_AsString(pobj);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003609 } else if (PyString_Check(pobj)) {
3610 pptr = PyString_AsString(pobj);
3611 } else if (pobj == Py_None) {
3612 pptr = (char *)NULL;
3613 } else {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003614 PyErr_SetString(socket_error, "Int or String expected");
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003615 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003616 }
3617 memset(&hints, 0, sizeof(hints));
3618 hints.ai_family = family;
3619 hints.ai_socktype = socktype;
3620 hints.ai_protocol = protocol;
3621 hints.ai_flags = flags;
Just van Rossum1040d2c2003-05-09 07:53:18 +00003622 Py_BEGIN_ALLOW_THREADS
3623 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003624 error = getaddrinfo(hptr, pptr, &hints, &res0);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003625 Py_END_ALLOW_THREADS
3626 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003627 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003628 set_gaierror(error);
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003629 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003630 }
3631
3632 if ((all = PyList_New(0)) == NULL)
3633 goto err;
3634 for (res = res0; res; res = res->ai_next) {
Guido van Rossum9ce8e382007-08-09 21:06:37 +00003635 PyObject *single;
Guido van Rossum716aac02001-10-12 18:59:27 +00003636 PyObject *addr =
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00003637 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
Guido van Rossum716aac02001-10-12 18:59:27 +00003638 if (addr == NULL)
3639 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003640 single = Py_BuildValue("iiisO", res->ai_family,
3641 res->ai_socktype, res->ai_protocol,
3642 res->ai_canonname ? res->ai_canonname : "",
Guido van Rossum716aac02001-10-12 18:59:27 +00003643 addr);
3644 Py_DECREF(addr);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003645 if (single == NULL)
3646 goto err;
3647
3648 if (PyList_Append(all, single))
3649 goto err;
3650 Py_XDECREF(single);
3651 }
Martin v. Löwis2548c732003-04-18 10:39:54 +00003652 Py_XDECREF(idna);
Neal Norwitz90128ba2002-08-09 03:37:42 +00003653 if (res0)
3654 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003655 return all;
3656 err:
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003657 Py_XDECREF(all);
Martin v. Löwis2548c732003-04-18 10:39:54 +00003658 Py_XDECREF(idna);
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003659 if (res0)
3660 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003661 return (PyObject *)NULL;
3662}
3663
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003664PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003665"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
3666 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003667\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003668Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003669
3670/* Python interface to getnameinfo(sa, flags). */
3671
3672/*ARGSUSED*/
3673static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003674socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003675{
3676 PyObject *sa = (PyObject *)NULL;
3677 int flags;
3678 char *hostp;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003679 int port, flowinfo, scope_id;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003680 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
3681 struct addrinfo hints, *res = NULL;
3682 int error;
3683 PyObject *ret = (PyObject *)NULL;
3684
3685 flags = flowinfo = scope_id = 0;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003686 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003687 return NULL;
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003688 if (!PyArg_ParseTuple(sa, "si|ii",
3689 &hostp, &port, &flowinfo, &scope_id))
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003690 return NULL;
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003691 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003692 memset(&hints, 0, sizeof(hints));
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003693 hints.ai_family = AF_UNSPEC;
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003694 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Just van Rossum1040d2c2003-05-09 07:53:18 +00003695 Py_BEGIN_ALLOW_THREADS
3696 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003697 error = getaddrinfo(hostp, pbuf, &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003698 Py_END_ALLOW_THREADS
3699 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003700 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003701 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003702 goto fail;
3703 }
3704 if (res->ai_next) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003705 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003706 "sockaddr resolved to multiple addresses");
3707 goto fail;
3708 }
3709 switch (res->ai_family) {
3710 case AF_INET:
3711 {
3712 char *t1;
3713 int t2;
3714 if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003715 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003716 "IPv4 sockaddr must be 2 tuple");
3717 goto fail;
3718 }
3719 break;
3720 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003721#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003722 case AF_INET6:
3723 {
3724 struct sockaddr_in6 *sin6;
3725 sin6 = (struct sockaddr_in6 *)res->ai_addr;
3726 sin6->sin6_flowinfo = flowinfo;
3727 sin6->sin6_scope_id = scope_id;
3728 break;
3729 }
3730#endif
3731 }
3732 error = getnameinfo(res->ai_addr, res->ai_addrlen,
3733 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
3734 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003735 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003736 goto fail;
3737 }
3738 ret = Py_BuildValue("ss", hbuf, pbuf);
3739
3740fail:
3741 if (res)
3742 freeaddrinfo(res);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003743 return ret;
3744}
3745
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003746PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003747"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003748\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003749Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003750
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003751
3752/* Python API to getting and setting the default timeout value. */
3753
3754static PyObject *
3755socket_getdefaulttimeout(PyObject *self)
3756{
3757 if (defaulttimeout < 0.0) {
3758 Py_INCREF(Py_None);
3759 return Py_None;
3760 }
3761 else
3762 return PyFloat_FromDouble(defaulttimeout);
3763}
3764
3765PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003766"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003767\n\
3768Returns the default timeout in floating seconds for new socket objects.\n\
3769A value of None indicates that new socket objects have no timeout.\n\
3770When the socket module is first imported, the default is None.");
3771
3772static PyObject *
3773socket_setdefaulttimeout(PyObject *self, PyObject *arg)
3774{
3775 double timeout;
3776
3777 if (arg == Py_None)
3778 timeout = -1.0;
3779 else {
3780 timeout = PyFloat_AsDouble(arg);
3781 if (timeout < 0.0) {
3782 if (!PyErr_Occurred())
3783 PyErr_SetString(PyExc_ValueError,
3784 "Timeout value out of range");
3785 return NULL;
3786 }
3787 }
3788
3789 defaulttimeout = timeout;
3790
3791 Py_INCREF(Py_None);
3792 return Py_None;
3793}
3794
3795PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003796"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003797\n\
3798Set the default timeout in floating seconds for new socket objects.\n\
3799A value of None indicates that new socket objects have no timeout.\n\
3800When the socket module is first imported, the default is None.");
3801
3802
Guido van Rossum30a685f1991-06-27 15:51:29 +00003803/* List of functions exported by this module. */
3804
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003805static PyMethodDef socket_methods[] = {
3806 {"gethostbyname", socket_gethostbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003807 METH_VARARGS, gethostbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003808 {"gethostbyname_ex", socket_gethostbyname_ex,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003809 METH_VARARGS, ghbn_ex_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003810 {"gethostbyaddr", socket_gethostbyaddr,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003811 METH_VARARGS, gethostbyaddr_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003812 {"gethostname", socket_gethostname,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003813 METH_NOARGS, gethostname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003814 {"getservbyname", socket_getservbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003815 METH_VARARGS, getservbyname_doc},
Barry Warsaw11b91a02004-06-28 00:50:43 +00003816 {"getservbyport", socket_getservbyport,
3817 METH_VARARGS, getservbyport_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003818 {"getprotobyname", socket_getprotobyname,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003819 METH_VARARGS, getprotobyname_doc},
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00003820#ifndef NO_DUP
3821 {"dup", socket_dup,
3822 METH_O, dup_doc},
3823#endif
Dave Cole331708b2004-08-09 04:51:41 +00003824#ifdef HAVE_SOCKETPAIR
3825 {"socketpair", socket_socketpair,
3826 METH_VARARGS, socketpair_doc},
3827#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003828 {"ntohs", socket_ntohs,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003829 METH_VARARGS, ntohs_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003830 {"ntohl", socket_ntohl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003831 METH_O, ntohl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003832 {"htons", socket_htons,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003833 METH_VARARGS, htons_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003834 {"htonl", socket_htonl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003835 METH_O, htonl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003836 {"inet_aton", socket_inet_aton,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003837 METH_VARARGS, inet_aton_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003838 {"inet_ntoa", socket_inet_ntoa,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003839 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003840#ifdef HAVE_INET_PTON
3841 {"inet_pton", socket_inet_pton,
3842 METH_VARARGS, inet_pton_doc},
3843 {"inet_ntop", socket_inet_ntop,
3844 METH_VARARGS, inet_ntop_doc},
3845#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003846 {"getaddrinfo", socket_getaddrinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003847 METH_VARARGS, getaddrinfo_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003848 {"getnameinfo", socket_getnameinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003849 METH_VARARGS, getnameinfo_doc},
Guido van Rossum1693ba82002-07-18 21:11:26 +00003850 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003851 METH_NOARGS, getdefaulttimeout_doc},
3852 {"setdefaulttimeout", socket_setdefaulttimeout,
3853 METH_O, setdefaulttimeout_doc},
Guido van Rossum30a685f1991-06-27 15:51:29 +00003854 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003855};
3856
Guido van Rossum30a685f1991-06-27 15:51:29 +00003857
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003858#ifdef MS_WINDOWS
3859#define OS_INIT_DEFINED
3860
3861/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003862
3863static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003864os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00003865{
3866 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00003867}
3868
3869static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003870os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00003871{
3872 WSADATA WSAData;
3873 int ret;
Guido van Rossumbe32c891996-06-20 16:25:29 +00003874 ret = WSAStartup(0x0101, &WSAData);
3875 switch (ret) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003876 case 0: /* No error */
Mark Hammond14350ab2003-07-09 04:57:46 +00003877 Py_AtExit(os_cleanup);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003878 return 1; /* Success */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003879 case WSASYSNOTREADY:
3880 PyErr_SetString(PyExc_ImportError,
3881 "WSAStartup failed: network not ready");
3882 break;
3883 case WSAVERNOTSUPPORTED:
3884 case WSAEINVAL:
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003885 PyErr_SetString(
3886 PyExc_ImportError,
3887 "WSAStartup failed: requested version not supported");
Guido van Rossumbe32c891996-06-20 16:25:29 +00003888 break;
3889 default:
Walter Dörwald0bbd8ab2007-06-05 13:49:43 +00003890 PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret);
Guido van Rossumbe32c891996-06-20 16:25:29 +00003891 break;
3892 }
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003893 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003894}
3895
Guido van Rossum8d665e61996-06-26 18:22:49 +00003896#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003897
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003898
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003899#ifdef PYOS_OS2
3900#define OS_INIT_DEFINED
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003901
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003902/* Additional initialization for OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003903
3904static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003905os_init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003906{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003907#ifndef PYCC_GCC
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003908 int rc = sock_init();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003909
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003910 if (rc == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003911 return 1; /* Success */
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003912 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003913
Walter Dörwald0bbd8ab2007-06-05 13:49:43 +00003914 PyErr_Format(PyExc_ImportError, "OS/2 TCP/IP Error# %d", sock_errno());
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003915
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003916 return 0; /* Failure */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00003917#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003918 /* No need to initialise sockets with GCC/EMX */
3919 return 1; /* Success */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00003920#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003921}
3922
3923#endif /* PYOS_OS2 */
3924
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003925
3926#ifndef OS_INIT_DEFINED
3927static int
3928os_init(void)
3929{
3930 return 1; /* Success */
3931}
3932#endif
3933
3934
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003935/* C API table - always add new things to the end for binary
3936 compatibility. */
3937static
3938PySocketModule_APIObject PySocketModuleAPI =
3939{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003940 &sock_type,
Brett Cannon06c34792004-03-23 23:16:54 +00003941 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003942};
3943
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003944
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003945/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003946
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003947 This module is actually called "_socket", and there's a wrapper
Guido van Rossum7d0a8262007-05-21 23:13:11 +00003948 "socket.py" which implements some additional functionality.
3949 The import of "_socket" may fail with an ImportError exception if
3950 os-specific initialization fails. On Windows, this does WINSOCK
3951 initialization. When WINSOCK is initialized succesfully, a call to
3952 WSACleanup() is scheduled to be made at exit time.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003953*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003954
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003955PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003956"Implementation module for socket operations.\n\
3957\n\
3958See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003959
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003960PyMODINIT_FUNC
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003961init_socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003962{
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003963 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00003964
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003965 if (!os_init())
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003966 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003967
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003968 Py_Type(&sock_type) = &PyType_Type;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003969 m = Py_InitModule3(PySocket_MODULE_NAME,
3970 socket_methods,
3971 socket_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003972 if (m == NULL)
3973 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003974
Gregory P. Smithc111d9f2007-09-09 23:55:55 +00003975 socket_error = PyErr_NewException("socket.error",
3976 PyExc_IOError, NULL);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003977 if (socket_error == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003978 return;
Brett Cannon06c34792004-03-23 23:16:54 +00003979 PySocketModuleAPI.error = socket_error;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003980 Py_INCREF(socket_error);
3981 PyModule_AddObject(m, "error", socket_error);
3982 socket_herror = PyErr_NewException("socket.herror",
3983 socket_error, NULL);
3984 if (socket_herror == NULL)
3985 return;
3986 Py_INCREF(socket_herror);
3987 PyModule_AddObject(m, "herror", socket_herror);
3988 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003989 NULL);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003990 if (socket_gaierror == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003991 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003992 Py_INCREF(socket_gaierror);
3993 PyModule_AddObject(m, "gaierror", socket_gaierror);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00003994 socket_timeout = PyErr_NewException("socket.timeout",
3995 socket_error, NULL);
3996 if (socket_timeout == NULL)
3997 return;
3998 Py_INCREF(socket_timeout);
3999 PyModule_AddObject(m, "timeout", socket_timeout);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004000 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00004001 if (PyModule_AddObject(m, "SocketType",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004002 (PyObject *)&sock_type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004003 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004004 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00004005 if (PyModule_AddObject(m, "socket",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004006 (PyObject *)&sock_type) != 0)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004007 return;
Guido van Rossum09be4091999-08-09 14:40:40 +00004008
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004009#ifdef ENABLE_IPV6
4010 has_ipv6 = Py_True;
4011#else
4012 has_ipv6 = Py_False;
4013#endif
4014 Py_INCREF(has_ipv6);
4015 PyModule_AddObject(m, "has_ipv6", has_ipv6);
4016
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004017 /* Export C API */
Fred Drake4baedc12002-04-01 14:53:37 +00004018 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004019 PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL)
4020 ) != 0)
4021 return;
4022
Guido van Rossum09be4091999-08-09 14:40:40 +00004023 /* Address families (we only support AF_INET and AF_UNIX) */
4024#ifdef AF_UNSPEC
Fred Drake4baedc12002-04-01 14:53:37 +00004025 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00004026#endif
Fred Drake4baedc12002-04-01 14:53:37 +00004027 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004028#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00004029 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004030#endif /* AF_INET6 */
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00004031#if defined(AF_UNIX)
Fred Drake4baedc12002-04-01 14:53:37 +00004032 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004033#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00004034#ifdef AF_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00004035 /* Amateur Radio AX.25 */
4036 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004037#endif
4038#ifdef AF_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00004039 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00004040#endif
4041#ifdef AF_APPLETALK
Fred Drake4baedc12002-04-01 14:53:37 +00004042 /* Appletalk DDP */
4043 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004044#endif
4045#ifdef AF_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00004046 /* Amateur radio NetROM */
4047 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004048#endif
4049#ifdef AF_BRIDGE
Fred Drake4baedc12002-04-01 14:53:37 +00004050 /* Multiprotocol bridge */
4051 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004052#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004053#ifdef AF_ATMPVC
4054 /* ATM PVCs */
4055 PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC);
4056#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004057#ifdef AF_AAL5
Fred Drake4baedc12002-04-01 14:53:37 +00004058 /* Reserved for Werner's ATM */
4059 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00004060#endif
4061#ifdef AF_X25
Fred Drake4baedc12002-04-01 14:53:37 +00004062 /* Reserved for X.25 project */
4063 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004064#endif
4065#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00004066 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00004067#endif
4068#ifdef AF_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00004069 /* Amateur Radio X.25 PLP */
4070 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004071#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004072#ifdef AF_DECnet
4073 /* Reserved for DECnet project */
4074 PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet);
4075#endif
4076#ifdef AF_NETBEUI
4077 /* Reserved for 802.2LLC project */
4078 PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI);
4079#endif
4080#ifdef AF_SECURITY
4081 /* Security callback pseudo AF */
4082 PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY);
4083#endif
4084#ifdef AF_KEY
4085 /* PF_KEY key management API */
4086 PyModule_AddIntConstant(m, "AF_KEY", AF_KEY);
4087#endif
4088#ifdef AF_NETLINK
4089 /* */
4090 PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);
Martin v. Löwis11017b12006-01-14 18:12:57 +00004091 PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004092#ifdef NETLINK_SKIP
Martin v. Löwis11017b12006-01-14 18:12:57 +00004093 PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004094#endif
4095#ifdef NETLINK_W1
4096 PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1);
4097#endif
Martin v. Löwis11017b12006-01-14 18:12:57 +00004098 PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK);
4099 PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004100#ifdef NETLINK_TCPDIAG
Martin v. Löwis11017b12006-01-14 18:12:57 +00004101 PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004102#endif
4103#ifdef NETLINK_NFLOG
Martin v. Löwis11017b12006-01-14 18:12:57 +00004104 PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004105#endif
Neal Norwitz65851662006-01-16 04:31:40 +00004106#ifdef NETLINK_XFRM
Martin v. Löwis11017b12006-01-14 18:12:57 +00004107 PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM);
Neal Norwitz65851662006-01-16 04:31:40 +00004108#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004109#ifdef NETLINK_ARPD
Martin v. Löwis11017b12006-01-14 18:12:57 +00004110 PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004111#endif
4112#ifdef NETLINK_ROUTE6
Martin v. Löwis11017b12006-01-14 18:12:57 +00004113 PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004114#endif
Martin v. Löwis11017b12006-01-14 18:12:57 +00004115 PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW);
Thomas Wouterscf297e42007-02-23 15:07:44 +00004116#ifdef NETLINK_DNRTMSG
Martin v. Löwis11017b12006-01-14 18:12:57 +00004117 PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG);
Guido van Rossum39eb8fa2007-11-16 01:24:05 +00004118#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004119#ifdef NETLINK_TAPBASE
Martin v. Löwis11017b12006-01-14 18:12:57 +00004120 PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004121#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004122#endif /* AF_NETLINK */
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004123#ifdef AF_ROUTE
4124 /* Alias to emulate 4.4BSD */
4125 PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE);
4126#endif
4127#ifdef AF_ASH
4128 /* Ash */
4129 PyModule_AddIntConstant(m, "AF_ASH", AF_ASH);
4130#endif
4131#ifdef AF_ECONET
4132 /* Acorn Econet */
4133 PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET);
4134#endif
4135#ifdef AF_ATMSVC
4136 /* ATM SVCs */
4137 PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC);
4138#endif
4139#ifdef AF_SNA
4140 /* Linux SNA Project (nutters!) */
4141 PyModule_AddIntConstant(m, "AF_SNA", AF_SNA);
4142#endif
4143#ifdef AF_IRDA
4144 /* IRDA sockets */
4145 PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA);
4146#endif
4147#ifdef AF_PPPOX
4148 /* PPPoX sockets */
4149 PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX);
4150#endif
4151#ifdef AF_WANPIPE
4152 /* Wanpipe API Sockets */
4153 PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE);
4154#endif
4155#ifdef AF_LLC
4156 /* Linux LLC */
4157 PyModule_AddIntConstant(m, "AF_LLC", AF_LLC);
4158#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00004159
Hye-Shik Chang81268602004-02-02 06:05:24 +00004160#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00004161 PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);
4162 PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);
Thomas Wouterscf297e42007-02-23 15:07:44 +00004163 PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI);
4164 PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI);
Thomas Wouterscf297e42007-02-23 15:07:44 +00004165 PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER);
Hye-Shik Chang81268602004-02-02 06:05:24 +00004166#if !defined(__FreeBSD__)
Guido van Rossumcd16bf62007-06-13 18:07:49 +00004167 PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP);
4168 PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR);
Martin v. Löwis12af0482004-01-31 12:34:17 +00004169 PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00004170#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00004171 PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004172 PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
4173 PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
Martin v. Löwis12af0482004-01-31 12:34:17 +00004174#endif
4175
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00004176#ifdef HAVE_NETPACKET_PACKET_H
Fred Drake4baedc12002-04-01 14:53:37 +00004177 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
4178 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
4179 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
4180 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
4181 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
4182 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
4183 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
4184 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
4185 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00004186#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004187
4188 /* Socket types */
Fred Drake4baedc12002-04-01 14:53:37 +00004189 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
4190 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004191/* We have incomplete socket support. */
Fred Drake4baedc12002-04-01 14:53:37 +00004192 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
4193 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00004194#if defined(SOCK_RDM)
Fred Drake4baedc12002-04-01 14:53:37 +00004195 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004196#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004197
4198#ifdef SO_DEBUG
Fred Drake4baedc12002-04-01 14:53:37 +00004199 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004200#endif
4201#ifdef SO_ACCEPTCONN
Fred Drake4baedc12002-04-01 14:53:37 +00004202 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004203#endif
4204#ifdef SO_REUSEADDR
Fred Drake4baedc12002-04-01 14:53:37 +00004205 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004206#endif
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00004207#ifdef SO_EXCLUSIVEADDRUSE
4208 PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE);
4209#endif
4210
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004211#ifdef SO_KEEPALIVE
Fred Drake4baedc12002-04-01 14:53:37 +00004212 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004213#endif
4214#ifdef SO_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00004215 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004216#endif
4217#ifdef SO_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00004218 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004219#endif
4220#ifdef SO_USELOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00004221 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004222#endif
4223#ifdef SO_LINGER
Fred Drake4baedc12002-04-01 14:53:37 +00004224 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004225#endif
4226#ifdef SO_OOBINLINE
Fred Drake4baedc12002-04-01 14:53:37 +00004227 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004228#endif
4229#ifdef SO_REUSEPORT
Fred Drake4baedc12002-04-01 14:53:37 +00004230 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004231#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004232#ifdef SO_SNDBUF
Fred Drake4baedc12002-04-01 14:53:37 +00004233 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004234#endif
4235#ifdef SO_RCVBUF
Fred Drake4baedc12002-04-01 14:53:37 +00004236 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004237#endif
4238#ifdef SO_SNDLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00004239 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004240#endif
4241#ifdef SO_RCVLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00004242 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004243#endif
4244#ifdef SO_SNDTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00004245 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004246#endif
4247#ifdef SO_RCVTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00004248 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004249#endif
4250#ifdef SO_ERROR
Fred Drake4baedc12002-04-01 14:53:37 +00004251 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004252#endif
4253#ifdef SO_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004254 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004255#endif
4256
4257 /* Maximum number of connections for "listen" */
4258#ifdef SOMAXCONN
Fred Drake4baedc12002-04-01 14:53:37 +00004259 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004260#else
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004261 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004262#endif
4263
4264 /* Flags for send, recv */
4265#ifdef MSG_OOB
Fred Drake4baedc12002-04-01 14:53:37 +00004266 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004267#endif
4268#ifdef MSG_PEEK
Fred Drake4baedc12002-04-01 14:53:37 +00004269 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004270#endif
4271#ifdef MSG_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00004272 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004273#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00004274#ifdef MSG_DONTWAIT
Fred Drake4baedc12002-04-01 14:53:37 +00004275 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00004276#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004277#ifdef MSG_EOR
Fred Drake4baedc12002-04-01 14:53:37 +00004278 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004279#endif
4280#ifdef MSG_TRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00004281 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004282#endif
4283#ifdef MSG_CTRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00004284 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004285#endif
4286#ifdef MSG_WAITALL
Fred Drake4baedc12002-04-01 14:53:37 +00004287 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004288#endif
4289#ifdef MSG_BTAG
Fred Drake4baedc12002-04-01 14:53:37 +00004290 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004291#endif
4292#ifdef MSG_ETAG
Fred Drake4baedc12002-04-01 14:53:37 +00004293 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004294#endif
4295
4296 /* Protocol level and numbers, usable for [gs]etsockopt */
4297#ifdef SOL_SOCKET
Fred Drake4baedc12002-04-01 14:53:37 +00004298 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004299#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004300#ifdef SOL_IP
Fred Drake4baedc12002-04-01 14:53:37 +00004301 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004302#else
Fred Drake4baedc12002-04-01 14:53:37 +00004303 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00004304#endif
4305#ifdef SOL_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00004306 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00004307#endif
4308#ifdef SOL_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00004309 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004310#endif
4311#ifdef SOL_ATALK
Fred Drake4baedc12002-04-01 14:53:37 +00004312 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004313#endif
4314#ifdef SOL_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00004315 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004316#endif
4317#ifdef SOL_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00004318 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004319#endif
4320#ifdef SOL_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00004321 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004322#else
Fred Drake4baedc12002-04-01 14:53:37 +00004323 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00004324#endif
4325#ifdef SOL_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00004326 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004327#else
Fred Drake4baedc12002-04-01 14:53:37 +00004328 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00004329#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004330#ifdef IPPROTO_IP
Fred Drake4baedc12002-04-01 14:53:37 +00004331 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00004332#else
Fred Drake4baedc12002-04-01 14:53:37 +00004333 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004334#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004335#ifdef IPPROTO_HOPOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004336 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004337#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004338#ifdef IPPROTO_ICMP
Fred Drake4baedc12002-04-01 14:53:37 +00004339 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00004340#else
Fred Drake4baedc12002-04-01 14:53:37 +00004341 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004342#endif
4343#ifdef IPPROTO_IGMP
Fred Drake4baedc12002-04-01 14:53:37 +00004344 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004345#endif
4346#ifdef IPPROTO_GGP
Fred Drake4baedc12002-04-01 14:53:37 +00004347 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004348#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004349#ifdef IPPROTO_IPV4
Fred Drake4baedc12002-04-01 14:53:37 +00004350 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004351#endif
Martin v. Löwisa0f17342003-10-03 13:56:20 +00004352#ifdef IPPROTO_IPV6
4353 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
4354#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004355#ifdef IPPROTO_IPIP
Fred Drake4baedc12002-04-01 14:53:37 +00004356 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004357#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004358#ifdef IPPROTO_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00004359 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00004360#else
Fred Drake4baedc12002-04-01 14:53:37 +00004361 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004362#endif
4363#ifdef IPPROTO_EGP
Fred Drake4baedc12002-04-01 14:53:37 +00004364 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004365#endif
4366#ifdef IPPROTO_PUP
Fred Drake4baedc12002-04-01 14:53:37 +00004367 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004368#endif
4369#ifdef IPPROTO_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00004370 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00004371#else
Fred Drake4baedc12002-04-01 14:53:37 +00004372 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004373#endif
4374#ifdef IPPROTO_IDP
Fred Drake4baedc12002-04-01 14:53:37 +00004375 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004376#endif
4377#ifdef IPPROTO_HELLO
Fred Drake4baedc12002-04-01 14:53:37 +00004378 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004379#endif
4380#ifdef IPPROTO_ND
Fred Drake4baedc12002-04-01 14:53:37 +00004381 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004382#endif
4383#ifdef IPPROTO_TP
Fred Drake4baedc12002-04-01 14:53:37 +00004384 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004385#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004386#ifdef IPPROTO_IPV6
Fred Drake4baedc12002-04-01 14:53:37 +00004387 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004388#endif
4389#ifdef IPPROTO_ROUTING
Fred Drake4baedc12002-04-01 14:53:37 +00004390 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004391#endif
4392#ifdef IPPROTO_FRAGMENT
Fred Drake4baedc12002-04-01 14:53:37 +00004393 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004394#endif
4395#ifdef IPPROTO_RSVP
Fred Drake4baedc12002-04-01 14:53:37 +00004396 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004397#endif
4398#ifdef IPPROTO_GRE
Fred Drake4baedc12002-04-01 14:53:37 +00004399 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004400#endif
4401#ifdef IPPROTO_ESP
Fred Drake4baedc12002-04-01 14:53:37 +00004402 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004403#endif
4404#ifdef IPPROTO_AH
Fred Drake4baedc12002-04-01 14:53:37 +00004405 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004406#endif
4407#ifdef IPPROTO_MOBILE
Fred Drake4baedc12002-04-01 14:53:37 +00004408 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004409#endif
4410#ifdef IPPROTO_ICMPV6
Fred Drake4baedc12002-04-01 14:53:37 +00004411 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004412#endif
4413#ifdef IPPROTO_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00004414 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004415#endif
4416#ifdef IPPROTO_DSTOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004417 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004418#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004419#ifdef IPPROTO_XTP
Fred Drake4baedc12002-04-01 14:53:37 +00004420 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004421#endif
4422#ifdef IPPROTO_EON
Fred Drake4baedc12002-04-01 14:53:37 +00004423 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004424#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004425#ifdef IPPROTO_PIM
Fred Drake4baedc12002-04-01 14:53:37 +00004426 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004427#endif
4428#ifdef IPPROTO_IPCOMP
Fred Drake4baedc12002-04-01 14:53:37 +00004429 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004430#endif
4431#ifdef IPPROTO_VRRP
Fred Drake4baedc12002-04-01 14:53:37 +00004432 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004433#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004434#ifdef IPPROTO_BIP
Fred Drake4baedc12002-04-01 14:53:37 +00004435 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004436#endif
4437/**/
4438#ifdef IPPROTO_RAW
Fred Drake4baedc12002-04-01 14:53:37 +00004439 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00004440#else
Fred Drake4baedc12002-04-01 14:53:37 +00004441 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004442#endif
4443#ifdef IPPROTO_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00004444 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004445#endif
4446
4447 /* Some port configuration */
4448#ifdef IPPORT_RESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00004449 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004450#else
Fred Drake4baedc12002-04-01 14:53:37 +00004451 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004452#endif
4453#ifdef IPPORT_USERRESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00004454 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004455#else
Fred Drake4baedc12002-04-01 14:53:37 +00004456 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004457#endif
4458
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004459 /* Some reserved IP v.4 addresses */
4460#ifdef INADDR_ANY
Fred Drake4baedc12002-04-01 14:53:37 +00004461 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004462#else
Fred Drake4baedc12002-04-01 14:53:37 +00004463 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004464#endif
4465#ifdef INADDR_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00004466 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004467#else
Fred Drake4baedc12002-04-01 14:53:37 +00004468 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004469#endif
4470#ifdef INADDR_LOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00004471 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004472#else
Fred Drake4baedc12002-04-01 14:53:37 +00004473 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004474#endif
4475#ifdef INADDR_UNSPEC_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004476 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004477#else
Fred Drake4baedc12002-04-01 14:53:37 +00004478 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004479#endif
4480#ifdef INADDR_ALLHOSTS_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004481 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
4482 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004483#else
Fred Drake4baedc12002-04-01 14:53:37 +00004484 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004485#endif
4486#ifdef INADDR_MAX_LOCAL_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004487 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
4488 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004489#else
Fred Drake4baedc12002-04-01 14:53:37 +00004490 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004491#endif
4492#ifdef INADDR_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00004493 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004494#else
Fred Drake4baedc12002-04-01 14:53:37 +00004495 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004496#endif
4497
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004498 /* IPv4 [gs]etsockopt options */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004499#ifdef IP_OPTIONS
Fred Drake4baedc12002-04-01 14:53:37 +00004500 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004501#endif
4502#ifdef IP_HDRINCL
Fred Drake4baedc12002-04-01 14:53:37 +00004503 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004504#endif
4505#ifdef IP_TOS
Fred Drake4baedc12002-04-01 14:53:37 +00004506 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004507#endif
4508#ifdef IP_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004509 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004510#endif
4511#ifdef IP_RECVOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004512 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004513#endif
4514#ifdef IP_RECVRETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004515 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004516#endif
4517#ifdef IP_RECVDSTADDR
Fred Drake4baedc12002-04-01 14:53:37 +00004518 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004519#endif
4520#ifdef IP_RETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004521 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004522#endif
4523#ifdef IP_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00004524 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004525#endif
4526#ifdef IP_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004527 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004528#endif
4529#ifdef IP_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004530 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004531#endif
4532#ifdef IP_ADD_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00004533 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004534#endif
4535#ifdef IP_DROP_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00004536 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004537#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004538#ifdef IP_DEFAULT_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004539 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
4540 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00004541#endif
4542#ifdef IP_DEFAULT_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004543 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
4544 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004545#endif
4546#ifdef IP_MAX_MEMBERSHIPS
Fred Drake4baedc12002-04-01 14:53:37 +00004547 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00004548#endif
4549
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004550 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
4551#ifdef IPV6_JOIN_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004552 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004553#endif
4554#ifdef IPV6_LEAVE_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004555 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004556#endif
4557#ifdef IPV6_MULTICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00004558 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004559#endif
4560#ifdef IPV6_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00004561 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004562#endif
4563#ifdef IPV6_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004564 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004565#endif
4566#ifdef IPV6_UNICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00004567 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004568#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004569 /* Additional IPV6 socket options, defined in RFC 3493 */
4570#ifdef IPV6_V6ONLY
4571 PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);
4572#endif
4573 /* Advanced IPV6 socket options, from RFC 3542 */
4574#ifdef IPV6_CHECKSUM
4575 PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);
4576#endif
4577#ifdef IPV6_DONTFRAG
4578 PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);
4579#endif
4580#ifdef IPV6_DSTOPTS
4581 PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);
4582#endif
4583#ifdef IPV6_HOPLIMIT
4584 PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);
4585#endif
4586#ifdef IPV6_HOPOPTS
4587 PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);
4588#endif
4589#ifdef IPV6_NEXTHOP
4590 PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);
4591#endif
4592#ifdef IPV6_PATHMTU
4593 PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);
4594#endif
4595#ifdef IPV6_PKTINFO
4596 PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);
4597#endif
4598#ifdef IPV6_RECVDSTOPTS
4599 PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);
4600#endif
4601#ifdef IPV6_RECVHOPLIMIT
4602 PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);
4603#endif
4604#ifdef IPV6_RECVHOPOPTS
4605 PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);
4606#endif
4607#ifdef IPV6_RECVPKTINFO
4608 PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);
4609#endif
4610#ifdef IPV6_RECVRTHDR
4611 PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);
4612#endif
4613#ifdef IPV6_RECVTCLASS
4614 PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);
4615#endif
4616#ifdef IPV6_RTHDR
4617 PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);
4618#endif
4619#ifdef IPV6_RTHDRDSTOPTS
4620 PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);
4621#endif
4622#ifdef IPV6_RTHDR_TYPE_0
4623 PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);
4624#endif
4625#ifdef IPV6_RECVPATHMTU
4626 PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);
4627#endif
4628#ifdef IPV6_TCLASS
4629 PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);
4630#endif
4631#ifdef IPV6_USE_MIN_MTU
4632 PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);
4633#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004634
Guido van Rossum09be4091999-08-09 14:40:40 +00004635 /* TCP options */
4636#ifdef TCP_NODELAY
Fred Drake4baedc12002-04-01 14:53:37 +00004637 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00004638#endif
4639#ifdef TCP_MAXSEG
Fred Drake4baedc12002-04-01 14:53:37 +00004640 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00004641#endif
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004642#ifdef TCP_CORK
Fred Drake4baedc12002-04-01 14:53:37 +00004643 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004644#endif
4645#ifdef TCP_KEEPIDLE
Fred Drake4baedc12002-04-01 14:53:37 +00004646 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004647#endif
4648#ifdef TCP_KEEPINTVL
Fred Drake4baedc12002-04-01 14:53:37 +00004649 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004650#endif
4651#ifdef TCP_KEEPCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004652 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004653#endif
4654#ifdef TCP_SYNCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004655 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004656#endif
4657#ifdef TCP_LINGER2
Fred Drake4baedc12002-04-01 14:53:37 +00004658 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004659#endif
4660#ifdef TCP_DEFER_ACCEPT
Fred Drake4baedc12002-04-01 14:53:37 +00004661 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004662#endif
4663#ifdef TCP_WINDOW_CLAMP
Fred Drake4baedc12002-04-01 14:53:37 +00004664 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004665#endif
4666#ifdef TCP_INFO
Fred Drake4baedc12002-04-01 14:53:37 +00004667 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004668#endif
4669#ifdef TCP_QUICKACK
Fred Drake4baedc12002-04-01 14:53:37 +00004670 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004671#endif
4672
Guido van Rossum09be4091999-08-09 14:40:40 +00004673
4674 /* IPX options */
4675#ifdef IPX_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004676 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004677#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004678
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004679 /* get{addr,name}info parameters */
4680#ifdef EAI_ADDRFAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004681 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004682#endif
4683#ifdef EAI_AGAIN
Fred Drake4baedc12002-04-01 14:53:37 +00004684 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004685#endif
4686#ifdef EAI_BADFLAGS
Fred Drake4baedc12002-04-01 14:53:37 +00004687 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004688#endif
4689#ifdef EAI_FAIL
Fred Drake4baedc12002-04-01 14:53:37 +00004690 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004691#endif
4692#ifdef EAI_FAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004693 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004694#endif
4695#ifdef EAI_MEMORY
Fred Drake4baedc12002-04-01 14:53:37 +00004696 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004697#endif
4698#ifdef EAI_NODATA
Fred Drake4baedc12002-04-01 14:53:37 +00004699 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004700#endif
4701#ifdef EAI_NONAME
Fred Drake4baedc12002-04-01 14:53:37 +00004702 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004703#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004704#ifdef EAI_OVERFLOW
4705 PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);
4706#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004707#ifdef EAI_SERVICE
Fred Drake4baedc12002-04-01 14:53:37 +00004708 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004709#endif
4710#ifdef EAI_SOCKTYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004711 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004712#endif
4713#ifdef EAI_SYSTEM
Fred Drake4baedc12002-04-01 14:53:37 +00004714 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004715#endif
4716#ifdef EAI_BADHINTS
Fred Drake4baedc12002-04-01 14:53:37 +00004717 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004718#endif
4719#ifdef EAI_PROTOCOL
Fred Drake4baedc12002-04-01 14:53:37 +00004720 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004721#endif
4722#ifdef EAI_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00004723 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004724#endif
4725#ifdef AI_PASSIVE
Fred Drake4baedc12002-04-01 14:53:37 +00004726 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004727#endif
4728#ifdef AI_CANONNAME
Fred Drake4baedc12002-04-01 14:53:37 +00004729 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004730#endif
4731#ifdef AI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004732 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004733#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004734#ifdef AI_NUMERICSERV
4735 PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);
4736#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004737#ifdef AI_MASK
Fred Drake4baedc12002-04-01 14:53:37 +00004738 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004739#endif
4740#ifdef AI_ALL
Fred Drake4baedc12002-04-01 14:53:37 +00004741 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004742#endif
4743#ifdef AI_V4MAPPED_CFG
Fred Drake4baedc12002-04-01 14:53:37 +00004744 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004745#endif
4746#ifdef AI_ADDRCONFIG
Fred Drake4baedc12002-04-01 14:53:37 +00004747 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004748#endif
4749#ifdef AI_V4MAPPED
Fred Drake4baedc12002-04-01 14:53:37 +00004750 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004751#endif
4752#ifdef AI_DEFAULT
Fred Drake4baedc12002-04-01 14:53:37 +00004753 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004754#endif
4755#ifdef NI_MAXHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004756 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004757#endif
4758#ifdef NI_MAXSERV
Fred Drake4baedc12002-04-01 14:53:37 +00004759 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004760#endif
4761#ifdef NI_NOFQDN
Fred Drake4baedc12002-04-01 14:53:37 +00004762 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004763#endif
4764#ifdef NI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004765 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004766#endif
4767#ifdef NI_NAMEREQD
Fred Drake4baedc12002-04-01 14:53:37 +00004768 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004769#endif
4770#ifdef NI_NUMERICSERV
Fred Drake4baedc12002-04-01 14:53:37 +00004771 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004772#endif
4773#ifdef NI_DGRAM
Fred Drake4baedc12002-04-01 14:53:37 +00004774 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004775#endif
4776
Martin v. Löwis94681fc2003-11-27 19:40:22 +00004777 /* shutdown() parameters */
4778#ifdef SHUT_RD
4779 PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);
4780#elif defined(SD_RECEIVE)
4781 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
4782#else
4783 PyModule_AddIntConstant(m, "SHUT_RD", 0);
4784#endif
4785#ifdef SHUT_WR
4786 PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);
4787#elif defined(SD_SEND)
4788 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
4789#else
4790 PyModule_AddIntConstant(m, "SHUT_WR", 1);
4791#endif
4792#ifdef SHUT_RDWR
4793 PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);
4794#elif defined(SD_BOTH)
4795 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
4796#else
4797 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
4798#endif
4799
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004800 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00004801#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
4802 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004803#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004804}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004805
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004806
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004807#ifndef HAVE_INET_PTON
Christian Heimesb6150692007-11-15 23:37:07 +00004808#if !(defined(_MSC_VER) && _MSC_VER>1499)
4809/* Don't redefine inet_pton in VS2008 */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004810
4811/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004812/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004813
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004814int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004815inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004816{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004817 if (af == AF_INET) {
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004818 long packed_addr;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004819 packed_addr = inet_addr(src);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004820 if (packed_addr == INADDR_NONE)
4821 return 0;
4822 memcpy(dst, &packed_addr, 4);
4823 return 1;
4824 }
4825 /* Should set errno to EAFNOSUPPORT */
4826 return -1;
4827}
4828
Martin v. Löwisc925b1532001-07-21 09:42:15 +00004829const char *
4830inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004831{
4832 if (af == AF_INET) {
4833 struct in_addr packed_addr;
4834 if (size < 16)
4835 /* Should set errno to ENOSPC. */
4836 return NULL;
4837 memcpy(&packed_addr, src, sizeof(packed_addr));
4838 return strncpy(dst, inet_ntoa(packed_addr), size);
4839 }
4840 /* Should set errno to EAFNOSUPPORT */
4841 return NULL;
4842}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004843
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004844#endif
Christian Heimesb6150692007-11-15 23:37:07 +00004845#endif