blob: 7fe562e71a1465eb184eb8a99268f497a5a357f4 [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\
92accept() -- accept a connection, returning new socket and client address\n\
93bind(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\
97dup() -- return a new socket object identical to the current one [*]\n\
98fileno() -- 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
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000288int inet_pton(int af, const char *src, void *dst);
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000289const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000290#endif
291
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000292#ifdef __APPLE__
293/* On OS X, getaddrinfo returns no error indication of lookup
294 failure, so we must use the emulation instead of the libinfo
295 implementation. Unfortunately, performing an autoconf test
296 for this bug would require DNS access for the machine performing
297 the configuration, which is not acceptable. Therefore, we
298 determine the bug just by checking for __APPLE__. If this bug
299 gets ever fixed, perhaps checking for sys/version.h would be
300 appropriate, which is 10/0 on the system with the bug. */
Jack Jansen84262fb2002-07-02 14:40:42 +0000301#ifndef HAVE_GETNAMEINFO
302/* This bug seems to be fixed in Jaguar. Ths easiest way I could
303 Find to check for Jaguar is that it has getnameinfo(), which
304 older releases don't have */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000305#undef HAVE_GETADDRINFO
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000306#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000307
308#ifdef HAVE_INET_ATON
309#define USE_INET_ATON_WEAKLINK
310#endif
311
Jack Jansen84262fb2002-07-02 14:40:42 +0000312#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000313
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000314/* I know this is a bad practice, but it is the easiest... */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000315#if !defined(HAVE_GETADDRINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000316/* avoid clashes with the C library definition of the symbol. */
317#define getaddrinfo fake_getaddrinfo
318#define gai_strerror fake_gai_strerror
319#define freeaddrinfo fake_freeaddrinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000320#include "getaddrinfo.c"
321#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000322#if !defined(HAVE_GETNAMEINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000323#define getnameinfo fake_getnameinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000324#include "getnameinfo.c"
325#endif
326
Skip Montanaroeb33e5a2007-08-17 12:57:41 +0000327#if defined(MS_WINDOWS)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000328/* seem to be a few differences in the API */
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000329#define SOCKETCLOSE closesocket
Guido van Rossumbe32c891996-06-20 16:25:29 +0000330#define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000331#endif
332
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000333#ifdef MS_WIN32
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000334#define EAFNOSUPPORT WSAEAFNOSUPPORT
335#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000336#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000337
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000338#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000339#define SOCKETCLOSE soclose
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000340#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000341#endif
342
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000343#ifndef SOCKETCLOSE
344#define SOCKETCLOSE close
345#endif
346
Hye-Shik Chang81268602004-02-02 06:05:24 +0000347#if defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)
348#define USE_BLUETOOTH 1
349#if defined(__FreeBSD__)
350#define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP
351#define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM
Thomas Wouterscf297e42007-02-23 15:07:44 +0000352#define BTPROTO_HCI BLUETOOTH_PROTO_HCI
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000353#define SOL_HCI SOL_HCI_RAW
354#define HCI_FILTER SO_HCI_RAW_FILTER
Hye-Shik Chang81268602004-02-02 06:05:24 +0000355#define sockaddr_l2 sockaddr_l2cap
356#define sockaddr_rc sockaddr_rfcomm
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000357#define hci_dev hci_node
Hye-Shik Chang81268602004-02-02 06:05:24 +0000358#define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb)
359#define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000360#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000361#elif defined(__NetBSD__)
362#define sockaddr_l2 sockaddr_bt
363#define sockaddr_rc sockaddr_bt
Thomas Wouterscf297e42007-02-23 15:07:44 +0000364#define sockaddr_hci sockaddr_bt
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000365#define sockaddr_sco sockaddr_bt
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000366#define _BT_L2_MEMB(sa, memb) ((sa)->bt_##memb)
367#define _BT_RC_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000368#define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000369#define _BT_SCO_MEMB(sa, memb) ((sa)->bt_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000370#else
Hye-Shik Chang81268602004-02-02 06:05:24 +0000371#define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)
372#define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000373#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000374#define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)
375#endif
376#endif
377
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000378#ifdef __VMS
379/* TCP/IP Services for VMS uses a maximum send/recv buffer length */
380#define SEGMENT_SIZE (32 * 1024 -1)
381#endif
382
Thomas Wouters89f507f2006-12-13 04:49:30 +0000383#define SAS2SA(x) ((struct sockaddr *)(x))
384
Martin v. Löwise9416172003-05-03 10:12:45 +0000385/*
386 * Constants for getnameinfo()
387 */
388#if !defined(NI_MAXHOST)
389#define NI_MAXHOST 1025
390#endif
391#if !defined(NI_MAXSERV)
392#define NI_MAXSERV 32
393#endif
394
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000395#ifndef INVALID_SOCKET /* MS defines this */
396#define INVALID_SOCKET (-1)
397#endif
398
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000399/* XXX There's a problem here: *static* functions are not supposed to have
400 a Py prefix (or use CapitalizedWords). Later... */
401
Guido van Rossum30a685f1991-06-27 15:51:29 +0000402/* Global variable holding the exception type for errors detected
403 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000404static PyObject *socket_error;
405static PyObject *socket_herror;
406static PyObject *socket_gaierror;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000407static PyObject *socket_timeout;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000408
Tim Peters643a7fc2002-02-17 04:13:21 +0000409/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000410 The sock_type variable contains pointers to various functions,
411 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000412 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000413static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000414
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000415#if defined(HAVE_POLL_H)
416#include <poll.h>
417#elif defined(HAVE_SYS_POLL_H)
418#include <sys/poll.h>
419#endif
420
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000421#ifdef Py_SOCKET_FD_CAN_BE_GE_FD_SETSIZE
422/* Platform can select file descriptors beyond FD_SETSIZE */
423#define IS_SELECTABLE(s) 1
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000424#elif defined(HAVE_POLL)
425/* Instead of select(), we'll use poll() since poll() works on any fd. */
426#define IS_SELECTABLE(s) 1
427/* Can we call select() with this socket without a buffer overrun? */
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000428#else
429/* POSIX says selecting file descriptors beyond FD_SETSIZE
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000430 has undefined behaviour. If there's no timeout left, we don't have to
431 call select, so it's a safe, little white lie. */
432#define IS_SELECTABLE(s) ((s)->sock_fd < FD_SETSIZE || s->sock_timeout <= 0.0)
Martin v. Löwisf84d1b92006-02-11 09:27:05 +0000433#endif
Neal Norwitz082b2df2006-02-07 07:04:46 +0000434
435static PyObject*
436select_error(void)
437{
438 PyErr_SetString(socket_error, "unable to select on socket");
439 return NULL;
440}
441
Guido van Rossum30a685f1991-06-27 15:51:29 +0000442/* Convenience function to raise an error according to errno
443 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000444
Guido van Rossum73624e91994-10-10 17:59:00 +0000445static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000446set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000447{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000448#ifdef MS_WINDOWS
Mark Hammond46a733d2000-07-24 01:45:11 +0000449 int err_no = WSAGetLastError();
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000450 static struct {
451 int no;
452 const char *msg;
453 } *msgp, msgs[] = {
454 {WSAEINTR, "Interrupted system call"},
455 {WSAEBADF, "Bad file descriptor"},
456 {WSAEACCES, "Permission denied"},
457 {WSAEFAULT, "Bad address"},
458 {WSAEINVAL, "Invalid argument"},
459 {WSAEMFILE, "Too many open files"},
460 {WSAEWOULDBLOCK,
461 "The socket operation could not complete "
462 "without blocking"},
463 {WSAEINPROGRESS, "Operation now in progress"},
464 {WSAEALREADY, "Operation already in progress"},
465 {WSAENOTSOCK, "Socket operation on non-socket"},
466 {WSAEDESTADDRREQ, "Destination address required"},
467 {WSAEMSGSIZE, "Message too long"},
468 {WSAEPROTOTYPE, "Protocol wrong type for socket"},
469 {WSAENOPROTOOPT, "Protocol not available"},
470 {WSAEPROTONOSUPPORT, "Protocol not supported"},
471 {WSAESOCKTNOSUPPORT, "Socket type not supported"},
472 {WSAEOPNOTSUPP, "Operation not supported"},
473 {WSAEPFNOSUPPORT, "Protocol family not supported"},
474 {WSAEAFNOSUPPORT, "Address family not supported"},
475 {WSAEADDRINUSE, "Address already in use"},
476 {WSAEADDRNOTAVAIL, "Can't assign requested address"},
477 {WSAENETDOWN, "Network is down"},
478 {WSAENETUNREACH, "Network is unreachable"},
479 {WSAENETRESET, "Network dropped connection on reset"},
480 {WSAECONNABORTED, "Software caused connection abort"},
481 {WSAECONNRESET, "Connection reset by peer"},
482 {WSAENOBUFS, "No buffer space available"},
483 {WSAEISCONN, "Socket is already connected"},
484 {WSAENOTCONN, "Socket is not connected"},
485 {WSAESHUTDOWN, "Can't send after socket shutdown"},
486 {WSAETOOMANYREFS, "Too many references: can't splice"},
487 {WSAETIMEDOUT, "Operation timed out"},
488 {WSAECONNREFUSED, "Connection refused"},
489 {WSAELOOP, "Too many levels of symbolic links"},
490 {WSAENAMETOOLONG, "File name too long"},
491 {WSAEHOSTDOWN, "Host is down"},
492 {WSAEHOSTUNREACH, "No route to host"},
493 {WSAENOTEMPTY, "Directory not empty"},
494 {WSAEPROCLIM, "Too many processes"},
495 {WSAEUSERS, "Too many users"},
496 {WSAEDQUOT, "Disc quota exceeded"},
497 {WSAESTALE, "Stale NFS file handle"},
498 {WSAEREMOTE, "Too many levels of remote in path"},
499 {WSASYSNOTREADY, "Network subsystem is unvailable"},
500 {WSAVERNOTSUPPORTED, "WinSock version is not supported"},
501 {WSANOTINITIALISED,
502 "Successful WSAStartup() not yet performed"},
503 {WSAEDISCON, "Graceful shutdown in progress"},
504 /* Resolver errors */
505 {WSAHOST_NOT_FOUND, "No such host is known"},
506 {WSATRY_AGAIN, "Host not found, or server failed"},
507 {WSANO_RECOVERY, "Unexpected server error encountered"},
508 {WSANO_DATA, "Valid name without requested data"},
509 {WSANO_ADDRESS, "No address, look for MX record"},
510 {0, NULL}
511 };
Mark Hammond46a733d2000-07-24 01:45:11 +0000512 if (err_no) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000513 PyObject *v;
Mark Hammond46a733d2000-07-24 01:45:11 +0000514 const char *msg = "winsock error";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000515
Mark Hammond46a733d2000-07-24 01:45:11 +0000516 for (msgp = msgs; msgp->msg; msgp++) {
517 if (err_no == msgp->no) {
518 msg = msgp->msg;
519 break;
520 }
521 }
522
523 v = Py_BuildValue("(is)", err_no, msg);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000524 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000525 PyErr_SetObject(socket_error, v);
Guido van Rossum73624e91994-10-10 17:59:00 +0000526 Py_DECREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000527 }
528 return NULL;
529 }
530 else
531#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000532
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000533#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000534 if (sock_errno() != NO_ERROR) {
535 APIRET rc;
536 ULONG msglen;
537 char outbuf[100];
538 int myerrorcode = sock_errno();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000539
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000540 /* Retrieve socket-related error message from MPTN.MSG file */
541 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
542 myerrorcode - SOCBASEERR + 26,
543 "mptn.msg",
544 &msglen);
545 if (rc == NO_ERROR) {
546 PyObject *v;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000547
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000548 /* OS/2 doesn't guarantee a terminator */
549 outbuf[msglen] = '\0';
550 if (strlen(outbuf) > 0) {
551 /* If non-empty msg, trim CRLF */
552 char *lastc = &outbuf[ strlen(outbuf)-1 ];
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000553 while (lastc > outbuf &&
554 isspace(Py_CHARMASK(*lastc))) {
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000555 /* Trim trailing whitespace (CRLF) */
556 *lastc-- = '\0';
557 }
558 }
559 v = Py_BuildValue("(is)", myerrorcode, outbuf);
560 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000561 PyErr_SetObject(socket_error, v);
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000562 Py_DECREF(v);
563 }
564 return NULL;
565 }
566 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000567#endif
568
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000569 return PyErr_SetFromErrno(socket_error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000570}
571
Guido van Rossum30a685f1991-06-27 15:51:29 +0000572
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000573static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000574set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000575{
576 PyObject *v;
577
578#ifdef HAVE_HSTRERROR
579 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
580#else
581 v = Py_BuildValue("(is)", h_error, "host not found");
582#endif
583 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000584 PyErr_SetObject(socket_herror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000585 Py_DECREF(v);
586 }
587
588 return NULL;
589}
590
591
592static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000593set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000594{
595 PyObject *v;
596
Martin v. Löwis272cb402002-03-01 08:31:07 +0000597#ifdef EAI_SYSTEM
598 /* EAI_SYSTEM is not available on Windows XP. */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000599 if (error == EAI_SYSTEM)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000600 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000601#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000602
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000603#ifdef HAVE_GAI_STRERROR
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000604 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000605#else
606 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
607#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000608 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000609 PyErr_SetObject(socket_gaierror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000610 Py_DECREF(v);
611 }
612
613 return NULL;
614}
615
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000616#ifdef __VMS
617/* Function to send in segments */
618static int
619sendsegmented(int sock_fd, char *buf, int len, int flags)
620{
621 int n = 0;
622 int remaining = len;
623
624 while (remaining > 0) {
625 unsigned int segment;
626
627 segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining);
628 n = send(sock_fd, buf, segment, flags);
629 if (n < 0) {
630 return n;
631 }
632 remaining -= segment;
633 buf += segment;
634 } /* end while */
635
636 return len;
637}
638#endif
639
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000640/* Function to perform the setting of socket blocking mode
641 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000642static int
643internal_setblocking(PySocketSockObject *s, int block)
644{
Guido van Rossum67f7a382002-06-06 21:08:16 +0000645#ifndef MS_WINDOWS
646 int delay_flag;
647#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +0000648
649 Py_BEGIN_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +0000650#ifndef MS_WINDOWS
651#if defined(PYOS_OS2) && !defined(PYCC_GCC)
652 block = !block;
653 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000654#elif defined(__VMS)
655 block = !block;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000656 ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);
657#else /* !PYOS_OS2 && !__VMS */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000658 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
659 if (block)
Neal Norwitz39d22e52002-11-02 19:55:21 +0000660 delay_flag &= (~O_NONBLOCK);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000661 else
Neal Norwitz39d22e52002-11-02 19:55:21 +0000662 delay_flag |= O_NONBLOCK;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000663 fcntl(s->sock_fd, F_SETFL, delay_flag);
664#endif /* !PYOS_OS2 */
665#else /* MS_WINDOWS */
666 block = !block;
667 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
668#endif /* MS_WINDOWS */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000669 Py_END_ALLOW_THREADS
670
671 /* Since these don't return anything */
672 return 1;
673}
674
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000675/* Do a select()/poll() on the socket, if necessary (sock_timeout > 0).
Guido van Rossum11ba0942002-06-13 15:07:44 +0000676 The argument writing indicates the direction.
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000677 This does not raise an exception; we'll let our caller do that
678 after they've reacquired the interpreter lock.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000679 Returns 1 on timeout, -1 on error, 0 otherwise. */
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000680static int
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000681internal_select(PySocketSockObject *s, int writing)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000682{
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000683 int n;
Guido van Rossum11ba0942002-06-13 15:07:44 +0000684
Guido van Rossumad654902002-07-19 12:44:59 +0000685 /* Nothing to do unless we're in timeout mode (not non-blocking) */
Guido van Rossum11ba0942002-06-13 15:07:44 +0000686 if (s->sock_timeout <= 0.0)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000687 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000688
Guido van Rossumad654902002-07-19 12:44:59 +0000689 /* Guard against closed socket */
690 if (s->sock_fd < 0)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000691 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000692
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000693 /* Prefer poll, if available, since you can poll() any fd
694 * which can't be done with select(). */
695#ifdef HAVE_POLL
696 {
697 struct pollfd pollfd;
698 int timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000699
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000700 pollfd.fd = s->sock_fd;
701 pollfd.events = writing ? POLLOUT : POLLIN;
702
703 /* s->sock_timeout is in seconds, timeout in ms */
704 timeout = (int)(s->sock_timeout * 1000 + 0.5);
705 n = poll(&pollfd, 1, timeout);
706 }
707#else
708 {
709 /* Construct the arguments to select */
710 fd_set fds;
711 struct timeval tv;
712 tv.tv_sec = (int)s->sock_timeout;
713 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
714 FD_ZERO(&fds);
715 FD_SET(s->sock_fd, &fds);
716
717 /* See if the socket is ready */
718 if (writing)
719 n = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
720 else
721 n = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
722 }
723#endif
724
725 if (n < 0)
726 return -1;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000727 if (n == 0)
728 return 1;
729 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000730}
731
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000732/* Initialize a new socket object. */
733
Tim Petersa12b4cf2002-07-18 22:38:44 +0000734static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000735
Mark Hammond62b1ab12002-07-23 06:31:15 +0000736PyMODINIT_FUNC
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000737init_sockobject(PySocketSockObject *s,
738 SOCKET_T fd, int family, int type, int proto)
739{
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000740 s->sock_fd = fd;
741 s->sock_family = family;
742 s->sock_type = type;
743 s->sock_proto = proto;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000744 s->sock_timeout = defaulttimeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000745
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000746 s->errorhandler = &set_error;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000747
748 if (defaulttimeout >= 0.0)
749 internal_setblocking(s, 0);
750
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000751}
752
753
Guido van Rossum30a685f1991-06-27 15:51:29 +0000754/* Create a new socket object.
755 This just creates the object and initializes it.
756 If the creation fails, return NULL and set an exception (implicit
757 in NEWOBJ()). */
758
Guido van Rossum73624e91994-10-10 17:59:00 +0000759static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000760new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000761{
Guido van Rossum73624e91994-10-10 17:59:00 +0000762 PySocketSockObject *s;
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000763 s = (PySocketSockObject *)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000764 PyType_GenericNew(&sock_type, NULL, NULL);
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000765 if (s != NULL)
766 init_sockobject(s, fd, family, type, proto);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000767 return s;
768}
769
Guido van Rossum30a685f1991-06-27 15:51:29 +0000770
Guido van Rossum48a680c2001-03-02 06:34:14 +0000771/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000772 thread to be in gethostbyname or getaddrinfo */
773#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
774PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000775#endif
776
777
Guido van Rossum30a685f1991-06-27 15:51:29 +0000778/* Convert a string specifying a host name or one of a few symbolic
779 names to a numeric IP address. This usually calls gethostbyname()
780 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000781 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000782 an error occurred; then an exception is raised. */
783
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000784static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000785setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000786{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000787 struct addrinfo hints, *res;
788 int error;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000789 int d1, d2, d3, d4;
790 char ch;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000791
Guido van Rossuma376cc51996-12-05 23:43:35 +0000792 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
Guido van Rossum30a685f1991-06-27 15:51:29 +0000793 if (name[0] == '\0') {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000794 int siz;
795 memset(&hints, 0, sizeof(hints));
796 hints.ai_family = af;
797 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
798 hints.ai_flags = AI_PASSIVE;
Just van Rossum1040d2c2003-05-09 07:53:18 +0000799 Py_BEGIN_ALLOW_THREADS
800 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000801 error = getaddrinfo(NULL, "0", &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +0000802 Py_END_ALLOW_THREADS
803 /* We assume that those thread-unsafe getaddrinfo() versions
804 *are* safe regarding their return value, ie. that a
805 subsequent call to getaddrinfo() does not destroy the
806 outcome of the first call. */
807 RELEASE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000808 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000809 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000810 return -1;
811 }
812 switch (res->ai_family) {
813 case AF_INET:
814 siz = 4;
815 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000816#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000817 case AF_INET6:
818 siz = 16;
819 break;
820#endif
821 default:
822 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000823 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000824 "unsupported address family");
825 return -1;
826 }
827 if (res->ai_next) {
Martin v. Löwisf0b11d22001-11-07 08:31:03 +0000828 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000829 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000830 "wildcard resolved to multiple address");
831 return -1;
832 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000833 if (res->ai_addrlen < addr_ret_size)
834 addr_ret_size = res->ai_addrlen;
835 memcpy(addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000836 freeaddrinfo(res);
837 return siz;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000838 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000839 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000840 struct sockaddr_in *sin;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +0000841 if (af != AF_INET && af != AF_UNSPEC) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000842 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000843 "address family mismatched");
844 return -1;
845 }
846 sin = (struct sockaddr_in *)addr_ret;
847 memset((void *) sin, '\0', sizeof(*sin));
848 sin->sin_family = AF_INET;
849#ifdef HAVE_SOCKADDR_SA_LEN
850 sin->sin_len = sizeof(*sin);
851#endif
852 sin->sin_addr.s_addr = INADDR_BROADCAST;
853 return sizeof(sin->sin_addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000854 }
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000855 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
856 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
857 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
858 struct sockaddr_in *sin;
859 sin = (struct sockaddr_in *)addr_ret;
860 sin->sin_addr.s_addr = htonl(
861 ((long) d1 << 24) | ((long) d2 << 16) |
862 ((long) d3 << 8) | ((long) d4 << 0));
863 sin->sin_family = AF_INET;
864#ifdef HAVE_SOCKADDR_SA_LEN
865 sin->sin_len = sizeof(*sin);
866#endif
867 return 4;
868 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000869 memset(&hints, 0, sizeof(hints));
870 hints.ai_family = af;
Just van Rossum1040d2c2003-05-09 07:53:18 +0000871 Py_BEGIN_ALLOW_THREADS
872 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000873 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000874#if defined(__digital__) && defined(__unix__)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000875 if (error == EAI_NONAME && af == AF_UNSPEC) {
876 /* On Tru64 V5.1, numeric-to-addr conversion fails
877 if no address family is given. Assume IPv4 for now.*/
878 hints.ai_family = AF_INET;
879 error = getaddrinfo(name, NULL, &hints, &res);
880 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000881#endif
Just van Rossum1040d2c2003-05-09 07:53:18 +0000882 Py_END_ALLOW_THREADS
883 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000884 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000885 set_gaierror(error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000886 return -1;
887 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000888 if (res->ai_addrlen < addr_ret_size)
889 addr_ret_size = res->ai_addrlen;
890 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000891 freeaddrinfo(res);
892 switch (addr_ret->sa_family) {
893 case AF_INET:
894 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000895#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000896 case AF_INET6:
897 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000898#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000899 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000900 PyErr_SetString(socket_error, "unknown address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000901 return -1;
902 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000903}
904
Guido van Rossum30a685f1991-06-27 15:51:29 +0000905
Guido van Rossum30a685f1991-06-27 15:51:29 +0000906/* Create a string object representing an IP address.
907 This is always a string of the form 'dd.dd.dd.dd' (with variable
908 size numbers). */
909
Guido van Rossum73624e91994-10-10 17:59:00 +0000910static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000911makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000912{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000913 char buf[NI_MAXHOST];
914 int error;
915
916 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
917 NI_NUMERICHOST);
918 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000919 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000920 return NULL;
921 }
Guido van Rossum32c4ac02007-08-15 03:56:40 +0000922 return PyUnicode_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000923}
924
925
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000926#ifdef USE_BLUETOOTH
927/* Convert a string representation of a Bluetooth address into a numeric
928 address. Returns the length (6), or raises an exception and returns -1 if
929 an error occurred. */
930
931static int
932setbdaddr(char *name, bdaddr_t *bdaddr)
933{
934 unsigned int b0, b1, b2, b3, b4, b5;
935 char ch;
936 int n;
937
938 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
939 &b5, &b4, &b3, &b2, &b1, &b0, &ch);
940 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
941 bdaddr->b[0] = b0;
942 bdaddr->b[1] = b1;
943 bdaddr->b[2] = b2;
944 bdaddr->b[3] = b3;
945 bdaddr->b[4] = b4;
946 bdaddr->b[5] = b5;
947 return 6;
948 } else {
949 PyErr_SetString(socket_error, "bad bluetooth address");
950 return -1;
951 }
952}
953
954/* Create a string representation of the Bluetooth address. This is always a
955 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
956 value (zero padded if necessary). */
957
958static PyObject *
959makebdaddr(bdaddr_t *bdaddr)
960{
961 char buf[(6 * 2) + 5 + 1];
962
963 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
964 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
965 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
Guido van Rossum32c4ac02007-08-15 03:56:40 +0000966 return PyUnicode_FromString(buf);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000967}
968#endif
969
970
Guido van Rossum30a685f1991-06-27 15:51:29 +0000971/* Create an object representing the given socket address,
972 suitable for passing it back to bind(), connect() etc.
973 The family field of the sockaddr structure is inspected
974 to determine what kind of address it really is. */
975
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000976/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000977static PyObject *
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000978makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000979{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000980 if (addrlen == 0) {
981 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +0000982 Py_INCREF(Py_None);
983 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000984 }
985
Guido van Rossum30a685f1991-06-27 15:51:29 +0000986 switch (addr->sa_family) {
987
988 case AF_INET:
989 {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000990 struct sockaddr_in *a;
991 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
Barry Warsaw752300b1997-01-03 17:18:10 +0000992 PyObject *ret = NULL;
993 if (addrobj) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000994 a = (struct sockaddr_in *)addr;
Barry Warsaw752300b1997-01-03 17:18:10 +0000995 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
996 Py_DECREF(addrobj);
997 }
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000998 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000999 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00001000
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001001#if defined(AF_UNIX)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001002 case AF_UNIX:
1003 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001004 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001005#ifdef linux
1006 if (a->sun_path[0] == 0) { /* Linux abstract namespace */
1007 addrlen -= (sizeof(*a) - sizeof(a->sun_path));
Guido van Rossum32c4ac02007-08-15 03:56:40 +00001008 return PyBytes_FromStringAndSize(a->sun_path, addrlen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001009 }
1010 else
1011#endif /* linux */
1012 {
1013 /* regular NULL-terminated string */
Guido van Rossum32c4ac02007-08-15 03:56:40 +00001014 return PyUnicode_FromString(a->sun_path);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001015 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001016 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001017#endif /* AF_UNIX */
1018
Martin v. Löwis11017b12006-01-14 18:12:57 +00001019#if defined(AF_NETLINK)
1020 case AF_NETLINK:
1021 {
1022 struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
Martin v. Löwisd0560052006-01-15 07:49:20 +00001023 return Py_BuildValue("II", a->nl_pid, a->nl_groups);
Martin v. Löwis11017b12006-01-14 18:12:57 +00001024 }
1025#endif /* AF_NETLINK */
1026
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001027#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001028 case AF_INET6:
1029 {
1030 struct sockaddr_in6 *a;
1031 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
1032 PyObject *ret = NULL;
1033 if (addrobj) {
1034 a = (struct sockaddr_in6 *)addr;
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001035 ret = Py_BuildValue("Oiii",
1036 addrobj,
1037 ntohs(a->sin6_port),
1038 a->sin6_flowinfo,
1039 a->sin6_scope_id);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001040 Py_DECREF(addrobj);
1041 }
1042 return ret;
1043 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001044#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001045
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001046#ifdef USE_BLUETOOTH
1047 case AF_BLUETOOTH:
1048 switch (proto) {
1049
1050 case BTPROTO_L2CAP:
1051 {
1052 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
1053 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
1054 PyObject *ret = NULL;
1055 if (addrobj) {
1056 ret = Py_BuildValue("Oi",
1057 addrobj,
1058 _BT_L2_MEMB(a, psm));
1059 Py_DECREF(addrobj);
1060 }
1061 return ret;
1062 }
1063
1064 case BTPROTO_RFCOMM:
1065 {
1066 struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
1067 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
1068 PyObject *ret = NULL;
1069 if (addrobj) {
1070 ret = Py_BuildValue("Oi",
1071 addrobj,
1072 _BT_RC_MEMB(a, channel));
1073 Py_DECREF(addrobj);
1074 }
1075 return ret;
1076 }
1077
Thomas Wouterscf297e42007-02-23 15:07:44 +00001078 case BTPROTO_HCI:
1079 {
1080 struct sockaddr_hci *a = (struct sockaddr_hci *) addr;
1081 PyObject *ret = NULL;
1082 ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
1083 return ret;
1084 }
1085
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001086#if !defined(__FreeBSD__)
1087 case BTPROTO_SCO:
1088 {
1089 struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
1090 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
1091 }
1092#endif
1093
1094 }
1095#endif
1096
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001097#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001098 case AF_PACKET:
1099 {
1100 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
1101 char *ifname = "";
1102 struct ifreq ifr;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001103 /* need to look up interface name give index */
1104 if (a->sll_ifindex) {
Jeremy Hylton22308652001-02-02 03:23:09 +00001105 ifr.ifr_ifindex = a->sll_ifindex;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001106 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
Jeremy Hylton22308652001-02-02 03:23:09 +00001107 ifname = ifr.ifr_name;
Jeremy Hylton22308652001-02-02 03:23:09 +00001108 }
Guido van Rossum32c4ac02007-08-15 03:56:40 +00001109 return Py_BuildValue("shbhy#",
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001110 ifname,
1111 ntohs(a->sll_protocol),
1112 a->sll_pkttype,
1113 a->sll_hatype,
1114 a->sll_addr,
1115 a->sll_halen);
Jeremy Hylton22308652001-02-02 03:23:09 +00001116 }
1117#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001118
Guido van Rossum30a685f1991-06-27 15:51:29 +00001119 /* More cases here... */
1120
1121 default:
Guido van Rossumaa948df1997-05-07 17:41:48 +00001122 /* If we don't know the address family, don't raise an
Guido van Rossum9ce8e382007-08-09 21:06:37 +00001123 exception -- return it as an (int, bytes) tuple. */
1124 return Py_BuildValue("iy#",
Guido van Rossumaa948df1997-05-07 17:41:48 +00001125 addr->sa_family,
1126 addr->sa_data,
1127 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001128
Guido van Rossum30a685f1991-06-27 15:51:29 +00001129 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001130}
1131
Guido van Rossum30a685f1991-06-27 15:51:29 +00001132
1133/* Parse a socket address argument according to the socket object's
1134 address family. Return 1 if the address was in the proper format,
1135 0 of not. The address is returned through addr_ret, its length
1136 through len_ret. */
1137
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001138static int
Guido van Rossum48a680c2001-03-02 06:34:14 +00001139getsockaddrarg(PySocketSockObject *s, PyObject *args,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001140 struct sockaddr *addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001141{
Guido van Rossum30a685f1991-06-27 15:51:29 +00001142 switch (s->sock_family) {
1143
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001144#if defined(AF_UNIX)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001145 case AF_UNIX:
1146 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001147 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001148 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001149 int len;
Guido van Rossume22905a2007-08-27 23:09:25 +00001150 if (!PyArg_Parse(args, "s#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001151 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001152
1153 addr = (struct sockaddr_un*)addr_ret;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001154#ifdef linux
1155 if (len > 0 && path[0] == 0) {
1156 /* Linux abstract namespace extension */
1157 if (len > sizeof addr->sun_path) {
1158 PyErr_SetString(socket_error,
1159 "AF_UNIX path too long");
1160 return 0;
1161 }
1162 }
1163 else
1164#endif /* linux */
1165 {
1166 /* regular NULL-terminated string */
1167 if (len >= sizeof addr->sun_path) {
1168 PyErr_SetString(socket_error,
1169 "AF_UNIX path too long");
1170 return 0;
1171 }
1172 addr->sun_path[len] = 0;
Guido van Rossum30a685f1991-06-27 15:51:29 +00001173 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001174 addr->sun_family = s->sock_family;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001175 memcpy(addr->sun_path, path, len);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001176#if defined(PYOS_OS2)
1177 *len_ret = sizeof(*addr);
1178#else
Guido van Rossum65af28a1996-06-11 18:36:33 +00001179 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001180#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001181 return 1;
1182 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001183#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001184
Martin v. Löwis11017b12006-01-14 18:12:57 +00001185#if defined(AF_NETLINK)
1186 case AF_NETLINK:
1187 {
1188 struct sockaddr_nl* addr;
1189 int pid, groups;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001190 addr = (struct sockaddr_nl *)addr_ret;
Martin v. Löwis11017b12006-01-14 18:12:57 +00001191 if (!PyTuple_Check(args)) {
1192 PyErr_Format(
1193 PyExc_TypeError,
1194 "getsockaddrarg: "
1195 "AF_NETLINK address must be tuple, not %.500s",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001196 Py_Type(args)->tp_name);
Martin v. Löwis11017b12006-01-14 18:12:57 +00001197 return 0;
1198 }
1199 if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
1200 return 0;
1201 addr->nl_family = AF_NETLINK;
1202 addr->nl_pid = pid;
1203 addr->nl_groups = groups;
Martin v. Löwis11017b12006-01-14 18:12:57 +00001204 *len_ret = sizeof(*addr);
1205 return 1;
1206 }
1207#endif
1208
Guido van Rossum30a685f1991-06-27 15:51:29 +00001209 case AF_INET:
1210 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001211 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001212 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001213 int port, result;
Guido van Rossume4dad902000-12-01 13:13:11 +00001214 if (!PyTuple_Check(args)) {
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001215 PyErr_Format(
1216 PyExc_TypeError,
1217 "getsockaddrarg: "
1218 "AF_INET address must be tuple, not %.500s",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001219 Py_Type(args)->tp_name);
Guido van Rossume4dad902000-12-01 13:13:11 +00001220 return 0;
1221 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001222 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
Martin v. Löwis2548c732003-04-18 10:39:54 +00001223 "idna", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001224 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001225 addr=(struct sockaddr_in*)addr_ret;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001226 result = setipaddr(host, (struct sockaddr *)addr,
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001227 sizeof(*addr), AF_INET);
1228 PyMem_Free(host);
1229 if (result < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001230 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001231 addr->sin_family = AF_INET;
Guido van Rossum644a12b1997-04-09 19:24:53 +00001232 addr->sin_port = htons((short)port);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001233 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001234 return 1;
1235 }
1236
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001237#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001238 case AF_INET6:
1239 {
1240 struct sockaddr_in6* addr;
1241 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001242 int port, flowinfo, scope_id, result;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001243 flowinfo = scope_id = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001244 if (!PyTuple_Check(args)) {
1245 PyErr_Format(
1246 PyExc_TypeError,
1247 "getsockaddrarg: "
1248 "AF_INET6 address must be tuple, not %.500s",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001249 Py_Type(args)->tp_name);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001250 return 0;
1251 }
1252 if (!PyArg_ParseTuple(args, "eti|ii",
Martin v. Löwis2548c732003-04-18 10:39:54 +00001253 "idna", &host, &port, &flowinfo,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001254 &scope_id)) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001255 return 0;
1256 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001257 addr = (struct sockaddr_in6*)addr_ret;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001258 result = setipaddr(host, (struct sockaddr *)addr,
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001259 sizeof(*addr), AF_INET6);
1260 PyMem_Free(host);
1261 if (result < 0)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001262 return 0;
1263 addr->sin6_family = s->sock_family;
1264 addr->sin6_port = htons((short)port);
1265 addr->sin6_flowinfo = flowinfo;
1266 addr->sin6_scope_id = scope_id;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001267 *len_ret = sizeof *addr;
1268 return 1;
1269 }
1270#endif
1271
Hye-Shik Chang81268602004-02-02 06:05:24 +00001272#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00001273 case AF_BLUETOOTH:
1274 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001275 switch (s->sock_proto) {
1276 case BTPROTO_L2CAP:
Martin v. Löwis12af0482004-01-31 12:34:17 +00001277 {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001278 struct sockaddr_l2 *addr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001279 char *straddr;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001280
Thomas Wouters89f507f2006-12-13 04:49:30 +00001281 addr = (struct sockaddr_l2 *)addr_ret;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001282 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
1283 if (!PyArg_ParseTuple(args, "si", &straddr,
1284 &_BT_L2_MEMB(addr, psm))) {
1285 PyErr_SetString(socket_error, "getsockaddrarg: "
1286 "wrong format");
Martin v. Löwis12af0482004-01-31 12:34:17 +00001287 return 0;
1288 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001289 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
1290 return 0;
1291
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001292 *len_ret = sizeof *addr;
1293 return 1;
1294 }
1295 case BTPROTO_RFCOMM:
1296 {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001297 struct sockaddr_rc *addr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001298 char *straddr;
1299
Thomas Wouters89f507f2006-12-13 04:49:30 +00001300 addr = (struct sockaddr_rc *)addr_ret;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001301 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
1302 if (!PyArg_ParseTuple(args, "si", &straddr,
1303 &_BT_RC_MEMB(addr, channel))) {
1304 PyErr_SetString(socket_error, "getsockaddrarg: "
1305 "wrong format");
1306 return 0;
1307 }
1308 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
1309 return 0;
1310
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001311 *len_ret = sizeof *addr;
1312 return 1;
1313 }
Thomas Wouterscf297e42007-02-23 15:07:44 +00001314 case BTPROTO_HCI:
1315 {
1316 struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret;
1317 _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
1318 if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) {
1319 PyErr_SetString(socket_error, "getsockaddrarg: "
1320 "wrong format");
1321 return 0;
1322 }
1323 *len_ret = sizeof *addr;
1324 return 1;
1325 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001326#if !defined(__FreeBSD__)
1327 case BTPROTO_SCO:
1328 {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001329 struct sockaddr_sco *addr;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001330 char *straddr;
1331
Thomas Wouters89f507f2006-12-13 04:49:30 +00001332 addr = (struct sockaddr_sco *)addr_ret;
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001333 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
Guido van Rossum32c4ac02007-08-15 03:56:40 +00001334 if (!PyBytes_Check(args)) {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001335 PyErr_SetString(socket_error, "getsockaddrarg: "
1336 "wrong format");
1337 return 0;
1338 }
Guido van Rossum32c4ac02007-08-15 03:56:40 +00001339 straddr = PyBytes_AS_STRING(args);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001340 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
1341 return 0;
1342
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001343 *len_ret = sizeof *addr;
1344 return 1;
1345 }
1346#endif
1347 default:
1348 PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol");
1349 return 0;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001350 }
1351 }
1352#endif
1353
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001354#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001355 case AF_PACKET:
1356 {
1357 struct sockaddr_ll* addr;
1358 struct ifreq ifr;
1359 char *interfaceName;
1360 int protoNumber;
1361 int hatype = 0;
1362 int pkttype = 0;
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001363 char *haddr = NULL;
1364 unsigned int halen = 0;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001365
Thomas Wouters477c8d52006-05-27 19:21:47 +00001366 if (!PyTuple_Check(args)) {
1367 PyErr_Format(
1368 PyExc_TypeError,
1369 "getsockaddrarg: "
1370 "AF_PACKET address must be tuple, not %.500s",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001371 Py_Type(args)->tp_name);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001372 return 0;
1373 }
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001374 if (!PyArg_ParseTuple(args, "si|iis#", &interfaceName,
1375 &protoNumber, &pkttype, &hatype,
1376 &haddr, &halen))
Jeremy Hylton22308652001-02-02 03:23:09 +00001377 return 0;
1378 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1379 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001380 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001381 s->errorhandler();
Jeremy Hylton22308652001-02-02 03:23:09 +00001382 return 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001383 }
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001384 if (halen > 8) {
1385 PyErr_SetString(PyExc_ValueError,
1386 "Hardware address must be 8 bytes or less");
1387 return 0;
1388 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001389 addr = (struct sockaddr_ll*)addr_ret;
1390 addr->sll_family = AF_PACKET;
1391 addr->sll_protocol = htons((short)protoNumber);
1392 addr->sll_ifindex = ifr.ifr_ifindex;
1393 addr->sll_pkttype = pkttype;
1394 addr->sll_hatype = hatype;
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001395 if (halen != 0) {
1396 memcpy(&addr->sll_addr, haddr, halen);
1397 }
1398 addr->sll_halen = halen;
Jeremy Hylton22308652001-02-02 03:23:09 +00001399 *len_ret = sizeof *addr;
1400 return 1;
1401 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001402#endif
1403
Guido van Rossum30a685f1991-06-27 15:51:29 +00001404 /* More cases here... */
1405
1406 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001407 PyErr_SetString(socket_error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001408 return 0;
1409
1410 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001411}
1412
Guido van Rossum30a685f1991-06-27 15:51:29 +00001413
Guido van Rossum48a680c2001-03-02 06:34:14 +00001414/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00001415 Return 1 if the family is known, 0 otherwise. The length is returned
1416 through len_ret. */
1417
1418static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00001419getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001420{
1421 switch (s->sock_family) {
1422
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001423#if defined(AF_UNIX)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001424 case AF_UNIX:
1425 {
1426 *len_ret = sizeof (struct sockaddr_un);
1427 return 1;
1428 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001429#endif /* AF_UNIX */
Martin v. Löwis11017b12006-01-14 18:12:57 +00001430#if defined(AF_NETLINK)
1431 case AF_NETLINK:
1432 {
1433 *len_ret = sizeof (struct sockaddr_nl);
1434 return 1;
1435 }
1436#endif
Guido van Rossum710e1df1992-06-12 10:39:36 +00001437
1438 case AF_INET:
1439 {
1440 *len_ret = sizeof (struct sockaddr_in);
1441 return 1;
1442 }
1443
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001444#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001445 case AF_INET6:
1446 {
1447 *len_ret = sizeof (struct sockaddr_in6);
1448 return 1;
1449 }
1450#endif
1451
Hye-Shik Chang81268602004-02-02 06:05:24 +00001452#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00001453 case AF_BLUETOOTH:
1454 {
1455 switch(s->sock_proto)
1456 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001457
1458 case BTPROTO_L2CAP:
1459 *len_ret = sizeof (struct sockaddr_l2);
1460 return 1;
1461 case BTPROTO_RFCOMM:
1462 *len_ret = sizeof (struct sockaddr_rc);
1463 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001464 case BTPROTO_HCI:
1465 *len_ret = sizeof (struct sockaddr_hci);
1466 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001467#if !defined(__FreeBSD__)
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001468 case BTPROTO_SCO:
1469 *len_ret = sizeof (struct sockaddr_sco);
1470 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001471#endif
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001472 default:
1473 PyErr_SetString(socket_error, "getsockaddrlen: "
1474 "unknown BT protocol");
1475 return 0;
1476
Martin v. Löwis12af0482004-01-31 12:34:17 +00001477 }
1478 }
1479#endif
1480
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001481#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001482 case AF_PACKET:
1483 {
1484 *len_ret = sizeof (struct sockaddr_ll);
1485 return 1;
1486 }
1487#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001488
Guido van Rossum710e1df1992-06-12 10:39:36 +00001489 /* More cases here... */
1490
1491 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001492 PyErr_SetString(socket_error, "getsockaddrlen: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +00001493 return 0;
1494
1495 }
1496}
1497
1498
Guido van Rossum30a685f1991-06-27 15:51:29 +00001499/* s.accept() method */
1500
Guido van Rossum73624e91994-10-10 17:59:00 +00001501static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001502sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001503{
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001504 sock_addr_t addrbuf;
Guido van Rossum7d0a8262007-05-21 23:13:11 +00001505 SOCKET_T newfd = INVALID_SOCKET;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001506 socklen_t addrlen;
Barry Warsaw752300b1997-01-03 17:18:10 +00001507 PyObject *sock = NULL;
1508 PyObject *addr = NULL;
1509 PyObject *res = NULL;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001510 int timeout;
Barry Warsaw752300b1997-01-03 17:18:10 +00001511
Guido van Rossum710e1df1992-06-12 10:39:36 +00001512 if (!getsockaddrlen(s, &addrlen))
1513 return NULL;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001514 memset(&addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001515
Neal Norwitz082b2df2006-02-07 07:04:46 +00001516 if (!IS_SELECTABLE(s))
1517 return select_error();
1518
Guido van Rossum73624e91994-10-10 17:59:00 +00001519 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001520 timeout = internal_select(s, 0);
1521 if (!timeout)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001522 newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001523 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001524
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001525 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001526 PyErr_SetString(socket_timeout, "timed out");
1527 return NULL;
1528 }
1529
Fred Drakea04eaad2000-06-30 02:46:07 +00001530 if (newfd == INVALID_SOCKET)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001531 return s->errorhandler();
Barry Warsaw752300b1997-01-03 17:18:10 +00001532
Guido van Rossum30a685f1991-06-27 15:51:29 +00001533 /* Create the new object with unspecified family,
1534 to avoid calls to bind() etc. on it. */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001535 sock = (PyObject *) new_sockobject(newfd,
1536 s->sock_family,
1537 s->sock_type,
1538 s->sock_proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001539
Barry Warsaw752300b1997-01-03 17:18:10 +00001540 if (sock == NULL) {
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001541 SOCKETCLOSE(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +00001542 goto finally;
1543 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001544 addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001545 addrlen, s->sock_proto);
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001546 if (addr == NULL)
Barry Warsaw752300b1997-01-03 17:18:10 +00001547 goto finally;
1548
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001549 res = PyTuple_Pack(2, sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001550
Guido van Rossum67f7a382002-06-06 21:08:16 +00001551finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001552 Py_XDECREF(sock);
1553 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001554 return res;
1555}
1556
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001557PyDoc_STRVAR(accept_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001558"accept() -> (socket object, address info)\n\
1559\n\
1560Wait for an incoming connection. Return a new socket representing the\n\
1561connection, and the address of the client. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001562info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001563
Guido van Rossum11ba0942002-06-13 15:07:44 +00001564/* s.setblocking(flag) method. Argument:
1565 False -- non-blocking mode; same as settimeout(0)
1566 True -- blocking mode; same as settimeout(None)
1567*/
Guido van Rossume4485b01994-09-07 14:32:49 +00001568
Guido van Rossum73624e91994-10-10 17:59:00 +00001569static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001570sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00001571{
1572 int block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001573
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001574 block = PyInt_AsLong(arg);
1575 if (block == -1 && PyErr_Occurred())
Guido van Rossume4485b01994-09-07 14:32:49 +00001576 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001577
Guido van Rossum11ba0942002-06-13 15:07:44 +00001578 s->sock_timeout = block ? -1.0 : 0.0;
Guido van Rossume8008f02002-06-07 03:36:20 +00001579 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00001580
Guido van Rossum73624e91994-10-10 17:59:00 +00001581 Py_INCREF(Py_None);
1582 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00001583}
Guido van Rossume4485b01994-09-07 14:32:49 +00001584
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001585PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001586"setblocking(flag)\n\
1587\n\
1588Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001589setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001590setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001591
Guido van Rossum11ba0942002-06-13 15:07:44 +00001592/* s.settimeout(timeout) method. Argument:
1593 None -- no timeout, blocking mode; same as setblocking(True)
1594 0.0 -- non-blocking mode; same as setblocking(False)
1595 > 0 -- timeout mode; operations time out after timeout seconds
1596 < 0 -- illegal; raises an exception
1597*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00001598static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001599sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001600{
Guido van Rossum11ba0942002-06-13 15:07:44 +00001601 double timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001602
1603 if (arg == Py_None)
Guido van Rossum11ba0942002-06-13 15:07:44 +00001604 timeout = -1.0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001605 else {
Guido van Rossum11ba0942002-06-13 15:07:44 +00001606 timeout = PyFloat_AsDouble(arg);
1607 if (timeout < 0.0) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00001608 if (!PyErr_Occurred())
1609 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001610 "Timeout value out of range");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001611 return NULL;
1612 }
1613 }
1614
Guido van Rossum11ba0942002-06-13 15:07:44 +00001615 s->sock_timeout = timeout;
1616 internal_setblocking(s, timeout < 0.0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001617
1618 Py_INCREF(Py_None);
1619 return Py_None;
1620}
1621
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001622PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001623"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001624\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001625Set a timeout on socket operations. 'timeout' can be a float,\n\
1626giving in seconds, or None. Setting a timeout of None disables\n\
1627the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001628Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001629
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001630/* s.gettimeout() method.
1631 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001632static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001633sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001634{
1635 if (s->sock_timeout < 0.0) {
1636 Py_INCREF(Py_None);
1637 return Py_None;
1638 }
1639 else
1640 return PyFloat_FromDouble(s->sock_timeout);
1641}
1642
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001643PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001644"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001645\n\
1646Returns the timeout in floating seconds associated with socket \n\
1647operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001648operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00001649
Guido van Rossumaee08791992-09-08 09:05:33 +00001650/* s.setsockopt() method.
1651 With an integer third argument, sets an integer option.
1652 With a string third argument, sets an option from a buffer;
1653 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001654
Guido van Rossum73624e91994-10-10 17:59:00 +00001655static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001656sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001657{
1658 int level;
1659 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001660 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +00001661 char *buf;
1662 int buflen;
1663 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001664
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001665 if (PyArg_ParseTuple(args, "iii:setsockopt",
1666 &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001667 buf = (char *) &flag;
1668 buflen = sizeof flag;
1669 }
1670 else {
Guido van Rossum73624e91994-10-10 17:59:00 +00001671 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001672 if (!PyArg_ParseTuple(args, "iis#:setsockopt",
1673 &level, &optname, &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +00001674 return NULL;
1675 }
Thomas Wouters334fb892000-07-25 12:56:38 +00001676 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001677 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001678 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001679 Py_INCREF(Py_None);
1680 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001681}
1682
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001683PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001684"setsockopt(level, option, value)\n\
1685\n\
1686Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001687The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001688
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001689
Guido van Rossumaee08791992-09-08 09:05:33 +00001690/* s.getsockopt() method.
1691 With two arguments, retrieves an integer option.
1692 With a third integer argument, retrieves a string buffer of that size;
1693 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001694
Guido van Rossum73624e91994-10-10 17:59:00 +00001695static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001696sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001697{
1698 int level;
1699 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001700 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00001701 PyObject *buf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001702 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001703
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001704 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
1705 &level, &optname, &buflen))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001706 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001707
Guido van Rossumbe32c891996-06-20 16:25:29 +00001708 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001709 int flag = 0;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001710 socklen_t flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001711 res = getsockopt(s->sock_fd, level, optname,
Thomas Wouters334fb892000-07-25 12:56:38 +00001712 (void *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +00001713 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001714 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001715 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +00001716 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001717#ifdef __VMS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001718 /* socklen_t is unsigned so no negative test is needed,
1719 test buflen == 0 is previously done */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001720 if (buflen > 1024) {
1721#else
Guido van Rossumaee08791992-09-08 09:05:33 +00001722 if (buflen <= 0 || buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001723#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001724 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +00001725 "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +00001726 return NULL;
1727 }
Guido van Rossum7d0a8262007-05-21 23:13:11 +00001728 buf = PyBytes_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001729 if (buf == NULL)
1730 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001731 res = getsockopt(s->sock_fd, level, optname,
Guido van Rossum7d0a8262007-05-21 23:13:11 +00001732 (void *)PyBytes_AS_STRING(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001733 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001734 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001735 return s->errorhandler();
Guido van Rossumaee08791992-09-08 09:05:33 +00001736 }
Guido van Rossum7d0a8262007-05-21 23:13:11 +00001737 if (PyBytes_Resize(buf, buflen) < 0) {
1738 Py_DECREF(buf);
1739 return NULL;
1740 }
Guido van Rossumaee08791992-09-08 09:05:33 +00001741 return buf;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001742}
1743
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001744PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001745"getsockopt(level, option[, buffersize]) -> value\n\
1746\n\
1747Get a socket option. See the Unix manual for level and option.\n\
1748If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001749string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001750
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001751
Fred Drake728819a2000-07-01 03:40:12 +00001752/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001753
Guido van Rossum73624e91994-10-10 17:59:00 +00001754static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001755sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001756{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001757 sock_addr_t addrbuf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001758 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001759 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001760
Thomas Wouters89f507f2006-12-13 04:49:30 +00001761 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001762 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001763 Py_BEGIN_ALLOW_THREADS
Thomas Wouters89f507f2006-12-13 04:49:30 +00001764 res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001765 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001766 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001767 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001768 Py_INCREF(Py_None);
1769 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001770}
1771
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001772PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001773"bind(address)\n\
1774\n\
1775Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00001776pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001777sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001778
Guido van Rossum30a685f1991-06-27 15:51:29 +00001779
1780/* s.close() method.
1781 Set the file descriptor to -1 so operations tried subsequently
1782 will surely fail. */
1783
Guido van Rossum73624e91994-10-10 17:59:00 +00001784static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001785sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001786{
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001787 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001788
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001789 if ((fd = s->sock_fd) != -1) {
1790 s->sock_fd = -1;
Guido van Rossum08481461996-10-12 14:07:22 +00001791 Py_BEGIN_ALLOW_THREADS
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001792 (void) SOCKETCLOSE(fd);
Guido van Rossum08481461996-10-12 14:07:22 +00001793 Py_END_ALLOW_THREADS
1794 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001795 Py_INCREF(Py_None);
1796 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001797}
1798
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001799PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001800"close()\n\
1801\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001802Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001803
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001804static int
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001805internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
1806 int *timeoutp)
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001807{
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001808 int res, timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001809
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001810 timeout = 0;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001811 res = connect(s->sock_fd, addr, addrlen);
1812
1813#ifdef MS_WINDOWS
1814
1815 if (s->sock_timeout > 0.0) {
Neal Norwitz082b2df2006-02-07 07:04:46 +00001816 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK &&
1817 IS_SELECTABLE(s)) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001818 /* This is a mess. Best solution: trust select */
1819 fd_set fds;
Mark Hammonda57ec932004-08-03 05:06:26 +00001820 fd_set fds_exc;
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001821 struct timeval tv;
1822 tv.tv_sec = (int)s->sock_timeout;
1823 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
1824 FD_ZERO(&fds);
1825 FD_SET(s->sock_fd, &fds);
Mark Hammonda57ec932004-08-03 05:06:26 +00001826 FD_ZERO(&fds_exc);
1827 FD_SET(s->sock_fd, &fds_exc);
1828 res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001829 if (res == 0) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001830 res = WSAEWOULDBLOCK;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001831 timeout = 1;
Mark Hammonda57ec932004-08-03 05:06:26 +00001832 } else if (res > 0) {
1833 if (FD_ISSET(s->sock_fd, &fds))
Sean Reifscheider54cf12b2007-09-17 17:55:36 +00001834 /* The socket is in the writable set - this
Mark Hammonda57ec932004-08-03 05:06:26 +00001835 means connected */
1836 res = 0;
1837 else {
1838 /* As per MS docs, we need to call getsockopt()
1839 to get the underlying error */
1840 int res_size = sizeof res;
1841 /* It must be in the exception set */
1842 assert(FD_ISSET(s->sock_fd, &fds_exc));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001843 if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
Mark Hammonda57ec932004-08-03 05:06:26 +00001844 (char *)&res, &res_size))
1845 /* getsockopt also clears WSAGetLastError,
1846 so reset it back. */
1847 WSASetLastError(res);
1848 else
1849 res = WSAGetLastError();
1850 }
1851 }
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001852 /* else if (res < 0) an error occurred */
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001853 }
1854 }
1855
1856 if (res < 0)
1857 res = WSAGetLastError();
1858
1859#else
1860
1861 if (s->sock_timeout > 0.0) {
Neal Norwitz082b2df2006-02-07 07:04:46 +00001862 if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001863 timeout = internal_select(s, 1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001864 if (timeout == 0) {
1865 res = connect(s->sock_fd, addr, addrlen);
1866 if (res < 0 && errno == EISCONN)
1867 res = 0;
1868 }
1869 else if (timeout == -1)
1870 res = errno; /* had error */
1871 else
1872 res = EWOULDBLOCK; /* timed out */
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001873 }
1874 }
1875
1876 if (res < 0)
1877 res = errno;
1878
1879#endif
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001880 *timeoutp = timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001881
1882 return res;
1883}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001884
Fred Drake728819a2000-07-01 03:40:12 +00001885/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001886
Guido van Rossum73624e91994-10-10 17:59:00 +00001887static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001888sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001889{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001890 sock_addr_t addrbuf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001891 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001892 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001893 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001894
Thomas Wouters89f507f2006-12-13 04:49:30 +00001895 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001896 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001897
Guido van Rossum73624e91994-10-10 17:59:00 +00001898 Py_BEGIN_ALLOW_THREADS
Thomas Wouters89f507f2006-12-13 04:49:30 +00001899 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
Guido van Rossum11ba0942002-06-13 15:07:44 +00001900 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001901
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001902 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001903 PyErr_SetString(socket_timeout, "timed out");
1904 return NULL;
1905 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001906 if (res != 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001907 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001908 Py_INCREF(Py_None);
1909 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001910}
1911
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001912PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001913"connect(address)\n\
1914\n\
1915Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001916is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001917
Guido van Rossum30a685f1991-06-27 15:51:29 +00001918
Fred Drake728819a2000-07-01 03:40:12 +00001919/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001920
1921static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001922sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001923{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001924 sock_addr_t addrbuf;
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001925 int addrlen;
1926 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001927 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001928
Thomas Wouters89f507f2006-12-13 04:49:30 +00001929 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001930 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001931
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001932 Py_BEGIN_ALLOW_THREADS
Thomas Wouters89f507f2006-12-13 04:49:30 +00001933 res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001934 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001935
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001936 /* Signals are not errors (though they may raise exceptions). Adapted
1937 from PyErr_SetFromErrnoWithFilenameObject(). */
1938#ifdef EINTR
1939 if (res == EINTR && PyErr_CheckSignals())
1940 return NULL;
1941#endif
1942
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001943 return PyInt_FromLong((long) res);
1944}
1945
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001946PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001947"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001948\n\
1949This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001950instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001951
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001952
Guido van Rossumed233a51992-06-23 09:07:03 +00001953/* s.fileno() method */
1954
Guido van Rossum73624e91994-10-10 17:59:00 +00001955static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001956sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00001957{
Fred Drakea04eaad2000-06-30 02:46:07 +00001958#if SIZEOF_SOCKET_T <= SIZEOF_LONG
Guido van Rossum73624e91994-10-10 17:59:00 +00001959 return PyInt_FromLong((long) s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00001960#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001961 return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00001962#endif
Guido van Rossumed233a51992-06-23 09:07:03 +00001963}
1964
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001965PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001966"fileno() -> integer\n\
1967\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001968Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001969
Guido van Rossumed233a51992-06-23 09:07:03 +00001970
Guido van Rossumbe32c891996-06-20 16:25:29 +00001971#ifndef NO_DUP
1972/* s.dup() method */
1973
1974static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001975sock_dup(PySocketSockObject *s)
Guido van Rossumbe32c891996-06-20 16:25:29 +00001976{
Fred Drakea04eaad2000-06-30 02:46:07 +00001977 SOCKET_T newfd;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001978 PyObject *sock;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001979
Guido van Rossumbe32c891996-06-20 16:25:29 +00001980 newfd = dup(s->sock_fd);
1981 if (newfd < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001982 return s->errorhandler();
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001983 sock = (PyObject *) new_sockobject(newfd,
1984 s->sock_family,
1985 s->sock_type,
1986 s->sock_proto);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001987 if (sock == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001988 SOCKETCLOSE(newfd);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001989 return sock;
1990}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001991
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001992PyDoc_STRVAR(dup_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001993"dup() -> socket object\n\
1994\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001995Return a new socket object connected to the same system resource.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001996
Guido van Rossumbe32c891996-06-20 16:25:29 +00001997#endif
1998
1999
Guido van Rossumc89705d1992-11-26 08:54:07 +00002000/* s.getsockname() method */
2001
Guido van Rossum73624e91994-10-10 17:59:00 +00002002static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002003sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002004{
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002005 sock_addr_t addrbuf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002006 int res;
2007 socklen_t addrlen;
2008
Guido van Rossumc89705d1992-11-26 08:54:07 +00002009 if (!getsockaddrlen(s, &addrlen))
2010 return NULL;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002011 memset(&addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002012 Py_BEGIN_ALLOW_THREADS
Thomas Wouters89f507f2006-12-13 04:49:30 +00002013 res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002014 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00002015 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002016 return s->errorhandler();
Thomas Wouters89f507f2006-12-13 04:49:30 +00002017 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00002018 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002019}
2020
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002021PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002022"getsockname() -> address info\n\
2023\n\
2024Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002025info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002026
Guido van Rossumc89705d1992-11-26 08:54:07 +00002027
Guido van Rossumb6775db1994-08-01 11:34:53 +00002028#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002029/* s.getpeername() method */
2030
Guido van Rossum73624e91994-10-10 17:59:00 +00002031static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002032sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00002033{
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002034 sock_addr_t addrbuf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002035 int res;
2036 socklen_t addrlen;
2037
Guido van Rossumc89705d1992-11-26 08:54:07 +00002038 if (!getsockaddrlen(s, &addrlen))
2039 return NULL;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002040 memset(&addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002041 Py_BEGIN_ALLOW_THREADS
Thomas Wouters89f507f2006-12-13 04:49:30 +00002042 res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002043 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00002044 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002045 return s->errorhandler();
Thomas Wouters89f507f2006-12-13 04:49:30 +00002046 return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00002047 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002048}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002049
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002050PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002051"getpeername() -> address info\n\
2052\n\
2053Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002054info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002055
Guido van Rossumb6775db1994-08-01 11:34:53 +00002056#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00002057
2058
Guido van Rossum30a685f1991-06-27 15:51:29 +00002059/* s.listen(n) method */
2060
Guido van Rossum73624e91994-10-10 17:59:00 +00002061static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002062sock_listen(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002063{
2064 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002065 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002066
2067 backlog = PyInt_AsLong(arg);
2068 if (backlog == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002069 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002070 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +00002071 if (backlog < 1)
2072 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002073 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +00002074 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002075 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002076 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002077 Py_INCREF(Py_None);
2078 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002079}
2080
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002081PyDoc_STRVAR(listen_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002082"listen(backlog)\n\
2083\n\
2084Enable a server to accept connections. The backlog argument must be at\n\
2085least 1; it specifies the number of unaccepted connection that the system\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002086will allow before refusing new connections.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002087
2088
Thomas Wouters477c8d52006-05-27 19:21:47 +00002089/*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002090 * This is the guts of the recv() and recv_into() methods, which reads into a
Thomas Wouters902d6eb2007-01-09 23:18:33 +00002091 * char buffer. If you have any inc/dec ref to do to the objects that contain
Thomas Wouters477c8d52006-05-27 19:21:47 +00002092 * the buffer, do it in the caller. This function returns the number of bytes
2093 * succesfully read. If there was an error, it returns -1. Note that it is
2094 * also possible that we return a number of bytes smaller than the request
2095 * bytes.
2096 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002097static ssize_t
Thomas Wouters477c8d52006-05-27 19:21:47 +00002098sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags)
2099{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002100 ssize_t outlen = -1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002101 int timeout;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002102#ifdef __VMS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002103 int remaining;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002104 char *read_buf;
2105#endif
2106
2107 if (!IS_SELECTABLE(s)) {
2108 select_error();
2109 return -1;
2110 }
Guido van Rossumc2de7c02007-08-03 22:27:51 +00002111 if (len == 0) {
2112 /* If 0 bytes were requested, do nothing. */
2113 return 0;
2114 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002115
2116#ifndef __VMS
2117 Py_BEGIN_ALLOW_THREADS
2118 timeout = internal_select(s, 0);
2119 if (!timeout)
2120 outlen = recv(s->sock_fd, cbuf, len, flags);
2121 Py_END_ALLOW_THREADS
2122
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002123 if (timeout == 1) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002124 PyErr_SetString(socket_timeout, "timed out");
2125 return -1;
2126 }
2127 if (outlen < 0) {
2128 /* Note: the call to errorhandler() ALWAYS indirectly returned
2129 NULL, so ignore its return value */
2130 s->errorhandler();
2131 return -1;
2132 }
2133#else
2134 read_buf = cbuf;
2135 remaining = len;
2136 while (remaining != 0) {
2137 unsigned int segment;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002138 int nread = -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002139
2140 segment = remaining /SEGMENT_SIZE;
2141 if (segment != 0) {
2142 segment = SEGMENT_SIZE;
2143 }
2144 else {
2145 segment = remaining;
2146 }
2147
2148 Py_BEGIN_ALLOW_THREADS
2149 timeout = internal_select(s, 0);
2150 if (!timeout)
2151 nread = recv(s->sock_fd, read_buf, segment, flags);
2152 Py_END_ALLOW_THREADS
2153
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002154 if (timeout == 1) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002155 PyErr_SetString(socket_timeout, "timed out");
2156 return -1;
2157 }
2158 if (nread < 0) {
2159 s->errorhandler();
2160 return -1;
2161 }
2162 if (nread != remaining) {
2163 read_buf += nread;
2164 break;
2165 }
2166
2167 remaining -= segment;
2168 read_buf += segment;
2169 }
2170 outlen = read_buf - cbuf;
2171#endif /* !__VMS */
2172
2173 return outlen;
2174}
2175
Guido van Rossum48a680c2001-03-02 06:34:14 +00002176
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002177/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002178
Guido van Rossum73624e91994-10-10 17:59:00 +00002179static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002180sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002181{
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002182 int recvlen, flags = 0;
2183 ssize_t outlen;
Guido van Rossum73624e91994-10-10 17:59:00 +00002184 PyObject *buf;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002185
Thomas Wouters477c8d52006-05-27 19:21:47 +00002186 if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002187 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002188
Thomas Wouters477c8d52006-05-27 19:21:47 +00002189 if (recvlen < 0) {
Martin v. Löwisfba64e12001-11-19 10:41:26 +00002190 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00002191 "negative buffersize in recv");
Martin v. Löwisfba64e12001-11-19 10:41:26 +00002192 return NULL;
2193 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002194
Thomas Wouters477c8d52006-05-27 19:21:47 +00002195 /* Allocate a new string. */
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002196 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002197 if (buf == NULL)
2198 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002199
Thomas Wouters477c8d52006-05-27 19:21:47 +00002200 /* Call the guts */
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002201 outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002202 if (outlen < 0) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002203 /* An error occurred, release the string and return an
Thomas Wouters477c8d52006-05-27 19:21:47 +00002204 error. */
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002205 Py_DECREF(buf);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002206 return NULL;
2207 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002208 if (outlen != recvlen) {
2209 /* We did not read as many bytes as we anticipated, resize the
Kurt B. Kaisere4050372007-08-22 21:38:31 +00002210 string if possible and be successful. */
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002211 if (PyBytes_Resize(buf, outlen) < 0)
Kurt B. Kaisere4050372007-08-22 21:38:31 +00002212 /* Oopsy, not so successful after all. */
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002213 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002214 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002215
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002216 return buf;
2217}
2218
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002219PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002220"recv(buffersize[, flags]) -> data\n\
2221\n\
2222Receive up to buffersize bytes from the socket. For the optional flags\n\
2223argument, see the Unix manual. When no data is available, block until\n\
2224at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002225the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002226
Guido van Rossum30a685f1991-06-27 15:51:29 +00002227
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002228/* s.recv_into(buffer, [nbytes [,flags]]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002229
Thomas Wouters477c8d52006-05-27 19:21:47 +00002230static PyObject*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002231sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002232{
2233 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
2234
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002235 int recvlen = 0, flags = 0;
2236 ssize_t readlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002237 char *buf;
2238 int buflen;
2239
2240 /* Get the buffer's memory */
Guido van Rossumd8faa362007-04-27 19:54:29 +00002241 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recv_into", kwlist,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002242 &buf, &buflen, &recvlen, &flags))
2243 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002244
2245 if (recvlen < 0) {
2246 PyErr_SetString(PyExc_ValueError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002247 "negative buffersize in recv_into");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002248 return NULL;
2249 }
2250 if (recvlen == 0) {
2251 /* If nbytes was not specified, use the buffer's length */
2252 recvlen = buflen;
2253 }
2254
2255 /* Check if the buffer is large enough */
2256 if (buflen < recvlen) {
2257 PyErr_SetString(PyExc_ValueError,
2258 "buffer too small for requested bytes");
2259 return NULL;
2260 }
2261
2262 /* Call the guts */
2263 readlen = sock_recv_guts(s, buf, recvlen, flags);
2264 if (readlen < 0) {
2265 /* Return an error. */
2266 return NULL;
2267 }
2268
2269 /* Return the number of bytes read. Note that we do not do anything
2270 special here in the case that readlen < recvlen. */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002271 return PyInt_FromSsize_t(readlen);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002272}
2273
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002274PyDoc_STRVAR(recv_into_doc,
2275"recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002276\n\
2277A version of recv() that stores its data into a buffer rather than creating \n\
2278a new string. Receive up to buffersize bytes from the socket. If buffersize \n\
2279is not specified (or 0), receive up to the size available in the given buffer.\n\
2280\n\
2281See recv() for documentation about the flags.");
2282
2283
2284/*
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002285 * This is the guts of the recv() and recv_into() methods, which reads into a
Thomas Wouters477c8d52006-05-27 19:21:47 +00002286 * char buffer. If you have any inc/def ref to do to the objects that contain
2287 * the buffer, do it in the caller. This function returns the number of bytes
2288 * succesfully read. If there was an error, it returns -1. Note that it is
2289 * also possible that we return a number of bytes smaller than the request
2290 * bytes.
2291 *
2292 * 'addr' is a return value for the address object. Note that you must decref
2293 * it yourself.
2294 */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002295static ssize_t
Thomas Wouters477c8d52006-05-27 19:21:47 +00002296sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags,
2297 PyObject** addr)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002298{
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002299 sock_addr_t addrbuf;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002300 int timeout;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002301 ssize_t n = -1;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002302 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002303
Thomas Wouters477c8d52006-05-27 19:21:47 +00002304 *addr = NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002305
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00002306 if (!getsockaddrlen(s, &addrlen))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002307 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002308
Thomas Wouters477c8d52006-05-27 19:21:47 +00002309 if (!IS_SELECTABLE(s)) {
2310 select_error();
2311 return -1;
2312 }
Neal Norwitz082b2df2006-02-07 07:04:46 +00002313
Guido van Rossum73624e91994-10-10 17:59:00 +00002314 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002315 memset(&addrbuf, 0, addrlen);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002316 timeout = internal_select(s, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002317 if (!timeout) {
Guido van Rossum8d665e61996-06-26 18:22:49 +00002318#ifndef MS_WINDOWS
Andrew MacIntyreba43e872002-03-03 03:03:52 +00002319#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002320 n = recvfrom(s->sock_fd, cbuf, len, flags,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002321 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum32c575d1997-12-02 20:37:32 +00002322#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002323 n = recvfrom(s->sock_fd, cbuf, len, flags,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002324 (void *) &addrbuf, &addrlen);
Guido van Rossum32c575d1997-12-02 20:37:32 +00002325#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002326#else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002327 n = recvfrom(s->sock_fd, cbuf, len, flags,
Thomas Wouters89f507f2006-12-13 04:49:30 +00002328 SAS2SA(&addrbuf), &addrlen);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002329#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002330 }
Guido van Rossum73624e91994-10-10 17:59:00 +00002331 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002332
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002333 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002334 PyErr_SetString(socket_timeout, "timed out");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002335 return -1;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002336 }
Guido van Rossum7c53b771995-09-13 18:39:47 +00002337 if (n < 0) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002338 s->errorhandler();
2339 return -1;
Guido van Rossum7c53b771995-09-13 18:39:47 +00002340 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002341
Thomas Wouters89f507f2006-12-13 04:49:30 +00002342 if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
Thomas Wouters477c8d52006-05-27 19:21:47 +00002343 addrlen, s->sock_proto)))
2344 return -1;
2345
2346 return n;
2347}
2348
2349/* s.recvfrom(nbytes [,flags]) method */
2350
2351static PyObject *
2352sock_recvfrom(PySocketSockObject *s, PyObject *args)
2353{
2354 PyObject *buf = NULL;
2355 PyObject *addr = NULL;
2356 PyObject *ret = NULL;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002357 int recvlen, flags = 0;
2358 ssize_t outlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002359
2360 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002361 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002362
Guido van Rossumd8faa362007-04-27 19:54:29 +00002363 if (recvlen < 0) {
2364 PyErr_SetString(PyExc_ValueError,
2365 "negative buffersize in recvfrom");
2366 return NULL;
2367 }
2368
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002369 buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002370 if (buf == NULL)
2371 return NULL;
2372
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002373 outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf),
Thomas Wouters477c8d52006-05-27 19:21:47 +00002374 recvlen, flags, &addr);
2375 if (outlen < 0) {
Barry Warsaw752300b1997-01-03 17:18:10 +00002376 goto finally;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002377 }
2378
2379 if (outlen != recvlen) {
2380 /* We did not read as many bytes as we anticipated, resize the
2381 string if possible and be succesful. */
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002382 if (PyBytes_Resize(buf, outlen) < 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002383 /* Oopsy, not so succesful after all. */
2384 goto finally;
2385 }
Barry Warsaw752300b1997-01-03 17:18:10 +00002386
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002387 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002388
2389finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00002390 Py_XDECREF(buf);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002391 Py_XDECREF(addr);
Guido van Rossum6f5afc91993-02-05 09:46:15 +00002392 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002393}
2394
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002395PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002396"recvfrom(buffersize[, flags]) -> (data, address info)\n\
2397\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002398Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002399
Thomas Wouters477c8d52006-05-27 19:21:47 +00002400
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002401/* s.recvfrom_into(buffer[, nbytes [,flags]]) method */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002402
2403static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002404sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002405{
2406 static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
2407
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002408 int recvlen = 0, flags = 0;
2409 ssize_t readlen;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002410 char *buf;
2411 int buflen;
2412
2413 PyObject *addr = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002414
Guido van Rossumd8faa362007-04-27 19:54:29 +00002415 if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recvfrom_into",
2416 kwlist, &buf, &buflen,
2417 &recvlen, &flags))
Thomas Wouters477c8d52006-05-27 19:21:47 +00002418 return NULL;
2419 assert(buf != 0 && buflen > 0);
2420
2421 if (recvlen < 0) {
2422 PyErr_SetString(PyExc_ValueError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002423 "negative buffersize in recvfrom_into");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002424 return NULL;
2425 }
2426 if (recvlen == 0) {
2427 /* If nbytes was not specified, use the buffer's length */
2428 recvlen = buflen;
2429 }
2430
2431 readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
2432 if (readlen < 0) {
2433 /* Return an error */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002434 Py_XDECREF(addr);
2435 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002436 }
2437
2438 /* Return the number of bytes read and the address. Note that we do
2439 not do anything special here in the case that readlen < recvlen. */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002440 return Py_BuildValue("lN", readlen, addr);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002441}
2442
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002443PyDoc_STRVAR(recvfrom_into_doc,
2444"recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00002445\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002446Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002447
2448
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002449/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002450
Guido van Rossum73624e91994-10-10 17:59:00 +00002451static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002452sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002453{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002454 char *buf;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002455 int len, n = -1, flags = 0, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002456
Guido van Rossum43713e52000-02-29 13:59:29 +00002457 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002458 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002459
Neal Norwitz082b2df2006-02-07 07:04:46 +00002460 if (!IS_SELECTABLE(s))
2461 return select_error();
2462
Guido van Rossum73624e91994-10-10 17:59:00 +00002463 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002464 timeout = internal_select(s, 1);
2465 if (!timeout)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002466#ifdef __VMS
2467 n = sendsegmented(s->sock_fd, buf, len, flags);
2468#else
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002469 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002470#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00002471 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002472
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002473 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002474 PyErr_SetString(socket_timeout, "timed out");
2475 return NULL;
2476 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00002477 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002478 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002479 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00002480}
2481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002482PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002483"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002484\n\
2485Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002486argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002487sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002488
2489
2490/* s.sendall(data [,flags]) method */
2491
2492static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002493sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002494{
2495 char *buf;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002496 int len, n = -1, flags = 0, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002497
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002498 if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags))
2499 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002500
Neal Norwitz082b2df2006-02-07 07:04:46 +00002501 if (!IS_SELECTABLE(s))
2502 return select_error();
2503
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002504 Py_BEGIN_ALLOW_THREADS
2505 do {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002506 timeout = internal_select(s, 1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002507 n = -1;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002508 if (timeout)
2509 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002510#ifdef __VMS
2511 n = sendsegmented(s->sock_fd, buf, len, flags);
2512#else
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002513 n = send(s->sock_fd, buf, len, flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002514#endif
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002515 if (n < 0)
2516 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002517 buf += n;
2518 len -= n;
2519 } while (len > 0);
2520 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002521
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002522 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002523 PyErr_SetString(socket_timeout, "timed out");
2524 return NULL;
2525 }
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002526 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002527 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00002528
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002529 Py_INCREF(Py_None);
2530 return Py_None;
2531}
2532
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002533PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002534"sendall(data[, flags])\n\
2535\n\
2536Send a data string to the socket. For the optional flags\n\
2537argument, see the Unix manual. This calls send() repeatedly\n\
2538until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002539to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002540
Guido van Rossum30a685f1991-06-27 15:51:29 +00002541
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002542/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002543
Guido van Rossum73624e91994-10-10 17:59:00 +00002544static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002545sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002546{
Guido van Rossum73624e91994-10-10 17:59:00 +00002547 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002548 char *buf;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002549 sock_addr_t addrbuf;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002550 int addrlen, len, n = -1, flags, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002551
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002552 flags = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002553 if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00002554 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002555 if (!PyArg_ParseTuple(args, "s#iO:sendto",
2556 &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002557 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002558 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002559
Neal Norwitz082b2df2006-02-07 07:04:46 +00002560 if (!IS_SELECTABLE(s))
2561 return select_error();
2562
Thomas Wouters89f507f2006-12-13 04:49:30 +00002563 if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
2564 return NULL;
2565
Guido van Rossum73624e91994-10-10 17:59:00 +00002566 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002567 timeout = internal_select(s, 1);
2568 if (!timeout)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002569 n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002570 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002571
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002572 if (timeout == 1) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002573 PyErr_SetString(socket_timeout, "timed out");
2574 return NULL;
2575 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002576 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002577 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002578 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002579}
2580
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002581PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002582"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002583\n\
2584Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002585For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002586
Guido van Rossum30a685f1991-06-27 15:51:29 +00002587
2588/* s.shutdown(how) method */
2589
Guido van Rossum73624e91994-10-10 17:59:00 +00002590static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002591sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002592{
2593 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002594 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002595
2596 how = PyInt_AsLong(arg);
2597 if (how == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002598 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002599 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002600 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00002601 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002602 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002603 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002604 Py_INCREF(Py_None);
2605 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002606}
2607
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002608PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002609"shutdown(flag)\n\
2610\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00002611Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
2612of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002613
Guido van Rossum30a685f1991-06-27 15:51:29 +00002614
2615/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002616
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002617static PyMethodDef sock_methods[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002618 {"accept", (PyCFunction)sock_accept, METH_NOARGS,
2619 accept_doc},
2620 {"bind", (PyCFunction)sock_bind, METH_O,
2621 bind_doc},
2622 {"close", (PyCFunction)sock_close, METH_NOARGS,
2623 close_doc},
2624 {"connect", (PyCFunction)sock_connect, METH_O,
2625 connect_doc},
2626 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
2627 connect_ex_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002628#ifndef NO_DUP
Thomas Wouters477c8d52006-05-27 19:21:47 +00002629 {"dup", (PyCFunction)sock_dup, METH_NOARGS,
2630 dup_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002631#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00002632 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
2633 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002634#ifdef HAVE_GETPEERNAME
Thomas Wouters477c8d52006-05-27 19:21:47 +00002635 {"getpeername", (PyCFunction)sock_getpeername,
2636 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00002637#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00002638 {"getsockname", (PyCFunction)sock_getsockname,
2639 METH_NOARGS, getsockname_doc},
2640 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
2641 getsockopt_doc},
2642 {"listen", (PyCFunction)sock_listen, METH_O,
2643 listen_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002644 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
2645 recv_doc},
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002646 {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
2647 recv_into_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002648 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
2649 recvfrom_doc},
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002650 {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
2651 recvfrom_into_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002652 {"send", (PyCFunction)sock_send, METH_VARARGS,
2653 send_doc},
2654 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
2655 sendall_doc},
2656 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
2657 sendto_doc},
2658 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
2659 setblocking_doc},
2660 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
2661 settimeout_doc},
2662 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
2663 gettimeout_doc},
2664 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
2665 setsockopt_doc},
2666 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
2667 shutdown_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002668 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002669};
2670
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002671/* SockObject members */
2672static PyMemberDef sock_memberlist[] = {
2673 {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},
2674 {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},
2675 {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},
2676 {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"},
2677 {0},
2678};
Guido van Rossum30a685f1991-06-27 15:51:29 +00002679
Guido van Rossum73624e91994-10-10 17:59:00 +00002680/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00002681 First close the file description. */
2682
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002683static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002684sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002685{
Guido van Rossumfa972c92000-04-10 12:45:45 +00002686 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00002687 (void) SOCKETCLOSE(s->sock_fd);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002688 Py_Type(s)->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002689}
2690
Guido van Rossum30a685f1991-06-27 15:51:29 +00002691
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002692static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002693sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002694{
Fred Drakea04eaad2000-06-30 02:46:07 +00002695#if SIZEOF_SOCKET_T > SIZEOF_LONG
2696 if (s->sock_fd > LONG_MAX) {
2697 /* this can occur on Win64, and actually there is a special
2698 ugly printf formatter for decimal pointer length integer
2699 printing, only bother if necessary*/
2700 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002701 "no printf formatter to display "
2702 "the socket descriptor in decimal");
Fred Drakea04eaad2000-06-30 02:46:07 +00002703 return NULL;
2704 }
2705#endif
Walter Dörwalda7eb93e2007-06-05 13:41:53 +00002706 return PyUnicode_FromFormat(
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002707 "<socket object, fd=%ld, family=%d, type=%d, proto=%d>",
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002708 (long)s->sock_fd, s->sock_family,
2709 s->sock_type,
2710 s->sock_proto);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002711}
2712
2713
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002714/* Create a new, uninitialized socket object. */
2715
2716static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002717sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002718{
2719 PyObject *new;
2720
2721 new = type->tp_alloc(type, 0);
Guido van Rossume1c478f2002-06-06 20:08:25 +00002722 if (new != NULL) {
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002723 ((PySocketSockObject *)new)->sock_fd = -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002724 ((PySocketSockObject *)new)->sock_timeout = -1.0;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002725 ((PySocketSockObject *)new)->errorhandler = &set_error;
Guido van Rossume1c478f2002-06-06 20:08:25 +00002726 }
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002727 return new;
2728}
2729
2730
2731/* Initialize a new socket object. */
2732
2733/*ARGSUSED*/
2734static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002735sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002736{
2737 PySocketSockObject *s = (PySocketSockObject *)self;
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002738 PyObject *fdobj = NULL;
2739 SOCKET_T fd = INVALID_SOCKET;
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002740 int family = AF_INET, type = SOCK_STREAM, proto = 0;
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002741 static char *keywords[] = {"family", "type", "proto", "fileno", 0};
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002742
2743 if (!PyArg_ParseTupleAndKeywords(args, kwds,
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002744 "|iiiO:socket", keywords,
2745 &family, &type, &proto, &fdobj))
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002746 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002747
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002748 if (fdobj != NULL) {
2749 fd = PyLong_AsLongLong(fdobj);
2750 if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
2751 return -1;
2752 if (fd == INVALID_SOCKET) {
2753 PyErr_SetString(PyExc_ValueError,
2754 "can't use invalid socket value");
2755 return -1;
2756 }
2757 }
2758 else {
2759 Py_BEGIN_ALLOW_THREADS
2760 fd = socket(family, type, proto);
2761 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002762
Guido van Rossum7d0a8262007-05-21 23:13:11 +00002763 if (fd == INVALID_SOCKET) {
2764 set_error();
2765 return -1;
2766 }
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002767 }
2768 init_sockobject(s, fd, family, type, proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002769
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002770 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002771
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002772}
2773
2774
Guido van Rossumb6775db1994-08-01 11:34:53 +00002775/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002776
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002777static PyTypeObject sock_type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002778 PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
Guido van Rossumca6dfa52001-10-28 12:31:33 +00002779 "_socket.socket", /* tp_name */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002780 sizeof(PySocketSockObject), /* tp_basicsize */
2781 0, /* tp_itemsize */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002782 (destructor)sock_dealloc, /* tp_dealloc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002783 0, /* tp_print */
2784 0, /* tp_getattr */
2785 0, /* tp_setattr */
2786 0, /* tp_compare */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002787 (reprfunc)sock_repr, /* tp_repr */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002788 0, /* tp_as_number */
2789 0, /* tp_as_sequence */
2790 0, /* tp_as_mapping */
2791 0, /* tp_hash */
2792 0, /* tp_call */
2793 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002794 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002795 0, /* tp_setattro */
2796 0, /* tp_as_buffer */
2797 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002798 sock_doc, /* tp_doc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002799 0, /* tp_traverse */
2800 0, /* tp_clear */
2801 0, /* tp_richcompare */
2802 0, /* tp_weaklistoffset */
2803 0, /* tp_iter */
2804 0, /* tp_iternext */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002805 sock_methods, /* tp_methods */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002806 sock_memberlist, /* tp_members */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002807 0, /* tp_getset */
2808 0, /* tp_base */
2809 0, /* tp_dict */
2810 0, /* tp_descr_get */
2811 0, /* tp_descr_set */
2812 0, /* tp_dictoffset */
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002813 sock_initobj, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002814 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002815 sock_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002816 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002817};
2818
Guido van Rossum30a685f1991-06-27 15:51:29 +00002819
Guido van Rossum81194471991-07-27 21:42:02 +00002820/* Python interface to gethostname(). */
2821
2822/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002823static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002824socket_gethostname(PyObject *self, PyObject *unused)
Guido van Rossum81194471991-07-27 21:42:02 +00002825{
2826 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00002827 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00002828 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002829 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00002830 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002831 if (res < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002832 return set_error();
Guido van Rossum81194471991-07-27 21:42:02 +00002833 buf[sizeof buf - 1] = '\0';
Guido van Rossum32c4ac02007-08-15 03:56:40 +00002834 return PyUnicode_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00002835}
Guido van Rossumff4949e1992-08-05 19:58:53 +00002836
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002837PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002838"gethostname() -> string\n\
2839\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002840Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002841
Guido van Rossumff4949e1992-08-05 19:58:53 +00002842
Guido van Rossum30a685f1991-06-27 15:51:29 +00002843/* Python interface to gethostbyname(name). */
2844
2845/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002846static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002847socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002848{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002849 char *name;
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00002850 sock_addr_t addrbuf;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002851
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002852 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002853 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002854 if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002855 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002856 return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002857}
2858
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002859PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002860"gethostbyname(host) -> address\n\
2861\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002862Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002863
2864
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002865/* Convenience function common to gethostbyname_ex and gethostbyaddr */
2866
2867static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002868gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002869{
2870 char **pch;
2871 PyObject *rtn_tuple = (PyObject *)NULL;
2872 PyObject *name_list = (PyObject *)NULL;
2873 PyObject *addr_list = (PyObject *)NULL;
2874 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002875
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002876 if (h == NULL) {
Guido van Rossum48a680c2001-03-02 06:34:14 +00002877 /* Let's get real error message to return */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002878 set_herror(h_errno);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002879 return NULL;
2880 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002881
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002882 if (h->h_addrtype != af) {
2883#ifdef HAVE_STRERROR
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002884 /* Let's get real error message to return */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002885 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002886 (char *)strerror(EAFNOSUPPORT));
2887#else
2888 PyErr_SetString(
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002889 socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002890 "Address family not supported by protocol family");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002891#endif
2892 return NULL;
2893 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002894
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002895 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002896
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002897 case AF_INET:
2898 if (alen < sizeof(struct sockaddr_in))
2899 return NULL;
2900 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002901
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002902#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002903 case AF_INET6:
2904 if (alen < sizeof(struct sockaddr_in6))
2905 return NULL;
2906 break;
2907#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002908
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002909 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002910
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002911 if ((name_list = PyList_New(0)) == NULL)
2912 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002913
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002914 if ((addr_list = PyList_New(0)) == NULL)
2915 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002916
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002917 /* SF #1511317: h_aliases can be NULL */
2918 if (h->h_aliases) {
2919 for (pch = h->h_aliases; *pch != NULL; pch++) {
2920 int status;
Guido van Rossum32c4ac02007-08-15 03:56:40 +00002921 tmp = PyUnicode_FromString(*pch);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002922 if (tmp == NULL)
2923 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002924
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002925 status = PyList_Append(name_list, tmp);
2926 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002927
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002928 if (status)
2929 goto err;
2930 }
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002931 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002932
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002933 for (pch = h->h_addr_list; *pch != NULL; pch++) {
2934 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002935
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002936 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002937
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002938 case AF_INET:
2939 {
2940 struct sockaddr_in sin;
2941 memset(&sin, 0, sizeof(sin));
2942 sin.sin_family = af;
2943#ifdef HAVE_SOCKADDR_SA_LEN
2944 sin.sin_len = sizeof(sin);
2945#endif
2946 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
2947 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002948
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002949 if (pch == h->h_addr_list && alen >= sizeof(sin))
2950 memcpy((char *) addr, &sin, sizeof(sin));
2951 break;
2952 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002953
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002954#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002955 case AF_INET6:
2956 {
2957 struct sockaddr_in6 sin6;
2958 memset(&sin6, 0, sizeof(sin6));
2959 sin6.sin6_family = af;
2960#ifdef HAVE_SOCKADDR_SA_LEN
2961 sin6.sin6_len = sizeof(sin6);
2962#endif
2963 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
2964 tmp = makeipaddr((struct sockaddr *)&sin6,
2965 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002966
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002967 if (pch == h->h_addr_list && alen >= sizeof(sin6))
2968 memcpy((char *) addr, &sin6, sizeof(sin6));
2969 break;
2970 }
2971#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002972
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002973 default: /* can't happen */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002974 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002975 "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002976 return NULL;
2977 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002978
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002979 if (tmp == NULL)
2980 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002981
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002982 status = PyList_Append(addr_list, tmp);
2983 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002984
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002985 if (status)
2986 goto err;
2987 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002988
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002989 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002990
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002991 err:
2992 Py_XDECREF(name_list);
2993 Py_XDECREF(addr_list);
2994 return rtn_tuple;
2995}
2996
2997
2998/* Python interface to gethostbyname_ex(name). */
2999
3000/*ARGSUSED*/
3001static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003002socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003003{
3004 char *name;
3005 struct hostent *h;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00003006#ifdef ENABLE_IPV6
3007 struct sockaddr_storage addr;
3008#else
3009 struct sockaddr_in addr;
3010#endif
Martin v. Löwis9db2f572001-07-23 01:30:10 +00003011 struct sockaddr *sa;
Guido van Rossum955becc1999-03-22 20:14:53 +00003012 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003013#ifdef HAVE_GETHOSTBYNAME_R
3014 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003015#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
3016 struct hostent_data data;
3017#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003018 char buf[16384];
3019 int buf_len = (sizeof buf) - 1;
3020 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003021#endif
3022#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003023 int result;
3024#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003025#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003026
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003027 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003028 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003029 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003030 return NULL;
3031 Py_BEGIN_ALLOW_THREADS
3032#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003033#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003034 result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
3035 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003036#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003037 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003038#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00003039 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003040 result = gethostbyname_r(name, &hp_allocated, &data);
3041 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003042#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003043#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003044#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00003045 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003046#endif
3047 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003048#endif /* HAVE_GETHOSTBYNAME_R */
3049 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00003050 /* Some C libraries would require addr.__ss_family instead of
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003051 addr.ss_family.
3052 Therefore, we cast the sockaddr_storage into sockaddr to
3053 access sa_family. */
Martin v. Löwis9db2f572001-07-23 01:30:10 +00003054 sa = (struct sockaddr*)&addr;
Guido van Rossum67f7a382002-06-06 21:08:16 +00003055 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003056 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003057#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00003058 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00003059#endif
3060 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003061}
3062
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003063PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003064"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
3065\n\
3066Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003067for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00003068
3069
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003070/* Python interface to gethostbyaddr(IP). */
3071
3072/*ARGSUSED*/
3073static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003074socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003075{
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003076#ifdef ENABLE_IPV6
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003077 struct sockaddr_storage addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003078#else
Guido van Rossum48a680c2001-03-02 06:34:14 +00003079 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003080#endif
3081 struct sockaddr *sa = (struct sockaddr *)&addr;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003082 char *ip_num;
3083 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00003084 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003085#ifdef HAVE_GETHOSTBYNAME_R
3086 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003087#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
3088 struct hostent_data data;
3089#else
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003090 char buf[16384];
3091 int buf_len = (sizeof buf) - 1;
3092 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003093#endif
3094#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003095 int result;
3096#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003097#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003098 char *ap;
3099 int al;
3100 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003101
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003102 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003103 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003104 af = AF_UNSPEC;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00003105 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003106 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003107 af = sa->sa_family;
3108 ap = NULL;
3109 al = 0;
3110 switch (af) {
3111 case AF_INET:
3112 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
3113 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3114 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003115#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003116 case AF_INET6:
3117 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
3118 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3119 break;
3120#endif
3121 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003122 PyErr_SetString(socket_error, "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003123 return NULL;
3124 }
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003125 Py_BEGIN_ALLOW_THREADS
3126#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003127#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003128 result = gethostbyaddr_r(ap, al, af,
3129 &hp_allocated, buf, buf_len,
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003130 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003131#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003132 h = gethostbyaddr_r(ap, al, af,
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003133 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003134#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00003135 memset((void *) &data, '\0', sizeof(data));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003136 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00003137 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00003138#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003139#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00003140#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00003141 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003142#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003143 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00003144#endif /* HAVE_GETHOSTBYNAME_R */
3145 Py_END_ALLOW_THREADS
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003146 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003147#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00003148 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00003149#endif
3150 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00003151}
3152
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003153PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003154"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
3155\n\
3156Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003157for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003158
Guido van Rossum30a685f1991-06-27 15:51:29 +00003159
3160/* Python interface to getservbyname(name).
3161 This only returns the port number, since the other info is already
3162 known or not useful (like the list of aliases). */
3163
3164/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00003165static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003166socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00003167{
Barry Warsaw11b91a02004-06-28 00:50:43 +00003168 char *name, *proto=NULL;
Guido van Rossum30a685f1991-06-27 15:51:29 +00003169 struct servent *sp;
Barry Warsaw11b91a02004-06-28 00:50:43 +00003170 if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00003171 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00003172 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00003173 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00003174 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00003175 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003176 PyErr_SetString(socket_error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00003177 return NULL;
3178 }
Guido van Rossum73624e91994-10-10 17:59:00 +00003179 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00003180}
3181
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003182PyDoc_STRVAR(getservbyname_doc,
Barry Warsaw11b91a02004-06-28 00:50:43 +00003183"getservbyname(servicename[, protocolname]) -> integer\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00003184\n\
3185Return a port number from a service name and protocol name.\n\
Barry Warsaw11b91a02004-06-28 00:50:43 +00003186The optional protocol name, if given, should be 'tcp' or 'udp',\n\
3187otherwise any protocol will match.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003188
Guido van Rossum30a685f1991-06-27 15:51:29 +00003189
Barry Warsaw11b91a02004-06-28 00:50:43 +00003190/* Python interface to getservbyport(port).
3191 This only returns the service name, since the other info is already
3192 known or not useful (like the list of aliases). */
3193
3194/*ARGSUSED*/
3195static PyObject *
3196socket_getservbyport(PyObject *self, PyObject *args)
3197{
Raymond Hettinger87de0ca2004-09-28 02:19:40 +00003198 unsigned short port;
Barry Warsaw11b91a02004-06-28 00:50:43 +00003199 char *proto=NULL;
3200 struct servent *sp;
Raymond Hettinger87de0ca2004-09-28 02:19:40 +00003201 if (!PyArg_ParseTuple(args, "H|s:getservbyport", &port, &proto))
Barry Warsaw11b91a02004-06-28 00:50:43 +00003202 return NULL;
3203 Py_BEGIN_ALLOW_THREADS
3204 sp = getservbyport(htons(port), proto);
3205 Py_END_ALLOW_THREADS
3206 if (sp == NULL) {
3207 PyErr_SetString(socket_error, "port/proto not found");
3208 return NULL;
3209 }
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003210 return PyUnicode_FromString(sp->s_name);
Barry Warsaw11b91a02004-06-28 00:50:43 +00003211}
3212
3213PyDoc_STRVAR(getservbyport_doc,
3214"getservbyport(port[, protocolname]) -> string\n\
3215\n\
3216Return the service name from a port number and protocol name.\n\
3217The optional protocol name, if given, should be 'tcp' or 'udp',\n\
3218otherwise any protocol will match.");
3219
Guido van Rossum3901d851996-12-19 16:35:04 +00003220/* Python interface to getprotobyname(name).
3221 This only returns the protocol number, since the other info is
3222 already known or not useful (like the list of aliases). */
3223
3224/*ARGSUSED*/
3225static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003226socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00003227{
3228 char *name;
3229 struct protoent *sp;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003230 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00003231 return NULL;
3232 Py_BEGIN_ALLOW_THREADS
3233 sp = getprotobyname(name);
3234 Py_END_ALLOW_THREADS
3235 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003236 PyErr_SetString(socket_error, "protocol not found");
Guido van Rossum3901d851996-12-19 16:35:04 +00003237 return NULL;
3238 }
3239 return PyInt_FromLong((long) sp->p_proto);
3240}
3241
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003242PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003243"getprotobyname(name) -> integer\n\
3244\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003245Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003246
Guido van Rossum3901d851996-12-19 16:35:04 +00003247
Dave Cole331708b2004-08-09 04:51:41 +00003248#ifdef HAVE_SOCKETPAIR
3249/* Create a pair of sockets using the socketpair() function.
Dave Cole07fda7e2004-08-23 05:16:23 +00003250 Arguments as for socket() except the default family is AF_UNIX if
Dave Colee8bbfe42004-08-26 00:51:16 +00003251 defined on the platform; otherwise, the default is AF_INET. */
Dave Cole331708b2004-08-09 04:51:41 +00003252
3253/*ARGSUSED*/
3254static PyObject *
3255socket_socketpair(PyObject *self, PyObject *args)
3256{
3257 PySocketSockObject *s0 = NULL, *s1 = NULL;
3258 SOCKET_T sv[2];
3259 int family, type = SOCK_STREAM, proto = 0;
3260 PyObject *res = NULL;
3261
3262#if defined(AF_UNIX)
3263 family = AF_UNIX;
3264#else
3265 family = AF_INET;
3266#endif
3267 if (!PyArg_ParseTuple(args, "|iii:socketpair",
3268 &family, &type, &proto))
3269 return NULL;
3270 /* Create a pair of socket fds */
3271 if (socketpair(family, type, proto, sv) < 0)
3272 return set_error();
Dave Cole331708b2004-08-09 04:51:41 +00003273 s0 = new_sockobject(sv[0], family, type, proto);
3274 if (s0 == NULL)
3275 goto finally;
3276 s1 = new_sockobject(sv[1], family, type, proto);
3277 if (s1 == NULL)
3278 goto finally;
3279 res = PyTuple_Pack(2, s0, s1);
3280
3281finally:
3282 if (res == NULL) {
3283 if (s0 == NULL)
3284 SOCKETCLOSE(sv[0]);
3285 if (s1 == NULL)
3286 SOCKETCLOSE(sv[1]);
3287 }
3288 Py_XDECREF(s0);
3289 Py_XDECREF(s1);
3290 return res;
3291}
3292
3293PyDoc_STRVAR(socketpair_doc,
3294"socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\
3295\n\
3296Create a pair of socket objects from the sockets returned by the platform\n\
3297socketpair() function.\n\
Dave Cole07fda7e2004-08-23 05:16:23 +00003298The arguments are the same as for socket() except the default family is\n\
Dave Colee8bbfe42004-08-26 00:51:16 +00003299AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
Dave Cole331708b2004-08-09 04:51:41 +00003300
3301#endif /* HAVE_SOCKETPAIR */
3302
3303
Guido van Rossum006bf911996-06-12 04:04:55 +00003304static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003305socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003306{
3307 int x1, x2;
3308
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003309 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00003310 return NULL;
3311 }
Guido van Rossum018919a2007-01-15 00:07:32 +00003312 if (x1 < 0) {
3313 PyErr_SetString(PyExc_OverflowError,
3314 "can't convert negative number to unsigned long");
3315 return NULL;
3316 }
3317 x2 = (unsigned int)ntohs((unsigned short)x1);
Guido van Rossum006bf911996-06-12 04:04:55 +00003318 return PyInt_FromLong(x2);
3319}
3320
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003321PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003322"ntohs(integer) -> integer\n\
3323\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003324Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003325
3326
Guido van Rossum006bf911996-06-12 04:04:55 +00003327static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003328socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003329{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003330 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003331
Guido van Rossumddefaf32007-01-14 03:31:43 +00003332 if (PyLong_Check(arg)) {
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003333 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003334 if (x == (unsigned long) -1 && PyErr_Occurred())
3335 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003336#if SIZEOF_LONG > 4
3337 {
3338 unsigned long y;
3339 /* only want the trailing 32 bits */
3340 y = x & 0xFFFFFFFFUL;
3341 if (y ^ x)
3342 return PyErr_Format(PyExc_OverflowError,
3343 "long int larger than 32 bits");
3344 x = y;
Tim Peters58141872002-08-06 22:25:02 +00003345 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003346#endif
3347 }
3348 else
Tim Peters58141872002-08-06 22:25:02 +00003349 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003350 "expected int/long, %s found",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003351 Py_Type(arg)->tp_name);
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003352 if (x == (unsigned long) -1 && PyErr_Occurred())
3353 return NULL;
Guido van Rossum018919a2007-01-15 00:07:32 +00003354 return PyLong_FromUnsignedLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003355}
3356
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003357PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003358"ntohl(integer) -> integer\n\
3359\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003360Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003361
3362
Guido van Rossum006bf911996-06-12 04:04:55 +00003363static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003364socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003365{
Hye-Shik Changa7b673f2003-12-17 09:40:23 +00003366 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00003367
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003368 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00003369 return NULL;
3370 }
Guido van Rossum018919a2007-01-15 00:07:32 +00003371 if (x1 < 0) {
3372 PyErr_SetString(PyExc_OverflowError,
3373 "can't convert negative number to unsigned long");
3374 return NULL;
3375 }
3376 x2 = (unsigned int)htons((unsigned short)x1);
Guido van Rossum006bf911996-06-12 04:04:55 +00003377 return PyInt_FromLong(x2);
3378}
3379
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003380PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003381"htons(integer) -> integer\n\
3382\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003383Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003384
3385
Guido van Rossum006bf911996-06-12 04:04:55 +00003386static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003387socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003388{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003389 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003390
Guido van Rossumddefaf32007-01-14 03:31:43 +00003391 if (PyLong_Check(arg)) {
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003392 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003393 if (x == (unsigned long) -1 && PyErr_Occurred())
3394 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003395#if SIZEOF_LONG > 4
3396 {
3397 unsigned long y;
3398 /* only want the trailing 32 bits */
3399 y = x & 0xFFFFFFFFUL;
3400 if (y ^ x)
3401 return PyErr_Format(PyExc_OverflowError,
3402 "long int larger than 32 bits");
3403 x = y;
Tim Peters58141872002-08-06 22:25:02 +00003404 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003405#endif
3406 }
3407 else
Tim Peters58141872002-08-06 22:25:02 +00003408 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003409 "expected int/long, %s found",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003410 Py_Type(arg)->tp_name);
Guido van Rossum018919a2007-01-15 00:07:32 +00003411 return PyLong_FromUnsignedLong(htonl((unsigned long)x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003412}
3413
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003414PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003415"htonl(integer) -> integer\n\
3416\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003417Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003418
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003419/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003420
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003421PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum7d0a8262007-05-21 23:13:11 +00003422"inet_aton(string) -> bytes giving packed 32-bit IP representation\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003423\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003424Convert 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 +00003425binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003426
3427static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003428socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003429{
Guido van Rossuma2e48551999-09-09 15:42:59 +00003430#ifndef INADDR_NONE
3431#define INADDR_NONE (-1)
3432#endif
Neal Norwitz88f115b2003-02-13 02:15:42 +00003433#ifdef HAVE_INET_ATON
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003434 struct in_addr buf;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003435#endif
3436
3437#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
Tim Peters1df9fdd2003-02-13 03:13:40 +00003438 /* Have to use inet_addr() instead */
3439 unsigned long packed_addr;
3440#endif
3441 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003442
Tim Peters1df9fdd2003-02-13 03:13:40 +00003443 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003444 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003445
Tim Peters1df9fdd2003-02-13 03:13:40 +00003446
3447#ifdef HAVE_INET_ATON
Thomas Wouters477c8d52006-05-27 19:21:47 +00003448
3449#ifdef USE_INET_ATON_WEAKLINK
3450 if (inet_aton != NULL) {
3451#endif
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003452 if (inet_aton(ip_addr, &buf))
Guido van Rossum7d0a8262007-05-21 23:13:11 +00003453 return PyBytes_FromStringAndSize((char *)(&buf),
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003454 sizeof(buf));
3455
3456 PyErr_SetString(socket_error,
3457 "illegal IP address string passed to inet_aton");
3458 return NULL;
3459
Thomas Wouters477c8d52006-05-27 19:21:47 +00003460#ifdef USE_INET_ATON_WEAKLINK
3461 } else {
3462#endif
3463
3464#endif
3465
3466#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
3467
Georg Brandld2e3ba72005-08-26 08:34:00 +00003468 /* special-case this address as inet_addr might return INADDR_NONE
3469 * for this */
3470 if (strcmp(ip_addr, "255.255.255.255") == 0) {
3471 packed_addr = 0xFFFFFFFF;
3472 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00003473
Georg Brandld2e3ba72005-08-26 08:34:00 +00003474 packed_addr = inet_addr(ip_addr);
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003475
Georg Brandld2e3ba72005-08-26 08:34:00 +00003476 if (packed_addr == INADDR_NONE) { /* invalid address */
3477 PyErr_SetString(socket_error,
3478 "illegal IP address string passed to inet_aton");
3479 return NULL;
3480 }
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003481 }
Guido van Rossum7d0a8262007-05-21 23:13:11 +00003482 return PyBytes_FromStringAndSize((char *) &packed_addr,
3483 sizeof(packed_addr));
Thomas Wouters477c8d52006-05-27 19:21:47 +00003484
3485#ifdef USE_INET_ATON_WEAKLINK
3486 }
3487#endif
3488
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003489#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003490}
3491
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003492PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00003493"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003494\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003495Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003496
3497static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003498socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003499{
3500 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003501 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003502 struct in_addr packed_addr;
3503
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003504 if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003505 return NULL;
3506 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00003507
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003508 if (addr_len != sizeof(packed_addr)) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003509 PyErr_SetString(socket_error,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003510 "packed IP wrong length for inet_ntoa");
3511 return NULL;
3512 }
3513
3514 memcpy(&packed_addr, packed_str, addr_len);
3515
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003516 return PyUnicode_FromString(inet_ntoa(packed_addr));
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003517}
Guido van Rossum82a5c661998-07-07 20:45:43 +00003518
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003519#ifdef HAVE_INET_PTON
3520
3521PyDoc_STRVAR(inet_pton_doc,
3522"inet_pton(af, ip) -> packed IP address string\n\
3523\n\
3524Convert an IP address from string format to a packed string suitable\n\
3525for use with low-level network functions.");
3526
3527static PyObject *
3528socket_inet_pton(PyObject *self, PyObject *args)
3529{
3530 int af;
3531 char* ip;
3532 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003533#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003534 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003535#else
3536 char packed[sizeof(struct in_addr)];
3537#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003538 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
3539 return NULL;
3540 }
3541
Martin v. Löwis04697e82004-06-02 12:35:29 +00003542#if !defined(ENABLE_IPV6) && defined(AF_INET6)
Martin v. Löwis10649092003-08-05 06:25:06 +00003543 if(af == AF_INET6) {
3544 PyErr_SetString(socket_error,
3545 "can't use AF_INET6, IPv6 is disabled");
3546 return NULL;
3547 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003548#endif
Martin v. Löwis10649092003-08-05 06:25:06 +00003549
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003550 retval = inet_pton(af, ip, packed);
3551 if (retval < 0) {
3552 PyErr_SetFromErrno(socket_error);
3553 return NULL;
3554 } else if (retval == 0) {
3555 PyErr_SetString(socket_error,
3556 "illegal IP address string passed to inet_pton");
3557 return NULL;
3558 } else if (af == AF_INET) {
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003559 return PyBytes_FromStringAndSize(packed,
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003560 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003561#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003562 } else if (af == AF_INET6) {
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003563 return PyBytes_FromStringAndSize(packed,
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003564 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003565#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003566 } else {
3567 PyErr_SetString(socket_error, "unknown address family");
3568 return NULL;
3569 }
3570}
Thomas Wouters477c8d52006-05-27 19:21:47 +00003571
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003572PyDoc_STRVAR(inet_ntop_doc,
3573"inet_ntop(af, packed_ip) -> string formatted IP address\n\
3574\n\
3575Convert a packed IP address of the given family to string format.");
3576
3577static PyObject *
3578socket_inet_ntop(PyObject *self, PyObject *args)
3579{
3580 int af;
3581 char* packed;
3582 int len;
3583 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003584#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003585 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003586#else
3587 char ip[INET_ADDRSTRLEN + 1];
3588#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00003589
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003590 /* Guarantee NUL-termination for PyUnicode_FromString() below */
Jeremy Hylton80961f32004-11-07 14:24:25 +00003591 memset((void *) &ip[0], '\0', sizeof(ip));
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003592
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003593 if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) {
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003594 return NULL;
3595 }
3596
3597 if (af == AF_INET) {
3598 if (len != sizeof(struct in_addr)) {
3599 PyErr_SetString(PyExc_ValueError,
3600 "invalid length of packed IP address string");
3601 return NULL;
3602 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003603#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003604 } else if (af == AF_INET6) {
3605 if (len != sizeof(struct in6_addr)) {
3606 PyErr_SetString(PyExc_ValueError,
3607 "invalid length of packed IP address string");
3608 return NULL;
3609 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003610#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003611 } else {
3612 PyErr_Format(PyExc_ValueError,
3613 "unknown address family %d", af);
3614 return NULL;
3615 }
3616
3617 retval = inet_ntop(af, packed, ip, sizeof(ip));
3618 if (!retval) {
3619 PyErr_SetFromErrno(socket_error);
3620 return NULL;
3621 } else {
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003622 return PyUnicode_FromString(retval);
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003623 }
3624
3625 /* NOTREACHED */
3626 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
3627 return NULL;
3628}
3629
3630#endif /* HAVE_INET_PTON */
3631
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003632/* Python interface to getaddrinfo(host, port). */
3633
3634/*ARGSUSED*/
3635static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003636socket_getaddrinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003637{
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003638 struct addrinfo hints, *res;
3639 struct addrinfo *res0 = NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003640 PyObject *hobj = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003641 PyObject *pobj = (PyObject *)NULL;
Martin v. Löwisf65b1a12001-08-12 09:28:40 +00003642 char pbuf[30];
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003643 char *hptr, *pptr;
3644 int family, socktype, protocol, flags;
3645 int error;
3646 PyObject *all = (PyObject *)NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003647 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003648
3649 family = socktype = protocol = flags = 0;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003650 family = AF_UNSPEC;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003651 if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
3652 &hobj, &pobj, &family, &socktype,
3653 &protocol, &flags)) {
3654 return NULL;
3655 }
3656 if (hobj == Py_None) {
3657 hptr = NULL;
3658 } else if (PyUnicode_Check(hobj)) {
3659 idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
3660 if (!idna)
3661 return NULL;
Guido van Rossumb5b22702007-05-18 18:55:53 +00003662 assert(PyBytes_Check(idna));
3663 hptr = PyBytes_AsString(idna);
Martin v. Löwis2548c732003-04-18 10:39:54 +00003664 } else if (PyString_Check(hobj)) {
3665 hptr = PyString_AsString(hobj);
3666 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00003667 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2548c732003-04-18 10:39:54 +00003668 "getaddrinfo() argument 1 must be string or None");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003669 return NULL;
3670 }
Guido van Rossumddefaf32007-01-14 03:31:43 +00003671 if (PyInt_CheckExact(pobj)) {
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003672 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003673 pptr = pbuf;
Guido van Rossum32c4ac02007-08-15 03:56:40 +00003674 } else if (PyUnicode_Check(pobj)) {
3675 pptr = PyUnicode_AsString(pobj);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003676 } else if (PyString_Check(pobj)) {
3677 pptr = PyString_AsString(pobj);
3678 } else if (pobj == Py_None) {
3679 pptr = (char *)NULL;
3680 } else {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003681 PyErr_SetString(socket_error, "Int or String expected");
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003682 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003683 }
3684 memset(&hints, 0, sizeof(hints));
3685 hints.ai_family = family;
3686 hints.ai_socktype = socktype;
3687 hints.ai_protocol = protocol;
3688 hints.ai_flags = flags;
Just van Rossum1040d2c2003-05-09 07:53:18 +00003689 Py_BEGIN_ALLOW_THREADS
3690 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003691 error = getaddrinfo(hptr, pptr, &hints, &res0);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003692 Py_END_ALLOW_THREADS
3693 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003694 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003695 set_gaierror(error);
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003696 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003697 }
3698
3699 if ((all = PyList_New(0)) == NULL)
3700 goto err;
3701 for (res = res0; res; res = res->ai_next) {
Guido van Rossum9ce8e382007-08-09 21:06:37 +00003702 PyObject *single;
Guido van Rossum716aac02001-10-12 18:59:27 +00003703 PyObject *addr =
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00003704 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
Guido van Rossum716aac02001-10-12 18:59:27 +00003705 if (addr == NULL)
3706 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003707 single = Py_BuildValue("iiisO", res->ai_family,
3708 res->ai_socktype, res->ai_protocol,
3709 res->ai_canonname ? res->ai_canonname : "",
Guido van Rossum716aac02001-10-12 18:59:27 +00003710 addr);
3711 Py_DECREF(addr);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003712 if (single == NULL)
3713 goto err;
3714
3715 if (PyList_Append(all, single))
3716 goto err;
3717 Py_XDECREF(single);
3718 }
Martin v. Löwis2548c732003-04-18 10:39:54 +00003719 Py_XDECREF(idna);
Neal Norwitz90128ba2002-08-09 03:37:42 +00003720 if (res0)
3721 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003722 return all;
3723 err:
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003724 Py_XDECREF(all);
Martin v. Löwis2548c732003-04-18 10:39:54 +00003725 Py_XDECREF(idna);
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003726 if (res0)
3727 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003728 return (PyObject *)NULL;
3729}
3730
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003731PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003732"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
3733 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003734\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003735Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003736
3737/* Python interface to getnameinfo(sa, flags). */
3738
3739/*ARGSUSED*/
3740static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003741socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003742{
3743 PyObject *sa = (PyObject *)NULL;
3744 int flags;
3745 char *hostp;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003746 int port, flowinfo, scope_id;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003747 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
3748 struct addrinfo hints, *res = NULL;
3749 int error;
3750 PyObject *ret = (PyObject *)NULL;
3751
3752 flags = flowinfo = scope_id = 0;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003753 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003754 return NULL;
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003755 if (!PyArg_ParseTuple(sa, "si|ii",
3756 &hostp, &port, &flowinfo, &scope_id))
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003757 return NULL;
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003758 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003759 memset(&hints, 0, sizeof(hints));
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003760 hints.ai_family = AF_UNSPEC;
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003761 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Just van Rossum1040d2c2003-05-09 07:53:18 +00003762 Py_BEGIN_ALLOW_THREADS
3763 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003764 error = getaddrinfo(hostp, pbuf, &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003765 Py_END_ALLOW_THREADS
3766 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003767 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003768 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003769 goto fail;
3770 }
3771 if (res->ai_next) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003772 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003773 "sockaddr resolved to multiple addresses");
3774 goto fail;
3775 }
3776 switch (res->ai_family) {
3777 case AF_INET:
3778 {
3779 char *t1;
3780 int t2;
3781 if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003782 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003783 "IPv4 sockaddr must be 2 tuple");
3784 goto fail;
3785 }
3786 break;
3787 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003788#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003789 case AF_INET6:
3790 {
3791 struct sockaddr_in6 *sin6;
3792 sin6 = (struct sockaddr_in6 *)res->ai_addr;
3793 sin6->sin6_flowinfo = flowinfo;
3794 sin6->sin6_scope_id = scope_id;
3795 break;
3796 }
3797#endif
3798 }
3799 error = getnameinfo(res->ai_addr, res->ai_addrlen,
3800 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
3801 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003802 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003803 goto fail;
3804 }
3805 ret = Py_BuildValue("ss", hbuf, pbuf);
3806
3807fail:
3808 if (res)
3809 freeaddrinfo(res);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003810 return ret;
3811}
3812
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003813PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003814"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003815\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003816Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003817
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003818
3819/* Python API to getting and setting the default timeout value. */
3820
3821static PyObject *
3822socket_getdefaulttimeout(PyObject *self)
3823{
3824 if (defaulttimeout < 0.0) {
3825 Py_INCREF(Py_None);
3826 return Py_None;
3827 }
3828 else
3829 return PyFloat_FromDouble(defaulttimeout);
3830}
3831
3832PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003833"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003834\n\
3835Returns the default timeout in floating seconds for new socket objects.\n\
3836A value of None indicates that new socket objects have no timeout.\n\
3837When the socket module is first imported, the default is None.");
3838
3839static PyObject *
3840socket_setdefaulttimeout(PyObject *self, PyObject *arg)
3841{
3842 double timeout;
3843
3844 if (arg == Py_None)
3845 timeout = -1.0;
3846 else {
3847 timeout = PyFloat_AsDouble(arg);
3848 if (timeout < 0.0) {
3849 if (!PyErr_Occurred())
3850 PyErr_SetString(PyExc_ValueError,
3851 "Timeout value out of range");
3852 return NULL;
3853 }
3854 }
3855
3856 defaulttimeout = timeout;
3857
3858 Py_INCREF(Py_None);
3859 return Py_None;
3860}
3861
3862PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003863"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003864\n\
3865Set the default timeout in floating seconds for new socket objects.\n\
3866A value of None indicates that new socket objects have no timeout.\n\
3867When the socket module is first imported, the default is None.");
3868
3869
Guido van Rossum30a685f1991-06-27 15:51:29 +00003870/* List of functions exported by this module. */
3871
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003872static PyMethodDef socket_methods[] = {
3873 {"gethostbyname", socket_gethostbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003874 METH_VARARGS, gethostbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003875 {"gethostbyname_ex", socket_gethostbyname_ex,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003876 METH_VARARGS, ghbn_ex_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003877 {"gethostbyaddr", socket_gethostbyaddr,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003878 METH_VARARGS, gethostbyaddr_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003879 {"gethostname", socket_gethostname,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003880 METH_NOARGS, gethostname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003881 {"getservbyname", socket_getservbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003882 METH_VARARGS, getservbyname_doc},
Barry Warsaw11b91a02004-06-28 00:50:43 +00003883 {"getservbyport", socket_getservbyport,
3884 METH_VARARGS, getservbyport_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003885 {"getprotobyname", socket_getprotobyname,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003886 METH_VARARGS, getprotobyname_doc},
Dave Cole331708b2004-08-09 04:51:41 +00003887#ifdef HAVE_SOCKETPAIR
3888 {"socketpair", socket_socketpair,
3889 METH_VARARGS, socketpair_doc},
3890#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003891 {"ntohs", socket_ntohs,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003892 METH_VARARGS, ntohs_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003893 {"ntohl", socket_ntohl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003894 METH_O, ntohl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003895 {"htons", socket_htons,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003896 METH_VARARGS, htons_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003897 {"htonl", socket_htonl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003898 METH_O, htonl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003899 {"inet_aton", socket_inet_aton,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003900 METH_VARARGS, inet_aton_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003901 {"inet_ntoa", socket_inet_ntoa,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003902 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003903#ifdef HAVE_INET_PTON
3904 {"inet_pton", socket_inet_pton,
3905 METH_VARARGS, inet_pton_doc},
3906 {"inet_ntop", socket_inet_ntop,
3907 METH_VARARGS, inet_ntop_doc},
3908#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003909 {"getaddrinfo", socket_getaddrinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003910 METH_VARARGS, getaddrinfo_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003911 {"getnameinfo", socket_getnameinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003912 METH_VARARGS, getnameinfo_doc},
Guido van Rossum1693ba82002-07-18 21:11:26 +00003913 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003914 METH_NOARGS, getdefaulttimeout_doc},
3915 {"setdefaulttimeout", socket_setdefaulttimeout,
3916 METH_O, setdefaulttimeout_doc},
Guido van Rossum30a685f1991-06-27 15:51:29 +00003917 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003918};
3919
Guido van Rossum30a685f1991-06-27 15:51:29 +00003920
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003921#ifdef MS_WINDOWS
3922#define OS_INIT_DEFINED
3923
3924/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003925
3926static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003927os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00003928{
3929 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00003930}
3931
3932static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003933os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00003934{
3935 WSADATA WSAData;
3936 int ret;
Guido van Rossumbe32c891996-06-20 16:25:29 +00003937 ret = WSAStartup(0x0101, &WSAData);
3938 switch (ret) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003939 case 0: /* No error */
Mark Hammond14350ab2003-07-09 04:57:46 +00003940 Py_AtExit(os_cleanup);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003941 return 1; /* Success */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003942 case WSASYSNOTREADY:
3943 PyErr_SetString(PyExc_ImportError,
3944 "WSAStartup failed: network not ready");
3945 break;
3946 case WSAVERNOTSUPPORTED:
3947 case WSAEINVAL:
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003948 PyErr_SetString(
3949 PyExc_ImportError,
3950 "WSAStartup failed: requested version not supported");
Guido van Rossumbe32c891996-06-20 16:25:29 +00003951 break;
3952 default:
Walter Dörwald0bbd8ab2007-06-05 13:49:43 +00003953 PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret);
Guido van Rossumbe32c891996-06-20 16:25:29 +00003954 break;
3955 }
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003956 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003957}
3958
Guido van Rossum8d665e61996-06-26 18:22:49 +00003959#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003960
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003961
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003962#ifdef PYOS_OS2
3963#define OS_INIT_DEFINED
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003964
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003965/* Additional initialization for OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003966
3967static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003968os_init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003969{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003970#ifndef PYCC_GCC
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003971 int rc = sock_init();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003972
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003973 if (rc == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003974 return 1; /* Success */
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003975 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003976
Walter Dörwald0bbd8ab2007-06-05 13:49:43 +00003977 PyErr_Format(PyExc_ImportError, "OS/2 TCP/IP Error# %d", sock_errno());
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003978
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003979 return 0; /* Failure */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00003980#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003981 /* No need to initialise sockets with GCC/EMX */
3982 return 1; /* Success */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00003983#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003984}
3985
3986#endif /* PYOS_OS2 */
3987
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003988
3989#ifndef OS_INIT_DEFINED
3990static int
3991os_init(void)
3992{
3993 return 1; /* Success */
3994}
3995#endif
3996
3997
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003998/* C API table - always add new things to the end for binary
3999 compatibility. */
4000static
4001PySocketModule_APIObject PySocketModuleAPI =
4002{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004003 &sock_type,
Brett Cannon06c34792004-03-23 23:16:54 +00004004 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004005};
4006
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004007
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004008/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004009
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004010 This module is actually called "_socket", and there's a wrapper
Guido van Rossum7d0a8262007-05-21 23:13:11 +00004011 "socket.py" which implements some additional functionality.
4012 The import of "_socket" may fail with an ImportError exception if
4013 os-specific initialization fails. On Windows, this does WINSOCK
4014 initialization. When WINSOCK is initialized succesfully, a call to
4015 WSACleanup() is scheduled to be made at exit time.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004016*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004017
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004018PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00004019"Implementation module for socket operations.\n\
4020\n\
4021See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00004022
Mark Hammondfe51c6d2002-08-02 02:27:13 +00004023PyMODINIT_FUNC
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004024init_socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004025{
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004026 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00004027
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004028 if (!os_init())
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004029 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004030
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00004031 Py_Type(&sock_type) = &PyType_Type;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004032 m = Py_InitModule3(PySocket_MODULE_NAME,
4033 socket_methods,
4034 socket_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00004035 if (m == NULL)
4036 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004037
Gregory P. Smithc111d9f2007-09-09 23:55:55 +00004038 socket_error = PyErr_NewException("socket.error",
4039 PyExc_IOError, NULL);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004040 if (socket_error == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004041 return;
Brett Cannon06c34792004-03-23 23:16:54 +00004042 PySocketModuleAPI.error = socket_error;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004043 Py_INCREF(socket_error);
4044 PyModule_AddObject(m, "error", socket_error);
4045 socket_herror = PyErr_NewException("socket.herror",
4046 socket_error, NULL);
4047 if (socket_herror == NULL)
4048 return;
4049 Py_INCREF(socket_herror);
4050 PyModule_AddObject(m, "herror", socket_herror);
4051 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004052 NULL);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004053 if (socket_gaierror == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004054 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004055 Py_INCREF(socket_gaierror);
4056 PyModule_AddObject(m, "gaierror", socket_gaierror);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00004057 socket_timeout = PyErr_NewException("socket.timeout",
4058 socket_error, NULL);
4059 if (socket_timeout == NULL)
4060 return;
4061 Py_INCREF(socket_timeout);
4062 PyModule_AddObject(m, "timeout", socket_timeout);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004063 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00004064 if (PyModule_AddObject(m, "SocketType",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004065 (PyObject *)&sock_type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00004066 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004067 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00004068 if (PyModule_AddObject(m, "socket",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004069 (PyObject *)&sock_type) != 0)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00004070 return;
Guido van Rossum09be4091999-08-09 14:40:40 +00004071
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004072#ifdef ENABLE_IPV6
4073 has_ipv6 = Py_True;
4074#else
4075 has_ipv6 = Py_False;
4076#endif
4077 Py_INCREF(has_ipv6);
4078 PyModule_AddObject(m, "has_ipv6", has_ipv6);
4079
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004080 /* Export C API */
Fred Drake4baedc12002-04-01 14:53:37 +00004081 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00004082 PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL)
4083 ) != 0)
4084 return;
4085
Guido van Rossum09be4091999-08-09 14:40:40 +00004086 /* Address families (we only support AF_INET and AF_UNIX) */
4087#ifdef AF_UNSPEC
Fred Drake4baedc12002-04-01 14:53:37 +00004088 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00004089#endif
Fred Drake4baedc12002-04-01 14:53:37 +00004090 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004091#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00004092 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004093#endif /* AF_INET6 */
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00004094#if defined(AF_UNIX)
Fred Drake4baedc12002-04-01 14:53:37 +00004095 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004096#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00004097#ifdef AF_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00004098 /* Amateur Radio AX.25 */
4099 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004100#endif
4101#ifdef AF_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00004102 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00004103#endif
4104#ifdef AF_APPLETALK
Fred Drake4baedc12002-04-01 14:53:37 +00004105 /* Appletalk DDP */
4106 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004107#endif
4108#ifdef AF_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00004109 /* Amateur radio NetROM */
4110 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004111#endif
4112#ifdef AF_BRIDGE
Fred Drake4baedc12002-04-01 14:53:37 +00004113 /* Multiprotocol bridge */
4114 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004115#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004116#ifdef AF_ATMPVC
4117 /* ATM PVCs */
4118 PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC);
4119#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004120#ifdef AF_AAL5
Fred Drake4baedc12002-04-01 14:53:37 +00004121 /* Reserved for Werner's ATM */
4122 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00004123#endif
4124#ifdef AF_X25
Fred Drake4baedc12002-04-01 14:53:37 +00004125 /* Reserved for X.25 project */
4126 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004127#endif
4128#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00004129 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00004130#endif
4131#ifdef AF_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00004132 /* Amateur Radio X.25 PLP */
4133 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004134#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004135#ifdef AF_DECnet
4136 /* Reserved for DECnet project */
4137 PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet);
4138#endif
4139#ifdef AF_NETBEUI
4140 /* Reserved for 802.2LLC project */
4141 PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI);
4142#endif
4143#ifdef AF_SECURITY
4144 /* Security callback pseudo AF */
4145 PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY);
4146#endif
4147#ifdef AF_KEY
4148 /* PF_KEY key management API */
4149 PyModule_AddIntConstant(m, "AF_KEY", AF_KEY);
4150#endif
4151#ifdef AF_NETLINK
4152 /* */
4153 PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);
Martin v. Löwis11017b12006-01-14 18:12:57 +00004154 PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004155#ifdef NETLINK_SKIP
Martin v. Löwis11017b12006-01-14 18:12:57 +00004156 PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004157#endif
4158#ifdef NETLINK_W1
4159 PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1);
4160#endif
Martin v. Löwis11017b12006-01-14 18:12:57 +00004161 PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK);
4162 PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004163#ifdef NETLINK_TCPDIAG
Martin v. Löwis11017b12006-01-14 18:12:57 +00004164 PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004165#endif
4166#ifdef NETLINK_NFLOG
Martin v. Löwis11017b12006-01-14 18:12:57 +00004167 PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG);
Guido van Rossum668a94a2006-02-21 01:07:27 +00004168#endif
Neal Norwitz65851662006-01-16 04:31:40 +00004169#ifdef NETLINK_XFRM
Martin v. Löwis11017b12006-01-14 18:12:57 +00004170 PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM);
Neal Norwitz65851662006-01-16 04:31:40 +00004171#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004172#ifdef NETLINK_ARPD
Martin v. Löwis11017b12006-01-14 18:12:57 +00004173 PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004174#endif
4175#ifdef NETLINK_ROUTE6
Martin v. Löwis11017b12006-01-14 18:12:57 +00004176 PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004177#endif
Martin v. Löwis11017b12006-01-14 18:12:57 +00004178 PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW);
Thomas Wouterscf297e42007-02-23 15:07:44 +00004179#ifdef NETLINK_DNRTMSG
Martin v. Löwis11017b12006-01-14 18:12:57 +00004180 PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG);
Thomas Wouterscf297e42007-02-23 15:07:44 +00004181#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004182#ifdef NETLINK_TAPBASE
Martin v. Löwis11017b12006-01-14 18:12:57 +00004183 PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE);
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004184#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004185#endif /* AF_NETLINK */
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00004186#ifdef AF_ROUTE
4187 /* Alias to emulate 4.4BSD */
4188 PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE);
4189#endif
4190#ifdef AF_ASH
4191 /* Ash */
4192 PyModule_AddIntConstant(m, "AF_ASH", AF_ASH);
4193#endif
4194#ifdef AF_ECONET
4195 /* Acorn Econet */
4196 PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET);
4197#endif
4198#ifdef AF_ATMSVC
4199 /* ATM SVCs */
4200 PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC);
4201#endif
4202#ifdef AF_SNA
4203 /* Linux SNA Project (nutters!) */
4204 PyModule_AddIntConstant(m, "AF_SNA", AF_SNA);
4205#endif
4206#ifdef AF_IRDA
4207 /* IRDA sockets */
4208 PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA);
4209#endif
4210#ifdef AF_PPPOX
4211 /* PPPoX sockets */
4212 PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX);
4213#endif
4214#ifdef AF_WANPIPE
4215 /* Wanpipe API Sockets */
4216 PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE);
4217#endif
4218#ifdef AF_LLC
4219 /* Linux LLC */
4220 PyModule_AddIntConstant(m, "AF_LLC", AF_LLC);
4221#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00004222
Hye-Shik Chang81268602004-02-02 06:05:24 +00004223#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00004224 PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);
4225 PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);
Thomas Wouterscf297e42007-02-23 15:07:44 +00004226 PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI);
4227 PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI);
Thomas Wouterscf297e42007-02-23 15:07:44 +00004228 PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER);
Hye-Shik Chang81268602004-02-02 06:05:24 +00004229#if !defined(__FreeBSD__)
Guido van Rossumcd16bf62007-06-13 18:07:49 +00004230 PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP);
4231 PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR);
Martin v. Löwis12af0482004-01-31 12:34:17 +00004232 PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00004233#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00004234 PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004235 PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
4236 PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
Martin v. Löwis12af0482004-01-31 12:34:17 +00004237#endif
4238
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00004239#ifdef HAVE_NETPACKET_PACKET_H
Fred Drake4baedc12002-04-01 14:53:37 +00004240 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
4241 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
4242 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
4243 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
4244 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
4245 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
4246 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
4247 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
4248 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00004249#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004250
4251 /* Socket types */
Fred Drake4baedc12002-04-01 14:53:37 +00004252 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
4253 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004254/* We have incomplete socket support. */
Fred Drake4baedc12002-04-01 14:53:37 +00004255 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
4256 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00004257#if defined(SOCK_RDM)
Fred Drake4baedc12002-04-01 14:53:37 +00004258 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004259#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004260
4261#ifdef SO_DEBUG
Fred Drake4baedc12002-04-01 14:53:37 +00004262 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004263#endif
4264#ifdef SO_ACCEPTCONN
Fred Drake4baedc12002-04-01 14:53:37 +00004265 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004266#endif
4267#ifdef SO_REUSEADDR
Fred Drake4baedc12002-04-01 14:53:37 +00004268 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004269#endif
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00004270#ifdef SO_EXCLUSIVEADDRUSE
4271 PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE);
4272#endif
4273
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004274#ifdef SO_KEEPALIVE
Fred Drake4baedc12002-04-01 14:53:37 +00004275 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004276#endif
4277#ifdef SO_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00004278 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004279#endif
4280#ifdef SO_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00004281 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004282#endif
4283#ifdef SO_USELOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00004284 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004285#endif
4286#ifdef SO_LINGER
Fred Drake4baedc12002-04-01 14:53:37 +00004287 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004288#endif
4289#ifdef SO_OOBINLINE
Fred Drake4baedc12002-04-01 14:53:37 +00004290 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004291#endif
4292#ifdef SO_REUSEPORT
Fred Drake4baedc12002-04-01 14:53:37 +00004293 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004294#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004295#ifdef SO_SNDBUF
Fred Drake4baedc12002-04-01 14:53:37 +00004296 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004297#endif
4298#ifdef SO_RCVBUF
Fred Drake4baedc12002-04-01 14:53:37 +00004299 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004300#endif
4301#ifdef SO_SNDLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00004302 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004303#endif
4304#ifdef SO_RCVLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00004305 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004306#endif
4307#ifdef SO_SNDTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00004308 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004309#endif
4310#ifdef SO_RCVTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00004311 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004312#endif
4313#ifdef SO_ERROR
Fred Drake4baedc12002-04-01 14:53:37 +00004314 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004315#endif
4316#ifdef SO_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004317 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004318#endif
4319
4320 /* Maximum number of connections for "listen" */
4321#ifdef SOMAXCONN
Fred Drake4baedc12002-04-01 14:53:37 +00004322 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004323#else
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004324 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004325#endif
4326
4327 /* Flags for send, recv */
4328#ifdef MSG_OOB
Fred Drake4baedc12002-04-01 14:53:37 +00004329 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004330#endif
4331#ifdef MSG_PEEK
Fred Drake4baedc12002-04-01 14:53:37 +00004332 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004333#endif
4334#ifdef MSG_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00004335 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004336#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00004337#ifdef MSG_DONTWAIT
Fred Drake4baedc12002-04-01 14:53:37 +00004338 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00004339#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004340#ifdef MSG_EOR
Fred Drake4baedc12002-04-01 14:53:37 +00004341 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004342#endif
4343#ifdef MSG_TRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00004344 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004345#endif
4346#ifdef MSG_CTRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00004347 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004348#endif
4349#ifdef MSG_WAITALL
Fred Drake4baedc12002-04-01 14:53:37 +00004350 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004351#endif
4352#ifdef MSG_BTAG
Fred Drake4baedc12002-04-01 14:53:37 +00004353 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004354#endif
4355#ifdef MSG_ETAG
Fred Drake4baedc12002-04-01 14:53:37 +00004356 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004357#endif
4358
4359 /* Protocol level and numbers, usable for [gs]etsockopt */
4360#ifdef SOL_SOCKET
Fred Drake4baedc12002-04-01 14:53:37 +00004361 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004362#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004363#ifdef SOL_IP
Fred Drake4baedc12002-04-01 14:53:37 +00004364 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004365#else
Fred Drake4baedc12002-04-01 14:53:37 +00004366 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00004367#endif
4368#ifdef SOL_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00004369 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00004370#endif
4371#ifdef SOL_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00004372 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004373#endif
4374#ifdef SOL_ATALK
Fred Drake4baedc12002-04-01 14:53:37 +00004375 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004376#endif
4377#ifdef SOL_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00004378 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004379#endif
4380#ifdef SOL_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00004381 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004382#endif
4383#ifdef SOL_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00004384 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004385#else
Fred Drake4baedc12002-04-01 14:53:37 +00004386 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00004387#endif
4388#ifdef SOL_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00004389 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004390#else
Fred Drake4baedc12002-04-01 14:53:37 +00004391 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00004392#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004393#ifdef IPPROTO_IP
Fred Drake4baedc12002-04-01 14:53:37 +00004394 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00004395#else
Fred Drake4baedc12002-04-01 14:53:37 +00004396 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004397#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004398#ifdef IPPROTO_HOPOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004399 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004400#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004401#ifdef IPPROTO_ICMP
Fred Drake4baedc12002-04-01 14:53:37 +00004402 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00004403#else
Fred Drake4baedc12002-04-01 14:53:37 +00004404 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004405#endif
4406#ifdef IPPROTO_IGMP
Fred Drake4baedc12002-04-01 14:53:37 +00004407 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004408#endif
4409#ifdef IPPROTO_GGP
Fred Drake4baedc12002-04-01 14:53:37 +00004410 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004411#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004412#ifdef IPPROTO_IPV4
Fred Drake4baedc12002-04-01 14:53:37 +00004413 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004414#endif
Martin v. Löwisa0f17342003-10-03 13:56:20 +00004415#ifdef IPPROTO_IPV6
4416 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
4417#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004418#ifdef IPPROTO_IPIP
Fred Drake4baedc12002-04-01 14:53:37 +00004419 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004420#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004421#ifdef IPPROTO_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00004422 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00004423#else
Fred Drake4baedc12002-04-01 14:53:37 +00004424 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004425#endif
4426#ifdef IPPROTO_EGP
Fred Drake4baedc12002-04-01 14:53:37 +00004427 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004428#endif
4429#ifdef IPPROTO_PUP
Fred Drake4baedc12002-04-01 14:53:37 +00004430 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004431#endif
4432#ifdef IPPROTO_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00004433 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00004434#else
Fred Drake4baedc12002-04-01 14:53:37 +00004435 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004436#endif
4437#ifdef IPPROTO_IDP
Fred Drake4baedc12002-04-01 14:53:37 +00004438 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004439#endif
4440#ifdef IPPROTO_HELLO
Fred Drake4baedc12002-04-01 14:53:37 +00004441 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004442#endif
4443#ifdef IPPROTO_ND
Fred Drake4baedc12002-04-01 14:53:37 +00004444 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004445#endif
4446#ifdef IPPROTO_TP
Fred Drake4baedc12002-04-01 14:53:37 +00004447 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004448#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004449#ifdef IPPROTO_IPV6
Fred Drake4baedc12002-04-01 14:53:37 +00004450 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004451#endif
4452#ifdef IPPROTO_ROUTING
Fred Drake4baedc12002-04-01 14:53:37 +00004453 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004454#endif
4455#ifdef IPPROTO_FRAGMENT
Fred Drake4baedc12002-04-01 14:53:37 +00004456 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004457#endif
4458#ifdef IPPROTO_RSVP
Fred Drake4baedc12002-04-01 14:53:37 +00004459 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004460#endif
4461#ifdef IPPROTO_GRE
Fred Drake4baedc12002-04-01 14:53:37 +00004462 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004463#endif
4464#ifdef IPPROTO_ESP
Fred Drake4baedc12002-04-01 14:53:37 +00004465 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004466#endif
4467#ifdef IPPROTO_AH
Fred Drake4baedc12002-04-01 14:53:37 +00004468 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004469#endif
4470#ifdef IPPROTO_MOBILE
Fred Drake4baedc12002-04-01 14:53:37 +00004471 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004472#endif
4473#ifdef IPPROTO_ICMPV6
Fred Drake4baedc12002-04-01 14:53:37 +00004474 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004475#endif
4476#ifdef IPPROTO_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00004477 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004478#endif
4479#ifdef IPPROTO_DSTOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004480 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004481#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004482#ifdef IPPROTO_XTP
Fred Drake4baedc12002-04-01 14:53:37 +00004483 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004484#endif
4485#ifdef IPPROTO_EON
Fred Drake4baedc12002-04-01 14:53:37 +00004486 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004487#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004488#ifdef IPPROTO_PIM
Fred Drake4baedc12002-04-01 14:53:37 +00004489 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004490#endif
4491#ifdef IPPROTO_IPCOMP
Fred Drake4baedc12002-04-01 14:53:37 +00004492 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004493#endif
4494#ifdef IPPROTO_VRRP
Fred Drake4baedc12002-04-01 14:53:37 +00004495 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004496#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004497#ifdef IPPROTO_BIP
Fred Drake4baedc12002-04-01 14:53:37 +00004498 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004499#endif
4500/**/
4501#ifdef IPPROTO_RAW
Fred Drake4baedc12002-04-01 14:53:37 +00004502 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00004503#else
Fred Drake4baedc12002-04-01 14:53:37 +00004504 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004505#endif
4506#ifdef IPPROTO_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00004507 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004508#endif
4509
4510 /* Some port configuration */
4511#ifdef IPPORT_RESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00004512 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004513#else
Fred Drake4baedc12002-04-01 14:53:37 +00004514 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004515#endif
4516#ifdef IPPORT_USERRESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00004517 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004518#else
Fred Drake4baedc12002-04-01 14:53:37 +00004519 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004520#endif
4521
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004522 /* Some reserved IP v.4 addresses */
4523#ifdef INADDR_ANY
Fred Drake4baedc12002-04-01 14:53:37 +00004524 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004525#else
Fred Drake4baedc12002-04-01 14:53:37 +00004526 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004527#endif
4528#ifdef INADDR_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00004529 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004530#else
Fred Drake4baedc12002-04-01 14:53:37 +00004531 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004532#endif
4533#ifdef INADDR_LOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00004534 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004535#else
Fred Drake4baedc12002-04-01 14:53:37 +00004536 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004537#endif
4538#ifdef INADDR_UNSPEC_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004539 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004540#else
Fred Drake4baedc12002-04-01 14:53:37 +00004541 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004542#endif
4543#ifdef INADDR_ALLHOSTS_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004544 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
4545 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004546#else
Fred Drake4baedc12002-04-01 14:53:37 +00004547 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004548#endif
4549#ifdef INADDR_MAX_LOCAL_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004550 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
4551 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004552#else
Fred Drake4baedc12002-04-01 14:53:37 +00004553 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004554#endif
4555#ifdef INADDR_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00004556 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004557#else
Fred Drake4baedc12002-04-01 14:53:37 +00004558 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004559#endif
4560
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004561 /* IPv4 [gs]etsockopt options */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004562#ifdef IP_OPTIONS
Fred Drake4baedc12002-04-01 14:53:37 +00004563 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004564#endif
4565#ifdef IP_HDRINCL
Fred Drake4baedc12002-04-01 14:53:37 +00004566 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004567#endif
4568#ifdef IP_TOS
Fred Drake4baedc12002-04-01 14:53:37 +00004569 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004570#endif
4571#ifdef IP_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004572 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004573#endif
4574#ifdef IP_RECVOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004575 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004576#endif
4577#ifdef IP_RECVRETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004578 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004579#endif
4580#ifdef IP_RECVDSTADDR
Fred Drake4baedc12002-04-01 14:53:37 +00004581 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004582#endif
4583#ifdef IP_RETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004584 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004585#endif
4586#ifdef IP_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00004587 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004588#endif
4589#ifdef IP_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004590 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004591#endif
4592#ifdef IP_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004593 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004594#endif
4595#ifdef IP_ADD_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00004596 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004597#endif
4598#ifdef IP_DROP_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00004599 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004600#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004601#ifdef IP_DEFAULT_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004602 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
4603 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00004604#endif
4605#ifdef IP_DEFAULT_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004606 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
4607 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004608#endif
4609#ifdef IP_MAX_MEMBERSHIPS
Fred Drake4baedc12002-04-01 14:53:37 +00004610 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00004611#endif
4612
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004613 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
4614#ifdef IPV6_JOIN_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004615 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004616#endif
4617#ifdef IPV6_LEAVE_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004618 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004619#endif
4620#ifdef IPV6_MULTICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00004621 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004622#endif
4623#ifdef IPV6_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00004624 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004625#endif
4626#ifdef IPV6_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004627 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004628#endif
4629#ifdef IPV6_UNICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00004630 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004631#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004632 /* Additional IPV6 socket options, defined in RFC 3493 */
4633#ifdef IPV6_V6ONLY
4634 PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);
4635#endif
4636 /* Advanced IPV6 socket options, from RFC 3542 */
4637#ifdef IPV6_CHECKSUM
4638 PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);
4639#endif
4640#ifdef IPV6_DONTFRAG
4641 PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);
4642#endif
4643#ifdef IPV6_DSTOPTS
4644 PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);
4645#endif
4646#ifdef IPV6_HOPLIMIT
4647 PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);
4648#endif
4649#ifdef IPV6_HOPOPTS
4650 PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);
4651#endif
4652#ifdef IPV6_NEXTHOP
4653 PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);
4654#endif
4655#ifdef IPV6_PATHMTU
4656 PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);
4657#endif
4658#ifdef IPV6_PKTINFO
4659 PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);
4660#endif
4661#ifdef IPV6_RECVDSTOPTS
4662 PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);
4663#endif
4664#ifdef IPV6_RECVHOPLIMIT
4665 PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);
4666#endif
4667#ifdef IPV6_RECVHOPOPTS
4668 PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);
4669#endif
4670#ifdef IPV6_RECVPKTINFO
4671 PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);
4672#endif
4673#ifdef IPV6_RECVRTHDR
4674 PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);
4675#endif
4676#ifdef IPV6_RECVTCLASS
4677 PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);
4678#endif
4679#ifdef IPV6_RTHDR
4680 PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);
4681#endif
4682#ifdef IPV6_RTHDRDSTOPTS
4683 PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);
4684#endif
4685#ifdef IPV6_RTHDR_TYPE_0
4686 PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);
4687#endif
4688#ifdef IPV6_RECVPATHMTU
4689 PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);
4690#endif
4691#ifdef IPV6_TCLASS
4692 PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);
4693#endif
4694#ifdef IPV6_USE_MIN_MTU
4695 PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);
4696#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004697
Guido van Rossum09be4091999-08-09 14:40:40 +00004698 /* TCP options */
4699#ifdef TCP_NODELAY
Fred Drake4baedc12002-04-01 14:53:37 +00004700 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00004701#endif
4702#ifdef TCP_MAXSEG
Fred Drake4baedc12002-04-01 14:53:37 +00004703 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00004704#endif
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004705#ifdef TCP_CORK
Fred Drake4baedc12002-04-01 14:53:37 +00004706 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004707#endif
4708#ifdef TCP_KEEPIDLE
Fred Drake4baedc12002-04-01 14:53:37 +00004709 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004710#endif
4711#ifdef TCP_KEEPINTVL
Fred Drake4baedc12002-04-01 14:53:37 +00004712 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004713#endif
4714#ifdef TCP_KEEPCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004715 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004716#endif
4717#ifdef TCP_SYNCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004718 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004719#endif
4720#ifdef TCP_LINGER2
Fred Drake4baedc12002-04-01 14:53:37 +00004721 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004722#endif
4723#ifdef TCP_DEFER_ACCEPT
Fred Drake4baedc12002-04-01 14:53:37 +00004724 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004725#endif
4726#ifdef TCP_WINDOW_CLAMP
Fred Drake4baedc12002-04-01 14:53:37 +00004727 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004728#endif
4729#ifdef TCP_INFO
Fred Drake4baedc12002-04-01 14:53:37 +00004730 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004731#endif
4732#ifdef TCP_QUICKACK
Fred Drake4baedc12002-04-01 14:53:37 +00004733 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004734#endif
4735
Guido van Rossum09be4091999-08-09 14:40:40 +00004736
4737 /* IPX options */
4738#ifdef IPX_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004739 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004740#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004741
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004742 /* get{addr,name}info parameters */
4743#ifdef EAI_ADDRFAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004744 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004745#endif
4746#ifdef EAI_AGAIN
Fred Drake4baedc12002-04-01 14:53:37 +00004747 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004748#endif
4749#ifdef EAI_BADFLAGS
Fred Drake4baedc12002-04-01 14:53:37 +00004750 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004751#endif
4752#ifdef EAI_FAIL
Fred Drake4baedc12002-04-01 14:53:37 +00004753 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004754#endif
4755#ifdef EAI_FAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004756 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004757#endif
4758#ifdef EAI_MEMORY
Fred Drake4baedc12002-04-01 14:53:37 +00004759 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004760#endif
4761#ifdef EAI_NODATA
Fred Drake4baedc12002-04-01 14:53:37 +00004762 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004763#endif
4764#ifdef EAI_NONAME
Fred Drake4baedc12002-04-01 14:53:37 +00004765 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004766#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004767#ifdef EAI_OVERFLOW
4768 PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);
4769#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004770#ifdef EAI_SERVICE
Fred Drake4baedc12002-04-01 14:53:37 +00004771 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004772#endif
4773#ifdef EAI_SOCKTYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004774 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004775#endif
4776#ifdef EAI_SYSTEM
Fred Drake4baedc12002-04-01 14:53:37 +00004777 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004778#endif
4779#ifdef EAI_BADHINTS
Fred Drake4baedc12002-04-01 14:53:37 +00004780 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004781#endif
4782#ifdef EAI_PROTOCOL
Fred Drake4baedc12002-04-01 14:53:37 +00004783 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004784#endif
4785#ifdef EAI_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00004786 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004787#endif
4788#ifdef AI_PASSIVE
Fred Drake4baedc12002-04-01 14:53:37 +00004789 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004790#endif
4791#ifdef AI_CANONNAME
Fred Drake4baedc12002-04-01 14:53:37 +00004792 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004793#endif
4794#ifdef AI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004795 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004796#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004797#ifdef AI_NUMERICSERV
4798 PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);
4799#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004800#ifdef AI_MASK
Fred Drake4baedc12002-04-01 14:53:37 +00004801 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004802#endif
4803#ifdef AI_ALL
Fred Drake4baedc12002-04-01 14:53:37 +00004804 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004805#endif
4806#ifdef AI_V4MAPPED_CFG
Fred Drake4baedc12002-04-01 14:53:37 +00004807 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004808#endif
4809#ifdef AI_ADDRCONFIG
Fred Drake4baedc12002-04-01 14:53:37 +00004810 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004811#endif
4812#ifdef AI_V4MAPPED
Fred Drake4baedc12002-04-01 14:53:37 +00004813 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004814#endif
4815#ifdef AI_DEFAULT
Fred Drake4baedc12002-04-01 14:53:37 +00004816 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004817#endif
4818#ifdef NI_MAXHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004819 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004820#endif
4821#ifdef NI_MAXSERV
Fred Drake4baedc12002-04-01 14:53:37 +00004822 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004823#endif
4824#ifdef NI_NOFQDN
Fred Drake4baedc12002-04-01 14:53:37 +00004825 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004826#endif
4827#ifdef NI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004828 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004829#endif
4830#ifdef NI_NAMEREQD
Fred Drake4baedc12002-04-01 14:53:37 +00004831 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004832#endif
4833#ifdef NI_NUMERICSERV
Fred Drake4baedc12002-04-01 14:53:37 +00004834 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004835#endif
4836#ifdef NI_DGRAM
Fred Drake4baedc12002-04-01 14:53:37 +00004837 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004838#endif
4839
Martin v. Löwis94681fc2003-11-27 19:40:22 +00004840 /* shutdown() parameters */
4841#ifdef SHUT_RD
4842 PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);
4843#elif defined(SD_RECEIVE)
4844 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
4845#else
4846 PyModule_AddIntConstant(m, "SHUT_RD", 0);
4847#endif
4848#ifdef SHUT_WR
4849 PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);
4850#elif defined(SD_SEND)
4851 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
4852#else
4853 PyModule_AddIntConstant(m, "SHUT_WR", 1);
4854#endif
4855#ifdef SHUT_RDWR
4856 PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);
4857#elif defined(SD_BOTH)
4858 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
4859#else
4860 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
4861#endif
4862
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004863 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00004864#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
4865 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004866#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004867}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004868
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004869
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004870#ifndef HAVE_INET_PTON
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004871
4872/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004873/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004874
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004875int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004876inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004877{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004878 if (af == AF_INET) {
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004879 long packed_addr;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004880 packed_addr = inet_addr(src);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004881 if (packed_addr == INADDR_NONE)
4882 return 0;
4883 memcpy(dst, &packed_addr, 4);
4884 return 1;
4885 }
4886 /* Should set errno to EAFNOSUPPORT */
4887 return -1;
4888}
4889
Martin v. Löwisc925b1532001-07-21 09:42:15 +00004890const char *
4891inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004892{
4893 if (af == AF_INET) {
4894 struct in_addr packed_addr;
4895 if (size < 16)
4896 /* Should set errno to ENOSPC. */
4897 return NULL;
4898 memcpy(&packed_addr, src, sizeof(packed_addr));
4899 return strncpy(dst, inet_ntoa(packed_addr), size);
4900 }
4901 /* Should set errno to EAFNOSUPPORT */
4902 return NULL;
4903}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004904
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004905#endif