blob: 4249045683704120d5f2f6cb53ed6a2bc5a9e1b4 [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
Jeremy Hylton22308652001-02-02 03:23:09 +000010 portable manner, though AF_PACKET is 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 Rossum83a072d2002-09-03 19:10:18 +000022- socket.fromfd(fd, family, type[, proto]) --> new socket object (created
23 from an existing file descriptor)
Guido van Rossum30a685f1991-06-27 15:51:29 +000024- socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000025- socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])
Guido van Rossum27e177d1995-03-16 15:43:47 +000026- socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')
Guido van Rossum25405c71996-12-19 16:42:52 +000027- socket.getprotobyname(protocolname) --> protocol number
Barry Warsaw11b91a02004-06-28 00:50:43 +000028- socket.getservbyname(servicename[, protocolname]) --> port number
29- socket.getservbyport(portnumber[, protocolname]) --> service name
Guido van Rossum83a072d2002-09-03 19:10:18 +000030- socket.socket([family[, type [, proto]]]) --> new socket object
Dave Cole331708b2004-08-09 04:51:41 +000031- socket.socketpair([family[, type [, proto]]]) --> (socket, socket)
Guido van Rossum006bf911996-06-12 04:04:55 +000032- socket.ntohs(16 bit value) --> new int object
33- socket.ntohl(32 bit value) --> new int object
34- socket.htons(16 bit value) --> new int object
35- socket.htonl(32 bit value) --> new int object
Martin v. Löwis2d8d4272001-07-21 18:05:31 +000036- socket.getaddrinfo(host, port [, family, socktype, proto, flags])
37 --> List of (family, socktype, proto, canonname, sockaddr)
38- socket.getnameinfo(sockaddr, flags) --> (host, port)
Guido van Rossum27e177d1995-03-16 15:43:47 +000039- socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
Guido van Rossum47dfa4a2003-04-25 05:48:32 +000040- socket.has_ipv6: boolean value indicating if IPv6 is supported
Guido van Rossum5c9eb211999-08-20 18:21:51 +000041- socket.inet_aton(IP address) -> 32-bit packed IP representation
42- socket.inet_ntoa(packed IP) -> IP address string
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +000043- socket.getdefaulttimeout() -> None | float
44- socket.setdefaulttimeout(None | float)
Guido van Rossum6574b3e1991-06-25 21:36:08 +000045- an Internet socket address is a pair (hostname, port)
46 where hostname can be anything recognized by gethostbyname()
47 (including the dd.dd.dd.dd notation) and port is in host byte order
48- where a hostname is returned, the dd.dd.dd.dd notation is used
Guido van Rossum27e177d1995-03-16 15:43:47 +000049- a UNIX domain socket address is a string specifying the pathname
Jeremy Hylton22308652001-02-02 03:23:09 +000050- an AF_PACKET socket address is a tuple containing a string
51 specifying the ethernet interface and an integer specifying
52 the Ethernet protocol number to be received. For example:
Jeremy Hyltondbfb6622001-02-02 19:55:17 +000053 ("eth0",0x1234). Optional 3rd,4th,5th elements in the tuple
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +000054 specify packet-type and ha-type/addr.
Guido van Rossum6574b3e1991-06-25 21:36:08 +000055
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000056Local naming conventions:
Guido van Rossum6574b3e1991-06-25 21:36:08 +000057
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000058- names starting with sock_ are socket object methods
59- names starting with socket_ are module-level functions
60- names starting with PySocket are exported through socketmodule.h
Guido van Rossum30a685f1991-06-27 15:51:29 +000061
Guido van Rossum6574b3e1991-06-25 21:36:08 +000062*/
63
Michael W. Hudson76b8cc82004-05-26 17:06:31 +000064#if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI
65#define _BSD_TYPES
66#endif
67
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000068#include "Python.h"
69
Guido van Rossum47dfa4a2003-04-25 05:48:32 +000070#undef MAX
71#define MAX(x, y) ((x) < (y) ? (y) : (x))
72
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000073/* Socket object documentation */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000074PyDoc_STRVAR(sock_doc,
Guido van Rossumc4fcfa32002-06-07 03:19:37 +000075"socket([family[, type[, proto]]]) -> socket object\n\
76\n\
77Open a socket of the given type. The family argument specifies the\n\
78address family; it defaults to AF_INET. The type argument specifies\n\
79whether this is a stream (SOCK_STREAM, this is the default)\n\
80or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,\n\
81specifying the default protocol. Keyword arguments are accepted.\n\
82\n\
83A socket object represents one endpoint of a network connection.\n\
84\n\
85Methods of socket objects (keyword arguments not allowed):\n\
86\n\
87accept() -- accept a connection, returning new socket and client address\n\
88bind(addr) -- bind the socket to a local address\n\
89close() -- close the socket\n\
90connect(addr) -- connect the socket to a remote address\n\
91connect_ex(addr) -- connect, return an error code instead of an exception\n\
92dup() -- return a new socket object identical to the current one [*]\n\
93fileno() -- return underlying file descriptor\n\
94getpeername() -- return remote address [*]\n\
95getsockname() -- return local address\n\
96getsockopt(level, optname[, buflen]) -- get socket options\n\
97gettimeout() -- return timeout or None\n\
98listen(n) -- start listening for incoming connections\n\
99makefile([mode, [bufsize]]) -- return a file object for the socket [*]\n\
100recv(buflen[, flags]) -- receive data\n\
101recvfrom(buflen[, flags]) -- receive data and sender's address\n\
102sendall(data[, flags]) -- send all data\n\
103send(data[, flags]) -- send data, may not send all of it\n\
104sendto(data[, flags], addr) -- send data to a given address\n\
105setblocking(0 | 1) -- set or clear the blocking I/O flag\n\
106setsockopt(level, optname, value) -- set socket options\n\
107settimeout(None | float) -- set or clear the timeout\n\
108shutdown(how) -- shut down traffic in one or both directions\n\
109\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000110 [*] not available on all platforms!");
Guido van Rossum3baaa131999-03-22 21:44:51 +0000111
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000112/* XXX This is a terrible mess of platform-dependent preprocessor hacks.
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000113 I hope some day someone can clean this up please... */
114
Guido van Rossum9376b741999-09-15 22:01:40 +0000115/* Hacks for gethostbyname_r(). On some non-Linux platforms, the configure
116 script doesn't get this right, so we hardcode some platform checks below.
117 On the other hand, not all Linux versions agree, so there the settings
118 computed by the configure script are needed! */
119
120#ifndef linux
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000121# undef HAVE_GETHOSTBYNAME_R_3_ARG
122# undef HAVE_GETHOSTBYNAME_R_5_ARG
123# undef HAVE_GETHOSTBYNAME_R_6_ARG
Guido van Rossum9376b741999-09-15 22:01:40 +0000124#endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000125
Guido van Rossum7a122991999-04-13 04:07:32 +0000126#ifndef WITH_THREAD
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000127# undef HAVE_GETHOSTBYNAME_R
Guido van Rossum7a122991999-04-13 04:07:32 +0000128#endif
129
Guido van Rossume7de2061999-03-24 17:24:33 +0000130#ifdef HAVE_GETHOSTBYNAME_R
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000131# if defined(_AIX) || defined(__osf__)
132# define HAVE_GETHOSTBYNAME_R_3_ARG
133# elif defined(__sun) || defined(__sgi)
134# define HAVE_GETHOSTBYNAME_R_5_ARG
135# elif defined(linux)
Guido van Rossum9376b741999-09-15 22:01:40 +0000136/* Rely on the configure script */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000137# else
138# undef HAVE_GETHOSTBYNAME_R
139# endif
Guido van Rossume7de2061999-03-24 17:24:33 +0000140#endif
141
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000142#if !defined(HAVE_GETHOSTBYNAME_R) && defined(WITH_THREAD) && \
143 !defined(MS_WINDOWS)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000144# define USE_GETHOSTBYNAME_LOCK
Guido van Rossum3baaa131999-03-22 21:44:51 +0000145#endif
146
Just van Rossum1040d2c2003-05-09 07:53:18 +0000147/* On systems on which getaddrinfo() is believed to not be thread-safe,
Just van Rossum09aecd72003-05-09 08:03:44 +0000148 (this includes the getaddrinfo emulation) protect access with a lock. */
Just van Rossum16e426b2003-05-09 08:12:00 +0000149#if defined(WITH_THREAD) && (defined(__APPLE__) || defined(__FreeBSD__) || \
150 defined(__OpenBSD__) || defined(__NetBSD__) || !defined(HAVE_GETADDRINFO))
Just van Rossum1040d2c2003-05-09 07:53:18 +0000151#define USE_GETADDRINFO_LOCK
152#endif
153
154#ifdef USE_GETADDRINFO_LOCK
155#define ACQUIRE_GETADDRINFO_LOCK PyThread_acquire_lock(netdb_lock, 1);
156#define RELEASE_GETADDRINFO_LOCK PyThread_release_lock(netdb_lock);
157#else
158#define ACQUIRE_GETADDRINFO_LOCK
159#define RELEASE_GETADDRINFO_LOCK
160#endif
161
162#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000163# include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000164#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000165
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000166#if defined(PYCC_VACPP)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000167# include <types.h>
168# include <io.h>
169# include <sys/ioctl.h>
170# include <utils.h>
171# include <ctype.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000172#endif
173
Martin v. Löwis9e437302002-12-06 12:57:26 +0000174#if defined(__VMS)
175#if ! defined(_SOCKADDR_LEN)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000176# ifdef getaddrinfo
177# undef getaddrinfo
178# endif
179# include "TCPIP_IOCTL_ROUTINE"
180#else
181# include <ioctl.h>
182#endif
Martin v. Löwis9e437302002-12-06 12:57:26 +0000183#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000184
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000185#if defined(PYOS_OS2)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000186# define INCL_DOS
187# define INCL_DOSERRORS
188# define INCL_NOPMAPI
189# include <os2.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000190#endif
191
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000192#if defined(__sgi) && _COMPILER_VERSION>700 && !_SGIAPI
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000193/* make sure that the reentrant (gethostbyaddr_r etc)
194 functions are declared correctly if compiling with
195 MIPSPro 7.x in ANSI C mode (default) */
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000196
197/* XXX Using _SGIAPI is the wrong thing,
198 but I don't know what the right thing is. */
Michael W. Hudson76b8cc82004-05-26 17:06:31 +0000199#undef _SGIAPI /* to avoid warning */
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000200#define _SGIAPI 1
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000201
Michael W. Hudson76b8cc82004-05-26 17:06:31 +0000202#undef _XOPEN_SOURCE
203#include <sys/socket.h>
204#include <sys/types.h>
205#include <netinet/in.h>
206#ifdef _SS_ALIGNSIZE
207#define HAVE_GETADDRINFO 1
208#define HAVE_GETNAMEINFO 1
209#endif
210
Jeremy Hyltonfb509a32003-07-17 16:58:48 +0000211#define HAVE_INET_PTON
212#include <netdb.h>
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000213#endif
214
Anthony Baxterbab23cf2003-10-04 08:00:49 +0000215/* Irix 6.5 fails to define this variable at all. This is needed
216 for both GCC and SGI's compiler. I'd say that the SGI headers
217 are just busted. */
218#if defined(__sgi) && !defined(INET_ADDRSTRLEN)
219#define INET_ADDRSTRLEN 16
220#endif
221
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000222/* Generic includes */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000223#include <sys/types.h>
Guido van Rossum81194471991-07-27 21:42:02 +0000224#include <signal.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000225
Marc-André Lemburg976ade62002-02-16 18:47:07 +0000226/* Generic socket object definitions and includes */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000227#define PySocket_BUILDING_SOCKET
Marc-André Lemburgbb8b78b2002-02-16 18:44:52 +0000228#include "socketmodule.h"
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000229
230/* Addressing includes */
231
Guido van Rossum6f489d91996-06-28 20:15:15 +0000232#ifndef MS_WINDOWS
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000233
234/* Non-MS WINDOWS includes */
235# include <netdb.h>
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000236
Guido van Rossum9376b741999-09-15 22:01:40 +0000237/* Headers needed for inet_ntoa() and inet_addr() */
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000238# ifdef __BEOS__
239# include <net/netdb.h>
240# elif defined(PYOS_OS2) && defined(PYCC_VACPP)
241# include <netdb.h>
Tim Peters603c6832001-11-05 02:45:59 +0000242typedef size_t socklen_t;
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000243# else
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000244# include <arpa/inet.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000245# endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +0000246
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000247# ifndef RISCOS
248# include <fcntl.h>
249# else
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000250# include <sys/ioctl.h>
251# include <socklib.h>
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000252# define NO_DUP
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000253int h_errno; /* not used */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000254# define INET_ADDRSTRLEN 16
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000255# endif
256
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000257#else
Guido van Rossum48a680c2001-03-02 06:34:14 +0000258
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000259/* MS_WINDOWS includes */
260# include <fcntl.h>
Guido van Rossum48a680c2001-03-02 06:34:14 +0000261
Jeremy Hylton22308652001-02-02 03:23:09 +0000262#endif
263
Skip Montanaro7befb992004-02-10 16:50:21 +0000264#include <stddef.h>
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000265
266#ifndef offsetof
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +0000267# define offsetof(type, member) ((size_t)(&((type *)0)->member))
Martin v. Löwisa45ecae2001-06-24 21:28:42 +0000268#endif
269
Neal Norwitz39d22e52002-11-02 19:55:21 +0000270#ifndef O_NONBLOCK
271# define O_NONBLOCK O_NDELAY
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000272#endif
273
Michael W. Hudson76b8cc82004-05-26 17:06:31 +0000274#if defined(__sgi) && _COMPILER_VERSION>700 \
275 && !defined(_SS_ALIGNSIZE) /* defined in sys/socket.h */
276 /* by some newer versions of IRIX */
277 /* (e.g. not by 6.5.10 but by 6.5.21) */
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000278#include "addrinfo.h"
Michael W. Hudson76b8cc82004-05-26 17:06:31 +0000279#endif
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000280
Andrew MacIntyre5a8b4592004-07-07 14:02:59 +0000281#if defined(PYOS_OS2) && defined(PYCC_GCC)
282#include "addrinfo.h"
283#endif
284
Raymond Hettingercbcff932004-08-03 08:52:46 +0000285#if defined(_MSC_VER) && _MSC_VER == 1200
286#include "addrinfo.h"
287#endif
288
Jason Tishlerc246cb72004-08-09 13:25:59 +0000289#if defined(__CYGWIN__)
290#include "addrinfo.h"
291#endif
292
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000293#ifndef HAVE_INET_PTON
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000294int inet_pton(int af, const char *src, void *dst);
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000295const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000296#endif
297
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000298#ifdef __APPLE__
299/* On OS X, getaddrinfo returns no error indication of lookup
300 failure, so we must use the emulation instead of the libinfo
301 implementation. Unfortunately, performing an autoconf test
302 for this bug would require DNS access for the machine performing
303 the configuration, which is not acceptable. Therefore, we
304 determine the bug just by checking for __APPLE__. If this bug
305 gets ever fixed, perhaps checking for sys/version.h would be
306 appropriate, which is 10/0 on the system with the bug. */
Jack Jansen84262fb2002-07-02 14:40:42 +0000307#ifndef HAVE_GETNAMEINFO
308/* This bug seems to be fixed in Jaguar. Ths easiest way I could
309 Find to check for Jaguar is that it has getnameinfo(), which
310 older releases don't have */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000311#undef HAVE_GETADDRINFO
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000312#endif
Jack Jansen84262fb2002-07-02 14:40:42 +0000313#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000314
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000315/* I know this is a bad practice, but it is the easiest... */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000316#if !defined(HAVE_GETADDRINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000317/* avoid clashes with the C library definition of the symbol. */
318#define getaddrinfo fake_getaddrinfo
319#define gai_strerror fake_gai_strerror
320#define freeaddrinfo fake_freeaddrinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000321#include "getaddrinfo.c"
322#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000323#if !defined(HAVE_GETNAMEINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000324#define getnameinfo fake_getnameinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000325#include "getnameinfo.c"
326#endif
327
Guido van Rossumbcc20741998-08-04 22:53:56 +0000328#if defined(MS_WINDOWS) || defined(__BEOS__)
329/* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000330/* seem to be a few differences in the API */
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000331#define SOCKETCLOSE closesocket
Guido van Rossumbe32c891996-06-20 16:25:29 +0000332#define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000333#endif
334
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000335#ifdef MS_WIN32
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000336#define EAFNOSUPPORT WSAEAFNOSUPPORT
337#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000338#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000339
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000340#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000341#define SOCKETCLOSE soclose
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000342#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000343#endif
344
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000345#ifndef SOCKETCLOSE
346#define SOCKETCLOSE close
347#endif
348
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000349#ifdef __VMS
350/* TCP/IP Services for VMS uses a maximum send/revc buffer length of 65535 */
351#define SEGMENT_SIZE 65535
352#endif
353
Hye-Shik Chang81268602004-02-02 06:05:24 +0000354#if defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)
355#define USE_BLUETOOTH 1
356#if defined(__FreeBSD__)
357#define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP
358#define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM
359#define sockaddr_l2 sockaddr_l2cap
360#define sockaddr_rc sockaddr_rfcomm
361#define _BT_SOCKADDR_MEMB(s, proto) &((s)->sock_addr)
362#define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb)
363#define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb)
364#else
Anthony Baxter5d7c0672004-02-16 05:35:28 +0000365#define _BT_SOCKADDR_MEMB(s, proto) (&((s)->sock_addr).bt_##proto)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000366#define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)
367#define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)
368#define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)
369#endif
370#endif
371
Martin v. Löwise9416172003-05-03 10:12:45 +0000372/*
373 * Constants for getnameinfo()
374 */
375#if !defined(NI_MAXHOST)
376#define NI_MAXHOST 1025
377#endif
378#if !defined(NI_MAXSERV)
379#define NI_MAXSERV 32
380#endif
381
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000382/* XXX There's a problem here: *static* functions are not supposed to have
383 a Py prefix (or use CapitalizedWords). Later... */
384
Guido van Rossum30a685f1991-06-27 15:51:29 +0000385/* Global variable holding the exception type for errors detected
386 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000387static PyObject *socket_error;
388static PyObject *socket_herror;
389static PyObject *socket_gaierror;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000390static PyObject *socket_timeout;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000391
Guido van Rossum48a680c2001-03-02 06:34:14 +0000392#ifdef RISCOS
393/* Global variable which is !=0 if Python is running in a RISC OS taskwindow */
394static int taskwindow;
395#endif
396
Tim Peters643a7fc2002-02-17 04:13:21 +0000397/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000398 The sock_type variable contains pointers to various functions,
399 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000400 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000401static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000402
Guido van Rossum30a685f1991-06-27 15:51:29 +0000403/* Convenience function to raise an error according to errno
404 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000405
Guido van Rossum73624e91994-10-10 17:59:00 +0000406static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000407set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000408{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000409#ifdef MS_WINDOWS
Mark Hammond46a733d2000-07-24 01:45:11 +0000410 int err_no = WSAGetLastError();
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000411 static struct {
412 int no;
413 const char *msg;
414 } *msgp, msgs[] = {
415 {WSAEINTR, "Interrupted system call"},
416 {WSAEBADF, "Bad file descriptor"},
417 {WSAEACCES, "Permission denied"},
418 {WSAEFAULT, "Bad address"},
419 {WSAEINVAL, "Invalid argument"},
420 {WSAEMFILE, "Too many open files"},
421 {WSAEWOULDBLOCK,
422 "The socket operation could not complete "
423 "without blocking"},
424 {WSAEINPROGRESS, "Operation now in progress"},
425 {WSAEALREADY, "Operation already in progress"},
426 {WSAENOTSOCK, "Socket operation on non-socket"},
427 {WSAEDESTADDRREQ, "Destination address required"},
428 {WSAEMSGSIZE, "Message too long"},
429 {WSAEPROTOTYPE, "Protocol wrong type for socket"},
430 {WSAENOPROTOOPT, "Protocol not available"},
431 {WSAEPROTONOSUPPORT, "Protocol not supported"},
432 {WSAESOCKTNOSUPPORT, "Socket type not supported"},
433 {WSAEOPNOTSUPP, "Operation not supported"},
434 {WSAEPFNOSUPPORT, "Protocol family not supported"},
435 {WSAEAFNOSUPPORT, "Address family not supported"},
436 {WSAEADDRINUSE, "Address already in use"},
437 {WSAEADDRNOTAVAIL, "Can't assign requested address"},
438 {WSAENETDOWN, "Network is down"},
439 {WSAENETUNREACH, "Network is unreachable"},
440 {WSAENETRESET, "Network dropped connection on reset"},
441 {WSAECONNABORTED, "Software caused connection abort"},
442 {WSAECONNRESET, "Connection reset by peer"},
443 {WSAENOBUFS, "No buffer space available"},
444 {WSAEISCONN, "Socket is already connected"},
445 {WSAENOTCONN, "Socket is not connected"},
446 {WSAESHUTDOWN, "Can't send after socket shutdown"},
447 {WSAETOOMANYREFS, "Too many references: can't splice"},
448 {WSAETIMEDOUT, "Operation timed out"},
449 {WSAECONNREFUSED, "Connection refused"},
450 {WSAELOOP, "Too many levels of symbolic links"},
451 {WSAENAMETOOLONG, "File name too long"},
452 {WSAEHOSTDOWN, "Host is down"},
453 {WSAEHOSTUNREACH, "No route to host"},
454 {WSAENOTEMPTY, "Directory not empty"},
455 {WSAEPROCLIM, "Too many processes"},
456 {WSAEUSERS, "Too many users"},
457 {WSAEDQUOT, "Disc quota exceeded"},
458 {WSAESTALE, "Stale NFS file handle"},
459 {WSAEREMOTE, "Too many levels of remote in path"},
460 {WSASYSNOTREADY, "Network subsystem is unvailable"},
461 {WSAVERNOTSUPPORTED, "WinSock version is not supported"},
462 {WSANOTINITIALISED,
463 "Successful WSAStartup() not yet performed"},
464 {WSAEDISCON, "Graceful shutdown in progress"},
465 /* Resolver errors */
466 {WSAHOST_NOT_FOUND, "No such host is known"},
467 {WSATRY_AGAIN, "Host not found, or server failed"},
468 {WSANO_RECOVERY, "Unexpected server error encountered"},
469 {WSANO_DATA, "Valid name without requested data"},
470 {WSANO_ADDRESS, "No address, look for MX record"},
471 {0, NULL}
472 };
Mark Hammond46a733d2000-07-24 01:45:11 +0000473 if (err_no) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000474 PyObject *v;
Mark Hammond46a733d2000-07-24 01:45:11 +0000475 const char *msg = "winsock error";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000476
Mark Hammond46a733d2000-07-24 01:45:11 +0000477 for (msgp = msgs; msgp->msg; msgp++) {
478 if (err_no == msgp->no) {
479 msg = msgp->msg;
480 break;
481 }
482 }
483
484 v = Py_BuildValue("(is)", err_no, msg);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000485 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000486 PyErr_SetObject(socket_error, v);
Guido van Rossum73624e91994-10-10 17:59:00 +0000487 Py_DECREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000488 }
489 return NULL;
490 }
491 else
492#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000493
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000494#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000495 if (sock_errno() != NO_ERROR) {
496 APIRET rc;
497 ULONG msglen;
498 char outbuf[100];
499 int myerrorcode = sock_errno();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000500
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000501 /* Retrieve socket-related error message from MPTN.MSG file */
502 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
503 myerrorcode - SOCBASEERR + 26,
504 "mptn.msg",
505 &msglen);
506 if (rc == NO_ERROR) {
507 PyObject *v;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000508
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000509 /* OS/2 doesn't guarantee a terminator */
510 outbuf[msglen] = '\0';
511 if (strlen(outbuf) > 0) {
512 /* If non-empty msg, trim CRLF */
513 char *lastc = &outbuf[ strlen(outbuf)-1 ];
514 while (lastc > outbuf && isspace(*lastc)) {
515 /* Trim trailing whitespace (CRLF) */
516 *lastc-- = '\0';
517 }
518 }
519 v = Py_BuildValue("(is)", myerrorcode, outbuf);
520 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000521 PyErr_SetObject(socket_error, v);
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000522 Py_DECREF(v);
523 }
524 return NULL;
525 }
526 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000527#endif
528
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000529#if defined(RISCOS)
530 if (_inet_error.errnum != NULL) {
531 PyObject *v;
532 v = Py_BuildValue("(is)", errno, _inet_err());
533 if (v != NULL) {
534 PyErr_SetObject(socket_error, v);
535 Py_DECREF(v);
536 }
537 return NULL;
538 }
539#endif
540
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000541 return PyErr_SetFromErrno(socket_error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000542}
543
Guido van Rossum30a685f1991-06-27 15:51:29 +0000544
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000545static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000546set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000547{
548 PyObject *v;
549
550#ifdef HAVE_HSTRERROR
551 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
552#else
553 v = Py_BuildValue("(is)", h_error, "host not found");
554#endif
555 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000556 PyErr_SetObject(socket_herror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000557 Py_DECREF(v);
558 }
559
560 return NULL;
561}
562
563
564static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000565set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000566{
567 PyObject *v;
568
Martin v. Löwis272cb402002-03-01 08:31:07 +0000569#ifdef EAI_SYSTEM
570 /* EAI_SYSTEM is not available on Windows XP. */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000571 if (error == EAI_SYSTEM)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000572 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000573#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000574
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000575#ifdef HAVE_GAI_STRERROR
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000576 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000577#else
578 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
579#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000580 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000581 PyErr_SetObject(socket_gaierror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000582 Py_DECREF(v);
583 }
584
585 return NULL;
586}
587
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000588/* Function to perform the setting of socket blocking mode
589 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000590static int
591internal_setblocking(PySocketSockObject *s, int block)
592{
593#ifndef RISCOS
594#ifndef MS_WINDOWS
595 int delay_flag;
596#endif
597#endif
598
599 Py_BEGIN_ALLOW_THREADS
600#ifdef __BEOS__
601 block = !block;
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000602 setsockopt(s->sock_fd, SOL_SOCKET, SO_NONBLOCK,
603 (void *)(&block), sizeof(int));
Guido van Rossum67f7a382002-06-06 21:08:16 +0000604#else
605#ifndef RISCOS
606#ifndef MS_WINDOWS
607#if defined(PYOS_OS2) && !defined(PYCC_GCC)
608 block = !block;
609 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000610#elif defined(__VMS)
611 block = !block;
612 ioctl(s->sock_fd, FIONBIO, (char *)&block);
613#else /* !PYOS_OS2 && !_VMS */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000614 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
615 if (block)
Neal Norwitz39d22e52002-11-02 19:55:21 +0000616 delay_flag &= (~O_NONBLOCK);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000617 else
Neal Norwitz39d22e52002-11-02 19:55:21 +0000618 delay_flag |= O_NONBLOCK;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000619 fcntl(s->sock_fd, F_SETFL, delay_flag);
620#endif /* !PYOS_OS2 */
621#else /* MS_WINDOWS */
622 block = !block;
623 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
624#endif /* MS_WINDOWS */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000625#else /* RISCOS */
626 block = !block;
627 socketioctl(s->sock_fd, FIONBIO, (u_long*)&block);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000628#endif /* RISCOS */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000629#endif /* __BEOS__ */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000630 Py_END_ALLOW_THREADS
631
632 /* Since these don't return anything */
633 return 1;
634}
635
Guido van Rossum11ba0942002-06-13 15:07:44 +0000636/* Do a select() on the socket, if necessary (sock_timeout > 0).
637 The argument writing indicates the direction.
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000638 This does not raise an exception; we'll let our caller do that
639 after they've reacquired the interpreter lock.
640 Returns 1 on timeout, 0 otherwise. */
641static int
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000642internal_select(PySocketSockObject *s, int writing)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000643{
644 fd_set fds;
645 struct timeval tv;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000646 int n;
Guido van Rossum11ba0942002-06-13 15:07:44 +0000647
Guido van Rossumad654902002-07-19 12:44:59 +0000648 /* Nothing to do unless we're in timeout mode (not non-blocking) */
Guido van Rossum11ba0942002-06-13 15:07:44 +0000649 if (s->sock_timeout <= 0.0)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000650 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000651
Guido van Rossumad654902002-07-19 12:44:59 +0000652 /* Guard against closed socket */
653 if (s->sock_fd < 0)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000654 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000655
Guido van Rossum67f7a382002-06-06 21:08:16 +0000656 /* Construct the arguments to select */
657 tv.tv_sec = (int)s->sock_timeout;
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000658 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000659 FD_ZERO(&fds);
660 FD_SET(s->sock_fd, &fds);
661
662 /* See if the socket is ready */
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000663 if (writing)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000664 n = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000665 else
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000666 n = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
667 if (n == 0)
668 return 1;
669 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000670}
671
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000672/* Initialize a new socket object. */
673
Tim Petersa12b4cf2002-07-18 22:38:44 +0000674static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000675
Mark Hammond62b1ab12002-07-23 06:31:15 +0000676PyMODINIT_FUNC
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000677init_sockobject(PySocketSockObject *s,
678 SOCKET_T fd, int family, int type, int proto)
679{
680#ifdef RISCOS
681 int block = 1;
682#endif
683 s->sock_fd = fd;
684 s->sock_family = family;
685 s->sock_type = type;
686 s->sock_proto = proto;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000687 s->sock_timeout = defaulttimeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000688
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000689 s->errorhandler = &set_error;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000690
691 if (defaulttimeout >= 0.0)
692 internal_setblocking(s, 0);
693
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000694#ifdef RISCOS
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000695 if (taskwindow)
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000696 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000697#endif
698}
699
700
Guido van Rossum30a685f1991-06-27 15:51:29 +0000701/* Create a new socket object.
702 This just creates the object and initializes it.
703 If the creation fails, return NULL and set an exception (implicit
704 in NEWOBJ()). */
705
Guido van Rossum73624e91994-10-10 17:59:00 +0000706static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000707new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000708{
Guido van Rossum73624e91994-10-10 17:59:00 +0000709 PySocketSockObject *s;
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000710 s = (PySocketSockObject *)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000711 PyType_GenericNew(&sock_type, NULL, NULL);
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000712 if (s != NULL)
713 init_sockobject(s, fd, family, type, proto);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000714 return s;
715}
716
Guido van Rossum30a685f1991-06-27 15:51:29 +0000717
Guido van Rossum48a680c2001-03-02 06:34:14 +0000718/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000719 thread to be in gethostbyname or getaddrinfo */
720#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
721PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000722#endif
723
724
Guido van Rossum30a685f1991-06-27 15:51:29 +0000725/* Convert a string specifying a host name or one of a few symbolic
726 names to a numeric IP address. This usually calls gethostbyname()
727 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000728 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000729 an error occurred; then an exception is raised. */
730
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000731static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000732setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000733{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000734 struct addrinfo hints, *res;
735 int error;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000736 int d1, d2, d3, d4;
737 char ch;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000738
Guido van Rossuma376cc51996-12-05 23:43:35 +0000739 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
Guido van Rossum30a685f1991-06-27 15:51:29 +0000740 if (name[0] == '\0') {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000741 int siz;
742 memset(&hints, 0, sizeof(hints));
743 hints.ai_family = af;
744 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
745 hints.ai_flags = AI_PASSIVE;
Just van Rossum1040d2c2003-05-09 07:53:18 +0000746 Py_BEGIN_ALLOW_THREADS
747 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000748 error = getaddrinfo(NULL, "0", &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +0000749 Py_END_ALLOW_THREADS
750 /* We assume that those thread-unsafe getaddrinfo() versions
751 *are* safe regarding their return value, ie. that a
752 subsequent call to getaddrinfo() does not destroy the
753 outcome of the first call. */
754 RELEASE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000755 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000756 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000757 return -1;
758 }
759 switch (res->ai_family) {
760 case AF_INET:
761 siz = 4;
762 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000763#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000764 case AF_INET6:
765 siz = 16;
766 break;
767#endif
768 default:
769 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000770 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000771 "unsupported address family");
772 return -1;
773 }
774 if (res->ai_next) {
Martin v. Löwisf0b11d22001-11-07 08:31:03 +0000775 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000776 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000777 "wildcard resolved to multiple address");
778 return -1;
779 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000780 if (res->ai_addrlen < addr_ret_size)
781 addr_ret_size = res->ai_addrlen;
782 memcpy(addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000783 freeaddrinfo(res);
784 return siz;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000785 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000786 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000787 struct sockaddr_in *sin;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +0000788 if (af != AF_INET && af != AF_UNSPEC) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000789 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000790 "address family mismatched");
791 return -1;
792 }
793 sin = (struct sockaddr_in *)addr_ret;
794 memset((void *) sin, '\0', sizeof(*sin));
795 sin->sin_family = AF_INET;
796#ifdef HAVE_SOCKADDR_SA_LEN
797 sin->sin_len = sizeof(*sin);
798#endif
799 sin->sin_addr.s_addr = INADDR_BROADCAST;
800 return sizeof(sin->sin_addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000801 }
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000802 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
803 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
804 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
805 struct sockaddr_in *sin;
806 sin = (struct sockaddr_in *)addr_ret;
807 sin->sin_addr.s_addr = htonl(
808 ((long) d1 << 24) | ((long) d2 << 16) |
809 ((long) d3 << 8) | ((long) d4 << 0));
810 sin->sin_family = AF_INET;
811#ifdef HAVE_SOCKADDR_SA_LEN
812 sin->sin_len = sizeof(*sin);
813#endif
814 return 4;
815 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000816 memset(&hints, 0, sizeof(hints));
817 hints.ai_family = af;
Just van Rossum1040d2c2003-05-09 07:53:18 +0000818 Py_BEGIN_ALLOW_THREADS
819 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000820 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000821#if defined(__digital__) && defined(__unix__)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000822 if (error == EAI_NONAME && af == AF_UNSPEC) {
823 /* On Tru64 V5.1, numeric-to-addr conversion fails
824 if no address family is given. Assume IPv4 for now.*/
825 hints.ai_family = AF_INET;
826 error = getaddrinfo(name, NULL, &hints, &res);
827 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000828#endif
Just van Rossum1040d2c2003-05-09 07:53:18 +0000829 Py_END_ALLOW_THREADS
830 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000831 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000832 set_gaierror(error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000833 return -1;
834 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000835 if (res->ai_addrlen < addr_ret_size)
836 addr_ret_size = res->ai_addrlen;
837 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000838 freeaddrinfo(res);
839 switch (addr_ret->sa_family) {
840 case AF_INET:
841 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000842#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000843 case AF_INET6:
844 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000845#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000846 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000847 PyErr_SetString(socket_error, "unknown address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000848 return -1;
849 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000850}
851
Guido van Rossum30a685f1991-06-27 15:51:29 +0000852
Guido van Rossum30a685f1991-06-27 15:51:29 +0000853/* Create a string object representing an IP address.
854 This is always a string of the form 'dd.dd.dd.dd' (with variable
855 size numbers). */
856
Guido van Rossum73624e91994-10-10 17:59:00 +0000857static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000858makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000859{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000860 char buf[NI_MAXHOST];
861 int error;
862
863 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
864 NI_NUMERICHOST);
865 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000866 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000867 return NULL;
868 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000869 return PyString_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000870}
871
872
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000873#ifdef USE_BLUETOOTH
874/* Convert a string representation of a Bluetooth address into a numeric
875 address. Returns the length (6), or raises an exception and returns -1 if
876 an error occurred. */
877
878static int
879setbdaddr(char *name, bdaddr_t *bdaddr)
880{
881 unsigned int b0, b1, b2, b3, b4, b5;
882 char ch;
883 int n;
884
885 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
886 &b5, &b4, &b3, &b2, &b1, &b0, &ch);
887 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
888 bdaddr->b[0] = b0;
889 bdaddr->b[1] = b1;
890 bdaddr->b[2] = b2;
891 bdaddr->b[3] = b3;
892 bdaddr->b[4] = b4;
893 bdaddr->b[5] = b5;
894 return 6;
895 } else {
896 PyErr_SetString(socket_error, "bad bluetooth address");
897 return -1;
898 }
899}
900
901/* Create a string representation of the Bluetooth address. This is always a
902 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
903 value (zero padded if necessary). */
904
905static PyObject *
906makebdaddr(bdaddr_t *bdaddr)
907{
908 char buf[(6 * 2) + 5 + 1];
909
910 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
911 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
912 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
913 return PyString_FromString(buf);
914}
915#endif
916
917
Guido van Rossum30a685f1991-06-27 15:51:29 +0000918/* Create an object representing the given socket address,
919 suitable for passing it back to bind(), connect() etc.
920 The family field of the sockaddr structure is inspected
921 to determine what kind of address it really is. */
922
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000923/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000924static PyObject *
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000925makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000926{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000927 if (addrlen == 0) {
928 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +0000929 Py_INCREF(Py_None);
930 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000931 }
932
Guido van Rossumbcc20741998-08-04 22:53:56 +0000933#ifdef __BEOS__
Guido van Rossum2c8bcb82000-04-25 21:34:53 +0000934 /* XXX: BeOS version of accept() doesn't set family correctly */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000935 addr->sa_family = AF_INET;
936#endif
937
Guido van Rossum30a685f1991-06-27 15:51:29 +0000938 switch (addr->sa_family) {
939
940 case AF_INET:
941 {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000942 struct sockaddr_in *a;
943 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
Barry Warsaw752300b1997-01-03 17:18:10 +0000944 PyObject *ret = NULL;
945 if (addrobj) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000946 a = (struct sockaddr_in *)addr;
Barry Warsaw752300b1997-01-03 17:18:10 +0000947 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
948 Py_DECREF(addrobj);
949 }
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000950 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000951 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000952
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +0000953#if defined(AF_UNIX)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000954 case AF_UNIX:
955 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000956 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000957 return PyString_FromString(a->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000958 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000959#endif /* AF_UNIX */
960
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000961#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000962 case AF_INET6:
963 {
964 struct sockaddr_in6 *a;
965 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
966 PyObject *ret = NULL;
967 if (addrobj) {
968 a = (struct sockaddr_in6 *)addr;
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000969 ret = Py_BuildValue("Oiii",
970 addrobj,
971 ntohs(a->sin6_port),
972 a->sin6_flowinfo,
973 a->sin6_scope_id);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000974 Py_DECREF(addrobj);
975 }
976 return ret;
977 }
Jeremy Hylton22308652001-02-02 03:23:09 +0000978#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +0000979
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000980#ifdef USE_BLUETOOTH
981 case AF_BLUETOOTH:
982 switch (proto) {
983
984 case BTPROTO_L2CAP:
985 {
986 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
987 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
988 PyObject *ret = NULL;
989 if (addrobj) {
990 ret = Py_BuildValue("Oi",
991 addrobj,
992 _BT_L2_MEMB(a, psm));
993 Py_DECREF(addrobj);
994 }
995 return ret;
996 }
997
998 case BTPROTO_RFCOMM:
999 {
1000 struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
1001 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
1002 PyObject *ret = NULL;
1003 if (addrobj) {
1004 ret = Py_BuildValue("Oi",
1005 addrobj,
1006 _BT_RC_MEMB(a, channel));
1007 Py_DECREF(addrobj);
1008 }
1009 return ret;
1010 }
1011
1012#if !defined(__FreeBSD__)
1013 case BTPROTO_SCO:
1014 {
1015 struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
1016 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
1017 }
1018#endif
1019
1020 }
1021#endif
1022
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001023#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001024 case AF_PACKET:
1025 {
1026 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
1027 char *ifname = "";
1028 struct ifreq ifr;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001029 /* need to look up interface name give index */
1030 if (a->sll_ifindex) {
Jeremy Hylton22308652001-02-02 03:23:09 +00001031 ifr.ifr_ifindex = a->sll_ifindex;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001032 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
Jeremy Hylton22308652001-02-02 03:23:09 +00001033 ifname = ifr.ifr_name;
Jeremy Hylton22308652001-02-02 03:23:09 +00001034 }
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001035 return Py_BuildValue("shbhs#",
1036 ifname,
1037 ntohs(a->sll_protocol),
1038 a->sll_pkttype,
1039 a->sll_hatype,
1040 a->sll_addr,
1041 a->sll_halen);
Jeremy Hylton22308652001-02-02 03:23:09 +00001042 }
1043#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001044
Guido van Rossum30a685f1991-06-27 15:51:29 +00001045 /* More cases here... */
1046
1047 default:
Guido van Rossumaa948df1997-05-07 17:41:48 +00001048 /* If we don't know the address family, don't raise an
1049 exception -- return it as a tuple. */
1050 return Py_BuildValue("is#",
1051 addr->sa_family,
1052 addr->sa_data,
1053 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001054
Guido van Rossum30a685f1991-06-27 15:51:29 +00001055 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001056}
1057
Guido van Rossum30a685f1991-06-27 15:51:29 +00001058
1059/* Parse a socket address argument according to the socket object's
1060 address family. Return 1 if the address was in the proper format,
1061 0 of not. The address is returned through addr_ret, its length
1062 through len_ret. */
1063
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001064static int
Guido van Rossum48a680c2001-03-02 06:34:14 +00001065getsockaddrarg(PySocketSockObject *s, PyObject *args,
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001066 struct sockaddr **addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001067{
Guido van Rossum30a685f1991-06-27 15:51:29 +00001068 switch (s->sock_family) {
1069
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001070#if defined(AF_UNIX)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001071 case AF_UNIX:
1072 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001073 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001074 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001075 int len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001076 addr = (struct sockaddr_un*)&(s->sock_addr).un;
Guido van Rossum7e488981998-10-08 02:25:24 +00001077 if (!PyArg_Parse(args, "t#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001078 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001079 if (len > sizeof addr->sun_path) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001080 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +00001081 "AF_UNIX path too long");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001082 return 0;
1083 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001084 addr->sun_family = s->sock_family;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001085 memcpy(addr->sun_path, path, len);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001086 addr->sun_path[len] = 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001087 *addr_ret = (struct sockaddr *) addr;
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001088#if defined(PYOS_OS2)
1089 *len_ret = sizeof(*addr);
1090#else
Guido van Rossum65af28a1996-06-11 18:36:33 +00001091 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001092#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001093 return 1;
1094 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001095#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001096
Guido van Rossum30a685f1991-06-27 15:51:29 +00001097 case AF_INET:
1098 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001099 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001100 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001101 int port, result;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001102 addr=(struct sockaddr_in*)&(s->sock_addr).in;
Guido van Rossume4dad902000-12-01 13:13:11 +00001103 if (!PyTuple_Check(args)) {
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001104 PyErr_Format(
1105 PyExc_TypeError,
1106 "getsockaddrarg: "
1107 "AF_INET address must be tuple, not %.500s",
1108 args->ob_type->tp_name);
Guido van Rossume4dad902000-12-01 13:13:11 +00001109 return 0;
1110 }
Martin v. Löwis2548c732003-04-18 10:39:54 +00001111 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
1112 "idna", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001113 return 0;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001114 result = setipaddr(host, (struct sockaddr *)addr,
1115 sizeof(*addr), AF_INET);
1116 PyMem_Free(host);
1117 if (result < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001118 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001119 addr->sin_family = AF_INET;
Guido van Rossum644a12b1997-04-09 19:24:53 +00001120 addr->sin_port = htons((short)port);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001121 *addr_ret = (struct sockaddr *) addr;
1122 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001123 return 1;
1124 }
1125
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001126#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001127 case AF_INET6:
1128 {
1129 struct sockaddr_in6* addr;
1130 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001131 int port, flowinfo, scope_id, result;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001132 addr = (struct sockaddr_in6*)&(s->sock_addr).in6;
1133 flowinfo = scope_id = 0;
Martin v. Löwis2548c732003-04-18 10:39:54 +00001134 if (!PyArg_ParseTuple(args, "eti|ii",
1135 "idna", &host, &port, &flowinfo,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001136 &scope_id)) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001137 return 0;
1138 }
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001139 result = setipaddr(host, (struct sockaddr *)addr,
1140 sizeof(*addr), AF_INET6);
1141 PyMem_Free(host);
1142 if (result < 0)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001143 return 0;
1144 addr->sin6_family = s->sock_family;
1145 addr->sin6_port = htons((short)port);
1146 addr->sin6_flowinfo = flowinfo;
1147 addr->sin6_scope_id = scope_id;
1148 *addr_ret = (struct sockaddr *) addr;
1149 *len_ret = sizeof *addr;
1150 return 1;
1151 }
1152#endif
1153
Hye-Shik Chang81268602004-02-02 06:05:24 +00001154#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00001155 case AF_BLUETOOTH:
1156 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001157 switch (s->sock_proto) {
1158 case BTPROTO_L2CAP:
Martin v. Löwis12af0482004-01-31 12:34:17 +00001159 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001160 struct sockaddr_l2 *addr = (struct sockaddr_l2 *) _BT_SOCKADDR_MEMB(s, l2);
1161 char *straddr;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001162
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001163 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
1164 if (!PyArg_ParseTuple(args, "si", &straddr,
1165 &_BT_L2_MEMB(addr, psm))) {
1166 PyErr_SetString(socket_error, "getsockaddrarg: "
1167 "wrong format");
Martin v. Löwis12af0482004-01-31 12:34:17 +00001168 return 0;
1169 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001170 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
1171 return 0;
1172
1173 *addr_ret = (struct sockaddr *) addr;
1174 *len_ret = sizeof *addr;
1175 return 1;
1176 }
1177 case BTPROTO_RFCOMM:
1178 {
1179 struct sockaddr_rc *addr = (struct sockaddr_rc *) _BT_SOCKADDR_MEMB(s, rc);
1180 char *straddr;
1181
1182 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
1183 if (!PyArg_ParseTuple(args, "si", &straddr,
1184 &_BT_RC_MEMB(addr, channel))) {
1185 PyErr_SetString(socket_error, "getsockaddrarg: "
1186 "wrong format");
1187 return 0;
1188 }
1189 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
1190 return 0;
1191
1192 *addr_ret = (struct sockaddr *) addr;
1193 *len_ret = sizeof *addr;
1194 return 1;
1195 }
1196#if !defined(__FreeBSD__)
1197 case BTPROTO_SCO:
1198 {
1199 struct sockaddr_sco *addr = (struct sockaddr_sco *) _BT_SOCKADDR_MEMB(s, sco);
1200 char *straddr;
1201
1202 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
1203 straddr = PyString_AsString(args);
1204 if (straddr == NULL) {
1205 PyErr_SetString(socket_error, "getsockaddrarg: "
1206 "wrong format");
1207 return 0;
1208 }
1209 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
1210 return 0;
1211
1212 *addr_ret = (struct sockaddr *) addr;
1213 *len_ret = sizeof *addr;
1214 return 1;
1215 }
1216#endif
1217 default:
1218 PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol");
1219 return 0;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001220 }
1221 }
1222#endif
1223
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001224#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001225 case AF_PACKET:
1226 {
1227 struct sockaddr_ll* addr;
1228 struct ifreq ifr;
1229 char *interfaceName;
1230 int protoNumber;
1231 int hatype = 0;
1232 int pkttype = 0;
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001233 char *haddr = NULL;
1234 unsigned int halen = 0;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001235
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001236 if (!PyArg_ParseTuple(args, "si|iis#", &interfaceName,
1237 &protoNumber, &pkttype, &hatype,
1238 &haddr, &halen))
Jeremy Hylton22308652001-02-02 03:23:09 +00001239 return 0;
1240 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1241 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001242 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001243 s->errorhandler();
Jeremy Hylton22308652001-02-02 03:23:09 +00001244 return 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001245 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001246 addr = &(s->sock_addr.ll);
1247 addr->sll_family = AF_PACKET;
1248 addr->sll_protocol = htons((short)protoNumber);
1249 addr->sll_ifindex = ifr.ifr_ifindex;
1250 addr->sll_pkttype = pkttype;
1251 addr->sll_hatype = hatype;
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001252 if (halen > 8) {
1253 PyErr_SetString(PyExc_ValueError,
1254 "Hardware address must be 8 bytes or less");
1255 return 0;
1256 }
1257 if (halen != 0) {
1258 memcpy(&addr->sll_addr, haddr, halen);
1259 }
1260 addr->sll_halen = halen;
Jeremy Hylton22308652001-02-02 03:23:09 +00001261 *addr_ret = (struct sockaddr *) addr;
1262 *len_ret = sizeof *addr;
1263 return 1;
1264 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001265#endif
1266
Guido van Rossum30a685f1991-06-27 15:51:29 +00001267 /* More cases here... */
1268
1269 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001270 PyErr_SetString(socket_error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001271 return 0;
1272
1273 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001274}
1275
Guido van Rossum30a685f1991-06-27 15:51:29 +00001276
Guido van Rossum48a680c2001-03-02 06:34:14 +00001277/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00001278 Return 1 if the family is known, 0 otherwise. The length is returned
1279 through len_ret. */
1280
1281static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00001282getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001283{
1284 switch (s->sock_family) {
1285
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001286#if defined(AF_UNIX)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001287 case AF_UNIX:
1288 {
1289 *len_ret = sizeof (struct sockaddr_un);
1290 return 1;
1291 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001292#endif /* AF_UNIX */
Guido van Rossum710e1df1992-06-12 10:39:36 +00001293
1294 case AF_INET:
1295 {
1296 *len_ret = sizeof (struct sockaddr_in);
1297 return 1;
1298 }
1299
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001300#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001301 case AF_INET6:
1302 {
1303 *len_ret = sizeof (struct sockaddr_in6);
1304 return 1;
1305 }
1306#endif
1307
Hye-Shik Chang81268602004-02-02 06:05:24 +00001308#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00001309 case AF_BLUETOOTH:
1310 {
1311 switch(s->sock_proto)
1312 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001313
1314 case BTPROTO_L2CAP:
1315 *len_ret = sizeof (struct sockaddr_l2);
1316 return 1;
1317 case BTPROTO_RFCOMM:
1318 *len_ret = sizeof (struct sockaddr_rc);
1319 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001320#if !defined(__FreeBSD__)
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001321 case BTPROTO_SCO:
1322 *len_ret = sizeof (struct sockaddr_sco);
1323 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001324#endif
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001325 default:
1326 PyErr_SetString(socket_error, "getsockaddrlen: "
1327 "unknown BT protocol");
1328 return 0;
1329
Martin v. Löwis12af0482004-01-31 12:34:17 +00001330 }
1331 }
1332#endif
1333
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001334#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001335 case AF_PACKET:
1336 {
1337 *len_ret = sizeof (struct sockaddr_ll);
1338 return 1;
1339 }
1340#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001341
Guido van Rossum710e1df1992-06-12 10:39:36 +00001342 /* More cases here... */
1343
1344 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001345 PyErr_SetString(socket_error, "getsockaddrlen: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +00001346 return 0;
1347
1348 }
1349}
1350
1351
Guido van Rossum30a685f1991-06-27 15:51:29 +00001352/* s.accept() method */
1353
Guido van Rossum73624e91994-10-10 17:59:00 +00001354static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001355sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001356{
1357 char addrbuf[256];
Fred Drakea04eaad2000-06-30 02:46:07 +00001358 SOCKET_T newfd;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001359 socklen_t addrlen;
Barry Warsaw752300b1997-01-03 17:18:10 +00001360 PyObject *sock = NULL;
1361 PyObject *addr = NULL;
1362 PyObject *res = NULL;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001363 int timeout;
Barry Warsaw752300b1997-01-03 17:18:10 +00001364
Guido van Rossum710e1df1992-06-12 10:39:36 +00001365 if (!getsockaddrlen(s, &addrlen))
1366 return NULL;
Fred Drakefd168342001-05-09 19:11:33 +00001367 memset(addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001368
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001369#ifdef MS_WINDOWS
1370 newfd = INVALID_SOCKET;
1371#else
1372 newfd = -1;
1373#endif
1374
Guido van Rossum73624e91994-10-10 17:59:00 +00001375 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001376 timeout = internal_select(s, 0);
1377 if (!timeout)
1378 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf,
1379 &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001380 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001381
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001382 if (timeout) {
1383 PyErr_SetString(socket_timeout, "timed out");
1384 return NULL;
1385 }
1386
Fred Drakea04eaad2000-06-30 02:46:07 +00001387#ifdef MS_WINDOWS
1388 if (newfd == INVALID_SOCKET)
1389#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001390 if (newfd < 0)
Fred Drakea04eaad2000-06-30 02:46:07 +00001391#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001392 return s->errorhandler();
Barry Warsaw752300b1997-01-03 17:18:10 +00001393
Guido van Rossum30a685f1991-06-27 15:51:29 +00001394 /* Create the new object with unspecified family,
1395 to avoid calls to bind() etc. on it. */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001396 sock = (PyObject *) new_sockobject(newfd,
1397 s->sock_family,
1398 s->sock_type,
1399 s->sock_proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001400
Barry Warsaw752300b1997-01-03 17:18:10 +00001401 if (sock == NULL) {
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001402 SOCKETCLOSE(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +00001403 goto finally;
1404 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001405 addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001406 addrlen, s->sock_proto);
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001407 if (addr == NULL)
Barry Warsaw752300b1997-01-03 17:18:10 +00001408 goto finally;
1409
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001410 res = PyTuple_Pack(2, sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001411
Guido van Rossum67f7a382002-06-06 21:08:16 +00001412finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001413 Py_XDECREF(sock);
1414 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001415 return res;
1416}
1417
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001418PyDoc_STRVAR(accept_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001419"accept() -> (socket object, address info)\n\
1420\n\
1421Wait for an incoming connection. Return a new socket representing the\n\
1422connection, and the address of the client. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001423info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001424
Guido van Rossum11ba0942002-06-13 15:07:44 +00001425/* s.setblocking(flag) method. Argument:
1426 False -- non-blocking mode; same as settimeout(0)
1427 True -- blocking mode; same as settimeout(None)
1428*/
Guido van Rossume4485b01994-09-07 14:32:49 +00001429
Guido van Rossum73624e91994-10-10 17:59:00 +00001430static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001431sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00001432{
1433 int block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001434
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001435 block = PyInt_AsLong(arg);
1436 if (block == -1 && PyErr_Occurred())
Guido van Rossume4485b01994-09-07 14:32:49 +00001437 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001438
Guido van Rossum11ba0942002-06-13 15:07:44 +00001439 s->sock_timeout = block ? -1.0 : 0.0;
Guido van Rossume8008f02002-06-07 03:36:20 +00001440 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00001441
Guido van Rossum73624e91994-10-10 17:59:00 +00001442 Py_INCREF(Py_None);
1443 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00001444}
Guido van Rossume4485b01994-09-07 14:32:49 +00001445
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001446PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001447"setblocking(flag)\n\
1448\n\
1449Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001450setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001451setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001452
Guido van Rossum11ba0942002-06-13 15:07:44 +00001453/* s.settimeout(timeout) method. Argument:
1454 None -- no timeout, blocking mode; same as setblocking(True)
1455 0.0 -- non-blocking mode; same as setblocking(False)
1456 > 0 -- timeout mode; operations time out after timeout seconds
1457 < 0 -- illegal; raises an exception
1458*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00001459static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001460sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001461{
Guido van Rossum11ba0942002-06-13 15:07:44 +00001462 double timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001463
1464 if (arg == Py_None)
Guido van Rossum11ba0942002-06-13 15:07:44 +00001465 timeout = -1.0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001466 else {
Guido van Rossum11ba0942002-06-13 15:07:44 +00001467 timeout = PyFloat_AsDouble(arg);
1468 if (timeout < 0.0) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00001469 if (!PyErr_Occurred())
1470 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001471 "Timeout value out of range");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001472 return NULL;
1473 }
1474 }
1475
Guido van Rossum11ba0942002-06-13 15:07:44 +00001476 s->sock_timeout = timeout;
1477 internal_setblocking(s, timeout < 0.0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001478
1479 Py_INCREF(Py_None);
1480 return Py_None;
1481}
1482
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001483PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001484"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001485\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001486Set a timeout on socket operations. 'timeout' can be a float,\n\
1487giving in seconds, or None. Setting a timeout of None disables\n\
1488the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001489Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001490
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001491/* s.gettimeout() method.
1492 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001493static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001494sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001495{
1496 if (s->sock_timeout < 0.0) {
1497 Py_INCREF(Py_None);
1498 return Py_None;
1499 }
1500 else
1501 return PyFloat_FromDouble(s->sock_timeout);
1502}
1503
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001504PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001505"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001506\n\
1507Returns the timeout in floating seconds associated with socket \n\
1508operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001509operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00001510
Guido van Rossum48a680c2001-03-02 06:34:14 +00001511#ifdef RISCOS
1512/* s.sleeptaskw(1 | 0) method */
1513
1514static PyObject *
Martin v. Löwisa94568a2003-05-10 07:36:56 +00001515sock_sleeptaskw(PySocketSockObject *s,PyObject *arg)
Guido van Rossum48a680c2001-03-02 06:34:14 +00001516{
Guido van Rossum67f7a382002-06-06 21:08:16 +00001517 int block;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00001518 block = PyInt_AsLong(arg);
1519 if (block == -1 && PyErr_Occurred())
Guido van Rossum67f7a382002-06-06 21:08:16 +00001520 return NULL;
1521 Py_BEGIN_ALLOW_THREADS
1522 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
1523 Py_END_ALLOW_THREADS
Guido van Rossum48a680c2001-03-02 06:34:14 +00001524
Guido van Rossum67f7a382002-06-06 21:08:16 +00001525 Py_INCREF(Py_None);
1526 return Py_None;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001527}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001528PyDoc_STRVAR(sleeptaskw_doc,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001529"sleeptaskw(flag)\n\
1530\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001531Allow sleeps in taskwindows.");
Guido van Rossum48a680c2001-03-02 06:34:14 +00001532#endif
1533
1534
Guido van Rossumaee08791992-09-08 09:05:33 +00001535/* s.setsockopt() method.
1536 With an integer third argument, sets an integer option.
1537 With a string third argument, sets an option from a buffer;
1538 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001539
Guido van Rossum73624e91994-10-10 17:59:00 +00001540static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001541sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001542{
1543 int level;
1544 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001545 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +00001546 char *buf;
1547 int buflen;
1548 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001549
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001550 if (PyArg_ParseTuple(args, "iii:setsockopt",
1551 &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001552 buf = (char *) &flag;
1553 buflen = sizeof flag;
1554 }
1555 else {
Guido van Rossum73624e91994-10-10 17:59:00 +00001556 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001557 if (!PyArg_ParseTuple(args, "iis#:setsockopt",
1558 &level, &optname, &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +00001559 return NULL;
1560 }
Thomas Wouters334fb892000-07-25 12:56:38 +00001561 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001562 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001563 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001564 Py_INCREF(Py_None);
1565 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001566}
1567
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001568PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001569"setsockopt(level, option, value)\n\
1570\n\
1571Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001572The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001573
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001574
Guido van Rossumaee08791992-09-08 09:05:33 +00001575/* s.getsockopt() method.
1576 With two arguments, retrieves an integer option.
1577 With a third integer argument, retrieves a string buffer of that size;
1578 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001579
Guido van Rossum73624e91994-10-10 17:59:00 +00001580static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001581sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001582{
1583 int level;
1584 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001585 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00001586 PyObject *buf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001587 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001588
Guido van Rossumbcc20741998-08-04 22:53:56 +00001589#ifdef __BEOS__
Fred Drake564a6cc2001-05-11 20:12:26 +00001590 /* We have incomplete socket support. */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001591 PyErr_SetString(socket_error, "getsockopt not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00001592 return NULL;
1593#else
1594
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001595 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
1596 &level, &optname, &buflen))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001597 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001598
Guido van Rossumbe32c891996-06-20 16:25:29 +00001599 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001600 int flag = 0;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001601 socklen_t flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001602 res = getsockopt(s->sock_fd, level, optname,
Thomas Wouters334fb892000-07-25 12:56:38 +00001603 (void *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +00001604 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001605 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001606 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +00001607 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001608#ifdef __VMS
1609 if (buflen > 1024) {
1610#else
Guido van Rossumaee08791992-09-08 09:05:33 +00001611 if (buflen <= 0 || buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001612#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001613 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +00001614 "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +00001615 return NULL;
1616 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001617 buf = PyString_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001618 if (buf == NULL)
1619 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001620 res = getsockopt(s->sock_fd, level, optname,
Fred Drake564a6cc2001-05-11 20:12:26 +00001621 (void *)PyString_AS_STRING(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001622 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001623 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001624 return s->errorhandler();
Guido van Rossumaee08791992-09-08 09:05:33 +00001625 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001626 _PyString_Resize(&buf, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001627 return buf;
Guido van Rossumbcc20741998-08-04 22:53:56 +00001628#endif /* __BEOS__ */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001629}
1630
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001631PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001632"getsockopt(level, option[, buffersize]) -> value\n\
1633\n\
1634Get a socket option. See the Unix manual for level and option.\n\
1635If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001636string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001637
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001638
Fred Drake728819a2000-07-01 03:40:12 +00001639/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001640
Guido van Rossum73624e91994-10-10 17:59:00 +00001641static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001642sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001643{
1644 struct sockaddr *addr;
1645 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001646 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001647
Fred Drake728819a2000-07-01 03:40:12 +00001648 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001649 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001650 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001651 res = bind(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001652 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001653 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001654 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001655 Py_INCREF(Py_None);
1656 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001657}
1658
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001659PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001660"bind(address)\n\
1661\n\
1662Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00001663pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001664sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001665
Guido van Rossum30a685f1991-06-27 15:51:29 +00001666
1667/* s.close() method.
1668 Set the file descriptor to -1 so operations tried subsequently
1669 will surely fail. */
1670
Guido van Rossum73624e91994-10-10 17:59:00 +00001671static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001672sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001673{
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001674 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001675
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001676 if ((fd = s->sock_fd) != -1) {
1677 s->sock_fd = -1;
Guido van Rossum08481461996-10-12 14:07:22 +00001678 Py_BEGIN_ALLOW_THREADS
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001679 (void) SOCKETCLOSE(fd);
Guido van Rossum08481461996-10-12 14:07:22 +00001680 Py_END_ALLOW_THREADS
1681 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001682 Py_INCREF(Py_None);
1683 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001684}
1685
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001686PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001687"close()\n\
1688\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001689Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001690
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001691static int
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001692internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
1693 int *timeoutp)
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001694{
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001695 int res, timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001696
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001697 timeout = 0;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001698 res = connect(s->sock_fd, addr, addrlen);
1699
1700#ifdef MS_WINDOWS
1701
1702 if (s->sock_timeout > 0.0) {
1703 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001704 /* This is a mess. Best solution: trust select */
1705 fd_set fds;
Mark Hammonda57ec932004-08-03 05:06:26 +00001706 fd_set fds_exc;
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001707 struct timeval tv;
1708 tv.tv_sec = (int)s->sock_timeout;
1709 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
1710 FD_ZERO(&fds);
1711 FD_SET(s->sock_fd, &fds);
Mark Hammonda57ec932004-08-03 05:06:26 +00001712 FD_ZERO(&fds_exc);
1713 FD_SET(s->sock_fd, &fds_exc);
1714 res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001715 if (res == 0) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001716 res = WSAEWOULDBLOCK;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001717 timeout = 1;
Mark Hammonda57ec932004-08-03 05:06:26 +00001718 } else if (res > 0) {
1719 if (FD_ISSET(s->sock_fd, &fds))
1720 /* The socket is in the writeable set - this
1721 means connected */
1722 res = 0;
1723 else {
1724 /* As per MS docs, we need to call getsockopt()
1725 to get the underlying error */
1726 int res_size = sizeof res;
1727 /* It must be in the exception set */
1728 assert(FD_ISSET(s->sock_fd, &fds_exc));
1729 if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
1730 (char *)&res, &res_size))
1731 /* getsockopt also clears WSAGetLastError,
1732 so reset it back. */
1733 WSASetLastError(res);
1734 else
1735 res = WSAGetLastError();
1736 }
1737 }
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001738 /* else if (res < 0) an error occurred */
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001739 }
1740 }
1741
1742 if (res < 0)
1743 res = WSAGetLastError();
1744
1745#else
1746
1747 if (s->sock_timeout > 0.0) {
1748 if (res < 0 && errno == EINPROGRESS) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001749 timeout = internal_select(s, 1);
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001750 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum12e3c712002-08-08 20:39:30 +00001751 if (res < 0 && errno == EISCONN)
1752 res = 0;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001753 }
1754 }
1755
1756 if (res < 0)
1757 res = errno;
1758
1759#endif
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001760 *timeoutp = timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001761
1762 return res;
1763}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001764
Fred Drake728819a2000-07-01 03:40:12 +00001765/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001766
Guido van Rossum73624e91994-10-10 17:59:00 +00001767static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001768sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001769{
1770 struct sockaddr *addr;
1771 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001772 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001773 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001774
Fred Drake728819a2000-07-01 03:40:12 +00001775 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001776 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001777
Guido van Rossum73624e91994-10-10 17:59:00 +00001778 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001779 res = internal_connect(s, addr, addrlen, &timeout);
Guido van Rossum11ba0942002-06-13 15:07:44 +00001780 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001781
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001782 if (timeout) {
1783 PyErr_SetString(socket_timeout, "timed out");
1784 return NULL;
1785 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001786 if (res != 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001787 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001788 Py_INCREF(Py_None);
1789 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001790}
1791
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001792PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001793"connect(address)\n\
1794\n\
1795Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001796is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001797
Guido van Rossum30a685f1991-06-27 15:51:29 +00001798
Fred Drake728819a2000-07-01 03:40:12 +00001799/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001800
1801static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001802sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001803{
1804 struct sockaddr *addr;
1805 int addrlen;
1806 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001807 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001808
Fred Drake728819a2000-07-01 03:40:12 +00001809 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001810 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001811
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001812 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001813 res = internal_connect(s, addr, addrlen, &timeout);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001814 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001815
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001816 return PyInt_FromLong((long) res);
1817}
1818
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001819PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001820"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001821\n\
1822This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001823instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001824
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001825
Guido van Rossumed233a51992-06-23 09:07:03 +00001826/* s.fileno() method */
1827
Guido van Rossum73624e91994-10-10 17:59:00 +00001828static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001829sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00001830{
Fred Drakea04eaad2000-06-30 02:46:07 +00001831#if SIZEOF_SOCKET_T <= SIZEOF_LONG
Guido van Rossum73624e91994-10-10 17:59:00 +00001832 return PyInt_FromLong((long) s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00001833#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001834 return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00001835#endif
Guido van Rossumed233a51992-06-23 09:07:03 +00001836}
1837
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001838PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001839"fileno() -> integer\n\
1840\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001841Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001842
Guido van Rossumed233a51992-06-23 09:07:03 +00001843
Guido van Rossumbe32c891996-06-20 16:25:29 +00001844#ifndef NO_DUP
1845/* s.dup() method */
1846
1847static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001848sock_dup(PySocketSockObject *s)
Guido van Rossumbe32c891996-06-20 16:25:29 +00001849{
Fred Drakea04eaad2000-06-30 02:46:07 +00001850 SOCKET_T newfd;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001851 PyObject *sock;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001852
Guido van Rossumbe32c891996-06-20 16:25:29 +00001853 newfd = dup(s->sock_fd);
1854 if (newfd < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001855 return s->errorhandler();
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001856 sock = (PyObject *) new_sockobject(newfd,
1857 s->sock_family,
1858 s->sock_type,
1859 s->sock_proto);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001860 if (sock == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001861 SOCKETCLOSE(newfd);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001862 return sock;
1863}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001864
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001865PyDoc_STRVAR(dup_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001866"dup() -> socket object\n\
1867\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001868Return a new socket object connected to the same system resource.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001869
Guido van Rossumbe32c891996-06-20 16:25:29 +00001870#endif
1871
1872
Guido van Rossumc89705d1992-11-26 08:54:07 +00001873/* s.getsockname() method */
1874
Guido van Rossum73624e91994-10-10 17:59:00 +00001875static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001876sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001877{
1878 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001879 int res;
1880 socklen_t addrlen;
1881
Guido van Rossumc89705d1992-11-26 08:54:07 +00001882 if (!getsockaddrlen(s, &addrlen))
1883 return NULL;
Guido van Rossumca42b161996-01-12 01:36:05 +00001884 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001885 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001886 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001887 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001888 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001889 return s->errorhandler();
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001890 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen,
1891 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001892}
1893
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001894PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001895"getsockname() -> address info\n\
1896\n\
1897Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001898info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001899
Guido van Rossumc89705d1992-11-26 08:54:07 +00001900
Guido van Rossumb6775db1994-08-01 11:34:53 +00001901#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001902/* s.getpeername() method */
1903
Guido van Rossum73624e91994-10-10 17:59:00 +00001904static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001905sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001906{
1907 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001908 int res;
1909 socklen_t addrlen;
1910
Guido van Rossumc89705d1992-11-26 08:54:07 +00001911 if (!getsockaddrlen(s, &addrlen))
1912 return NULL;
Fred Drakefd168342001-05-09 19:11:33 +00001913 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001914 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001915 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001916 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001917 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001918 return s->errorhandler();
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001919 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen,
1920 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001921}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001922
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001923PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001924"getpeername() -> address info\n\
1925\n\
1926Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001927info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001928
Guido van Rossumb6775db1994-08-01 11:34:53 +00001929#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001930
1931
Guido van Rossum30a685f1991-06-27 15:51:29 +00001932/* s.listen(n) method */
1933
Guido van Rossum73624e91994-10-10 17:59:00 +00001934static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001935sock_listen(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001936{
1937 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001938 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001939
1940 backlog = PyInt_AsLong(arg);
1941 if (backlog == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001942 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001943 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001944 if (backlog < 1)
1945 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001946 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +00001947 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001948 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001949 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001950 Py_INCREF(Py_None);
1951 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001952}
1953
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001954PyDoc_STRVAR(listen_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001955"listen(backlog)\n\
1956\n\
1957Enable a server to accept connections. The backlog argument must be at\n\
1958least 1; it specifies the number of unaccepted connection that the system\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001959will allow before refusing new connections.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001960
1961
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001962#ifndef NO_DUP
Guido van Rossum30a685f1991-06-27 15:51:29 +00001963/* s.makefile(mode) method.
1964 Create a new open file object referring to a dupped version of
1965 the socket's file descriptor. (The dup() call is necessary so
1966 that the open file and socket objects may be closed independent
1967 of each other.)
1968 The mode argument specifies 'r' or 'w' passed to fdopen(). */
1969
Guido van Rossum73624e91994-10-10 17:59:00 +00001970static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001971sock_makefile(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001972{
Tim Petersdbd9ba62000-07-09 03:09:57 +00001973 extern int fclose(FILE *);
Guido van Rossum6b144911995-03-14 15:05:13 +00001974 char *mode = "r";
1975 int bufsize = -1;
Fred Drakea04eaad2000-06-30 02:46:07 +00001976#ifdef MS_WIN32
Tim Peters79248aa2001-08-29 21:37:10 +00001977 Py_intptr_t fd;
Fred Drakea04eaad2000-06-30 02:46:07 +00001978#else
Guido van Rossum30a685f1991-06-27 15:51:29 +00001979 int fd;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001980#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001981 FILE *fp;
Guido van Rossum6b144911995-03-14 15:05:13 +00001982 PyObject *f;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001983#ifdef __VMS
1984 char *mode_r = "r";
1985 char *mode_w = "w";
1986#endif
Guido van Rossum6b144911995-03-14 15:05:13 +00001987
Guido van Rossum43713e52000-02-29 13:59:29 +00001988 if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001989 return NULL;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001990#ifdef __VMS
1991 if (strcmp(mode,"rb") == 0) {
1992 mode = mode_r;
1993 }
1994 else {
1995 if (strcmp(mode,"wb") == 0) {
1996 mode = mode_w;
1997 }
1998 }
1999#endif
Guido van Rossum8d665e61996-06-26 18:22:49 +00002000#ifdef MS_WIN32
Guido van Rossum82a5c661998-07-07 20:45:43 +00002001 if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||
2002 ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00002003#else
Guido van Rossum82a5c661998-07-07 20:45:43 +00002004 if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00002005#endif
Guido van Rossum82a5c661998-07-07 20:45:43 +00002006 {
Guido van Rossum6b144911995-03-14 15:05:13 +00002007 if (fd >= 0)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00002008 SOCKETCLOSE(fd);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002009 return s->errorhandler();
Guido van Rossum6b144911995-03-14 15:05:13 +00002010 }
2011 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
2012 if (f != NULL)
2013 PyFile_SetBufSize(f, bufsize);
2014 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +00002015}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002016
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002017PyDoc_STRVAR(makefile_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002018"makefile([mode[, buffersize]]) -> file object\n\
2019\n\
2020Return a regular file object corresponding to the socket.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002021The mode and buffersize arguments are as for the built-in open() function.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002022
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002023#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002024
Guido van Rossum48a680c2001-03-02 06:34:14 +00002025
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002026/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002027
Guido van Rossum73624e91994-10-10 17:59:00 +00002028static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002029sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002030{
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002031 int len, n = 0, flags = 0, timeout;
Guido van Rossum73624e91994-10-10 17:59:00 +00002032 PyObject *buf;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002033#ifdef __VMS
2034 int read_length;
2035 char *read_buf;
2036#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002037
Guido van Rossum43713e52000-02-29 13:59:29 +00002038 if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002039 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002040
2041 if (len < 0) {
Martin v. Löwisfba64e12001-11-19 10:41:26 +00002042 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00002043 "negative buffersize in recv");
Martin v. Löwisfba64e12001-11-19 10:41:26 +00002044 return NULL;
2045 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002046
Guido van Rossum73624e91994-10-10 17:59:00 +00002047 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002048 if (buf == NULL)
2049 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002050
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002051#ifndef __VMS
Guido van Rossum73624e91994-10-10 17:59:00 +00002052 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002053 timeout = internal_select(s, 0);
2054 if (!timeout)
2055 n = recv(s->sock_fd, PyString_AS_STRING(buf), len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00002056 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002057
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002058 if (timeout) {
2059 Py_DECREF(buf);
2060 PyErr_SetString(socket_timeout, "timed out");
2061 return NULL;
2062 }
Guido van Rossum7c53b771995-09-13 18:39:47 +00002063 if (n < 0) {
2064 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002065 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00002066 }
Tim Peters5de98422002-04-27 18:44:32 +00002067 if (n != len)
2068 _PyString_Resize(&buf, n);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002069#else
2070 read_buf = PyString_AsString(buf);
2071 read_length = len;
2072 while (read_length != 0) {
2073 unsigned int segment;
2074
2075 segment = read_length /SEGMENT_SIZE;
2076 if (segment != 0) {
2077 segment = SEGMENT_SIZE;
2078 }
2079 else {
2080 segment = read_length;
2081 }
2082
2083 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002084 timeout = internal_select(s, 0);
2085 if (!timeout)
2086 n = recv(s->sock_fd, read_buf, segment, flags);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002087 Py_END_ALLOW_THREADS
2088
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002089 if (timeout) {
2090 Py_DECREF(buf);
2091 PyErr_SetString(socket_timeout, "timed out");
2092 return NULL;
2093 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002094 if (n < 0) {
2095 Py_DECREF(buf);
2096 return s->errorhandler();
2097 }
2098 if (n != read_length) {
2099 read_buf += n;
2100 break;
2101 }
2102
2103 read_length -= segment;
2104 read_buf += segment;
2105 }
2106 if (_PyString_Resize(&buf, (read_buf - PyString_AsString(buf))) < 0)
2107 {
2108 return NULL;
2109 }
2110#endif /* !__VMS */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002111 return buf;
2112}
2113
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002114PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002115"recv(buffersize[, flags]) -> data\n\
2116\n\
2117Receive up to buffersize bytes from the socket. For the optional flags\n\
2118argument, see the Unix manual. When no data is available, block until\n\
2119at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002120the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002121
Guido van Rossum30a685f1991-06-27 15:51:29 +00002122
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002123/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002124
Guido van Rossum73624e91994-10-10 17:59:00 +00002125static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002126sock_recvfrom(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002127{
2128 char addrbuf[256];
Barry Warsaw752300b1997-01-03 17:18:10 +00002129 PyObject *buf = NULL;
2130 PyObject *addr = NULL;
2131 PyObject *ret = NULL;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002132 int len, n = 0, flags = 0, timeout;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002133 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002134
Guido van Rossum43713e52000-02-29 13:59:29 +00002135 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002136 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002137
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00002138 if (!getsockaddrlen(s, &addrlen))
2139 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002140 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00002141 if (buf == NULL)
2142 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002143
Guido van Rossum73624e91994-10-10 17:59:00 +00002144 Py_BEGIN_ALLOW_THREADS
Fred Drakefd168342001-05-09 19:11:33 +00002145 memset(addrbuf, 0, addrlen);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002146 timeout = internal_select(s, 0);
2147 if (!timeout)
2148 n = recvfrom(s->sock_fd, PyString_AS_STRING(buf), len, flags,
Guido van Rossum8d665e61996-06-26 18:22:49 +00002149#ifndef MS_WINDOWS
Andrew MacIntyreba43e872002-03-03 03:03:52 +00002150#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002151 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00002152#else
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002153 (void *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00002154#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002155#else
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002156 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002157#endif
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002158 );
Guido van Rossum73624e91994-10-10 17:59:00 +00002159 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002160
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002161 if (timeout) {
2162 Py_DECREF(buf);
2163 PyErr_SetString(socket_timeout, "timed out");
2164 return NULL;
2165 }
Guido van Rossum7c53b771995-09-13 18:39:47 +00002166 if (n < 0) {
2167 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002168 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00002169 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002170
Guido van Rossum53a9bf81996-06-11 18:35:24 +00002171 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002172 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002173
Guido van Rossum67f7a382002-06-06 21:08:16 +00002174 if (!(addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00002175 addrlen, s->sock_proto)))
Barry Warsaw752300b1997-01-03 17:18:10 +00002176 goto finally;
2177
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002178 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002179
2180finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00002181 Py_XDECREF(addr);
2182 Py_XDECREF(buf);
Guido van Rossum6f5afc91993-02-05 09:46:15 +00002183 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002184}
2185
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002186PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002187"recvfrom(buffersize[, flags]) -> (data, address info)\n\
2188\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002189Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002190
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002191/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002192
Guido van Rossum73624e91994-10-10 17:59:00 +00002193static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002194sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002195{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002196 char *buf;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002197 int len, n = 0, flags = 0, timeout;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002198#ifdef __VMS
2199 int send_length;
2200#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002201
Guido van Rossum43713e52000-02-29 13:59:29 +00002202 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002203 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002204
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002205#ifndef __VMS
Guido van Rossum73624e91994-10-10 17:59:00 +00002206 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002207 timeout = internal_select(s, 1);
2208 if (!timeout)
2209 n = send(s->sock_fd, buf, len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00002210 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002211
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002212 if (timeout) {
2213 PyErr_SetString(socket_timeout, "timed out");
2214 return NULL;
2215 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00002216 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002217 return s->errorhandler();
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002218#else
2219 /* Divide packet into smaller segments for */
2220 /* TCP/IP Services for OpenVMS */
2221 send_length = len;
2222 while (send_length != 0) {
2223 unsigned int segment;
2224
2225 segment = send_length / SEGMENT_SIZE;
2226 if (segment != 0) {
2227 segment = SEGMENT_SIZE;
2228 }
2229 else {
2230 segment = send_length;
2231 }
2232 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002233 timeout = internal_select(s, 1);
2234 if (!timeout)
2235 n = send(s->sock_fd, buf, segment, flags);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002236 Py_END_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002237 if (timeout) {
2238 PyErr_SetString(socket_timeout, "timed out");
2239 return NULL;
2240 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002241 if (n < 0) {
2242 return s->errorhandler();
2243 }
2244 send_length -= segment;
2245 buf += segment;
2246 } /* end while */
2247#endif /* !__VMS */
Guido van Rossum73624e91994-10-10 17:59:00 +00002248 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00002249}
2250
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002251PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002252"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002253\n\
2254Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002255argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002256sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002257
2258
2259/* s.sendall(data [,flags]) method */
2260
2261static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002262sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002263{
2264 char *buf;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002265 int len, n = 0, flags = 0, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002266
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002267 if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags))
2268 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002269
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002270 Py_BEGIN_ALLOW_THREADS
2271 do {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002272 timeout = internal_select(s, 1);
2273 if (timeout)
2274 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002275 n = send(s->sock_fd, buf, len, flags);
2276 if (n < 0)
2277 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002278 buf += n;
2279 len -= n;
2280 } while (len > 0);
2281 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002282
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002283 if (timeout) {
2284 PyErr_SetString(socket_timeout, "timed out");
2285 return NULL;
2286 }
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002287 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002288 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00002289
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002290 Py_INCREF(Py_None);
2291 return Py_None;
2292}
2293
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002294PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002295"sendall(data[, flags])\n\
2296\n\
2297Send a data string to the socket. For the optional flags\n\
2298argument, see the Unix manual. This calls send() repeatedly\n\
2299until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002300to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002301
Guido van Rossum30a685f1991-06-27 15:51:29 +00002302
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002303/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002304
Guido van Rossum73624e91994-10-10 17:59:00 +00002305static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002306sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002307{
Guido van Rossum73624e91994-10-10 17:59:00 +00002308 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002309 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002310 struct sockaddr *addr;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002311 int addrlen, len, n = 0, flags, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002312
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002313 flags = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002314 if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00002315 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002316 if (!PyArg_ParseTuple(args, "s#iO:sendto",
2317 &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002318 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002319 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002320
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002321 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002322 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002323
Guido van Rossum73624e91994-10-10 17:59:00 +00002324 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002325 timeout = internal_select(s, 1);
2326 if (!timeout)
2327 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002328 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002329
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002330 if (timeout) {
2331 PyErr_SetString(socket_timeout, "timed out");
2332 return NULL;
2333 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002334 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002335 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002336 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002337}
2338
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002339PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002340"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002341\n\
2342Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002343For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002344
Guido van Rossum30a685f1991-06-27 15:51:29 +00002345
2346/* s.shutdown(how) method */
2347
Guido van Rossum73624e91994-10-10 17:59:00 +00002348static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002349sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002350{
2351 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002352 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002353
2354 how = PyInt_AsLong(arg);
2355 if (how == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002356 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002357 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002358 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00002359 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002360 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002361 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002362 Py_INCREF(Py_None);
2363 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002364}
2365
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002366PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002367"shutdown(flag)\n\
2368\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00002369Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
2370of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002371
Guido van Rossum30a685f1991-06-27 15:51:29 +00002372
2373/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002374
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002375static PyMethodDef sock_methods[] = {
2376 {"accept", (PyCFunction)sock_accept, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002377 accept_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002378 {"bind", (PyCFunction)sock_bind, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002379 bind_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002380 {"close", (PyCFunction)sock_close, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002381 close_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002382 {"connect", (PyCFunction)sock_connect, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002383 connect_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002384 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002385 connect_ex_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002386#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002387 {"dup", (PyCFunction)sock_dup, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002388 dup_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002389#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002390 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002391 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002392#ifdef HAVE_GETPEERNAME
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002393 {"getpeername", (PyCFunction)sock_getpeername,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002394 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00002395#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002396 {"getsockname", (PyCFunction)sock_getsockname,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002397 METH_NOARGS, getsockname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002398 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002399 getsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002400 {"listen", (PyCFunction)sock_listen, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002401 listen_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002402#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002403 {"makefile", (PyCFunction)sock_makefile, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002404 makefile_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002405#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002406 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002407 recv_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002408 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002409 recvfrom_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002410 {"send", (PyCFunction)sock_send, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002411 send_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002412 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002413 sendall_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002414 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002415 sendto_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002416 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002417 setblocking_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002418 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
Guido van Rossum67f7a382002-06-06 21:08:16 +00002419 settimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002420 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
Guido van Rossum67f7a382002-06-06 21:08:16 +00002421 gettimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002422 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002423 setsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002424 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002425 shutdown_doc},
2426#ifdef RISCOS
Martin v. Löwisa94568a2003-05-10 07:36:56 +00002427 {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002428 sleeptaskw_doc},
2429#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00002430 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002431};
2432
Guido van Rossum30a685f1991-06-27 15:51:29 +00002433
Guido van Rossum73624e91994-10-10 17:59:00 +00002434/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00002435 First close the file description. */
2436
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002437static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002438sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002439{
Guido van Rossumfa972c92000-04-10 12:45:45 +00002440 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00002441 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002442 s->ob_type->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002443}
2444
Guido van Rossum30a685f1991-06-27 15:51:29 +00002445
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002446static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002447sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002448{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002449 char buf[512];
Fred Drakea04eaad2000-06-30 02:46:07 +00002450#if SIZEOF_SOCKET_T > SIZEOF_LONG
2451 if (s->sock_fd > LONG_MAX) {
2452 /* this can occur on Win64, and actually there is a special
2453 ugly printf formatter for decimal pointer length integer
2454 printing, only bother if necessary*/
2455 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002456 "no printf formatter to display "
2457 "the socket descriptor in decimal");
Fred Drakea04eaad2000-06-30 02:46:07 +00002458 return NULL;
2459 }
2460#endif
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002461 PyOS_snprintf(
2462 buf, sizeof(buf),
2463 "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",
2464 (long)s->sock_fd, s->sock_family,
2465 s->sock_type,
2466 s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00002467 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002468}
2469
2470
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002471/* Create a new, uninitialized socket object. */
2472
2473static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002474sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002475{
2476 PyObject *new;
2477
2478 new = type->tp_alloc(type, 0);
Guido van Rossume1c478f2002-06-06 20:08:25 +00002479 if (new != NULL) {
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002480 ((PySocketSockObject *)new)->sock_fd = -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002481 ((PySocketSockObject *)new)->sock_timeout = -1.0;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002482 ((PySocketSockObject *)new)->errorhandler = &set_error;
Guido van Rossume1c478f2002-06-06 20:08:25 +00002483 }
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002484 return new;
2485}
2486
2487
2488/* Initialize a new socket object. */
2489
2490/*ARGSUSED*/
2491static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002492sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002493{
2494 PySocketSockObject *s = (PySocketSockObject *)self;
2495 SOCKET_T fd;
2496 int family = AF_INET, type = SOCK_STREAM, proto = 0;
2497 static char *keywords[] = {"family", "type", "proto", 0};
2498
2499 if (!PyArg_ParseTupleAndKeywords(args, kwds,
2500 "|iii:socket", keywords,
2501 &family, &type, &proto))
2502 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002503
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002504 Py_BEGIN_ALLOW_THREADS
2505 fd = socket(family, type, proto);
2506 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002507
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002508#ifdef MS_WINDOWS
2509 if (fd == INVALID_SOCKET)
2510#else
2511 if (fd < 0)
2512#endif
2513 {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002514 set_error();
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002515 return -1;
2516 }
2517 init_sockobject(s, fd, family, type, proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002518
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002519 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002520
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002521}
2522
2523
Guido van Rossumb6775db1994-08-01 11:34:53 +00002524/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002525
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002526static PyTypeObject sock_type = {
Guido van Rossum55558541996-05-23 22:54:50 +00002527 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002528 0, /* ob_size */
Guido van Rossumca6dfa52001-10-28 12:31:33 +00002529 "_socket.socket", /* tp_name */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002530 sizeof(PySocketSockObject), /* tp_basicsize */
2531 0, /* tp_itemsize */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002532 (destructor)sock_dealloc, /* tp_dealloc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002533 0, /* tp_print */
2534 0, /* tp_getattr */
2535 0, /* tp_setattr */
2536 0, /* tp_compare */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002537 (reprfunc)sock_repr, /* tp_repr */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002538 0, /* tp_as_number */
2539 0, /* tp_as_sequence */
2540 0, /* tp_as_mapping */
2541 0, /* tp_hash */
2542 0, /* tp_call */
2543 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002544 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002545 0, /* tp_setattro */
2546 0, /* tp_as_buffer */
2547 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002548 sock_doc, /* tp_doc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002549 0, /* tp_traverse */
2550 0, /* tp_clear */
2551 0, /* tp_richcompare */
2552 0, /* tp_weaklistoffset */
2553 0, /* tp_iter */
2554 0, /* tp_iternext */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002555 sock_methods, /* tp_methods */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002556 0, /* tp_members */
2557 0, /* tp_getset */
2558 0, /* tp_base */
2559 0, /* tp_dict */
2560 0, /* tp_descr_get */
2561 0, /* tp_descr_set */
2562 0, /* tp_dictoffset */
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002563 sock_initobj, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002564 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002565 sock_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002566 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002567};
2568
Guido van Rossum30a685f1991-06-27 15:51:29 +00002569
Guido van Rossum81194471991-07-27 21:42:02 +00002570/* Python interface to gethostname(). */
2571
2572/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002573static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002574socket_gethostname(PyObject *self, PyObject *args)
Guido van Rossum81194471991-07-27 21:42:02 +00002575{
2576 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00002577 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002578 if (!PyArg_ParseTuple(args, ":gethostname"))
Guido van Rossum81194471991-07-27 21:42:02 +00002579 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002580 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002581 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00002582 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002583 if (res < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002584 return set_error();
Guido van Rossum81194471991-07-27 21:42:02 +00002585 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00002586 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00002587}
Guido van Rossumff4949e1992-08-05 19:58:53 +00002588
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002589PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002590"gethostname() -> string\n\
2591\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002592Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002593
Guido van Rossumff4949e1992-08-05 19:58:53 +00002594
Guido van Rossum30a685f1991-06-27 15:51:29 +00002595/* Python interface to gethostbyname(name). */
2596
2597/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002598static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002599socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002600{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002601 char *name;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002602#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002603 struct sockaddr_storage addrbuf;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002604#else
2605 struct sockaddr_in addrbuf;
2606#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002607
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002608 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002609 return NULL;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002610 if (setipaddr(name, (struct sockaddr *)&addrbuf, sizeof(addrbuf), AF_INET) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002611 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002612 return makeipaddr((struct sockaddr *)&addrbuf,
2613 sizeof(struct sockaddr_in));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002614}
2615
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002616PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002617"gethostbyname(host) -> address\n\
2618\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002619Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002620
2621
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002622/* Convenience function common to gethostbyname_ex and gethostbyaddr */
2623
2624static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002625gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002626{
2627 char **pch;
2628 PyObject *rtn_tuple = (PyObject *)NULL;
2629 PyObject *name_list = (PyObject *)NULL;
2630 PyObject *addr_list = (PyObject *)NULL;
2631 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002632
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002633 if (h == NULL) {
Guido van Rossum48a680c2001-03-02 06:34:14 +00002634 /* Let's get real error message to return */
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002635#ifndef RISCOS
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002636 set_herror(h_errno);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002637#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002638 PyErr_SetString(socket_error, "host not found");
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002639#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002640 return NULL;
2641 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002642
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002643 if (h->h_addrtype != af) {
2644#ifdef HAVE_STRERROR
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002645 /* Let's get real error message to return */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002646 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002647 (char *)strerror(EAFNOSUPPORT));
2648#else
2649 PyErr_SetString(
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002650 socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002651 "Address family not supported by protocol family");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002652#endif
2653 return NULL;
2654 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002655
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002656 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002657
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002658 case AF_INET:
2659 if (alen < sizeof(struct sockaddr_in))
2660 return NULL;
2661 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002662
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002663#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002664 case AF_INET6:
2665 if (alen < sizeof(struct sockaddr_in6))
2666 return NULL;
2667 break;
2668#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002669
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002670 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002671
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002672 if ((name_list = PyList_New(0)) == NULL)
2673 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002674
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002675 if ((addr_list = PyList_New(0)) == NULL)
2676 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002677
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002678 for (pch = h->h_aliases; *pch != NULL; pch++) {
2679 int status;
2680 tmp = PyString_FromString(*pch);
2681 if (tmp == NULL)
2682 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002683
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002684 status = PyList_Append(name_list, tmp);
2685 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002686
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002687 if (status)
2688 goto err;
2689 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002690
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002691 for (pch = h->h_addr_list; *pch != NULL; pch++) {
2692 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002693
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002694 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002695
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002696 case AF_INET:
2697 {
2698 struct sockaddr_in sin;
2699 memset(&sin, 0, sizeof(sin));
2700 sin.sin_family = af;
2701#ifdef HAVE_SOCKADDR_SA_LEN
2702 sin.sin_len = sizeof(sin);
2703#endif
2704 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
2705 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002706
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002707 if (pch == h->h_addr_list && alen >= sizeof(sin))
2708 memcpy((char *) addr, &sin, sizeof(sin));
2709 break;
2710 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002711
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002712#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002713 case AF_INET6:
2714 {
2715 struct sockaddr_in6 sin6;
2716 memset(&sin6, 0, sizeof(sin6));
2717 sin6.sin6_family = af;
2718#ifdef HAVE_SOCKADDR_SA_LEN
2719 sin6.sin6_len = sizeof(sin6);
2720#endif
2721 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
2722 tmp = makeipaddr((struct sockaddr *)&sin6,
2723 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002724
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002725 if (pch == h->h_addr_list && alen >= sizeof(sin6))
2726 memcpy((char *) addr, &sin6, sizeof(sin6));
2727 break;
2728 }
2729#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002730
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002731 default: /* can't happen */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002732 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002733 "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002734 return NULL;
2735 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002736
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002737 if (tmp == NULL)
2738 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002739
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002740 status = PyList_Append(addr_list, tmp);
2741 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002742
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002743 if (status)
2744 goto err;
2745 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002746
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002747 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002748
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002749 err:
2750 Py_XDECREF(name_list);
2751 Py_XDECREF(addr_list);
2752 return rtn_tuple;
2753}
2754
2755
2756/* Python interface to gethostbyname_ex(name). */
2757
2758/*ARGSUSED*/
2759static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002760socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002761{
2762 char *name;
2763 struct hostent *h;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002764#ifdef ENABLE_IPV6
2765 struct sockaddr_storage addr;
2766#else
2767 struct sockaddr_in addr;
2768#endif
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002769 struct sockaddr *sa;
Guido van Rossum955becc1999-03-22 20:14:53 +00002770 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002771#ifdef HAVE_GETHOSTBYNAME_R
2772 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002773#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2774 struct hostent_data data;
2775#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002776 char buf[16384];
2777 int buf_len = (sizeof buf) - 1;
2778 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002779#endif
2780#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002781 int result;
2782#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002783#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002784
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002785 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002786 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00002787 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002788 return NULL;
2789 Py_BEGIN_ALLOW_THREADS
2790#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002791#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002792 result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
2793 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002794#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002795 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002796#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002797 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002798 result = gethostbyname_r(name, &hp_allocated, &data);
2799 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002800#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002801#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002802#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002803 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002804#endif
2805 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002806#endif /* HAVE_GETHOSTBYNAME_R */
2807 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002808 /* Some C libraries would require addr.__ss_family instead of
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002809 addr.ss_family.
2810 Therefore, we cast the sockaddr_storage into sockaddr to
2811 access sa_family. */
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002812 sa = (struct sockaddr*)&addr;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002813 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002814 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002815#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002816 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00002817#endif
2818 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002819}
2820
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002821PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002822"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
2823\n\
2824Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002825for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002826
2827
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002828/* Python interface to gethostbyaddr(IP). */
2829
2830/*ARGSUSED*/
2831static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002832socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002833{
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002834#ifdef ENABLE_IPV6
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002835 struct sockaddr_storage addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002836#else
Guido van Rossum48a680c2001-03-02 06:34:14 +00002837 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002838#endif
2839 struct sockaddr *sa = (struct sockaddr *)&addr;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002840 char *ip_num;
2841 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00002842 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002843#ifdef HAVE_GETHOSTBYNAME_R
2844 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002845#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2846 struct hostent_data data;
2847#else
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002848 char buf[16384];
2849 int buf_len = (sizeof buf) - 1;
2850 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002851#endif
2852#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002853 int result;
2854#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002855#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002856 char *ap;
2857 int al;
2858 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002859
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002860 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002861 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00002862 af = AF_UNSPEC;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002863 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002864 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002865 af = sa->sa_family;
2866 ap = NULL;
2867 al = 0;
2868 switch (af) {
2869 case AF_INET:
2870 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
2871 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
2872 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002873#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002874 case AF_INET6:
2875 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
2876 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
2877 break;
2878#endif
2879 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002880 PyErr_SetString(socket_error, "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002881 return NULL;
2882 }
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002883 Py_BEGIN_ALLOW_THREADS
2884#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002885#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002886 result = gethostbyaddr_r(ap, al, af,
2887 &hp_allocated, buf, buf_len,
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002888 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002889#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002890 h = gethostbyaddr_r(ap, al, af,
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002891 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002892#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002893 memset((void *) &data, '\0', sizeof(data));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002894 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002895 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002896#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002897#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002898#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002899 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002900#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002901 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002902#endif /* HAVE_GETHOSTBYNAME_R */
2903 Py_END_ALLOW_THREADS
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002904 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002905#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002906 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002907#endif
2908 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002909}
2910
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002911PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002912"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
2913\n\
2914Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002915for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002916
Guido van Rossum30a685f1991-06-27 15:51:29 +00002917
2918/* Python interface to getservbyname(name).
2919 This only returns the port number, since the other info is already
2920 known or not useful (like the list of aliases). */
2921
2922/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002923static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002924socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002925{
Barry Warsaw11b91a02004-06-28 00:50:43 +00002926 char *name, *proto=NULL;
Guido van Rossum30a685f1991-06-27 15:51:29 +00002927 struct servent *sp;
Barry Warsaw11b91a02004-06-28 00:50:43 +00002928 if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002929 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002930 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002931 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00002932 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00002933 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002934 PyErr_SetString(socket_error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00002935 return NULL;
2936 }
Guido van Rossum73624e91994-10-10 17:59:00 +00002937 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002938}
2939
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002940PyDoc_STRVAR(getservbyname_doc,
Barry Warsaw11b91a02004-06-28 00:50:43 +00002941"getservbyname(servicename[, protocolname]) -> integer\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002942\n\
2943Return a port number from a service name and protocol name.\n\
Barry Warsaw11b91a02004-06-28 00:50:43 +00002944The optional protocol name, if given, should be 'tcp' or 'udp',\n\
2945otherwise any protocol will match.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002946
Guido van Rossum30a685f1991-06-27 15:51:29 +00002947
Barry Warsaw11b91a02004-06-28 00:50:43 +00002948/* Python interface to getservbyport(port).
2949 This only returns the service name, since the other info is already
2950 known or not useful (like the list of aliases). */
2951
2952/*ARGSUSED*/
2953static PyObject *
2954socket_getservbyport(PyObject *self, PyObject *args)
2955{
2956 int port;
2957 char *proto=NULL;
2958 struct servent *sp;
2959 if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto))
2960 return NULL;
2961 Py_BEGIN_ALLOW_THREADS
2962 sp = getservbyport(htons(port), proto);
2963 Py_END_ALLOW_THREADS
2964 if (sp == NULL) {
2965 PyErr_SetString(socket_error, "port/proto not found");
2966 return NULL;
2967 }
2968 return PyString_FromString(sp->s_name);
2969}
2970
2971PyDoc_STRVAR(getservbyport_doc,
2972"getservbyport(port[, protocolname]) -> string\n\
2973\n\
2974Return the service name from a port number and protocol name.\n\
2975The optional protocol name, if given, should be 'tcp' or 'udp',\n\
2976otherwise any protocol will match.");
2977
Guido van Rossum3901d851996-12-19 16:35:04 +00002978/* Python interface to getprotobyname(name).
2979 This only returns the protocol number, since the other info is
2980 already known or not useful (like the list of aliases). */
2981
2982/*ARGSUSED*/
2983static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002984socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00002985{
2986 char *name;
2987 struct protoent *sp;
Guido van Rossumbcc20741998-08-04 22:53:56 +00002988#ifdef __BEOS__
2989/* Not available in BeOS yet. - [cjh] */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002990 PyErr_SetString(socket_error, "getprotobyname not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00002991 return NULL;
2992#else
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002993 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00002994 return NULL;
2995 Py_BEGIN_ALLOW_THREADS
2996 sp = getprotobyname(name);
2997 Py_END_ALLOW_THREADS
2998 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002999 PyErr_SetString(socket_error, "protocol not found");
Guido van Rossum3901d851996-12-19 16:35:04 +00003000 return NULL;
3001 }
3002 return PyInt_FromLong((long) sp->p_proto);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003003#endif
Guido van Rossum3901d851996-12-19 16:35:04 +00003004}
3005
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003006PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003007"getprotobyname(name) -> integer\n\
3008\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003009Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003010
Guido van Rossum3901d851996-12-19 16:35:04 +00003011
Dave Cole331708b2004-08-09 04:51:41 +00003012#ifdef HAVE_SOCKETPAIR
3013/* Create a pair of sockets using the socketpair() function.
Dave Cole07fda7e2004-08-23 05:16:23 +00003014 Arguments as for socket() except the default family is AF_UNIX if
3015 defined for the platform, otherwise the default is AF_INET. */
Dave Cole331708b2004-08-09 04:51:41 +00003016
3017/*ARGSUSED*/
3018static PyObject *
3019socket_socketpair(PyObject *self, PyObject *args)
3020{
3021 PySocketSockObject *s0 = NULL, *s1 = NULL;
3022 SOCKET_T sv[2];
3023 int family, type = SOCK_STREAM, proto = 0;
3024 PyObject *res = NULL;
3025
3026#if defined(AF_UNIX)
3027 family = AF_UNIX;
3028#else
3029 family = AF_INET;
3030#endif
3031 if (!PyArg_ParseTuple(args, "|iii:socketpair",
3032 &family, &type, &proto))
3033 return NULL;
3034 /* Create a pair of socket fds */
3035 if (socketpair(family, type, proto, sv) < 0)
3036 return set_error();
Dave Cole331708b2004-08-09 04:51:41 +00003037 s0 = new_sockobject(sv[0], family, type, proto);
3038 if (s0 == NULL)
3039 goto finally;
3040 s1 = new_sockobject(sv[1], family, type, proto);
3041 if (s1 == NULL)
3042 goto finally;
3043 res = PyTuple_Pack(2, s0, s1);
3044
3045finally:
3046 if (res == NULL) {
3047 if (s0 == NULL)
3048 SOCKETCLOSE(sv[0]);
3049 if (s1 == NULL)
3050 SOCKETCLOSE(sv[1]);
3051 }
3052 Py_XDECREF(s0);
3053 Py_XDECREF(s1);
3054 return res;
3055}
3056
3057PyDoc_STRVAR(socketpair_doc,
3058"socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\
3059\n\
3060Create a pair of socket objects from the sockets returned by the platform\n\
3061socketpair() function.\n\
Dave Cole07fda7e2004-08-23 05:16:23 +00003062The arguments are the same as for socket() except the default family is\n\
3063AF_UNIX if defined for the platform, otherwise the default is AF_INET.");
Dave Cole331708b2004-08-09 04:51:41 +00003064
3065#endif /* HAVE_SOCKETPAIR */
3066
3067
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003068#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00003069/* Create a socket object from a numeric file description.
3070 Useful e.g. if stdin is a socket.
3071 Additional arguments as for socket(). */
3072
3073/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00003074static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003075socket_fromfd(PyObject *self, PyObject *args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00003076{
Guido van Rossum73624e91994-10-10 17:59:00 +00003077 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00003078 SOCKET_T fd;
3079 int family, type, proto = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003080 if (!PyArg_ParseTuple(args, "iii|i:fromfd",
3081 &fd, &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00003082 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00003083 /* Dup the fd so it and the socket can be closed independently */
3084 fd = dup(fd);
3085 if (fd < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003086 return set_error();
3087 s = new_sockobject(fd, family, type, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00003088 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00003089}
Guido van Rossum82a5c661998-07-07 20:45:43 +00003090
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003091PyDoc_STRVAR(fromfd_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003092"fromfd(fd, family, type[, proto]) -> socket object\n\
3093\n\
3094Create a socket object from the given file descriptor.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003095The remaining arguments are the same as for socket().");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003096
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003097#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00003098
Guido van Rossum82a5c661998-07-07 20:45:43 +00003099
Guido van Rossum006bf911996-06-12 04:04:55 +00003100static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003101socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003102{
3103 int x1, x2;
3104
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003105 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00003106 return NULL;
3107 }
3108 x2 = (int)ntohs((short)x1);
3109 return PyInt_FromLong(x2);
3110}
3111
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003112PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003113"ntohs(integer) -> integer\n\
3114\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003115Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003116
3117
Guido van Rossum006bf911996-06-12 04:04:55 +00003118static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003119socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003120{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003121 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003122
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003123 if (PyInt_Check(arg)) {
3124 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003125 if (x == (unsigned long) -1 && PyErr_Occurred())
3126 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00003127 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003128 else if (PyLong_Check(arg)) {
3129 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003130 if (x == (unsigned long) -1 && PyErr_Occurred())
3131 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003132#if SIZEOF_LONG > 4
3133 {
3134 unsigned long y;
3135 /* only want the trailing 32 bits */
3136 y = x & 0xFFFFFFFFUL;
3137 if (y ^ x)
3138 return PyErr_Format(PyExc_OverflowError,
3139 "long int larger than 32 bits");
3140 x = y;
Tim Peters58141872002-08-06 22:25:02 +00003141 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003142#endif
3143 }
3144 else
Tim Peters58141872002-08-06 22:25:02 +00003145 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003146 "expected int/long, %s found",
3147 arg->ob_type->tp_name);
3148 if (x == (unsigned long) -1 && PyErr_Occurred())
3149 return NULL;
3150 return PyInt_FromLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003151}
3152
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003153PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003154"ntohl(integer) -> integer\n\
3155\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003156Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003157
3158
Guido van Rossum006bf911996-06-12 04:04:55 +00003159static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003160socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003161{
Hye-Shik Changa7b673f2003-12-17 09:40:23 +00003162 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00003163
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003164 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00003165 return NULL;
3166 }
3167 x2 = (int)htons((short)x1);
3168 return PyInt_FromLong(x2);
3169}
3170
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003171PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003172"htons(integer) -> integer\n\
3173\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003174Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003175
3176
Guido van Rossum006bf911996-06-12 04:04:55 +00003177static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003178socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003179{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003180 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003181
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003182 if (PyInt_Check(arg)) {
3183 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003184 if (x == (unsigned long) -1 && PyErr_Occurred())
3185 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00003186 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003187 else if (PyLong_Check(arg)) {
3188 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003189 if (x == (unsigned long) -1 && PyErr_Occurred())
3190 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003191#if SIZEOF_LONG > 4
3192 {
3193 unsigned long y;
3194 /* only want the trailing 32 bits */
3195 y = x & 0xFFFFFFFFUL;
3196 if (y ^ x)
3197 return PyErr_Format(PyExc_OverflowError,
3198 "long int larger than 32 bits");
3199 x = y;
Tim Peters58141872002-08-06 22:25:02 +00003200 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003201#endif
3202 }
3203 else
Tim Peters58141872002-08-06 22:25:02 +00003204 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003205 "expected int/long, %s found",
3206 arg->ob_type->tp_name);
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003207 return PyInt_FromLong(htonl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003208}
3209
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003210PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003211"htonl(integer) -> integer\n\
3212\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003213Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003214
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003215/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003216
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003217PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003218"inet_aton(string) -> packed 32-bit IP representation\n\
3219\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003220Convert 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 +00003221binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003222
3223static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003224socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003225{
Guido van Rossuma2e48551999-09-09 15:42:59 +00003226#ifndef INADDR_NONE
3227#define INADDR_NONE (-1)
3228#endif
Neal Norwitz88f115b2003-02-13 02:15:42 +00003229#ifdef HAVE_INET_ATON
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003230 struct in_addr buf;
Tim Peters1df9fdd2003-02-13 03:13:40 +00003231#else
3232 /* Have to use inet_addr() instead */
3233 unsigned long packed_addr;
3234#endif
3235 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003236
Tim Peters1df9fdd2003-02-13 03:13:40 +00003237 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003238 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003239
Tim Peters1df9fdd2003-02-13 03:13:40 +00003240
3241#ifdef HAVE_INET_ATON
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003242 if (inet_aton(ip_addr, &buf))
3243 return PyString_FromStringAndSize((char *)(&buf),
3244 sizeof(buf));
3245
3246 PyErr_SetString(socket_error,
3247 "illegal IP address string passed to inet_aton");
3248 return NULL;
3249
Tim Peters1df9fdd2003-02-13 03:13:40 +00003250#else /* ! HAVE_INET_ATON */
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003251 /* XXX Problem here: inet_aton('255.255.255.255') raises
3252 an exception while it should be a valid address. */
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003253 packed_addr = inet_addr(ip_addr);
3254
3255 if (packed_addr == INADDR_NONE) { /* invalid address */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003256 PyErr_SetString(socket_error,
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003257 "illegal IP address string passed to inet_aton");
3258 return NULL;
3259 }
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003260 return PyString_FromStringAndSize((char *) &packed_addr,
3261 sizeof(packed_addr));
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003262#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003263}
3264
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003265PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00003266"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003267\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003268Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003269
3270static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003271socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003272{
3273 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003274 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003275 struct in_addr packed_addr;
3276
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003277 if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003278 return NULL;
3279 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00003280
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003281 if (addr_len != sizeof(packed_addr)) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003282 PyErr_SetString(socket_error,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003283 "packed IP wrong length for inet_ntoa");
3284 return NULL;
3285 }
3286
3287 memcpy(&packed_addr, packed_str, addr_len);
3288
3289 return PyString_FromString(inet_ntoa(packed_addr));
3290}
Guido van Rossum82a5c661998-07-07 20:45:43 +00003291
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003292#ifdef HAVE_INET_PTON
3293
3294PyDoc_STRVAR(inet_pton_doc,
3295"inet_pton(af, ip) -> packed IP address string\n\
3296\n\
3297Convert an IP address from string format to a packed string suitable\n\
3298for use with low-level network functions.");
3299
3300static PyObject *
3301socket_inet_pton(PyObject *self, PyObject *args)
3302{
3303 int af;
3304 char* ip;
3305 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003306#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003307 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003308#else
3309 char packed[sizeof(struct in_addr)];
3310#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003311 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
3312 return NULL;
3313 }
3314
Martin v. Löwis04697e82004-06-02 12:35:29 +00003315#if !defined(ENABLE_IPV6) && defined(AF_INET6)
Martin v. Löwis10649092003-08-05 06:25:06 +00003316 if(af == AF_INET6) {
3317 PyErr_SetString(socket_error,
3318 "can't use AF_INET6, IPv6 is disabled");
3319 return NULL;
3320 }
3321#endif
3322
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003323 retval = inet_pton(af, ip, packed);
3324 if (retval < 0) {
3325 PyErr_SetFromErrno(socket_error);
3326 return NULL;
3327 } else if (retval == 0) {
3328 PyErr_SetString(socket_error,
3329 "illegal IP address string passed to inet_pton");
3330 return NULL;
3331 } else if (af == AF_INET) {
3332 return PyString_FromStringAndSize(packed,
3333 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003334#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003335 } else if (af == AF_INET6) {
3336 return PyString_FromStringAndSize(packed,
3337 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003338#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003339 } else {
3340 PyErr_SetString(socket_error, "unknown address family");
3341 return NULL;
3342 }
3343}
3344
3345PyDoc_STRVAR(inet_ntop_doc,
3346"inet_ntop(af, packed_ip) -> string formatted IP address\n\
3347\n\
3348Convert a packed IP address of the given family to string format.");
3349
3350static PyObject *
3351socket_inet_ntop(PyObject *self, PyObject *args)
3352{
3353 int af;
3354 char* packed;
3355 int len;
3356 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003357#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003358 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003359#else
3360 char ip[INET_ADDRSTRLEN + 1];
3361#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003362
3363 /* Guarantee NUL-termination for PyString_FromString() below */
3364 memset((void *) &ip[0], '\0', sizeof(ip) + 1);
3365
3366 if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) {
3367 return NULL;
3368 }
3369
3370 if (af == AF_INET) {
3371 if (len != sizeof(struct in_addr)) {
3372 PyErr_SetString(PyExc_ValueError,
3373 "invalid length of packed IP address string");
3374 return NULL;
3375 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003376#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003377 } else if (af == AF_INET6) {
3378 if (len != sizeof(struct in6_addr)) {
3379 PyErr_SetString(PyExc_ValueError,
3380 "invalid length of packed IP address string");
3381 return NULL;
3382 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003383#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003384 } else {
3385 PyErr_Format(PyExc_ValueError,
3386 "unknown address family %d", af);
3387 return NULL;
3388 }
3389
3390 retval = inet_ntop(af, packed, ip, sizeof(ip));
3391 if (!retval) {
3392 PyErr_SetFromErrno(socket_error);
3393 return NULL;
3394 } else {
3395 return PyString_FromString(retval);
3396 }
3397
3398 /* NOTREACHED */
3399 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
3400 return NULL;
3401}
3402
3403#endif /* HAVE_INET_PTON */
3404
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003405/* Python interface to getaddrinfo(host, port). */
3406
3407/*ARGSUSED*/
3408static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003409socket_getaddrinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003410{
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003411 struct addrinfo hints, *res;
3412 struct addrinfo *res0 = NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003413 PyObject *hobj = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003414 PyObject *pobj = (PyObject *)NULL;
Martin v. Löwisf65b1a12001-08-12 09:28:40 +00003415 char pbuf[30];
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003416 char *hptr, *pptr;
3417 int family, socktype, protocol, flags;
3418 int error;
3419 PyObject *all = (PyObject *)NULL;
3420 PyObject *single = (PyObject *)NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003421 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003422
3423 family = socktype = protocol = flags = 0;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003424 family = AF_UNSPEC;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003425 if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
3426 &hobj, &pobj, &family, &socktype,
3427 &protocol, &flags)) {
3428 return NULL;
3429 }
3430 if (hobj == Py_None) {
3431 hptr = NULL;
3432 } else if (PyUnicode_Check(hobj)) {
3433 idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
3434 if (!idna)
3435 return NULL;
3436 hptr = PyString_AsString(idna);
3437 } else if (PyString_Check(hobj)) {
3438 hptr = PyString_AsString(hobj);
3439 } else {
3440 PyErr_SetString(PyExc_TypeError,
3441 "getaddrinfo() argument 1 must be string or None");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003442 return NULL;
3443 }
3444 if (PyInt_Check(pobj)) {
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003445 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003446 pptr = pbuf;
3447 } else if (PyString_Check(pobj)) {
3448 pptr = PyString_AsString(pobj);
3449 } else if (pobj == Py_None) {
3450 pptr = (char *)NULL;
3451 } else {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003452 PyErr_SetString(socket_error, "Int or String expected");
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003453 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003454 }
3455 memset(&hints, 0, sizeof(hints));
3456 hints.ai_family = family;
3457 hints.ai_socktype = socktype;
3458 hints.ai_protocol = protocol;
3459 hints.ai_flags = flags;
Just van Rossum1040d2c2003-05-09 07:53:18 +00003460 Py_BEGIN_ALLOW_THREADS
3461 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003462 error = getaddrinfo(hptr, pptr, &hints, &res0);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003463 Py_END_ALLOW_THREADS
3464 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003465 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003466 set_gaierror(error);
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003467 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003468 }
3469
3470 if ((all = PyList_New(0)) == NULL)
3471 goto err;
3472 for (res = res0; res; res = res->ai_next) {
Guido van Rossum716aac02001-10-12 18:59:27 +00003473 PyObject *addr =
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00003474 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
Guido van Rossum716aac02001-10-12 18:59:27 +00003475 if (addr == NULL)
3476 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003477 single = Py_BuildValue("iiisO", res->ai_family,
3478 res->ai_socktype, res->ai_protocol,
3479 res->ai_canonname ? res->ai_canonname : "",
Guido van Rossum716aac02001-10-12 18:59:27 +00003480 addr);
3481 Py_DECREF(addr);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003482 if (single == NULL)
3483 goto err;
3484
3485 if (PyList_Append(all, single))
3486 goto err;
3487 Py_XDECREF(single);
3488 }
Martin v. Löwis2548c732003-04-18 10:39:54 +00003489 Py_XDECREF(idna);
Neal Norwitz90128ba2002-08-09 03:37:42 +00003490 if (res0)
3491 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003492 return all;
3493 err:
3494 Py_XDECREF(single);
3495 Py_XDECREF(all);
Martin v. Löwis2548c732003-04-18 10:39:54 +00003496 Py_XDECREF(idna);
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003497 if (res0)
3498 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003499 return (PyObject *)NULL;
3500}
3501
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003502PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003503"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
3504 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003505\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003506Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003507
3508/* Python interface to getnameinfo(sa, flags). */
3509
3510/*ARGSUSED*/
3511static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003512socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003513{
3514 PyObject *sa = (PyObject *)NULL;
3515 int flags;
3516 char *hostp;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003517 int port, flowinfo, scope_id;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003518 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
3519 struct addrinfo hints, *res = NULL;
3520 int error;
3521 PyObject *ret = (PyObject *)NULL;
3522
3523 flags = flowinfo = scope_id = 0;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003524 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003525 return NULL;
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003526 if (!PyArg_ParseTuple(sa, "si|ii",
3527 &hostp, &port, &flowinfo, &scope_id))
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003528 return NULL;
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003529 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003530 memset(&hints, 0, sizeof(hints));
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003531 hints.ai_family = AF_UNSPEC;
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003532 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Just van Rossum1040d2c2003-05-09 07:53:18 +00003533 Py_BEGIN_ALLOW_THREADS
3534 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003535 error = getaddrinfo(hostp, pbuf, &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003536 Py_END_ALLOW_THREADS
3537 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003538 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003539 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003540 goto fail;
3541 }
3542 if (res->ai_next) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003543 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003544 "sockaddr resolved to multiple addresses");
3545 goto fail;
3546 }
3547 switch (res->ai_family) {
3548 case AF_INET:
3549 {
3550 char *t1;
3551 int t2;
3552 if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003553 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003554 "IPv4 sockaddr must be 2 tuple");
3555 goto fail;
3556 }
3557 break;
3558 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003559#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003560 case AF_INET6:
3561 {
3562 struct sockaddr_in6 *sin6;
3563 sin6 = (struct sockaddr_in6 *)res->ai_addr;
3564 sin6->sin6_flowinfo = flowinfo;
3565 sin6->sin6_scope_id = scope_id;
3566 break;
3567 }
3568#endif
3569 }
3570 error = getnameinfo(res->ai_addr, res->ai_addrlen,
3571 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
3572 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003573 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003574 goto fail;
3575 }
3576 ret = Py_BuildValue("ss", hbuf, pbuf);
3577
3578fail:
3579 if (res)
3580 freeaddrinfo(res);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003581 return ret;
3582}
3583
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003584PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003585"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003586\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003587Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003588
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003589
3590/* Python API to getting and setting the default timeout value. */
3591
3592static PyObject *
3593socket_getdefaulttimeout(PyObject *self)
3594{
3595 if (defaulttimeout < 0.0) {
3596 Py_INCREF(Py_None);
3597 return Py_None;
3598 }
3599 else
3600 return PyFloat_FromDouble(defaulttimeout);
3601}
3602
3603PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003604"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003605\n\
3606Returns the default timeout in floating seconds for new socket objects.\n\
3607A value of None indicates that new socket objects have no timeout.\n\
3608When the socket module is first imported, the default is None.");
3609
3610static PyObject *
3611socket_setdefaulttimeout(PyObject *self, PyObject *arg)
3612{
3613 double timeout;
3614
3615 if (arg == Py_None)
3616 timeout = -1.0;
3617 else {
3618 timeout = PyFloat_AsDouble(arg);
3619 if (timeout < 0.0) {
3620 if (!PyErr_Occurred())
3621 PyErr_SetString(PyExc_ValueError,
3622 "Timeout value out of range");
3623 return NULL;
3624 }
3625 }
3626
3627 defaulttimeout = timeout;
3628
3629 Py_INCREF(Py_None);
3630 return Py_None;
3631}
3632
3633PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003634"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003635\n\
3636Set the default timeout in floating seconds for new socket objects.\n\
3637A value of None indicates that new socket objects have no timeout.\n\
3638When the socket module is first imported, the default is None.");
3639
3640
Guido van Rossum30a685f1991-06-27 15:51:29 +00003641/* List of functions exported by this module. */
3642
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003643static PyMethodDef socket_methods[] = {
3644 {"gethostbyname", socket_gethostbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003645 METH_VARARGS, gethostbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003646 {"gethostbyname_ex", socket_gethostbyname_ex,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003647 METH_VARARGS, ghbn_ex_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003648 {"gethostbyaddr", socket_gethostbyaddr,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003649 METH_VARARGS, gethostbyaddr_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003650 {"gethostname", socket_gethostname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003651 METH_VARARGS, gethostname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003652 {"getservbyname", socket_getservbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003653 METH_VARARGS, getservbyname_doc},
Barry Warsaw11b91a02004-06-28 00:50:43 +00003654 {"getservbyport", socket_getservbyport,
3655 METH_VARARGS, getservbyport_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003656 {"getprotobyname", socket_getprotobyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003657 METH_VARARGS,getprotobyname_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003658#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003659 {"fromfd", socket_fromfd,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003660 METH_VARARGS, fromfd_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003661#endif
Dave Cole331708b2004-08-09 04:51:41 +00003662#ifdef HAVE_SOCKETPAIR
3663 {"socketpair", socket_socketpair,
3664 METH_VARARGS, socketpair_doc},
3665#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003666 {"ntohs", socket_ntohs,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003667 METH_VARARGS, ntohs_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003668 {"ntohl", socket_ntohl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003669 METH_O, ntohl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003670 {"htons", socket_htons,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003671 METH_VARARGS, htons_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003672 {"htonl", socket_htonl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003673 METH_O, htonl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003674 {"inet_aton", socket_inet_aton,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003675 METH_VARARGS, inet_aton_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003676 {"inet_ntoa", socket_inet_ntoa,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003677 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003678#ifdef HAVE_INET_PTON
3679 {"inet_pton", socket_inet_pton,
3680 METH_VARARGS, inet_pton_doc},
3681 {"inet_ntop", socket_inet_ntop,
3682 METH_VARARGS, inet_ntop_doc},
3683#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003684 {"getaddrinfo", socket_getaddrinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003685 METH_VARARGS, getaddrinfo_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003686 {"getnameinfo", socket_getnameinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003687 METH_VARARGS, getnameinfo_doc},
Guido van Rossum1693ba82002-07-18 21:11:26 +00003688 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003689 METH_NOARGS, getdefaulttimeout_doc},
3690 {"setdefaulttimeout", socket_setdefaulttimeout,
3691 METH_O, setdefaulttimeout_doc},
Guido van Rossum30a685f1991-06-27 15:51:29 +00003692 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003693};
3694
Guido van Rossum30a685f1991-06-27 15:51:29 +00003695
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003696#ifdef RISCOS
3697#define OS_INIT_DEFINED
Guido van Rossumbe32c891996-06-20 16:25:29 +00003698
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003699static int
3700os_init(void)
3701{
3702 _kernel_swi_regs r;
3703
3704 r.r[0] = 0;
3705 _kernel_swi(0x43380, &r, &r);
3706 taskwindow = r.r[0];
3707
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003708 return 1;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003709}
3710
3711#endif /* RISCOS */
3712
3713
3714#ifdef MS_WINDOWS
3715#define OS_INIT_DEFINED
3716
3717/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003718
3719static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003720os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00003721{
3722 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00003723}
3724
3725static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003726os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00003727{
3728 WSADATA WSAData;
3729 int ret;
3730 char buf[100];
3731 ret = WSAStartup(0x0101, &WSAData);
3732 switch (ret) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003733 case 0: /* No error */
Mark Hammond14350ab2003-07-09 04:57:46 +00003734 Py_AtExit(os_cleanup);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003735 return 1; /* Success */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003736 case WSASYSNOTREADY:
3737 PyErr_SetString(PyExc_ImportError,
3738 "WSAStartup failed: network not ready");
3739 break;
3740 case WSAVERNOTSUPPORTED:
3741 case WSAEINVAL:
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003742 PyErr_SetString(
3743 PyExc_ImportError,
3744 "WSAStartup failed: requested version not supported");
Guido van Rossumbe32c891996-06-20 16:25:29 +00003745 break;
3746 default:
Tim Peters885d4572001-11-28 20:27:42 +00003747 PyOS_snprintf(buf, sizeof(buf),
3748 "WSAStartup failed: error code %d", ret);
Guido van Rossumbe32c891996-06-20 16:25:29 +00003749 PyErr_SetString(PyExc_ImportError, buf);
3750 break;
3751 }
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003752 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003753}
3754
Guido van Rossum8d665e61996-06-26 18:22:49 +00003755#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003756
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003757
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003758#ifdef PYOS_OS2
3759#define OS_INIT_DEFINED
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003760
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003761/* Additional initialization for OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003762
3763static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003764os_init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003765{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003766#ifndef PYCC_GCC
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003767 char reason[64];
3768 int rc = sock_init();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003769
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003770 if (rc == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003771 return 1; /* Success */
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003772 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003773
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003774 PyOS_snprintf(reason, sizeof(reason),
3775 "OS/2 TCP/IP Error# %d", sock_errno());
3776 PyErr_SetString(PyExc_ImportError, reason);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003777
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003778 return 0; /* Failure */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00003779#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003780 /* No need to initialise sockets with GCC/EMX */
3781 return 1; /* Success */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00003782#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003783}
3784
3785#endif /* PYOS_OS2 */
3786
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003787
3788#ifndef OS_INIT_DEFINED
3789static int
3790os_init(void)
3791{
3792 return 1; /* Success */
3793}
3794#endif
3795
3796
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003797/* C API table - always add new things to the end for binary
3798 compatibility. */
3799static
3800PySocketModule_APIObject PySocketModuleAPI =
3801{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003802 &sock_type,
Brett Cannon06c34792004-03-23 23:16:54 +00003803 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003804};
3805
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003806
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003807/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003808
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003809 This module is actually called "_socket", and there's a wrapper
3810 "socket.py" which implements some additional functionality. On some
3811 platforms (e.g. Windows and OS/2), socket.py also implements a
3812 wrapper for the socket type that provides missing functionality such
3813 as makefile(), dup() and fromfd(). The import of "_socket" may fail
3814 with an ImportError exception if os-specific initialization fails.
3815 On Windows, this does WINSOCK initialization. When WINSOCK is
3816 initialized succesfully, a call to WSACleanup() is scheduled to be
3817 made at exit time.
3818*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003819
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003820PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003821"Implementation module for socket operations.\n\
3822\n\
3823See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003824
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003825PyMODINIT_FUNC
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003826init_socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003827{
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003828 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00003829
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003830 if (!os_init())
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003831 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003832
3833 sock_type.ob_type = &PyType_Type;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003834 m = Py_InitModule3(PySocket_MODULE_NAME,
3835 socket_methods,
3836 socket_doc);
3837
3838 socket_error = PyErr_NewException("socket.error", NULL, NULL);
3839 if (socket_error == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003840 return;
Brett Cannon06c34792004-03-23 23:16:54 +00003841 PySocketModuleAPI.error = socket_error;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003842 Py_INCREF(socket_error);
3843 PyModule_AddObject(m, "error", socket_error);
3844 socket_herror = PyErr_NewException("socket.herror",
3845 socket_error, NULL);
3846 if (socket_herror == NULL)
3847 return;
3848 Py_INCREF(socket_herror);
3849 PyModule_AddObject(m, "herror", socket_herror);
3850 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003851 NULL);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003852 if (socket_gaierror == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003853 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003854 Py_INCREF(socket_gaierror);
3855 PyModule_AddObject(m, "gaierror", socket_gaierror);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00003856 socket_timeout = PyErr_NewException("socket.timeout",
3857 socket_error, NULL);
3858 if (socket_timeout == NULL)
3859 return;
3860 Py_INCREF(socket_timeout);
3861 PyModule_AddObject(m, "timeout", socket_timeout);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003862 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003863 if (PyModule_AddObject(m, "SocketType",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003864 (PyObject *)&sock_type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003865 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003866 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003867 if (PyModule_AddObject(m, "socket",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003868 (PyObject *)&sock_type) != 0)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003869 return;
Guido van Rossum09be4091999-08-09 14:40:40 +00003870
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003871#ifdef ENABLE_IPV6
3872 has_ipv6 = Py_True;
3873#else
3874 has_ipv6 = Py_False;
3875#endif
3876 Py_INCREF(has_ipv6);
3877 PyModule_AddObject(m, "has_ipv6", has_ipv6);
3878
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003879 /* Export C API */
Fred Drake4baedc12002-04-01 14:53:37 +00003880 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003881 PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL)
3882 ) != 0)
3883 return;
3884
Guido van Rossum09be4091999-08-09 14:40:40 +00003885 /* Address families (we only support AF_INET and AF_UNIX) */
3886#ifdef AF_UNSPEC
Fred Drake4baedc12002-04-01 14:53:37 +00003887 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00003888#endif
Fred Drake4baedc12002-04-01 14:53:37 +00003889 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003890#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003891 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003892#endif /* AF_INET6 */
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00003893#if defined(AF_UNIX)
Fred Drake4baedc12002-04-01 14:53:37 +00003894 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003895#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003896#ifdef AF_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00003897 /* Amateur Radio AX.25 */
3898 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003899#endif
3900#ifdef AF_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00003901 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003902#endif
3903#ifdef AF_APPLETALK
Fred Drake4baedc12002-04-01 14:53:37 +00003904 /* Appletalk DDP */
3905 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00003906#endif
3907#ifdef AF_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00003908 /* Amateur radio NetROM */
3909 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00003910#endif
3911#ifdef AF_BRIDGE
Fred Drake4baedc12002-04-01 14:53:37 +00003912 /* Multiprotocol bridge */
3913 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003914#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00003915#ifdef AF_ATMPVC
3916 /* ATM PVCs */
3917 PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC);
3918#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003919#ifdef AF_AAL5
Fred Drake4baedc12002-04-01 14:53:37 +00003920 /* Reserved for Werner's ATM */
3921 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00003922#endif
3923#ifdef AF_X25
Fred Drake4baedc12002-04-01 14:53:37 +00003924 /* Reserved for X.25 project */
3925 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003926#endif
3927#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003928 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00003929#endif
3930#ifdef AF_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00003931 /* Amateur Radio X.25 PLP */
3932 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003933#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00003934#ifdef AF_DECnet
3935 /* Reserved for DECnet project */
3936 PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet);
3937#endif
3938#ifdef AF_NETBEUI
3939 /* Reserved for 802.2LLC project */
3940 PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI);
3941#endif
3942#ifdef AF_SECURITY
3943 /* Security callback pseudo AF */
3944 PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY);
3945#endif
3946#ifdef AF_KEY
3947 /* PF_KEY key management API */
3948 PyModule_AddIntConstant(m, "AF_KEY", AF_KEY);
3949#endif
3950#ifdef AF_NETLINK
3951 /* */
3952 PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);
3953#endif
3954#ifdef AF_ROUTE
3955 /* Alias to emulate 4.4BSD */
3956 PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE);
3957#endif
3958#ifdef AF_ASH
3959 /* Ash */
3960 PyModule_AddIntConstant(m, "AF_ASH", AF_ASH);
3961#endif
3962#ifdef AF_ECONET
3963 /* Acorn Econet */
3964 PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET);
3965#endif
3966#ifdef AF_ATMSVC
3967 /* ATM SVCs */
3968 PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC);
3969#endif
3970#ifdef AF_SNA
3971 /* Linux SNA Project (nutters!) */
3972 PyModule_AddIntConstant(m, "AF_SNA", AF_SNA);
3973#endif
3974#ifdef AF_IRDA
3975 /* IRDA sockets */
3976 PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA);
3977#endif
3978#ifdef AF_PPPOX
3979 /* PPPoX sockets */
3980 PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX);
3981#endif
3982#ifdef AF_WANPIPE
3983 /* Wanpipe API Sockets */
3984 PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE);
3985#endif
3986#ifdef AF_LLC
3987 /* Linux LLC */
3988 PyModule_AddIntConstant(m, "AF_LLC", AF_LLC);
3989#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00003990
Hye-Shik Chang81268602004-02-02 06:05:24 +00003991#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00003992 PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);
3993 PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);
Hye-Shik Chang81268602004-02-02 06:05:24 +00003994#if !defined(__FreeBSD__)
Martin v. Löwis12af0482004-01-31 12:34:17 +00003995 PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00003996#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00003997 PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00003998 PyModule_AddObject(m, "BDADDR_ANY", Py_BuildValue("s", "00:00:00:00:00:00"));
3999 PyModule_AddObject(m, "BDADDR_LOCAL", Py_BuildValue("s", "00:00:00:FF:FF:FF"));
Martin v. Löwis12af0482004-01-31 12:34:17 +00004000#endif
4001
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00004002#ifdef HAVE_NETPACKET_PACKET_H
Fred Drake4baedc12002-04-01 14:53:37 +00004003 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
4004 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
4005 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
4006 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
4007 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
4008 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
4009 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
4010 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
4011 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00004012#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004013
4014 /* Socket types */
Fred Drake4baedc12002-04-01 14:53:37 +00004015 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
4016 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004017#ifndef __BEOS__
4018/* We have incomplete socket support. */
Fred Drake4baedc12002-04-01 14:53:37 +00004019 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
4020 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00004021#if defined(SOCK_RDM)
Fred Drake4baedc12002-04-01 14:53:37 +00004022 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004023#endif
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00004024#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004025
4026#ifdef SO_DEBUG
Fred Drake4baedc12002-04-01 14:53:37 +00004027 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004028#endif
4029#ifdef SO_ACCEPTCONN
Fred Drake4baedc12002-04-01 14:53:37 +00004030 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004031#endif
4032#ifdef SO_REUSEADDR
Fred Drake4baedc12002-04-01 14:53:37 +00004033 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004034#endif
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00004035#ifdef SO_EXCLUSIVEADDRUSE
4036 PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE);
4037#endif
4038
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004039#ifdef SO_KEEPALIVE
Fred Drake4baedc12002-04-01 14:53:37 +00004040 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004041#endif
4042#ifdef SO_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00004043 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004044#endif
4045#ifdef SO_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00004046 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004047#endif
4048#ifdef SO_USELOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00004049 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004050#endif
4051#ifdef SO_LINGER
Fred Drake4baedc12002-04-01 14:53:37 +00004052 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004053#endif
4054#ifdef SO_OOBINLINE
Fred Drake4baedc12002-04-01 14:53:37 +00004055 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004056#endif
4057#ifdef SO_REUSEPORT
Fred Drake4baedc12002-04-01 14:53:37 +00004058 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004059#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004060#ifdef SO_SNDBUF
Fred Drake4baedc12002-04-01 14:53:37 +00004061 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004062#endif
4063#ifdef SO_RCVBUF
Fred Drake4baedc12002-04-01 14:53:37 +00004064 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004065#endif
4066#ifdef SO_SNDLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00004067 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004068#endif
4069#ifdef SO_RCVLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00004070 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004071#endif
4072#ifdef SO_SNDTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00004073 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004074#endif
4075#ifdef SO_RCVTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00004076 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004077#endif
4078#ifdef SO_ERROR
Fred Drake4baedc12002-04-01 14:53:37 +00004079 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004080#endif
4081#ifdef SO_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004082 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004083#endif
4084
4085 /* Maximum number of connections for "listen" */
4086#ifdef SOMAXCONN
Fred Drake4baedc12002-04-01 14:53:37 +00004087 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004088#else
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004089 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004090#endif
4091
4092 /* Flags for send, recv */
4093#ifdef MSG_OOB
Fred Drake4baedc12002-04-01 14:53:37 +00004094 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004095#endif
4096#ifdef MSG_PEEK
Fred Drake4baedc12002-04-01 14:53:37 +00004097 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004098#endif
4099#ifdef MSG_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00004100 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004101#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00004102#ifdef MSG_DONTWAIT
Fred Drake4baedc12002-04-01 14:53:37 +00004103 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00004104#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004105#ifdef MSG_EOR
Fred Drake4baedc12002-04-01 14:53:37 +00004106 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004107#endif
4108#ifdef MSG_TRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00004109 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004110#endif
4111#ifdef MSG_CTRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00004112 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004113#endif
4114#ifdef MSG_WAITALL
Fred Drake4baedc12002-04-01 14:53:37 +00004115 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004116#endif
4117#ifdef MSG_BTAG
Fred Drake4baedc12002-04-01 14:53:37 +00004118 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004119#endif
4120#ifdef MSG_ETAG
Fred Drake4baedc12002-04-01 14:53:37 +00004121 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004122#endif
4123
4124 /* Protocol level and numbers, usable for [gs]etsockopt */
4125#ifdef SOL_SOCKET
Fred Drake4baedc12002-04-01 14:53:37 +00004126 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004127#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004128#ifdef SOL_IP
Fred Drake4baedc12002-04-01 14:53:37 +00004129 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004130#else
Fred Drake4baedc12002-04-01 14:53:37 +00004131 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00004132#endif
4133#ifdef SOL_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00004134 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00004135#endif
4136#ifdef SOL_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00004137 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004138#endif
4139#ifdef SOL_ATALK
Fred Drake4baedc12002-04-01 14:53:37 +00004140 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004141#endif
4142#ifdef SOL_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00004143 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004144#endif
4145#ifdef SOL_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00004146 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004147#endif
4148#ifdef SOL_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00004149 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004150#else
Fred Drake4baedc12002-04-01 14:53:37 +00004151 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00004152#endif
4153#ifdef SOL_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00004154 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004155#else
Fred Drake4baedc12002-04-01 14:53:37 +00004156 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00004157#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004158#ifdef IPPROTO_IP
Fred Drake4baedc12002-04-01 14:53:37 +00004159 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00004160#else
Fred Drake4baedc12002-04-01 14:53:37 +00004161 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004162#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004163#ifdef IPPROTO_HOPOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004164 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004165#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004166#ifdef IPPROTO_ICMP
Fred Drake4baedc12002-04-01 14:53:37 +00004167 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00004168#else
Fred Drake4baedc12002-04-01 14:53:37 +00004169 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004170#endif
4171#ifdef IPPROTO_IGMP
Fred Drake4baedc12002-04-01 14:53:37 +00004172 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004173#endif
4174#ifdef IPPROTO_GGP
Fred Drake4baedc12002-04-01 14:53:37 +00004175 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004176#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004177#ifdef IPPROTO_IPV4
Fred Drake4baedc12002-04-01 14:53:37 +00004178 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004179#endif
Martin v. Löwisa0f17342003-10-03 13:56:20 +00004180#ifdef IPPROTO_IPV6
4181 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
4182#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004183#ifdef IPPROTO_IPIP
Fred Drake4baedc12002-04-01 14:53:37 +00004184 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004185#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004186#ifdef IPPROTO_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00004187 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00004188#else
Fred Drake4baedc12002-04-01 14:53:37 +00004189 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004190#endif
4191#ifdef IPPROTO_EGP
Fred Drake4baedc12002-04-01 14:53:37 +00004192 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004193#endif
4194#ifdef IPPROTO_PUP
Fred Drake4baedc12002-04-01 14:53:37 +00004195 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004196#endif
4197#ifdef IPPROTO_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00004198 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00004199#else
Fred Drake4baedc12002-04-01 14:53:37 +00004200 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004201#endif
4202#ifdef IPPROTO_IDP
Fred Drake4baedc12002-04-01 14:53:37 +00004203 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004204#endif
4205#ifdef IPPROTO_HELLO
Fred Drake4baedc12002-04-01 14:53:37 +00004206 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004207#endif
4208#ifdef IPPROTO_ND
Fred Drake4baedc12002-04-01 14:53:37 +00004209 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004210#endif
4211#ifdef IPPROTO_TP
Fred Drake4baedc12002-04-01 14:53:37 +00004212 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004213#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004214#ifdef IPPROTO_IPV6
Fred Drake4baedc12002-04-01 14:53:37 +00004215 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004216#endif
4217#ifdef IPPROTO_ROUTING
Fred Drake4baedc12002-04-01 14:53:37 +00004218 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004219#endif
4220#ifdef IPPROTO_FRAGMENT
Fred Drake4baedc12002-04-01 14:53:37 +00004221 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004222#endif
4223#ifdef IPPROTO_RSVP
Fred Drake4baedc12002-04-01 14:53:37 +00004224 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004225#endif
4226#ifdef IPPROTO_GRE
Fred Drake4baedc12002-04-01 14:53:37 +00004227 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004228#endif
4229#ifdef IPPROTO_ESP
Fred Drake4baedc12002-04-01 14:53:37 +00004230 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004231#endif
4232#ifdef IPPROTO_AH
Fred Drake4baedc12002-04-01 14:53:37 +00004233 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004234#endif
4235#ifdef IPPROTO_MOBILE
Fred Drake4baedc12002-04-01 14:53:37 +00004236 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004237#endif
4238#ifdef IPPROTO_ICMPV6
Fred Drake4baedc12002-04-01 14:53:37 +00004239 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004240#endif
4241#ifdef IPPROTO_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00004242 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004243#endif
4244#ifdef IPPROTO_DSTOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004245 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004246#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004247#ifdef IPPROTO_XTP
Fred Drake4baedc12002-04-01 14:53:37 +00004248 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004249#endif
4250#ifdef IPPROTO_EON
Fred Drake4baedc12002-04-01 14:53:37 +00004251 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004252#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004253#ifdef IPPROTO_PIM
Fred Drake4baedc12002-04-01 14:53:37 +00004254 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004255#endif
4256#ifdef IPPROTO_IPCOMP
Fred Drake4baedc12002-04-01 14:53:37 +00004257 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004258#endif
4259#ifdef IPPROTO_VRRP
Fred Drake4baedc12002-04-01 14:53:37 +00004260 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004261#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004262#ifdef IPPROTO_BIP
Fred Drake4baedc12002-04-01 14:53:37 +00004263 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004264#endif
4265/**/
4266#ifdef IPPROTO_RAW
Fred Drake4baedc12002-04-01 14:53:37 +00004267 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00004268#else
Fred Drake4baedc12002-04-01 14:53:37 +00004269 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004270#endif
4271#ifdef IPPROTO_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00004272 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004273#endif
4274
4275 /* Some port configuration */
4276#ifdef IPPORT_RESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00004277 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004278#else
Fred Drake4baedc12002-04-01 14:53:37 +00004279 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004280#endif
4281#ifdef IPPORT_USERRESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00004282 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004283#else
Fred Drake4baedc12002-04-01 14:53:37 +00004284 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004285#endif
4286
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004287 /* Some reserved IP v.4 addresses */
4288#ifdef INADDR_ANY
Fred Drake4baedc12002-04-01 14:53:37 +00004289 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004290#else
Fred Drake4baedc12002-04-01 14:53:37 +00004291 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004292#endif
4293#ifdef INADDR_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00004294 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004295#else
Fred Drake4baedc12002-04-01 14:53:37 +00004296 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004297#endif
4298#ifdef INADDR_LOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00004299 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004300#else
Fred Drake4baedc12002-04-01 14:53:37 +00004301 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004302#endif
4303#ifdef INADDR_UNSPEC_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004304 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004305#else
Fred Drake4baedc12002-04-01 14:53:37 +00004306 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004307#endif
4308#ifdef INADDR_ALLHOSTS_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004309 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
4310 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004311#else
Fred Drake4baedc12002-04-01 14:53:37 +00004312 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004313#endif
4314#ifdef INADDR_MAX_LOCAL_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004315 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
4316 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004317#else
Fred Drake4baedc12002-04-01 14:53:37 +00004318 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004319#endif
4320#ifdef INADDR_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00004321 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004322#else
Fred Drake4baedc12002-04-01 14:53:37 +00004323 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004324#endif
4325
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004326 /* IPv4 [gs]etsockopt options */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004327#ifdef IP_OPTIONS
Fred Drake4baedc12002-04-01 14:53:37 +00004328 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004329#endif
4330#ifdef IP_HDRINCL
Fred Drake4baedc12002-04-01 14:53:37 +00004331 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004332#endif
4333#ifdef IP_TOS
Fred Drake4baedc12002-04-01 14:53:37 +00004334 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004335#endif
4336#ifdef IP_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004337 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004338#endif
4339#ifdef IP_RECVOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004340 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004341#endif
4342#ifdef IP_RECVRETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004343 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004344#endif
4345#ifdef IP_RECVDSTADDR
Fred Drake4baedc12002-04-01 14:53:37 +00004346 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004347#endif
4348#ifdef IP_RETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004349 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004350#endif
4351#ifdef IP_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00004352 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004353#endif
4354#ifdef IP_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004355 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004356#endif
4357#ifdef IP_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004358 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004359#endif
4360#ifdef IP_ADD_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00004361 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004362#endif
4363#ifdef IP_DROP_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00004364 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004365#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004366#ifdef IP_DEFAULT_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004367 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
4368 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00004369#endif
4370#ifdef IP_DEFAULT_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004371 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
4372 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004373#endif
4374#ifdef IP_MAX_MEMBERSHIPS
Fred Drake4baedc12002-04-01 14:53:37 +00004375 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00004376#endif
4377
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004378 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
4379#ifdef IPV6_JOIN_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004380 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004381#endif
4382#ifdef IPV6_LEAVE_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004383 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004384#endif
4385#ifdef IPV6_MULTICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00004386 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004387#endif
4388#ifdef IPV6_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00004389 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004390#endif
4391#ifdef IPV6_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004392 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004393#endif
4394#ifdef IPV6_UNICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00004395 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004396#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004397 /* Additional IPV6 socket options, defined in RFC 3493 */
4398#ifdef IPV6_V6ONLY
4399 PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);
4400#endif
4401 /* Advanced IPV6 socket options, from RFC 3542 */
4402#ifdef IPV6_CHECKSUM
4403 PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);
4404#endif
4405#ifdef IPV6_DONTFRAG
4406 PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);
4407#endif
4408#ifdef IPV6_DSTOPTS
4409 PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);
4410#endif
4411#ifdef IPV6_HOPLIMIT
4412 PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);
4413#endif
4414#ifdef IPV6_HOPOPTS
4415 PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);
4416#endif
4417#ifdef IPV6_NEXTHOP
4418 PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);
4419#endif
4420#ifdef IPV6_PATHMTU
4421 PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);
4422#endif
4423#ifdef IPV6_PKTINFO
4424 PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);
4425#endif
4426#ifdef IPV6_RECVDSTOPTS
4427 PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);
4428#endif
4429#ifdef IPV6_RECVHOPLIMIT
4430 PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);
4431#endif
4432#ifdef IPV6_RECVHOPOPTS
4433 PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);
4434#endif
4435#ifdef IPV6_RECVPKTINFO
4436 PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);
4437#endif
4438#ifdef IPV6_RECVRTHDR
4439 PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);
4440#endif
4441#ifdef IPV6_RECVTCLASS
4442 PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);
4443#endif
4444#ifdef IPV6_RTHDR
4445 PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);
4446#endif
4447#ifdef IPV6_RTHDRDSTOPTS
4448 PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);
4449#endif
4450#ifdef IPV6_RTHDR_TYPE_0
4451 PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);
4452#endif
4453#ifdef IPV6_RECVPATHMTU
4454 PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);
4455#endif
4456#ifdef IPV6_TCLASS
4457 PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);
4458#endif
4459#ifdef IPV6_USE_MIN_MTU
4460 PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);
4461#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004462
Guido van Rossum09be4091999-08-09 14:40:40 +00004463 /* TCP options */
4464#ifdef TCP_NODELAY
Fred Drake4baedc12002-04-01 14:53:37 +00004465 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00004466#endif
4467#ifdef TCP_MAXSEG
Fred Drake4baedc12002-04-01 14:53:37 +00004468 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00004469#endif
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004470#ifdef TCP_CORK
Fred Drake4baedc12002-04-01 14:53:37 +00004471 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004472#endif
4473#ifdef TCP_KEEPIDLE
Fred Drake4baedc12002-04-01 14:53:37 +00004474 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004475#endif
4476#ifdef TCP_KEEPINTVL
Fred Drake4baedc12002-04-01 14:53:37 +00004477 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004478#endif
4479#ifdef TCP_KEEPCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004480 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004481#endif
4482#ifdef TCP_SYNCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004483 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004484#endif
4485#ifdef TCP_LINGER2
Fred Drake4baedc12002-04-01 14:53:37 +00004486 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004487#endif
4488#ifdef TCP_DEFER_ACCEPT
Fred Drake4baedc12002-04-01 14:53:37 +00004489 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004490#endif
4491#ifdef TCP_WINDOW_CLAMP
Fred Drake4baedc12002-04-01 14:53:37 +00004492 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004493#endif
4494#ifdef TCP_INFO
Fred Drake4baedc12002-04-01 14:53:37 +00004495 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004496#endif
4497#ifdef TCP_QUICKACK
Fred Drake4baedc12002-04-01 14:53:37 +00004498 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004499#endif
4500
Guido van Rossum09be4091999-08-09 14:40:40 +00004501
4502 /* IPX options */
4503#ifdef IPX_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004504 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004505#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004506
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004507 /* get{addr,name}info parameters */
4508#ifdef EAI_ADDRFAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004509 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004510#endif
4511#ifdef EAI_AGAIN
Fred Drake4baedc12002-04-01 14:53:37 +00004512 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004513#endif
4514#ifdef EAI_BADFLAGS
Fred Drake4baedc12002-04-01 14:53:37 +00004515 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004516#endif
4517#ifdef EAI_FAIL
Fred Drake4baedc12002-04-01 14:53:37 +00004518 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004519#endif
4520#ifdef EAI_FAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004521 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004522#endif
4523#ifdef EAI_MEMORY
Fred Drake4baedc12002-04-01 14:53:37 +00004524 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004525#endif
4526#ifdef EAI_NODATA
Fred Drake4baedc12002-04-01 14:53:37 +00004527 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004528#endif
4529#ifdef EAI_NONAME
Fred Drake4baedc12002-04-01 14:53:37 +00004530 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004531#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004532#ifdef EAI_OVERFLOW
4533 PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);
4534#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004535#ifdef EAI_SERVICE
Fred Drake4baedc12002-04-01 14:53:37 +00004536 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004537#endif
4538#ifdef EAI_SOCKTYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004539 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004540#endif
4541#ifdef EAI_SYSTEM
Fred Drake4baedc12002-04-01 14:53:37 +00004542 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004543#endif
4544#ifdef EAI_BADHINTS
Fred Drake4baedc12002-04-01 14:53:37 +00004545 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004546#endif
4547#ifdef EAI_PROTOCOL
Fred Drake4baedc12002-04-01 14:53:37 +00004548 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004549#endif
4550#ifdef EAI_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00004551 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004552#endif
4553#ifdef AI_PASSIVE
Fred Drake4baedc12002-04-01 14:53:37 +00004554 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004555#endif
4556#ifdef AI_CANONNAME
Fred Drake4baedc12002-04-01 14:53:37 +00004557 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004558#endif
4559#ifdef AI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004560 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004561#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004562#ifdef AI_NUMERICSERV
4563 PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);
4564#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004565#ifdef AI_MASK
Fred Drake4baedc12002-04-01 14:53:37 +00004566 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004567#endif
4568#ifdef AI_ALL
Fred Drake4baedc12002-04-01 14:53:37 +00004569 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004570#endif
4571#ifdef AI_V4MAPPED_CFG
Fred Drake4baedc12002-04-01 14:53:37 +00004572 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004573#endif
4574#ifdef AI_ADDRCONFIG
Fred Drake4baedc12002-04-01 14:53:37 +00004575 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004576#endif
4577#ifdef AI_V4MAPPED
Fred Drake4baedc12002-04-01 14:53:37 +00004578 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004579#endif
4580#ifdef AI_DEFAULT
Fred Drake4baedc12002-04-01 14:53:37 +00004581 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004582#endif
4583#ifdef NI_MAXHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004584 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004585#endif
4586#ifdef NI_MAXSERV
Fred Drake4baedc12002-04-01 14:53:37 +00004587 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004588#endif
4589#ifdef NI_NOFQDN
Fred Drake4baedc12002-04-01 14:53:37 +00004590 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004591#endif
4592#ifdef NI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004593 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004594#endif
4595#ifdef NI_NAMEREQD
Fred Drake4baedc12002-04-01 14:53:37 +00004596 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004597#endif
4598#ifdef NI_NUMERICSERV
Fred Drake4baedc12002-04-01 14:53:37 +00004599 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004600#endif
4601#ifdef NI_DGRAM
Fred Drake4baedc12002-04-01 14:53:37 +00004602 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004603#endif
4604
Martin v. Löwis94681fc2003-11-27 19:40:22 +00004605 /* shutdown() parameters */
4606#ifdef SHUT_RD
4607 PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);
4608#elif defined(SD_RECEIVE)
4609 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
4610#else
4611 PyModule_AddIntConstant(m, "SHUT_RD", 0);
4612#endif
4613#ifdef SHUT_WR
4614 PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);
4615#elif defined(SD_SEND)
4616 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
4617#else
4618 PyModule_AddIntConstant(m, "SHUT_WR", 1);
4619#endif
4620#ifdef SHUT_RDWR
4621 PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);
4622#elif defined(SD_BOTH)
4623 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
4624#else
4625 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
4626#endif
4627
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004628 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00004629#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
4630 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004631#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004632}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004633
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004634
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004635#ifndef HAVE_INET_PTON
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004636
4637/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004638/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004639
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004640int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004641inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004642{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004643 if (af == AF_INET) {
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004644 long packed_addr;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004645 packed_addr = inet_addr(src);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004646 if (packed_addr == INADDR_NONE)
4647 return 0;
4648 memcpy(dst, &packed_addr, 4);
4649 return 1;
4650 }
4651 /* Should set errno to EAFNOSUPPORT */
4652 return -1;
4653}
4654
Martin v. Löwisc925b1532001-07-21 09:42:15 +00004655const char *
4656inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004657{
4658 if (af == AF_INET) {
4659 struct in_addr packed_addr;
4660 if (size < 16)
4661 /* Should set errno to ENOSPC. */
4662 return NULL;
4663 memcpy(&packed_addr, src, sizeof(packed_addr));
4664 return strncpy(dst, inet_ntoa(packed_addr), size);
4665 }
4666 /* Should set errno to EAFNOSUPPORT */
4667 return NULL;
4668}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004669
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004670#endif