blob: f06e253e7668a3abeb8439da0660cab4bee73d8c [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
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000289#ifndef HAVE_INET_PTON
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000290int inet_pton(int af, const char *src, void *dst);
Martin v. Löwisc925b1532001-07-21 09:42:15 +0000291const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +0000292#endif
293
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000294#ifdef __APPLE__
295/* On OS X, getaddrinfo returns no error indication of lookup
296 failure, so we must use the emulation instead of the libinfo
297 implementation. Unfortunately, performing an autoconf test
298 for this bug would require DNS access for the machine performing
299 the configuration, which is not acceptable. Therefore, we
300 determine the bug just by checking for __APPLE__. If this bug
301 gets ever fixed, perhaps checking for sys/version.h would be
302 appropriate, which is 10/0 on the system with the bug. */
Jack Jansen84262fb2002-07-02 14:40:42 +0000303#ifndef HAVE_GETNAMEINFO
304/* This bug seems to be fixed in Jaguar. Ths easiest way I could
305 Find to check for Jaguar is that it has getnameinfo(), which
306 older releases don't have */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000307#undef HAVE_GETADDRINFO
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000308#endif
Jack Jansen84262fb2002-07-02 14:40:42 +0000309#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000310
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000311/* I know this is a bad practice, but it is the easiest... */
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000312#if !defined(HAVE_GETADDRINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000313/* avoid clashes with the C library definition of the symbol. */
314#define getaddrinfo fake_getaddrinfo
315#define gai_strerror fake_gai_strerror
316#define freeaddrinfo fake_freeaddrinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000317#include "getaddrinfo.c"
318#endif
Martin v. Löwisae26dc22001-08-03 10:02:29 +0000319#if !defined(HAVE_GETNAMEINFO)
Martin v. Löwisfccac2e2003-05-01 05:20:46 +0000320#define getnameinfo fake_getnameinfo
Martin v. Löwis01dfdb32001-06-23 16:30:13 +0000321#include "getnameinfo.c"
322#endif
323
Guido van Rossumbcc20741998-08-04 22:53:56 +0000324#if defined(MS_WINDOWS) || defined(__BEOS__)
325/* BeOS suffers from the same socket dichotomy as Win32... - [cjh] */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000326/* seem to be a few differences in the API */
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000327#define SOCKETCLOSE closesocket
Guido van Rossumbe32c891996-06-20 16:25:29 +0000328#define NO_DUP /* Actually it exists on NT 3.5, but what the heck... */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000329#endif
330
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000331#ifdef MS_WIN32
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000332#define EAFNOSUPPORT WSAEAFNOSUPPORT
333#define snprintf _snprintf
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000334#endif
Fred Drakea04eaad2000-06-30 02:46:07 +0000335
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000336#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000337#define SOCKETCLOSE soclose
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000338#define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000339#endif
340
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +0000341#ifndef SOCKETCLOSE
342#define SOCKETCLOSE close
343#endif
344
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000345#ifdef __VMS
346/* TCP/IP Services for VMS uses a maximum send/revc buffer length of 65535 */
347#define SEGMENT_SIZE 65535
348#endif
349
Hye-Shik Chang81268602004-02-02 06:05:24 +0000350#if defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)
351#define USE_BLUETOOTH 1
352#if defined(__FreeBSD__)
353#define BTPROTO_L2CAP BLUETOOTH_PROTO_L2CAP
354#define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM
355#define sockaddr_l2 sockaddr_l2cap
356#define sockaddr_rc sockaddr_rfcomm
357#define _BT_SOCKADDR_MEMB(s, proto) &((s)->sock_addr)
358#define _BT_L2_MEMB(sa, memb) ((sa)->l2cap_##memb)
359#define _BT_RC_MEMB(sa, memb) ((sa)->rfcomm_##memb)
360#else
Anthony Baxter5d7c0672004-02-16 05:35:28 +0000361#define _BT_SOCKADDR_MEMB(s, proto) (&((s)->sock_addr).bt_##proto)
Hye-Shik Chang81268602004-02-02 06:05:24 +0000362#define _BT_L2_MEMB(sa, memb) ((sa)->l2_##memb)
363#define _BT_RC_MEMB(sa, memb) ((sa)->rc_##memb)
364#define _BT_SCO_MEMB(sa, memb) ((sa)->sco_##memb)
365#endif
366#endif
367
Martin v. Löwise9416172003-05-03 10:12:45 +0000368/*
369 * Constants for getnameinfo()
370 */
371#if !defined(NI_MAXHOST)
372#define NI_MAXHOST 1025
373#endif
374#if !defined(NI_MAXSERV)
375#define NI_MAXSERV 32
376#endif
377
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000378/* XXX There's a problem here: *static* functions are not supposed to have
379 a Py prefix (or use CapitalizedWords). Later... */
380
Guido van Rossum30a685f1991-06-27 15:51:29 +0000381/* Global variable holding the exception type for errors detected
382 by this module (but not argument type or memory errors, etc.). */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000383static PyObject *socket_error;
384static PyObject *socket_herror;
385static PyObject *socket_gaierror;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000386static PyObject *socket_timeout;
Guido van Rossum30a685f1991-06-27 15:51:29 +0000387
Guido van Rossum48a680c2001-03-02 06:34:14 +0000388#ifdef RISCOS
389/* Global variable which is !=0 if Python is running in a RISC OS taskwindow */
390static int taskwindow;
391#endif
392
Tim Peters643a7fc2002-02-17 04:13:21 +0000393/* A forward reference to the socket type object.
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000394 The sock_type variable contains pointers to various functions,
395 some of which call new_sockobject(), which uses sock_type, so
Tim Peters643a7fc2002-02-17 04:13:21 +0000396 there has to be a circular reference. */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000397static PyTypeObject sock_type;
Guido van Rossum48a680c2001-03-02 06:34:14 +0000398
Guido van Rossum30a685f1991-06-27 15:51:29 +0000399/* Convenience function to raise an error according to errno
400 and return a NULL pointer from a function. */
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000401
Guido van Rossum73624e91994-10-10 17:59:00 +0000402static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000403set_error(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000404{
Guido van Rossum8d665e61996-06-26 18:22:49 +0000405#ifdef MS_WINDOWS
Mark Hammond46a733d2000-07-24 01:45:11 +0000406 int err_no = WSAGetLastError();
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000407 static struct {
408 int no;
409 const char *msg;
410 } *msgp, msgs[] = {
411 {WSAEINTR, "Interrupted system call"},
412 {WSAEBADF, "Bad file descriptor"},
413 {WSAEACCES, "Permission denied"},
414 {WSAEFAULT, "Bad address"},
415 {WSAEINVAL, "Invalid argument"},
416 {WSAEMFILE, "Too many open files"},
417 {WSAEWOULDBLOCK,
418 "The socket operation could not complete "
419 "without blocking"},
420 {WSAEINPROGRESS, "Operation now in progress"},
421 {WSAEALREADY, "Operation already in progress"},
422 {WSAENOTSOCK, "Socket operation on non-socket"},
423 {WSAEDESTADDRREQ, "Destination address required"},
424 {WSAEMSGSIZE, "Message too long"},
425 {WSAEPROTOTYPE, "Protocol wrong type for socket"},
426 {WSAENOPROTOOPT, "Protocol not available"},
427 {WSAEPROTONOSUPPORT, "Protocol not supported"},
428 {WSAESOCKTNOSUPPORT, "Socket type not supported"},
429 {WSAEOPNOTSUPP, "Operation not supported"},
430 {WSAEPFNOSUPPORT, "Protocol family not supported"},
431 {WSAEAFNOSUPPORT, "Address family not supported"},
432 {WSAEADDRINUSE, "Address already in use"},
433 {WSAEADDRNOTAVAIL, "Can't assign requested address"},
434 {WSAENETDOWN, "Network is down"},
435 {WSAENETUNREACH, "Network is unreachable"},
436 {WSAENETRESET, "Network dropped connection on reset"},
437 {WSAECONNABORTED, "Software caused connection abort"},
438 {WSAECONNRESET, "Connection reset by peer"},
439 {WSAENOBUFS, "No buffer space available"},
440 {WSAEISCONN, "Socket is already connected"},
441 {WSAENOTCONN, "Socket is not connected"},
442 {WSAESHUTDOWN, "Can't send after socket shutdown"},
443 {WSAETOOMANYREFS, "Too many references: can't splice"},
444 {WSAETIMEDOUT, "Operation timed out"},
445 {WSAECONNREFUSED, "Connection refused"},
446 {WSAELOOP, "Too many levels of symbolic links"},
447 {WSAENAMETOOLONG, "File name too long"},
448 {WSAEHOSTDOWN, "Host is down"},
449 {WSAEHOSTUNREACH, "No route to host"},
450 {WSAENOTEMPTY, "Directory not empty"},
451 {WSAEPROCLIM, "Too many processes"},
452 {WSAEUSERS, "Too many users"},
453 {WSAEDQUOT, "Disc quota exceeded"},
454 {WSAESTALE, "Stale NFS file handle"},
455 {WSAEREMOTE, "Too many levels of remote in path"},
456 {WSASYSNOTREADY, "Network subsystem is unvailable"},
457 {WSAVERNOTSUPPORTED, "WinSock version is not supported"},
458 {WSANOTINITIALISED,
459 "Successful WSAStartup() not yet performed"},
460 {WSAEDISCON, "Graceful shutdown in progress"},
461 /* Resolver errors */
462 {WSAHOST_NOT_FOUND, "No such host is known"},
463 {WSATRY_AGAIN, "Host not found, or server failed"},
464 {WSANO_RECOVERY, "Unexpected server error encountered"},
465 {WSANO_DATA, "Valid name without requested data"},
466 {WSANO_ADDRESS, "No address, look for MX record"},
467 {0, NULL}
468 };
Mark Hammond46a733d2000-07-24 01:45:11 +0000469 if (err_no) {
Guido van Rossum73624e91994-10-10 17:59:00 +0000470 PyObject *v;
Mark Hammond46a733d2000-07-24 01:45:11 +0000471 const char *msg = "winsock error";
Guido van Rossum48a680c2001-03-02 06:34:14 +0000472
Mark Hammond46a733d2000-07-24 01:45:11 +0000473 for (msgp = msgs; msgp->msg; msgp++) {
474 if (err_no == msgp->no) {
475 msg = msgp->msg;
476 break;
477 }
478 }
479
480 v = Py_BuildValue("(is)", err_no, msg);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000481 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000482 PyErr_SetObject(socket_error, v);
Guido van Rossum73624e91994-10-10 17:59:00 +0000483 Py_DECREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000484 }
485 return NULL;
486 }
487 else
488#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000489
Andrew MacIntyreba43e872002-03-03 03:03:52 +0000490#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000491 if (sock_errno() != NO_ERROR) {
492 APIRET rc;
493 ULONG msglen;
494 char outbuf[100];
495 int myerrorcode = sock_errno();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000496
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000497 /* Retrieve socket-related error message from MPTN.MSG file */
498 rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
499 myerrorcode - SOCBASEERR + 26,
500 "mptn.msg",
501 &msglen);
502 if (rc == NO_ERROR) {
503 PyObject *v;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000504
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000505 /* OS/2 doesn't guarantee a terminator */
506 outbuf[msglen] = '\0';
507 if (strlen(outbuf) > 0) {
508 /* If non-empty msg, trim CRLF */
509 char *lastc = &outbuf[ strlen(outbuf)-1 ];
510 while (lastc > outbuf && isspace(*lastc)) {
511 /* Trim trailing whitespace (CRLF) */
512 *lastc-- = '\0';
513 }
514 }
515 v = Py_BuildValue("(is)", myerrorcode, outbuf);
516 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000517 PyErr_SetObject(socket_error, v);
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000518 Py_DECREF(v);
519 }
520 return NULL;
521 }
522 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000523#endif
524
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000525#if defined(RISCOS)
526 if (_inet_error.errnum != NULL) {
527 PyObject *v;
528 v = Py_BuildValue("(is)", errno, _inet_err());
529 if (v != NULL) {
530 PyErr_SetObject(socket_error, v);
531 Py_DECREF(v);
532 }
533 return NULL;
534 }
535#endif
536
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000537 return PyErr_SetFromErrno(socket_error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000538}
539
Guido van Rossum30a685f1991-06-27 15:51:29 +0000540
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000541static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000542set_herror(int h_error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000543{
544 PyObject *v;
545
546#ifdef HAVE_HSTRERROR
547 v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
548#else
549 v = Py_BuildValue("(is)", h_error, "host not found");
550#endif
551 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000552 PyErr_SetObject(socket_herror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000553 Py_DECREF(v);
554 }
555
556 return NULL;
557}
558
559
560static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000561set_gaierror(int error)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000562{
563 PyObject *v;
564
Martin v. Löwis272cb402002-03-01 08:31:07 +0000565#ifdef EAI_SYSTEM
566 /* EAI_SYSTEM is not available on Windows XP. */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000567 if (error == EAI_SYSTEM)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000568 return set_error();
Martin v. Löwis272cb402002-03-01 08:31:07 +0000569#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000570
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000571#ifdef HAVE_GAI_STRERROR
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000572 v = Py_BuildValue("(is)", error, gai_strerror(error));
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000573#else
574 v = Py_BuildValue("(is)", error, "getaddrinfo failed");
575#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000576 if (v != NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000577 PyErr_SetObject(socket_gaierror, v);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000578 Py_DECREF(v);
579 }
580
581 return NULL;
582}
583
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000584/* Function to perform the setting of socket blocking mode
585 internally. block = (1 | 0). */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000586static int
587internal_setblocking(PySocketSockObject *s, int block)
588{
589#ifndef RISCOS
590#ifndef MS_WINDOWS
591 int delay_flag;
592#endif
593#endif
594
595 Py_BEGIN_ALLOW_THREADS
596#ifdef __BEOS__
597 block = !block;
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000598 setsockopt(s->sock_fd, SOL_SOCKET, SO_NONBLOCK,
599 (void *)(&block), sizeof(int));
Guido van Rossum67f7a382002-06-06 21:08:16 +0000600#else
601#ifndef RISCOS
602#ifndef MS_WINDOWS
603#if defined(PYOS_OS2) && !defined(PYCC_GCC)
604 block = !block;
605 ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000606#elif defined(__VMS)
607 block = !block;
608 ioctl(s->sock_fd, FIONBIO, (char *)&block);
609#else /* !PYOS_OS2 && !_VMS */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000610 delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
611 if (block)
Neal Norwitz39d22e52002-11-02 19:55:21 +0000612 delay_flag &= (~O_NONBLOCK);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000613 else
Neal Norwitz39d22e52002-11-02 19:55:21 +0000614 delay_flag |= O_NONBLOCK;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000615 fcntl(s->sock_fd, F_SETFL, delay_flag);
616#endif /* !PYOS_OS2 */
617#else /* MS_WINDOWS */
618 block = !block;
619 ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
620#endif /* MS_WINDOWS */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000621#else /* RISCOS */
622 block = !block;
623 socketioctl(s->sock_fd, FIONBIO, (u_long*)&block);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000624#endif /* RISCOS */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000625#endif /* __BEOS__ */
Guido van Rossum67f7a382002-06-06 21:08:16 +0000626 Py_END_ALLOW_THREADS
627
628 /* Since these don't return anything */
629 return 1;
630}
631
Guido van Rossum11ba0942002-06-13 15:07:44 +0000632/* Do a select() on the socket, if necessary (sock_timeout > 0).
633 The argument writing indicates the direction.
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000634 This does not raise an exception; we'll let our caller do that
635 after they've reacquired the interpreter lock.
636 Returns 1 on timeout, 0 otherwise. */
637static int
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000638internal_select(PySocketSockObject *s, int writing)
Guido van Rossum67f7a382002-06-06 21:08:16 +0000639{
640 fd_set fds;
641 struct timeval tv;
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000642 int n;
Guido van Rossum11ba0942002-06-13 15:07:44 +0000643
Guido van Rossumad654902002-07-19 12:44:59 +0000644 /* Nothing to do unless we're in timeout mode (not non-blocking) */
Guido van Rossum11ba0942002-06-13 15:07:44 +0000645 if (s->sock_timeout <= 0.0)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000646 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000647
Guido van Rossumad654902002-07-19 12:44:59 +0000648 /* Guard against closed socket */
649 if (s->sock_fd < 0)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000650 return 0;
Guido van Rossumad654902002-07-19 12:44:59 +0000651
Guido van Rossum67f7a382002-06-06 21:08:16 +0000652 /* Construct the arguments to select */
653 tv.tv_sec = (int)s->sock_timeout;
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000654 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000655 FD_ZERO(&fds);
656 FD_SET(s->sock_fd, &fds);
657
658 /* See if the socket is ready */
Guido van Rossumb9e916a2002-06-07 01:42:47 +0000659 if (writing)
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000660 n = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
Guido van Rossum67f7a382002-06-06 21:08:16 +0000661 else
Raymond Hettingeref7343c2003-06-29 03:08:05 +0000662 n = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
663 if (n == 0)
664 return 1;
665 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000666}
667
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000668/* Initialize a new socket object. */
669
Tim Petersa12b4cf2002-07-18 22:38:44 +0000670static double defaulttimeout = -1.0; /* Default timeout for new sockets */
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000671
Mark Hammond62b1ab12002-07-23 06:31:15 +0000672PyMODINIT_FUNC
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000673init_sockobject(PySocketSockObject *s,
674 SOCKET_T fd, int family, int type, int proto)
675{
676#ifdef RISCOS
677 int block = 1;
678#endif
679 s->sock_fd = fd;
680 s->sock_family = family;
681 s->sock_type = type;
682 s->sock_proto = proto;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000683 s->sock_timeout = defaulttimeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +0000684
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000685 s->errorhandler = &set_error;
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000686
687 if (defaulttimeout >= 0.0)
688 internal_setblocking(s, 0);
689
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000690#ifdef RISCOS
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000691 if (taskwindow)
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000692 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000693#endif
694}
695
696
Guido van Rossum30a685f1991-06-27 15:51:29 +0000697/* Create a new socket object.
698 This just creates the object and initializes it.
699 If the creation fails, return NULL and set an exception (implicit
700 in NEWOBJ()). */
701
Guido van Rossum73624e91994-10-10 17:59:00 +0000702static PySocketSockObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000703new_sockobject(SOCKET_T fd, int family, int type, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000704{
Guido van Rossum73624e91994-10-10 17:59:00 +0000705 PySocketSockObject *s;
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000706 s = (PySocketSockObject *)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000707 PyType_GenericNew(&sock_type, NULL, NULL);
Guido van Rossum384ca9c2001-10-27 22:20:47 +0000708 if (s != NULL)
709 init_sockobject(s, fd, family, type, proto);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000710 return s;
711}
712
Guido van Rossum30a685f1991-06-27 15:51:29 +0000713
Guido van Rossum48a680c2001-03-02 06:34:14 +0000714/* Lock to allow python interpreter to continue, but only allow one
Just van Rossum1040d2c2003-05-09 07:53:18 +0000715 thread to be in gethostbyname or getaddrinfo */
716#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
717PyThread_type_lock netdb_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000718#endif
719
720
Guido van Rossum30a685f1991-06-27 15:51:29 +0000721/* Convert a string specifying a host name or one of a few symbolic
722 names to a numeric IP address. This usually calls gethostbyname()
723 to do the work; the names "" and "<broadcast>" are special.
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000724 Return the length (IPv4 should be 4 bytes), or negative if
Guido van Rossum30a685f1991-06-27 15:51:29 +0000725 an error occurred; then an exception is raised. */
726
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000727static int
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000728setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000729{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000730 struct addrinfo hints, *res;
731 int error;
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000732 int d1, d2, d3, d4;
733 char ch;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000734
Guido van Rossuma376cc51996-12-05 23:43:35 +0000735 memset((void *) addr_ret, '\0', sizeof(*addr_ret));
Guido van Rossum30a685f1991-06-27 15:51:29 +0000736 if (name[0] == '\0') {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000737 int siz;
738 memset(&hints, 0, sizeof(hints));
739 hints.ai_family = af;
740 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
741 hints.ai_flags = AI_PASSIVE;
Just van Rossum1040d2c2003-05-09 07:53:18 +0000742 Py_BEGIN_ALLOW_THREADS
743 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000744 error = getaddrinfo(NULL, "0", &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +0000745 Py_END_ALLOW_THREADS
746 /* We assume that those thread-unsafe getaddrinfo() versions
747 *are* safe regarding their return value, ie. that a
748 subsequent call to getaddrinfo() does not destroy the
749 outcome of the first call. */
750 RELEASE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000751 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000752 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000753 return -1;
754 }
755 switch (res->ai_family) {
756 case AF_INET:
757 siz = 4;
758 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000759#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000760 case AF_INET6:
761 siz = 16;
762 break;
763#endif
764 default:
765 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000766 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000767 "unsupported address family");
768 return -1;
769 }
770 if (res->ai_next) {
Martin v. Löwisf0b11d22001-11-07 08:31:03 +0000771 freeaddrinfo(res);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000772 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000773 "wildcard resolved to multiple address");
774 return -1;
775 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000776 if (res->ai_addrlen < addr_ret_size)
777 addr_ret_size = res->ai_addrlen;
778 memcpy(addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000779 freeaddrinfo(res);
780 return siz;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000781 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000782 if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000783 struct sockaddr_in *sin;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +0000784 if (af != AF_INET && af != AF_UNSPEC) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000785 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000786 "address family mismatched");
787 return -1;
788 }
789 sin = (struct sockaddr_in *)addr_ret;
790 memset((void *) sin, '\0', sizeof(*sin));
791 sin->sin_family = AF_INET;
792#ifdef HAVE_SOCKADDR_SA_LEN
793 sin->sin_len = sizeof(*sin);
794#endif
795 sin->sin_addr.s_addr = INADDR_BROADCAST;
796 return sizeof(sin->sin_addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000797 }
Anthony Baxter0e85f9d2003-05-02 15:40:46 +0000798 if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
799 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
800 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
801 struct sockaddr_in *sin;
802 sin = (struct sockaddr_in *)addr_ret;
803 sin->sin_addr.s_addr = htonl(
804 ((long) d1 << 24) | ((long) d2 << 16) |
805 ((long) d3 << 8) | ((long) d4 << 0));
806 sin->sin_family = AF_INET;
807#ifdef HAVE_SOCKADDR_SA_LEN
808 sin->sin_len = sizeof(*sin);
809#endif
810 return 4;
811 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000812 memset(&hints, 0, sizeof(hints));
813 hints.ai_family = af;
Just van Rossum1040d2c2003-05-09 07:53:18 +0000814 Py_BEGIN_ALLOW_THREADS
815 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000816 error = getaddrinfo(name, NULL, &hints, &res);
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000817#if defined(__digital__) && defined(__unix__)
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000818 if (error == EAI_NONAME && af == AF_UNSPEC) {
819 /* On Tru64 V5.1, numeric-to-addr conversion fails
820 if no address family is given. Assume IPv4 for now.*/
821 hints.ai_family = AF_INET;
822 error = getaddrinfo(name, NULL, &hints, &res);
823 }
Martin v. Löwis7c4b5fa2001-10-25 09:04:03 +0000824#endif
Just van Rossum1040d2c2003-05-09 07:53:18 +0000825 Py_END_ALLOW_THREADS
826 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000827 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000828 set_gaierror(error);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000829 return -1;
830 }
Martin v. Löwisddc6f472002-07-28 16:10:31 +0000831 if (res->ai_addrlen < addr_ret_size)
832 addr_ret_size = res->ai_addrlen;
833 memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000834 freeaddrinfo(res);
835 switch (addr_ret->sa_family) {
836 case AF_INET:
837 return 4;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000838#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000839 case AF_INET6:
840 return 16;
Guido van Rossum955becc1999-03-22 20:14:53 +0000841#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000842 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000843 PyErr_SetString(socket_error, "unknown address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000844 return -1;
845 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000846}
847
Guido van Rossum30a685f1991-06-27 15:51:29 +0000848
Guido van Rossum30a685f1991-06-27 15:51:29 +0000849/* Create a string object representing an IP address.
850 This is always a string of the form 'dd.dd.dd.dd' (with variable
851 size numbers). */
852
Guido van Rossum73624e91994-10-10 17:59:00 +0000853static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000854makeipaddr(struct sockaddr *addr, int addrlen)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000855{
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000856 char buf[NI_MAXHOST];
857 int error;
858
859 error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
860 NI_NUMERICHOST);
861 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +0000862 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000863 return NULL;
864 }
Guido van Rossum73624e91994-10-10 17:59:00 +0000865 return PyString_FromString(buf);
Guido van Rossum30a685f1991-06-27 15:51:29 +0000866}
867
868
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000869#ifdef USE_BLUETOOTH
870/* Convert a string representation of a Bluetooth address into a numeric
871 address. Returns the length (6), or raises an exception and returns -1 if
872 an error occurred. */
873
874static int
875setbdaddr(char *name, bdaddr_t *bdaddr)
876{
877 unsigned int b0, b1, b2, b3, b4, b5;
878 char ch;
879 int n;
880
881 n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
882 &b5, &b4, &b3, &b2, &b1, &b0, &ch);
883 if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
884 bdaddr->b[0] = b0;
885 bdaddr->b[1] = b1;
886 bdaddr->b[2] = b2;
887 bdaddr->b[3] = b3;
888 bdaddr->b[4] = b4;
889 bdaddr->b[5] = b5;
890 return 6;
891 } else {
892 PyErr_SetString(socket_error, "bad bluetooth address");
893 return -1;
894 }
895}
896
897/* Create a string representation of the Bluetooth address. This is always a
898 string of the form 'XX:XX:XX:XX:XX:XX' where XX is a two digit hexadecimal
899 value (zero padded if necessary). */
900
901static PyObject *
902makebdaddr(bdaddr_t *bdaddr)
903{
904 char buf[(6 * 2) + 5 + 1];
905
906 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
907 bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
908 bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
909 return PyString_FromString(buf);
910}
911#endif
912
913
Guido van Rossum30a685f1991-06-27 15:51:29 +0000914/* Create an object representing the given socket address,
915 suitable for passing it back to bind(), connect() etc.
916 The family field of the sockaddr structure is inspected
917 to determine what kind of address it really is. */
918
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000919/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +0000920static PyObject *
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000921makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto)
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000922{
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000923 if (addrlen == 0) {
924 /* No address -- may be recvfrom() from known socket */
Guido van Rossum73624e91994-10-10 17:59:00 +0000925 Py_INCREF(Py_None);
926 return Py_None;
Guido van Rossum25bec8c1992-08-05 19:00:45 +0000927 }
928
Guido van Rossumbcc20741998-08-04 22:53:56 +0000929#ifdef __BEOS__
Guido van Rossum2c8bcb82000-04-25 21:34:53 +0000930 /* XXX: BeOS version of accept() doesn't set family correctly */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000931 addr->sa_family = AF_INET;
932#endif
933
Guido van Rossum30a685f1991-06-27 15:51:29 +0000934 switch (addr->sa_family) {
935
936 case AF_INET:
937 {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000938 struct sockaddr_in *a;
939 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
Barry Warsaw752300b1997-01-03 17:18:10 +0000940 PyObject *ret = NULL;
941 if (addrobj) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000942 a = (struct sockaddr_in *)addr;
Barry Warsaw752300b1997-01-03 17:18:10 +0000943 ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
944 Py_DECREF(addrobj);
945 }
Guido van Rossum6f5afc91993-02-05 09:46:15 +0000946 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000947 }
Guido van Rossum30a685f1991-06-27 15:51:29 +0000948
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +0000949#if defined(AF_UNIX)
Guido van Rossum30a685f1991-06-27 15:51:29 +0000950 case AF_UNIX:
951 {
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000952 struct sockaddr_un *a = (struct sockaddr_un *) addr;
Guido van Rossum73624e91994-10-10 17:59:00 +0000953 return PyString_FromString(a->sun_path);
Guido van Rossum6574b3e1991-06-25 21:36:08 +0000954 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000955#endif /* AF_UNIX */
956
Martin v. Löwis44ddbde2001-12-02 10:15:37 +0000957#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000958 case AF_INET6:
959 {
960 struct sockaddr_in6 *a;
961 PyObject *addrobj = makeipaddr(addr, sizeof(*a));
962 PyObject *ret = NULL;
963 if (addrobj) {
964 a = (struct sockaddr_in6 *)addr;
Guido van Rossum3eede5a2002-06-07 02:08:35 +0000965 ret = Py_BuildValue("Oiii",
966 addrobj,
967 ntohs(a->sin6_port),
968 a->sin6_flowinfo,
969 a->sin6_scope_id);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +0000970 Py_DECREF(addrobj);
971 }
972 return ret;
973 }
Jeremy Hylton22308652001-02-02 03:23:09 +0000974#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +0000975
Martin v. Löwis558d9bf2004-06-03 09:24:42 +0000976#ifdef USE_BLUETOOTH
977 case AF_BLUETOOTH:
978 switch (proto) {
979
980 case BTPROTO_L2CAP:
981 {
982 struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
983 PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
984 PyObject *ret = NULL;
985 if (addrobj) {
986 ret = Py_BuildValue("Oi",
987 addrobj,
988 _BT_L2_MEMB(a, psm));
989 Py_DECREF(addrobj);
990 }
991 return ret;
992 }
993
994 case BTPROTO_RFCOMM:
995 {
996 struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
997 PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
998 PyObject *ret = NULL;
999 if (addrobj) {
1000 ret = Py_BuildValue("Oi",
1001 addrobj,
1002 _BT_RC_MEMB(a, channel));
1003 Py_DECREF(addrobj);
1004 }
1005 return ret;
1006 }
1007
1008#if !defined(__FreeBSD__)
1009 case BTPROTO_SCO:
1010 {
1011 struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
1012 return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
1013 }
1014#endif
1015
1016 }
1017#endif
1018
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001019#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001020 case AF_PACKET:
1021 {
1022 struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
1023 char *ifname = "";
1024 struct ifreq ifr;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001025 /* need to look up interface name give index */
1026 if (a->sll_ifindex) {
Jeremy Hylton22308652001-02-02 03:23:09 +00001027 ifr.ifr_ifindex = a->sll_ifindex;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001028 if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
Jeremy Hylton22308652001-02-02 03:23:09 +00001029 ifname = ifr.ifr_name;
Jeremy Hylton22308652001-02-02 03:23:09 +00001030 }
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001031 return Py_BuildValue("shbhs#",
1032 ifname,
1033 ntohs(a->sll_protocol),
1034 a->sll_pkttype,
1035 a->sll_hatype,
1036 a->sll_addr,
1037 a->sll_halen);
Jeremy Hylton22308652001-02-02 03:23:09 +00001038 }
1039#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001040
Guido van Rossum30a685f1991-06-27 15:51:29 +00001041 /* More cases here... */
1042
1043 default:
Guido van Rossumaa948df1997-05-07 17:41:48 +00001044 /* If we don't know the address family, don't raise an
1045 exception -- return it as a tuple. */
1046 return Py_BuildValue("is#",
1047 addr->sa_family,
1048 addr->sa_data,
1049 sizeof(addr->sa_data));
Guido van Rossum25bec8c1992-08-05 19:00:45 +00001050
Guido van Rossum30a685f1991-06-27 15:51:29 +00001051 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001052}
1053
Guido van Rossum30a685f1991-06-27 15:51:29 +00001054
1055/* Parse a socket address argument according to the socket object's
1056 address family. Return 1 if the address was in the proper format,
1057 0 of not. The address is returned through addr_ret, its length
1058 through len_ret. */
1059
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001060static int
Guido van Rossum48a680c2001-03-02 06:34:14 +00001061getsockaddrarg(PySocketSockObject *s, PyObject *args,
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001062 struct sockaddr **addr_ret, int *len_ret)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001063{
Guido van Rossum30a685f1991-06-27 15:51:29 +00001064 switch (s->sock_family) {
1065
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001066#if defined(AF_UNIX)
Guido van Rossum30a685f1991-06-27 15:51:29 +00001067 case AF_UNIX:
1068 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001069 struct sockaddr_un* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001070 char *path;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001071 int len;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001072 addr = (struct sockaddr_un*)&(s->sock_addr).un;
Guido van Rossum7e488981998-10-08 02:25:24 +00001073 if (!PyArg_Parse(args, "t#", &path, &len))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001074 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001075 if (len > sizeof addr->sun_path) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001076 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +00001077 "AF_UNIX path too long");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001078 return 0;
1079 }
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001080 addr->sun_family = s->sock_family;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001081 memcpy(addr->sun_path, path, len);
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001082 addr->sun_path[len] = 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001083 *addr_ret = (struct sockaddr *) addr;
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001084#if defined(PYOS_OS2)
1085 *len_ret = sizeof(*addr);
1086#else
Guido van Rossum65af28a1996-06-11 18:36:33 +00001087 *len_ret = len + sizeof(*addr) - sizeof(addr->sun_path);
Andrew MacIntyredaedf212004-04-11 12:03:57 +00001088#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001089 return 1;
1090 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001091#endif /* AF_UNIX */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001092
Guido van Rossum30a685f1991-06-27 15:51:29 +00001093 case AF_INET:
1094 {
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001095 struct sockaddr_in* addr;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001096 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001097 int port, result;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001098 addr=(struct sockaddr_in*)&(s->sock_addr).in;
Guido van Rossume4dad902000-12-01 13:13:11 +00001099 if (!PyTuple_Check(args)) {
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001100 PyErr_Format(
1101 PyExc_TypeError,
1102 "getsockaddrarg: "
1103 "AF_INET address must be tuple, not %.500s",
1104 args->ob_type->tp_name);
Guido van Rossume4dad902000-12-01 13:13:11 +00001105 return 0;
1106 }
Martin v. Löwis2548c732003-04-18 10:39:54 +00001107 if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
1108 "idna", &host, &port))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001109 return 0;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001110 result = setipaddr(host, (struct sockaddr *)addr,
1111 sizeof(*addr), AF_INET);
1112 PyMem_Free(host);
1113 if (result < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001114 return 0;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001115 addr->sin_family = AF_INET;
Guido van Rossum644a12b1997-04-09 19:24:53 +00001116 addr->sin_port = htons((short)port);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00001117 *addr_ret = (struct sockaddr *) addr;
1118 *len_ret = sizeof *addr;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001119 return 1;
1120 }
1121
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001122#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001123 case AF_INET6:
1124 {
1125 struct sockaddr_in6* addr;
1126 char *host;
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001127 int port, flowinfo, scope_id, result;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001128 addr = (struct sockaddr_in6*)&(s->sock_addr).in6;
1129 flowinfo = scope_id = 0;
Martin v. Löwis2548c732003-04-18 10:39:54 +00001130 if (!PyArg_ParseTuple(args, "eti|ii",
1131 "idna", &host, &port, &flowinfo,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001132 &scope_id)) {
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001133 return 0;
1134 }
Martin v. Löwis5db099a2003-08-07 11:55:15 +00001135 result = setipaddr(host, (struct sockaddr *)addr,
1136 sizeof(*addr), AF_INET6);
1137 PyMem_Free(host);
1138 if (result < 0)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001139 return 0;
1140 addr->sin6_family = s->sock_family;
1141 addr->sin6_port = htons((short)port);
1142 addr->sin6_flowinfo = flowinfo;
1143 addr->sin6_scope_id = scope_id;
1144 *addr_ret = (struct sockaddr *) addr;
1145 *len_ret = sizeof *addr;
1146 return 1;
1147 }
1148#endif
1149
Hye-Shik Chang81268602004-02-02 06:05:24 +00001150#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00001151 case AF_BLUETOOTH:
1152 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001153 switch (s->sock_proto) {
1154 case BTPROTO_L2CAP:
Martin v. Löwis12af0482004-01-31 12:34:17 +00001155 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001156 struct sockaddr_l2 *addr = (struct sockaddr_l2 *) _BT_SOCKADDR_MEMB(s, l2);
1157 char *straddr;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001158
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001159 _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
1160 if (!PyArg_ParseTuple(args, "si", &straddr,
1161 &_BT_L2_MEMB(addr, psm))) {
1162 PyErr_SetString(socket_error, "getsockaddrarg: "
1163 "wrong format");
Martin v. Löwis12af0482004-01-31 12:34:17 +00001164 return 0;
1165 }
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001166 if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
1167 return 0;
1168
1169 *addr_ret = (struct sockaddr *) addr;
1170 *len_ret = sizeof *addr;
1171 return 1;
1172 }
1173 case BTPROTO_RFCOMM:
1174 {
1175 struct sockaddr_rc *addr = (struct sockaddr_rc *) _BT_SOCKADDR_MEMB(s, rc);
1176 char *straddr;
1177
1178 _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
1179 if (!PyArg_ParseTuple(args, "si", &straddr,
1180 &_BT_RC_MEMB(addr, channel))) {
1181 PyErr_SetString(socket_error, "getsockaddrarg: "
1182 "wrong format");
1183 return 0;
1184 }
1185 if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
1186 return 0;
1187
1188 *addr_ret = (struct sockaddr *) addr;
1189 *len_ret = sizeof *addr;
1190 return 1;
1191 }
1192#if !defined(__FreeBSD__)
1193 case BTPROTO_SCO:
1194 {
1195 struct sockaddr_sco *addr = (struct sockaddr_sco *) _BT_SOCKADDR_MEMB(s, sco);
1196 char *straddr;
1197
1198 _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
1199 straddr = PyString_AsString(args);
1200 if (straddr == NULL) {
1201 PyErr_SetString(socket_error, "getsockaddrarg: "
1202 "wrong format");
1203 return 0;
1204 }
1205 if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
1206 return 0;
1207
1208 *addr_ret = (struct sockaddr *) addr;
1209 *len_ret = sizeof *addr;
1210 return 1;
1211 }
1212#endif
1213 default:
1214 PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol");
1215 return 0;
Martin v. Löwis12af0482004-01-31 12:34:17 +00001216 }
1217 }
1218#endif
1219
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001220#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001221 case AF_PACKET:
1222 {
1223 struct sockaddr_ll* addr;
1224 struct ifreq ifr;
1225 char *interfaceName;
1226 int protoNumber;
1227 int hatype = 0;
1228 int pkttype = 0;
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001229 char *haddr = NULL;
1230 unsigned int halen = 0;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001231
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001232 if (!PyArg_ParseTuple(args, "si|iis#", &interfaceName,
1233 &protoNumber, &pkttype, &hatype,
1234 &haddr, &halen))
Jeremy Hylton22308652001-02-02 03:23:09 +00001235 return 0;
1236 strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
1237 ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001238 if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001239 s->errorhandler();
Jeremy Hylton22308652001-02-02 03:23:09 +00001240 return 0;
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001241 }
Jeremy Hylton22308652001-02-02 03:23:09 +00001242 addr = &(s->sock_addr.ll);
1243 addr->sll_family = AF_PACKET;
1244 addr->sll_protocol = htons((short)protoNumber);
1245 addr->sll_ifindex = ifr.ifr_ifindex;
1246 addr->sll_pkttype = pkttype;
1247 addr->sll_hatype = hatype;
Andrew M. Kuchlingb8e17172004-07-10 23:39:35 +00001248 if (halen > 8) {
1249 PyErr_SetString(PyExc_ValueError,
1250 "Hardware address must be 8 bytes or less");
1251 return 0;
1252 }
1253 if (halen != 0) {
1254 memcpy(&addr->sll_addr, haddr, halen);
1255 }
1256 addr->sll_halen = halen;
Jeremy Hylton22308652001-02-02 03:23:09 +00001257 *addr_ret = (struct sockaddr *) addr;
1258 *len_ret = sizeof *addr;
1259 return 1;
1260 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001261#endif
1262
Guido van Rossum30a685f1991-06-27 15:51:29 +00001263 /* More cases here... */
1264
1265 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001266 PyErr_SetString(socket_error, "getsockaddrarg: bad family");
Guido van Rossum30a685f1991-06-27 15:51:29 +00001267 return 0;
1268
1269 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001270}
1271
Guido van Rossum30a685f1991-06-27 15:51:29 +00001272
Guido van Rossum48a680c2001-03-02 06:34:14 +00001273/* Get the address length according to the socket object's address family.
Guido van Rossum710e1df1992-06-12 10:39:36 +00001274 Return 1 if the family is known, 0 otherwise. The length is returned
1275 through len_ret. */
1276
1277static int
Peter Schneider-Kamp618e25d2000-07-11 23:00:12 +00001278getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001279{
1280 switch (s->sock_family) {
1281
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00001282#if defined(AF_UNIX)
Guido van Rossum710e1df1992-06-12 10:39:36 +00001283 case AF_UNIX:
1284 {
1285 *len_ret = sizeof (struct sockaddr_un);
1286 return 1;
1287 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00001288#endif /* AF_UNIX */
Guido van Rossum710e1df1992-06-12 10:39:36 +00001289
1290 case AF_INET:
1291 {
1292 *len_ret = sizeof (struct sockaddr_in);
1293 return 1;
1294 }
1295
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00001296#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00001297 case AF_INET6:
1298 {
1299 *len_ret = sizeof (struct sockaddr_in6);
1300 return 1;
1301 }
1302#endif
1303
Hye-Shik Chang81268602004-02-02 06:05:24 +00001304#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00001305 case AF_BLUETOOTH:
1306 {
1307 switch(s->sock_proto)
1308 {
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001309
1310 case BTPROTO_L2CAP:
1311 *len_ret = sizeof (struct sockaddr_l2);
1312 return 1;
1313 case BTPROTO_RFCOMM:
1314 *len_ret = sizeof (struct sockaddr_rc);
1315 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001316#if !defined(__FreeBSD__)
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001317 case BTPROTO_SCO:
1318 *len_ret = sizeof (struct sockaddr_sco);
1319 return 1;
Hye-Shik Chang81268602004-02-02 06:05:24 +00001320#endif
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001321 default:
1322 PyErr_SetString(socket_error, "getsockaddrlen: "
1323 "unknown BT protocol");
1324 return 0;
1325
Martin v. Löwis12af0482004-01-31 12:34:17 +00001326 }
1327 }
1328#endif
1329
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00001330#ifdef HAVE_NETPACKET_PACKET_H
Jeremy Hylton22308652001-02-02 03:23:09 +00001331 case AF_PACKET:
1332 {
1333 *len_ret = sizeof (struct sockaddr_ll);
1334 return 1;
1335 }
1336#endif
Guido van Rossum48a680c2001-03-02 06:34:14 +00001337
Guido van Rossum710e1df1992-06-12 10:39:36 +00001338 /* More cases here... */
1339
1340 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001341 PyErr_SetString(socket_error, "getsockaddrlen: bad family");
Guido van Rossum710e1df1992-06-12 10:39:36 +00001342 return 0;
1343
1344 }
1345}
1346
1347
Guido van Rossum30a685f1991-06-27 15:51:29 +00001348/* s.accept() method */
1349
Guido van Rossum73624e91994-10-10 17:59:00 +00001350static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001351sock_accept(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001352{
1353 char addrbuf[256];
Fred Drakea04eaad2000-06-30 02:46:07 +00001354 SOCKET_T newfd;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001355 socklen_t addrlen;
Barry Warsaw752300b1997-01-03 17:18:10 +00001356 PyObject *sock = NULL;
1357 PyObject *addr = NULL;
1358 PyObject *res = NULL;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001359 int timeout;
Barry Warsaw752300b1997-01-03 17:18:10 +00001360
Guido van Rossum710e1df1992-06-12 10:39:36 +00001361 if (!getsockaddrlen(s, &addrlen))
1362 return NULL;
Fred Drakefd168342001-05-09 19:11:33 +00001363 memset(addrbuf, 0, addrlen);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001364
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001365#ifdef MS_WINDOWS
1366 newfd = INVALID_SOCKET;
1367#else
1368 newfd = -1;
1369#endif
1370
Guido van Rossum73624e91994-10-10 17:59:00 +00001371 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001372 timeout = internal_select(s, 0);
1373 if (!timeout)
1374 newfd = accept(s->sock_fd, (struct sockaddr *) addrbuf,
1375 &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001376 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001377
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001378 if (timeout) {
1379 PyErr_SetString(socket_timeout, "timed out");
1380 return NULL;
1381 }
1382
Fred Drakea04eaad2000-06-30 02:46:07 +00001383#ifdef MS_WINDOWS
1384 if (newfd == INVALID_SOCKET)
1385#else
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001386 if (newfd < 0)
Fred Drakea04eaad2000-06-30 02:46:07 +00001387#endif
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001388 return s->errorhandler();
Barry Warsaw752300b1997-01-03 17:18:10 +00001389
Guido van Rossum30a685f1991-06-27 15:51:29 +00001390 /* Create the new object with unspecified family,
1391 to avoid calls to bind() etc. on it. */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001392 sock = (PyObject *) new_sockobject(newfd,
1393 s->sock_family,
1394 s->sock_type,
1395 s->sock_proto);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001396
Barry Warsaw752300b1997-01-03 17:18:10 +00001397 if (sock == NULL) {
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001398 SOCKETCLOSE(newfd);
Barry Warsaw752300b1997-01-03 17:18:10 +00001399 goto finally;
1400 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00001401 addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001402 addrlen, s->sock_proto);
Jeremy Hyltondbfb6622001-02-02 19:55:17 +00001403 if (addr == NULL)
Barry Warsaw752300b1997-01-03 17:18:10 +00001404 goto finally;
1405
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001406 res = PyTuple_Pack(2, sock, addr);
Barry Warsaw752300b1997-01-03 17:18:10 +00001407
Guido van Rossum67f7a382002-06-06 21:08:16 +00001408finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00001409 Py_XDECREF(sock);
1410 Py_XDECREF(addr);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001411 return res;
1412}
1413
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001414PyDoc_STRVAR(accept_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001415"accept() -> (socket object, address info)\n\
1416\n\
1417Wait for an incoming connection. Return a new socket representing the\n\
1418connection, and the address of the client. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001419info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001420
Guido van Rossum11ba0942002-06-13 15:07:44 +00001421/* s.setblocking(flag) method. Argument:
1422 False -- non-blocking mode; same as settimeout(0)
1423 True -- blocking mode; same as settimeout(None)
1424*/
Guido van Rossume4485b01994-09-07 14:32:49 +00001425
Guido van Rossum73624e91994-10-10 17:59:00 +00001426static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001427sock_setblocking(PySocketSockObject *s, PyObject *arg)
Guido van Rossume4485b01994-09-07 14:32:49 +00001428{
1429 int block;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001430
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001431 block = PyInt_AsLong(arg);
1432 if (block == -1 && PyErr_Occurred())
Guido van Rossume4485b01994-09-07 14:32:49 +00001433 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001434
Guido van Rossum11ba0942002-06-13 15:07:44 +00001435 s->sock_timeout = block ? -1.0 : 0.0;
Guido van Rossume8008f02002-06-07 03:36:20 +00001436 internal_setblocking(s, block);
Guido van Rossume4485b01994-09-07 14:32:49 +00001437
Guido van Rossum73624e91994-10-10 17:59:00 +00001438 Py_INCREF(Py_None);
1439 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +00001440}
Guido van Rossume4485b01994-09-07 14:32:49 +00001441
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001442PyDoc_STRVAR(setblocking_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001443"setblocking(flag)\n\
1444\n\
1445Set the socket to blocking (flag is true) or non-blocking (false).\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001446setblocking(True) is equivalent to settimeout(None);\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001447setblocking(False) is equivalent to settimeout(0.0).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001448
Guido van Rossum11ba0942002-06-13 15:07:44 +00001449/* s.settimeout(timeout) method. Argument:
1450 None -- no timeout, blocking mode; same as setblocking(True)
1451 0.0 -- non-blocking mode; same as setblocking(False)
1452 > 0 -- timeout mode; operations time out after timeout seconds
1453 < 0 -- illegal; raises an exception
1454*/
Guido van Rossum67f7a382002-06-06 21:08:16 +00001455static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001456sock_settimeout(PySocketSockObject *s, PyObject *arg)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001457{
Guido van Rossum11ba0942002-06-13 15:07:44 +00001458 double timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001459
1460 if (arg == Py_None)
Guido van Rossum11ba0942002-06-13 15:07:44 +00001461 timeout = -1.0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001462 else {
Guido van Rossum11ba0942002-06-13 15:07:44 +00001463 timeout = PyFloat_AsDouble(arg);
1464 if (timeout < 0.0) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00001465 if (!PyErr_Occurred())
1466 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00001467 "Timeout value out of range");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001468 return NULL;
1469 }
1470 }
1471
Guido van Rossum11ba0942002-06-13 15:07:44 +00001472 s->sock_timeout = timeout;
1473 internal_setblocking(s, timeout < 0.0);
Guido van Rossum67f7a382002-06-06 21:08:16 +00001474
1475 Py_INCREF(Py_None);
1476 return Py_None;
1477}
1478
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001479PyDoc_STRVAR(settimeout_doc,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001480"settimeout(timeout)\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001481\n\
Guido van Rossum11ba0942002-06-13 15:07:44 +00001482Set a timeout on socket operations. 'timeout' can be a float,\n\
1483giving in seconds, or None. Setting a timeout of None disables\n\
1484the timeout feature and is equivalent to setblocking(1).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001485Setting a timeout of zero is the same as setblocking(0).");
Guido van Rossum67f7a382002-06-06 21:08:16 +00001486
Guido van Rossum3eede5a2002-06-07 02:08:35 +00001487/* s.gettimeout() method.
1488 Returns the timeout associated with a socket. */
Guido van Rossum67f7a382002-06-06 21:08:16 +00001489static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001490sock_gettimeout(PySocketSockObject *s)
Guido van Rossum67f7a382002-06-06 21:08:16 +00001491{
1492 if (s->sock_timeout < 0.0) {
1493 Py_INCREF(Py_None);
1494 return Py_None;
1495 }
1496 else
1497 return PyFloat_FromDouble(s->sock_timeout);
1498}
1499
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001500PyDoc_STRVAR(gettimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001501"gettimeout() -> timeout\n\
Guido van Rossum67f7a382002-06-06 21:08:16 +00001502\n\
1503Returns the timeout in floating seconds associated with socket \n\
1504operations. A timeout of None indicates that timeouts on socket \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001505operations are disabled.");
Guido van Rossume4485b01994-09-07 14:32:49 +00001506
Guido van Rossum48a680c2001-03-02 06:34:14 +00001507#ifdef RISCOS
1508/* s.sleeptaskw(1 | 0) method */
1509
1510static PyObject *
Martin v. Löwisa94568a2003-05-10 07:36:56 +00001511sock_sleeptaskw(PySocketSockObject *s,PyObject *arg)
Guido van Rossum48a680c2001-03-02 06:34:14 +00001512{
Guido van Rossum67f7a382002-06-06 21:08:16 +00001513 int block;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00001514 block = PyInt_AsLong(arg);
1515 if (block == -1 && PyErr_Occurred())
Guido van Rossum67f7a382002-06-06 21:08:16 +00001516 return NULL;
1517 Py_BEGIN_ALLOW_THREADS
1518 socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);
1519 Py_END_ALLOW_THREADS
Guido van Rossum48a680c2001-03-02 06:34:14 +00001520
Guido van Rossum67f7a382002-06-06 21:08:16 +00001521 Py_INCREF(Py_None);
1522 return Py_None;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001523}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001524PyDoc_STRVAR(sleeptaskw_doc,
Guido van Rossum48a680c2001-03-02 06:34:14 +00001525"sleeptaskw(flag)\n\
1526\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001527Allow sleeps in taskwindows.");
Guido van Rossum48a680c2001-03-02 06:34:14 +00001528#endif
1529
1530
Guido van Rossumaee08791992-09-08 09:05:33 +00001531/* s.setsockopt() method.
1532 With an integer third argument, sets an integer option.
1533 With a string third argument, sets an option from a buffer;
1534 use optional built-in module 'struct' to encode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001535
Guido van Rossum73624e91994-10-10 17:59:00 +00001536static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001537sock_setsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001538{
1539 int level;
1540 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001541 int res;
Guido van Rossumaee08791992-09-08 09:05:33 +00001542 char *buf;
1543 int buflen;
1544 int flag;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001545
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001546 if (PyArg_ParseTuple(args, "iii:setsockopt",
1547 &level, &optname, &flag)) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001548 buf = (char *) &flag;
1549 buflen = sizeof flag;
1550 }
1551 else {
Guido van Rossum73624e91994-10-10 17:59:00 +00001552 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001553 if (!PyArg_ParseTuple(args, "iis#:setsockopt",
1554 &level, &optname, &buf, &buflen))
Guido van Rossumaee08791992-09-08 09:05:33 +00001555 return NULL;
1556 }
Thomas Wouters334fb892000-07-25 12:56:38 +00001557 res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001558 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001559 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001560 Py_INCREF(Py_None);
1561 return Py_None;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001562}
1563
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001564PyDoc_STRVAR(setsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001565"setsockopt(level, option, value)\n\
1566\n\
1567Set a socket option. See the Unix manual for level and option.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001568The value argument can either be an integer or a string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001569
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001570
Guido van Rossumaee08791992-09-08 09:05:33 +00001571/* s.getsockopt() method.
1572 With two arguments, retrieves an integer option.
1573 With a third integer argument, retrieves a string buffer of that size;
1574 use optional built-in module 'struct' to decode the string. */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001575
Guido van Rossum73624e91994-10-10 17:59:00 +00001576static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001577sock_getsockopt(PySocketSockObject *s, PyObject *args)
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001578{
1579 int level;
1580 int optname;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001581 int res;
Guido van Rossum73624e91994-10-10 17:59:00 +00001582 PyObject *buf;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001583 socklen_t buflen = 0;
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001584
Guido van Rossumbcc20741998-08-04 22:53:56 +00001585#ifdef __BEOS__
Fred Drake564a6cc2001-05-11 20:12:26 +00001586 /* We have incomplete socket support. */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001587 PyErr_SetString(socket_error, "getsockopt not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00001588 return NULL;
1589#else
1590
Guido van Rossum27fc3c02000-03-24 20:56:56 +00001591 if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
1592 &level, &optname, &buflen))
Guido van Rossumbe32c891996-06-20 16:25:29 +00001593 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001594
Guido van Rossumbe32c891996-06-20 16:25:29 +00001595 if (buflen == 0) {
Guido van Rossumaee08791992-09-08 09:05:33 +00001596 int flag = 0;
Guido van Rossumff3ab422000-04-24 15:16:03 +00001597 socklen_t flagsize = sizeof flag;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001598 res = getsockopt(s->sock_fd, level, optname,
Thomas Wouters334fb892000-07-25 12:56:38 +00001599 (void *)&flag, &flagsize);
Guido van Rossumaee08791992-09-08 09:05:33 +00001600 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001601 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001602 return PyInt_FromLong(flag);
Guido van Rossumaee08791992-09-08 09:05:33 +00001603 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001604#ifdef __VMS
1605 if (buflen > 1024) {
1606#else
Guido van Rossumaee08791992-09-08 09:05:33 +00001607 if (buflen <= 0 || buflen > 1024) {
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001608#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001609 PyErr_SetString(socket_error,
Barry Warsaw752300b1997-01-03 17:18:10 +00001610 "getsockopt buflen out of range");
Guido van Rossumaee08791992-09-08 09:05:33 +00001611 return NULL;
1612 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001613 buf = PyString_FromStringAndSize((char *)NULL, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001614 if (buf == NULL)
1615 return NULL;
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001616 res = getsockopt(s->sock_fd, level, optname,
Fred Drake564a6cc2001-05-11 20:12:26 +00001617 (void *)PyString_AS_STRING(buf), &buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001618 if (res < 0) {
Guido van Rossum73624e91994-10-10 17:59:00 +00001619 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001620 return s->errorhandler();
Guido van Rossumaee08791992-09-08 09:05:33 +00001621 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001622 _PyString_Resize(&buf, buflen);
Guido van Rossumaee08791992-09-08 09:05:33 +00001623 return buf;
Guido van Rossumbcc20741998-08-04 22:53:56 +00001624#endif /* __BEOS__ */
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001625}
1626
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001627PyDoc_STRVAR(getsockopt_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001628"getsockopt(level, option[, buffersize]) -> value\n\
1629\n\
1630Get a socket option. See the Unix manual for level and option.\n\
1631If a nonzero buffersize argument is given, the return value is a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001632string of that length; otherwise it is an integer.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001633
Guido van Rossum0e69587d1992-06-05 15:11:30 +00001634
Fred Drake728819a2000-07-01 03:40:12 +00001635/* s.bind(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001636
Guido van Rossum73624e91994-10-10 17:59:00 +00001637static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001638sock_bind(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001639{
1640 struct sockaddr *addr;
1641 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001642 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001643
Fred Drake728819a2000-07-01 03:40:12 +00001644 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001645 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001646 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001647 res = bind(s->sock_fd, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001648 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001649 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001650 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001651 Py_INCREF(Py_None);
1652 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001653}
1654
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001655PyDoc_STRVAR(bind_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001656"bind(address)\n\
1657\n\
1658Bind the socket to a local address. For IP sockets, the address is a\n\
Jeremy Hylton22308652001-02-02 03:23:09 +00001659pair (host, port); the host must refer to the local host. For raw packet\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001660sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001661
Guido van Rossum30a685f1991-06-27 15:51:29 +00001662
1663/* s.close() method.
1664 Set the file descriptor to -1 so operations tried subsequently
1665 will surely fail. */
1666
Guido van Rossum73624e91994-10-10 17:59:00 +00001667static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001668sock_close(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001669{
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001670 SOCKET_T fd;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001671
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001672 if ((fd = s->sock_fd) != -1) {
1673 s->sock_fd = -1;
Guido van Rossum08481461996-10-12 14:07:22 +00001674 Py_BEGIN_ALLOW_THREADS
Guido van Rossum20d3fc02000-12-18 22:23:44 +00001675 (void) SOCKETCLOSE(fd);
Guido van Rossum08481461996-10-12 14:07:22 +00001676 Py_END_ALLOW_THREADS
1677 }
Guido van Rossum73624e91994-10-10 17:59:00 +00001678 Py_INCREF(Py_None);
1679 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001680}
1681
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001682PyDoc_STRVAR(close_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001683"close()\n\
1684\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001685Close the socket. It cannot be used after this call.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001686
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001687static int
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001688internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
1689 int *timeoutp)
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001690{
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001691 int res, timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001692
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001693 timeout = 0;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001694 res = connect(s->sock_fd, addr, addrlen);
1695
1696#ifdef MS_WINDOWS
1697
1698 if (s->sock_timeout > 0.0) {
1699 if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001700 /* This is a mess. Best solution: trust select */
1701 fd_set fds;
Mark Hammonda57ec932004-08-03 05:06:26 +00001702 fd_set fds_exc;
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001703 struct timeval tv;
1704 tv.tv_sec = (int)s->sock_timeout;
1705 tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
1706 FD_ZERO(&fds);
1707 FD_SET(s->sock_fd, &fds);
Mark Hammonda57ec932004-08-03 05:06:26 +00001708 FD_ZERO(&fds_exc);
1709 FD_SET(s->sock_fd, &fds_exc);
1710 res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001711 if (res == 0) {
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001712 res = WSAEWOULDBLOCK;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001713 timeout = 1;
Mark Hammonda57ec932004-08-03 05:06:26 +00001714 } else if (res > 0) {
1715 if (FD_ISSET(s->sock_fd, &fds))
1716 /* The socket is in the writeable set - this
1717 means connected */
1718 res = 0;
1719 else {
1720 /* As per MS docs, we need to call getsockopt()
1721 to get the underlying error */
1722 int res_size = sizeof res;
1723 /* It must be in the exception set */
1724 assert(FD_ISSET(s->sock_fd, &fds_exc));
1725 if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
1726 (char *)&res, &res_size))
1727 /* getsockopt also clears WSAGetLastError,
1728 so reset it back. */
1729 WSASetLastError(res);
1730 else
1731 res = WSAGetLastError();
1732 }
1733 }
Guido van Rossumb76bdf82003-02-19 17:50:16 +00001734 /* else if (res < 0) an error occurred */
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001735 }
1736 }
1737
1738 if (res < 0)
1739 res = WSAGetLastError();
1740
1741#else
1742
1743 if (s->sock_timeout > 0.0) {
1744 if (res < 0 && errno == EINPROGRESS) {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001745 timeout = internal_select(s, 1);
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001746 res = connect(s->sock_fd, addr, addrlen);
Guido van Rossum12e3c712002-08-08 20:39:30 +00001747 if (res < 0 && errno == EISCONN)
1748 res = 0;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001749 }
1750 }
1751
1752 if (res < 0)
1753 res = errno;
1754
1755#endif
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001756 *timeoutp = timeout;
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001757
1758 return res;
1759}
Guido van Rossum30a685f1991-06-27 15:51:29 +00001760
Fred Drake728819a2000-07-01 03:40:12 +00001761/* s.connect(sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00001762
Guido van Rossum73624e91994-10-10 17:59:00 +00001763static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001764sock_connect(PySocketSockObject *s, PyObject *addro)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001765{
1766 struct sockaddr *addr;
1767 int addrlen;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001768 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001769 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001770
Fred Drake728819a2000-07-01 03:40:12 +00001771 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001772 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001773
Guido van Rossum73624e91994-10-10 17:59:00 +00001774 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001775 res = internal_connect(s, addr, addrlen, &timeout);
Guido van Rossum11ba0942002-06-13 15:07:44 +00001776 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001777
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001778 if (timeout) {
1779 PyErr_SetString(socket_timeout, "timed out");
1780 return NULL;
1781 }
Guido van Rossum7b8bac12002-06-13 16:07:04 +00001782 if (res != 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001783 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001784 Py_INCREF(Py_None);
1785 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001786}
1787
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001788PyDoc_STRVAR(connect_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001789"connect(address)\n\
1790\n\
1791Connect the socket to a remote address. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001792is a pair (host, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001793
Guido van Rossum30a685f1991-06-27 15:51:29 +00001794
Fred Drake728819a2000-07-01 03:40:12 +00001795/* s.connect_ex(sockaddr) method */
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001796
1797static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001798sock_connect_ex(PySocketSockObject *s, PyObject *addro)
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001799{
1800 struct sockaddr *addr;
1801 int addrlen;
1802 int res;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001803 int timeout;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001804
Fred Drake728819a2000-07-01 03:40:12 +00001805 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001806 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00001807
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001808 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00001809 res = internal_connect(s, addr, addrlen, &timeout);
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001810 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00001811
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001812 return PyInt_FromLong((long) res);
1813}
1814
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001815PyDoc_STRVAR(connect_ex_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00001816"connect_ex(address) -> errno\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00001817\n\
1818This is like connect(address), but returns an error code (the errno value)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001819instead of raising an exception when an error occurs.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001820
Guido van Rossumfc4255d1997-11-19 18:57:13 +00001821
Guido van Rossumed233a51992-06-23 09:07:03 +00001822/* s.fileno() method */
1823
Guido van Rossum73624e91994-10-10 17:59:00 +00001824static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001825sock_fileno(PySocketSockObject *s)
Guido van Rossumed233a51992-06-23 09:07:03 +00001826{
Fred Drakea04eaad2000-06-30 02:46:07 +00001827#if SIZEOF_SOCKET_T <= SIZEOF_LONG
Guido van Rossum73624e91994-10-10 17:59:00 +00001828 return PyInt_FromLong((long) s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00001829#else
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001830 return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd);
Fred Drakea04eaad2000-06-30 02:46:07 +00001831#endif
Guido van Rossumed233a51992-06-23 09:07:03 +00001832}
1833
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001834PyDoc_STRVAR(fileno_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001835"fileno() -> integer\n\
1836\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001837Return the integer file descriptor of the socket.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001838
Guido van Rossumed233a51992-06-23 09:07:03 +00001839
Guido van Rossumbe32c891996-06-20 16:25:29 +00001840#ifndef NO_DUP
1841/* s.dup() method */
1842
1843static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001844sock_dup(PySocketSockObject *s)
Guido van Rossumbe32c891996-06-20 16:25:29 +00001845{
Fred Drakea04eaad2000-06-30 02:46:07 +00001846 SOCKET_T newfd;
Guido van Rossumbe32c891996-06-20 16:25:29 +00001847 PyObject *sock;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001848
Guido van Rossumbe32c891996-06-20 16:25:29 +00001849 newfd = dup(s->sock_fd);
1850 if (newfd < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001851 return s->errorhandler();
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001852 sock = (PyObject *) new_sockobject(newfd,
1853 s->sock_family,
1854 s->sock_type,
1855 s->sock_proto);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001856 if (sock == NULL)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00001857 SOCKETCLOSE(newfd);
Guido van Rossumbe32c891996-06-20 16:25:29 +00001858 return sock;
1859}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001860
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001861PyDoc_STRVAR(dup_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001862"dup() -> socket object\n\
1863\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001864Return a new socket object connected to the same system resource.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001865
Guido van Rossumbe32c891996-06-20 16:25:29 +00001866#endif
1867
1868
Guido van Rossumc89705d1992-11-26 08:54:07 +00001869/* s.getsockname() method */
1870
Guido van Rossum73624e91994-10-10 17:59:00 +00001871static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001872sock_getsockname(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001873{
1874 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001875 int res;
1876 socklen_t addrlen;
1877
Guido van Rossumc89705d1992-11-26 08:54:07 +00001878 if (!getsockaddrlen(s, &addrlen))
1879 return NULL;
Guido van Rossumca42b161996-01-12 01:36:05 +00001880 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001881 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001882 res = getsockname(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001883 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001884 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001885 return s->errorhandler();
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001886 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen,
1887 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001888}
1889
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001890PyDoc_STRVAR(getsockname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001891"getsockname() -> address info\n\
1892\n\
1893Return the address of the local endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001894info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001895
Guido van Rossumc89705d1992-11-26 08:54:07 +00001896
Guido van Rossumb6775db1994-08-01 11:34:53 +00001897#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001898/* s.getpeername() method */
1899
Guido van Rossum73624e91994-10-10 17:59:00 +00001900static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001901sock_getpeername(PySocketSockObject *s)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001902{
1903 char addrbuf[256];
Guido van Rossumff3ab422000-04-24 15:16:03 +00001904 int res;
1905 socklen_t addrlen;
1906
Guido van Rossumc89705d1992-11-26 08:54:07 +00001907 if (!getsockaddrlen(s, &addrlen))
1908 return NULL;
Fred Drakefd168342001-05-09 19:11:33 +00001909 memset(addrbuf, 0, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001910 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001911 res = getpeername(s->sock_fd, (struct sockaddr *) addrbuf, &addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00001912 Py_END_ALLOW_THREADS
Guido van Rossumc89705d1992-11-26 08:54:07 +00001913 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001914 return s->errorhandler();
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00001915 return makesockaddr(s->sock_fd, (struct sockaddr *) addrbuf, addrlen,
1916 s->sock_proto);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001917}
Guido van Rossum82a5c661998-07-07 20:45:43 +00001918
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001919PyDoc_STRVAR(getpeername_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001920"getpeername() -> address info\n\
1921\n\
1922Return the address of the remote endpoint. For IP sockets, the address\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001923info is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001924
Guido van Rossumb6775db1994-08-01 11:34:53 +00001925#endif /* HAVE_GETPEERNAME */
Guido van Rossumc89705d1992-11-26 08:54:07 +00001926
1927
Guido van Rossum30a685f1991-06-27 15:51:29 +00001928/* s.listen(n) method */
1929
Guido van Rossum73624e91994-10-10 17:59:00 +00001930static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001931sock_listen(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001932{
1933 int backlog;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001934 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00001935
1936 backlog = PyInt_AsLong(arg);
1937 if (backlog == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001938 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00001939 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001940 if (backlog < 1)
1941 backlog = 1;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001942 res = listen(s->sock_fd, backlog);
Guido van Rossum73624e91994-10-10 17:59:00 +00001943 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001944 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00001945 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00001946 Py_INCREF(Py_None);
1947 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001948}
1949
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001950PyDoc_STRVAR(listen_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00001951"listen(backlog)\n\
1952\n\
1953Enable a server to accept connections. The backlog argument must be at\n\
1954least 1; it specifies the number of unaccepted connection that the system\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001955will allow before refusing new connections.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00001956
1957
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001958#ifndef NO_DUP
Guido van Rossum30a685f1991-06-27 15:51:29 +00001959/* s.makefile(mode) method.
1960 Create a new open file object referring to a dupped version of
1961 the socket's file descriptor. (The dup() call is necessary so
1962 that the open file and socket objects may be closed independent
1963 of each other.)
1964 The mode argument specifies 'r' or 'w' passed to fdopen(). */
1965
Guido van Rossum73624e91994-10-10 17:59:00 +00001966static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00001967sock_makefile(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001968{
Tim Petersdbd9ba62000-07-09 03:09:57 +00001969 extern int fclose(FILE *);
Guido van Rossum6b144911995-03-14 15:05:13 +00001970 char *mode = "r";
1971 int bufsize = -1;
Fred Drakea04eaad2000-06-30 02:46:07 +00001972#ifdef MS_WIN32
Tim Peters79248aa2001-08-29 21:37:10 +00001973 Py_intptr_t fd;
Fred Drakea04eaad2000-06-30 02:46:07 +00001974#else
Guido van Rossum30a685f1991-06-27 15:51:29 +00001975 int fd;
Guido van Rossum48a680c2001-03-02 06:34:14 +00001976#endif
Guido van Rossum30a685f1991-06-27 15:51:29 +00001977 FILE *fp;
Guido van Rossum6b144911995-03-14 15:05:13 +00001978 PyObject *f;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001979#ifdef __VMS
1980 char *mode_r = "r";
1981 char *mode_w = "w";
1982#endif
Guido van Rossum6b144911995-03-14 15:05:13 +00001983
Guido van Rossum43713e52000-02-29 13:59:29 +00001984 if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00001985 return NULL;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001986#ifdef __VMS
1987 if (strcmp(mode,"rb") == 0) {
1988 mode = mode_r;
1989 }
1990 else {
1991 if (strcmp(mode,"wb") == 0) {
1992 mode = mode_w;
1993 }
1994 }
1995#endif
Guido van Rossum8d665e61996-06-26 18:22:49 +00001996#ifdef MS_WIN32
Guido van Rossum82a5c661998-07-07 20:45:43 +00001997 if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||
1998 ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00001999#else
Guido van Rossum82a5c661998-07-07 20:45:43 +00002000 if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)
Guido van Rossum30b6b2b1995-06-14 22:28:08 +00002001#endif
Guido van Rossum82a5c661998-07-07 20:45:43 +00002002 {
Guido van Rossum6b144911995-03-14 15:05:13 +00002003 if (fd >= 0)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00002004 SOCKETCLOSE(fd);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002005 return s->errorhandler();
Guido van Rossum6b144911995-03-14 15:05:13 +00002006 }
2007 f = PyFile_FromFile(fp, "<socket>", mode, fclose);
2008 if (f != NULL)
2009 PyFile_SetBufSize(f, bufsize);
2010 return f;
Guido van Rossum30a685f1991-06-27 15:51:29 +00002011}
Guido van Rossum82a5c661998-07-07 20:45:43 +00002012
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002013PyDoc_STRVAR(makefile_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002014"makefile([mode[, buffersize]]) -> file object\n\
2015\n\
2016Return a regular file object corresponding to the socket.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002017The mode and buffersize arguments are as for the built-in open() function.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002018
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002019#endif /* NO_DUP */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002020
Guido van Rossum48a680c2001-03-02 06:34:14 +00002021
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002022/* s.recv(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002023
Guido van Rossum73624e91994-10-10 17:59:00 +00002024static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002025sock_recv(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002026{
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002027 int len, n = 0, flags = 0, timeout;
Guido van Rossum73624e91994-10-10 17:59:00 +00002028 PyObject *buf;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002029#ifdef __VMS
2030 int read_length;
2031 char *read_buf;
2032#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002033
Guido van Rossum43713e52000-02-29 13:59:29 +00002034 if (!PyArg_ParseTuple(args, "i|i:recv", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002035 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002036
2037 if (len < 0) {
Martin v. Löwisfba64e12001-11-19 10:41:26 +00002038 PyErr_SetString(PyExc_ValueError,
Guido van Rossum11ba0942002-06-13 15:07:44 +00002039 "negative buffersize in recv");
Martin v. Löwisfba64e12001-11-19 10:41:26 +00002040 return NULL;
2041 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002042
Guido van Rossum73624e91994-10-10 17:59:00 +00002043 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002044 if (buf == NULL)
2045 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002046
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002047#ifndef __VMS
Guido van Rossum73624e91994-10-10 17:59:00 +00002048 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002049 timeout = internal_select(s, 0);
2050 if (!timeout)
2051 n = recv(s->sock_fd, PyString_AS_STRING(buf), len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00002052 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002053
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002054 if (timeout) {
2055 Py_DECREF(buf);
2056 PyErr_SetString(socket_timeout, "timed out");
2057 return NULL;
2058 }
Guido van Rossum7c53b771995-09-13 18:39:47 +00002059 if (n < 0) {
2060 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002061 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00002062 }
Tim Peters5de98422002-04-27 18:44:32 +00002063 if (n != len)
2064 _PyString_Resize(&buf, n);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002065#else
2066 read_buf = PyString_AsString(buf);
2067 read_length = len;
2068 while (read_length != 0) {
2069 unsigned int segment;
2070
2071 segment = read_length /SEGMENT_SIZE;
2072 if (segment != 0) {
2073 segment = SEGMENT_SIZE;
2074 }
2075 else {
2076 segment = read_length;
2077 }
2078
2079 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002080 timeout = internal_select(s, 0);
2081 if (!timeout)
2082 n = recv(s->sock_fd, read_buf, segment, flags);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002083 Py_END_ALLOW_THREADS
2084
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002085 if (timeout) {
2086 Py_DECREF(buf);
2087 PyErr_SetString(socket_timeout, "timed out");
2088 return NULL;
2089 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002090 if (n < 0) {
2091 Py_DECREF(buf);
2092 return s->errorhandler();
2093 }
2094 if (n != read_length) {
2095 read_buf += n;
2096 break;
2097 }
2098
2099 read_length -= segment;
2100 read_buf += segment;
2101 }
2102 if (_PyString_Resize(&buf, (read_buf - PyString_AsString(buf))) < 0)
2103 {
2104 return NULL;
2105 }
2106#endif /* !__VMS */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002107 return buf;
2108}
2109
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002110PyDoc_STRVAR(recv_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002111"recv(buffersize[, flags]) -> data\n\
2112\n\
2113Receive up to buffersize bytes from the socket. For the optional flags\n\
2114argument, see the Unix manual. When no data is available, block until\n\
2115at least one byte is available or until the remote end is closed. When\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002116the remote end is closed and all data is read, return the empty string.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002117
Guido van Rossum30a685f1991-06-27 15:51:29 +00002118
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002119/* s.recvfrom(nbytes [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002120
Guido van Rossum73624e91994-10-10 17:59:00 +00002121static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002122sock_recvfrom(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002123{
2124 char addrbuf[256];
Barry Warsaw752300b1997-01-03 17:18:10 +00002125 PyObject *buf = NULL;
2126 PyObject *addr = NULL;
2127 PyObject *ret = NULL;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002128 int len, n = 0, flags = 0, timeout;
Guido van Rossumff3ab422000-04-24 15:16:03 +00002129 socklen_t addrlen;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002130
Guido van Rossum43713e52000-02-29 13:59:29 +00002131 if (!PyArg_ParseTuple(args, "i|i:recvfrom", &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002132 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002133
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00002134 if (!getsockaddrlen(s, &addrlen))
2135 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002136 buf = PyString_FromStringAndSize((char *) 0, len);
Guido van Rossum18c9a4f1993-05-25 12:16:29 +00002137 if (buf == NULL)
2138 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002139
Guido van Rossum73624e91994-10-10 17:59:00 +00002140 Py_BEGIN_ALLOW_THREADS
Fred Drakefd168342001-05-09 19:11:33 +00002141 memset(addrbuf, 0, addrlen);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002142 timeout = internal_select(s, 0);
2143 if (!timeout)
2144 n = recvfrom(s->sock_fd, PyString_AS_STRING(buf), len, flags,
Guido van Rossum8d665e61996-06-26 18:22:49 +00002145#ifndef MS_WINDOWS
Andrew MacIntyreba43e872002-03-03 03:03:52 +00002146#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002147 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00002148#else
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002149 (void *)addrbuf, &addrlen
Guido van Rossum32c575d1997-12-02 20:37:32 +00002150#endif
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002151#else
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002152 (struct sockaddr *)addrbuf, &addrlen
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002153#endif
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002154 );
Guido van Rossum73624e91994-10-10 17:59:00 +00002155 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002156
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002157 if (timeout) {
2158 Py_DECREF(buf);
2159 PyErr_SetString(socket_timeout, "timed out");
2160 return NULL;
2161 }
Guido van Rossum7c53b771995-09-13 18:39:47 +00002162 if (n < 0) {
2163 Py_DECREF(buf);
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002164 return s->errorhandler();
Guido van Rossum7c53b771995-09-13 18:39:47 +00002165 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002166
Guido van Rossum53a9bf81996-06-11 18:35:24 +00002167 if (n != len && _PyString_Resize(&buf, n) < 0)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002168 return NULL;
Guido van Rossum48a680c2001-03-02 06:34:14 +00002169
Guido van Rossum67f7a382002-06-06 21:08:16 +00002170 if (!(addr = makesockaddr(s->sock_fd, (struct sockaddr *)addrbuf,
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00002171 addrlen, s->sock_proto)))
Barry Warsaw752300b1997-01-03 17:18:10 +00002172 goto finally;
2173
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002174 ret = PyTuple_Pack(2, buf, addr);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002175
2176finally:
Guido van Rossum73624e91994-10-10 17:59:00 +00002177 Py_XDECREF(addr);
2178 Py_XDECREF(buf);
Guido van Rossum6f5afc91993-02-05 09:46:15 +00002179 return ret;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002180}
2181
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002182PyDoc_STRVAR(recvfrom_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002183"recvfrom(buffersize[, flags]) -> (data, address info)\n\
2184\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002185Like recv(buffersize, flags) but also return the sender's address info.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002186
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002187/* s.send(data [,flags]) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002188
Guido van Rossum73624e91994-10-10 17:59:00 +00002189static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002190sock_send(PySocketSockObject *s, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002191{
Guido van Rossumff4949e1992-08-05 19:58:53 +00002192 char *buf;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002193 int len, n = 0, flags = 0, timeout;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002194#ifdef __VMS
2195 int send_length;
2196#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002197
Guido van Rossum43713e52000-02-29 13:59:29 +00002198 if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))
Guido van Rossumbe32c891996-06-20 16:25:29 +00002199 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002200
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002201#ifndef __VMS
Guido van Rossum73624e91994-10-10 17:59:00 +00002202 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002203 timeout = internal_select(s, 1);
2204 if (!timeout)
2205 n = send(s->sock_fd, buf, len, flags);
Guido van Rossum73624e91994-10-10 17:59:00 +00002206 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002207
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002208 if (timeout) {
2209 PyErr_SetString(socket_timeout, "timed out");
2210 return NULL;
2211 }
Guido van Rossum30a685f1991-06-27 15:51:29 +00002212 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002213 return s->errorhandler();
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002214#else
2215 /* Divide packet into smaller segments for */
2216 /* TCP/IP Services for OpenVMS */
2217 send_length = len;
2218 while (send_length != 0) {
2219 unsigned int segment;
2220
2221 segment = send_length / SEGMENT_SIZE;
2222 if (segment != 0) {
2223 segment = SEGMENT_SIZE;
2224 }
2225 else {
2226 segment = send_length;
2227 }
2228 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002229 timeout = internal_select(s, 1);
2230 if (!timeout)
2231 n = send(s->sock_fd, buf, segment, flags);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002232 Py_END_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002233 if (timeout) {
2234 PyErr_SetString(socket_timeout, "timed out");
2235 return NULL;
2236 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002237 if (n < 0) {
2238 return s->errorhandler();
2239 }
2240 send_length -= segment;
2241 buf += segment;
2242 } /* end while */
2243#endif /* !__VMS */
Guido van Rossum73624e91994-10-10 17:59:00 +00002244 return PyInt_FromLong((long)n);
Guido van Rossum30a685f1991-06-27 15:51:29 +00002245}
2246
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002247PyDoc_STRVAR(send_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002248"send(data[, flags]) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002249\n\
2250Send a data string to the socket. For the optional flags\n\
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002251argument, see the Unix manual. Return the number of bytes\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002252sent; this may be less than len(data) if the network is busy.");
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002253
2254
2255/* s.sendall(data [,flags]) method */
2256
2257static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002258sock_sendall(PySocketSockObject *s, PyObject *args)
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002259{
2260 char *buf;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002261 int len, n = 0, flags = 0, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002262
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002263 if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags))
2264 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002265
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002266 Py_BEGIN_ALLOW_THREADS
2267 do {
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002268 timeout = internal_select(s, 1);
2269 if (timeout)
2270 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002271 n = send(s->sock_fd, buf, len, flags);
2272 if (n < 0)
2273 break;
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002274 buf += n;
2275 len -= n;
2276 } while (len > 0);
2277 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002278
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002279 if (timeout) {
2280 PyErr_SetString(socket_timeout, "timed out");
2281 return NULL;
2282 }
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002283 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002284 return s->errorhandler();
Guido van Rossum67f7a382002-06-06 21:08:16 +00002285
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002286 Py_INCREF(Py_None);
2287 return Py_None;
2288}
2289
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002290PyDoc_STRVAR(sendall_doc,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002291"sendall(data[, flags])\n\
2292\n\
2293Send a data string to the socket. For the optional flags\n\
2294argument, see the Unix manual. This calls send() repeatedly\n\
2295until all data is sent. If an error occurs, it's impossible\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002296to tell how much data has been sent.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002297
Guido van Rossum30a685f1991-06-27 15:51:29 +00002298
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002299/* s.sendto(data, [flags,] sockaddr) method */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002300
Guido van Rossum73624e91994-10-10 17:59:00 +00002301static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002302sock_sendto(PySocketSockObject *s, PyObject *args)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002303{
Guido van Rossum73624e91994-10-10 17:59:00 +00002304 PyObject *addro;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002305 char *buf;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002306 struct sockaddr *addr;
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002307 int addrlen, len, n = 0, flags, timeout;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002308
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002309 flags = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002310 if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {
Guido van Rossum73624e91994-10-10 17:59:00 +00002311 PyErr_Clear();
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002312 if (!PyArg_ParseTuple(args, "s#iO:sendto",
2313 &buf, &len, &flags, &addro))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002314 return NULL;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002315 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002316
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002317 if (!getsockaddrarg(s, addro, &addr, &addrlen))
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002318 return NULL;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002319
Guido van Rossum73624e91994-10-10 17:59:00 +00002320 Py_BEGIN_ALLOW_THREADS
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002321 timeout = internal_select(s, 1);
2322 if (!timeout)
2323 n = sendto(s->sock_fd, buf, len, flags, addr, addrlen);
Guido van Rossum73624e91994-10-10 17:59:00 +00002324 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002325
Raymond Hettingeref7343c2003-06-29 03:08:05 +00002326 if (timeout) {
2327 PyErr_SetString(socket_timeout, "timed out");
2328 return NULL;
2329 }
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002330 if (n < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002331 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002332 return PyInt_FromLong((long)n);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002333}
2334
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002335PyDoc_STRVAR(sendto_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00002336"sendto(data[, flags], address) -> count\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002337\n\
2338Like send(data, flags) but allows specifying the destination address.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002339For IP sockets, the address is a pair (hostaddr, port).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002340
Guido van Rossum30a685f1991-06-27 15:51:29 +00002341
2342/* s.shutdown(how) method */
2343
Guido van Rossum73624e91994-10-10 17:59:00 +00002344static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002345sock_shutdown(PySocketSockObject *s, PyObject *arg)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002346{
2347 int how;
Guido van Rossumff4949e1992-08-05 19:58:53 +00002348 int res;
Jeremy Hyltonae0013d2001-10-11 16:36:35 +00002349
2350 how = PyInt_AsLong(arg);
2351 if (how == -1 && PyErr_Occurred())
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002352 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002353 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002354 res = shutdown(s->sock_fd, how);
Guido van Rossum73624e91994-10-10 17:59:00 +00002355 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002356 if (res < 0)
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00002357 return s->errorhandler();
Guido van Rossum73624e91994-10-10 17:59:00 +00002358 Py_INCREF(Py_None);
2359 return Py_None;
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002360}
2361
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002362PyDoc_STRVAR(shutdown_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002363"shutdown(flag)\n\
2364\n\
Martin v. Löwis94681fc2003-11-27 19:40:22 +00002365Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\
2366of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002367
Guido van Rossum30a685f1991-06-27 15:51:29 +00002368
2369/* List of methods for socket objects */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002370
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002371static PyMethodDef sock_methods[] = {
2372 {"accept", (PyCFunction)sock_accept, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002373 accept_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002374 {"bind", (PyCFunction)sock_bind, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002375 bind_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002376 {"close", (PyCFunction)sock_close, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002377 close_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002378 {"connect", (PyCFunction)sock_connect, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002379 connect_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002380 {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002381 connect_ex_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002382#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002383 {"dup", (PyCFunction)sock_dup, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002384 dup_doc},
Guido van Rossumbe32c891996-06-20 16:25:29 +00002385#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002386 {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002387 fileno_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002388#ifdef HAVE_GETPEERNAME
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002389 {"getpeername", (PyCFunction)sock_getpeername,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002390 METH_NOARGS, getpeername_doc},
Guido van Rossum9575a441993-04-07 14:06:14 +00002391#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002392 {"getsockname", (PyCFunction)sock_getsockname,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002393 METH_NOARGS, getsockname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002394 {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002395 getsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002396 {"listen", (PyCFunction)sock_listen, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002397 listen_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002398#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002399 {"makefile", (PyCFunction)sock_makefile, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002400 makefile_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002401#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002402 {"recv", (PyCFunction)sock_recv, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002403 recv_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002404 {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002405 recvfrom_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002406 {"send", (PyCFunction)sock_send, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002407 send_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002408 {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
Guido van Rossum9f7a5392001-10-26 03:25:00 +00002409 sendall_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002410 {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002411 sendto_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002412 {"setblocking", (PyCFunction)sock_setblocking, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002413 setblocking_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002414 {"settimeout", (PyCFunction)sock_settimeout, METH_O,
Guido van Rossum67f7a382002-06-06 21:08:16 +00002415 settimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002416 {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
Guido van Rossum67f7a382002-06-06 21:08:16 +00002417 gettimeout_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002418 {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002419 setsockopt_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002420 {"shutdown", (PyCFunction)sock_shutdown, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002421 shutdown_doc},
2422#ifdef RISCOS
Martin v. Löwisa94568a2003-05-10 07:36:56 +00002423 {"sleeptaskw", (PyCFunction)sock_sleeptaskw, METH_O,
Guido van Rossum48a680c2001-03-02 06:34:14 +00002424 sleeptaskw_doc},
2425#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00002426 {NULL, NULL} /* sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002427};
2428
Guido van Rossum30a685f1991-06-27 15:51:29 +00002429
Guido van Rossum73624e91994-10-10 17:59:00 +00002430/* Deallocate a socket object in response to the last Py_DECREF().
Guido van Rossum30a685f1991-06-27 15:51:29 +00002431 First close the file description. */
2432
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002433static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002434sock_dealloc(PySocketSockObject *s)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002435{
Guido van Rossumfa972c92000-04-10 12:45:45 +00002436 if (s->sock_fd != -1)
Guido van Rossum2dd8ddd2000-04-21 20:33:00 +00002437 (void) SOCKETCLOSE(s->sock_fd);
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002438 s->ob_type->tp_free((PyObject *)s);
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002439}
2440
Guido van Rossum30a685f1991-06-27 15:51:29 +00002441
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002442static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002443sock_repr(PySocketSockObject *s)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002444{
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002445 char buf[512];
Fred Drakea04eaad2000-06-30 02:46:07 +00002446#if SIZEOF_SOCKET_T > SIZEOF_LONG
2447 if (s->sock_fd > LONG_MAX) {
2448 /* this can occur on Win64, and actually there is a special
2449 ugly printf formatter for decimal pointer length integer
2450 printing, only bother if necessary*/
2451 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002452 "no printf formatter to display "
2453 "the socket descriptor in decimal");
Fred Drakea04eaad2000-06-30 02:46:07 +00002454 return NULL;
2455 }
2456#endif
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002457 PyOS_snprintf(
2458 buf, sizeof(buf),
2459 "<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",
2460 (long)s->sock_fd, s->sock_family,
2461 s->sock_type,
2462 s->sock_proto);
Guido van Rossumdc1c64a1996-02-27 15:21:19 +00002463 return PyString_FromString(buf);
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002464}
2465
2466
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002467/* Create a new, uninitialized socket object. */
2468
2469static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002470sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002471{
2472 PyObject *new;
2473
2474 new = type->tp_alloc(type, 0);
Guido van Rossume1c478f2002-06-06 20:08:25 +00002475 if (new != NULL) {
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002476 ((PySocketSockObject *)new)->sock_fd = -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002477 ((PySocketSockObject *)new)->sock_timeout = -1.0;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002478 ((PySocketSockObject *)new)->errorhandler = &set_error;
Guido van Rossume1c478f2002-06-06 20:08:25 +00002479 }
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002480 return new;
2481}
2482
2483
2484/* Initialize a new socket object. */
2485
2486/*ARGSUSED*/
2487static int
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002488sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002489{
2490 PySocketSockObject *s = (PySocketSockObject *)self;
2491 SOCKET_T fd;
2492 int family = AF_INET, type = SOCK_STREAM, proto = 0;
2493 static char *keywords[] = {"family", "type", "proto", 0};
2494
2495 if (!PyArg_ParseTupleAndKeywords(args, kwds,
2496 "|iii:socket", keywords,
2497 &family, &type, &proto))
2498 return -1;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002499
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002500 Py_BEGIN_ALLOW_THREADS
2501 fd = socket(family, type, proto);
2502 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002503
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002504#ifdef MS_WINDOWS
2505 if (fd == INVALID_SOCKET)
2506#else
2507 if (fd < 0)
2508#endif
2509 {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002510 set_error();
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002511 return -1;
2512 }
2513 init_sockobject(s, fd, family, type, proto);
2514 /* From now on, ignore SIGPIPE and let the error checking
2515 do the work. */
2516#ifdef SIGPIPE
2517 (void) signal(SIGPIPE, SIG_IGN);
2518#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002519
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002520 return 0;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002521
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002522}
2523
2524
Guido van Rossumb6775db1994-08-01 11:34:53 +00002525/* Type object for socket objects. */
Guido van Rossum30a685f1991-06-27 15:51:29 +00002526
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002527static PyTypeObject sock_type = {
Guido van Rossum55558541996-05-23 22:54:50 +00002528 PyObject_HEAD_INIT(0) /* Must fill in type value later */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002529 0, /* ob_size */
Guido van Rossumca6dfa52001-10-28 12:31:33 +00002530 "_socket.socket", /* tp_name */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002531 sizeof(PySocketSockObject), /* tp_basicsize */
2532 0, /* tp_itemsize */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002533 (destructor)sock_dealloc, /* tp_dealloc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002534 0, /* tp_print */
2535 0, /* tp_getattr */
2536 0, /* tp_setattr */
2537 0, /* tp_compare */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002538 (reprfunc)sock_repr, /* tp_repr */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002539 0, /* tp_as_number */
2540 0, /* tp_as_sequence */
2541 0, /* tp_as_mapping */
2542 0, /* tp_hash */
2543 0, /* tp_call */
2544 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002545 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002546 0, /* tp_setattro */
2547 0, /* tp_as_buffer */
2548 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002549 sock_doc, /* tp_doc */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002550 0, /* tp_traverse */
2551 0, /* tp_clear */
2552 0, /* tp_richcompare */
2553 0, /* tp_weaklistoffset */
2554 0, /* tp_iter */
2555 0, /* tp_iternext */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002556 sock_methods, /* tp_methods */
Guido van Rossum384ca9c2001-10-27 22:20:47 +00002557 0, /* tp_members */
2558 0, /* tp_getset */
2559 0, /* tp_base */
2560 0, /* tp_dict */
2561 0, /* tp_descr_get */
2562 0, /* tp_descr_set */
2563 0, /* tp_dictoffset */
Andrew MacIntyre7aec4a22002-06-13 11:53:52 +00002564 sock_initobj, /* tp_init */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002565 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002566 sock_new, /* tp_new */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002567 PyObject_Del, /* tp_free */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00002568};
2569
Guido van Rossum30a685f1991-06-27 15:51:29 +00002570
Guido van Rossum81194471991-07-27 21:42:02 +00002571/* Python interface to gethostname(). */
2572
2573/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002574static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002575socket_gethostname(PyObject *self, PyObject *args)
Guido van Rossum81194471991-07-27 21:42:02 +00002576{
2577 char buf[1024];
Guido van Rossumff4949e1992-08-05 19:58:53 +00002578 int res;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002579 if (!PyArg_ParseTuple(args, ":gethostname"))
Guido van Rossum81194471991-07-27 21:42:02 +00002580 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002581 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002582 res = gethostname(buf, (int) sizeof buf - 1);
Guido van Rossum73624e91994-10-10 17:59:00 +00002583 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002584 if (res < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002585 return set_error();
Guido van Rossum81194471991-07-27 21:42:02 +00002586 buf[sizeof buf - 1] = '\0';
Guido van Rossum73624e91994-10-10 17:59:00 +00002587 return PyString_FromString(buf);
Guido van Rossum81194471991-07-27 21:42:02 +00002588}
Guido van Rossumff4949e1992-08-05 19:58:53 +00002589
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002590PyDoc_STRVAR(gethostname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002591"gethostname() -> string\n\
2592\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002593Return the current host name.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002594
Guido van Rossumff4949e1992-08-05 19:58:53 +00002595
Guido van Rossum30a685f1991-06-27 15:51:29 +00002596/* Python interface to gethostbyname(name). */
2597
2598/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002599static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002600socket_gethostbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002601{
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002602 char *name;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002603#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002604 struct sockaddr_storage addrbuf;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002605#else
2606 struct sockaddr_in addrbuf;
2607#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002608
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002609 if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002610 return NULL;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002611 if (setipaddr(name, (struct sockaddr *)&addrbuf, sizeof(addrbuf), AF_INET) < 0)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002612 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002613 return makeipaddr((struct sockaddr *)&addrbuf,
2614 sizeof(struct sockaddr_in));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002615}
2616
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002617PyDoc_STRVAR(gethostbyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002618"gethostbyname(host) -> address\n\
2619\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002620Return the IP address (a string of the form '255.255.255.255') for a host.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002621
2622
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002623/* Convenience function common to gethostbyname_ex and gethostbyaddr */
2624
2625static PyObject *
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002626gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002627{
2628 char **pch;
2629 PyObject *rtn_tuple = (PyObject *)NULL;
2630 PyObject *name_list = (PyObject *)NULL;
2631 PyObject *addr_list = (PyObject *)NULL;
2632 PyObject *tmp;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002633
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002634 if (h == NULL) {
Guido van Rossum48a680c2001-03-02 06:34:14 +00002635 /* Let's get real error message to return */
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002636#ifndef RISCOS
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002637 set_herror(h_errno);
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002638#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002639 PyErr_SetString(socket_error, "host not found");
Guido van Rossume2ae77b2001-10-24 20:42:55 +00002640#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002641 return NULL;
2642 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002643
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002644 if (h->h_addrtype != af) {
2645#ifdef HAVE_STRERROR
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002646 /* Let's get real error message to return */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002647 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002648 (char *)strerror(EAFNOSUPPORT));
2649#else
2650 PyErr_SetString(
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002651 socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002652 "Address family not supported by protocol family");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002653#endif
2654 return NULL;
2655 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002656
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002657 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002658
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002659 case AF_INET:
2660 if (alen < sizeof(struct sockaddr_in))
2661 return NULL;
2662 break;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002663
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002664#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002665 case AF_INET6:
2666 if (alen < sizeof(struct sockaddr_in6))
2667 return NULL;
2668 break;
2669#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002670
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002671 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002672
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002673 if ((name_list = PyList_New(0)) == NULL)
2674 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002675
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002676 if ((addr_list = PyList_New(0)) == NULL)
2677 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002678
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002679 for (pch = h->h_aliases; *pch != NULL; pch++) {
2680 int status;
2681 tmp = PyString_FromString(*pch);
2682 if (tmp == NULL)
2683 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002684
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002685 status = PyList_Append(name_list, tmp);
2686 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002687
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002688 if (status)
2689 goto err;
2690 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002691
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002692 for (pch = h->h_addr_list; *pch != NULL; pch++) {
2693 int status;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002694
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002695 switch (af) {
Guido van Rossum67f7a382002-06-06 21:08:16 +00002696
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002697 case AF_INET:
2698 {
2699 struct sockaddr_in sin;
2700 memset(&sin, 0, sizeof(sin));
2701 sin.sin_family = af;
2702#ifdef HAVE_SOCKADDR_SA_LEN
2703 sin.sin_len = sizeof(sin);
2704#endif
2705 memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
2706 tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002707
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002708 if (pch == h->h_addr_list && alen >= sizeof(sin))
2709 memcpy((char *) addr, &sin, sizeof(sin));
2710 break;
2711 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002712
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002713#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002714 case AF_INET6:
2715 {
2716 struct sockaddr_in6 sin6;
2717 memset(&sin6, 0, sizeof(sin6));
2718 sin6.sin6_family = af;
2719#ifdef HAVE_SOCKADDR_SA_LEN
2720 sin6.sin6_len = sizeof(sin6);
2721#endif
2722 memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
2723 tmp = makeipaddr((struct sockaddr *)&sin6,
2724 sizeof(sin6));
Guido van Rossum67f7a382002-06-06 21:08:16 +00002725
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002726 if (pch == h->h_addr_list && alen >= sizeof(sin6))
2727 memcpy((char *) addr, &sin6, sizeof(sin6));
2728 break;
2729 }
2730#endif
Guido van Rossum67f7a382002-06-06 21:08:16 +00002731
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002732 default: /* can't happen */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002733 PyErr_SetString(socket_error,
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002734 "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002735 return NULL;
2736 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002737
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002738 if (tmp == NULL)
2739 goto err;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002740
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002741 status = PyList_Append(addr_list, tmp);
2742 Py_DECREF(tmp);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002743
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002744 if (status)
2745 goto err;
2746 }
Guido van Rossum67f7a382002-06-06 21:08:16 +00002747
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002748 rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
Guido van Rossum67f7a382002-06-06 21:08:16 +00002749
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002750 err:
2751 Py_XDECREF(name_list);
2752 Py_XDECREF(addr_list);
2753 return rtn_tuple;
2754}
2755
2756
2757/* Python interface to gethostbyname_ex(name). */
2758
2759/*ARGSUSED*/
2760static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002761socket_gethostbyname_ex(PyObject *self, PyObject *args)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002762{
2763 char *name;
2764 struct hostent *h;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +00002765#ifdef ENABLE_IPV6
2766 struct sockaddr_storage addr;
2767#else
2768 struct sockaddr_in addr;
2769#endif
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002770 struct sockaddr *sa;
Guido van Rossum955becc1999-03-22 20:14:53 +00002771 PyObject *ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002772#ifdef HAVE_GETHOSTBYNAME_R
2773 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002774#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2775 struct hostent_data data;
2776#else
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002777 char buf[16384];
2778 int buf_len = (sizeof buf) - 1;
2779 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002780#endif
2781#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002782 int result;
2783#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002784#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002785
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002786 if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002787 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00002788 if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002789 return NULL;
2790 Py_BEGIN_ALLOW_THREADS
2791#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002792#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002793 result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
2794 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002795#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002796 h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002797#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002798 memset((void *) &data, '\0', sizeof(data));
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002799 result = gethostbyname_r(name, &hp_allocated, &data);
2800 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002801#endif
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002802#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002803#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002804 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002805#endif
2806 h = gethostbyname(name);
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002807#endif /* HAVE_GETHOSTBYNAME_R */
2808 Py_END_ALLOW_THREADS
Guido van Rossum67f7a382002-06-06 21:08:16 +00002809 /* Some C libraries would require addr.__ss_family instead of
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002810 addr.ss_family.
2811 Therefore, we cast the sockaddr_storage into sockaddr to
2812 access sa_family. */
Martin v. Löwis9db2f572001-07-23 01:30:10 +00002813 sa = (struct sockaddr*)&addr;
Guido van Rossum67f7a382002-06-06 21:08:16 +00002814 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002815 sa->sa_family);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002816#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002817 PyThread_release_lock(netdb_lock);
Guido van Rossum955becc1999-03-22 20:14:53 +00002818#endif
2819 return ret;
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002820}
2821
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002822PyDoc_STRVAR(ghbn_ex_doc,
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002823"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\
2824\n\
2825Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002826for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum7d896ab1998-08-04 22:16:43 +00002827
2828
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002829/* Python interface to gethostbyaddr(IP). */
2830
2831/*ARGSUSED*/
2832static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002833socket_gethostbyaddr(PyObject *self, PyObject *args)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002834{
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002835#ifdef ENABLE_IPV6
Guido van Rossum3eede5a2002-06-07 02:08:35 +00002836 struct sockaddr_storage addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002837#else
Guido van Rossum48a680c2001-03-02 06:34:14 +00002838 struct sockaddr_in addr;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002839#endif
2840 struct sockaddr *sa = (struct sockaddr *)&addr;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002841 char *ip_num;
2842 struct hostent *h;
Guido van Rossum3baaa131999-03-22 21:44:51 +00002843 PyObject *ret;
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002844#ifdef HAVE_GETHOSTBYNAME_R
2845 struct hostent hp_allocated;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002846#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
2847 struct hostent_data data;
2848#else
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002849 char buf[16384];
2850 int buf_len = (sizeof buf) - 1;
2851 int errnop;
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002852#endif
2853#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002854 int result;
2855#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002856#endif /* HAVE_GETHOSTBYNAME_R */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002857 char *ap;
2858 int al;
2859 int af;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002860
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002861 if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002862 return NULL;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00002863 af = AF_UNSPEC;
Martin v. Löwisddc6f472002-07-28 16:10:31 +00002864 if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002865 return NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002866 af = sa->sa_family;
2867 ap = NULL;
2868 al = 0;
2869 switch (af) {
2870 case AF_INET:
2871 ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
2872 al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
2873 break;
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00002874#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002875 case AF_INET6:
2876 ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
2877 al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
2878 break;
2879#endif
2880 default:
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002881 PyErr_SetString(socket_error, "unsupported address family");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002882 return NULL;
2883 }
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002884 Py_BEGIN_ALLOW_THREADS
2885#ifdef HAVE_GETHOSTBYNAME_R
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002886#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002887 result = gethostbyaddr_r(ap, al, af,
2888 &hp_allocated, buf, buf_len,
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002889 &h, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002890#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002891 h = gethostbyaddr_r(ap, al, af,
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002892 &hp_allocated, buf, buf_len, &errnop);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002893#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
Guido van Rossume7de2061999-03-24 17:24:33 +00002894 memset((void *) &data, '\0', sizeof(data));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002895 result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
Guido van Rossum7b6c71f1999-03-24 17:20:40 +00002896 h = (result != 0) ? NULL : &hp_allocated;
Guido van Rossume9cd07b1999-03-15 21:40:14 +00002897#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002898#else /* not HAVE_GETHOSTBYNAME_R */
Guido van Rossum3baaa131999-03-22 21:44:51 +00002899#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002900 PyThread_acquire_lock(netdb_lock, 1);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002901#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002902 h = gethostbyaddr(ap, al, af);
Guido van Rossum4f199ea1998-04-09 20:56:35 +00002903#endif /* HAVE_GETHOSTBYNAME_R */
2904 Py_END_ALLOW_THREADS
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00002905 ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002906#ifdef USE_GETHOSTBYNAME_LOCK
Just van Rossum1040d2c2003-05-09 07:53:18 +00002907 PyThread_release_lock(netdb_lock);
Guido van Rossum3baaa131999-03-22 21:44:51 +00002908#endif
2909 return ret;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002910}
2911
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002912PyDoc_STRVAR(gethostbyaddr_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00002913"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\
2914\n\
2915Return the true host name, a list of aliases, and a list of IP addresses,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002916for a host. The host argument is a string giving a host name or IP number.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002917
Guido van Rossum30a685f1991-06-27 15:51:29 +00002918
2919/* Python interface to getservbyname(name).
2920 This only returns the port number, since the other info is already
2921 known or not useful (like the list of aliases). */
2922
2923/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00002924static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002925socket_getservbyname(PyObject *self, PyObject *args)
Guido van Rossum30a685f1991-06-27 15:51:29 +00002926{
Barry Warsaw11b91a02004-06-28 00:50:43 +00002927 char *name, *proto=NULL;
Guido van Rossum30a685f1991-06-27 15:51:29 +00002928 struct servent *sp;
Barry Warsaw11b91a02004-06-28 00:50:43 +00002929 if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
Guido van Rossum30a685f1991-06-27 15:51:29 +00002930 return NULL;
Guido van Rossum73624e91994-10-10 17:59:00 +00002931 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00002932 sp = getservbyname(name, proto);
Guido van Rossum73624e91994-10-10 17:59:00 +00002933 Py_END_ALLOW_THREADS
Guido van Rossum30a685f1991-06-27 15:51:29 +00002934 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002935 PyErr_SetString(socket_error, "service/proto not found");
Guido van Rossum30a685f1991-06-27 15:51:29 +00002936 return NULL;
2937 }
Guido van Rossum73624e91994-10-10 17:59:00 +00002938 return PyInt_FromLong((long) ntohs(sp->s_port));
Guido van Rossum30a685f1991-06-27 15:51:29 +00002939}
2940
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002941PyDoc_STRVAR(getservbyname_doc,
Barry Warsaw11b91a02004-06-28 00:50:43 +00002942"getservbyname(servicename[, protocolname]) -> integer\n\
Guido van Rossum82a5c661998-07-07 20:45:43 +00002943\n\
2944Return a port number from a service name and protocol name.\n\
Barry Warsaw11b91a02004-06-28 00:50:43 +00002945The optional protocol name, if given, should be 'tcp' or 'udp',\n\
2946otherwise any protocol will match.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00002947
Guido van Rossum30a685f1991-06-27 15:51:29 +00002948
Barry Warsaw11b91a02004-06-28 00:50:43 +00002949/* Python interface to getservbyport(port).
2950 This only returns the service name, since the other info is already
2951 known or not useful (like the list of aliases). */
2952
2953/*ARGSUSED*/
2954static PyObject *
2955socket_getservbyport(PyObject *self, PyObject *args)
2956{
2957 int port;
2958 char *proto=NULL;
2959 struct servent *sp;
2960 if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto))
2961 return NULL;
2962 Py_BEGIN_ALLOW_THREADS
2963 sp = getservbyport(htons(port), proto);
2964 Py_END_ALLOW_THREADS
2965 if (sp == NULL) {
2966 PyErr_SetString(socket_error, "port/proto not found");
2967 return NULL;
2968 }
2969 return PyString_FromString(sp->s_name);
2970}
2971
2972PyDoc_STRVAR(getservbyport_doc,
2973"getservbyport(port[, protocolname]) -> string\n\
2974\n\
2975Return the service name from a port number and protocol name.\n\
2976The optional protocol name, if given, should be 'tcp' or 'udp',\n\
2977otherwise any protocol will match.");
2978
Guido van Rossum3901d851996-12-19 16:35:04 +00002979/* Python interface to getprotobyname(name).
2980 This only returns the protocol number, since the other info is
2981 already known or not useful (like the list of aliases). */
2982
2983/*ARGSUSED*/
2984static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002985socket_getprotobyname(PyObject *self, PyObject *args)
Guido van Rossum3901d851996-12-19 16:35:04 +00002986{
2987 char *name;
2988 struct protoent *sp;
Guido van Rossumbcc20741998-08-04 22:53:56 +00002989#ifdef __BEOS__
2990/* Not available in BeOS yet. - [cjh] */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00002991 PyErr_SetString(socket_error, "getprotobyname not supported");
Guido van Rossumbcc20741998-08-04 22:53:56 +00002992 return NULL;
2993#else
Guido van Rossum27fc3c02000-03-24 20:56:56 +00002994 if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
Guido van Rossum3901d851996-12-19 16:35:04 +00002995 return NULL;
2996 Py_BEGIN_ALLOW_THREADS
2997 sp = getprotobyname(name);
2998 Py_END_ALLOW_THREADS
2999 if (sp == NULL) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003000 PyErr_SetString(socket_error, "protocol not found");
Guido van Rossum3901d851996-12-19 16:35:04 +00003001 return NULL;
3002 }
3003 return PyInt_FromLong((long) sp->p_proto);
Guido van Rossumbcc20741998-08-04 22:53:56 +00003004#endif
Guido van Rossum3901d851996-12-19 16:35:04 +00003005}
3006
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003007PyDoc_STRVAR(getprotobyname_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003008"getprotobyname(name) -> integer\n\
3009\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003010Return the protocol number for the named protocol. (Rarely used.)");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003011
Guido van Rossum3901d851996-12-19 16:35:04 +00003012
Dave Cole331708b2004-08-09 04:51:41 +00003013#ifdef HAVE_SOCKETPAIR
3014/* Create a pair of sockets using the socketpair() function.
3015 Arguments as for socket(). */
3016
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();
3037#ifdef SIGPIPE
3038 (void) signal(SIGPIPE, SIG_IGN);
3039#endif
3040 s0 = new_sockobject(sv[0], family, type, proto);
3041 if (s0 == NULL)
3042 goto finally;
3043 s1 = new_sockobject(sv[1], family, type, proto);
3044 if (s1 == NULL)
3045 goto finally;
3046 res = PyTuple_Pack(2, s0, s1);
3047
3048finally:
3049 if (res == NULL) {
3050 if (s0 == NULL)
3051 SOCKETCLOSE(sv[0]);
3052 if (s1 == NULL)
3053 SOCKETCLOSE(sv[1]);
3054 }
3055 Py_XDECREF(s0);
3056 Py_XDECREF(s1);
3057 return res;
3058}
3059
3060PyDoc_STRVAR(socketpair_doc,
3061"socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\
3062\n\
3063Create a pair of socket objects from the sockets returned by the platform\n\
3064socketpair() function.\n\
3065The arguments are the same as for socket().");
3066
3067#endif /* HAVE_SOCKETPAIR */
3068
3069
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003070#ifndef NO_DUP
Guido van Rossum2a7178e1992-12-08 13:38:24 +00003071/* Create a socket object from a numeric file description.
3072 Useful e.g. if stdin is a socket.
3073 Additional arguments as for socket(). */
3074
3075/*ARGSUSED*/
Guido van Rossum73624e91994-10-10 17:59:00 +00003076static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003077socket_fromfd(PyObject *self, PyObject *args)
Guido van Rossum2a7178e1992-12-08 13:38:24 +00003078{
Guido van Rossum73624e91994-10-10 17:59:00 +00003079 PySocketSockObject *s;
Fred Drakea04eaad2000-06-30 02:46:07 +00003080 SOCKET_T fd;
3081 int family, type, proto = 0;
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003082 if (!PyArg_ParseTuple(args, "iii|i:fromfd",
3083 &fd, &family, &type, &proto))
Guido van Rossumbe32c891996-06-20 16:25:29 +00003084 return NULL;
Guido van Rossum5f59d601992-12-14 16:59:51 +00003085 /* Dup the fd so it and the socket can be closed independently */
3086 fd = dup(fd);
3087 if (fd < 0)
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003088 return set_error();
3089 s = new_sockobject(fd, family, type, proto);
Guido van Rossum2a7178e1992-12-08 13:38:24 +00003090 /* From now on, ignore SIGPIPE and let the error checking
3091 do the work. */
Guido van Rossum48a680c2001-03-02 06:34:14 +00003092#ifdef SIGPIPE
Guido van Rossum2a7178e1992-12-08 13:38:24 +00003093 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossum48a680c2001-03-02 06:34:14 +00003094#endif
Guido van Rossum73624e91994-10-10 17:59:00 +00003095 return (PyObject *) s;
Guido van Rossum2a7178e1992-12-08 13:38:24 +00003096}
Guido van Rossum82a5c661998-07-07 20:45:43 +00003097
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003098PyDoc_STRVAR(fromfd_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003099"fromfd(fd, family, type[, proto]) -> socket object\n\
3100\n\
3101Create a socket object from the given file descriptor.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003102The remaining arguments are the same as for socket().");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003103
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003104#endif /* NO_DUP */
Guido van Rossum2a7178e1992-12-08 13:38:24 +00003105
Guido van Rossum82a5c661998-07-07 20:45:43 +00003106
Guido van Rossum006bf911996-06-12 04:04:55 +00003107static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003108socket_ntohs(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003109{
3110 int x1, x2;
3111
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003112 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00003113 return NULL;
3114 }
3115 x2 = (int)ntohs((short)x1);
3116 return PyInt_FromLong(x2);
3117}
3118
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003119PyDoc_STRVAR(ntohs_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003120"ntohs(integer) -> integer\n\
3121\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003122Convert a 16-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003123
3124
Guido van Rossum006bf911996-06-12 04:04:55 +00003125static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003126socket_ntohl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003127{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003128 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003129
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003130 if (PyInt_Check(arg)) {
3131 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003132 if (x == (unsigned long) -1 && PyErr_Occurred())
3133 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00003134 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003135 else if (PyLong_Check(arg)) {
3136 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003137 if (x == (unsigned long) -1 && PyErr_Occurred())
3138 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003139#if SIZEOF_LONG > 4
3140 {
3141 unsigned long y;
3142 /* only want the trailing 32 bits */
3143 y = x & 0xFFFFFFFFUL;
3144 if (y ^ x)
3145 return PyErr_Format(PyExc_OverflowError,
3146 "long int larger than 32 bits");
3147 x = y;
Tim Peters58141872002-08-06 22:25:02 +00003148 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003149#endif
3150 }
3151 else
Tim Peters58141872002-08-06 22:25:02 +00003152 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003153 "expected int/long, %s found",
3154 arg->ob_type->tp_name);
3155 if (x == (unsigned long) -1 && PyErr_Occurred())
3156 return NULL;
3157 return PyInt_FromLong(ntohl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003158}
3159
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003160PyDoc_STRVAR(ntohl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003161"ntohl(integer) -> integer\n\
3162\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003163Convert a 32-bit integer from network to host byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003164
3165
Guido van Rossum006bf911996-06-12 04:04:55 +00003166static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003167socket_htons(PyObject *self, PyObject *args)
Guido van Rossum006bf911996-06-12 04:04:55 +00003168{
Hye-Shik Changa7b673f2003-12-17 09:40:23 +00003169 int x1, x2;
Guido van Rossum006bf911996-06-12 04:04:55 +00003170
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003171 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
Guido van Rossum006bf911996-06-12 04:04:55 +00003172 return NULL;
3173 }
3174 x2 = (int)htons((short)x1);
3175 return PyInt_FromLong(x2);
3176}
3177
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003178PyDoc_STRVAR(htons_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003179"htons(integer) -> integer\n\
3180\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003181Convert a 16-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003182
3183
Guido van Rossum006bf911996-06-12 04:04:55 +00003184static PyObject *
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003185socket_htonl(PyObject *self, PyObject *arg)
Guido van Rossum006bf911996-06-12 04:04:55 +00003186{
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003187 unsigned long x;
Guido van Rossum006bf911996-06-12 04:04:55 +00003188
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003189 if (PyInt_Check(arg)) {
3190 x = PyInt_AS_LONG(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003191 if (x == (unsigned long) -1 && PyErr_Occurred())
3192 return NULL;
Guido van Rossum006bf911996-06-12 04:04:55 +00003193 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003194 else if (PyLong_Check(arg)) {
3195 x = PyLong_AsUnsignedLong(arg);
Jeremy Hylton825e47b2002-07-25 16:37:51 +00003196 if (x == (unsigned long) -1 && PyErr_Occurred())
3197 return NULL;
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003198#if SIZEOF_LONG > 4
3199 {
3200 unsigned long y;
3201 /* only want the trailing 32 bits */
3202 y = x & 0xFFFFFFFFUL;
3203 if (y ^ x)
3204 return PyErr_Format(PyExc_OverflowError,
3205 "long int larger than 32 bits");
3206 x = y;
Tim Peters58141872002-08-06 22:25:02 +00003207 }
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003208#endif
3209 }
3210 else
Tim Peters58141872002-08-06 22:25:02 +00003211 return PyErr_Format(PyExc_TypeError,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003212 "expected int/long, %s found",
3213 arg->ob_type->tp_name);
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003214 return PyInt_FromLong(htonl(x));
Guido van Rossum006bf911996-06-12 04:04:55 +00003215}
3216
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003217PyDoc_STRVAR(htonl_doc,
Guido van Rossum82a5c661998-07-07 20:45:43 +00003218"htonl(integer) -> integer\n\
3219\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003220Convert a 32-bit integer from host to network byte order.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003221
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003222/* socket.inet_aton() and socket.inet_ntoa() functions. */
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003223
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003224PyDoc_STRVAR(inet_aton_doc,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003225"inet_aton(string) -> packed 32-bit IP representation\n\
3226\n\
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003227Convert 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 +00003228binary format used in low-level network functions.");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003229
3230static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003231socket_inet_aton(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003232{
Guido van Rossuma2e48551999-09-09 15:42:59 +00003233#ifndef INADDR_NONE
3234#define INADDR_NONE (-1)
3235#endif
Neal Norwitz88f115b2003-02-13 02:15:42 +00003236#ifdef HAVE_INET_ATON
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003237 struct in_addr buf;
Tim Peters1df9fdd2003-02-13 03:13:40 +00003238#else
3239 /* Have to use inet_addr() instead */
3240 unsigned long packed_addr;
3241#endif
3242 char *ip_addr;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003243
Tim Peters1df9fdd2003-02-13 03:13:40 +00003244 if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003245 return NULL;
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003246
Tim Peters1df9fdd2003-02-13 03:13:40 +00003247
3248#ifdef HAVE_INET_ATON
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003249 if (inet_aton(ip_addr, &buf))
3250 return PyString_FromStringAndSize((char *)(&buf),
3251 sizeof(buf));
3252
3253 PyErr_SetString(socket_error,
3254 "illegal IP address string passed to inet_aton");
3255 return NULL;
3256
Tim Peters1df9fdd2003-02-13 03:13:40 +00003257#else /* ! HAVE_INET_ATON */
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003258 /* XXX Problem here: inet_aton('255.255.255.255') raises
3259 an exception while it should be a valid address. */
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003260 packed_addr = inet_addr(ip_addr);
3261
3262 if (packed_addr == INADDR_NONE) { /* invalid address */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003263 PyErr_SetString(socket_error,
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003264 "illegal IP address string passed to inet_aton");
3265 return NULL;
3266 }
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003267 return PyString_FromStringAndSize((char *) &packed_addr,
3268 sizeof(packed_addr));
Guido van Rossumad05cdf2003-02-12 23:08:22 +00003269#endif
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003270}
3271
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003272PyDoc_STRVAR(inet_ntoa_doc,
Fred Drakee0661342000-03-07 14:05:16 +00003273"inet_ntoa(packed_ip) -> ip_address_string\n\
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003274\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003275Convert an IP address from 32-bit packed binary format to string format");
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003276
3277static PyObject*
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003278socket_inet_ntoa(PyObject *self, PyObject *args)
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003279{
3280 char *packed_str;
Guido van Rossumc6a164b1999-08-20 19:11:27 +00003281 int addr_len;
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003282 struct in_addr packed_addr;
3283
Guido van Rossum27fc3c02000-03-24 20:56:56 +00003284 if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) {
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003285 return NULL;
3286 }
Guido van Rossum48a680c2001-03-02 06:34:14 +00003287
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003288 if (addr_len != sizeof(packed_addr)) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003289 PyErr_SetString(socket_error,
Guido van Rossum5c9eb211999-08-20 18:21:51 +00003290 "packed IP wrong length for inet_ntoa");
3291 return NULL;
3292 }
3293
3294 memcpy(&packed_addr, packed_str, addr_len);
3295
3296 return PyString_FromString(inet_ntoa(packed_addr));
3297}
Guido van Rossum82a5c661998-07-07 20:45:43 +00003298
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003299#ifdef HAVE_INET_PTON
3300
3301PyDoc_STRVAR(inet_pton_doc,
3302"inet_pton(af, ip) -> packed IP address string\n\
3303\n\
3304Convert an IP address from string format to a packed string suitable\n\
3305for use with low-level network functions.");
3306
3307static PyObject *
3308socket_inet_pton(PyObject *self, PyObject *args)
3309{
3310 int af;
3311 char* ip;
3312 int retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003313#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003314 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003315#else
3316 char packed[sizeof(struct in_addr)];
3317#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003318 if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
3319 return NULL;
3320 }
3321
Martin v. Löwis04697e82004-06-02 12:35:29 +00003322#if !defined(ENABLE_IPV6) && defined(AF_INET6)
Martin v. Löwis10649092003-08-05 06:25:06 +00003323 if(af == AF_INET6) {
3324 PyErr_SetString(socket_error,
3325 "can't use AF_INET6, IPv6 is disabled");
3326 return NULL;
3327 }
3328#endif
3329
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003330 retval = inet_pton(af, ip, packed);
3331 if (retval < 0) {
3332 PyErr_SetFromErrno(socket_error);
3333 return NULL;
3334 } else if (retval == 0) {
3335 PyErr_SetString(socket_error,
3336 "illegal IP address string passed to inet_pton");
3337 return NULL;
3338 } else if (af == AF_INET) {
3339 return PyString_FromStringAndSize(packed,
3340 sizeof(struct in_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003341#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003342 } else if (af == AF_INET6) {
3343 return PyString_FromStringAndSize(packed,
3344 sizeof(struct in6_addr));
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003345#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003346 } else {
3347 PyErr_SetString(socket_error, "unknown address family");
3348 return NULL;
3349 }
3350}
3351
3352PyDoc_STRVAR(inet_ntop_doc,
3353"inet_ntop(af, packed_ip) -> string formatted IP address\n\
3354\n\
3355Convert a packed IP address of the given family to string format.");
3356
3357static PyObject *
3358socket_inet_ntop(PyObject *self, PyObject *args)
3359{
3360 int af;
3361 char* packed;
3362 int len;
3363 const char* retval;
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003364#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003365 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003366#else
3367 char ip[INET_ADDRSTRLEN + 1];
3368#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003369
3370 /* Guarantee NUL-termination for PyString_FromString() below */
3371 memset((void *) &ip[0], '\0', sizeof(ip) + 1);
3372
3373 if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) {
3374 return NULL;
3375 }
3376
3377 if (af == AF_INET) {
3378 if (len != sizeof(struct in_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#ifdef ENABLE_IPV6
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003384 } else if (af == AF_INET6) {
3385 if (len != sizeof(struct in6_addr)) {
3386 PyErr_SetString(PyExc_ValueError,
3387 "invalid length of packed IP address string");
3388 return NULL;
3389 }
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003390#endif
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003391 } else {
3392 PyErr_Format(PyExc_ValueError,
3393 "unknown address family %d", af);
3394 return NULL;
3395 }
3396
3397 retval = inet_ntop(af, packed, ip, sizeof(ip));
3398 if (!retval) {
3399 PyErr_SetFromErrno(socket_error);
3400 return NULL;
3401 } else {
3402 return PyString_FromString(retval);
3403 }
3404
3405 /* NOTREACHED */
3406 PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
3407 return NULL;
3408}
3409
3410#endif /* HAVE_INET_PTON */
3411
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003412/* Python interface to getaddrinfo(host, port). */
3413
3414/*ARGSUSED*/
3415static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003416socket_getaddrinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003417{
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003418 struct addrinfo hints, *res;
3419 struct addrinfo *res0 = NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003420 PyObject *hobj = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003421 PyObject *pobj = (PyObject *)NULL;
Martin v. Löwisf65b1a12001-08-12 09:28:40 +00003422 char pbuf[30];
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003423 char *hptr, *pptr;
3424 int family, socktype, protocol, flags;
3425 int error;
3426 PyObject *all = (PyObject *)NULL;
3427 PyObject *single = (PyObject *)NULL;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003428 PyObject *idna = NULL;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003429
3430 family = socktype = protocol = flags = 0;
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003431 family = AF_UNSPEC;
Martin v. Löwis2548c732003-04-18 10:39:54 +00003432 if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
3433 &hobj, &pobj, &family, &socktype,
3434 &protocol, &flags)) {
3435 return NULL;
3436 }
3437 if (hobj == Py_None) {
3438 hptr = NULL;
3439 } else if (PyUnicode_Check(hobj)) {
3440 idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
3441 if (!idna)
3442 return NULL;
3443 hptr = PyString_AsString(idna);
3444 } else if (PyString_Check(hobj)) {
3445 hptr = PyString_AsString(hobj);
3446 } else {
3447 PyErr_SetString(PyExc_TypeError,
3448 "getaddrinfo() argument 1 must be string or None");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003449 return NULL;
3450 }
3451 if (PyInt_Check(pobj)) {
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003452 PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj));
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003453 pptr = pbuf;
3454 } else if (PyString_Check(pobj)) {
3455 pptr = PyString_AsString(pobj);
3456 } else if (pobj == Py_None) {
3457 pptr = (char *)NULL;
3458 } else {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003459 PyErr_SetString(socket_error, "Int or String expected");
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003460 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003461 }
3462 memset(&hints, 0, sizeof(hints));
3463 hints.ai_family = family;
3464 hints.ai_socktype = socktype;
3465 hints.ai_protocol = protocol;
3466 hints.ai_flags = flags;
Just van Rossum1040d2c2003-05-09 07:53:18 +00003467 Py_BEGIN_ALLOW_THREADS
3468 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003469 error = getaddrinfo(hptr, pptr, &hints, &res0);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003470 Py_END_ALLOW_THREADS
3471 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003472 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003473 set_gaierror(error);
Martin v. Löwis5db099a2003-08-07 11:55:15 +00003474 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003475 }
3476
3477 if ((all = PyList_New(0)) == NULL)
3478 goto err;
3479 for (res = res0; res; res = res->ai_next) {
Guido van Rossum716aac02001-10-12 18:59:27 +00003480 PyObject *addr =
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00003481 makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
Guido van Rossum716aac02001-10-12 18:59:27 +00003482 if (addr == NULL)
3483 goto err;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003484 single = Py_BuildValue("iiisO", res->ai_family,
3485 res->ai_socktype, res->ai_protocol,
3486 res->ai_canonname ? res->ai_canonname : "",
Guido van Rossum716aac02001-10-12 18:59:27 +00003487 addr);
3488 Py_DECREF(addr);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003489 if (single == NULL)
3490 goto err;
3491
3492 if (PyList_Append(all, single))
3493 goto err;
3494 Py_XDECREF(single);
3495 }
Martin v. Löwis2548c732003-04-18 10:39:54 +00003496 Py_XDECREF(idna);
Neal Norwitz90128ba2002-08-09 03:37:42 +00003497 if (res0)
3498 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003499 return all;
3500 err:
3501 Py_XDECREF(single);
3502 Py_XDECREF(all);
Martin v. Löwis2548c732003-04-18 10:39:54 +00003503 Py_XDECREF(idna);
Martin v. Löwisf0b11d22001-11-07 08:31:03 +00003504 if (res0)
3505 freeaddrinfo(res0);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003506 return (PyObject *)NULL;
3507}
3508
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003509PyDoc_STRVAR(getaddrinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003510"getaddrinfo(host, port [, family, socktype, proto, flags])\n\
3511 -> list of (family, socktype, proto, canonname, sockaddr)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003512\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003513Resolve host and port into addrinfo struct.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003514
3515/* Python interface to getnameinfo(sa, flags). */
3516
3517/*ARGSUSED*/
3518static PyObject *
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003519socket_getnameinfo(PyObject *self, PyObject *args)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003520{
3521 PyObject *sa = (PyObject *)NULL;
3522 int flags;
3523 char *hostp;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003524 int port, flowinfo, scope_id;
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003525 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
3526 struct addrinfo hints, *res = NULL;
3527 int error;
3528 PyObject *ret = (PyObject *)NULL;
3529
3530 flags = flowinfo = scope_id = 0;
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003531 if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003532 return NULL;
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003533 if (!PyArg_ParseTuple(sa, "si|ii",
3534 &hostp, &port, &flowinfo, &scope_id))
Martin v. Löwis06b1d212001-11-02 23:34:52 +00003535 return NULL;
Marc-André Lemburg03d1b182001-07-31 18:05:33 +00003536 PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003537 memset(&hints, 0, sizeof(hints));
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00003538 hints.ai_family = AF_UNSPEC;
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00003539 hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
Just van Rossum1040d2c2003-05-09 07:53:18 +00003540 Py_BEGIN_ALLOW_THREADS
3541 ACQUIRE_GETADDRINFO_LOCK
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003542 error = getaddrinfo(hostp, pbuf, &hints, &res);
Just van Rossum1040d2c2003-05-09 07:53:18 +00003543 Py_END_ALLOW_THREADS
3544 RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003545 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003546 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003547 goto fail;
3548 }
3549 if (res->ai_next) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003550 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003551 "sockaddr resolved to multiple addresses");
3552 goto fail;
3553 }
3554 switch (res->ai_family) {
3555 case AF_INET:
3556 {
3557 char *t1;
3558 int t2;
3559 if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003560 PyErr_SetString(socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003561 "IPv4 sockaddr must be 2 tuple");
3562 goto fail;
3563 }
3564 break;
3565 }
Martin v. Löwis44ddbde2001-12-02 10:15:37 +00003566#ifdef ENABLE_IPV6
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003567 case AF_INET6:
3568 {
3569 struct sockaddr_in6 *sin6;
3570 sin6 = (struct sockaddr_in6 *)res->ai_addr;
3571 sin6->sin6_flowinfo = flowinfo;
3572 sin6->sin6_scope_id = scope_id;
3573 break;
3574 }
3575#endif
3576 }
3577 error = getnameinfo(res->ai_addr, res->ai_addrlen,
3578 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
3579 if (error) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003580 set_gaierror(error);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003581 goto fail;
3582 }
3583 ret = Py_BuildValue("ss", hbuf, pbuf);
3584
3585fail:
3586 if (res)
3587 freeaddrinfo(res);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003588 return ret;
3589}
3590
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003591PyDoc_STRVAR(getnameinfo_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003592"getnameinfo(sockaddr, flags) --> (host, port)\n\
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003593\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003594Get host and port for a sockaddr.");
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003595
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003596
3597/* Python API to getting and setting the default timeout value. */
3598
3599static PyObject *
3600socket_getdefaulttimeout(PyObject *self)
3601{
3602 if (defaulttimeout < 0.0) {
3603 Py_INCREF(Py_None);
3604 return Py_None;
3605 }
3606 else
3607 return PyFloat_FromDouble(defaulttimeout);
3608}
3609
3610PyDoc_STRVAR(getdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003611"getdefaulttimeout() -> timeout\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003612\n\
3613Returns the default timeout in floating seconds for new socket objects.\n\
3614A value of None indicates that new socket objects have no timeout.\n\
3615When the socket module is first imported, the default is None.");
3616
3617static PyObject *
3618socket_setdefaulttimeout(PyObject *self, PyObject *arg)
3619{
3620 double timeout;
3621
3622 if (arg == Py_None)
3623 timeout = -1.0;
3624 else {
3625 timeout = PyFloat_AsDouble(arg);
3626 if (timeout < 0.0) {
3627 if (!PyErr_Occurred())
3628 PyErr_SetString(PyExc_ValueError,
3629 "Timeout value out of range");
3630 return NULL;
3631 }
3632 }
3633
3634 defaulttimeout = timeout;
3635
3636 Py_INCREF(Py_None);
3637 return Py_None;
3638}
3639
3640PyDoc_STRVAR(setdefaulttimeout_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003641"setdefaulttimeout(timeout)\n\
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003642\n\
3643Set the default timeout in floating seconds for new socket objects.\n\
3644A value of None indicates that new socket objects have no timeout.\n\
3645When the socket module is first imported, the default is None.");
3646
3647
Guido van Rossum30a685f1991-06-27 15:51:29 +00003648/* List of functions exported by this module. */
3649
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003650static PyMethodDef socket_methods[] = {
3651 {"gethostbyname", socket_gethostbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003652 METH_VARARGS, gethostbyname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003653 {"gethostbyname_ex", socket_gethostbyname_ex,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003654 METH_VARARGS, ghbn_ex_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003655 {"gethostbyaddr", socket_gethostbyaddr,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003656 METH_VARARGS, gethostbyaddr_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003657 {"gethostname", socket_gethostname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003658 METH_VARARGS, gethostname_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003659 {"getservbyname", socket_getservbyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003660 METH_VARARGS, getservbyname_doc},
Barry Warsaw11b91a02004-06-28 00:50:43 +00003661 {"getservbyport", socket_getservbyport,
3662 METH_VARARGS, getservbyport_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003663 {"getprotobyname", socket_getprotobyname,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003664 METH_VARARGS,getprotobyname_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003665#ifndef NO_DUP
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003666 {"fromfd", socket_fromfd,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003667 METH_VARARGS, fromfd_doc},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003668#endif
Dave Cole331708b2004-08-09 04:51:41 +00003669#ifdef HAVE_SOCKETPAIR
3670 {"socketpair", socket_socketpair,
3671 METH_VARARGS, socketpair_doc},
3672#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003673 {"ntohs", socket_ntohs,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003674 METH_VARARGS, ntohs_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003675 {"ntohl", socket_ntohl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003676 METH_O, ntohl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003677 {"htons", socket_htons,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003678 METH_VARARGS, htons_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003679 {"htonl", socket_htonl,
Jeremy Hyltonc075e192002-07-25 16:01:12 +00003680 METH_O, htonl_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003681 {"inet_aton", socket_inet_aton,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003682 METH_VARARGS, inet_aton_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003683 {"inet_ntoa", socket_inet_ntoa,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +00003684 METH_VARARGS, inet_ntoa_doc},
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003685#ifdef HAVE_INET_PTON
3686 {"inet_pton", socket_inet_pton,
3687 METH_VARARGS, inet_pton_doc},
3688 {"inet_ntop", socket_inet_ntop,
3689 METH_VARARGS, inet_ntop_doc},
3690#endif
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003691 {"getaddrinfo", socket_getaddrinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003692 METH_VARARGS, getaddrinfo_doc},
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003693 {"getnameinfo", socket_getnameinfo,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003694 METH_VARARGS, getnameinfo_doc},
Guido van Rossum1693ba82002-07-18 21:11:26 +00003695 {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +00003696 METH_NOARGS, getdefaulttimeout_doc},
3697 {"setdefaulttimeout", socket_setdefaulttimeout,
3698 METH_O, setdefaulttimeout_doc},
Guido van Rossum30a685f1991-06-27 15:51:29 +00003699 {NULL, NULL} /* Sentinel */
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003700};
3701
Guido van Rossum30a685f1991-06-27 15:51:29 +00003702
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003703#ifdef RISCOS
3704#define OS_INIT_DEFINED
Guido van Rossumbe32c891996-06-20 16:25:29 +00003705
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003706static int
3707os_init(void)
3708{
3709 _kernel_swi_regs r;
3710
3711 r.r[0] = 0;
3712 _kernel_swi(0x43380, &r, &r);
3713 taskwindow = r.r[0];
3714
Martin v. Löwisa94568a2003-05-10 07:36:56 +00003715 return 1;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003716}
3717
3718#endif /* RISCOS */
3719
3720
3721#ifdef MS_WINDOWS
3722#define OS_INIT_DEFINED
3723
3724/* Additional initialization and cleanup for Windows */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003725
3726static void
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003727os_cleanup(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00003728{
3729 WSACleanup();
Guido van Rossumbe32c891996-06-20 16:25:29 +00003730}
3731
3732static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003733os_init(void)
Guido van Rossumbe32c891996-06-20 16:25:29 +00003734{
3735 WSADATA WSAData;
3736 int ret;
3737 char buf[100];
3738 ret = WSAStartup(0x0101, &WSAData);
3739 switch (ret) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003740 case 0: /* No error */
Mark Hammond14350ab2003-07-09 04:57:46 +00003741 Py_AtExit(os_cleanup);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003742 return 1; /* Success */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003743 case WSASYSNOTREADY:
3744 PyErr_SetString(PyExc_ImportError,
3745 "WSAStartup failed: network not ready");
3746 break;
3747 case WSAVERNOTSUPPORTED:
3748 case WSAEINVAL:
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003749 PyErr_SetString(
3750 PyExc_ImportError,
3751 "WSAStartup failed: requested version not supported");
Guido van Rossumbe32c891996-06-20 16:25:29 +00003752 break;
3753 default:
Tim Peters885d4572001-11-28 20:27:42 +00003754 PyOS_snprintf(buf, sizeof(buf),
3755 "WSAStartup failed: error code %d", ret);
Guido van Rossumbe32c891996-06-20 16:25:29 +00003756 PyErr_SetString(PyExc_ImportError, buf);
3757 break;
3758 }
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003759 return 0; /* Failure */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003760}
3761
Guido van Rossum8d665e61996-06-26 18:22:49 +00003762#endif /* MS_WINDOWS */
Guido van Rossumbe32c891996-06-20 16:25:29 +00003763
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003764
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003765#ifdef PYOS_OS2
3766#define OS_INIT_DEFINED
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003767
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003768/* Additional initialization for OS/2 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003769
3770static int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003771os_init(void)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003772{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003773#ifndef PYCC_GCC
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003774 char reason[64];
3775 int rc = sock_init();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003776
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003777 if (rc == 0) {
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003778 return 1; /* Success */
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003779 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003780
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003781 PyOS_snprintf(reason, sizeof(reason),
3782 "OS/2 TCP/IP Error# %d", sock_errno());
3783 PyErr_SetString(PyExc_ImportError, reason);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003784
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003785 return 0; /* Failure */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00003786#else
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003787 /* No need to initialise sockets with GCC/EMX */
3788 return 1; /* Success */
Andrew MacIntyreba43e872002-03-03 03:03:52 +00003789#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003790}
3791
3792#endif /* PYOS_OS2 */
3793
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003794
3795#ifndef OS_INIT_DEFINED
3796static int
3797os_init(void)
3798{
3799 return 1; /* Success */
3800}
3801#endif
3802
3803
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003804/* C API table - always add new things to the end for binary
3805 compatibility. */
3806static
3807PySocketModule_APIObject PySocketModuleAPI =
3808{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003809 &sock_type,
Brett Cannon06c34792004-03-23 23:16:54 +00003810 NULL
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003811};
3812
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003813
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003814/* Initialize the _socket module.
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003815
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003816 This module is actually called "_socket", and there's a wrapper
3817 "socket.py" which implements some additional functionality. On some
3818 platforms (e.g. Windows and OS/2), socket.py also implements a
3819 wrapper for the socket type that provides missing functionality such
3820 as makefile(), dup() and fromfd(). The import of "_socket" may fail
3821 with an ImportError exception if os-specific initialization fails.
3822 On Windows, this does WINSOCK initialization. When WINSOCK is
3823 initialized succesfully, a call to WSACleanup() is scheduled to be
3824 made at exit time.
3825*/
Guido van Rossum3eede5a2002-06-07 02:08:35 +00003826
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003827PyDoc_STRVAR(socket_doc,
Guido van Rossum22a97152002-08-08 20:37:08 +00003828"Implementation module for socket operations.\n\
3829\n\
3830See the socket module for documentation.");
Guido van Rossum82a5c661998-07-07 20:45:43 +00003831
Mark Hammondfe51c6d2002-08-02 02:27:13 +00003832PyMODINIT_FUNC
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003833init_socket(void)
Guido van Rossum6574b3e1991-06-25 21:36:08 +00003834{
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003835 PyObject *m, *has_ipv6;
Fred Drake4baedc12002-04-01 14:53:37 +00003836
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003837 if (!os_init())
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003838 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003839
3840 sock_type.ob_type = &PyType_Type;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003841 m = Py_InitModule3(PySocket_MODULE_NAME,
3842 socket_methods,
3843 socket_doc);
3844
3845 socket_error = PyErr_NewException("socket.error", NULL, NULL);
3846 if (socket_error == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003847 return;
Brett Cannon06c34792004-03-23 23:16:54 +00003848 PySocketModuleAPI.error = socket_error;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003849 Py_INCREF(socket_error);
3850 PyModule_AddObject(m, "error", socket_error);
3851 socket_herror = PyErr_NewException("socket.herror",
3852 socket_error, NULL);
3853 if (socket_herror == NULL)
3854 return;
3855 Py_INCREF(socket_herror);
3856 PyModule_AddObject(m, "herror", socket_herror);
3857 socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003858 NULL);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003859 if (socket_gaierror == NULL)
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003860 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003861 Py_INCREF(socket_gaierror);
3862 PyModule_AddObject(m, "gaierror", socket_gaierror);
Raymond Hettingeref7343c2003-06-29 03:08:05 +00003863 socket_timeout = PyErr_NewException("socket.timeout",
3864 socket_error, NULL);
3865 if (socket_timeout == NULL)
3866 return;
3867 Py_INCREF(socket_timeout);
3868 PyModule_AddObject(m, "timeout", socket_timeout);
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003869 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003870 if (PyModule_AddObject(m, "SocketType",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003871 (PyObject *)&sock_type) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003872 return;
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003873 Py_INCREF((PyObject *)&sock_type);
Fred Drake4baedc12002-04-01 14:53:37 +00003874 if (PyModule_AddObject(m, "socket",
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00003875 (PyObject *)&sock_type) != 0)
Guido van Rossum384ca9c2001-10-27 22:20:47 +00003876 return;
Guido van Rossum09be4091999-08-09 14:40:40 +00003877
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00003878#ifdef ENABLE_IPV6
3879 has_ipv6 = Py_True;
3880#else
3881 has_ipv6 = Py_False;
3882#endif
3883 Py_INCREF(has_ipv6);
3884 PyModule_AddObject(m, "has_ipv6", has_ipv6);
3885
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003886 /* Export C API */
Fred Drake4baedc12002-04-01 14:53:37 +00003887 if (PyModule_AddObject(m, PySocket_CAPI_NAME,
Marc-André Lemburga5d2b4c2002-02-16 18:23:30 +00003888 PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL)
3889 ) != 0)
3890 return;
3891
Guido van Rossum09be4091999-08-09 14:40:40 +00003892 /* Address families (we only support AF_INET and AF_UNIX) */
3893#ifdef AF_UNSPEC
Fred Drake4baedc12002-04-01 14:53:37 +00003894 PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
Guido van Rossum09be4091999-08-09 14:40:40 +00003895#endif
Fred Drake4baedc12002-04-01 14:53:37 +00003896 PyModule_AddIntConstant(m, "AF_INET", AF_INET);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003897#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003898 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00003899#endif /* AF_INET6 */
Andrew MacIntyred12dfbb2004-04-04 07:13:49 +00003900#if defined(AF_UNIX)
Fred Drake4baedc12002-04-01 14:53:37 +00003901 PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003902#endif /* AF_UNIX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003903#ifdef AF_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00003904 /* Amateur Radio AX.25 */
3905 PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003906#endif
3907#ifdef AF_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00003908 PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
Guido van Rossum09be4091999-08-09 14:40:40 +00003909#endif
3910#ifdef AF_APPLETALK
Fred Drake4baedc12002-04-01 14:53:37 +00003911 /* Appletalk DDP */
3912 PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00003913#endif
3914#ifdef AF_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00003915 /* Amateur radio NetROM */
3916 PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00003917#endif
3918#ifdef AF_BRIDGE
Fred Drake4baedc12002-04-01 14:53:37 +00003919 /* Multiprotocol bridge */
3920 PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003921#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00003922#ifdef AF_ATMPVC
3923 /* ATM PVCs */
3924 PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC);
3925#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00003926#ifdef AF_AAL5
Fred Drake4baedc12002-04-01 14:53:37 +00003927 /* Reserved for Werner's ATM */
3928 PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
Guido van Rossum09be4091999-08-09 14:40:40 +00003929#endif
3930#ifdef AF_X25
Fred Drake4baedc12002-04-01 14:53:37 +00003931 /* Reserved for X.25 project */
3932 PyModule_AddIntConstant(m, "AF_X25", AF_X25);
Guido van Rossum09be4091999-08-09 14:40:40 +00003933#endif
3934#ifdef AF_INET6
Fred Drake4baedc12002-04-01 14:53:37 +00003935 PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
Guido van Rossum09be4091999-08-09 14:40:40 +00003936#endif
3937#ifdef AF_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00003938 /* Amateur Radio X.25 PLP */
3939 PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00003940#endif
Martin v. Löwis81aec4b2004-07-19 17:01:20 +00003941#ifdef AF_DECnet
3942 /* Reserved for DECnet project */
3943 PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet);
3944#endif
3945#ifdef AF_NETBEUI
3946 /* Reserved for 802.2LLC project */
3947 PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI);
3948#endif
3949#ifdef AF_SECURITY
3950 /* Security callback pseudo AF */
3951 PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY);
3952#endif
3953#ifdef AF_KEY
3954 /* PF_KEY key management API */
3955 PyModule_AddIntConstant(m, "AF_KEY", AF_KEY);
3956#endif
3957#ifdef AF_NETLINK
3958 /* */
3959 PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);
3960#endif
3961#ifdef AF_ROUTE
3962 /* Alias to emulate 4.4BSD */
3963 PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE);
3964#endif
3965#ifdef AF_ASH
3966 /* Ash */
3967 PyModule_AddIntConstant(m, "AF_ASH", AF_ASH);
3968#endif
3969#ifdef AF_ECONET
3970 /* Acorn Econet */
3971 PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET);
3972#endif
3973#ifdef AF_ATMSVC
3974 /* ATM SVCs */
3975 PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC);
3976#endif
3977#ifdef AF_SNA
3978 /* Linux SNA Project (nutters!) */
3979 PyModule_AddIntConstant(m, "AF_SNA", AF_SNA);
3980#endif
3981#ifdef AF_IRDA
3982 /* IRDA sockets */
3983 PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA);
3984#endif
3985#ifdef AF_PPPOX
3986 /* PPPoX sockets */
3987 PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX);
3988#endif
3989#ifdef AF_WANPIPE
3990 /* Wanpipe API Sockets */
3991 PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE);
3992#endif
3993#ifdef AF_LLC
3994 /* Linux LLC */
3995 PyModule_AddIntConstant(m, "AF_LLC", AF_LLC);
3996#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00003997
Hye-Shik Chang81268602004-02-02 06:05:24 +00003998#ifdef USE_BLUETOOTH
Martin v. Löwis12af0482004-01-31 12:34:17 +00003999 PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);
4000 PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);
Hye-Shik Chang81268602004-02-02 06:05:24 +00004001#if !defined(__FreeBSD__)
Martin v. Löwis12af0482004-01-31 12:34:17 +00004002 PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);
Hye-Shik Chang81268602004-02-02 06:05:24 +00004003#endif
Martin v. Löwis12af0482004-01-31 12:34:17 +00004004 PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);
Martin v. Löwis558d9bf2004-06-03 09:24:42 +00004005 PyModule_AddObject(m, "BDADDR_ANY", Py_BuildValue("s", "00:00:00:00:00:00"));
4006 PyModule_AddObject(m, "BDADDR_LOCAL", Py_BuildValue("s", "00:00:00:FF:FF:FF"));
Martin v. Löwis12af0482004-01-31 12:34:17 +00004007#endif
4008
Martin v. Löwis1ba3fd52001-08-10 20:29:40 +00004009#ifdef HAVE_NETPACKET_PACKET_H
Fred Drake4baedc12002-04-01 14:53:37 +00004010 PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
4011 PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
4012 PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
4013 PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
4014 PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
4015 PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
4016 PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
4017 PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
4018 PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
Guido van Rossum48a680c2001-03-02 06:34:14 +00004019#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004020
4021 /* Socket types */
Fred Drake4baedc12002-04-01 14:53:37 +00004022 PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
4023 PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004024#ifndef __BEOS__
4025/* We have incomplete socket support. */
Fred Drake4baedc12002-04-01 14:53:37 +00004026 PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
4027 PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00004028#if defined(SOCK_RDM)
Fred Drake4baedc12002-04-01 14:53:37 +00004029 PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
Guido van Rossumbcc20741998-08-04 22:53:56 +00004030#endif
Martin v. Löwiscf8f47e2002-12-11 13:10:57 +00004031#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004032
4033#ifdef SO_DEBUG
Fred Drake4baedc12002-04-01 14:53:37 +00004034 PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004035#endif
4036#ifdef SO_ACCEPTCONN
Fred Drake4baedc12002-04-01 14:53:37 +00004037 PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004038#endif
4039#ifdef SO_REUSEADDR
Fred Drake4baedc12002-04-01 14:53:37 +00004040 PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004041#endif
Andrew M. Kuchling42851ab2004-07-10 14:19:21 +00004042#ifdef SO_EXCLUSIVEADDRUSE
4043 PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE);
4044#endif
4045
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004046#ifdef SO_KEEPALIVE
Fred Drake4baedc12002-04-01 14:53:37 +00004047 PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004048#endif
4049#ifdef SO_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00004050 PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004051#endif
4052#ifdef SO_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00004053 PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004054#endif
4055#ifdef SO_USELOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00004056 PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004057#endif
4058#ifdef SO_LINGER
Fred Drake4baedc12002-04-01 14:53:37 +00004059 PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004060#endif
4061#ifdef SO_OOBINLINE
Fred Drake4baedc12002-04-01 14:53:37 +00004062 PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004063#endif
4064#ifdef SO_REUSEPORT
Fred Drake4baedc12002-04-01 14:53:37 +00004065 PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004066#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004067#ifdef SO_SNDBUF
Fred Drake4baedc12002-04-01 14:53:37 +00004068 PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004069#endif
4070#ifdef SO_RCVBUF
Fred Drake4baedc12002-04-01 14:53:37 +00004071 PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004072#endif
4073#ifdef SO_SNDLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00004074 PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004075#endif
4076#ifdef SO_RCVLOWAT
Fred Drake4baedc12002-04-01 14:53:37 +00004077 PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004078#endif
4079#ifdef SO_SNDTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00004080 PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004081#endif
4082#ifdef SO_RCVTIMEO
Fred Drake4baedc12002-04-01 14:53:37 +00004083 PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004084#endif
4085#ifdef SO_ERROR
Fred Drake4baedc12002-04-01 14:53:37 +00004086 PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004087#endif
4088#ifdef SO_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004089 PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004090#endif
4091
4092 /* Maximum number of connections for "listen" */
4093#ifdef SOMAXCONN
Fred Drake4baedc12002-04-01 14:53:37 +00004094 PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004095#else
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004096 PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004097#endif
4098
4099 /* Flags for send, recv */
4100#ifdef MSG_OOB
Fred Drake4baedc12002-04-01 14:53:37 +00004101 PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004102#endif
4103#ifdef MSG_PEEK
Fred Drake4baedc12002-04-01 14:53:37 +00004104 PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004105#endif
4106#ifdef MSG_DONTROUTE
Fred Drake4baedc12002-04-01 14:53:37 +00004107 PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004108#endif
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00004109#ifdef MSG_DONTWAIT
Fred Drake4baedc12002-04-01 14:53:37 +00004110 PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
Guido van Rossum2c8bcb82000-04-25 21:34:53 +00004111#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004112#ifdef MSG_EOR
Fred Drake4baedc12002-04-01 14:53:37 +00004113 PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004114#endif
4115#ifdef MSG_TRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00004116 PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004117#endif
4118#ifdef MSG_CTRUNC
Fred Drake4baedc12002-04-01 14:53:37 +00004119 PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004120#endif
4121#ifdef MSG_WAITALL
Fred Drake4baedc12002-04-01 14:53:37 +00004122 PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004123#endif
4124#ifdef MSG_BTAG
Fred Drake4baedc12002-04-01 14:53:37 +00004125 PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004126#endif
4127#ifdef MSG_ETAG
Fred Drake4baedc12002-04-01 14:53:37 +00004128 PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004129#endif
4130
4131 /* Protocol level and numbers, usable for [gs]etsockopt */
4132#ifdef SOL_SOCKET
Fred Drake4baedc12002-04-01 14:53:37 +00004133 PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004134#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004135#ifdef SOL_IP
Fred Drake4baedc12002-04-01 14:53:37 +00004136 PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004137#else
Fred Drake4baedc12002-04-01 14:53:37 +00004138 PyModule_AddIntConstant(m, "SOL_IP", 0);
Guido van Rossum09be4091999-08-09 14:40:40 +00004139#endif
4140#ifdef SOL_IPX
Fred Drake4baedc12002-04-01 14:53:37 +00004141 PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
Guido van Rossum09be4091999-08-09 14:40:40 +00004142#endif
4143#ifdef SOL_AX25
Fred Drake4baedc12002-04-01 14:53:37 +00004144 PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
Guido van Rossum09be4091999-08-09 14:40:40 +00004145#endif
4146#ifdef SOL_ATALK
Fred Drake4baedc12002-04-01 14:53:37 +00004147 PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
Guido van Rossum09be4091999-08-09 14:40:40 +00004148#endif
4149#ifdef SOL_NETROM
Fred Drake4baedc12002-04-01 14:53:37 +00004150 PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
Guido van Rossum09be4091999-08-09 14:40:40 +00004151#endif
4152#ifdef SOL_ROSE
Fred Drake4baedc12002-04-01 14:53:37 +00004153 PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004154#endif
4155#ifdef SOL_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00004156 PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004157#else
Fred Drake4baedc12002-04-01 14:53:37 +00004158 PyModule_AddIntConstant(m, "SOL_TCP", 6);
Guido van Rossum09be4091999-08-09 14:40:40 +00004159#endif
4160#ifdef SOL_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00004161 PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004162#else
Fred Drake4baedc12002-04-01 14:53:37 +00004163 PyModule_AddIntConstant(m, "SOL_UDP", 17);
Guido van Rossum09be4091999-08-09 14:40:40 +00004164#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004165#ifdef IPPROTO_IP
Fred Drake4baedc12002-04-01 14:53:37 +00004166 PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
Guido van Rossum578de301998-05-28 20:18:18 +00004167#else
Fred Drake4baedc12002-04-01 14:53:37 +00004168 PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004169#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004170#ifdef IPPROTO_HOPOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004171 PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004172#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004173#ifdef IPPROTO_ICMP
Fred Drake4baedc12002-04-01 14:53:37 +00004174 PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
Guido van Rossum578de301998-05-28 20:18:18 +00004175#else
Fred Drake4baedc12002-04-01 14:53:37 +00004176 PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004177#endif
4178#ifdef IPPROTO_IGMP
Fred Drake4baedc12002-04-01 14:53:37 +00004179 PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004180#endif
4181#ifdef IPPROTO_GGP
Fred Drake4baedc12002-04-01 14:53:37 +00004182 PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004183#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004184#ifdef IPPROTO_IPV4
Fred Drake4baedc12002-04-01 14:53:37 +00004185 PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004186#endif
Martin v. Löwisa0f17342003-10-03 13:56:20 +00004187#ifdef IPPROTO_IPV6
4188 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
4189#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004190#ifdef IPPROTO_IPIP
Fred Drake4baedc12002-04-01 14:53:37 +00004191 PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004192#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004193#ifdef IPPROTO_TCP
Fred Drake4baedc12002-04-01 14:53:37 +00004194 PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
Guido van Rossum578de301998-05-28 20:18:18 +00004195#else
Fred Drake4baedc12002-04-01 14:53:37 +00004196 PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004197#endif
4198#ifdef IPPROTO_EGP
Fred Drake4baedc12002-04-01 14:53:37 +00004199 PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004200#endif
4201#ifdef IPPROTO_PUP
Fred Drake4baedc12002-04-01 14:53:37 +00004202 PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004203#endif
4204#ifdef IPPROTO_UDP
Fred Drake4baedc12002-04-01 14:53:37 +00004205 PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
Guido van Rossum578de301998-05-28 20:18:18 +00004206#else
Fred Drake4baedc12002-04-01 14:53:37 +00004207 PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004208#endif
4209#ifdef IPPROTO_IDP
Fred Drake4baedc12002-04-01 14:53:37 +00004210 PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004211#endif
4212#ifdef IPPROTO_HELLO
Fred Drake4baedc12002-04-01 14:53:37 +00004213 PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004214#endif
4215#ifdef IPPROTO_ND
Fred Drake4baedc12002-04-01 14:53:37 +00004216 PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004217#endif
4218#ifdef IPPROTO_TP
Fred Drake4baedc12002-04-01 14:53:37 +00004219 PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004220#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004221#ifdef IPPROTO_IPV6
Fred Drake4baedc12002-04-01 14:53:37 +00004222 PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004223#endif
4224#ifdef IPPROTO_ROUTING
Fred Drake4baedc12002-04-01 14:53:37 +00004225 PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004226#endif
4227#ifdef IPPROTO_FRAGMENT
Fred Drake4baedc12002-04-01 14:53:37 +00004228 PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004229#endif
4230#ifdef IPPROTO_RSVP
Fred Drake4baedc12002-04-01 14:53:37 +00004231 PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004232#endif
4233#ifdef IPPROTO_GRE
Fred Drake4baedc12002-04-01 14:53:37 +00004234 PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004235#endif
4236#ifdef IPPROTO_ESP
Fred Drake4baedc12002-04-01 14:53:37 +00004237 PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004238#endif
4239#ifdef IPPROTO_AH
Fred Drake4baedc12002-04-01 14:53:37 +00004240 PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004241#endif
4242#ifdef IPPROTO_MOBILE
Fred Drake4baedc12002-04-01 14:53:37 +00004243 PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004244#endif
4245#ifdef IPPROTO_ICMPV6
Fred Drake4baedc12002-04-01 14:53:37 +00004246 PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004247#endif
4248#ifdef IPPROTO_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00004249 PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004250#endif
4251#ifdef IPPROTO_DSTOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004252 PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004253#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004254#ifdef IPPROTO_XTP
Fred Drake4baedc12002-04-01 14:53:37 +00004255 PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004256#endif
4257#ifdef IPPROTO_EON
Fred Drake4baedc12002-04-01 14:53:37 +00004258 PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004259#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004260#ifdef IPPROTO_PIM
Fred Drake4baedc12002-04-01 14:53:37 +00004261 PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004262#endif
4263#ifdef IPPROTO_IPCOMP
Fred Drake4baedc12002-04-01 14:53:37 +00004264 PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004265#endif
4266#ifdef IPPROTO_VRRP
Fred Drake4baedc12002-04-01 14:53:37 +00004267 PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004268#endif
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004269#ifdef IPPROTO_BIP
Fred Drake4baedc12002-04-01 14:53:37 +00004270 PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004271#endif
4272/**/
4273#ifdef IPPROTO_RAW
Fred Drake4baedc12002-04-01 14:53:37 +00004274 PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
Guido van Rossum578de301998-05-28 20:18:18 +00004275#else
Fred Drake4baedc12002-04-01 14:53:37 +00004276 PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004277#endif
4278#ifdef IPPROTO_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00004279 PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004280#endif
4281
4282 /* Some port configuration */
4283#ifdef IPPORT_RESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00004284 PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004285#else
Fred Drake4baedc12002-04-01 14:53:37 +00004286 PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004287#endif
4288#ifdef IPPORT_USERRESERVED
Fred Drake4baedc12002-04-01 14:53:37 +00004289 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004290#else
Fred Drake4baedc12002-04-01 14:53:37 +00004291 PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004292#endif
4293
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004294 /* Some reserved IP v.4 addresses */
4295#ifdef INADDR_ANY
Fred Drake4baedc12002-04-01 14:53:37 +00004296 PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004297#else
Fred Drake4baedc12002-04-01 14:53:37 +00004298 PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004299#endif
4300#ifdef INADDR_BROADCAST
Fred Drake4baedc12002-04-01 14:53:37 +00004301 PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004302#else
Fred Drake4baedc12002-04-01 14:53:37 +00004303 PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004304#endif
4305#ifdef INADDR_LOOPBACK
Fred Drake4baedc12002-04-01 14:53:37 +00004306 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004307#else
Fred Drake4baedc12002-04-01 14:53:37 +00004308 PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004309#endif
4310#ifdef INADDR_UNSPEC_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004311 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004312#else
Fred Drake4baedc12002-04-01 14:53:37 +00004313 PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004314#endif
4315#ifdef INADDR_ALLHOSTS_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004316 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
4317 INADDR_ALLHOSTS_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004318#else
Fred Drake4baedc12002-04-01 14:53:37 +00004319 PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004320#endif
4321#ifdef INADDR_MAX_LOCAL_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004322 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
4323 INADDR_MAX_LOCAL_GROUP);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004324#else
Fred Drake4baedc12002-04-01 14:53:37 +00004325 PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004326#endif
4327#ifdef INADDR_NONE
Fred Drake4baedc12002-04-01 14:53:37 +00004328 PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004329#else
Fred Drake4baedc12002-04-01 14:53:37 +00004330 PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
Guido van Rossum5f05eb41995-02-17 15:11:07 +00004331#endif
4332
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004333 /* IPv4 [gs]etsockopt options */
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004334#ifdef IP_OPTIONS
Fred Drake4baedc12002-04-01 14:53:37 +00004335 PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004336#endif
4337#ifdef IP_HDRINCL
Fred Drake4baedc12002-04-01 14:53:37 +00004338 PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004339#endif
4340#ifdef IP_TOS
Fred Drake4baedc12002-04-01 14:53:37 +00004341 PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004342#endif
4343#ifdef IP_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004344 PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004345#endif
4346#ifdef IP_RECVOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004347 PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004348#endif
4349#ifdef IP_RECVRETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004350 PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004351#endif
4352#ifdef IP_RECVDSTADDR
Fred Drake4baedc12002-04-01 14:53:37 +00004353 PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004354#endif
4355#ifdef IP_RETOPTS
Fred Drake4baedc12002-04-01 14:53:37 +00004356 PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004357#endif
4358#ifdef IP_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00004359 PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004360#endif
4361#ifdef IP_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004362 PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004363#endif
4364#ifdef IP_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004365 PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004366#endif
4367#ifdef IP_ADD_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00004368 PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004369#endif
4370#ifdef IP_DROP_MEMBERSHIP
Fred Drake4baedc12002-04-01 14:53:37 +00004371 PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
Guido van Rossum3fdf58b1995-02-07 15:39:52 +00004372#endif
Guido van Rossum09be4091999-08-09 14:40:40 +00004373#ifdef IP_DEFAULT_MULTICAST_TTL
Fred Drake4baedc12002-04-01 14:53:37 +00004374 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
4375 IP_DEFAULT_MULTICAST_TTL);
Guido van Rossum09be4091999-08-09 14:40:40 +00004376#endif
4377#ifdef IP_DEFAULT_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004378 PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
4379 IP_DEFAULT_MULTICAST_LOOP);
Guido van Rossum09be4091999-08-09 14:40:40 +00004380#endif
4381#ifdef IP_MAX_MEMBERSHIPS
Fred Drake4baedc12002-04-01 14:53:37 +00004382 PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
Guido van Rossum09be4091999-08-09 14:40:40 +00004383#endif
4384
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004385 /* IPv6 [gs]etsockopt options, defined in RFC2553 */
4386#ifdef IPV6_JOIN_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004387 PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004388#endif
4389#ifdef IPV6_LEAVE_GROUP
Fred Drake4baedc12002-04-01 14:53:37 +00004390 PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004391#endif
4392#ifdef IPV6_MULTICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00004393 PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004394#endif
4395#ifdef IPV6_MULTICAST_IF
Fred Drake4baedc12002-04-01 14:53:37 +00004396 PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004397#endif
4398#ifdef IPV6_MULTICAST_LOOP
Fred Drake4baedc12002-04-01 14:53:37 +00004399 PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004400#endif
4401#ifdef IPV6_UNICAST_HOPS
Fred Drake4baedc12002-04-01 14:53:37 +00004402 PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004403#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004404 /* Additional IPV6 socket options, defined in RFC 3493 */
4405#ifdef IPV6_V6ONLY
4406 PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);
4407#endif
4408 /* Advanced IPV6 socket options, from RFC 3542 */
4409#ifdef IPV6_CHECKSUM
4410 PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);
4411#endif
4412#ifdef IPV6_DONTFRAG
4413 PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);
4414#endif
4415#ifdef IPV6_DSTOPTS
4416 PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);
4417#endif
4418#ifdef IPV6_HOPLIMIT
4419 PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);
4420#endif
4421#ifdef IPV6_HOPOPTS
4422 PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);
4423#endif
4424#ifdef IPV6_NEXTHOP
4425 PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);
4426#endif
4427#ifdef IPV6_PATHMTU
4428 PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);
4429#endif
4430#ifdef IPV6_PKTINFO
4431 PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);
4432#endif
4433#ifdef IPV6_RECVDSTOPTS
4434 PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);
4435#endif
4436#ifdef IPV6_RECVHOPLIMIT
4437 PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);
4438#endif
4439#ifdef IPV6_RECVHOPOPTS
4440 PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);
4441#endif
4442#ifdef IPV6_RECVPKTINFO
4443 PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);
4444#endif
4445#ifdef IPV6_RECVRTHDR
4446 PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);
4447#endif
4448#ifdef IPV6_RECVTCLASS
4449 PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);
4450#endif
4451#ifdef IPV6_RTHDR
4452 PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);
4453#endif
4454#ifdef IPV6_RTHDRDSTOPTS
4455 PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);
4456#endif
4457#ifdef IPV6_RTHDR_TYPE_0
4458 PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);
4459#endif
4460#ifdef IPV6_RECVPATHMTU
4461 PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);
4462#endif
4463#ifdef IPV6_TCLASS
4464 PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);
4465#endif
4466#ifdef IPV6_USE_MIN_MTU
4467 PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);
4468#endif
Martin v. Löwisbcf4b352001-08-04 22:37:23 +00004469
Guido van Rossum09be4091999-08-09 14:40:40 +00004470 /* TCP options */
4471#ifdef TCP_NODELAY
Fred Drake4baedc12002-04-01 14:53:37 +00004472 PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
Guido van Rossum09be4091999-08-09 14:40:40 +00004473#endif
4474#ifdef TCP_MAXSEG
Fred Drake4baedc12002-04-01 14:53:37 +00004475 PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
Guido van Rossum09be4091999-08-09 14:40:40 +00004476#endif
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004477#ifdef TCP_CORK
Fred Drake4baedc12002-04-01 14:53:37 +00004478 PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004479#endif
4480#ifdef TCP_KEEPIDLE
Fred Drake4baedc12002-04-01 14:53:37 +00004481 PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004482#endif
4483#ifdef TCP_KEEPINTVL
Fred Drake4baedc12002-04-01 14:53:37 +00004484 PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004485#endif
4486#ifdef TCP_KEEPCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004487 PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004488#endif
4489#ifdef TCP_SYNCNT
Fred Drake4baedc12002-04-01 14:53:37 +00004490 PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004491#endif
4492#ifdef TCP_LINGER2
Fred Drake4baedc12002-04-01 14:53:37 +00004493 PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004494#endif
4495#ifdef TCP_DEFER_ACCEPT
Fred Drake4baedc12002-04-01 14:53:37 +00004496 PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004497#endif
4498#ifdef TCP_WINDOW_CLAMP
Fred Drake4baedc12002-04-01 14:53:37 +00004499 PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004500#endif
4501#ifdef TCP_INFO
Fred Drake4baedc12002-04-01 14:53:37 +00004502 PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004503#endif
4504#ifdef TCP_QUICKACK
Fred Drake4baedc12002-04-01 14:53:37 +00004505 PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
Martin v. Löwis3cde2cb2001-12-22 15:05:32 +00004506#endif
4507
Guido van Rossum09be4091999-08-09 14:40:40 +00004508
4509 /* IPX options */
4510#ifdef IPX_TYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004511 PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
Guido van Rossum09be4091999-08-09 14:40:40 +00004512#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004513
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004514 /* get{addr,name}info parameters */
4515#ifdef EAI_ADDRFAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004516 PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004517#endif
4518#ifdef EAI_AGAIN
Fred Drake4baedc12002-04-01 14:53:37 +00004519 PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004520#endif
4521#ifdef EAI_BADFLAGS
Fred Drake4baedc12002-04-01 14:53:37 +00004522 PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004523#endif
4524#ifdef EAI_FAIL
Fred Drake4baedc12002-04-01 14:53:37 +00004525 PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004526#endif
4527#ifdef EAI_FAMILY
Fred Drake4baedc12002-04-01 14:53:37 +00004528 PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004529#endif
4530#ifdef EAI_MEMORY
Fred Drake4baedc12002-04-01 14:53:37 +00004531 PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004532#endif
4533#ifdef EAI_NODATA
Fred Drake4baedc12002-04-01 14:53:37 +00004534 PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004535#endif
4536#ifdef EAI_NONAME
Fred Drake4baedc12002-04-01 14:53:37 +00004537 PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004538#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004539#ifdef EAI_OVERFLOW
4540 PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);
4541#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004542#ifdef EAI_SERVICE
Fred Drake4baedc12002-04-01 14:53:37 +00004543 PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004544#endif
4545#ifdef EAI_SOCKTYPE
Fred Drake4baedc12002-04-01 14:53:37 +00004546 PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004547#endif
4548#ifdef EAI_SYSTEM
Fred Drake4baedc12002-04-01 14:53:37 +00004549 PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004550#endif
4551#ifdef EAI_BADHINTS
Fred Drake4baedc12002-04-01 14:53:37 +00004552 PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004553#endif
4554#ifdef EAI_PROTOCOL
Fred Drake4baedc12002-04-01 14:53:37 +00004555 PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004556#endif
4557#ifdef EAI_MAX
Fred Drake4baedc12002-04-01 14:53:37 +00004558 PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004559#endif
4560#ifdef AI_PASSIVE
Fred Drake4baedc12002-04-01 14:53:37 +00004561 PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004562#endif
4563#ifdef AI_CANONNAME
Fred Drake4baedc12002-04-01 14:53:37 +00004564 PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004565#endif
4566#ifdef AI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004567 PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004568#endif
Martin v. Löwisda91d022003-12-30 11:14:01 +00004569#ifdef AI_NUMERICSERV
4570 PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);
4571#endif
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004572#ifdef AI_MASK
Fred Drake4baedc12002-04-01 14:53:37 +00004573 PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004574#endif
4575#ifdef AI_ALL
Fred Drake4baedc12002-04-01 14:53:37 +00004576 PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004577#endif
4578#ifdef AI_V4MAPPED_CFG
Fred Drake4baedc12002-04-01 14:53:37 +00004579 PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004580#endif
4581#ifdef AI_ADDRCONFIG
Fred Drake4baedc12002-04-01 14:53:37 +00004582 PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004583#endif
4584#ifdef AI_V4MAPPED
Fred Drake4baedc12002-04-01 14:53:37 +00004585 PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004586#endif
4587#ifdef AI_DEFAULT
Fred Drake4baedc12002-04-01 14:53:37 +00004588 PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004589#endif
4590#ifdef NI_MAXHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004591 PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004592#endif
4593#ifdef NI_MAXSERV
Fred Drake4baedc12002-04-01 14:53:37 +00004594 PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004595#endif
4596#ifdef NI_NOFQDN
Fred Drake4baedc12002-04-01 14:53:37 +00004597 PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004598#endif
4599#ifdef NI_NUMERICHOST
Fred Drake4baedc12002-04-01 14:53:37 +00004600 PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004601#endif
4602#ifdef NI_NAMEREQD
Fred Drake4baedc12002-04-01 14:53:37 +00004603 PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004604#endif
4605#ifdef NI_NUMERICSERV
Fred Drake4baedc12002-04-01 14:53:37 +00004606 PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004607#endif
4608#ifdef NI_DGRAM
Fred Drake4baedc12002-04-01 14:53:37 +00004609 PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
Martin v. Löwis2d8d4272001-07-21 18:05:31 +00004610#endif
4611
Martin v. Löwis94681fc2003-11-27 19:40:22 +00004612 /* shutdown() parameters */
4613#ifdef SHUT_RD
4614 PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);
4615#elif defined(SD_RECEIVE)
4616 PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
4617#else
4618 PyModule_AddIntConstant(m, "SHUT_RD", 0);
4619#endif
4620#ifdef SHUT_WR
4621 PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);
4622#elif defined(SD_SEND)
4623 PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
4624#else
4625 PyModule_AddIntConstant(m, "SHUT_WR", 1);
4626#endif
4627#ifdef SHUT_RDWR
4628 PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);
4629#elif defined(SD_BOTH)
4630 PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
4631#else
4632 PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
4633#endif
4634
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004635 /* Initialize gethostbyname lock */
Just van Rossum1040d2c2003-05-09 07:53:18 +00004636#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
4637 netdb_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +00004638#endif
Guido van Rossum6574b3e1991-06-25 21:36:08 +00004639}
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004640
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004641
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004642#ifndef HAVE_INET_PTON
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004643
4644/* Simplistic emulation code for inet_pton that only works for IPv4 */
Guido van Rossum47dfa4a2003-04-25 05:48:32 +00004645/* These are not exposed because they do not set errno properly */
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004646
Guido van Rossum3eede5a2002-06-07 02:08:35 +00004647int
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004648inet_pton(int af, const char *src, void *dst)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004649{
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004650 if (af == AF_INET) {
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004651 long packed_addr;
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004652 packed_addr = inet_addr(src);
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004653 if (packed_addr == INADDR_NONE)
4654 return 0;
4655 memcpy(dst, &packed_addr, 4);
4656 return 1;
4657 }
4658 /* Should set errno to EAFNOSUPPORT */
4659 return -1;
4660}
4661
Martin v. Löwisc925b1532001-07-21 09:42:15 +00004662const char *
4663inet_ntop(int af, const void *src, char *dst, socklen_t size)
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004664{
4665 if (af == AF_INET) {
4666 struct in_addr packed_addr;
4667 if (size < 16)
4668 /* Should set errno to ENOSPC. */
4669 return NULL;
4670 memcpy(&packed_addr, src, sizeof(packed_addr));
4671 return strncpy(dst, inet_ntoa(packed_addr), size);
4672 }
4673 /* Should set errno to EAFNOSUPPORT */
4674 return NULL;
4675}
Guido van Rossumc4fcfa32002-06-07 03:19:37 +00004676
Martin v. Löwisb9ab1592001-06-24 21:18:26 +00004677#endif